Git commit a59b9e61897887ea93cf736aff99243facdc45d9 by Aleix Pol. Committed on 05/04/2017 at 17:41. Pushed by apol into branch 'master'.
Merge the AppStream runner into the Plasma Workspace CCMAIL: sit...@kde.org CCMAIL: plasma-devel@kde.org M +7 -0 CMakeLists.txt M +5 -1 runners/CMakeLists.txt A +5 -0 runners/appstream/CMakeLists.txt A +125 -0 runners/appstream/appstreamrunner.cpp [License: GPL (v2/3)] A +13 -0 runners/appstream/appstreamrunner.desktop A +47 -0 runners/appstream/appstreamrunner.h [License: GPL (v2/3)] https://commits.kde.org/plasma-workspace/a59b9e61897887ea93cf736aff99243facdc45d9 diff --git a/CMakeLists.txt b/CMakeLists.txt index d20a3adc..4f03ba05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,13 @@ if(X11_FOUND AND XCB_XCB_FOUND) set(HAVE_X11 1) endif() +find_package(AppStreamQt 0.10.4) +set_package_properties(AppStreamQt PROPERTIES DESCRIPTION "Access metadata for listing available software" + URL "https://www.freedesktop.org/wiki/Distributions/AppStream/" + TYPE OPTIONAL + ) + + include(ConfigureChecks.cmake) include_directories("${CMAKE_CURRENT_BINARY_DIR}") diff --git a/runners/CMakeLists.txt b/runners/CMakeLists.txt index d7656c82..a1feee07 100644 --- a/runners/CMakeLists.txt +++ b/runners/CMakeLists.txt @@ -12,7 +12,11 @@ add_subdirectory(shell) # add_subdirectory(solid) add_subdirectory(webshortcuts) add_subdirectory(windowedwidgets) -# + +if(AppStreamQt_FOUND) + add_subdirectory(appstream) +endif() + if(NOT WIN32) add_subdirectory(powerdevil) add_subdirectory(sessions) diff --git a/runners/appstream/CMakeLists.txt b/runners/appstream/CMakeLists.txt new file mode 100644 index 00000000..271e8c8c --- /dev/null +++ b/runners/appstream/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(appstreamrunner MODULE appstreamrunner.cpp) +target_link_libraries(appstreamrunner PUBLIC KF5::Runner KF5::I18n KF5::Service AppStreamQt) + +install(TARGETS appstreamrunner DESTINATION ${PLUGIN_INSTALL_DIR}) +install(FILES appstreamrunner.desktop DESTINATION ${SERVICES_INSTALL_DIR}) diff --git a/runners/appstream/appstreamrunner.cpp b/runners/appstream/appstreamrunner.cpp new file mode 100644 index 00000000..a8aa9d2a --- /dev/null +++ b/runners/appstream/appstreamrunner.cpp @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright © 2016 Aleix Pol Gonzalez <aleix...@kde.org> * + * * + * 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 "appstreamrunner.h" + +#include <AppStreamQt/icon.h> + +#include <QDir> +#include <QIcon> +#include <QDesktopServices> +#include <QDebug> + +#include <KLocalizedString> +#include <KServiceTypeTrader> + +K_EXPORT_PLASMA_RUNNER(installer, InstallerRunner) + +InstallerRunner::InstallerRunner(QObject *parent, const QVariantList &args) + : Plasma::AbstractRunner(parent, args) +{ + Q_UNUSED(args) + + setObjectName("Installation Suggestions"); + setPriority(AbstractRunner::HighestPriority); + + addSyntax(Plasma::RunnerSyntax(":q:", i18n("Looks for non-installed components according to :q:"))); +} + +InstallerRunner::~InstallerRunner() +{ +} + +static QIcon componentIcon(const AppStream::Component &comp) +{ + QIcon ret; + const auto icons = comp.icons(); + if (icons.isEmpty()) { + ret = QIcon::fromTheme(QStringLiteral("package-x-generic")); + } else foreach(const AppStream::Icon &icon, icons) { + QStringList stock; + switch(icon.kind()) { + case AppStream::Icon::KindLocal: + ret.addFile(icon.url().toLocalFile(), icon.size()); + break; + case AppStream::Icon::KindCached: + ret.addFile(icon.url().toLocalFile(), icon.size()); + break; + case AppStream::Icon::KindStock: + stock += icon.name(); + break; + default: + break; + } + if (ret.isNull() && !stock.isEmpty()) { + ret = QIcon::fromTheme(stock.first()); + } + } + return ret; +} + +void InstallerRunner::match(Plasma::RunnerContext &context) +{ + if(context.query().size() <= 2) + return; + + auto components = findComponentsByString(context.query()); + + foreach(const AppStream::Component &component, components) { + if (component.kind() != AppStream::Component::KindDesktopApp) + continue; + + const auto idWithoutDesktop = component.id().remove(".desktop"); + const auto serviceQuery = QStringLiteral("exist Exec and (('%1' =~ DesktopEntryName) or '%2' =~ DesktopEntryName)").arg(component.id(), idWithoutDesktop); + const auto servicesFound = KServiceTypeTrader::self()->query(QStringLiteral("Application"), serviceQuery); + if (!servicesFound.isEmpty()) + continue; + + Plasma::QueryMatch match(this); + match.setType(Plasma::QueryMatch::PossibleMatch); + match.setId(component.id()); + match.setIcon(componentIcon(component)); + match.setText(i18n("Get %1...", component.name())); + match.setSubtext(component.summary()); + match.setData(QUrl("appstream://" + component.id())); + context.addMatch(match); + } +} + +void InstallerRunner::run(const Plasma::RunnerContext &/*context*/, const Plasma::QueryMatch &match) +{ + const QUrl appstreamUrl = match.data().toUrl(); + if (!QDesktopServices::openUrl(appstreamUrl)) + qWarning() << "couldn't open" << appstreamUrl; +} + +QList<AppStream::Component> InstallerRunner::findComponentsByString(const QString &query) +{ + QMutexLocker locker(&m_appstreamMutex); + static bool opened = m_db.load(); + if(!opened) { + qWarning() << "no appstream for you"; + return QList<AppStream::Component>(); + } + + return m_db.search(query); +} + +#include "appstreamrunner.moc" diff --git a/runners/appstream/appstreamrunner.desktop b/runners/appstream/appstreamrunner.desktop new file mode 100644 index 00000000..e6b81a37 --- /dev/null +++ b/runners/appstream/appstreamrunner.desktop @@ -0,0 +1,13 @@ +[Desktop Entry] +Name=Software Center +Comment=Lets you find software +Icon=applications-other +Type=Service +X-KDE-ServiceTypes=Plasma/Runner + +X-KDE-Library=appstreamrunner +X-KDE-PluginInfo-Author=Aleix Pol Gonzalez +X-KDE-PluginInfo-Email=aleix...@blue-systems.com +X-KDE-PluginInfo-Version=0.1 +X-KDE-PluginInfo-License=GPL +X-KDE-PluginInfo-EnabledByDefault=true diff --git a/runners/appstream/appstreamrunner.h b/runners/appstream/appstreamrunner.h new file mode 100644 index 00000000..91e1d3bb --- /dev/null +++ b/runners/appstream/appstreamrunner.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * Copyright © 2016 Aleix Pol Gonzalez <aleix...@kde.org> * + * * + * 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/>. * + ***************************************************************************/ + +#ifndef INSTALLERRUNNER_H +#define INSTALLERRUNNER_H + +#include <KRunner/AbstractRunner> +#include <AppStreamQt/pool.h> +#include <QMutex> + +class InstallerRunner : public Plasma::AbstractRunner +{ +Q_OBJECT + +public: + InstallerRunner(QObject *parent, const QVariantList &args); + ~InstallerRunner(); + + void match(Plasma::RunnerContext &context) override; + void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action) override; + +private: + QList<AppStream::Component> findComponentsByString(const QString &query); + + AppStream::Pool m_db; + QMutex m_appstreamMutex; +}; + +#endif +