diff --git a/shell/CMakeLists.txt b/shell/CMakeLists.txt index 59521e8c6..62b70161e 100644 --- a/shell/CMakeLists.txt +++ b/shell/CMakeLists.txt @@ -8,6 +8,7 @@ include_directories( set(okular_SRCS main.cpp shell.cpp + shellutils.cpp ) kde4_add_app_icon(okular_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/../ui/data/icons/hi*-apps-okular.png") diff --git a/shell/shell.cpp b/shell/shell.cpp index 32091e2c8..709108c75 100644 --- a/shell/shell.cpp +++ b/shell/shell.cpp @@ -45,41 +45,15 @@ // local includes #include "kdocumentviewer.h" +#include "shellutils.h" Shell::Shell(KCmdLineArgs* args, int argIndex) : KParts::MainWindow(), m_args(args), m_menuBarWasShown(true), m_toolBarWasShown(true) { if (m_args && argIndex != -1) { - /* - Rationale for the small "cut-and-paste" work being done below: - KCmdLineArgs::makeURL() (used by ::url() encodes any # into the URL itself, - so we have to find it manually and build up the URL by taking its ref, - if any. - */ - QString arg = m_args->arg(argIndex); - const QString origArg = arg; - arg.replace(QRegExp("^file:/{1,3}"), "/"); - if (arg != origArg) - { - arg = QString::fromUtf8(QByteArray::fromPercentEncoding(arg.toUtf8())); - } - KUrl url = KCmdLineArgs::makeURL(arg.toUtf8()); - int sharpPos = -1; - if (!url.isLocalFile() || !QFile::exists(url.toLocalFile())) - { - sharpPos = arg.lastIndexOf(QLatin1Char('#')); - } - if (sharpPos != -1) - { - url = KCmdLineArgs::makeURL(arg.left(sharpPos).toUtf8()); - url.setHTMLRef(arg.mid(sharpPos + 1)); - } - else if (!m_args->getOption("page").isEmpty()) - { - url.setHTMLRef(m_args->getOption("page")); - } - m_openUrl = url; + m_openUrl = ShellUtils::urlFromArg(m_args->arg(argIndex), + ShellUtils::qfileExistFunc(), m_args->getOption("page")); } init(); } diff --git a/shell/shellutils.cpp b/shell/shellutils.cpp new file mode 100644 index 000000000..26c682512 --- /dev/null +++ b/shell/shellutils.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2009 by Pino Toscano * + * * + * 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 "shellutils.h" + +// qt/kde includes +#include +#include +#include + +namespace ShellUtils +{ + +namespace detail +{ + +bool qfileExistFunc( const QString& fileName ) +{ + return QFile::exists( fileName ); +} + +} + +FileExistFunc qfileExistFunc() +{ + return detail::qfileExistFunc; +} + +KUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& pageArg ) +{ + /* + Rationale for the small "cut-and-paste" work being done below: + KCmdLineArgs::makeURL() (used by ::url() encodes any # into the URL itself, + so we have to find it manually and build up the URL by taking its ref, + if any. + */ + QString arg = _arg; + arg.replace( QRegExp( "^file:/{1,3}"), "/" ); + if ( arg != _arg ) + { + arg = QString::fromUtf8( QByteArray::fromPercentEncoding( arg.toUtf8() ) ); + } + KUrl url = KCmdLineArgs::makeURL( arg.toUtf8() ); + int sharpPos = -1; + if ( !url.isLocalFile() || !exist_func( url.toLocalFile() ) ) + { + sharpPos = arg.lastIndexOf( QLatin1Char( '#' ) ); + } + if ( sharpPos != -1 ) + { + url = KCmdLineArgs::makeURL( arg.left( sharpPos ).toUtf8() ); + url.setHTMLRef( arg.mid( sharpPos + 1 ) ); + } + else if ( !pageArg.isEmpty() ) + { + url.setHTMLRef( pageArg ); + } + return url; +} + +} diff --git a/shell/shellutils.h b/shell/shellutils.h new file mode 100644 index 000000000..6c0c228a9 --- /dev/null +++ b/shell/shellutils.h @@ -0,0 +1,27 @@ +/*************************************************************************** + * Copyright (C) 2009 by Pino Toscano * + * * + * 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. * + ***************************************************************************/ + +#ifndef OKULAR_SHELLUTILS_H +#define OKULAR_SHELLUTILS_H + +#include + +#include + +namespace ShellUtils +{ + +typedef bool (*FileExistFunc)( const QString& fileName ); + +FileExistFunc qfileExistFunc(); +KUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& pageArg = QString() ); + +} + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8b6659a62..9f8c288a7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,4 @@ set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} ) -kde4_add_unit_test( shelltest shelltest.cpp ) +kde4_add_unit_test( shelltest shelltest.cpp ../shell/shellutils.cpp ) target_link_libraries( shelltest ${KDE4_KDECORE_LIBS} ${QT_QTTEST_LIBRARY} ) diff --git a/tests/shelltest.cpp b/tests/shelltest.cpp index e16626677..985b78c66 100644 --- a/tests/shelltest.cpp +++ b/tests/shelltest.cpp @@ -11,6 +11,8 @@ #include #include +#include "../shell/shellutils.h" + namespace QTest { template<> @@ -29,6 +31,16 @@ static const KUrl makeUrlFromCwd( const QString& u, const QString& ref = QString return url; } +static bool fileExist_always_Func( const QString& ) +{ + return true; +} + +static bool fileExist_never_Func( const QString& ) +{ + return false; +} + class ShellTest : public QObject { @@ -100,24 +112,7 @@ void ShellTest::testUrlArgs() QFETCH( bool, exists ); QFETCH( KUrl, resUrl ); - // note: below is a snippet taken from the Shell ctor - const QString origArg = arg; - arg.replace(QRegExp("^file:/{1,3}"), "/"); - if (arg != origArg) - { - arg = QString::fromUtf8(QByteArray::fromPercentEncoding(arg.toUtf8())); - } - KUrl url = KCmdLineArgs::makeURL(arg.toUtf8()); - int sharpPos = -1; - if (!url.isLocalFile() || !exists) - { - sharpPos = arg.lastIndexOf(QLatin1Char('#')); - } - if (sharpPos != -1) - { - url = KCmdLineArgs::makeURL(arg.left(sharpPos).toUtf8()); - url.setHTMLRef(arg.mid(sharpPos + 1)); - } + KUrl url = ShellUtils::urlFromArg( arg, exists ? fileExist_always_Func : fileExist_never_Func ); QCOMPARE( url, resUrl ); }