2009-10-08 23:48:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* 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
|
2014-08-13 20:57:48 +00:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <KCmdLineArgs>
|
2009-10-08 23:48:04 +00:00
|
|
|
|
|
|
|
namespace ShellUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
bool qfileExistFunc( const QString& fileName )
|
|
|
|
{
|
|
|
|
return QFile::exists( fileName );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FileExistFunc qfileExistFunc()
|
|
|
|
{
|
|
|
|
return detail::qfileExistFunc;
|
|
|
|
}
|
|
|
|
|
2014-08-13 20:57:48 +00:00
|
|
|
QUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& pageArg )
|
2009-10-08 23:48:04 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
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() ) );
|
|
|
|
}
|
2014-08-13 20:57:48 +00:00
|
|
|
QUrl url = KCmdLineArgs::makeURL( arg.toUtf8() );
|
2009-10-08 23:48:04 +00:00
|
|
|
int sharpPos = -1;
|
|
|
|
if ( !url.isLocalFile() || !exist_func( url.toLocalFile() ) )
|
|
|
|
{
|
|
|
|
sharpPos = arg.lastIndexOf( QLatin1Char( '#' ) );
|
|
|
|
}
|
|
|
|
if ( sharpPos != -1 )
|
|
|
|
{
|
|
|
|
url = KCmdLineArgs::makeURL( arg.left( sharpPos ).toUtf8() );
|
2014-08-13 20:57:48 +00:00
|
|
|
url.setFragment( arg.mid( sharpPos + 1 ) );
|
2009-10-08 23:48:04 +00:00
|
|
|
}
|
|
|
|
else if ( !pageArg.isEmpty() )
|
|
|
|
{
|
2014-08-13 20:57:48 +00:00
|
|
|
url.setFragment( pageArg );
|
2009-10-08 23:48:04 +00:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|