isolate the argument -> url conversion code in an own function in the ShellUtils namespace

make the function able to be reused in unit tests (ie adding a function hook for the filename existance check)

svn path=/trunk/KDE/kdegraphics/okular/; revision=1032962
This commit is contained in:
Pino Toscano 2009-10-08 23:48:04 +00:00
parent d5dd8fce01
commit a355d9fea4
6 changed files with 112 additions and 48 deletions

View file

@ -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")

View file

@ -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();
}

67
shell/shellutils.cpp Normal file
View file

@ -0,0 +1,67 @@
/***************************************************************************
* Copyright (C) 2009 by Pino Toscano <pino@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 "shellutils.h"
// qt/kde includes
#include <qfile.h>
#include <qregexp.h>
#include <kcmdlineargs.h>
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;
}
}

27
shell/shellutils.h Normal file
View file

@ -0,0 +1,27 @@
/***************************************************************************
* Copyright (C) 2009 by Pino Toscano <pino@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. *
***************************************************************************/
#ifndef OKULAR_SHELLUTILS_H
#define OKULAR_SHELLUTILS_H
#include <qstring.h>
#include <kurl.h>
namespace ShellUtils
{
typedef bool (*FileExistFunc)( const QString& fileName );
FileExistFunc qfileExistFunc();
KUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& pageArg = QString() );
}
#endif

View file

@ -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} )

View file

@ -11,6 +11,8 @@
#include <qdir.h>
#include <kurl.h>
#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 );
}