Skip to content

Commit

Permalink
- If favicon.ico is 404, download the HTML and parse it looking
Browse files Browse the repository at this point in the history
  for the appropriate <link> to the favicon
- Search for "UNCONFIRMED" bugs in Bugzilla, as well
  • Loading branch information
Matt Barringer committed Feb 22, 2011
1 parent f2570d6 commit f2b5157
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
8 changes: 3 additions & 5 deletions Bugzilla.cpp
Expand Up @@ -174,7 +174,7 @@ Bugzilla::getUserBugs()
{
QString url = mUrl + QString("/buglist.cgi?query_format=advanced"
"&bug_status=NEW&bug_status=ASSIGNED"
"&bug_status=REOPENED&bug_status=NEEDINFO"
"&bug_status=REOPENED&bug_status=NEEDINFO&bug_status=UNCONFIRMED"
"&chfieldfrom=%1"
"&emailassigned_to1=1"
"&emailtype1=substring&email1=%2&ctype=csv")
Expand Down Expand Up @@ -205,7 +205,7 @@ Bugzilla::getReportedBugs()
{
QString url = mUrl + QString("/buglist.cgi?query_format=advanced"
"&bug_status=NEW&bug_status=ASSIGNED"
"&bug_status=REOPENED&bug_status=NEEDINFO"
"&bug_status=REOPENED&bug_status=NEEDINFO&bug_status=UNCONFIRMED"
"&chfieldfrom=%1"
"&emailreporter1=1"
"&emailtype1=substring&email1=%2&ctype=csv")
Expand Down Expand Up @@ -233,10 +233,8 @@ void
Bugzilla::getCCs()
{
qDebug() << "Getting CCs...";
//buglist.cgi?emailcc1=1&emailtype1=substring&query_format=advanced&bug_status=NEW&bug_status=ASSIGNED&bug_status=NEEDINFO&bug_status=REOPENED&email1=mbarringer%40novell.com

QString url = mUrl + QString("/buglist.cgi?emailcc1=1&emailtype1=substring"
"&query_format=advanced&bug_status=NEW&bug_status=ASSIGNED&bug_status=NEEDINFO&bug_status=REOPENED"
"&query_format=advanced&bug_status=NEW&bug_status=ASSIGNED&bug_status=NEEDINFO&bug_status=REOPENED&bug_status=UNCONFIRMED"
"&chfieldfrom=%1"
"&email1=%2&ctype=csv")
.arg(mLastSync.toString("yyyy-MM-dd"))
Expand Down
85 changes: 79 additions & 6 deletions MainWindow.cpp
Expand Up @@ -81,6 +81,8 @@ MainWindow::MainWindow(QWidget *parent) :
splitterSizes << 100;
splitterSizes << 400;
pManager = new QNetworkAccessManager();
connect(pManager, SIGNAL(sslErrors(QNetworkReply *, const QList<QSslError> &)),
this, SLOT(handleSslErrors(QNetworkReply *, const QList<QSslError> &)));

ui->setupUi(this);
setupTrayIcon();
Expand Down Expand Up @@ -651,7 +653,6 @@ MainWindow::addTrackerToList(Backend *newTracker)
mBackendMap[newTracker->id()] = newTracker;

syncTracker(newTracker);
// TODO this doesn't work for sites that link to the shortcut icon in the html <head>
if(!QFile::exists(iconPath))
fetchIcon(newTracker->url(), iconPath);
}
Expand Down Expand Up @@ -789,10 +790,7 @@ MainWindow::fetchIcon(const QString &url,
{
QUrl u(url);
QString fetch = "https://" + u.host() + "/favicon.ico";

// Some sites might not use the favicon.ico convention,
// but instead link to it in the HTML, but I don't think
// we'll bother with that.
qDebug() << "Fetching " << fetch;

QNetworkRequest req = QNetworkRequest(QUrl(fetch));
req.setAttribute(QNetworkRequest::User, QVariant(savePath));
Expand All @@ -806,12 +804,15 @@ void
MainWindow::iconDownloaded()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
QString savePath = reply->request().attribute(QNetworkRequest::User).toString();
qDebug() << "Icon downloaded";
if (reply->error())
{
reply->close();
qDebug() << "Couldn't get icon";
fetchHTMLIcon(reply->url().toString(), savePath);
return;
}
QString savePath = reply->request().attribute(QNetworkRequest::User).toString();
QByteArray logoData = reply->readAll();
// The favicon can be in various formats, so convert it to something
// we know we can safely display
Expand All @@ -834,6 +835,70 @@ MainWindow::iconDownloaded()
reply->close();
}

void
MainWindow::fetchHTMLIcon(const QString &url,
const QString &savePath)
{
QUrl u(url);
QString fetch = "https://" + u.host() + "/";
qDebug() << "Fetching " << fetch;
QNetworkRequest req = QNetworkRequest(QUrl(fetch));
req.setAttribute(QNetworkRequest::User, QVariant(savePath));
QNetworkReply *rep = pManager->get(req);
connect(rep, SIGNAL(finished()),
this, SLOT(htmlIconDownloaded()));
}

void
MainWindow::htmlIconDownloaded()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
QString url = reply->url().toString();
QString savePath = reply->request().attribute(QNetworkRequest::User).toString();
if (reply->error())
{
reply->close();
qDebug() << "Couldn't get an icon or HTML for " << url << " so I'm giving up: " << reply->errorString();
qDebug() << reply->readAll();
return;
}

QString html(reply->readAll());
QRegExp reg("<link (rel=\"([^\"]+)\")?\\s*(type=\"([^\"]+)\")?\\s*(href=\"([^\"]+)\")?\\s*/?>");
QString iconPath = "";
// Look for the first match
int pos = 0;
while ((pos = reg.indexIn(html, pos)) != -1)
{
if (reg.cap(2).endsWith("icon"))
{
iconPath = reg.cap(6);
break;
}

pos += reg.matchedLength();
}

if (iconPath.isEmpty())
{
qDebug() << "Couldn't find an icon in " << url;
return;
}

if (!iconPath.startsWith("http"))
{
qDebug() << "Path was wrong, fixing";
iconPath= "https://" + QUrl(url).host() + "/" + iconPath;
}
qDebug() << "Going to fetch " << iconPath;

QNetworkRequest req = QNetworkRequest(QUrl(iconPath));
req.setAttribute(QNetworkRequest::User, QVariant(savePath));
QNetworkReply *rep = pManager->get(req);
connect(rep, SIGNAL(finished()),
this, SLOT(iconDownloaded()));
}

// Sync a specific tracker
void
MainWindow::syncTracker(Backend *tracker)
Expand Down Expand Up @@ -1608,3 +1673,11 @@ MainWindow::eventFilter(QObject *obj, QEvent *event)
}
return false;
}

