From 26f4618ae3169f0e60264be9ff4d64071a7c4b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Emanuel=20L=C3=B3pez?= Date: Fri, 12 Jan 2007 23:54:26 +0000 Subject: [PATCH] Moved the handleCompressed method to part svn path=/trunk/playground/graphics/okular/; revision=622784 --- part.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++++++- part.h | 5 ++- shell/shell.cpp | 101 +--------------------------------------------- shell/shell.h | 3 -- 4 files changed, 107 insertions(+), 106 deletions(-) diff --git a/part.cpp b/part.cpp index 50382087e..f8fbdf83f 100644 --- a/part.cpp +++ b/part.cpp @@ -53,6 +53,8 @@ #include #include #include +#include + // local includes #include "part.h" #include "ui/pageview.h" @@ -711,8 +713,24 @@ bool Part::openUrl(const KUrl &url) // if it matches then: download it (if not local) extract to a temp file using // KTar and proceed with the URL of the temporary file + const QString path = url.path(); + const KMimeType::Ptr mimetype = KMimeType::findByPath( path ); + bool isCompressedFile = false; + KUrl tempUrl; + if (( mimetype->name() == "application/x-gzip" ) + || ( mimetype->name() == "application/x-bzip2" ) + || ( mimetype->parentMimeType() == "application/x-gzip" ) + || ( mimetype->parentMimeType() == "application/x-bzip2" ) + ) + { + isCompressedFile=handleCompressed(tempUrl,path,mimetype); + } + // this calls in sequence the 'closeUrl' and 'openFile' methods - bool openOk = KParts::ReadOnlyPart::openUrl(url); + bool openOk; + if ( !isCompressedFile ) + openOk = KParts::ReadOnlyPart::openUrl(url); + else openOk = KParts::ReadOnlyPart::openUrl(tempUrl); if ( openOk ) { @@ -1374,6 +1392,90 @@ void Part::psTransformEnded() openFile(); } +bool Part::handleCompressed(KUrl & url, const QString &path, const KMimeType::Ptr mimetype) +{ + + // we are working with a compressed file, decompressing + // temporary file for decompressing + KTemporaryFile *m_tempfile = new KTemporaryFile; + if ( !m_tempfile ) + { + KMessageBox::error( 0, + i18n("File Error! Could not create " + "temporary file.")); + return false; + } + + m_tempfile->setAutoRemove(true); + + if ( ! m_tempfile->open() ) + { + KMessageBox::error( 0, + i18n("File Error! Could not create temporary file " + "%1.", + strerror(m_tempfile->error()))); + delete m_tempfile; + return false; + } + + // decompression filer + QIODevice* filterDev; + if (( mimetype->parentMimeType() == "application/x-gzip" ) || + ( mimetype->parentMimeType() == "application/x-bzip2" )) + filterDev = KFilterDev::deviceForFile(path, mimetype->parentMimeType()); + else + filterDev = KFilterDev::deviceForFile(path); + + if (!filterDev) + { + delete m_tempfile; + return false; + } + + if ( !filterDev->open(QIODevice::ReadOnly) ) + { + KMessageBox::detailedError( 0, + i18n("File Error! Could not open the file " + "%1 for uncompression. " + "The file will not be loaded.", path), + i18n("This error typically occurs if you do " + "not have enough permissions to read the file. " + "You can check ownership and permissions if you " + "right-click on the file in the Konqueror " + "file manager and then choose the 'Properties' menu.")); + + delete filterDev; + delete m_tempfile; + return false; + } + + QByteArray buf(1024, '\0'); + int read = 0, wrtn = 0; + + while ((read = filterDev->read(buf.data(), buf.size())) > 0) + { + wrtn = m_tempfile->write(buf.data(), read); + if ( read != wrtn ) + break; + } + delete filterDev; + if ((read != 0) || (m_tempfile->size() == 0)) + { + KMessageBox::detailedError(0, + i18n("File Error! Could not uncompress " + "the file %1. " + "The file will not be loaded.", path ), + i18n("This error typically occurs if the file is corrupt. " + "If you want to be sure, try to decompress the file manually " + "using command-line tools.")); + delete m_tempfile; + return false; + } + url=m_tempfile->fileName(); + return true; +} + + /* * BrowserExtension class */ diff --git a/part.h b/part.h index f5b71c133..2ee1a1a99 100644 --- a/part.h +++ b/part.h @@ -154,7 +154,8 @@ public slots: private: void doPrint( KPrinter& printer ); void fillGenerators(); - + bool handleCompressed(KUrl & url, const QString &path, const KMimeType::Ptr mimetype); + // the document Okular::Document * m_document; QString m_temporaryLocalFile; @@ -204,7 +205,7 @@ private: QAction *m_showPresentation; KToggleAction* m_showMenuBarAction; KToggleAction* m_showLeftPanel; - KToggleFullScreenAction* m_showFullScreenAction; + KToggleFullScreenAction* m_showFullScreenAction; bool m_actionsSearched; bool m_searchStarted; diff --git a/shell/shell.cpp b/shell/shell.cpp index 7bbf248da..9295097a1 100644 --- a/shell/shell.cpp +++ b/shell/shell.cpp @@ -39,9 +39,7 @@ #include #include #include -#include #include -#include #include // local includes #include "shell.h" @@ -62,7 +60,6 @@ void Shell::init() setXMLFile("shell.rc"); m_doc=0L; m_fileformats=0L; - m_tempfile=0L; // this routine will find and load our Part. it finds the Part by // name which is a bad idea usually.. but it's alright in this // case since our Part is made for this Shell @@ -124,7 +121,6 @@ Shell::~Shell() { if ( m_part ) writeSettings(); if ( m_fileformats ) delete m_fileformats; - if ( m_tempfile ) delete m_tempfile; delete m_part; } @@ -264,89 +260,6 @@ QStringList* Shell::fileFormats() } -bool Shell::handleCompressed(KUrl & url, const QString &path, const KMimeType::Ptr mimetype) -{ - - // we are working with a compressed file, decompressing - // temporary file for decompressing - m_tempfile = new KTemporaryFile; - if ( !m_tempfile ) - { - KMessageBox::error(this, - i18n("File Error! Could not create " - "temporary file.")); - return false; - } - - m_tempfile->setAutoRemove(true); - - // something failed while creating the tempfile - if ( ! m_tempfile->open() ) - { - KMessageBox::error( this, - i18n("File Error! Could not create temporary file " - "%1.", - strerror(m_tempfile->error()))); - delete m_tempfile; - return false; - } - - // decompression filer - QIODevice* filterDev; - if (( mimetype->parentMimeType() == "application/x-gzip" ) || - ( mimetype->parentMimeType() == "application/x-bzip2" )) - filterDev = KFilterDev::deviceForFile(path, mimetype->parentMimeType()); - else - filterDev = KFilterDev::deviceForFile(path); - - if (!filterDev) - { - delete m_tempfile; - return false; - } - - if ( !filterDev->open(QIODevice::ReadOnly) ) - { - KMessageBox::detailedError( this, - i18n("File Error! Could not open the file " - "%1 for uncompression. " - "The file will not be loaded.", path), - i18n("This error typically occurs if you do " - "not have enough permissions to read the file. " - "You can check ownership and permissions if you " - "right-click on the file in the Konqueror " - "file manager and then choose the 'Properties' menu.")); - - delete filterDev; - delete m_tempfile; - return false; - } - - QByteArray buf(1024, '\0'); - int read = 0, wrtn = 0; - - while ((read = filterDev->read(buf.data(), buf.size())) > 0) - { - wrtn = m_tempfile->write(buf.data(), read); - if ( read != wrtn ) - break; - } - delete filterDev; - if ((read != 0) || (m_tempfile->size() == 0)) - { - KMessageBox::detailedError(this, - i18n("File Error! Could not uncompress " - "the file %1. " - "The file will not be loaded.", path ), - i18n("This error typically occurs if the file is corrupt. " - "If you want to be sure, try to decompress the file manually " - "using command-line tools.")); - delete m_tempfile; - return false; - } - url=m_tempfile->fileName(); - return true; -} void Shell::fileOpen() { @@ -363,19 +276,7 @@ void Shell::fileOpen() startDir = m_openUrl.path(); KUrl url = KFileDialog::getOpenUrl( startDir, m_fileformats->join("\n") );//getOpenFileName(); bool reallyOpen=!url.isEmpty(); - if (reallyOpen) - { - QString path = url.path(); - KMimeType::Ptr mimetype = KMimeType::findByPath( path ); - if (( mimetype->name() == "application/x-gzip" ) - || ( mimetype->name() == "application/x-bzip2" ) - || ( mimetype->parentMimeType() == "application/x-gzip" ) - || ( mimetype->parentMimeType() == "application/x-bzip2" ) - ) - { - reallyOpen=handleCompressed(url,path,mimetype); - } - } + if (reallyOpen) openUrl(url); } diff --git a/shell/shell.h b/shell/shell.h index fbc8105b3..a94955d50 100644 --- a/shell/shell.h +++ b/shell/shell.h @@ -25,7 +25,6 @@ class KCmdLineArgs; class KRecentFilesAction; -class KTemporaryFile; class KToggleAction; class KDocumentViewer; @@ -92,14 +91,12 @@ private: void setupAccel(); void setupActions(); void init(); - bool handleCompressed(KUrl & url, const QString &path, const KMimeType::Ptr mimetype); QStringList* fileFormats(); private: KCmdLineArgs* m_args; KParts::ReadOnlyPart* m_part; KDocumentViewer* m_doc; - KTemporaryFile* m_tempfile; KRecentFilesAction* m_recent; QStringList* m_fileformats; KAction* m_printAction;