1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/***************************************************************************
* Copyright (C) 2010 by Daniel Nicoletti <dantti12@gmail.com> *
* Copyright (C) 2012-2013 by Matthias Klumpp <matthias@tenstral.net> *
* *
* 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 2 of *
* the License or (at your option) version 3 or any later version *
* accepted by the membership of KDE e.V. (or its successor approved *
* by the membership of KDE e.V.), which shall act as a proxy *
* defined in Section 14 of version 3 of the license. *
* *
* 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/>. *
***************************************************************************/
#include <config.h>
#include <AppStreamQt/pool.h>
#include <AppStreamQt/icon.h>
#include <AppStreamQt/image.h>
#include <AppStreamQt/screenshot.h>
#include "AppStream.h"
#include <QApplication>
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(APPER_LIB)
AppStreamHelper* AppStreamHelper::m_instance = 0;
AppStreamHelper* AppStreamHelper::instance()
{
if(!m_instance) {
m_instance = new AppStreamHelper(qApp);
m_instance->open();
}
return m_instance;
}
AppStreamHelper::AppStreamHelper(QObject *parent)
: QObject(parent)
{
#ifdef HAVE_APPSTREAM
// create new AppStream metadata pool
m_pool = new AppStream::Pool(this);
#endif //HAVE_APPSTREAM
}
AppStreamHelper::~AppStreamHelper()
{
}
bool AppStreamHelper::open()
{
#ifdef HAVE_APPSTREAM
QString error;
if (!m_pool->load(&error)) {
qCWarning(APPER_LIB) << "Unable to open AppStream metadata pool:" << error;
return false;
}
// // cache application data (we should actually search the data directly via as_pool_search()...)
// auto cptArray = as_pool_get_components(m_pool);
// if (cptArray == NULL) {
// qWarning("AppStream application array way NULL! (This should never happen)");
// return false;
// }
// for (uint i = 0; i < cptArray->len; i++) {
// auto cpt = AS_COMPONENT (g_ptr_array_index(cptArray, i));
// // we only want apps at time
// auto cptKind = as_component_get_kind (cpt);
// if ((cptKind != AS_COMPONENT_KIND_DESKTOP_APP) &&
// (cptKind != AS_COMPONENT_KIND_CONSOLE_APP) &&
// (cptKind != AS_COMPONENT_KIND_WEB_APP))
// continue;
// Application app;
// // Application name
// app.name = QString::fromUtf8(as_component_get_name(cpt));
// // Package name
// auto pkgnameC = as_component_get_pkgname(cpt);
// QString pkgName;
// if (pkgnameC != NULL)
// pkgName = QString::fromUtf8(pkgnameC);
// // Desktop file
// app.id = QString::fromUtf8(as_component_get_id(cpt));
// // Summary
// app.summary = QString::fromUtf8(as_component_get_summary(cpt));
// // Description
// app.description = QString::fromUtf8(as_component_get_description(cpt));
// // Application stock icon
// auto icons = as_component_get_icons(cpt);
// for (uint i = 0; i < icons->len; i++) {
// auto icon = AS_ICON (g_ptr_array_index (icons, i));
// if (as_icon_get_kind (icon) != AS_ICON_KIND_STOCK)
// app.icon_url = QString::fromUtf8(as_icon_get_filename (icon));
// }
// // Application categories
// app.categories = QStringList();
// auto cats = as_component_get_categories(cpt);
// for (uint i = 0; i < cats->len; i++) {
// auto category = (const gchar*) g_ptr_array_index (cats, i);
// app.categories << QString::fromUtf8(category);
// }
// // add default screenshot urls
// auto scrs = as_component_get_screenshots (cpt);
// // find default screenshot, if possible (otherwise we get a random one)
// AsScreenshot *scr = NULL;
// for (uint i = 0; i < scrs->len; i++) {
// scr = AS_SCREENSHOT (g_ptr_array_index (scrs, i));
// if (as_screenshot_get_kind (scr) == AS_SCREENSHOT_KIND_DEFAULT)
// break;
// }
// if (scr != NULL) {
// auto imgs = as_screenshot_get_images (scr);
// for (uint i = 0; i < imgs->len; i++) {
// auto img = AS_IMAGE (g_ptr_array_index (imgs, i));
// if ((as_image_get_kind (img) == AS_IMAGE_KIND_SOURCE) && (app.screenshot.isEmpty())) {
// app.screenshot = QString::fromUtf8(as_image_get_url (img));
// } else if ((as_image_get_kind (img) == AS_IMAGE_KIND_THUMBNAIL) && (app.thumbnail.isEmpty())) {
// app.thumbnail = QString::fromUtf8(as_image_get_url (img));
// }
// if ((!app.screenshot.isEmpty()) && (!app.thumbnail.isEmpty()))
// break;
// }
// }
// m_appInfo.insertMulti(pkgName, app);
// }
const QList<AppStream::Component> apps = m_pool->componentsByKind(AppStream::Component::KindDesktopApp);
for (const AppStream::Component &app : apps) {
const QStringList pkgNames = app.packageNames();
for (const QString &pkgName : pkgNames) {
m_appInfo.insertMulti(pkgName, app);
}
}
return true;
#else
return false;
#endif
}
QList<AppStream::Component> AppStreamHelper::applications(const QString &pkgName) const
{
return m_appInfo.values(pkgName);
}
QString AppStreamHelper::genericIcon(const QString &pkgName) const
{
if (m_appInfo.contains(pkgName)) {
// const QList<AppStream::Component> apps = applications(pkgName);
// for (const AppStream::Component &app : apps) {
// if (!app.icon_url.isEmpty()) {
// return app.icon_url;
// }
// }
}
return QString();
}
QStringList AppStreamHelper::findPkgNames(const CategoryMatcher &/*parser*/) const
{
QStringList packages;
// QHash<QString, Application>::const_iterator i = m_appInfo.constBegin();
// while (i != m_appInfo.constEnd()) {
// if (parser.match(i.value().categories)) {
//// kDebug() << i.key() << categories;
// packages << i.key();
// }
// ++i;
// }
return packages;
}
QUrl AppStreamHelper::thumbnail(const QString &pkgName) const
{
QUrl url;
#ifdef HAVE_APPSTREAM
if (m_appInfo.contains(pkgName)) {
AppStream::Image thumb;
AppStream::Component app = m_appInfo.value(pkgName);
const QList<AppStream::Screenshot> screenshots = app.screenshots();
for (const AppStream::Screenshot &screenshot : screenshots) {
const QList<AppStream::Image> images = screenshot.images();
for (const AppStream::Image &image : images) {
if (image.kind() == AppStream::Image::KindThumbnail) {
if (thumb.kind() == AppStream::Image::KindUnknown || image.size().height() < thumb.size().height()) {
thumb = image;
url = image.url();
}
}
}
if (screenshot.isDefault() && !url.isEmpty()) {
break;
}
}
}
return url;
#else
Q_UNUSED(pkgName)
return url;
#endif //HAVE_APPSTREAM
}
QUrl AppStreamHelper::screenshot(const QString &pkgName) const
{
QUrl url;
#ifdef HAVE_APPSTREAM
if (m_appInfo.contains(pkgName)) {
AppStream::Component app = m_appInfo.value(pkgName);
const QList<AppStream::Screenshot> screenshots = app.screenshots();
for (const AppStream::Screenshot &screenshot : screenshots) {
const QList<AppStream::Image> images = screenshot.images();
for (const AppStream::Image &image : images) {
url = image.url();
break;
}
if (screenshot.isDefault() && !url.isEmpty()) {
break;
}
}
}
#else
Q_UNUSED(pkgName)
#endif //HAVE_APPSTREAM
return url;
}
|