2011-10-29 12:24:12 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-10-29 12:24:12 +02:00
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-10-28 23:17:38 +02:00
|
|
|
#include "iconfetcher.h"
|
2011-10-29 13:36:04 +02:00
|
|
|
#include "followredirectreply.h"
|
2013-02-24 10:57:58 +01:00
|
|
|
#include "qzregexp.h"
|
|
|
|
|
|
|
|
#include <QNetworkReply>
|
2011-10-28 23:17:38 +02:00
|
|
|
|
|
|
|
IconFetcher::IconFetcher(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_manager(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconFetcher::fetchIcon(const QUrl &url)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_manager) {
|
2011-10-28 23:17:38 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 23:17:38 +02:00
|
|
|
|
|
|
|
FollowRedirectReply* reply = new FollowRedirectReply(url, m_manager);
|
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded()));
|
2012-03-06 15:28:52 +01:00
|
|
|
|
|
|
|
m_url = url;
|
2011-10-28 23:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconFetcher::pageDownloaded()
|
|
|
|
{
|
|
|
|
FollowRedirectReply* reply = qobject_cast<FollowRedirectReply*> (sender());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!reply) {
|
2011-10-28 23:17:38 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 23:17:38 +02:00
|
|
|
|
2012-07-03 21:54:04 +02:00
|
|
|
QString html = reply->readAll();
|
|
|
|
QUrl replyUrl = reply->url();
|
2011-12-04 20:09:44 +01:00
|
|
|
reply->deleteLater();
|
2011-10-28 23:17:38 +02:00
|
|
|
|
2013-02-24 10:57:58 +01:00
|
|
|
QzRegExp rx("<link(.*)>", Qt::CaseInsensitive);
|
2011-10-28 23:17:38 +02:00
|
|
|
rx.setMinimal(true);
|
|
|
|
|
|
|
|
QString shortcutIconTag;
|
|
|
|
int pos = 0;
|
|
|
|
while ((pos = rx.indexIn(html, pos)) != -1) {
|
|
|
|
QString linkTag = rx.cap(0);
|
|
|
|
pos += rx.matchedLength();
|
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (linkTag.contains(QLatin1String("rel=\"shortcut icon\""), Qt::CaseInsensitive)) {
|
2011-10-28 23:17:38 +02:00
|
|
|
shortcutIconTag = linkTag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FollowRedirectReply* newReply;
|
|
|
|
if (shortcutIconTag.isEmpty()) {
|
|
|
|
// QUrl faviconUrl = replyUrl.resolved(QUrl("favicon.ico"));
|
2011-11-05 18:36:53 +01:00
|
|
|
//
|
|
|
|
// Rather getting favicon.ico from base directory than from subfolders
|
2011-10-28 23:17:38 +02:00
|
|
|
QUrl faviconUrl = QUrl(replyUrl.toString(QUrl::RemovePath | QUrl::RemoveQuery) + "/favicon.ico");
|
|
|
|
newReply = new FollowRedirectReply(faviconUrl, m_manager);
|
|
|
|
}
|
|
|
|
else {
|
2013-02-24 10:57:58 +01:00
|
|
|
QzRegExp rx("href=\"(.*)\"", Qt::CaseInsensitive);
|
2011-10-28 23:17:38 +02:00
|
|
|
rx.setMinimal(true);
|
|
|
|
rx.indexIn(shortcutIconTag);
|
|
|
|
QUrl url = QUrl(rx.cap(1));
|
|
|
|
|
|
|
|
QUrl iconUrl = QUrl(replyUrl).resolved(url);
|
|
|
|
newReply = new FollowRedirectReply(iconUrl, m_manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(newReply, SIGNAL(finished()), this, SLOT(iconDownloaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconFetcher::iconDownloaded()
|
|
|
|
{
|
|
|
|
FollowRedirectReply* reply = qobject_cast<FollowRedirectReply*> (sender());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!reply) {
|
2011-10-28 23:17:38 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 23:17:38 +02:00
|
|
|
|
2012-07-03 21:54:04 +02:00
|
|
|
QByteArray response = reply->readAll();
|
2011-12-04 20:09:44 +01:00
|
|
|
reply->deleteLater();
|
2011-10-28 23:17:38 +02:00
|
|
|
|
|
|
|
if (!response.isEmpty()) {
|
|
|
|
QImage image;
|
|
|
|
image.loadFromData(response);
|
2012-03-06 15:28:52 +01:00
|
|
|
if (!image.isNull()) {
|
|
|
|
emit iconFetched(image);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 23:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emit finished();
|
|
|
|
}
|