Changeset 27

Show
Ignore:
Timestamp:
04/11/06 12:15:53 (3 years ago)
Author:
jordi
Message:

The user can now open an encrypted pdf file, as the presenter will ask for the password three time before to show an error, or until the user gives the cancels out.

Location:
trunk
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/IMainView.h

    r26 r27  
    2929        public: 
    3030            virtual ~IMainView () {} 
    31  
     31             
    3232            virtual gchar *openFileDialog () = 0; 
     33            virtual gchar *promptPasswordDialog () = 0; 
    3334            virtual void sensitiveGoToFirstPage (gboolean sensitive) = 0; 
    3435            virtual void sensitiveGoToLastPage (gboolean sensitive) = 0; 
  • trunk/src/MainPter.cxx

    r26 r27  
    153153            // state. 
    154154            setInitialState (); 
    155             g_free (fileName); 
    156155        } 
    157156        else 
    158         { 
     157        {             
    159158            // We got an error, but also can be that the file is encrypted. 
    160159            if ( DocumentErrorEncrypted == error->code ) 
    161160            { 
    162                 // TODO 
     161                // Now we got an encrypted file. 
     162                // Prompt for the password up to three time, until the password 
     163                // is correct or the user cancels the password dialog. 
     164                int passwordTries = 0; 
     165                gchar *password = NULL; 
     166                gboolean loaded = FALSE; 
     167                do  
     168                { 
     169                    g_free (password); 
     170                    password = getView ().promptPasswordDialog (); 
     171                    loaded = m_Document->loadFile (fileName, password, NULL); 
     172                } while ( 3 > ++passwordTries &&  
     173                          NULL != password &&  
     174                          !loaded); 
     175 
     176                // We are go, just check if we could load it or show the 
     177                // error. 
     178                if ( loaded ) 
     179                { 
     180                    // Phew, finally loaded. Set the initial state. 
     181                    setInitialState (); 
     182                } 
     183                else if ( NULL != password ) 
     184                { 
     185                    // We didn't get the correct password, but the user 
     186                    // tried, because the password is not NULL. 
     187                    getView ().showErrorMessage (_("Error Loading File"), 
     188                            _("The password you have supplier is not a" 
     189                              "valid password for this file.")); 
     190                    g_free (password); 
     191                } 
    163192            } 
    164193            else 
     
    171200            g_free (error); 
    172201        } 
     202        g_free (fileName); 
    173203    } 
    174204} 
  • trunk/tests/DumbDocument.cxx

    r26 r27  
    3030    m_Loaded = FALSE; 
    3131    m_OpenError = DocumentErrorNone; 
     32    m_Password = NULL; 
    3233} 
    3334 
    3435DumbDocument::~DumbDocument () 
    3536{ 
     37    g_free (m_Password); 
    3638} 
    3739 
     
    4749{ 
    4850    if ( DocumentErrorNone == m_OpenError ) 
     51    { 
     52        setFileName (filename); 
     53        m_Loaded = TRUE; 
     54    } 
     55    else if ( DocumentErrorEncrypted == m_OpenError && 
     56              password != NULL &&  
     57              0 == g_ascii_strcasecmp (password, m_Password)) 
    4958    { 
    5059        setFileName (filename); 
     
    8089    m_OpenError = error; 
    8190} 
     91 
     92void 
     93DumbDocument::setPassword (const gchar *password) 
     94{ 
     95    g_free (m_Password); 
     96    m_Password = g_strdup (password); 
     97} 
  • trunk/tests/DumbDocument.h

    r26 r27  
    3636            // Test functions. 
    3737            void setOpenError (DocumentError error); 
     38            void setPassword (const gchar *password); 
    3839 
    3940        private: 
    4041            gboolean m_Loaded; 
    4142            DocumentError m_OpenError; 
     43            gchar *m_Password; 
    4244    }; 
    4345} 
  • trunk/tests/DumbMainView.cxx

    r26 r27  
    3030    m_DocumentPage = NULL; 
    3131    m_OpenFileName = g_strdup (""); 
     32    m_Password = NULL; 
    3233    m_SensitiveGoToFirstPage = TRUE; 
    3334    m_SensitiveGoToLastPage = TRUE; 
     
    4243    m_ShownError = FALSE; 
    4344    m_Title = g_strdup (""); 
     45    m_TimesShownPassword = 0; 
    4446} 
    4547 
     
    4749{ 
    4850    g_free (m_OpenFileName); 
     51    g_free (m_Password); 
    4952    g_free (m_Title); 
    5053} 
     
    5659} 
    5760 
     61gchar * 
     62DumbMainView::promptPasswordDialog (void) 
     63{ 
     64    m_TimesShownPassword++; 
     65    return g_strdup (m_Password); 
     66} 
     67 
    5868void 
    5969DumbMainView::sensitiveGoToFirstPage (gboolean sensitive) 
     
    139149// Test Methods 
    140150//////////////////////////////////////////////////////////////// 
     151 
     152gint 
     153DumbMainView::countTimesShownPasswordPrompt () 
     154{ 
     155    return m_TimesShownPassword; 
     156} 
    141157 
    142158gboolean 
     
    220236{ 
    221237    g_free (m_OpenFileName); 
    222     m_OpenFileName = NULL; 
    223     if ( NULL != fileName ) 
    224     { 
    225         m_OpenFileName = g_strdup (fileName); 
    226     } 
     238    m_OpenFileName = g_strdup (fileName); 
     239} 
     240 
     241void 
     242DumbMainView::setPassword (const gchar *password) 
     243{ 
     244    g_free (m_Password); 
     245    m_Password = g_strdup (password); 
     246    m_TimesShownPassword = 0; 
    227247} 
    228248 
  • trunk/tests/DumbMainView.h

    r26 r27  
    2929 
    3030            gchar *openFileDialog (void); 
     31            gchar *promptPasswordDialog (void); 
    3132            void sensitiveGoToFirstPage (gboolean sensitive); 
    3233            void sensitiveGoToLastPage (gboolean sensitive); 
     
    4445 
    4546            // Methods for test purposes. 
     47            gint countTimesShownPasswordPrompt (); 
    4648            const gchar *getTitle (void); 
    4749            gboolean hasImagePageView (void); 
     
    5759            gboolean isSensitiveZoomWidth (void); 
    5860            void setOpenFileName (const gchar *fileName); 
     61            void setPassword (const gchar *password); 
    5962            gboolean shownError (void); 
    6063 
     
    6265            DocumentPage *m_DocumentPage; 
    6366            gchar *m_OpenFileName; 
     67            gchar *m_Password; 
    6468            gboolean m_SensitiveGoToFirstPage; 
    6569            gboolean m_SensitiveGoToLastPage; 
     
    7377            gboolean m_Shown; 
    7478            gboolean m_ShownError; 
     79            gint m_TimesShownPassword; 
    7580            gchar *m_Title; 
    7681    }; 
  • trunk/tests/MainPterTest.cxx

    r26 r27  
    170170    CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
    171171    CPPUNIT_ASSERT (m_View->shownError ()); 
    172  
    173 } 
     172} 
     173 
     174/// 
     175/// @brief Test a cancelled password. 
     176/// 
     177/// A cancelled password is more or less the same as cancelling the loading 
     178/// of a file. It's when the user tried to open an encrypted file, but when  
     179/// the password is prompted, then it cancels :-) 
     180/// It should happen the same as when cancelling the open file dialog. 
     181/// 
     182void 
     183MainPterTest::cancelledPassword () 
     184{ 
     185    m_View->setOpenFileName ("/tmp/test.pdf"); 
     186    m_View->setPassword (NULL); 
     187    m_Document->setOpenError (DocumentErrorEncrypted); 
     188    m_MainPter->openFileActivated (); 
     189    CPPUNIT_ASSERT_EQUAL (0,  
     190            g_ascii_strcasecmp ("PDF Viewer", m_View->getTitle ())); 
     191    CPPUNIT_ASSERT (!m_View->isSensitiveGoToFirstPage ()); 
     192    CPPUNIT_ASSERT (!m_View->isSensitiveGoToLastPage ()); 
     193    CPPUNIT_ASSERT (!m_View->isSensitiveGoToNextPage ()); 
     194    CPPUNIT_ASSERT (!m_View->isSensitiveGoToPage ()); 
     195    CPPUNIT_ASSERT (!m_View->isSensitiveGoToPreviousPage ()); 
     196    CPPUNIT_ASSERT (!m_View->isSensitiveZoomIn ()); 
     197    CPPUNIT_ASSERT (!m_View->isSensitiveZoomOut ()); 
     198    CPPUNIT_ASSERT (!m_View->isSensitiveZoomFit ()); 
     199    CPPUNIT_ASSERT (!m_View->isSensitiveZoomWidth ()); 
     200    CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
     201    CPPUNIT_ASSERT (!m_View->shownError ()); 
     202} 
     203 
     204/// 
     205/// @brief Test a bad password. 
     206/// 
     207/// This test is very similar to the previous, but this time the user 
     208/// enters a bad password, trying to guess which is the correct password. 
     209/// The presenter gives up after three times and shows an error message. 
     210/// 
     211void 
     212MainPterTest::badPassword () 
     213{ 
     214    m_View->setOpenFileName ("/tmp/test.pdf"); 
     215    m_View->setPassword ("badpassword"); 
     216    m_Document->setPassword ("goodpassword"); 
     217    m_Document->setOpenError (DocumentErrorEncrypted); 
     218    m_MainPter->openFileActivated (); 
     219    CPPUNIT_ASSERT_EQUAL (0,  
     220            g_ascii_strcasecmp ("PDF Viewer", m_View->getTitle ())); 
     221    CPPUNIT_ASSERT (!m_View->isSensitiveGoToFirstPage ()); 
     222    CPPUNIT_ASSERT (!m_View->isSensitiveGoToLastPage ()); 
     223    CPPUNIT_ASSERT (!m_View->isSensitiveGoToNextPage ()); 
     224    CPPUNIT_ASSERT (!m_View->isSensitiveGoToPage ()); 
     225    CPPUNIT_ASSERT (!m_View->isSensitiveGoToPreviousPage ()); 
     226    CPPUNIT_ASSERT (!m_View->isSensitiveZoomIn ()); 
     227    CPPUNIT_ASSERT (!m_View->isSensitiveZoomOut ()); 
     228    CPPUNIT_ASSERT (!m_View->isSensitiveZoomFit ()); 
     229    CPPUNIT_ASSERT (!m_View->isSensitiveZoomWidth ()); 
     230    CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
     231    CPPUNIT_ASSERT (m_View->shownError ()); 
     232    CPPUNIT_ASSERT_EQUAL (3, m_View->countTimesShownPasswordPrompt ()); 
     233} 
  • trunk/tests/MainPterTest.h

    r26 r27  
    3030        CPPUNIT_TEST (loadCancelled); 
    3131        CPPUNIT_TEST (loadFailed); 
     32        CPPUNIT_TEST (cancelledPassword); 
     33        CPPUNIT_TEST (badPassword); 
    3234        CPPUNIT_TEST_SUITE_END(); 
    3335 
     
    4042            void loadCancelled (void); 
    4143            void loadFailed (void); 
    42  
     44            void cancelledPassword (void); 
     45            void badPassword (void); 
    4346        private: 
    4447            DumbMainView *m_View;