~ubuntu-branches/debian/stretch/apper/stretch

« back to all changes in this revision

Viewing changes to libapper/AppStream.cpp

  • Committer: Package Import Robot
  • Author(s): Matthias Klumpp
  • Date: 2013-07-30 12:34:46 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130730123446-cgzujaj03pc61drn
Tags: 0.8.1-1
* New upstream release: 0.8.1
* Depend on software-properties-kde instead of suggesting it (Closes: #696583)
* Add patch to make AppStream loading more failsafe

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2010 by Daniel Nicoletti <dantti12@gmail.com>           *
 
3
 *   Copyright (C) 2012-2013 by Matthias Klumpp <matthias@tenstral.net>    *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or         *
 
6
 *   modify it under the terms of the GNU General Public License as        *
 
7
 *   published by the Free Software Foundation; either version 2 of        *
 
8
 *   the License or (at your option) version 3 or any later version        *
 
9
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 
10
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 
11
 *   defined in Section 14 of version 3 of the license.                    *
 
12
 *                                                                         *
 
13
 *   This program is distributed in the hope that it will be useful,       *
 
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
16
 *   GNU General Public License for more details.                          *
 
17
 *                                                                         *
 
18
 *   You should have received a copy of the GNU General Public License     *
 
19
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 
20
 ***************************************************************************/
 
21
 
 
22
#include <config.h>
 
23
 
 
24
#include <appstream.h>
 
25
 
 
26
#include "AppStream.h"
 
27
 
 
28
#include <QApplication>
 
29
#include <QVariant>
 
30
#include <QStringBuilder>
 
31
 
 
32
#include <KLocale>
 
33
#include <KGlobal>
 
34
#include <KDebug>
 
35
 
 
36
AppStream* AppStream::m_instance = 0;
 
37
 
 
38
AppStream* AppStream::instance()
 
39
{
 
40
    if(!m_instance) {
 
41
        m_instance = new AppStream(qApp);
 
42
        m_instance->open();
 
43
    }
 
44
 
 
45
    return m_instance;
 
46
}
 
47
 
 
48
AppStream::AppStream(QObject *parent)
 
49
 : QObject(parent)
 
50
{
 
51
#ifdef HAVE_APPSTREAM
 
52
    // create new AppStream database and screenshot service
 
53
    m_asDB = appstream_database_new();
 
54
    m_asScreenshots = appstream_screenshot_service_new();
 
55
#endif //HAVE_APPSTREAM
 
56
}
 
57
 
 
58
AppStream::~AppStream()
 
59
{
 
60
#ifdef HAVE_APPSTREAM
 
61
    g_object_unref(m_asDB);
 
62
    g_object_unref(m_asScreenshots);
 
63
#endif
 
64
}
 
65
 
 
66
bool AppStream::open()
 
67
{
 
68
#ifdef HAVE_APPSTREAM
 
69
    bool ret = appstream_database_open(m_asDB);
 
70
    if (!ret) {
 
71
        qWarning("Unable to open AppStream Xapian database!");
 
72
        return false;
 
73
    }
 
74
 
 
75
    // cache application data (we might use the db directly, later (making use of AppstreamSearchQuery))
 
76
    GPtrArray *appArray;
 
77
    appArray = appstream_database_get_all_applications(m_asDB);
 
78
    if (appArray == NULL) {
 
79
        qWarning("AppStream application array way NULL! (This should never happen)");
 
80
        return false;
 
81
    }
 
82
 
 
83
    for (uint i = 0; i < appArray->len; i++) {
 
84
        AppstreamAppInfo *appInfo;
 
85
        appInfo = (AppstreamAppInfo*) g_ptr_array_index(appArray, i);
 
86
 
 
87
        Application app;
 
88
        // Application name
 
89
        app.name = QString::fromUtf8(appstream_app_info_get_name(appInfo));
 
90
 
 
91
        // Package name
 
92
        QString pkgName = QString::fromUtf8(appstream_app_info_get_pkgname(appInfo));
 
93
 
 
94
        // Desktop file
 
95
        app.id = QString::fromUtf8(appstream_app_info_get_desktop_file(appInfo));
 
96
 
 
97
        // Summary
 
98
        app.summary = QString::fromUtf8(appstream_app_info_get_summary(appInfo));
 
99
 
 
100
        // Application stock icon
 
101
        app.icon = QString::fromUtf8(appstream_app_info_get_icon(appInfo));
 
102
 
 
103
        // Application categories
 
104
        int clen;
 
105
        gchar **cats = appstream_app_info_get_categories(appInfo, &clen);
 
106
        if (cats != NULL) {
 
107
            app.categories = QStringList();
 
108
            for (int j = 0; j < clen; j++) {
 
109
                app.categories << QString::fromUtf8(cats[j]);
 
110
            }
 
111
        }
 
112
        g_strfreev(cats);
 
113
 
 
114
        m_appInfo.insertMulti(pkgName, app);
 
115
    }
 
116
    g_ptr_array_unref(appArray);
 
117
 
 
118
    return true;
 
119
#else
 
120
    return false;
 
121
#endif
 
122
}
 
123
 
 
124
QList<AppStream::Application> AppStream::applications(const QString &pkgName) const
 
125
{
 
126
    return m_appInfo.values(pkgName);;
 
127
}
 
128
 
 
129
QString AppStream::genericIcon(const QString &pkgName) const
 
130
{
 
131
    if (m_appInfo.contains(pkgName)) {
 
132
        foreach (const Application &app, applications(pkgName)) {
 
133
            if (!app.icon.isEmpty()) {
 
134
                return app.icon;
 
135
            }
 
136
        }
 
137
    }
 
138
 
 
139
    return QString();
 
140
}
 
141
 
 
142
QStringList AppStream::findPkgNames(const CategoryMatcher &parser) const
 
143
{
 
144
    QStringList packages;
 
145
 
 
146
    QHash<QString, Application>::const_iterator i = m_appInfo.constBegin();
 
147
    while (i != m_appInfo.constEnd()) {
 
148
        if (parser.match(i.value().categories)) {
 
149
//            kDebug() << i.key() << categories;
 
150
            packages << i.key();
 
151
        }
 
152
        ++i;
 
153
    }
 
154
 
 
155
    return packages;
 
156
}
 
157
 
 
158
QString AppStream::thumbnail(const QString &pkgName) const
 
159
{
 
160
#ifdef HAVE_APPSTREAM
 
161
    const gchar *url = appstream_screenshot_service_get_thumbnail_url(m_asScreenshots, pkgName.toLatin1().data());
 
162
    return QLatin1String(url);
 
163
#else
 
164
    Q_UNUSED(pkgName)
 
165
    return QString();
 
166
#endif //HAVE_APPSTREAM
 
167
}
 
168
 
 
169
QString AppStream::screenshot(const QString &pkgName) const
 
170
{
 
171
#ifdef HAVE_APPSTREAM
 
172
    const gchar *url = appstream_screenshot_service_get_screenshot_url(m_asScreenshots, pkgName.toLatin1().data());
 
173
    return QLatin1String(url);
 
174
#else
 
175
    Q_UNUSED(pkgName)
 
176
    return QString();
 
177
#endif //HAVE_APPSTREAM
 
178
}