// TODO implement this?
void
MainWindow::handleSslErrors(QNetworkReply *reply,
const QList<QSslError> &errors)
{
reply->ignoreSslErrors();
}
7 changes: 6 additions & 1 deletion MainWindow.h
Expand Up @@ -28,14 +28,15 @@
#include <QMap>
#include <QThread>
#include <QSystemTrayIcon>

#include <QSslError>
namespace Ui {
class MainWindow;
}

class QListWidgetItem;
class QModelIndex;
class QMovie;
class QNetworkReply;
class QLabel;
class QNetworkAccessManager;
class QSpacerItem;
Expand All @@ -54,6 +55,8 @@ class MainWindow : public QMainWindow {

public slots:
void quitEvent();
void handleSslErrors(QNetworkReply *reply,
const QList<QSslError> &errors);
void trayActivated(QSystemTrayIcon::ActivationReason reason);
void addTrackerTriggered();
void prefsTriggered();
Expand All @@ -67,6 +70,7 @@ public slots:
void tableViewContextMenu(const QPoint &p);

void iconDownloaded();
void htmlIconDownloaded();
void bugsUpdated();
void bugClicked(const QModelIndex &);
void finishedDetecting(QMap<QString, QString> data);
Expand Down Expand Up @@ -100,6 +104,7 @@ public slots:
void addTracker(QMap<QString, QString> info);
void addTrackerToList(Backend *newTracker);
void fetchIcon(const QString &url, const QString &savePath);
void fetchHTMLIcon(const QString &url, const QString &savePath);
bool isOnline();
void loadDetails(long long id);
bool hasPendingChanges();
Expand Down

0 comments on commit f2b5157

Please sign in to comment.