Changeset 65

Show
Ignore:
Timestamp:
04/14/06 16:24:42 (2 years ago)
Author:
jordi
Message:

The Main presenter now can reload encrypted files without bothering the user about password, unless the document's password has been changed. In this case, it will behaviour like opening the file as new.

Location:
trunk
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/IDocument.cxx

    r64 r65  
    136136    m_PageMode = PageModeUnset; 
    137137    m_PageNumber = 0; 
     138    m_Password = NULL; 
    138139    m_Producer = NULL; 
    139140    m_Rotation = 0; 
     
    156157    g_free (m_Keywords); 
    157158    g_free (m_ModifiedDate); 
     159    g_free (m_Password); 
    158160    g_free (m_Producer); 
    159161    g_free (m_Subject); 
     
    504506 
    505507/// 
    506 /// @brief Set the document's number of pages. 
     508/// @brief Gets the password used to open the document. 
     509/// 
     510/// @return The password used to open the document, or NULL 
     511///         if no password was used. 
     512/// 
     513const gchar * 
     514IDocument::getPassword () 
     515{ 
     516    return m_Password; 
     517} 
     518 
     519/// 
     520/// @brief Sets the password user to open the document. 
     521/// 
     522/// @param password The password used. 
     523/// 
     524void 
     525IDocument::setPassword (const gchar *password) 
     526{ 
     527    gchar *oldPassword = m_Password; 
     528    m_Password = g_strdup (password); 
     529    g_free (oldPassword); 
     530} 
     531 
     532/// 
     533/// @brief Sets the document's number of pages. 
    507534/// 
    508535/// @param numPages The number of pages the document has. 
  • trunk/src/IDocument.h

    r63 r65  
    180180            const gchar *getCreator (void); 
    181181            void setCreator (const gchar *creator); 
     182            const gchar *getPassword (void); 
     183            void setPassword (const gchar *password); 
    182184            const gchar *getProducer (void); 
    183185            void setProducer (const gchar *producer); 
     
    242244            PageMode m_PageMode; 
    243245            gint m_PageNumber; 
     246            gchar *m_Password; 
    244247            gchar *m_Producer; 
    245248            gint m_Rotation; 
  • trunk/src/MainPter.cxx

    r64 r65  
    245245    IMainView &view = getView (); 
    246246    gchar *fileName = view.openFileDialog (); 
    247     openDocument (fileName, TRUE); 
     247    openDocument (fileName, NULL, TRUE); 
    248248    g_free (fileName); 
    249249} 
     
    263263    gdouble currentZoom = m_Document->getZoom (); 
    264264    // Reopen the document. 
    265     openDocument (m_Document->getFileName (), FALSE);     
     265    openDocument (m_Document->getFileName (), m_Document->getPassword (),  
     266                  FALSE);  
    266267    // And restore the state 
    267268    m_Document->setZoom (currentZoom); 
     
    359360///                 NULL then that means the user didn't wanted to 
    360361///                 open a file, so this function won't do anything. 
     362/// @param oldPassword This is only used when reloading. It is the last 
     363///                    password that was used to open a file, and will be 
     364///                    used to open the file the first try before asking 
     365///                    to the user. For opening a new file, just set to NULL. 
    361366/// @param canShowPage Set to TRUE if the application should show the page  
    362367///                    after the loading or not. This is useful when reloading 
     
    365370/// 
    366371void 
    367 MainPter::openDocument (const gchar *fileName, gboolean canShowPage) 
     372MainPter::openDocument (const gchar *fileName, const gchar *oldPassword, 
     373                        gboolean canShowPage) 
    368374{ 
    369375    // if fileName is NULL, then the user cancelled the operation. 
     
    372378    if ( NULL != fileName ) 
    373379    { 
    374         GError *error = NULL; 
    375         if ( m_Document->loadFile (fileName, NULL, &error) ) 
     380        GError *error = NULL;         
     381        if ( m_Document->loadFile (fileName, oldPassword, &error) ) 
    376382        { 
    377383            // Now that the document has been loaded, just reset the initial 
     
    380386        } 
    381387        else 
    382         {             
     388        { 
    383389            // We got an error, but also can be that the file is encrypted. 
    384390            if ( DocumentErrorEncrypted == error->code ) 
  • trunk/src/MainPter.h

    r63 r65  
    6060 
    6161        protected: 
    62             void openDocument (const gchar *fileName, gboolean canShowPage); 
     62            void openDocument (const gchar *fileName, const gchar *oldPassword, 
     63                               gboolean canShowPage); 
    6364            void showPage (void); 
    6465 
  • trunk/src/PDFDocument.cxx

    r63 r65  
    125125    
    126126    setFileName (filename); 
     127    setPassword (password); 
    127128    delete m_Document; 
    128129    m_Document = newDocument; 
  • trunk/tests/DumbDocument.cxx

    r63 r65  
    3030    m_Loaded = FALSE; 
    3131    m_OpenError = DocumentErrorNone; 
    32     m_Password = NULL; 
     32    m_TestPassword = NULL;  
    3333    setNumPages (2); 
    3434} 
     
    3636DumbDocument::~DumbDocument () 
    3737{ 
    38     g_free (m_Password); 
     38    g_free (m_TestPassword); 
    3939} 
    4040 
     
    5151    if ( DocumentErrorNone == m_OpenError ) 
    5252    { 
     53        m_Loaded = TRUE; 
    5354        setFileName (filename); 
    54         m_Loaded = TRUE; 
     55        setPassword (password); 
    5556    } 
    5657    else if ( DocumentErrorEncrypted == m_OpenError && 
    57               password != NULL &&  
    58               0 == g_ascii_strcasecmp (password, m_Password)) 
     58              NULL != password && NULL != m_TestPassword && 
     59              0 == g_ascii_strcasecmp (password, m_TestPassword)) 
    5960    { 
     61        m_Loaded = TRUE; 
    6062        setFileName (filename); 
    61         m_Loaded = TRUE; 
     63        setPassword (password); 
    6264    } 
    6365    else 
     
    6567        g_set_error (error, EPDFVIEW_DOCUMENT_ERROR, m_OpenError,  
    6668                     "%s", IDocument::getErrorMessage (m_OpenError)); 
     69        m_Loaded = FALSE; 
    6770    } 
    6871    setRotation (0); 
     
    102105 
    103106void 
    104 DumbDocument::setPassword (const gchar *password) 
     107DumbDocument::setTestPassword (const gchar *password) 
    105108{ 
    106     g_free (m_Password); 
    107     m_Password = g_strdup (password); 
     109    gchar *oldPassword = m_TestPassword; 
     110    m_TestPassword = g_strdup (password); 
     111    g_free (oldPassword); 
    108112} 
  • trunk/tests/DumbDocument.h

    r27 r65  
    3636            // Test functions. 
    3737            void setOpenError (DocumentError error); 
    38             void setPassword (const gchar *password); 
     38            void setTestPassword (const gchar *password); 
    3939 
    4040        private: 
    4141            gboolean m_Loaded; 
    4242            DocumentError m_OpenError; 
    43             gchar *m_Password; 
     43            gchar *m_TestPassword; 
    4444    }; 
    4545} 
  • trunk/tests/MainPterTest.cxx

    r64 r65  
    233233    m_View->setOpenFileName ("/tmp/test.pdf"); 
    234234    m_View->setPassword ("badpassword"); 
    235     m_Document->setPassword ("goodpassword"); 
     235    m_Document->setTestPassword ("goodpassword"); 
    236236    m_Document->setOpenError (DocumentErrorEncrypted); 
    237237    m_MainPter->openFileActivated (); 
     
    266266    m_View->setOpenFileName ("/tmp/test.pdf"); 
    267267    m_View->setPassword ("goodpassword"); 
    268     m_Document->setPassword ("goodpassword"); 
     268    m_Document->setTestPassword ("goodpassword"); 
    269269    m_Document->setOpenError (DocumentErrorEncrypted); 
    270270    m_MainPter->openFileActivated (); 
     
    566566    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); 
    567567    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    568     CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
    569568 
    570569    // Reload the document. 
     
    576575    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); 
    577576    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    578     CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
    579 } 
     577} 
     578 
     579/// 
     580/// @brief Tries to reload an encrypted file. 
     581/// 
     582/// Reloading an encrypted file won't ask for the password unless it changed,  
     583/// but here we assume that don't 
     584/// 
     585void 
     586MainPterTest::reloadEncrypted () 
     587{ 
     588    m_View->setOpenFileName ("/tmp/test.pdf"); 
     589    m_View->setPassword ("goodpassword"); 
     590    m_Document->setTestPassword ("goodpassword"); 
     591    m_Document->setOpenError (DocumentErrorEncrypted); 
     592    m_MainPter->openFileActivated (); 
     593    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     594    CPPUNIT_ASSERT_EQUAL (0,  
     595            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     596     
     597    m_MainPter->goToNextPageActivated (); 
     598    m_MainPter->rotateRightActivated ();  
     599    m_MainPter->zoomWidthActivated (); 
     600    CPPUNIT_ASSERT_EQUAL (2, m_View->getCurrentPage ());  
     601    CPPUNIT_ASSERT_EQUAL (90, m_Document->getRotation ());  
     602    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); 
     603    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     604 
     605    // Reload the document. 
     606    m_View->setPassword ("badpassword"); 
     607    m_Document->setTestPassword ("goodpassword"); 
     608    m_Document->setOpenError (DocumentErrorEncrypted); 
     609    m_MainPter->reloadActivated (); 
     610    CPPUNIT_ASSERT_EQUAL (0,  
     611            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     612    CPPUNIT_ASSERT_EQUAL (2, m_View->getCurrentPage ());  
     613    CPPUNIT_ASSERT_EQUAL (90, m_Document->getRotation ());  
     614    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); 
     615    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     616    CPPUNIT_ASSERT (!m_View->shownError ()); 
     617    CPPUNIT_ASSERT_EQUAL (0, m_View->countTimesShownPasswordPrompt ()); 
     618} 
     619 
     620/// 
     621/// @brief Tries to reload an encrypted file whose password changed. 
     622/// 
     623/// Reloading an encrypted file won't ask for the password unless it changed.  
     624/// 
     625void 
     626MainPterTest::reloadChangedPassword () 
     627{ 
     628    m_View->setOpenFileName ("/tmp/test.pdf"); 
     629    m_View->setPassword ("goodpassword"); 
     630    m_Document->setTestPassword ("goodpassword"); 
     631    m_Document->setOpenError (DocumentErrorEncrypted); 
     632    m_MainPter->openFileActivated (); 
     633    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     634    CPPUNIT_ASSERT_EQUAL (0,  
     635            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     636     
     637    m_MainPter->goToNextPageActivated (); 
     638    m_MainPter->rotateRightActivated ();  
     639    m_MainPter->zoomWidthActivated (); 
     640    CPPUNIT_ASSERT_EQUAL (2, m_View->getCurrentPage ());  
     641    CPPUNIT_ASSERT_EQUAL (90, m_Document->getRotation ());  
     642    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); 
     643    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     644 
     645    // Reload the document. 
     646    m_View->setPassword ("newpassword"); 
     647    m_Document->setTestPassword ("newpassword"); 
     648    CPPUNIT_ASSERT_EQUAL (0, 
     649            g_ascii_strcasecmp("goodpassword", m_Document->getPassword ())); 
     650    m_Document->setOpenError (DocumentErrorEncrypted); 
     651    m_MainPter->reloadActivated (); 
     652    CPPUNIT_ASSERT_EQUAL (0, 
     653            g_ascii_strcasecmp("newpassword", m_Document->getPassword ())); 
     654    CPPUNIT_ASSERT_EQUAL (0,  
     655            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     656    CPPUNIT_ASSERT_EQUAL (2, m_View->getCurrentPage ());  
     657    CPPUNIT_ASSERT_EQUAL (90, m_Document->getRotation ());  
     658    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); 
     659    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     660    CPPUNIT_ASSERT (!m_View->shownError ()); 
     661    CPPUNIT_ASSERT_EQUAL (1, m_View->countTimesShownPasswordPrompt ()); 
     662} 
  • trunk/tests/MainPterTest.h

    r63 r65  
    3838        CPPUNIT_TEST (pageZoom); 
    3939        CPPUNIT_TEST (reloadNormal); 
     40        CPPUNIT_TEST (reloadEncrypted); 
     41        CPPUNIT_TEST (reloadChangedPassword); 
    4042        CPPUNIT_TEST_SUITE_END(); 
    4143 
     
    5658            void pageZoom (void); 
    5759            void reloadNormal (void); 
     60            void reloadEncrypted (void); 
     61            void reloadChangedPassword (void); 
    5862 
    5963        private: 
  • trunk/tests/PDFDocumentTest.cxx

    r58 r65  
    194194            g_ascii_strcasecmp (TEST_DATA_DIR "test_encrypted.pdf",  
    195195                                m_Document->getFileName ())); 
     196    CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("testpasswd",  
     197                                                 m_Document->getPassword ())); 
    196198} 
    197199