Changeset 91

Show
Ignore:
Timestamp:
04/20/06 02:37:27 (2 years ago)
Author:
jordi
Message:

The PDFDocument now can open files names that are in relative path.

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/PDFDocument.cxx

    r78 r91  
    3030static PageLayout convertPageLayout (gint pageLayout); 
    3131static PageMode convertPageMode (gint pageMode); 
     32static gchar *getAbsoluteFileName (const gchar *fileName); 
    3233 
    3334/// 
     
    8182    g_assert (NULL != filename && "Tried to load a NULL file name"); 
    8283 
    83     gchar *filename_uri = g_filename_to_uri (filename, NULL, error); 
     84    gchar *absoluteFileName = getAbsoluteFileName (filename); 
     85    gchar *filename_uri = g_filename_to_uri (absoluteFileName, NULL, error); 
     86    g_free (absoluteFileName); 
    8487    if ( NULL == filename_uri ) 
    8588    { 
     
    276279 
    277280/// 
    278 /// @brief Gets the document's page layout from Poppler's page layout. 
    279 /// 
    280 /// @param pageLayout Is the page layout that Poppler's glib wrapper gives. 
    281 /// 
    282 /// @return The PageLayout based on @a pageLayout. 
    283 /// 
    284 PageLayout 
    285 convertPageLayout (gint pageLayout) 
    286 { 
    287     PageLayout layout = PageLayoutUnset; 
    288     switch (pageLayout) 
    289     { 
    290         case POPPLER_PAGE_LAYOUT_SINGLE_PAGE: 
    291             layout = PageLayoutSinglePage; 
    292             break; 
    293         case POPPLER_PAGE_LAYOUT_ONE_COLUMN: 
    294             layout = PageLayoutOneColumn; 
    295             break; 
    296         case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT: 
    297             layout = PageLayoutTwoColumnLeft; 
    298             break; 
    299         case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT: 
    300             layout = PageLayoutTwoColumnRight; 
    301             break; 
    302         case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT: 
    303             layout = PageLayoutTwoPageLeft; 
    304             break; 
    305         case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT: 
    306             layout = PageLayoutTwoPageRight; 
    307             break; 
    308         case POPPLER_PAGE_LAYOUT_UNSET: 
    309         default: 
    310             layout = PageLayoutUnset; 
    311     } 
    312  
    313     return layout; 
    314 } 
    315  
    316 /// 
    317 /// @brief Get the document's page mode. 
    318 /// 
    319 /// @param pageLayout Is the page mode that Poppler's catalog gives. 
    320 /// 
    321 /// @return The PageLayout based on @a pageMode. 
    322 /// 
    323 PageMode 
    324 convertPageMode (gint pageMode) 
    325 { 
    326     PageMode mode = PageModeUnset; 
    327     switch (pageMode) 
    328     { 
    329         case POPPLER_PAGE_MODE_USE_OUTLINES: 
    330             mode = PageModeOutlines; 
    331             break; 
    332         case POPPLER_PAGE_MODE_USE_THUMBS: 
    333             mode = PageModeThumbs; 
    334             break; 
    335         case POPPLER_PAGE_MODE_FULL_SCREEN: 
    336             mode = PageModeFullScreen; 
    337             break; 
    338         case POPPLER_PAGE_MODE_USE_OC: 
    339             mode = PageModeOC; 
    340             break; 
    341         case POPPLER_PAGE_MODE_USE_ATTACHMENTS: 
    342             mode = PageModeAttach; 
    343             break; 
    344         case POPPLER_PAGE_MODE_NONE: 
    345         case POPPLER_PAGE_MODE_UNSET: 
    346         default: 
    347             mode = PageModeUnset; 
    348     } 
    349  
    350     return mode; 
    351 } 
    352  
    353 /// 
    354281/// @brief Gets the current page's unscaled size. 
    355282/// 
     
    425352    return (renderedPage); 
    426353} 
     354 
     355/// 
     356/// @brief Gets the document's page layout from Poppler's page layout. 
     357/// 
     358/// @param pageLayout Is the page layout that Poppler's glib wrapper gives. 
     359/// 
     360/// @return The PageLayout based on @a pageLayout. 
     361/// 
     362PageLayout 
     363convertPageLayout (gint pageLayout) 
     364{ 
     365    PageLayout layout = PageLayoutUnset; 
     366    switch (pageLayout) 
     367    { 
     368        case POPPLER_PAGE_LAYOUT_SINGLE_PAGE: 
     369            layout = PageLayoutSinglePage; 
     370            break; 
     371        case POPPLER_PAGE_LAYOUT_ONE_COLUMN: 
     372            layout = PageLayoutOneColumn; 
     373            break; 
     374        case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT: 
     375            layout = PageLayoutTwoColumnLeft; 
     376            break; 
     377        case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT: 
     378            layout = PageLayoutTwoColumnRight; 
     379            break; 
     380        case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT: 
     381            layout = PageLayoutTwoPageLeft; 
     382            break; 
     383        case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT: 
     384            layout = PageLayoutTwoPageRight; 
     385            break; 
     386        case POPPLER_PAGE_LAYOUT_UNSET: 
     387        default: 
     388            layout = PageLayoutUnset; 
     389    } 
     390 
     391    return layout; 
     392} 
     393 
     394/// 
     395/// @brief Get the document's page mode. 
     396/// 
     397/// @param pageLayout Is the page mode that Poppler's catalog gives. 
     398/// 
     399/// @return The PageLayout based on @a pageMode. 
     400/// 
     401PageMode 
     402convertPageMode (gint pageMode) 
     403{ 
     404    PageMode mode = PageModeUnset; 
     405    switch (pageMode) 
     406    { 
     407        case POPPLER_PAGE_MODE_USE_OUTLINES: 
     408            mode = PageModeOutlines; 
     409            break; 
     410        case POPPLER_PAGE_MODE_USE_THUMBS: 
     411            mode = PageModeThumbs; 
     412            break; 
     413        case POPPLER_PAGE_MODE_FULL_SCREEN: 
     414            mode = PageModeFullScreen; 
     415            break; 
     416        case POPPLER_PAGE_MODE_USE_OC: 
     417            mode = PageModeOC; 
     418            break; 
     419        case POPPLER_PAGE_MODE_USE_ATTACHMENTS: 
     420            mode = PageModeAttach; 
     421            break; 
     422        case POPPLER_PAGE_MODE_NONE: 
     423        case POPPLER_PAGE_MODE_UNSET: 
     424        default: 
     425            mode = PageModeUnset; 
     426    } 
     427 
     428    return mode; 
     429} 
     430 
     431/// 
     432/// @brief Gets the absolute path of a filename. 
     433/// 
     434/// This function checks if the given @a fileName is an absolute path. If 
     435/// it is then it returns a copy of it, otherwise it prepends the current 
     436/// working directory to it. 
     437/// 
     438/// @param fileName The filename to get the absolute path from. 
     439/// 
     440/// @return A copy of the absolute path to the file name. This copy must be 
     441///         freed when no longer needed. 
     442/// 
     443gchar * 
     444getAbsoluteFileName (const gchar *fileName) 
     445{ 
     446    gchar *absoluteFileName = NULL; 
     447    if ( g_path_is_absolute (fileName) ) 
     448    { 
     449        absoluteFileName = g_strdup (fileName); 
     450    } 
     451    else 
     452    { 
     453        gchar *currentDir = g_get_current_dir (); 
     454        absoluteFileName = g_build_filename (currentDir, fileName, NULL); 
     455        g_free (currentDir); 
     456    } 
     457 
     458    return absoluteFileName; 
     459} 
  • trunk/tests/PDFDocumentTest.cxx

    r78 r91  
    3737    gchar *currentDir = g_get_current_dir (); 
    3838    gchar *filePath = g_build_filename (currentDir, TEST_DIR, fileName, NULL); 
     39    g_free (currentDir); 
    3940 
    4041    return filePath; 
     
    289290    g_free (testFile); 
    290291} 
     292 
     293/// 
     294/// @brief Test loading a relative path filename. 
     295/// 
     296/// Poppler's glib wrapper needs an URI instead of just a filename, but 
     297/// in order to build it I need an absolute path. This check test that 
     298/// giving a relative path converts it to absolute and loads the file 
     299/// properly. 
     300/// 
     301void 
     302PDFDocumentTest::relativePath () 
     303{ 
     304    CPPUNIT_ASSERT (m_Document->loadFile (TEST_DIR "test1.pdf", NULL, NULL)); 
     305    CPPUNIT_ASSERT (m_Document->isLoaded ()); 
     306    CPPUNIT_ASSERT_EQUAL (0, 
     307            g_ascii_strcasecmp (TEST_DIR "test1.pdf",  
     308                                m_Document->getFileName ())); 
     309} 
     310 
    291311 
    292312/// 
  • trunk/tests/PDFDocumentTest.h

    r21 r91  
    3131        CPPUNIT_TEST (encryptedFile); 
    3232        CPPUNIT_TEST (validFile); 
     33        CPPUNIT_TEST (relativePath); 
    3334        CPPUNIT_TEST (pageChange); 
    3435        CPPUNIT_TEST (pageRotate); 
     
    4647            void encryptedFile (void); 
    4748            void validFile (void); 
     49            void relativePath (void); 
    4850            void pageChange (void); 
    4951            void pageRotate (void);