Changeset 6

Show
Ignore:
Timestamp:
04/10/06 11:11:30 (3 years ago)
Author:
jordi
Message:

The Document class now actually tries to open a PDFDoc document from Poppler and reports an error if it can't find the PDF file.

Also, now the test file has a member which is the document to test, since it will be the same for all test (even for the empty, because I don't load a file in the setUp() function).

Location:
trunk
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/Document.cxx

    r4 r6  
    1616// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    1717 
     18// Poppler headers. 
     19#include <ErrorCodes.h> 
     20#include <GlobalParams.h> 
     21 
    1822#include "epdfview.h" 
    1923 
     
    2529Document::Document () 
    2630{ 
     31    this->m_Document = NULL; 
    2732} 
    2833 
     
    3237Document::~Document () 
    3338{ 
     39    delete this->m_Document; 
    3440} 
    3541 
     
    4349{ 
    4450    return false; 
     51} 
     52 
     53/// 
     54/// @brief Load a PDF file. 
     55/// 
     56/// Tries to open the PDF file @a filename. 
     57/// 
     58/// @param filename The name of the filename to open. 
     59/// @param error Locatrion to store the error ocurringm or NULL to ignore  
     60///              errors. 
     61/// 
     62/// @return True if the file could be opened, false otherwise. 
     63/// 
     64bool 
     65Document::loadFile (const gchar *filename, GError **error) 
     66{ 
     67    assert (NULL != filename && "Tried to load a NULL filename"); 
     68 
     69    // The poppler library has a GLOBAL file for parameters. 
     70    // If this file isn't loaded then weird error happens. 
     71    if ( NULL == globalParams ) 
     72    { 
     73        globalParams = new GlobalParams ("/etc/xpdfrc"); 
     74    } 
     75 
     76    GooString *filename_g = new GooString (filename); 
     77    GooString *password_g = NULL; 
     78    // Try to open the PDF document. 
     79    PDFDoc *newDocument = new PDFDoc (filename_g, password_g, password_g); 
     80    delete password_g; 
     81    // The filename_g can't be deleted as PDFDoc saves the reference. 
     82    // deleting it will cause trouble when deleting the newDocument. 
     83 
     84    // Check if the document couldn't be opened sucessfully and why. 
     85    if ( !newDocument->isOk() ) 
     86    { 
     87        gint errorCode = newDocument->getErrorCode (); 
     88        delete newDocument; 
     89 
     90        if ( errEncrypted == errorCode ) 
     91        { 
     92            g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,  
     93                         _("Document is encrypted.")); 
     94        } 
     95        else 
     96        { 
     97            g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, 
     98                        _("Failed to load document '%s'. Error %d"), 
     99                        filename, errorCode); 
     100        } 
     101 
     102        return false; 
     103    } 
     104 
     105    delete this->m_Document; 
     106    this->m_Document = newDocument; 
     107 
     108    return true; 
    45109} 
    46110 
  • trunk/src/Document.h

    r4 r6  
    5454 
    5555            bool isLoaded (void); 
     56            bool loadFile (const gchar *filename, GError **error); 
    5657            const gchar *getTitle (void); 
    5758            const gchar *getAuthor (void); 
     
    6970            gint getCurrentPageNum (void); 
    7071            DocumentIndex &getDocumentIndex (void); 
     72 
     73        protected: 
     74            PDFDoc *m_Document; 
    7175    }; 
    7276} 
  • trunk/src/Makefile.am

    r4 r6  
    1111 
    1212libepdfview_a_CXXFLAGS =    \ 
    13     $(GLIB_CFLAGS) 
     13    $(GLIB_CFLAGS)          \ 
     14    $(POPPLER_CFLAGS) 
  • trunk/src/epdfview.h

    r4 r6  
    1919#define __E_PDF_VIEW_H__ 
    2020 
     21#include <assert.h> 
    2122#include <gettext.h> 
    2223#include <glib.h> 
     24 
     25// Poppler headers. 
     26#include <PDFDoc.h> 
    2327 
    2428#include <DocumentIndex.h> 
  • trunk/tests/DocumentTest.cxx

    r4 r6  
    3030DocumentTest::setUp () 
    3131{ 
     32    m_Document = new Document (); 
    3233} 
    3334 
     
    3839DocumentTest::tearDown () 
    3940{ 
     41    delete m_Document; 
    4042} 
    4143 
     
    5153DocumentTest::emptyDocument () 
    5254{ 
    53     Document *doc = new Document (); 
     55    CPPUNIT_ASSERT (!m_Document->isLoaded ()); 
     56    CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", m_Document->getTitle ())); 
     57    CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", m_Document->getAuthor ())); 
     58    CPPUNIT_ASSERT_EQUAL (0,  
     59            g_ascii_strcasecmp ("", m_Document->getSubject ())); 
     60    CPPUNIT_ASSERT_EQUAL (0,  
     61            g_ascii_strcasecmp ("", m_Document->getKeywords ())); 
     62    CPPUNIT_ASSERT_EQUAL (0,  
     63            g_ascii_strcasecmp ("", m_Document->getCreator ())); 
     64    CPPUNIT_ASSERT_EQUAL (0,  
     65            g_ascii_strcasecmp ("", m_Document->getProducer ())); 
     66    CPPUNIT_ASSERT_EQUAL (0,  
     67            g_ascii_strcasecmp ("", m_Document->getFormat ())); 
     68    CPPUNIT_ASSERT_EQUAL (0,  
     69            g_ascii_strcasecmp ("", m_Document->getLinearized ())); 
     70    CPPUNIT_ASSERT_EQUAL (0,  
     71            g_ascii_strcasecmp ("", m_Document->getCreationDate ())); 
     72    CPPUNIT_ASSERT_EQUAL (0,  
     73            g_ascii_strcasecmp ("", m_Document->getModifiedDate ())); 
     74    CPPUNIT_ASSERT_EQUAL (PageModeUnset, m_Document->getPageMode ()); 
     75    CPPUNIT_ASSERT_EQUAL (PageLayoutUnset, m_Document->getPageLayout ()); 
     76    CPPUNIT_ASSERT_EQUAL (0, m_Document->getNumPages ()); 
     77    CPPUNIT_ASSERT_EQUAL (0, m_Document->getCurrentPageNum ()); 
    5478 
    55     CPPUNIT_ASSERT (!doc->isLoaded ()); 
    56     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getTitle ())); 
    57     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getAuthor ())); 
    58     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getSubject ())); 
    59     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getKeywords ())); 
    60     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getCreator ())); 
    61     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getProducer ())); 
    62     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getFormat ())); 
    63     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getLinearized ())); 
    64     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getCreationDate ())); 
    65     CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", doc->getModifiedDate ())); 
    66     CPPUNIT_ASSERT_EQUAL (PageModeUnset, doc->getPageMode ()); 
    67     CPPUNIT_ASSERT_EQUAL (PageLayoutUnset, doc->getPageLayout ()); 
    68     CPPUNIT_ASSERT_EQUAL (0, doc->getNumPages ()); 
    69     CPPUNIT_ASSERT_EQUAL (0, doc->getCurrentPageNum ()); 
    70  
    71     DocumentIndex &index = doc->getDocumentIndex (); 
     79    DocumentIndex &index = m_Document->getDocumentIndex (); 
    7280    CPPUNIT_ASSERT_EQUAL (0, index.getNumChildren ()); 
    7381} 
     82 
     83/// 
     84/// @brief Tests that loading a file which does not exist fails. 
     85/// 
     86/// Loading a PDF can fail under three conditions: the file is not found, 
     87/// the file is not a PDF (or a malformed PDF file) and the file is encrypted 
     88/// but not password has been supplied. 
     89/// 
     90/// This test just check that loading a not existent file fails. 
     91/// 
     92void 
     93DocumentTest::fileNotFound (void) 
     94{ 
     95    gchar *error_message; 
     96    GError *error = NULL;     
     97 
     98    CPPUNIT_ASSERT (!m_Document->loadFile (TEST_DATA_DIR "noFile.pdf", &error)); 
     99    CPPUNIT_ASSERT (!m_Document->isLoaded ()); 
     100     
     101    error_message = g_strdup_printf ( 
     102            "Failed to load document '%sNoFile.pdf'. Error 1", TEST_DATA_DIR); 
     103    CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp (error_message,  
     104                                                 error->message)); 
     105} 
  • trunk/tests/DocumentTest.h

    r4 r6  
    2727        CPPUNIT_TEST_SUITE (DocumentTest); 
    2828        CPPUNIT_TEST (emptyDocument); 
     29        CPPUNIT_TEST (fileNotFound); 
    2930        CPPUNIT_TEST_SUITE_END (); 
    3031 
     
    3435 
    3536            void emptyDocument (void); 
     37            void fileNotFound (void); 
     38 
     39        private: 
     40            Document *m_Document; 
    3641    }; 
    3742} 
  • trunk/tests/Makefile.am

    r5 r6  
    99    main.cxx 
    1010 
    11 test_epdfview_CXXFLAGS =                \ 
    12     -DTEST_DATA_DIR=$(top_srcdir)/src   \ 
    13     -I$(top_srcdir)/src                 \ 
    14     $(GLIB_CFLAGS)                      \ 
     11test_epdfview_CXXFLAGS =                    \ 
     12    -DTEST_DATA_DIR='"$(top_srcdir)/src/"'  \ 
     13    -I$(top_srcdir)/src                     \ 
     14    $(GLIB_CFLAGS)                          \ 
     15    $(POPPLER_CFLAGS)                       \ 
    1516    $(CPPUNIT_CFLAGS) 
    1617 
    1718test_epdfview_LDFLAGS = \ 
    1819    $(GLIB_LIBS)        \ 
     20    $(POPPLER_LIBS)     \ 
    1921    $(CPPUNIT_LIBS) 
    2022