Changeset 6 for trunk/src/Document.cxx

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).

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