faviconfromblob.cpp 5.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 *   Copyright 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
 *   Copyright 2012 Glenn Ergeerts <marco.gulino@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2, 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 Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "faviconfromblob.h"

Alexander Lohnau's avatar
Alexander Lohnau committed
23
#include "bookmarksrunner_defs.h"
24
#include <QDebug>
Alexander Lohnau's avatar
Alexander Lohnau committed
25
#include <QDir>
26
27
28
#include <QFile>
#include <QFileInfo>
#include <QPainter>
Alexander Lohnau's avatar
Alexander Lohnau committed
29
30
#include <QPixmap>
#include <QStandardPaths>
31
32
33

#include <QSqlDatabase>
#include <QSqlError>
Alexander Lohnau's avatar
Alexander Lohnau committed
34
#include <QSqlQuery>
35
36
37
38
39
#include <QSqlRecord>

FaviconFromBlob *FaviconFromBlob::chrome(const QString &profileDirectory, QObject *parent)
{
    QString profileName = QFileInfo(profileDirectory).fileName();
Alexander Lohnau's avatar
Alexander Lohnau committed
40
41
    QString faviconCache =
        QStringLiteral("%1/KRunner-Chrome-Favicons-%2.sqlite").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), profileName);
42
    FetchSqlite *fetchSqlite = new FetchSqlite(faviconCache, parent);
43
44

    QString faviconQuery;
Alexander Lohnau's avatar
Alexander Lohnau committed
45
46
47
48
49
50
    if (fetchSqlite->tables().contains(QLatin1String("favicon_bitmaps"))) {
        faviconQuery = QLatin1String(
            "SELECT * FROM favicons "
            "inner join icon_mapping on icon_mapping.icon_id = favicons.id "
            "inner join favicon_bitmaps on icon_mapping.icon_id = favicon_bitmaps.icon_id "
            "WHERE page_url = :url ORDER BY height desc LIMIT 1;");
51
    } else {
Alexander Lohnau's avatar
Alexander Lohnau committed
52
53
54
55
        faviconQuery = QLatin1String(
            "SELECT * FROM favicons "
            "inner join icon_mapping on icon_mapping.icon_id = favicons.id "
            "WHERE page_url = :url LIMIT 1;");
56
57
    }

58
    return new FaviconFromBlob(profileName, faviconQuery, QStringLiteral("image_data"), fetchSqlite, parent);
59
60
61
62
}

FaviconFromBlob *FaviconFromBlob::firefox(FetchSqlite *fetchSqlite, QObject *parent)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
63
64
65
66
67
    QString faviconQuery = QStringLiteral(
        "SELECT moz_icons.data FROM moz_icons"
        " INNER JOIN moz_icons_to_pages ON moz_icons.id = moz_icons_to_pages.icon_id"
        " INNER JOIN moz_pages_w_icons ON moz_icons_to_pages.page_id = moz_pages_w_icons.id"
        " WHERE moz_pages_w_icons.page_url = :url LIMIT 1;");
68
    return new FaviconFromBlob(QStringLiteral("firefox-default"), faviconQuery, QStringLiteral("data"), fetchSqlite, parent);
69
70
}

71
72
73
74
75
FaviconFromBlob *FaviconFromBlob::falkon(const QString &profileDirectory, QObject *parent)
{
    const QString dbPath = profileDirectory + QStringLiteral("/browsedata.db");
    FetchSqlite *fetchSqlite = new FetchSqlite(dbPath, parent);
    const QString faviconQuery = QStringLiteral("SELECT icon FROM icons WHERE url = :url LIMIT 1;");
76
    return new FaviconFromBlob(QStringLiteral("falkon-default"), faviconQuery, QStringLiteral("icon"), fetchSqlite, parent);
77
}
78

79
FaviconFromBlob::FaviconFromBlob(const QString &profileName, const QString &query, const QString &blobColumn, FetchSqlite *fetchSqlite, QObject *parent)
Alexander Lohnau's avatar
Alexander Lohnau committed
80
81
82
83
    : Favicon(parent)
    , m_query(query)
    , m_blobcolumn(blobColumn)
    , m_fetchsqlite(fetchSqlite)
84
{
Alexander Lohnau's avatar
Alexander Lohnau committed
85
86
    m_profileCacheDirectory = QStringLiteral("%1/KRunner-Favicons-%2").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), profileName);
    // qDebug() << "got cache directory: " << m_profileCacheDirectory;
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    cleanCacheDirectory();
    QDir().mkpath(m_profileCacheDirectory);
}

FaviconFromBlob::~FaviconFromBlob()
{
    cleanCacheDirectory();
}

void FaviconFromBlob::prepare()
{
    m_fetchsqlite->prepare();
}

void FaviconFromBlob::teardown()
{
    m_fetchsqlite->teardown();
}

void FaviconFromBlob::cleanCacheDirectory()
{
108
    QDir(m_profileCacheDirectory).removeRecursively();
109
110
111
112
}

QIcon FaviconFromBlob::iconFor(const QString &url)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
113
    // qDebug() << "got url: " << url;
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
114
    QString fileChecksum = QString::number(qChecksum(url.toLatin1(), url.toLatin1().size()));
Alexander Lohnau's avatar
Alexander Lohnau committed
115
116
    QFile iconFile(m_profileCacheDirectory + QDir::separator() + fileChecksum + QStringLiteral("_favicon"));
    if (iconFile.size() == 0)
117
        iconFile.remove();
Alexander Lohnau's avatar
Alexander Lohnau committed
118
119
    if (!iconFile.exists()) {
        QMap<QString, QVariant> bindVariables;
120
        bindVariables.insert(QStringLiteral(":url"), url);
121
        QList<QVariantMap> faviconFound = m_fetchsqlite->query(m_query, bindVariables);
Alexander Lohnau's avatar
Alexander Lohnau committed
122
123
        if (faviconFound.isEmpty())
            return defaultIcon();
124
125

        QByteArray iconData = faviconFound.first().value(m_blobcolumn).toByteArray();
Alexander Lohnau's avatar
Alexander Lohnau committed
126
127
        // qDebug() << "Favicon found: " << iconData.size() << " bytes";
        if (iconData.size() <= 0)
128
129
130
131
132
133
134
135
            return defaultIcon();

        iconFile.open(QFile::WriteOnly);
        iconFile.write(iconData);
        iconFile.close();
    }
    return QIcon(iconFile.fileName());
}