diff --git a/actionlist.cpp b/actionlist.cpp index 57f2a2c7f..2aa97d934 100644 --- a/actionlist.cpp +++ b/actionlist.cpp @@ -1,311 +1,311 @@ /*************************************************************************** * Copyright (C) 2013 by Aurélien Gâteau * * Copyright (C) 2014 by Eike Hein * * * * 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) 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 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 "actionlist.h" #include #include #include #include #include -#include -#include -#include +#include +#include +#include #include "containmentinterface.h" -namespace KAStats = KActivities::Experimental::Stats; +namespace KAStats = KActivities::Stats; using namespace KAStats; using namespace KAStats::Terms; namespace Kicker { QVariantMap createActionItem(const QString &label, const QString &actionId, const QVariant &argument) { QVariantMap map; map["text"] = label; map["actionId"] = actionId; if (argument.isValid()) { map["actionArgument"] = argument; } return map; } QVariantMap createTitleActionItem(const QString &label) { QVariantMap map; map["text"] = label; map["type"] = "title"; return map; } QVariantMap createSeparatorActionItem() { QVariantMap map; map["type"] = "separator"; return map; } QVariantList createActionListForFileItem(const KFileItem &fileItem) { QVariantList list; KService::List services = KMimeTypeTrader::self()->query(fileItem.mimetype(), "Application"); if (!services.isEmpty()) { list << createTitleActionItem(i18n("Open with:")); foreach (const KService::Ptr service, services) { const QString text = service->name().replace('&', "&&"); QVariantMap item = createActionItem(text, "_kicker_fileItem_openWith", service->entryPath()); item["icon"] = service->icon(); list << item; } list << createSeparatorActionItem(); } list << createActionItem(i18n("Properties"), "_kicker_fileItem_properties"); return list; } bool handleFileItemAction(const KFileItem &fileItem, const QString &actionId, const QVariant &argument, bool *close) { if (actionId == "_kicker_fileItem_properties") { KPropertiesDialog *dlg = new KPropertiesDialog(fileItem, QApplication::activeWindow()); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->show(); *close = false; return true; } if (actionId == "_kicker_fileItem_openWith") { const QString path = argument.toString(); const KService::Ptr service = KService::serviceByDesktopPath(path); if (!service) { return false; } KRun::runService(*service, QList() << fileItem.url(), QApplication::activeWindow()); *close = true; return true; } return false; } QVariantList createAddLauncherActionList(QObject *appletInterface, const KService::Ptr &service) { QVariantList actionList; if (!service) { return actionList; } if (ContainmentInterface::mayAddLauncher(appletInterface, ContainmentInterface::Desktop)) { actionList << Kicker::createActionItem(i18n("Add to Desktop"), "addToDesktop"); } if (ContainmentInterface::mayAddLauncher(appletInterface, ContainmentInterface::Panel)) { actionList << Kicker::createActionItem(i18n("Add to Panel"), "addToPanel"); } if (service && ContainmentInterface::mayAddLauncher(appletInterface, ContainmentInterface::TaskManager, service->entryPath())) { actionList << Kicker::createActionItem(i18n("Add as Launcher"), "addToTaskManager"); } return actionList; } bool handleAddLauncherAction(const QString &actionId, QObject *appletInterface, const KService::Ptr &service) { if (!service) { return false; } if (actionId == QLatin1String("addToDesktop")) { if (ContainmentInterface::mayAddLauncher(appletInterface, ContainmentInterface::Desktop)) { ContainmentInterface::addLauncher(appletInterface, ContainmentInterface::Desktop, service->entryPath()); } return true; } else if (actionId == QLatin1String("addToPanel")) { if (ContainmentInterface::mayAddLauncher(appletInterface, ContainmentInterface::Panel)) { ContainmentInterface::addLauncher(appletInterface, ContainmentInterface::Panel, service->entryPath()); } return true; } else if (actionId == QLatin1String("addToTaskManager")) { if (ContainmentInterface::mayAddLauncher(appletInterface, ContainmentInterface::TaskManager, service->entryPath())) { ContainmentInterface::addLauncher(appletInterface, ContainmentInterface::TaskManager, service->entryPath()); } return true; } return false; } // HACK TEMP FIXME TODO IVAN QString storageIdFromService(KService::Ptr service) { QString storageId = service->storageId(); if (storageId.startsWith("org.kde.")) { storageId = storageId.right(storageId.length() - 8); } if (storageId.endsWith(".desktop")) { storageId = storageId.left(storageId.length() - 8); } return storageId; } QVariantList jumpListActions(KService::Ptr service) { QVariantList list; if (!service) { return list; } const auto &actions = service->actions(); foreach (const KServiceAction &action, actions) { if (action.text().isEmpty() || action.exec().isEmpty()) { continue; } QVariantMap item = createActionItem(action.text(), "_kicker_jumpListAction", action.exec()); item[QStringLiteral("icon")] = action.icon(); list << item; } return list; } QVariantList recentDocumentActions(KService::Ptr service) { QVariantList list; if (!service) { return list; } const QString storageId = storageIdFromService(service); if (storageId.isEmpty()) { return list; } auto query = UsedResources | RecentlyUsedFirst | Agent(storageId) | Type::any() | Activity::current() | Url::file(); ResultSet results(query); ResultSet::const_iterator resultIt; resultIt = results.begin(); while (list.count() < 6 && resultIt != results.end()) { const QString resource = (*resultIt).resource(); const QUrl url(resource); if (!url.isValid()) { continue; } const KFileItem fileItem(url); if (!fileItem.isFile()) { continue; } if (list.count() == 0) { list << createTitleActionItem(i18n("Recent Documents")); } QVariantMap item = createActionItem(url.fileName(), "_kicker_recentDocument", resource); item["icon"] = fileItem.iconName(); list << item; ++resultIt; } if (list.count()) { list << createActionItem(i18n("Forget Recent Documents"), "_kicker_forgetRecentDocuments"); } return list; } bool handleRecentDocumentAction(KService::Ptr service, const QString &actionId, const QVariant &_argument) { if (!service) { return false; } if (actionId == "_kicker_forgetRecentDocuments") { const QString storageId = storageIdFromService(service); if (storageId.isEmpty()) { return false; } auto query = UsedResources | Agent(storageId) | Type::any() | Activity::current() | Url::file(); KAStats::forgetResources(query); return false; } QString argument = _argument.toString(); if (argument.isEmpty()) { return false; } return (KRun::runService(*service, QList() << QUrl(argument), QApplication::activeWindow()) != 0); } } diff --git a/recentcontactsmodel.cpp b/recentcontactsmodel.cpp index 182b7d059..1d2c9f54e 100644 --- a/recentcontactsmodel.cpp +++ b/recentcontactsmodel.cpp @@ -1,244 +1,244 @@ /*************************************************************************** * Copyright (C) 2012 by Aurélien Gâteau * * Copyright (C) 2014-2015 by Eike Hein * * * * 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) 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 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 "recentcontactsmodel.h" #include "actionlist.h" #include "contactentry.h" #include #include -#include -#include +#include +#include #include //FIXME TODO: Pretty include in KPeople broken. #include #include -namespace KAStats = KActivities::Experimental::Stats; +namespace KAStats = KActivities::Stats; using namespace KAStats; using namespace KAStats::Terms; RecentContactsModel::RecentContactsModel(QObject *parent) : ForwardingModel(parent) { refresh(); } RecentContactsModel::~RecentContactsModel() { } QString RecentContactsModel::description() const { return i18n("Contacts"); } QVariant RecentContactsModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } QString id = sourceModel()->data(index, ResultModel::ResourceRole).toString(); KPeople::PersonData *data = 0; if (m_idToData.contains(id)) { data = m_idToData[id]; } if (!data) { const_cast(this)->insertPersonData(id, index.row()); return QVariant(); } if (role == Qt::DisplayRole) { return data->name(); } else if (role == Qt::DecorationRole) { return data->presenceIconName(); } else if (role == Kicker::FavoriteIdRole) { return id; } else if (role == Kicker::HasActionListRole) { return true; } else if (role == Kicker::ActionListRole) { QVariantList actionList ; const QVariantMap &forgetAction = Kicker::createActionItem(i18n("Forget Contact"), "forget"); actionList << forgetAction; const QVariantMap &forgetAllAction = Kicker::createActionItem(i18n("Forget All Contacts"), "forgetAll"); actionList << forgetAllAction; actionList << Kicker::createSeparatorActionItem(); actionList << Kicker::createActionItem(i18n("Show Contact Information..."), "showContactInfo"); return actionList; } return QVariant(); } bool RecentContactsModel::trigger(int row, const QString &actionId, const QVariant &argument) { Q_UNUSED(argument) bool withinBounds = row >= 0 && row < rowCount(); if (actionId.isEmpty() && withinBounds) { QString id = sourceModel()->data(sourceModel()->index(row, 0), ResultModel::ResourceRole).toString(); const QList actionList = KPeople::actionsForPerson(id, this); if (!actionList.isEmpty()) { QAction *chat = 0; foreach (QAction *action, actionList) { const QVariant &actionType = action->property("actionType"); if (!actionType.isNull() && actionType.toInt() == KPeople::ActionType::TextChatAction) { chat = action; } } if (chat) { chat->trigger(); return true; } } return false; } else if (actionId == "showContactInfo" && withinBounds) { ContactEntry::showPersonDetailsDialog(sourceModel()->data(sourceModel()->index(row, 0), ResultModel::ResourceRole).toString()); } else if (actionId == "forget" && withinBounds) { if (sourceModel()) { ResultModel *resultModel = static_cast(sourceModel()); resultModel->forgetResource(row); } return false; } else if (actionId == "forgetAll") { if (sourceModel()) { ResultModel *resultModel = static_cast(sourceModel()); resultModel->forgetAllResources(); } return false; } return false; } bool RecentContactsModel::hasActions() const { return rowCount(); } QVariantList RecentContactsModel::actions() const { QVariantList actionList; if (rowCount()) { actionList << Kicker::createActionItem(i18n("Forget All Contacts"), "forgetAll"); } return actionList; } void RecentContactsModel::refresh() { QObject *oldModel = sourceModel(); auto query = UsedResources | RecentlyUsedFirst | Agent("KTp") | Type::any() | Activity::current() | Url::startsWith("ktp") | Limit(15); ResultModel *model = new ResultModel(query); QModelIndex index; if (model->canFetchMore(index)) { model->fetchMore(index); } // FIXME TODO: Don't wipe entire cache on transactions. connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(buildCache()), Qt::UniqueConnection); connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(buildCache()), Qt::UniqueConnection); connect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), this, SLOT(buildCache()), Qt::UniqueConnection); connect(model, SIGNAL(modelReset()), this, SLOT(buildCache()), Qt::UniqueConnection); setSourceModel(model); buildCache(); delete oldModel; } void RecentContactsModel::buildCache() { qDeleteAll(m_idToData.values()); m_idToData.clear(); m_dataToRow.clear(); QString id; for(int i = 0; i < sourceModel()->rowCount(); ++i) { id = sourceModel()->data(sourceModel()->index(i, 0), ResultModel::ResourceRole).toString(); if (!m_idToData.contains(id)) { insertPersonData(id, i); } } } void RecentContactsModel::insertPersonData(const QString& id, int row) { KPeople::PersonData *data = new KPeople::PersonData(id); m_idToData[id] = data; m_dataToRow[data] = row; connect(data, SIGNAL(dataChanged()), this, SLOT(personDataChanged())); } void RecentContactsModel::personDataChanged() { KPeople::PersonData *data = static_cast(sender()); if (m_dataToRow.contains(data)) { int row = m_dataToRow[data]; QModelIndex idx = sourceModel()->index(row, 0); emit dataChanged(idx, idx); } } diff --git a/recentusagemodel.cpp b/recentusagemodel.cpp index d6fe840cd..85fd607b3 100644 --- a/recentusagemodel.cpp +++ b/recentusagemodel.cpp @@ -1,426 +1,426 @@ /*************************************************************************** * Copyright (C) 2014-2015 by Eike Hein * * * * 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) 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 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 "recentusagemodel.h" #include "actionlist.h" #include "appsmodel.h" #include "appentry.h" #include "favoritesmodel.h" #include #include #if HAVE_X11 #include #endif #include #include #include #include #include #include -#include -#include -#include +#include +#include +#include -namespace KAStats = KActivities::Experimental::Stats; +namespace KAStats = KActivities::Stats; using namespace KAStats; using namespace KAStats::Terms; GroupSortProxy::GroupSortProxy(QAbstractItemModel *sourceModel) : QSortFilterProxyModel(nullptr) { sourceModel->setParent(this); setSourceModel(sourceModel); sort(0); } GroupSortProxy::~GroupSortProxy() { } InvalidAppsFilterProxy::InvalidAppsFilterProxy(AbstractModel *parentModel, QAbstractItemModel *sourceModel) : QSortFilterProxyModel(nullptr) , m_parentModel(parentModel) { connect(parentModel, &AbstractModel::favoritesModelChanged, this, &InvalidAppsFilterProxy::connectNewFavoritesModel); connectNewFavoritesModel(); sourceModel->setParent(this); setSourceModel(sourceModel); } InvalidAppsFilterProxy::~InvalidAppsFilterProxy() { } void InvalidAppsFilterProxy::connectNewFavoritesModel() { FavoritesModel* favoritesModel = static_cast(m_parentModel->favoritesModel()); connect(favoritesModel, &FavoritesModel::favoritesChanged, this, &QSortFilterProxyModel::invalidate); invalidate(); } bool InvalidAppsFilterProxy::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { Q_UNUSED(source_parent); const QString resource = sourceModel()->index(source_row, 0).data(ResultModel::ResourceRole).toString(); if (resource.startsWith(QLatin1String("applications:"))) { KService::Ptr service = KService::serviceByStorageId(resource.section(':', 1)); FavoritesModel* favoritesModel = m_parentModel ? static_cast(m_parentModel->favoritesModel()) : nullptr; return (service && (!favoritesModel || !favoritesModel->isFavorite(service->storageId()))); } return true; } bool GroupSortProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const { const QString &lResource = sourceModel()->data(left, ResultModel::ResourceRole).toString(); const QString &rResource = sourceModel()->data(right, ResultModel::ResourceRole).toString(); if (lResource.startsWith(QLatin1String("applications:")) && !rResource.startsWith(QLatin1String("applications:"))) { return true; } else if (!lResource.startsWith(QLatin1String("applications:")) && rResource.startsWith(QLatin1String("applications:"))) { return false; } return (left.row() < right.row()); } RecentUsageModel::RecentUsageModel(QObject *parent, IncludeUsage usage) : ForwardingModel(parent) , m_usage(usage) { refresh(); } RecentUsageModel::~RecentUsageModel() { } RecentUsageModel::IncludeUsage RecentUsageModel::usage() const { return m_usage; } QString RecentUsageModel::description() const { switch (m_usage) { case AppsAndDocs: return i18n("Recently Used"); case OnlyApps: return i18n("Applications"); case OnlyDocs: default: return i18n("Documents"); } } QString RecentUsageModel::resourceAt(int row) const { QSortFilterProxyModel *sourceProxy = qobject_cast(sourceModel()); if (sourceProxy) { return sourceProxy->sourceModel()->data(sourceProxy->mapToSource(sourceProxy->index(row, 0)), ResultModel::ResourceRole).toString(); } return sourceModel()->data(index(row, 0), ResultModel::ResourceRole).toString(); } QVariant RecentUsageModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } const QString &resource = resourceAt(index.row()); if (resource.startsWith(QLatin1String("applications:"))) { return appData(resource, role); } else { return docData(resource, role); } } QVariant RecentUsageModel::appData(const QString &resource, int role) const { const QString storageId = resource.section(':', 1); KService::Ptr service = KService::serviceByStorageId(storageId); if (!service || !service->isApplication()) { return QVariant(); } if (role == Qt::DisplayRole) { AppsModel *parentModel = qobject_cast(QObject::parent()); if (parentModel) { return AppEntry::nameFromService(service, (AppEntry::NameFormat)qobject_cast(QObject::parent())->appNameFormat()); } else { return AppEntry::nameFromService(service, AppEntry::NameOnly); } } else if (role == Qt::DecorationRole) { return QIcon::fromTheme(service->icon(), QIcon::fromTheme("unknown")); } else if (role == Kicker::DescriptionRole) { return service->comment(); } else if (role == Kicker::GroupRole) { return i18n("Applications"); } else if (role == Kicker::FavoriteIdRole) { return service->storageId(); } else if (role == Kicker::HasActionListRole) { return true; } else if (role == Kicker::ActionListRole) { QVariantList actionList; const QVariantList &jumpList = Kicker::jumpListActions(service); if (jumpList.count()) { actionList << jumpList << Kicker::createSeparatorActionItem(); } const QVariantList &recentDocuments = Kicker::recentDocumentActions(service); if (recentDocuments.count()) { actionList << recentDocuments << Kicker::createSeparatorActionItem(); } const QVariantMap &forgetAction = Kicker::createActionItem(i18n("Forget Application"), "forget"); actionList << forgetAction; const QVariantMap &forgetAllAction = Kicker::createActionItem(forgetAllActionName(), "forgetAll"); actionList << forgetAllAction; return actionList; } return QVariant(); } QVariant RecentUsageModel::docData(const QString &resource, int role) const { QUrl url(resource); if (url.scheme().isEmpty()) { url.setScheme(QStringLiteral("file")); } const KFileItem fileItem(url); if (!url.isValid() || !(fileItem.isFile() || fileItem.isDir())) { return QVariant(); } if (role == Qt::DisplayRole) { return fileItem.text(); } else if (role == Qt::DecorationRole) { return QIcon::fromTheme(fileItem.iconName(), QIcon::fromTheme("unknown")); } else if (role == Kicker::GroupRole) { return i18n("Documents"); } else if (role == Kicker::FavoriteIdRole || role == Kicker::UrlRole) { return url.toString(); } else if (role == Kicker::UrlRole) { return url; } else if (role == Kicker::HasActionListRole) { return true; } else if (role == Kicker::ActionListRole) { QVariantList actionList = Kicker::createActionListForFileItem(fileItem); actionList << Kicker::createSeparatorActionItem(); const QVariantMap &forgetAction = Kicker::createActionItem(i18n("Forget Document"), "forget"); actionList << forgetAction; const QVariantMap &forgetAllAction = Kicker::createActionItem(forgetAllActionName(), "forgetAll"); actionList << forgetAllAction; return actionList; } return QVariant(); } bool RecentUsageModel::trigger(int row, const QString &actionId, const QVariant &argument) { Q_UNUSED(argument) bool withinBounds = row >= 0 && row < rowCount(); if (actionId.isEmpty() && withinBounds) { const QString &resource = resourceAt(row); if (!resource.startsWith(QLatin1String("applications:"))) { new KRun(QUrl(resource), 0); return true; } const QString storageId = resource.section(':', 1); KService::Ptr service = KService::serviceByStorageId(storageId); if (!service) { return false; } quint32 timeStamp = 0; #if HAVE_X11 if (QX11Info::isPlatformX11()) { timeStamp = QX11Info::appUserTime(); } #endif new KRun(QUrl::fromLocalFile(service->entryPath()), 0, true, KStartupInfo::createNewStartupIdForTimestamp(timeStamp)); KActivities::ResourceInstance::notifyAccessed(QUrl("applications:" + storageId), "org.kde.plasma.kicker"); return true; } else if (actionId == "forget" && withinBounds) { if (m_activitiesModel) { QModelIndex idx = sourceModel()->index(row, 0); QSortFilterProxyModel *sourceProxy = qobject_cast(sourceModel()); while (sourceProxy) { idx = sourceProxy->mapToSource(idx); sourceProxy = qobject_cast(sourceProxy->sourceModel()); } static_cast(m_activitiesModel.data())->forgetResource(idx.row()); } return false; } else if (actionId == "forgetAll") { if (m_activitiesModel) { static_cast(m_activitiesModel.data())->forgetAllResources(); } return false; } else if (withinBounds) { const QString &resource = resourceAt(row); if (resource.startsWith(QLatin1String("applications:"))) { const QString storageId = sourceModel()->data(sourceModel()->index(row, 0), ResultModel::ResourceRole).toString().section(':', 1); KService::Ptr service = KService::serviceByStorageId(storageId); if (service) { return Kicker::handleRecentDocumentAction(service, actionId, argument); } } else { bool close = false; QUrl url(sourceModel()->data(sourceModel()->index(row, 0), ResultModel::ResourceRole).toString()); KFileItem item(url); if (Kicker::handleFileItemAction(item, actionId, argument, &close)) { return close; } } } return false; } bool RecentUsageModel::hasActions() const { return rowCount(); } QVariantList RecentUsageModel::actions() const { QVariantList actionList; if (rowCount()) { actionList << Kicker::createActionItem(forgetAllActionName(), "forgetAll"); } return actionList; } QString RecentUsageModel::forgetAllActionName() const { switch (m_usage) { case AppsAndDocs: return i18n("Forget All"); case OnlyApps: return i18n("Forget All Applications"); case OnlyDocs: default: return i18n("Forget All Documents"); } } void RecentUsageModel::refresh() { QAbstractItemModel *oldModel = sourceModel(); auto query = UsedResources | RecentlyUsedFirst | Agent::any() | Type::any() | Activity::current(); switch (m_usage) { case AppsAndDocs: { query = query | Url::startsWith("applications:") | Url::file() | Limit(30); break; } case OnlyApps: { query = query | Url::startsWith("applications:") | Limit(15); break; } case OnlyDocs: default: { query = query | Url::file() | Limit(15); } } m_activitiesModel = new ResultModel(query); QAbstractItemModel *model = m_activitiesModel; QModelIndex index; if (model->canFetchMore(index)) { model->fetchMore(index); } if (m_usage != OnlyDocs) { model = new InvalidAppsFilterProxy(this, model); } if (m_usage == AppsAndDocs) { model = new GroupSortProxy(model); } setSourceModel(model); delete oldModel; }