Changeset 8 for trunk/src/Document.cxx

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.

Files:
1 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;