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=1032962remotes/origin/KDE/4.4
parent
d5dd8fce01
commit
a355d9fea4
6 changed files with 112 additions and 48 deletions
@ -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; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -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 |
||||||
@ -1,4 +1,4 @@ |
|||||||
set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} ) |
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} ) |
target_link_libraries( shelltest ${KDE4_KDECORE_LIBS} ${QT_QTTEST_LIBRARY} ) |
||||||
|
|||||||
Loading…
Reference in new issue