• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

recentapplications.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 // Own
00021 #include "core/recentapplications.h"
00022 
00023 // Qt
00024 #include <QtCore/QHash>
00025 #include <QtCore/QLinkedList>
00026 
00027 // KDE
00028 #include <KConfigGroup>
00029 #include <KGlobal>
00030 #include <KDebug>
00031 
00032 // Local
00033 #include "core/models.h"
00034 
00035 using namespace Kickoff;
00036 
00037 class RecentApplications::Private
00038 {
00039 public:
00040     class ServiceInfo;
00041 
00042     Private() : defaultMaxServices(DEFAULT_MAX_SERVICES) {
00043         KConfigGroup recentGroup = componentData().config()->group("RecentlyUsed");
00044         QList<QString> recentApplications = recentGroup.readEntry("Applications", QList<QString>());
00045         defaultMaxServices = maxServices = qMax(0, recentGroup.readEntry("MaxApplications", defaultMaxServices));
00046 
00047         // TESTING
00048         //      the actual last date/time is not currently recorded, instead we just use
00049         //      the current date/time and adjust it by one second after each item is added
00050         //      to preserve the order of the applications in the list loaded from the KConfig
00051         //      source
00052         QDateTime dateTime = QDateTime::currentDateTime();
00053         foreach(const QString& application, recentApplications) {
00054             ServiceInfo info;
00055             info.storageId = application;
00056             info.startCount = 1;
00057             info.lastStartedTime = dateTime;
00058             addEntry(info.storageId, info);
00059             dateTime = dateTime.addSecs(1);
00060         }
00061     };
00062     ~Private() {
00063         KConfigGroup recentGroup = componentData().config()->group("RecentlyUsed");
00064 
00065         QList<ServiceInfo> services = serviceInfo.values();
00066         qSort(services.begin(), services.end());
00067 
00068         // TESTING
00069         //      only the desktop file used is currently recorded, information such
00070         //      as start count and date/time of last used is lost
00071         QList<QString> recentApplications;
00072         foreach(const ServiceInfo& info, services) {
00073             recentApplications << info.storageId;
00074         }
00075 
00076         recentGroup.writeEntry("Applications", recentApplications);
00077     }
00078     void addEntry(const QString& id, ServiceInfo& info) {
00079         // if this service is already in the list then remove the existing
00080         // queue entry (so that there are no duplicates in the queue)
00081         if (serviceInfo.contains(id)) {
00082             kDebug() << "Duplicate entry added.  Removing existing entry from queue.";
00083             serviceQueue.erase(serviceInfo[id].queueIter);
00084         }
00085 
00086         serviceQueue.append(id);
00087         info.queueIter = --serviceQueue.end();
00088         serviceInfo.insert(id, info);
00089     }
00090 
00091     void removeExpiredEntries() {
00092         // if more than the maximum number of services have been added
00093         // remove the least recently used service
00094         while (serviceQueue.count() > maxServices) {
00095             QString removeId = serviceQueue.takeFirst();
00096             kDebug() << "More than the maximal " << maxServices << " services added.  Removing" << removeId << "from queue.";
00097             serviceInfo.remove(removeId);
00098             emit instance.applicationRemoved(KService::serviceByStorageId(removeId));
00099         }
00100     }
00101 
00102     class ServiceInfo
00103     {
00104     public:
00105         ServiceInfo() : startCount(0) {}
00106 
00107         QString storageId;
00108         int startCount;
00109         QDateTime lastStartedTime;
00110         QLinkedList<QString>::iterator queueIter;
00111 
00112         bool operator<(const ServiceInfo& rhs) const {
00113             return this->lastStartedTime < rhs.lastStartedTime;
00114         }
00115     };
00116 
00117     static const int DEFAULT_MAX_SERVICES = 5;
00118     int defaultMaxServices, maxServices;
00119     // queue to keep track of the order in which services have been used
00120     // (most recently used at the back)
00121     QLinkedList<QString> serviceQueue;
00122     QHash<QString, ServiceInfo> serviceInfo;
00123     RecentApplications instance;
00124 };
00125 K_GLOBAL_STATIC(RecentApplications::Private, privateSelf)
00126 
00127 RecentApplications *RecentApplications::self()
00128 {
00129     return &privateSelf->instance;
00130 }
00131 
00132 RecentApplications::RecentApplications()
00133 {
00134 }
00135 
00136 QList<KService::Ptr> RecentApplications::recentApplications() const
00137 {
00138     QList<Private::ServiceInfo> services = privateSelf->serviceInfo.values();
00139     qSort(services.begin(), services.end(), qGreater<Private::ServiceInfo>());
00140 
00141     QList<KService::Ptr> servicePtrs;
00142     foreach(const Private::ServiceInfo& info, services) {
00143         KService::Ptr s = KService::serviceByStorageId(info.storageId);
00144 
00145         if (s) {
00146             servicePtrs << s;
00147         }
00148     }
00149     return servicePtrs;
00150 }
00151 int RecentApplications::startCount(KService::Ptr service) const
00152 {
00153     return privateSelf->serviceInfo[service->storageId()].startCount;
00154 }
00155 QDateTime RecentApplications::lastStartedTime(KService::Ptr service) const
00156 {
00157     return privateSelf->serviceInfo[service->storageId()].lastStartedTime;
00158 }
00159 void RecentApplications::setMaximum(int maximum)
00160 {
00161     Q_ASSERT(maximum >= 0);
00162     privateSelf->maxServices = maximum;
00163     privateSelf->removeExpiredEntries();
00164 }
00165 int RecentApplications::maximum() const
00166 {
00167     return privateSelf->maxServices;
00168 }
00169 int RecentApplications::defaultMaximum() const
00170 {
00171     return privateSelf->defaultMaxServices;
00172 }
00173 void RecentApplications::add(KService::Ptr service)
00174 {
00175     Private::ServiceInfo info = privateSelf->serviceInfo.value(service->storageId());
00176     info.storageId = service->storageId();
00177     info.startCount++;
00178     info.lastStartedTime = QDateTime::currentDateTime();
00179 
00180     privateSelf->addEntry(info.storageId, info);
00181 
00182     kDebug() << "Recent app added" << info.storageId << info.startCount;
00183     emit applicationAdded(service, info.startCount);
00184 
00185     privateSelf->removeExpiredEntries();
00186 }
00187 void RecentApplications::clear()
00188 {
00189     privateSelf->serviceInfo.clear();
00190     emit cleared();
00191 }
00192 
00193 #include "recentapplications.moc"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal