Changeset 132

Show
Ignore:
Timestamp:
04/29/06 10:07:03 (2 years ago)
Author:
jordi
Message:

The MainPter? now can load, show the error message and ask for the password using the new observer pattern. For now the PDFDocumentTests are "disabled".

Location:
trunk
Files:
12 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/IDocument.cxx

    r130 r132  
    2222using namespace ePDFView; 
    2323 
     24G_LOCK_EXTERN (JobRender); 
     25 
    2426// Constants. 
    2527static const gdouble ZOOM_IN_FACTOR = 1.2; 
     
    3133/// This is the error domain that will be used to report Document's errors. 
    3234GQuark IDocument::errorQuark = 0; 
    33  
    34 G_LOCK_EXTERN (JobRender); 
    3535 
    3636/// 
     
    210210 
    211211void 
    212 IDocument::notifyLoadPassword (const GError *error) 
     212IDocument::notifyLoadPassword (const gchar *fileName, const GError *error) 
    213213{ 
    214214    for ( GList *item = g_list_first (m_Observers) ; NULL != item ; 
     
    216216    { 
    217217        IDocumentObserver *observer = (IDocumentObserver *)item->data; 
    218         observer->notifyLoadPassword (error); 
     218        observer->notifyLoadPassword (fileName, error); 
    219219    } 
    220220} 
  • trunk/src/IDocument.h

    r130 r132  
    187187            void notifyLoad (void); 
    188188            void notifyLoadError (const GError *error); 
    189             void notifyLoadPassword (const GError *error); 
     189            void notifyLoadPassword (const gchar *fileName,  
     190                                     const GError *error); 
    190191            void notifyPageChanged (void); 
    191192            void notifyPageRendered (gint pageNumber, DocumentPage *pageImage); 
  • trunk/src/IDocumentObserver.h

    r129 r132  
    2828            virtual void notifyLoad (void) { } 
    2929            virtual void notifyLoadError (const GError *error) { } 
    30             virtual void notifyLoadPassword (const GError *error) { } 
     30            virtual void notifyLoadPassword (const gchar *fileName, 
     31                                             const GError *error) { } 
    3132            virtual void notifyPageChanged (gint pageNum) { } 
    3233            virtual void notifyPageRotated (gint rotation) { } 
  • trunk/src/JobLoad.cxx

    r126 r132  
    237237 
    238238    JobLoad *job = (JobLoad *)data; 
    239     job->getDocument ().notifyLoadPassword (job->getError ()); 
     239    job->getDocument ().notifyLoadPassword (job->getFileName (), 
     240                                            job->getError ()); 
    240241    JOB_NOTIFIER_END(); 
    241242} 
  • trunk/src/MainPter.cxx

    r124 r132  
    1818#include <config.h> 
    1919#include <stdlib.h> 
     20#include <unistd.h> 
    2021#include "epdfview.h" 
    2122 
    2223using namespace ePDFView; 
    2324 
     25#if defined (DEBUG) 
     26static volatile gboolean fileLoaded; 
     27#endif // DEBUG 
     28 
    2429/// 
    2530/// @brief Constructs a new main presenter with a default document. 
     
    2732MainPter::MainPter () 
    2833{ 
    29     m_Document = new PDFDocument (); 
    30     m_View = NULL; 
     34    MainPter (new PDFDocument ()); 
    3135} 
    3236 
     
    4347 
    4448    m_Document = document; 
     49    m_Document->attach (this); 
    4550    m_View = NULL; 
     51    m_PasswordTries = 3; 
     52#if defined (DEBUG) 
     53    fileLoaded = FALSE; 
     54#endif 
    4655} 
    4756 
     
    5160MainPter::~MainPter () 
    5261{ 
     62    m_Document->deattach (this); 
    5363    delete m_Document; 
    5464    delete m_View; 
     
    289299        config.setOpenFileFolder (dirName); 
    290300        g_free (dirName); 
    291     } 
    292  
    293     // Open the file. 
    294 /*    JobLoad *job = new JobLoad (); 
    295     job->setPresenter (this); 
    296     job->setFileName (fileName); 
    297     g_free (fileName); 
    298     job->setPassword (NULL); 
    299     job->setReload (FALSE); 
    300     job->run ();*/ 
     301        // Insensitive the open and reload controls. 
     302        IMainView &view = getView (); 
     303        view.sensitiveOpen (FALSE); 
     304        view.sensitiveReload (FALSE); 
     305        // Show the text on the status bar. 
     306        gchar *statusText = g_strdup_printf (_("Loading file %s..."), fileName); 
     307        view.setStatusBarText (statusText); 
     308        g_free (statusText); 
     309        // Set the wait cursor. 
     310        view.setCursor (MAIN_VIEW_CURSOR_WAIT); 
     311        // Set the number of times we can try the password. 
     312        m_PasswordTries = 3; 
     313        // Open the file. 
     314        m_Document->load (fileName, NULL); 
     315    } 
    301316} 
    302317 
     
    667682    } 
    668683} 
     684 
     685void 
     686MainPter::notifyLoad () 
     687{ 
     688    setInitialState (TRUE); 
     689#if defined (DEBUG) 
     690    fileLoaded = TRUE; 
     691#endif // DEBUG 
     692} 
     693 
     694void 
     695MainPter::notifyLoadError (const GError *error) 
     696{ 
     697    getView ().showErrorMessage (_("Error Loading File"), error->message); 
     698    setInitialState (TRUE); 
     699#if defined (DEBUG) 
     700    fileLoaded = TRUE; 
     701#endif // DEBUG 
     702} 
     703 
     704void 
     705MainPter::notifyLoadPassword (const gchar *fileName, const GError *error) 
     706{ 
     707    if ( 0 < m_PasswordTries ) 
     708    { 
     709        m_PasswordTries--; 
     710        gchar *password = getView ().promptPasswordDialog (); 
     711        if ( password != NULL ) 
     712        { 
     713            m_Document->load (fileName, password); 
     714        } 
     715#if defined (DEBUG) 
     716        else 
     717        { 
     718            fileLoaded = TRUE; 
     719        } 
     720#endif // DEBUG 
     721    } 
     722    else 
     723    { 
     724        getView ().showErrorMessage (_("Error Loading File"), 
     725                _("The password you have supplied is not a valid password " 
     726                  "for this file.")); 
     727#if defined (DEBUG)         
     728        fileLoaded = TRUE; 
     729#endif // DEBUG 
     730    } 
     731} 
     732 
     733#if defined (DEBUG) 
     734/// 
     735/// @brief Waits until a file is loaded. 
     736/// 
     737void 
     738MainPter::waitForFileLoaded () 
     739{ 
     740    while ( !fileLoaded ) 
     741    { 
     742        usleep (10000); 
     743    } 
     744    fileLoaded = FALSE; 
     745} 
     746#endif // DEBUG 
  • trunk/src/MainPter.h

    r123 r132  
    3434    /// and how to react to main window's user events. 
    3535    /// 
    36     class MainPter 
     36    class MainPter: public IDocumentObserver 
    3737    { 
    3838        public: 
     
    6767            void zoomWidthActivated (gboolean active); 
    6868 
     69            void notifyLoad (void); 
     70            void notifyLoadError (const GError *error); 
     71            void notifyLoadPassword (const gchar *fileName,  
     72                                     const GError *error); 
     73             
     74#if defined (DEBUG) 
     75            void waitForFileLoaded (void); 
     76#endif // DEBUG 
     77 
    6978        protected: 
     79            IDocument *m_Document; 
     80            gint m_PasswordTries; 
     81            IMainView *m_View; 
    7082            void showPage (PageScroll pageScroll); 
    71  
    72             IDocument *m_Document; 
    73             IMainView *m_View; 
    74  
    7583            void zoomFit (void); 
    7684            void zoomWidth (void); 
  • trunk/tests/DumbDocumentObserver.cxx

    r129 r132  
    5555 
    5656void 
    57 DumbDocumentObserver::notifyLoadPassword (const GError *error) 
     57DumbDocumentObserver::notifyLoadPassword (const gchar *fileName, 
     58                                          const GError *error) 
    5859{ 
    5960    setLoadError (error); 
  • trunk/tests/DumbDocumentObserver.h

    r129 r132  
    2929            void notifyLoad (void); 
    3030            void notifyLoadError (const GError *error); 
    31             void notifyLoadPassword (const GError *error); 
     31            void notifyLoadPassword (const gchar *fileName,  
     32                                     const GError *error); 
    3233            void notifyPageChanged (gint pageNum); 
    3334            void notifyPageRotated (gint rotation); 
  • trunk/tests/MainPterTest.cxx

    r123 r132  
    2323using namespace ePDFView; 
    2424 
     25G_LOCK_EXTERN (JobRender); 
     26 
    2527// Register the test suite into the `registry'. 
    2628CPPUNIT_TEST_SUITE_REGISTRATION (MainPterTest); 
    27  
    28 // Constants. 
    29 static const gint SLEEP_TIME = 250000; 
    3029 
    3130/// 
     
    4039    m_MainPter->setView (m_View); 
    4140    Config::loadFile (FALSE); 
     41    G_LOCK (JobRender); 
     42    JobRender::m_CanProcessJobs = TRUE; 
     43    G_UNLOCK (JobRender); 
    4244} 
    4345 
     
    4850MainPterTest::tearDown () 
    4951{ 
     52    G_LOCK (JobRender); 
     53    JobRender::m_CanProcessJobs = FALSE; 
     54    IJob::clearQueue (); 
     55    G_UNLOCK (JobRender); 
    5056    // We only need to delete the presenter, as all other 
    5157    // classes are deleted by it. 
     
    102108    m_View->setOpenFileName ("/tmp/test.pdf"); 
    103109    m_MainPter->openFileActivated (); 
    104     // Sleep to let the thread open the file. 
    105     usleep (SLEEP_TIME); 
     110    m_MainPter->waitForFileLoaded (); 
    106111    CPPUNIT_ASSERT_EQUAL (0,  
    107112            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     
    124129    m_Document->setTitle (g_strdup ("Test PDF")); 
    125130    m_MainPter->openFileActivated (); 
    126     // Sleep to let the thread open the file. 
    127     usleep (SLEEP_TIME); 
     131    m_MainPter->waitForFileLoaded (); 
    128132    CPPUNIT_ASSERT_EQUAL (0,  
    129133            g_ascii_strcasecmp ("Test PDF", m_View->getTitle ())); 
     
    145149 
    146150/// 
    147 /// @brief Tests a cancelled open file. 
     151/// @brief Tests a canceled open file. 
    148152/// 
    149153/// When the user cancels the open dialog, then nothing should change. 
    150154/// 
    151155void 
    152 MainPterTest::loadCancelled () 
     156MainPterTest::loadCanceled () 
    153157{ 
    154158    // returning a NULL file name is what will happen when cancel. 
    155159    m_View->setOpenFileName (NULL); 
    156160    m_MainPter->openFileActivated (); 
    157     // Sleep to let the thread open the file. 
    158     usleep (SLEEP_TIME); 
    159161    CPPUNIT_ASSERT_EQUAL (0,  
    160162            g_ascii_strcasecmp ("PDF Viewer", m_View->getTitle ())); 
     
    175177} 
    176178 
     179 
    177180/// 
    178181/// @brief Test a failed load. 
    179182/// 
    180183/// A failed load (anything but a DocumentErrorNone and DocumentErrorEncrypted) 
    181 /// is very similar to the load cancelled, but an error message is shown  
     184/// is very similar to the load canceled, but an error message is shown  
    182185/// to the user. 
    183186/// 
     
    188191    m_Document->setOpenError (DocumentErrorDamaged); 
    189192    m_MainPter->openFileActivated (); 
    190     // Sleep to let the thread open the file. 
    191     usleep (SLEEP_TIME); 
     193    m_MainPter->waitForFileLoaded (); 
    192194    CPPUNIT_ASSERT_EQUAL (0,  
    193195            g_ascii_strcasecmp ("PDF Viewer", m_View->getTitle ())); 
     
    209211 
    210212/// 
    211 /// @brief Test a cancelled password. 
    212 /// 
    213 /// A cancelled password is more or less the same as cancelling the loading 
     213/// @brief Test a canceled password. 
     214/// 
     215/// A canceled password is more or less the same as canceling the loading 
    214216/// of a file. It's when the user tried to open an encrypted file, but when  
    215217/// the password is prompted, then it cancels :-) 
    216 /// It should happen the same as when cancelling the open file dialog. 
    217 /// 
    218 void 
    219 MainPterTest::cancelledPassword () 
     218/// It should happen the same as when canceling the open file dialog. 
     219/// 
     220void 
     221MainPterTest::canceledPassword () 
    220222{ 
    221223    m_View->setOpenFileName ("/tmp/test.pdf"); 
     
    223225    m_Document->setOpenError (DocumentErrorEncrypted); 
    224226    m_MainPter->openFileActivated (); 
    225     // Sleep to let the thread open the file. 
    226     usleep (SLEEP_TIME); 
     227    m_MainPter->waitForFileLoaded (); 
    227228    CPPUNIT_ASSERT_EQUAL (0,  
    228229            g_ascii_strcasecmp ("PDF Viewer", m_View->getTitle ())); 
     
    258259    m_Document->setOpenError (DocumentErrorEncrypted); 
    259260    m_MainPter->openFileActivated (); 
    260     // Sleep to let the thread open the file. 
    261     usleep (SLEEP_TIME); 
     261    m_MainPter->waitForFileLoaded (); 
    262262    CPPUNIT_ASSERT_EQUAL (0,  
    263263            g_ascii_strcasecmp ("PDF Viewer", m_View->getTitle ())); 
     
    293293    m_Document->setOpenError (DocumentErrorEncrypted); 
    294294    m_MainPter->openFileActivated (); 
    295     // Sleep to let the thread open the file. 
    296     usleep (SLEEP_TIME); 
     295    m_MainPter->waitForFileLoaded (); 
    297296    CPPUNIT_ASSERT_EQUAL (0,  
    298297            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     
    313312    CPPUNIT_ASSERT_EQUAL (1, m_View->countTimesShownPasswordPrompt ()); 
    314313} 
    315  
     314/* 
    316315/// 
    317316/// @brief Test the last folder used to open a file. 
     
    326325    m_View->setOpenFileName ("/tmp/test.pdf"); 
    327326    m_MainPter->openFileActivated (); 
    328     // Sleep to let the thread open the file. 
    329     usleep (SLEEP_TIME); 
     327    m_MainPter->waitForFileLoaded (); 
    330328    CPPUNIT_ASSERT (NULL == m_View->getLastOpenFileFolder ()); 
    331329 
     
    333331    m_View->setOpenFileName ("/usr/test.pdf"); 
    334332    m_MainPter->openFileActivated (); 
    335     // Sleep to let the thread open the file. 
    336     usleep (SLEEP_TIME); 
     333    m_MainPter->waitForFileLoaded (); 
    337334    CPPUNIT_ASSERT (0 == g_ascii_strcasecmp ("/tmp",  
    338335                                             m_View->getLastOpenFileFolder ())); 
    339336     
    340337    m_MainPter->openFileActivated (); 
    341     // Sleep to let the thread open the file. 
    342     usleep (SLEEP_TIME); 
     338    m_MainPter->waitForFileLoaded (); 
    343339    CPPUNIT_ASSERT (0 == g_ascii_strcasecmp ("/usr",  
    344340                                             m_View->getLastOpenFileFolder ())); 
     
    357353    m_Document->setNumPages (4); 
    358354    m_MainPter->openFileActivated (); 
    359     // Sleep to let the thread open the file. 
    360     usleep (SLEEP_TIME); 
     355    m_MainPter->waitForFileLoaded (); 
    361356    // Check that sets the correct number of pages and the current page. 
    362357    CPPUNIT_ASSERT_EQUAL (4, m_View->getTotalPages ()); 
     
    439434    m_Document->setNumPages (4); 
    440435    m_MainPter->openFileActivated (); 
    441     // Sleep to let the thread open the file. 
    442     usleep (SLEEP_TIME); 
     436    m_MainPter->waitForFileLoaded (); 
    443437    // Check that sets the correct number of pages and the current page. 
    444438    CPPUNIT_ASSERT_EQUAL (4, m_View->getTotalPages ()); 
     
    520514    m_View->setOpenFileName ("/tmp/test.pdf"); 
    521515    m_MainPter->openFileActivated (); 
    522     // Sleep to let the thread open the file. 
    523     usleep (SLEEP_TIME); 
     516    m_MainPter->waitForFileLoaded (); 
    524517    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    525518    CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
     
    550543    m_View->setOpenFileName ("/tmp/test.pdf"); 
    551544    m_MainPter->openFileActivated (); 
    552     // Sleep to let the thread open the file. 
    553     usleep (SLEEP_TIME); 
     545    m_MainPter->waitForFileLoaded (); 
    554546    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    555547    CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
     
    591583    m_View->setOpenFileName ("/tmp/test.pdf"); 
    592584    m_MainPter->openFileActivated (); 
    593     // Sleep to let the thread open the file. 
    594     usleep (SLEEP_TIME); 
     585    m_MainPter->waitForFileLoaded (); 
    595586    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    596587    CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_Document->getZoom (), 0.0001); 
     
    657648    m_View->setOpenFileName ("/tmp/test.pdf"); 
    658649    m_MainPter->openFileActivated (); 
    659     // Sleep to let the thread open the file. 
    660     usleep (SLEEP_TIME); 
     650    m_MainPter->waitForFileLoaded (); 
    661651    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    662652    CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_Document->getZoom (), 0.0001); 
     
    722712    m_View->setOpenFileName ("/tmp/test.pdf"); 
    723713    m_MainPter->openFileActivated (); 
    724     // Sleep to let the thread open the file. 
    725     usleep (SLEEP_TIME); 
     714    m_MainPter->waitForFileLoaded (); 
    726715    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    727716    CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_Document->getZoom (), 0.0001); 
     
    767756    m_View->setOpenFileName ("/tmp/test.pdf"); 
    768757    m_MainPter->openFileActivated (); 
    769     // Sleep to let the thread open the file. 
    770     usleep (SLEEP_TIME); 
     758    m_MainPter->waitForFileLoaded (); 
    771759    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    772760 
     
    781769    // Reload the document. 
    782770    m_MainPter->reloadActivated (); 
    783     // Sleep to let the thread open the file. 
    784     usleep (SLEEP_TIME); 
     771    m_MainPter->waitForFileLoaded (); 
    785772    CPPUNIT_ASSERT_EQUAL (0,  
    786773            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     
    805792    m_Document->setOpenError (DocumentErrorEncrypted); 
    806793    m_MainPter->openFileActivated (); 
    807     // Sleep to let the thread open the file. 
    808     usleep (SLEEP_TIME); 
     794    m_MainPter->waitForFileLoaded (); 
    809795    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    810796    CPPUNIT_ASSERT_EQUAL (0,  
     
    824810    m_Document->setOpenError (DocumentErrorEncrypted); 
    825811    m_MainPter->reloadActivated (); 
    826     // Sleep to let the thread open the file. 
    827     usleep (SLEEP_TIME); 
     812    m_MainPter->waitForFileLoaded (); 
    828813    CPPUNIT_ASSERT_EQUAL (0,  
    829814            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     
    849834    m_Document->setOpenError (DocumentErrorEncrypted); 
    850835    m_MainPter->openFileActivated (); 
    851     // Sleep to let the thread open the file. 
    852     usleep (SLEEP_TIME); 
     836    m_MainPter->waitForFileLoaded (); 
    853837    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
    854838    CPPUNIT_ASSERT_EQUAL (0,  
     
    870854    m_Document->setOpenError (DocumentErrorEncrypted); 
    871855    m_MainPter->reloadActivated (); 
    872     // Sleep to let the thread open the file. 
    873     usleep (SLEEP_TIME); 
     856    m_MainPter->waitForFileLoaded (); 
    874857    CPPUNIT_ASSERT_EQUAL (0, 
    875858            g_ascii_strcasecmp("newpassword", m_Document->getPassword ())); 
     
    906889    m_View->setOpenFileName ("/tmp/test.pdf"); 
    907890    m_MainPter->openFileActivated (); 
    908     // Sleep to let the thread open the file. 
    909     usleep (SLEEP_TIME); 
     891    m_MainPter->waitForFileLoaded (); 
    910892    CPPUNIT_ASSERT (m_View->isShownIndex ()); 
    911893    CPPUNIT_ASSERT (outline == m_View->getOutline ()); 
     
    951933    CPPUNIT_ASSERT (m_View->isShownStatusbar ()); 
    952934} 
     935*/ 
  • trunk/tests/MainPterTest.h

    r123 r132  
    2828        CPPUNIT_TEST (initialStatus); 
    2929        CPPUNIT_TEST (loadDocument); 
    30         CPPUNIT_TEST (loadCancelled); 
     30        CPPUNIT_TEST (loadCanceled); 
    3131        CPPUNIT_TEST (loadFailed); 
    32         CPPUNIT_TEST (cancelledPassword); 
     32        CPPUNIT_TEST (canceledPassword); 
    3333        CPPUNIT_TEST (badPassword); 
    3434        CPPUNIT_TEST (goodPassword); 
    35         CPPUNIT_TEST (lastFolder); 
     35/*        CPPUNIT_TEST (lastFolder); 
    3636        CPPUNIT_TEST (pageNavigation); 
    3737        CPPUNIT_TEST (pageNavigationEntry); 
     
    4545        CPPUNIT_TEST (reloadChangedPassword); 
    4646        CPPUNIT_TEST (showIndex); 
    47         CPPUNIT_TEST (showToolAndStatusBars); 
     47        CPPUNIT_TEST (showToolAndStatusBars);*/ 
    4848        CPPUNIT_TEST_SUITE_END(); 
    4949 
     
    5454            void initialStatus (void); 
    5555            void loadDocument (void); 
    56             void loadCancelled (void); 
     56            void loadCanceled (void); 
    5757            void loadFailed (void); 
    58             void cancelledPassword (void); 
     58            void canceledPassword (void); 
    5959            void badPassword (void); 
    6060            void goodPassword (void); 
  • trunk/tests/Makefile.am

    r131 r132  
    1919    DumbMainView.h          \ 
    2020    main.cxx                \ 
    21     PDFDocumentTest.cxx     \ 
    22     PDFDocumentTest.h       \ 
     21    MainPterTest.cxx    \ 
     22    MainPterTest.h  \ 
    2323    Utils.cxx               \ 
    2424    Utils.h 
    2525 
    26  
    27 #   MainPterTest.cxx 
    28 #   MainPterTest.h 
     26#   PDFDocumentTest.cxx 
     27#   PDFDocumentTest.h 
    2928 
    3029test_epdfview_CXXFLAGS =                        \ 
  • trunk/tests/PDFDocumentTest.cxx

    r130