diff --git a/libtaskmanager/activityinfo.cpp b/libtaskmanager/activityinfo.cpp index c003ca2fa..ebc1c2604 100644 --- a/libtaskmanager/activityinfo.cpp +++ b/libtaskmanager/activityinfo.cpp @@ -1,99 +1,126 @@ /******************************************************************** Copyright 2016 Eike Hein This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #include "activityinfo.h" +#include #include namespace TaskManager { class ActivityInfo::Private { public: Private(ActivityInfo *q); ~Private(); static int instanceCount; static KActivities::Consumer* activityConsumer; + static KActivities::ActivitiesModel* activitiesModel; }; int ActivityInfo::Private::instanceCount = 0; KActivities::Consumer* ActivityInfo::Private::activityConsumer = nullptr; +KActivities::ActivitiesModel* ActivityInfo::Private::activitiesModel = nullptr; + ActivityInfo::Private::Private(ActivityInfo *) { ++instanceCount; } ActivityInfo::Private::~Private() { --instanceCount; if (!instanceCount) { delete activityConsumer; activityConsumer = nullptr; + delete activitiesModel; + activitiesModel = nullptr; } } ActivityInfo::ActivityInfo(QObject *parent) : QObject(parent) , d(new Private(this)) { if (!d->activityConsumer) { d->activityConsumer = new KActivities::Consumer(); } connect(d->activityConsumer, &KActivities::Consumer::currentActivityChanged, this, &ActivityInfo::currentActivityChanged); connect(d->activityConsumer, &KActivities::Consumer::runningActivitiesChanged, this, &ActivityInfo::numberOfRunningActivitiesChanged); + connect(d->activityConsumer, &KActivities::Consumer::runningActivitiesChanged, + this, &ActivityInfo::namesOfRunningActivitiesChanged); + + if (!d->activitiesModel) { + d->activitiesModel = new KActivities::ActivitiesModel(); + d->activitiesModel->setShownStates(QVector{KActivities::Info::Running}); + } + + connect(d->activitiesModel, &KActivities::ActivitiesModel::modelReset, + this, &ActivityInfo::namesOfRunningActivitiesChanged); + + connect(d->activitiesModel, &KActivities::ActivitiesModel::dataChanged, this, + [this](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) { + Q_UNUSED(topLeft) + Q_UNUSED(bottomRight) + + if (roles.isEmpty() || roles.contains(Qt::DisplayRole)) { + emit namesOfRunningActivitiesChanged(); + } + } + ); } ActivityInfo::~ActivityInfo() { } QString ActivityInfo::currentActivity() const { return d->activityConsumer->currentActivity(); } int ActivityInfo::numberOfRunningActivities() const { return d->activityConsumer->activities(KActivities::Info::State::Running).count(); } QStringList ActivityInfo::runningActivities() const { return d->activityConsumer->activities(KActivities::Info::State::Running); } QString ActivityInfo::activityName(const QString &id) { KActivities::Info info(id); if (info.state() != KActivities::Info::Invalid) { return info.name(); } return QString(); } } diff --git a/libtaskmanager/activityinfo.h b/libtaskmanager/activityinfo.h index 33552cb73..a11f1f8e2 100644 --- a/libtaskmanager/activityinfo.h +++ b/libtaskmanager/activityinfo.h @@ -1,96 +1,102 @@ /******************************************************************** Copyright 2016 Eike Hein This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #ifndef ACTIVITYINFO_H #define ACTIVITYINFO_H #include #include "taskmanager_export.h" namespace TaskManager { /** * @short Provides basic activity information. * * This class provides basic information about the activities defined in * the system. * * @NOTE: This is a placeholder, to be moved into KActivities (which it * wraps) or the Task Manager applet backend. * * @see KActivities * * @author Eike Hein **/ class TASKMANAGER_EXPORT ActivityInfo : public QObject { Q_OBJECT Q_PROPERTY(QString currentActivity READ currentActivity NOTIFY currentActivityChanged) Q_PROPERTY(int numberOfRunningActivities READ numberOfRunningActivities NOTIFY numberOfRunningActivitiesChanged) public: explicit ActivityInfo(QObject *parent = 0); virtual ~ActivityInfo(); /** * The currently active virtual desktop. * * @returns the number of the currently active virtual desktop. **/ QString currentActivity() const; /** * The number of currently-running activities defined in the session. * * @returns the number of activities defined in the session. **/ int numberOfRunningActivities() const; /** * The list of currently-running activities defined in the session. * * @returns the list of currently-running activities defined in the session. **/ Q_INVOKABLE QStringList runningActivities() const; /** * The name of the activity of the given id. * * @param id An activity id string. * @returns the name of the activity of the given id. **/ Q_INVOKABLE QString activityName(const QString &id); Q_SIGNALS: void currentActivityChanged() const; void numberOfRunningActivitiesChanged() const; + /** + * The names of the running activities have changed. + * @since 5.40.0 + **/ + void namesOfRunningActivitiesChanged() const; + private: class Private; QScopedPointer d; }; } #endif