Changeset 8

Show
Ignore:
Timestamp:
04/10/06 12:49:58 (3 years ago)
Author:
jordi
Message:

I've changed the way the loadFile() function handles the error. Now all Poppler error are defined under the DocumentError? enumeration. When an error happens we can call the Document::getErrorMessage() static method to get the error message from the error code.

Also I've added the Document domain to the GError.

Location:
trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/Document.cxx

    r6 r8  
    2424using namespace ePDFView; 
    2525 
     26/// This is the error domain that will be used to report Document's errors. 
     27GQuark Document::errorQuark = 0; 
     28 
     29/// 
     30/// @brief Gets the Document's error quark. 
     31/// 
     32/// The first time it's called will create the new quark value for the  
     33/// document class. Successive calls will just return the previously created 
     34/// quark value. 
     35/// 
     36/// @return The Document's quark. 
     37/// 
     38GQuark 
     39Document::getErrorQuark () 
     40{ 
     41    if ( 0 == Document::errorQuark ) 
     42        Document::errorQuark = g_quark_from_static_string ("epdfview-document"); 
     43 
     44    return Document::errorQuark; 
     45} 
     46 
     47/// 
     48/// @brief Gives the error's description. 
     49/// 
     50/// This function looks the error at @a errorCode and gives an human 
     51/// readable error string. 
     52/// 
     53/// The error message must be freed using g_free(). 
     54/// 
     55/// @param errorCode The error code to get the string from. 
     56/// 
     57/// @return An human readable error message. 
     58/// 
     59gchar * 
     60Document::getErrorMessage (DocumentError errorCode) 
     61{ 
     62    gchar *errorMessage = NULL; 
     63    switch (errorCode) 
     64    { 
     65        case DocumentErrorNone: 
     66            errorMessage = g_strdup (_("No error")); 
     67            break; 
     68        case DocumentErrorOpenFile: 
     69            errorMessage = g_strdup (_("File not found.")); 
     70            break; 
     71        case DocumentErrorBadCatalog: 
     72            errorMessage = g_strdup (_("Couldn't read the page catalog.")); 
     73            break; 
     74        case DocumentErrorDamaged: 
     75            errorMessage = g_strdup (_("The PDF file is damaged and can't be repaired.")); 
     76            break; 
     77        case DocumentErrorEncrypted: 
     78            errorMessage = g_strdup (_("The file is encrypted and the password was incorrect or not supplied.")); 
     79            break; 
     80        case DocumentErrorHighlightFile: 
     81            errorMessage = g_strdup (_("Nonexistent or invlid highlight file.")); 
     82            break; 
     83        case DocumentErrorBadPrinter: 
     84            errorMessage = g_strdup (_("Invalid printer.")); 
     85            break; 
     86        case DocumentErrorPrinting: 
     87            errorMessage = g_strdup (_("Error during printing.")); 
     88            break; 
     89        case DocumentErrorPermission: 
     90            errorMessage = g_strdup (_("The PDF file doesn't allow that operation.")); 
     91            break; 
     92        case DocumentErrorBadPageNumber: 
     93            errorMessage = g_strdup (_("Invalid page number.")); 
     94            break; 
     95        case DocumentErrorFileIO: 
     96            errorMessage = g_strdup (_("File I/O error.")); 
     97            break; 
     98        default: 
     99            errorMessage = g_strdup_printf (_("Unknown error (%d)"), errorCode);             
     100    } 
     101 
     102    g_assert (NULL != errorMessage && "The error message is NULL"); 
     103    return errorMessage; 
     104} 
     105 
    26106/// 
    27107/// @brief Constructs a new Document object. 
     
    65145Document::loadFile (const gchar *filename, GError **error) 
    66146{ 
    67     assert (NULL != filename && "Tried to load a NULL filename"); 
     147    g_assert (NULL != filename && "Tried to load a NULL filename"); 
    68148 
    69149    // The poppler library has a GLOBAL file for parameters. 
     
    85165    if ( !newDocument->isOk() ) 
    86166    { 
    87         gint errorCode = newDocument->getErrorCode (); 
     167        DocumentError errorCode = (DocumentError)newDocument->getErrorCode (); 
    88168        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         } 
     169        gchar *errorMessage = Document::getErrorMessage (errorCode); 
     170        g_set_error (error,  
     171                     EPDFVIEW_DOCUMENT_ERROR, errorCode, 
     172                     _("Failed to load document '%s'.\n%s\n"), 
     173                     filename, errorMessage); 
     174        g_free (errorMessage); 
    101175 
    102176        return false; 
  • trunk/src/Document.h

    r6 r8  
    1919#define __DOCUMENT_H__ 
    2020 
     21/// This is the definition of the GQuark used for Document's errors. 
     22#define EPDFVIEW_DOCUMENT_ERROR     ePDFView::Document::getErrorQuark () 
     23 
    2124namespace ePDFView  
    2225{ 
     26    /// 
     27    /// @brief Defines the possible errors loading a document. 
     28    /// 
     29    /// This enumeration matches the definitions at ErrorCodes.h of Poppler. 
     30    /// 
     31    typedef enum 
     32    { 
     33        /// No error. 
     34        DocumentErrorNone = 0, 
     35        /// Couldn't open the PDF file. 
     36        DocumentErrorOpenFile = 1, 
     37        /// Coudln't read the page catalog. 
     38        DocumentErrorBadCatalog = 2, 
     39        /// PDF file is damaged and couldn't be repaired. 
     40        DocumentErrorDamaged = 3, 
     41        /// The file is encrypted and the password was incorrect  
     42        /// or not supplied. 
     43        DocumentErrorEncrypted = 4, 
     44        /// Nonexistent or invalid highlight file. 
     45        DocumentErrorHighlightFile = 5, 
     46        /// Invalid printer. 
     47        DocumentErrorBadPrinter = 6, 
     48        /// Error during printing. 
     49        DocumentErrorPrinting = 7, 
     50        /// PDF File doesn't allow that operation. 
     51        DocumentErrorPermission = 8, 
     52        /// Invalid page number. 
     53        DocumentErrorBadPageNumber = 9, 
     54        /// File I/O Error. 
     55        DocumentErrorFileIO = 10 
     56    } DocumentError; 
     57         
    2358    /// 
    2459    /// @brief Defines the document's page mode. 
     
    71106            DocumentIndex &getDocumentIndex (void); 
    72107 
     108            static GQuark getErrorQuark (void); 
     109            static gchar *getErrorMessage (DocumentError errorCode); 
     110 
    73111        protected: 
    74112            PDFDoc *m_Document; 
     113 
     114            static GQuark errorQuark; 
    75115    }; 
    76116} 
  • trunk/src/epdfview.h

    r6 r8  
    1919#define __E_PDF_VIEW_H__ 
    2020 
    21 #include <assert.h> 
    2221#include <gettext.h> 
    2322#include <glib.h> 
  • trunk/tests/DocumentTest.cxx

    r6 r8  
    9393DocumentTest::fileNotFound (void) 
    9494{ 
    95     gchar *error_message; 
    9695    GError *error = NULL;     
    9796 
     
    9998    CPPUNIT_ASSERT (!m_Document->isLoaded ()); 
    10099     
    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,  
     100    DocumentError errorCode = (DocumentError)error->code; 
     101    gchar *documentError = Document::getErrorMessage(errorCode); 
     102    gchar *errorMessage = g_strdup_printf ( 
     103            "Failed to load document '%sNoFile.pdf'.\n%s\n",  
     104            TEST_DATA_DIR, documentError); 
     105    g_free(documentError); 
     106    CPPUNIT_ASSERT_EQUAL (DocumentErrorOpenFile, errorCode); 
     107    CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp (errorMessage,  
    104108                                                 error->message)); 
     109    g_free (errorMessage); 
     110    g_error_free (error); 
    105111} 
     112 
     113/// 
     114/// @brief Test that loading an invalid file fails. 
     115/// 
     116/// This test checks the second load failure: the file is not a PDF or  
     117/// is malformed. 
     118/// 
     119void 
     120DocumentTest::invalidFile (void) 
     121{ 
     122    gchar *error_message; 
     123    GError *error = NULL; 
     124 
     125    CPPUNIT_ASSERT (!m_Document->loadFile (TEST_DATA_DIR "DocumentTest.cxx", 
     126                                           &error)); 
     127    CPPUNIT_ASSERT (!m_Document->isLoaded ()); 
     128 
     129    DocumentError errorCode = (DocumentError)error->code; 
     130    gchar *documentError = Document::getErrorMessage(errorCode); 
     131    gchar *errorMessage = g_strdup_printf ( 
     132            "Failed to load document '%sDocumentTest.cxx'.\n%s\n",  
     133            TEST_DATA_DIR, documentError); 
     134    g_free(documentError); 
     135    CPPUNIT_ASSERT_EQUAL (DocumentErrorDamaged, errorCode); 
     136    CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp (errorMessage,  
     137                                                 error->message)); 
     138    g_free (errorMessage); 
     139    g_error_free (error); 
     140} 
  • trunk/tests/DocumentTest.h

    r6 r8  
    2828        CPPUNIT_TEST (emptyDocument); 
    2929        CPPUNIT_TEST (fileNotFound); 
     30        CPPUNIT_TEST (invalidFile); 
    3031        CPPUNIT_TEST_SUITE_END (); 
    3132 
     
    3637            void emptyDocument (void); 
    3738            void fileNotFound (void); 
    38  
     39            void invalidFile (void); 
     40             
    3941        private: 
    4042            Document *m_Document; 
  • trunk/tests/Makefile.am

    r7 r8  
    99    main.cxx 
    1010 
    11 test_epdfview_CXXFLAGS =                    \ 
    12     -DTEST_DATA_DIR='"$(top_srcdir)/src/"'  \ 
    13     -I$(top_srcdir)/src                     \ 
    14     $(GLIB_CFLAGS)                          \ 
    15     $(POPPLER_CFLAGS)                       \ 
     11test_epdfview_CXXFLAGS =                        \ 
     12    -DTEST_DATA_DIR='"$(top_srcdir)/tests/"'    \ 
     13    -I$(top_srcdir)/src                         \ 
     14    $(GLIB_CFLAGS)                              \ 
     15    $(POPPLER_CFLAGS)                           \ 
    1616    $(CPPUNIT_CFLAGS) 
    1717