Remove Url::Class, add Qt4x5::Url::ensureLeadingSlash() and fix all related

This commit is contained in:
Timon Wong 2013-05-31 12:20:25 +08:00
parent f0c1aa26e7
commit f30c41dbf6
19 changed files with 88 additions and 124 deletions

View file

@ -29,7 +29,6 @@
#include "ufile.hh"
#include "wstring_qt.hh"
#include "qt4x5.hh"
#include "url.hh"
namespace Aard {

View file

@ -8,11 +8,12 @@
#include "wstring_qt.hh"
#include <limits.h>
#include <QFile>
#include <QUrl>
#include "folding.hh"
#include "langcoder.hh"
#include "dprintf.hh"
#include "url.hh"
#include "qt4x5.hh"
using std::vector;
using std::string;
@ -875,11 +876,11 @@ QPair< ArticleRequest::Words, ArticleRequest::Spacings > ArticleRequest::splitIn
string ArticleRequest::linkWord( QString const & str )
{
Url::Class url;
QUrl url;
url.setScheme( "gdlookup" );
url.setHost( "localhost" );
url.setPath( str );
url.setPath( Qt4x5::Url::ensureLeadingSlash( str ) );
string escapedResult = Html::escape( str.toUtf8().data() );
return string( "<a href=\"" ) + url.toEncoded().data() + "\">" + escapedResult +"</a>";

View file

@ -7,11 +7,12 @@
#include <stdint.h>
#endif
#include <QUrl>
#include "article_netmgr.hh"
#include "wstring_qt.hh"
#include "dprintf.hh"
#include "qt4x5.hh"
#include "url.hh"
using std::string;
@ -57,7 +58,7 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
{
// We have to override the local load policy for the qrc scheme, hence
// we use qrcx and redirect it here back to qrc
Url::Class newUrl( req.url() );
QUrl newUrl( req.url() );
newUrl.setScheme( "qrc" );
newUrl.setHost( "" );
@ -85,7 +86,7 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
//DPRINTF( "Referer: %s\n", referer.data() );
Url::Class refererUrl = QUrl::fromEncoded( referer );
QUrl refererUrl = QUrl::fromEncoded( referer );
//DPRINTF( "Considering %s vs %s\n", getHostBase( req.url() ).toUtf8().data(),
// getHostBase( refererUrl ).toUtf8().data() );
@ -105,8 +106,8 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
QString fileName = req.url().toLocalFile();
if( req.url().host().isEmpty() && articleMaker.adjustFilePath( fileName ) )
{
Url::Class newUrl( req.url() );
newUrl.setPath( QUrl::fromLocalFile( fileName ).path() );
QUrl newUrl( req.url() );
newUrl.setPath( Qt4x5::Url::ensureLeadingSlash( QUrl::fromLocalFile( fileName ).path() ) );
QNetworkRequest newReq( req );
newReq.setUrl( newUrl );
@ -232,7 +233,7 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource(
if ( url.scheme() == "gdpicture" )
{
contentType = "text/html";
Url::Class imgUrl ( url );
QUrl imgUrl ( url );
imgUrl.setScheme( "bres" );
return articleMaker.makePicturePage( imgUrl.toEncoded().data() );
}

View file

@ -13,6 +13,7 @@
#include <QKeyEvent>
#include <QFileDialog>
#include <QDebug>
#include <QUrl>
#include <QWebElement>
#include <QCryptographicHash>
#include "folding.hh"
@ -22,7 +23,6 @@
#include "dprintf.hh"
#include "ffmpegaudio.hh"
#include "qt4x5.hh"
#include "url.hh"
#include <assert.h>
@ -165,7 +165,7 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
// Load the default blank page instantly, so there would be no flicker.
QString contentType;
Url::Class blankPage( "gdlookup://localhost?blank=1" );
QUrl blankPage( "gdlookup://localhost?blank=1" );
sptr< Dictionary::DataRequest > r = articleNetMgr.getResource( blankPage,
contentType );
@ -198,7 +198,7 @@ void ArticleView::showDefinition( QString const & word, unsigned group,
QString const & scrollTo,
Contexts const & contexts )
{
Url::Class req;
QUrl req;
req.setScheme( "gdlookup" );
req.setHost( "localhost" );
@ -255,7 +255,7 @@ void ArticleView::showAnticipation()
void ArticleView::loadFinished( bool )
{
Url::Class url = ui.definition->url();
QUrl url = ui.definition->url();
// See if we have any iframes in need of expansion
@ -482,7 +482,7 @@ void ArticleView::tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts )
contexts[ ca.mid( 7 ) ] = QString::fromLatin1( url.toEncoded() );
Url::Class target;
QUrl target;
QString queryWord = result.toString();
@ -665,7 +665,7 @@ QString ArticleView::getMutedForGroup( unsigned group )
void ArticleView::linkHovered ( const QString & link, const QString & , const QString & )
{
QString msg;
Url::Class url(link);
QUrl url(link);
if ( url.scheme() == "bres" )
{
@ -723,7 +723,7 @@ void ArticleView::linkClicked( QUrl const & url_ )
{
updateCurrentArticleFromCurrentFrame();
Url::Class url( url_ );
QUrl url( url_ );
Contexts contexts;
tryMangleWebsiteClickedUrl( url, contexts );
@ -1044,7 +1044,7 @@ vector< ResourceToSaveHandler * > ArticleView::saveResource( const QUrl & url, c
void ArticleView::updateMutedContents()
{
Url::Class currentUrl = ui.definition->url();
QUrl currentUrl = ui.definition->url();
if ( currentUrl.scheme() != "gdlookup" )
return; // Weird url -- do nothing
@ -1164,7 +1164,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
QAction * saveImageAction = 0;
QAction * saveSoundAction = 0;
Url::Class targetUrl( r.linkUrl() );
QUrl targetUrl( r.linkUrl() );
Contexts contexts;
tryMangleWebsiteClickedUrl( targetUrl, contexts );
@ -1193,7 +1193,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
}
QWebElement el = r.element();
Url::Class imageUrl;
QUrl imageUrl;
if( !popupView && el.tagName().compare( "img", Qt::CaseInsensitive ) == 0 )
{
imageUrl = QUrl::fromPercentEncoding( el.attribute( "src" ).toLatin1() );
@ -1381,7 +1381,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
else
if( result == saveImageAction || result == saveSoundAction )
{
Url::Class url = ( result == saveImageAction ) ? imageUrl : targetUrl;
QUrl url = ( result == saveImageAction ) ? imageUrl : targetUrl;
QString savePath;
QString fileName;

View file

@ -6,12 +6,12 @@
#include <QWebView>
#include <QMap>
#include <QUrl>
#include <list>
#include "article_netmgr.hh"
#include "instances.hh"
#include "groupcombobox.hh"
#include "ui_articleview.h"
#include "url.hh"
class ResourceToSaveHandler;
@ -42,7 +42,7 @@ class ArticleView: public QFrame
/// a multitude of requests.
std::list< sptr< Dictionary::DataRequest > > resourceDownloadRequests;
/// Url of the resourceDownloadRequests
Url::Class resourceDownloadUrl;
QUrl resourceDownloadUrl;
/// For resources opened via desktop services
QString desktopOpenedTempFile;

21
dsl.cc
View file

@ -48,7 +48,6 @@
#include <QtSvg/QSvgRenderer>
#include "qt4x5.hh"
#include "url.hh"
namespace Dsl {
@ -774,10 +773,10 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
( !resourceZip.isOpen() ||
!resourceZip.hasFile( Utf8::decode( filename ) ) );
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( search ? "search" : getId().c_str() ) );
url.setPath( QString::fromUtf8( filename.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
@ -789,10 +788,10 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
else
if ( Filetype::isNameOfPicture( filename ) )
{
Url::Class url;
QUrl url;
url.setScheme( "bres" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( filename.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
vector< char > imgdata;
bool resize = false;
@ -866,10 +865,10 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
{
// Unknown file type, downgrade to a hyperlink
Url::Class url;
QUrl url;
url.setScheme( "bres" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( filename.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
result += string( "<a class=\"dsl_s\" href=\"" ) + url.toEncoded().data()
+ "\">" + processNodeChildren( node ) + "</a>";
@ -939,11 +938,11 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
else
if ( node.tagName == GD_NATIVE_TO_WS( L"ref" ) )
{
Url::Class url;
QUrl url;
url.setScheme( "gdlookup" );
url.setHost( "localhost" );
url.setPath( gd::toQString( node.renderAsText() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( gd::toQString( node.renderAsText() ) ) );
if( !node.tagAttrs.empty() )
{
QString attr = gd::toQString( node.tagAttrs ).remove( '\"' );
@ -963,13 +962,13 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
{
// Special case - insided card header was not parsed
Url::Class url;
QUrl url;
url.setScheme( "gdlookup" );
url.setHost( "localhost" );
wstring nodeStr = node.renderAsText();
ArticleDom nodeDom( nodeStr );
url.setPath( gd::toQString( nodeDom.root.renderAsText() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( gd::toQString( nodeDom.root.renderAsText() ) ) );
result += string( "<a class=\"dsl_ref\" href=\"" ) + url.toEncoded().data() +"\">" + processNodeChildren( nodeDom.root ) + "</a>";
}

View file

@ -15,7 +15,6 @@
#include "utf8.hh"
#include "dprintf.hh"
#include "qt4x5.hh"
#include "url.hh"
namespace Forvo {
@ -174,7 +173,7 @@ void ForvoArticleRequest::addQuery( QNetworkAccessManager & mgr,
else
key = apiKey;
Url::Class reqUrl = QUrl::fromEncoded(
QUrl reqUrl = QUrl::fromEncoded(
QString( "http://apifree.forvo.com/key/" + key + "/format/xml/action/word-pronunciations/word/" +
QString::fromLatin1( QUrl::toPercentEncoding( gd::toQString( str ) ) ) + "/language/" + languageCode
).toUtf8() );
@ -261,7 +260,7 @@ void ForvoArticleRequest::requestFinished( QNetworkReply * r )
{
articleBody += "<tr>";
Url::Class url( mp3.toElement().text() );
QUrl url( mp3.toElement().text() );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";

View file

@ -254,7 +254,6 @@ HEADERS += folding.hh \
voiceengines.hh \
ffmpegaudio.hh \
articleinspector.hh \
url.hh \
qt4x5.hh
FORMS += groups.ui \
@ -363,8 +362,7 @@ SOURCES += folding.cc \
mdx.cc \
voiceengines.cc \
ffmpegaudio.cc \
articleinspector.cc \
url.cc
articleinspector.cc
win32 {
FORMS += texttospeechsource.ui

12
lsa.cc
View file

@ -20,7 +20,9 @@
#include <vorbis/vorbisfile.h>
#include <QDir>
#include "url.hh"
#include <QUrl>
#include "qt4x5.hh"
namespace Lsa {
@ -266,10 +268,10 @@ sptr< Dictionary::DataRequest > LsaDictionary::getArticle( wstring const & word,
{
result += "<tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( i->second.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( i->second.c_str() ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
@ -284,10 +286,10 @@ sptr< Dictionary::DataRequest > LsaDictionary::getArticle( wstring const & word,
{
result += "<tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( i->second.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( i->second.c_str() ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";

View file

@ -11,6 +11,7 @@
#include <QDebug>
#include <QTextStream>
#include <QDir>
#include <QUrl>
#include <QMessageBox>
#include <QIcon>
#include <QList>
@ -33,7 +34,7 @@
#include "dictinfo.hh"
#include "fsencoding.hh"
#include "historypanewidget.hh"
#include "url.hh"
#include "qt4x5.hh"
#ifdef Q_OS_MAC
#include "lionsupport.h"
@ -2306,7 +2307,7 @@ void MainWindow::showTranslationFor( QString const & inWord,
updateBackForwardButtons();
#if 0
Url::Class req;
QUrl req;
req.setScheme( "gdlookup" );
req.setHost( "localhost" );
@ -2900,7 +2901,7 @@ static void filterAndCollectResources( QString & html, QRegExp & rx, const QStri
while ( ( pos = rx.indexIn( html, pos ) ) != -1 )
{
Url::Class url( rx.cap( 1 ) );
QUrl url( rx.cap( 1 ) );
QString host = url.host();
QString resourcePath = QString::fromLatin1( QUrl::toPercentEncoding( url.path(), "/" ) );

View file

@ -10,7 +10,6 @@
#include "dprintf.hh"
#include "audiolink.hh"
#include "qt4x5.hh"
#include "url.hh"
namespace MediaWiki {
@ -111,7 +110,7 @@ MediaWikiWordSearchRequest::MediaWikiWordSearchRequest( wstring const & str,
livedLongEnough( false ), isCancelling( false )
{
DPRINTF( "request begin\n" );
Url::Class reqUrl( url + "/api.php?action=query&list=allpages&aplimit=40&format=xml" );
QUrl reqUrl( url + "/api.php?action=query&list=allpages&aplimit=40&format=xml" );
Qt4x5::Url::addQueryItem( reqUrl, "apfrom", gd::toQString( str ) );
@ -251,7 +250,7 @@ void MediaWikiArticleRequest::addQuery( QNetworkAccessManager & mgr,
#endif
);
Url::Class reqUrl( url + "/api.php?action=parse&prop=text|revid&format=xml&redirects" );
QUrl reqUrl( url + "/api.php?action=parse&prop=text|revid&format=xml&redirects" );
Qt4x5::Url::addQueryItem( reqUrl, "page", gd::toQString( str ) );
@ -317,8 +316,8 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
{
QString articleString = textNode.toElement().text();
Url::Class wikiUrl( url );
wikiUrl.setPath( "" );
QUrl wikiUrl( url );
wikiUrl.setPath( "/" );
// Update any special index.php pages to be absolute
articleString.replace( QRegExp( "<a\\shref=\"(/(\\w*/)*index.php\\?)" ),

View file

@ -7,7 +7,8 @@
#include "utf8.hh"
#include "wstring_qt.hh"
#include "parsecmdline.hh"
#include "url.hh"
#include "qt4x5.hh"
#include <QDir>
#include <QFileInfo>
@ -85,10 +86,10 @@ sptr< Dictionary::DataRequest > ProgramsDictionary::getArticle(
result += "<table class=\"programs_play\"><tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdprg" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( wordUtf8.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( wordUtf8.c_str() ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";

View file

@ -48,6 +48,17 @@ inline int loadAcquire( QAtomicInt const & ref )
namespace Url
{
// This wrapper is created due to behavior change of the setPath() method
// See: https://bugreports.qt-project.org/browse/QTBUG-27728
// https://codereview.qt-project.org/#change,38257
inline QString ensureLeadingSlash( const QString & path )
{
QLatin1Char slash( '/' );
if ( path.startsWith( slash ) )
return path;
return slash + path;
}
inline bool hasQueryItem( QUrl const & url, QString const & key )
{
#if IS_QT_5

View file

@ -12,7 +12,8 @@
#include "audiolink.hh"
#include "wstring_qt.hh"
#include "fsencoding.hh"
#include "url.hh"
#include "qt4x5.hh"
#include <set>
#include <QDir>
#include <QFileInfo>
@ -181,10 +182,10 @@ sptr< Dictionary::DataRequest > SoundDirDictionary::getArticle( wstring const &
{
result += "<tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::number( chain[ i->second ].articleOffset ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::number( chain[ i->second ].articleOffset ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
@ -199,10 +200,10 @@ sptr< Dictionary::DataRequest > SoundDirDictionary::getArticle( wstring const &
{
result += "<tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::number( chain[ i->second ].articleOffset ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::number( chain[ i->second ].articleOffset ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";

17
url.cc
View file

@ -1,17 +0,0 @@
#include "url.hh"
namespace Url
{
#if IS_QT_5
void Class::setPath( const QString & path )
{
QLatin1Char slash( '/' );
if ( path.startsWith( slash ) )
QUrl::setPath( path );
else
QUrl::setPath( slash + path );
}
#endif
}

31
url.hh
View file

@ -1,31 +0,0 @@
#ifndef URL_HH
#define URL_HH
#include <QUrl>
#include "qt4x5.hh"
namespace Url
{
#if IS_QT_5
/// This class is created due to behavior change of the setPath() method
/// See: https://bugreports.qt-project.org/browse/QTBUG-27728
// https://codereview.qt-project.org/#change,38257
class Class : public QUrl
{
public:
Class() : QUrl() {}
Class( QString const & url ) : QUrl( url ) {}
Class( QUrl const & other ) : QUrl( other ) {}
Class( QString const & url, ParsingMode parsingMode ) : QUrl( url, parsingMode ) {}
void setPath( QString const & path );
};
#else
typedef QUrl Class;
#endif
}
#endif // URL_HH

View file

@ -15,7 +15,6 @@
#include <QCryptographicHash>
#include "qt4x5.hh"
#include "url.hh"
namespace VoiceEngines
{
@ -89,10 +88,10 @@ sptr< Dictionary::DataRequest > VoiceEnginesDictionary::getArticle(
result += "<table class=\"voiceengines_play\"><tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdtts" );
url.setHost( "localhost" );
url.setPath( QString::fromUtf8( wordUtf8.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( wordUtf8.c_str() ) ) );
QList< QPair<QString, QString> > query;
query.push_back( QPair<QString, QString>( "engine", QString::fromStdString( getId() ) ) );
Qt4x5::Url::setQueryItems( url, query );

View file

@ -3,6 +3,7 @@
#include "xdxf2html.hh"
#include <QtXml>
#include <Qurl>
#include "dprintf.hh"
#include "utf8.hh"
#include "wstring_qt.hh"
@ -12,16 +13,16 @@
#include "file.hh"
#include "filetype.hh"
#include "htmlescape.hh"
#include "url.hh"
#include "qt4x5.hh"
namespace Xdxf2Html {
static void fixLink( QDomElement & el, string const & dictId, const char *attrName )
{
Url::Class url;
QUrl url;
url.setScheme( "bres" );
url.setHost( QString::fromStdString(dictId) );
url.setPath( el.attribute(attrName) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( el.attribute(attrName) ) );
el.setAttribute( attrName, url.toEncoded().data() );
}
@ -275,10 +276,10 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const
if ( Filetype::isNameOfPicture( filename ) )
{
Url::Class url;
QUrl url;
url.setScheme( "bres" );
url.setHost( QString::fromUtf8( dictPtr->getId().c_str() ) );
url.setPath( QString::fromUtf8( filename.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
QDomElement newEl = dd.createElement( "img" );
newEl.setAttribute( "src", url.toEncoded().data() );
@ -293,10 +294,10 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const
}
else if( Filetype::isNameOfSound( filename ) )
{
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( dictPtr->getId().c_str() ) );
url.setPath( QString::fromUtf8( filename.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
QDomElement el_script = dd.createElement( "script" );
QDomNode parent = el.parentNode();

View file

@ -21,7 +21,7 @@
#include <stub_msvc.h>
#endif
#include "url.hh"
#include "qt4x5.hh"
namespace ZipSounds {
@ -217,10 +217,10 @@ sptr< Dictionary::DataRequest > ZipSoundsDictionary::getArticle( wstring const &
{
result += "<tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( i->second.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( i->second.c_str() ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
@ -235,10 +235,10 @@ sptr< Dictionary::DataRequest > ZipSoundsDictionary::getArticle( wstring const &
{
result += "<tr>";
Url::Class url;
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( QString::fromUtf8( i->second.c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( i->second.c_str() ) ) );
string ref = string( "\"" ) + url.toEncoded().data() + "\"";