Merge remote-tracking branch 'origin/KDE/4.14'

This commit is contained in:
Albert Astals Cid 2014-08-08 22:42:12 +02:00
commit a170e35ca8
13 changed files with 746 additions and 201 deletions

View file

@ -4,6 +4,7 @@ File=okular.kcfg
Inherits=SettingsCore
Mutators=true
Singleton=true
IncludeFiles=settings_core.h
Visibility=OKULAR_PART_EXPORT
IncludeFiles=settings_core.h,okular_part_export.h
SourceIncludeFiles=kstandarddirs.h,qdom.h
MemberVariables=dpointer

View file

@ -2105,6 +2105,26 @@ private:
const KMimeType::Ptr &_mime;
};
QString DocumentPrivate::docDataFileName(const KUrl &url, qint64 document_size)
{
QString fn = url.fileName();
fn = QString::number( document_size ) + '.' + fn + ".xml";
QString newokular = "okular/docdata/" + fn;
QString newokularfile = KStandardDirs::locateLocal( "data", newokular );
if ( !QFile::exists( newokularfile ) )
{
QString oldkpdf = "kpdf/" + fn;
QString oldkpdffile = KStandardDirs::locateLocal( "data", oldkpdf );
if ( QFile::exists( oldkpdffile ) )
{
// ### copy or move?
if ( !QFile::copy( oldkpdffile, newokularfile ) )
return QString();
}
}
return newokularfile;
}
Document::OpenResult Document::openDocument( const QString & docFile, const KUrl& url, const KMimeType::Ptr &_mime, const QString & password )
{
KMimeType::Ptr mime = _mime;
@ -2129,23 +2149,8 @@ Document::OpenResult Document::openDocument( const QString & docFile, const KUrl
d->m_docFileName = docFile;
if ( url.isLocalFile() && !d->m_archiveData )
{
QString fn = url.fileName();
document_size = fileReadTest.size();
fn = QString::number( document_size ) + '.' + fn + ".xml";
QString newokular = "okular/docdata/" + fn;
QString newokularfile = KStandardDirs::locateLocal( "data", newokular );
if ( !QFile::exists( newokularfile ) )
{
QString oldkpdf = "kpdf/" + fn;
QString oldkpdffile = KStandardDirs::locateLocal( "data", oldkpdf );
if ( QFile::exists( oldkpdffile ) )
{
// ### copy or move?
if ( !QFile::copy( oldkpdffile, newokularfile ) )
return OpenError;
}
}
d->m_xmlFileName = newokularfile;
d->m_xmlFileName = DocumentPrivate::docDataFileName(url, document_size);
}
}
else

View file

@ -136,6 +136,7 @@ class DocumentPrivate
bool canModifyExternalAnnotations() const;
bool canRemoveExternalAnnotations() const;
void warnLimitedAnnotSupport();
OKULAR_EXPORT static QString docDataFileName(const KUrl &url, qint64 document_size);
// Methods that implement functionality needed by undo commands
void performAddPageAnnotation( int page, Annotation *annotation );

View file

@ -17,6 +17,7 @@ endif(KActivities_FOUND)
set(okular_SRCS
main.cpp
okular_main.cpp
shell.cpp
shellutils.cpp
)

View file

@ -15,111 +15,10 @@
#include "shell.h"
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <QtDBus/qdbusinterface.h>
#include <QTextStream>
#include <kwindowsystem.h>
#include "aboutdata.h"
#include "okular_main.h"
#include "shellutils.h"
static bool attachUniqueInstance(KCmdLineArgs* args)
{
if (!args->isSet("unique") || args->count() != 1)
return false;
QDBusInterface iface("org.kde.okular", "/okular", "org.kde.okular");
QDBusInterface iface2("org.kde.okular", "/okularshell", "org.kde.okular");
if (!iface.isValid() || !iface2.isValid())
return false;
if (args->isSet("print"))
iface.call("enableStartWithPrint");
if (args->isSet("page"))
iface.call("openDocument", ShellUtils::urlFromArg(args->arg(0), ShellUtils::qfileExistFunc(), args->getOption("page")).url());
else
iface.call("openDocument", ShellUtils::urlFromArg(args->arg(0), ShellUtils::qfileExistFunc()).url());
if (args->isSet("raise")) {
iface2.call("tryRaise");
}
return true;
}
// Ask an existing non-unique instance to open new tabs
static bool attachExistingInstance( KCmdLineArgs* args )
{
if ( args->count() < 1 )
return false;
const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
// Don't match the service without trailing "-" (unique instance)
const QString pattern = "org.kde.okular-";
const QString myPid = QString::number( kapp->applicationPid() );
QScopedPointer<QDBusInterface> bestService;
const int desktop = KWindowSystem::currentDesktop();
// Select the first instance that isn't us (metric may change in future)
foreach ( const QString& service, services )
{
if ( service.startsWith(pattern) && !service.endsWith( myPid ) )
{
bestService.reset( new QDBusInterface(service, "/okularshell", "org.kde.okular") );
// Find a window that can handle our documents
const QDBusReply<bool> reply = bestService->call( "canOpenDocs", args->count(), desktop );
if( reply.isValid() && reply.value() )
break;
bestService.reset();
}
}
if ( !bestService )
return false;
for( int i = 0; i < args->count(); ++i )
{
QString arg = args->arg( i );
// Copy stdin to temporary file which can be opened by the existing
// window. The temp file is automatically deleted after it has been
// opened. Not sure if this behavior is safe on all platforms.
QScopedPointer<QTemporaryFile> tempFile;
if( arg == "-" )
{
tempFile.reset( new QTemporaryFile );
QFile stdinFile;
if( !tempFile->open() || !stdinFile.open(stdin,QIODevice::ReadOnly) )
return false;
const size_t bufSize = 1024*1024;
QScopedPointer<char,QScopedPointerArrayDeleter<char> > buf( new char[bufSize] );
size_t bytes;
do
{
bytes = stdinFile.read( buf.data(), bufSize );
tempFile->write( buf.data(), bytes );
} while( bytes != 0 );
arg = tempFile->fileName();
}
else
{
arg = args->url( i ).url();
}
// Returns false if it can't fit another document
const QDBusReply<bool> reply = bestService->call( "openDocument", arg );
if( !reply.isValid() || !reply.value() )
return false;
}
bestService->call( "tryRaise" );
return true;
}
int main(int argc, char** argv)
{
KAboutData about = okularAboutData( "okular", I18N_NOOP( "Okular" ) );
@ -141,37 +40,24 @@ int main(int argc, char** argv)
if (app.isSessionRestored())
{
RESTORE(Shell);
} else {
}
else
{
// no session.. just start up normally
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
// try to attach to existing session, unique or not
if (attachUniqueInstance(args) || attachExistingInstance(args))
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
QStringList paths;
for ( int i = 0; i < args->count(); ++i )
paths << args->arg(i);
Okular::Status status = Okular::main(paths, ShellUtils::serializeOptions(*args));
switch (status)
{
args->clear();
return 0;
}
if (args->isSet( "unique" ) && args->count() > 1)
{
QTextStream stream(stderr);
stream << i18n( "Error: Can't open more than one document with the --unique switch" ) << endl;
return -1;
}
else
{
Shell* shell = new Shell( args );
shell->show();
for ( int i = 0; i < args->count(); )
{
if ( shell->openDocument( args->arg(i)) )
++i;
else
{
shell = new Shell( args );
shell->show();
}
}
case Okular::Error:
return -1;
case Okular::AttachedOtherProcess:
return 0;
case Okular::Success:
// Do nothing
break;
}
}

159
shell/okular_main.cpp Normal file
View file

@ -0,0 +1,159 @@
/***************************************************************************
* Copyright (C) 2002 by Wilco Greven <greven@kde.org> *
* Copyright (C) 2003 by Christophe Devriese *
* <Christophe.Devriese@student.kuleuven.ac.be> *
* Copyright (C) 2003 by Laurent Montel <montel@kde.org> *
* Copyright (C) 2003-2007 by Albert Astals Cid <aacid@kde.org> *
* Copyright (C) 2004 by Andy Goossens <andygoossens@telenet.be> *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "okular_main.h"
#include "shell.h"
#include <kapplication.h>
#include <klocale.h>
#include <QtDBus/qdbusinterface.h>
#include <QTextStream>
#include <kwindowsystem.h>
#include "aboutdata.h"
#include "shellutils.h"
static bool attachUniqueInstance(const QStringList &paths, const QString &serializedOptions)
{
if (!ShellUtils::unique(serializedOptions) || paths.count() != 1)
return false;
QDBusInterface iface("org.kde.okular", "/okularshell", "org.kde.okular");
if (!iface.isValid())
return false;
const QString page = ShellUtils::page(serializedOptions);
iface.call("openDocument", ShellUtils::urlFromArg(paths[0], ShellUtils::qfileExistFunc(), page).url(), serializedOptions);
if (!ShellUtils::noRaise(serializedOptions)) {
iface.call("tryRaise");
}
return true;
}
// Ask an existing non-unique instance to open new tabs
static bool attachExistingInstance(const QStringList &paths, const QString &serializedOptions)
{
if ( paths.count() < 1 )
return false;
const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
// Don't match the service without trailing "-" (unique instance)
const QString pattern = "org.kde.okular-";
const QString myPid = QString::number( kapp->applicationPid() );
QScopedPointer<QDBusInterface> bestService;
const int desktop = KWindowSystem::currentDesktop();
// Select the first instance that isn't us (metric may change in future)
foreach ( const QString& service, services )
{
if ( service.startsWith(pattern) && !service.endsWith( myPid ) )
{
bestService.reset( new QDBusInterface(service, "/okularshell", "org.kde.okular") );
// Find a window that can handle our documents
const QDBusReply<bool> reply = bestService->call( "canOpenDocs", paths.count(), desktop );
if( reply.isValid() && reply.value() )
break;
bestService.reset();
}
}
if ( !bestService )
return false;
foreach( const QString &arg, paths )
{
// Copy stdin to temporary file which can be opened by the existing
// window. The temp file is automatically deleted after it has been
// opened. Not sure if this behavior is safe on all platforms.
QScopedPointer<QTemporaryFile> tempFile;
QString path;
if( arg == "-" )
{
tempFile.reset( new QTemporaryFile );
QFile stdinFile;
if( !tempFile->open() || !stdinFile.open(stdin,QIODevice::ReadOnly) )
return false;
const size_t bufSize = 1024*1024;
QScopedPointer<char,QScopedPointerArrayDeleter<char> > buf( new char[bufSize] );
size_t bytes;
do
{
bytes = stdinFile.read( buf.data(), bufSize );
tempFile->write( buf.data(), bytes );
} while( bytes != 0 );
path = tempFile->fileName();
}
else
{
// Page only makes sense if we are opening one file
const QString page = paths.count() == 1 ? ShellUtils::page(serializedOptions) : QString();
path = ShellUtils::urlFromArg(arg, ShellUtils::qfileExistFunc(), page).url();
}
// Returns false if it can't fit another document
const QDBusReply<bool> reply = bestService->call( "openDocument", path, serializedOptions );
if( !reply.isValid() || !reply.value() )
return false;
}
bestService->call( "tryRaise" );
return true;
}
namespace Okular {
Status main(const QStringList &paths, const QString &serializedOptions)
{
if (ShellUtils::unique(serializedOptions) && paths.count() > 1)
{
QTextStream stream(stderr);
stream << i18n( "Error: Can't open more than one document with the --unique switch" ) << endl;
return Error;
}
// try to attach to existing session, unique or not
if (attachUniqueInstance(paths, serializedOptions) || attachExistingInstance(paths, serializedOptions))
{
return AttachedOtherProcess;
}
Shell* shell = new Shell( serializedOptions );
shell->show();
for ( int i = 0; i < paths.count(); )
{
// Page only makes sense if we are opening one file
const QString page = paths.count() == 1 ? ShellUtils::page(serializedOptions) : QString();
if ( shell->openDocument( ShellUtils::urlFromArg(paths[i], ShellUtils::qfileExistFunc(), page).url(), serializedOptions) )
{
++i;
}
else
{
shell = new Shell( serializedOptions );
shell->show();
}
}
return Success;
}
}
/* kate: replace-tabs on; indent-width 4; */

27
shell/okular_main.h Normal file
View file

@ -0,0 +1,27 @@
/***************************************************************************
* Copyright (C) 2002 by Wilco Greven <greven@kde.org> *
* Copyright (C) 2003 by Christophe Devriese *
* <Christophe.Devriese@student.kuleuven.ac.be> *
* Copyright (C) 2003 by Laurent Montel <montel@kde.org> *
* Copyright (C) 2003-2007 by Albert Astals Cid <aacid@kde.org> *
* Copyright (C) 2004 by Andy Goossens <andygoossens@telenet.be> *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
class QString;
class QStringList;
namespace Okular
{
enum Status { Error, AttachedOtherProcess, Success };
Status main(const QStringList &paths, const QString &serializedOptions);
}
/* kate: replace-tabs on; indent-width 4; */

View file

@ -25,7 +25,6 @@
#include <QtDBus/qdbusconnection.h>
#include <kaction.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kfiledialog.h>
#include <kpluginloader.h>
#include <kmessagebox.h>
@ -60,21 +59,11 @@
static const char *shouldShowMenuBarComingFromFullScreen = "shouldShowMenuBarComingFromFullScreen";
static const char *shouldShowToolBarComingFromFullScreen = "shouldShowToolBarComingFromFullScreen";
Shell::Shell(KCmdLineArgs* args, int argIndex)
: KParts::MainWindow(), m_args(args), m_menuBarWasShown(true), m_toolBarWasShown(true)
Shell::Shell( const QString &serializedOptions )
: KParts::MainWindow(), m_menuBarWasShown(true), m_toolBarWasShown(true)
#ifdef KActivities_FOUND
, m_activityResource(0)
#endif
{
if (m_args && argIndex != -1)
{
m_openUrl = ShellUtils::urlFromArg(m_args->arg(argIndex),
ShellUtils::qfileExistFunc(), m_args->getOption("page"));
}
init();
}
void Shell::init()
{
setObjectName( QLatin1String( "okular::Shell" ) );
setContextMenuPolicy( Qt::NoContextMenu );
@ -125,22 +114,19 @@ void Shell::init()
readSettings();
m_unique = false;
if (m_args && m_args->isSet("unique") && m_args->count() <= 1)
m_unique = ShellUtils::unique(serializedOptions);
if (m_unique)
{
m_unique = QDBusConnection::sessionBus().registerService("org.kde.okular");
if (!m_unique)
KMessageBox::information(this, i18n("There is already a unique Okular instance running. This instance won't be the unique one."));
}
if (m_args && !m_args->isSet("raise"))
if (ShellUtils::noRaise(serializedOptions))
{
setAttribute(Qt::WA_ShowWithoutActivating);
}
QDBusConnection::sessionBus().registerObject("/okularshell", this, QDBusConnection::ExportScriptableSlots);
if (m_openUrl.isValid()) QTimer::singleShot(0, this, SLOT(delayedOpen()));
}
else
{
@ -148,11 +134,6 @@ void Shell::init()
}
}
void Shell::delayedOpen()
{
openUrl( m_openUrl );
}
void Shell::showOpenRecentMenu()
{
m_recent->menu()->popup(QCursor::pos());
@ -168,13 +149,13 @@ Shell::~Shell()
it->part->closeUrl( false );
}
}
if ( m_args )
m_args->clear();
if (m_unique)
QDBusConnection::sessionBus().unregisterService("org.kde.okular");
}
// Open a new document if we have space for it
// This can hang if called on a unique instance and openUrl pops a messageBox
bool Shell::openDocument( const QString& doc )
bool Shell::openDocument( const QString& url, const QString &serializedOptions )
{
if( m_tabs.size() <= 0 )
return false;
@ -182,11 +163,14 @@ bool Shell::openDocument( const QString& doc )
KParts::ReadWritePart* const part = m_tabs[0].part;
// Return false if we can't open new tabs and the only part is occupied
if( !dynamic_cast<Okular::ViewerInterface*>(part)->openNewFilesInTabs()
&& !part->url().isEmpty() )
if ( !dynamic_cast<Okular::ViewerInterface*>(part)->openNewFilesInTabs()
&& !part->url().isEmpty()
&& !ShellUtils::unique(serializedOptions))
{
return false;
}
openUrl( ShellUtils::urlFromArg(doc,ShellUtils::qfileExistFunc()) );
openUrl( url, serializedOptions );
return true;
}
@ -209,7 +193,7 @@ bool Shell::canOpenDocs( int numDocs, int desktop )
return true;
}
void Shell::openUrl( const KUrl & url )
void Shell::openUrl( const KUrl & url, const QString &serializedOptions )
{
const int activeTab = m_tabWidget->currentIndex();
if ( activeTab < m_tabs.size() )
@ -219,19 +203,19 @@ void Shell::openUrl( const KUrl & url )
{
if( m_unique )
{
KMessageBox::error(this, i18n("Can't open more than one document in the unique Okular instance."));
applyOptionsToPart( activePart, serializedOptions );
activePart->openUrl( url );
}
else
{
if( dynamic_cast<Okular::ViewerInterface *>(activePart)->openNewFilesInTabs() )
{
openNewTab( url );
setActiveTab( m_tabs.size()-1 );
openNewTab( url, serializedOptions );
}
else
{
Shell* newShell = new Shell();
newShell->openUrl( url );
Shell* newShell = new Shell( serializedOptions );
newShell->openUrl( url, serializedOptions );
newShell->show();
}
}
@ -239,13 +223,7 @@ void Shell::openUrl( const KUrl & url )
else
{
m_tabWidget->setTabText( activeTab, url.fileName() );
if ( m_args ){
KDocumentViewer* const doc = qobject_cast<KDocumentViewer*>(activePart);
if ( doc && m_args->isSet( "presentation" ) )
doc->startPresentation();
if ( m_args->isSet( "print" ) )
QMetaObject::invokeMethod( activePart, "enableStartWithPrint" );
}
applyOptionsToPart( activePart, serializedOptions );
bool openOk = activePart->openUrl( url );
const bool isstdin = url.fileName( KUrl::ObeyTrailingSlash ) == QLatin1String( "-" );
if ( !isstdin )
@ -525,7 +503,7 @@ void Shell::closeTab( int tab )
}
void Shell::openNewTab( const KUrl& url )
void Shell::openNewTab( const KUrl& url, const QString &serializedOptions )
{
// Tabs are hidden when there's only one, so show it
if( m_tabs.size() == 1 )
@ -545,8 +523,24 @@ void Shell::openNewTab( const KUrl& url )
KParts::ReadWritePart* const part = m_tabs[newIndex].part;
m_tabWidget->addTab( part->widget(), url.fileName() );
applyOptionsToPart(part, serializedOptions);
int previousActiveTab = m_tabWidget->currentIndex();
setActiveTab( m_tabs.size() - 1 );
if( part->openUrl(url) )
m_recent->addUrl( url );
else
setActiveTab( previousActiveTab );
}
void Shell::applyOptionsToPart( QObject* part, const QString &serializedOptions )
{
KDocumentViewer* const doc = qobject_cast<KDocumentViewer*>(part);
if ( ShellUtils::startInPresentation(serializedOptions) )
doc->startPresentation();
if ( ShellUtils::showPrintDialog(serializedOptions) )
QMetaObject::invokeMethod( part, "enableStartWithPrint" );
}
void Shell::connectPart( QObject* part )

View file

@ -21,7 +21,6 @@
#include <QtDBus/QtDBus>
class KCmdLineArgs;
class KRecentFilesAction;
class KToggleAction;
class KTabWidget;
@ -47,11 +46,13 @@ class Shell : public KParts::MainWindow
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.okular")
friend class MainShellTest;
public:
/**
* Constructor
*/
explicit Shell(KCmdLineArgs* args = 0, int argIndex = -1);
explicit Shell( const QString &serializedOptions = QString() );
/**
* Default Destructor
@ -63,7 +64,7 @@ public slots:
void slotQuit();
Q_SCRIPTABLE Q_NOREPLY void tryRaise();
Q_SCRIPTABLE bool openDocument( const QString& doc );
Q_SCRIPTABLE bool openDocument( const QString& url, const QString &serializedOptions = QString() );
Q_SCRIPTABLE bool canOpenDocs( int numDocs, int desktop );
protected:
@ -92,8 +93,7 @@ private slots:
void slotUpdateFullScreen();
void slotShowMenubar();
void openUrl( const KUrl & url );
void delayedOpen();
void openUrl( const KUrl & url, const QString &serializedOptions = QString() );
void showOpenRecentMenu();
void closeUrl();
void print();
@ -118,14 +118,13 @@ signals:
private:
void setupAccel();
void setupActions();
void init();
QStringList fileFormats() const;
void openNewTab( const KUrl& url );
void openNewTab( const KUrl& url, const QString &serializedOptions );
void applyOptionsToPart( QObject* part, const QString &serializedOptions );
void connectPart( QObject* part );
int findTabIndex( QObject* sender );
private:
KCmdLineArgs* m_args;
KPluginFactory* m_partFactory;
KRecentFilesAction* m_recent;
QStringList m_fileformats;
@ -136,7 +135,6 @@ private:
KToggleAction* m_showMenuBarAction;
bool m_menuBarWasShown, m_toolBarWasShown;
bool m_unique;
KUrl m_openUrl;
KTabWidget* m_tabWidget;
KToggleAction* m_openInTab;

View file

@ -64,4 +64,71 @@ KUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& p
return url;
}
QString serializeOptions(const KCmdLineArgs &args)
{
const bool startInPresentation = args.isSet( "presentation" );
const bool showPrintDialog = args.isSet( "print" );
const bool unique = args.isSet("unique") && args.count() <= 1;
const bool noRaise = !args.isSet("raise");
const QString page = args.getOption("page");
return serializeOptions(startInPresentation, showPrintDialog, unique, noRaise, page);
}
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool unique, bool noRaise, const QString &page)
{
return QString("%1:%2:%3:%4:%5").arg(startInPresentation).arg(showPrintDialog).arg(unique).arg(noRaise).arg(page);
}
bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *unique, bool *noraise, QString *page)
{
const QStringList args = serializedOptions.split(":");
if (args.count() == 5)
{
*presentation = args[0] == "1";
*print = args[1] == "1";
*unique = args[2] == "1";
*noraise = args[3] == "1";
*page = args[4];
return true;
}
return false;
}
bool startInPresentation(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &result, &dummy, &dummy, &dummy, &dummyString) && result;
}
bool showPrintDialog(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &dummy, &result, &dummy, &dummy, &dummyString) && result;
}
bool unique(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &dummy, &dummy, &result, &dummy, &dummyString) && result;
}
bool noRaise(const QString &serializedOptions)
{
bool result, dummy;
QString dummyString;
return unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &result, &dummyString) && result;
}
QString page(const QString &serializedOptions)
{
QString result;
bool dummy;
unserializeOptions(serializedOptions, &dummy, &dummy, &dummy, &dummy, &result);
return result;
}
}

View file

@ -14,6 +14,8 @@
#include <kurl.h>
class KCmdLineArgs;
namespace ShellUtils
{
@ -21,6 +23,14 @@ typedef bool (*FileExistFunc)( const QString& fileName );
FileExistFunc qfileExistFunc();
KUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& pageArg = QString() );
QString serializeOptions(const KCmdLineArgs &args);
QString serializeOptions(bool startInPresentation, bool showPrintDialog, bool unique, bool noRaise, const QString &page);
bool unserializeOptions(const QString &serializedOptions, bool *presentation, bool *print, bool *unique, bool *noraise, QString *page);
bool unique(const QString &serializedOptions);
bool noRaise(const QString &serializedOptions);
bool startInPresentation(const QString &serializedOptions);
bool showPrintDialog(const QString &serializedOptions);
QString page(const QString &serializedOptions);
}

View file

@ -1,5 +1,7 @@
set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )
include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
kde4_add_unit_test( shelltest shelltest.cpp ../shell/shellutils.cpp )
target_link_libraries( shelltest ${KDE4_KDECORE_LIBS} ${QT_QTTEST_LIBRARY} )
@ -32,3 +34,6 @@ target_link_libraries( modifyannotationpropertiestest ${KDE4_KDECORE_LIBS} ${QT_
kde4_add_unit_test( editformstest editformstest.cpp )
target_link_libraries( editformstest ${KDE4_KDECORE_LIBS} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${QT_QTXML_LIBRARY} okularcore )
kde4_add_unit_test( mainshelltest mainshelltest.cpp ../shell/okular_main.cpp ../shell/shellutils.cpp ../shell/shell.cpp )
target_link_libraries( mainshelltest ${KDE4_KPARTS_LIBS} ${QT_QTTEST_LIBRARY} okularpart okularcore )

391
tests/mainshelltest.cpp Normal file
View file

@ -0,0 +1,391 @@
/***************************************************************************
* Copyright (C) 2014 by Albert Astals Cid <aacid@kde.org> *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <qtest_kde.h>
#include <qprintdialog.h>
#include <qwidget.h>
#include <ktabwidget.h>
#include "../shell/okular_main.h"
#include "../shell/shell.h"
#include "../shell/shellutils.h"
#include "../core/document_p.h"
#include "../ui/presentationwidget.h"
#include "../part.h"
#include "../settings.h"
#include <sys/types.h>
#include <unistd.h>
namespace Okular {
class PartTest
{
public:
Okular::Document *partDocument(Okular::Part *part) const {
return part->m_document;
}
QWidget *presentationWidget(Okular::Part *part) const {
return part->m_presentationWidget;
}
};
}
class ClosePrintDialogHelper : public QObject
{
Q_OBJECT
public:
ClosePrintDialogHelper(int expectedTab) : foundDialog(false), m_expectedTab(expectedTab) { }
bool foundDialog;
private slots:
void closePrintDialog();
private:
int m_expectedTab;
};
class MainShellTest : public QObject, public Okular::PartTest
{
Q_OBJECT
public:
static KTabWidget* tabWidget(Shell *s)
{
return s->m_tabWidget;
}
private slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
void testShell_data();
void testShell();
void testUnique2FilesError();
private:
};
Shell *findShell(Shell *ignore = 0)
{
foreach (QWidget *widget, QApplication::topLevelWidgets())
{
Shell *s = qobject_cast<Shell*>(widget);
if (s && s != ignore)
return s;
}
return 0;
}
void MainShellTest::initTestCase()
{
// Don't pollute people's okular settings
Okular::Settings::instance( "mainshelltest" );
// Register in bus as okular
QDBusConnectionInterface *bus = QDBusConnection::sessionBus().interface();
const QString myPid = QString::number( getpid() );
const QString serviceName = "org.kde.okular-"+ myPid;
QVERIFY( bus->registerService(serviceName) == QDBusConnectionInterface::ServiceRegistered );
// Tell the presentationWidget to not be annoying
KSharedConfigPtr c = KGlobal::config();
KConfigGroup cg = c->group("Notification Messages");
cg.writeEntry("presentationInfo", false);
}
void MainShellTest::cleanupTestCase()
{
}
void MainShellTest::init()
{
// Default settings for every test
Okular::Settings::self()->setDefaults();
// Clean docdatas
QList<KUrl> urls;
urls << KUrl("file://" KDESRCDIR "data/file1.pdf");
urls << KUrl("file://" KDESRCDIR "data/tocreload.pdf");
urls << KUrl("file://" KDESRCDIR "data/contents.epub");
foreach (const KUrl &url, urls)
{
QFileInfo fileReadTest( url.toLocalFile() );
const QString docDataPath = Okular::DocumentPrivate::docDataFileName(url, fileReadTest.size());
QFile::remove(docDataPath);
}
}
void MainShellTest::cleanup()
{
Shell *s;
while ((s = findShell()))
{
delete s;
}
}
void MainShellTest::testShell_data()
{
QTest::addColumn<QStringList>("paths");
QTest::addColumn<QString>("serializedOptions");
QTest::addColumn<bool>("useTabs");
QTest::addColumn<QString>("externalProcessPath");
QTest::addColumn<uint>("expectedPage");
QTest::addColumn<bool>("expectPresentation");
QTest::addColumn<bool>("expectPrintDialog");
QTest::addColumn<bool>("unique");
QTest::addColumn<uint>("externalProcessExpectedPage");
QTest::addColumn<bool>("externalProcessExpectPresentation");
QTest::addColumn<bool>("externalProcessExpectPrintDialog");
const QStringList contentsEpub = QStringList(KDESRCDIR "data/contents.epub");
const QStringList file1 = QStringList(KDESRCDIR "data/file1.pdf");
QStringList file1AndToc;
file1AndToc << KDESRCDIR "data/file1.pdf";
file1AndToc << KDESRCDIR "data/tocreload.pdf";
const QString tocReload = KDESRCDIR "data/tocreload.pdf";
const QString optionsPage2 = ShellUtils::serializeOptions(false, false, false, false, "2");
const QString optionsPage2Presentation = ShellUtils::serializeOptions(true, false, false, false, "2");
const QString optionsPrint = ShellUtils::serializeOptions(false, true, false, false, QString());
const QString optionsUnique = ShellUtils::serializeOptions(false, false, true, false, QString());
QTest::newRow("just show shell") << QStringList() << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("open file") << file1 << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files no tabs") << file1AndToc << QString() << false << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files with tabs") << file1AndToc << QString() << true << QString() << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files sequence no tabs") << file1 << QString() << false << tocReload << 0u << false << false << false << 0u << false << false;
QTest::newRow("two files sequence with tabs") << file1 << QString() << true << tocReload << 0u << false << false << false << 0u << false << false;
QTest::newRow("open file page number") << contentsEpub << optionsPage2 << false << QString() << 1u << false << false << false << 0u << false << false;
QTest::newRow("open file page number and presentation") << contentsEpub << optionsPage2Presentation << false << QString() << 1u << true << false << false << 0u << false << false;
QTest::newRow("open file print") << file1 << optionsPrint << false << QString() << 0u << false << true << false << 0u << false << false;
QTest::newRow("open two files unique") << file1 << optionsUnique << false << tocReload << 0u << false << false << true << 0u << false << false;
QTest::newRow("open two files unique tabs") << file1 << optionsUnique << true << tocReload << 0u << false << false << true << 0u << false << false;
QTest::newRow("page number attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << false << false;
QTest::newRow("presentation attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << false << false << 2u << true << false;
QTest::newRow("print attach tabs") << file1 << QString() << true << contentsEpub[0] << 0u << false << true << false << 2u << false << true;
QTest::newRow("page number attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 3u << false << false;
QTest::newRow("presentation attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << true << false;
QTest::newRow("print attach unique") << file1 << optionsUnique << false << contentsEpub[0] << 0u << false << false << true << 2u << false << true;
QTest::newRow("page number attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 3u << false << false;
QTest::newRow("presentation attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << true << false;
QTest::newRow("print attach unique tabs") << file1 << optionsUnique << true << contentsEpub[0] << 0u << false << false << true << 2u << false << true;
}
void MainShellTest::testShell()
{
QFETCH(QStringList, paths);
QFETCH(QString, serializedOptions);
QFETCH(bool, useTabs);
QFETCH(QString, externalProcessPath);
QFETCH(uint, expectedPage);
QFETCH(bool, expectPresentation);
QFETCH(bool, expectPrintDialog);
QFETCH(bool, unique);
QFETCH(uint, externalProcessExpectedPage);
QFETCH(bool, externalProcessExpectPresentation);
QFETCH(bool, externalProcessExpectPrintDialog);
QScopedPointer<ClosePrintDialogHelper> helper;
Okular::Settings::self()->setShellOpenFileInTabs(useTabs);
if (expectPrintDialog || externalProcessExpectPrintDialog) {
const int expectedTab = externalProcessExpectPrintDialog && !unique ? 1 : 0;
helper.reset(new ClosePrintDialogHelper(expectedTab));
QTimer::singleShot(0, helper.data(), SLOT(closePrintDialog()));
}
Okular::Status status = Okular::main(paths, serializedOptions);
QCOMPARE(status, Okular::Success);
Shell *s = findShell();
QVERIFY(s);
if (paths.count() == 1)
{
QCOMPARE(s->m_tabs.count(), 1);
Okular::Part *part = s->findChild<Okular::Part*>();
QVERIFY(part);
QCOMPARE(part->url().url(), QString("file://%1").arg(paths[0]));
QCOMPARE(partDocument(part)->currentPage(), expectedPage);
}
else if (paths.count() == 2)
{
if (useTabs)
{
QSet<QString> openUrls;
Shell *s = findShell();
QVERIFY(s);
Okular::Part *part = dynamic_cast<Okular::Part*>(s->m_tabs[0].part);
Okular::Part *part2 = dynamic_cast<Okular::Part*>(s->m_tabs[1].part);
QCOMPARE(s->m_tabs.count(), 2);
QCOMPARE(part->url().url(), QString("file://%1").arg(paths[0]));
QCOMPARE(part2->url().url(), QString("file://%1").arg(paths[1]));
QCOMPARE(partDocument(part)->currentPage(), expectedPage);
QCOMPARE(partDocument(part2)->currentPage(), expectedPage);
}
else
{
QSet<QString> openUrls;
Shell *s = findShell();
QVERIFY(s);
QCOMPARE(s->m_tabs.count(), 1);
Okular::Part *part = s->findChild<Okular::Part*>();
QVERIFY(part);
QCOMPARE(partDocument(part)->currentPage(), expectedPage);
openUrls << part->url().url();
Shell *s2 = findShell(s);
QVERIFY(s2);
QCOMPARE(s2->m_tabs.count(), 1);
Okular::Part *part2 = s2->findChild<Okular::Part*>();
QVERIFY(part2);
QCOMPARE(partDocument(part2)->currentPage(), expectedPage);
openUrls << part2->url().url();
foreach(const QString &path, paths)
{
QVERIFY(openUrls.contains(QString("file://%1").arg(path)));
}
}
}
if (!externalProcessPath.isEmpty())
{
Okular::Part *part = s->findChild<Okular::Part*>();
QProcess p;
QString command = "okular " + externalProcessPath;
if (unique)
command += " -unique";
if (externalProcessExpectedPage != 0)
command += QString(" -page %1").arg(externalProcessExpectedPage + 1);
if (externalProcessExpectPresentation)
command += QString(" -presentation");
if (externalProcessExpectPrintDialog)
command += QString(" -print");
p.start(command);
p.waitForStarted();
QCOMPARE(p.state(), QProcess::Running);
if (useTabs || unique)
{
// It is attaching to us, so will eventually stop
for (int i = 0; p.state() != QProcess::NotRunning && i < 20; ++i) {
QTest::qWait(100);
}
QCOMPARE(p.state(), QProcess::NotRunning);
QCOMPARE(p.exitStatus(), QProcess::NormalExit);
QCOMPARE(p.exitCode(), 0);
if (unique)
{
// It is unique so part got "overriten"
QCOMPARE(s->m_tabs.count(), 1);
QCOMPARE(part->url().url(), QString("file://%1").arg(externalProcessPath));
QCOMPARE(partDocument(part)->currentPage(), externalProcessExpectedPage);
}
else
{
// It is attaching to us so a second tab is there
QCOMPARE(s->m_tabs.count(), 2);
Okular::Part *part2 = dynamic_cast<Okular::Part*>(s->m_tabs[1].part);
QCOMPARE(part2->url().url(), QString("file://%1").arg(externalProcessPath));
QCOMPARE(partDocument(part2)->currentPage(), externalProcessExpectedPage);
}
}
else
{
QTest::qWait(750);
// It opened on a new process, so it is still running, we need to kill it
QCOMPARE(p.state(), QProcess::Running);
p.terminate();
p.waitForFinished();
QCOMPARE(p.exitCode(), 0);
// It opened on a new process, so no change for us
QCOMPARE(s->m_tabs.count(), 1);
QCOMPARE(part->url().url(), QString("file://%1").arg(paths[0]));
QCOMPARE(partDocument(part)->currentPage(), externalProcessExpectedPage);
}
}
if (expectPresentation)
{
QCOMPARE(paths.count(), 1);
Okular::Part *part = s->findChild<Okular::Part*>();
// Oh Qt5 i want your QTRY_VERIFY
for (int i = 0; presentationWidget(part) == 0 && i < 20; ++i) {
QTest::qWait(100);
}
QVERIFY(presentationWidget(part) != 0);
}
if (externalProcessExpectPresentation)
{
Okular::Part *part;
if (unique)
{
QCOMPARE(s->m_tabs.count(), 1);
part = dynamic_cast<Okular::Part*>(s->m_tabs[0].part);
}
else
{
QCOMPARE(s->m_tabs.count(), 2);
part = dynamic_cast<Okular::Part*>(s->m_tabs[1].part);
}
for (int i = 0; presentationWidget(part) == 0 && i < 20; ++i) {
QTest::qWait(100);
}
QVERIFY(presentationWidget(part) != 0);
}
if (helper)
{
QVERIFY(helper->foundDialog);
}
}
void ClosePrintDialogHelper::closePrintDialog()
{
Shell *s = findShell();
QPrintDialog *dialog = s->findChild<QPrintDialog*>();
if (!dialog) {
QTimer::singleShot(0, this, SLOT(closePrintDialog()));
return;
}
QVERIFY(dialog);
QCOMPARE(MainShellTest::tabWidget(s)->currentIndex(), m_expectedTab);
dialog->close();
foundDialog = true;
}
void MainShellTest::testUnique2FilesError()
{
QStringList paths;
QString serializedOptions = ShellUtils::serializeOptions(false, false, true, false, QString());
paths << KDESRCDIR "data/file1.pdf" << KDESRCDIR "data/tocreload.pdf";
Okular::Status status = Okular::main(paths, serializedOptions);
QCOMPARE(status, Okular::Error);
QSet<QString> openUrls;
Shell *s = findShell();
QVERIFY(!s);
}
QTEST_KDEMAIN( MainShellTest, GUI )
#include "mainshelltest.moc"