Commit f5bbf892 authored by Piotr Szymanski's avatar Piotr Szymanski

- support for taking getting a list of suported mimelsit and nice generation of it

  based on what generators provide via ktrader, includes caching the mimetype list
- handling of compressed files added too
- both fucntions basedo kviewshell's version that Wilfried Huss suggested, although with 
  significant remodelling by myself, still i see no problem on adding relevant copyrights
  to this file for the original functions' author just tell me how he/she is

svn path=/trunk/playground/graphics/oKular/kpdf/; revision=434859
parent a4730dc8
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
* Copyright (C) 2003 by Malcolm Hunter <malcolm.hunter@gmx.co.uk> * * Copyright (C) 2003 by Malcolm Hunter <malcolm.hunter@gmx.co.uk> *
* Copyright (C) 2004 by Dominique Devriese <devriese@kde.org> * * Copyright (C) 2004 by Dominique Devriese <devriese@kde.org> *
* Copyright (C) 2004 by Dirk Mueller <mueller@kde.org> * * Copyright (C) 2004 by Dirk Mueller <mueller@kde.org> *
* Copyright (C) 2005 by Piotr Szymanski <niedakh@gmail.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
...@@ -34,6 +35,10 @@ ...@@ -34,6 +35,10 @@
#include <kparts/componentfactory.h> #include <kparts/componentfactory.h>
#include <kio/netaccess.h> #include <kio/netaccess.h>
#include <kmainwindowiface.h> #include <kmainwindowiface.h>
#include <ktrader.h>
#include <ktempfile.h>
#include <kfilterbase.h>
#include <kfilterdev.h>
// local includes // local includes
#include "shell.h" #include "shell.h"
...@@ -57,7 +62,7 @@ void Shell::init() ...@@ -57,7 +62,7 @@ void Shell::init()
{ {
// set the shell's ui resource file // set the shell's ui resource file
setXMLFile("shell.rc"); setXMLFile("shell.rc");
m_fileformats=0;
// this routine will find and load our Part. it finds the Part by // 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 // name which is a bad idea usually.. but it's alright in this
// case since our Part is made for this Shell // case since our Part is made for this Shell
...@@ -109,6 +114,7 @@ void Shell::delayedOpen() ...@@ -109,6 +114,7 @@ void Shell::delayedOpen()
Shell::~Shell() Shell::~Shell()
{ {
if(m_part) writeSettings(); if(m_part) writeSettings();
if (m_fileformats) delete m_fileformats;
} }
void Shell::openURL( const KURL & url ) void Shell::openURL( const KURL & url )
...@@ -205,16 +211,183 @@ void Shell::readProperties(KConfig* config) ...@@ -205,16 +211,183 @@ void Shell::readProperties(KConfig* config)
} }
} }
void // this comes from kviewpart/kviewpart.cpp, fileformats function
Shell::fileOpen()
QStringList* Shell::fileFormats()
{
QString constraint("([X-KDE-Priority] > 0) and (exist Library) ") ;
KTrader::OfferList offers=KTrader::self()->query("oKular/Generator",QString::null,constraint, QString::null);
QStringList supportedMimeTypes;
QStringList * supportedPattern;
if (offers.isEmpty())
{
return 0;
}
bool bzip2Available = (KFilterBase::findFilterByMimeType( "application/x-bzip2" ) != 0L);
supportedPattern = new QStringList;
KTrader::OfferList::ConstIterator iterator = offers.begin();
KTrader::OfferList::ConstIterator end = offers.end();
QStringList::Iterator mimeType;
QString tmp;
QStringList mimeTypes,pattern,extensions;
QString allExt,comment;
for (; iterator != end; ++iterator)
{
KService::Ptr service = *iterator;
mimeTypes = service->serviceTypes();
for (mimeType=mimeTypes.begin();mimeType!=mimeTypes.end();++mimeType)
{
if (! (*mimeType).contains("oKular"))
{
supportedMimeTypes << *mimeType;
pattern = KMimeType::mimeType(*mimeType)->patterns();
extensions.clear();
while(!pattern.isEmpty())
{
tmp=pattern.front().stripWhiteSpace();
extensions.append(tmp);
if (tmp.find(".gz", -3) == -1)
extensions.append(tmp+".gz");
if ((bzip2Available) && (tmp.find(".bz2", -4) == -1))
extensions.append(tmp+".bz2");
pattern.pop_front();
}
comment=KMimeType::mimeType(*mimeType)->comment();
if (! comment.contains("Unknown"))
supportedPattern->append(extensions.join(" ") + "|" + comment);
allExt+=extensions.join(" ");
}
}
}
supportedPattern->prepend(allExt + "|All Files");
return supportedPattern;
}
bool Shell::handleCompressed(KURL & url, const QString &path, const KMimeType::Ptr mimetype)
{
// we are working with a compressed file, decompressing
// temporary file for decompressing
KTempFile* tmpUnzipped = new KTempFile;
if ( !tmpUnzipped )
{
KMessageBox::error(this,
i18n("<qt><strong>File Error!</strong> Could not create "
"temporary file.</qt>"));
return false;
}
tmpUnzipped->setAutoDelete(true);
// something failed while creating the tempfile
if ( tmpUnzipped->status() != 0 )
{
KMessageBox::error( this,
i18n("<qt><strong>File Error!</strong> Could not create temporary file "
"<nobr><strong>%1</strong></nobr>.</qt>").arg(
strerror(tmpUnzipped->status())));
delete tmpUnzipped;
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 tmpUnzipped;
return false;
}
if ( !filterDev->open(IO_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>").arg(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 tmpUnzipped;
return false;
}
QByteArray buf(1024);
int read = 0, wrtn = 0;
while ((read = filterDev->readBlock(buf.data(), buf.size())) > 0)
{
wrtn = tmpUnzipped->file()->writeBlock(buf.data(), read);
if ( read != wrtn )
break;
}
delete filterDev;
if ((read != 0) || (tmpUnzipped->file()->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>").arg( 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 tmpUnzipped;
return false;
}
tmpUnzipped->close();
url=tmpUnzipped->name();
}
void Shell::fileOpen()
{ {
// this slot is called whenever the File->Open menu is selected, // this slot is called whenever the File->Open menu is selected,
// the Open shortcut is pressed (usually CTRL+O) or the Open toolbar // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
// button is clicked // button is clicked
KURL url = KFileDialog::getOpenURL( QString::null, "application/pdf" );//getOpenFileName(); if (!m_fileformats)
m_fileformats = fileFormats();
if (!url.isEmpty()) if (m_fileformats)
openURL(url); {
KURL url = KFileDialog::getOpenURL( QString::null, 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);
}
else
{
KMessageBox::error(this, i18n("No oKular plugins were found."));
slotQuit();
}
} }
void void
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* Copyright (C) 2004 by Christophe Devriese * * Copyright (C) 2004 by Christophe Devriese *
* <Christophe.Devriese@student.kuleuven.ac.be> * * <Christophe.Devriese@student.kuleuven.ac.be> *
* Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es> * * Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es> *
* Copyright (C) 2005 by Piotr Szymanski <niedakh@gmail.com> *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
...@@ -21,9 +22,11 @@ ...@@ -21,9 +22,11 @@
#endif #endif
#include <kparts/mainwindow.h> #include <kparts/mainwindow.h>
#include <kmimetype.h>
namespace KPDF namespace KPDF
{ {
class Part;
/** /**
* This is the application "Shell". It has a menubar and a toolbar * This is the application "Shell". It has a menubar and a toolbar
* but relies on the "Part" to do all the real work. * but relies on the "Part" to do all the real work.
...@@ -68,7 +71,7 @@ namespace KPDF ...@@ -68,7 +71,7 @@ namespace KPDF
void readSettings(); void readSettings();
void writeSettings(); void writeSettings();
void setFullScreen( bool ); void setFullScreen( bool );
private slots: private slots:
void fileOpen(); void fileOpen();
void slotQuit(); void slotQuit();
...@@ -89,10 +92,13 @@ namespace KPDF ...@@ -89,10 +92,13 @@ namespace KPDF
void setupAccel(); void setupAccel();
void setupActions(); void setupActions();
void init(); void init();
bool handleCompressed(KURL & url, const QString &path, const KMimeType::Ptr mimetype);
QStringList* fileFormats();
private: private:
KParts::ReadOnlyPart* m_part; KParts::ReadOnlyPart* m_part;
KRecentFilesAction* m_recent; KRecentFilesAction* m_recent;
QStringList* m_fileformats;
KAction* m_printAction; KAction* m_printAction;
KToggleAction* m_fullScreenAction; KToggleAction* m_fullScreenAction;
KToggleAction* m_showMenuBarAction; KToggleAction* m_showMenuBarAction;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment