Moved the handleCompressed method to part

svn path=/trunk/playground/graphics/okular/; revision=622784
This commit is contained in:
Leandro Emanuel López 2007-01-12 23:54:26 +00:00
parent 91b7675c14
commit 26f4618ae3
4 changed files with 107 additions and 106 deletions

104
part.cpp
View file

@ -53,6 +53,8 @@
#include <ktogglefullscreenaction.h>
#include <kio/job.h>
#include <kicon.h>
#include <kfilterdev.h>
// 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("<qt><strong>File Error!</strong> Could not create "
"temporary file.</qt>"));
return false;
}
m_tempfile->setAutoRemove(true);
if ( ! m_tempfile->open() )
{
KMessageBox::error( 0,
i18n("<qt><strong>File Error!</strong> Could not create temporary file "
"<nobr><strong>%1</strong></nobr>.</qt>",
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("<qt><strong>File Error!</strong> Could not open the file "
"<nobr><strong>%1</strong></nobr> for uncompression. "
"The file will not be loaded.</qt>", path),
i18n("<qt>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.</qt>"));
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("<qt><strong>File Error!</strong> Could not uncompress "
"the file <nobr><strong>%1</strong></nobr>. "
"The file will not be loaded.</qt>", path ),
i18n("<qt>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.</qt>"));
delete m_tempfile;
return false;
}
url=m_tempfile->fileName();
return true;
}
/*
* BrowserExtension class
*/

5
part.h
View file

@ -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;

View file

@ -39,9 +39,7 @@
#include <kservicetypetrader.h>
#include <ktoggleaction.h>
#include <ktogglefullscreenaction.h>
#include <ktemporaryfile.h>
#include <kfilterbase.h>
#include <kfilterdev.h>
#include <kactioncollection.h>
// 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("<qt><strong>File Error!</strong> Could not create "
"temporary file.</qt>"));
return false;
}
m_tempfile->setAutoRemove(true);
// something failed while creating the tempfile
if ( ! m_tempfile->open() )
{
KMessageBox::error( this,
i18n("<qt><strong>File Error!</strong> Could not create temporary file "
"<nobr><strong>%1</strong></nobr>.</qt>",
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("<qt><strong>File Error!</strong> Could not open the file "
"<nobr><strong>%1</strong></nobr> for uncompression. "
"The file will not be loaded.</qt>", path),
i18n("<qt>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.</qt>"));
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("<qt><strong>File Error!</strong> Could not uncompress "
"the file <nobr><strong>%1</strong></nobr>. "
"The file will not be loaded.</qt>", path ),
i18n("<qt>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.</qt>"));
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);
}

View file

@ -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;