• Skip to content
  • Skip to link menu
KDE 4.1 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 <QHash>
00025 #include <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()
00043         : maxServices(DEFAULT_MAX_SERVICES)
00044     {
00045         KConfigGroup recentGroup = componentData().config()->group("RecentlyUsed");
00046         QList<QString> recentApplications = recentGroup.readEntry("Applications",QList<QString>());
00047         maxServices = recentGroup.readEntry("MaxApplications",maxServices);
00048 
00049         // TESTING
00050         //      the actual last date/time is not currently recorded, instead we just use
00051         //      the current date/time and adjust it by one second after each item is added
00052         //      to preserve the order of the applications in the list loaded from the KConfig
00053         //      source
00054         QDateTime dateTime = QDateTime::currentDateTime();
00055         foreach(const QString& application,recentApplications) {
00056             ServiceInfo info;
00057             info.storageId = application;
00058             info.startCount = 1;
00059             info.lastStartedTime = dateTime;
00060             addEntry(info.storageId,info);
00061             dateTime = dateTime.addSecs(1);
00062         }
00063     };
00064     ~Private()
00065     {
00066         KConfigGroup recentGroup = componentData().config()->group("RecentlyUsed");
00067         
00068         QList<ServiceInfo> services = serviceInfo.values();
00069         qSort(services.begin(),services.end());
00070 
00071         // TESTING
00072         //      only the desktop file used is currently recorded, information such
00073         //      as start count and date/time of last used is lost 
00074         QList<QString> recentApplications;
00075         foreach(const ServiceInfo& info,services) {
00076             recentApplications << info.storageId;
00077         }
00078 
00079         recentGroup.writeEntry("Applications",recentApplications);
00080 
00081         if (maxServices != DEFAULT_MAX_SERVICES) {
00082             recentGroup.writeEntry("MaxApplications",maxServices);
00083         }
00084     }
00085     void addEntry(const QString& id,ServiceInfo& info)
00086     {
00087         // if this service is already in the list then remove the existing
00088         // queue entry (so that there are no duplicates in the queue)
00089         if (serviceInfo.contains(id)) {
00090             kDebug() << "Duplicate entry added.  Removing existing entry from queue.";
00091             serviceQueue.erase(serviceInfo[id].queueIter);
00092         } 
00093 
00094         serviceQueue.append(id);
00095         info.queueIter = --serviceQueue.end();
00096         serviceInfo.insert(id,info);
00097         
00098         // if more than the maximum number of services have been added
00099         // remove the least recently used service
00100         if (serviceQueue.count() > maxServices) {
00101            QString removeId = serviceQueue.takeFirst();
00102            kDebug() << "More than max services added.  Removing" << removeId << "from queue."; 
00103            serviceInfo.remove(removeId);
00104            emit instance.applicationRemoved(KService::serviceByStorageId(removeId));
00105         }
00106     }
00107 
00108     class ServiceInfo
00109     {
00110     public:
00111         ServiceInfo() : startCount(0){}
00112 
00113         QString storageId;
00114         int startCount;
00115         QDateTime lastStartedTime;
00116         QLinkedList<QString>::iterator queueIter;
00117 
00118         bool operator<(const ServiceInfo& rhs) const
00119         {
00120             return this->lastStartedTime < rhs.lastStartedTime;
00121         }
00122     };
00123 
00124     static const int DEFAULT_MAX_SERVICES = 5;
00125     int maxServices;
00126     // queue to keep track of the order in which services have been used
00127     // (most recently used at the back)
00128     QLinkedList<QString> serviceQueue;
00129     QHash<QString,ServiceInfo> serviceInfo;
00130     RecentApplications instance;
00131 };
00132 K_GLOBAL_STATIC(RecentApplications::Private,privateSelf)
00133 
00134 RecentApplications *RecentApplications::self() 
00135 {
00136     return &privateSelf->instance;
00137 }
00138 
00139 RecentApplications::RecentApplications()
00140 {
00141 }
00142 QList<KService::Ptr> RecentApplications::recentApplications() const
00143 {
00144     QList<Private::ServiceInfo> services = privateSelf->serviceInfo.values();
00145     qSort(services.begin(),services.end(),qGreater<Private::ServiceInfo>());
00146 
00147     QList<KService::Ptr> servicePtrs;
00148     foreach (const Private::ServiceInfo& info,services) {
00149         KService::Ptr s = KService::serviceByStorageId(info.storageId);
00150 
00151         if (s) {
00152             servicePtrs << s;
00153         }
00154     }
00155     return servicePtrs;
00156 }
00157 int RecentApplications::startCount(KService::Ptr service) const
00158 {
00159     return privateSelf->serviceInfo[service->storageId()].startCount;
00160 }
00161 QDateTime RecentApplications::lastStartedTime(KService::Ptr service) const
00162 {
00163     return privateSelf->serviceInfo[service->storageId()].lastStartedTime;
00164 }
00165 void RecentApplications::setMaximum(int maximum)
00166 {
00167     Q_ASSERT(maximum > 0);
00168     privateSelf->maxServices = maximum;
00169 }
00170 int RecentApplications::maximum() const
00171 {
00172     return privateSelf->maxServices;
00173 }
00174 void RecentApplications::add(KService::Ptr service)
00175 {
00176     Private::ServiceInfo info = privateSelf->serviceInfo.value(service->storageId());
00177     info.storageId = service->storageId();
00178     info.startCount++;
00179     info.lastStartedTime = QDateTime::currentDateTime();
00180 
00181     privateSelf->addEntry(info.storageId,info);
00182 
00183     kDebug() << "Recent app added" << info.storageId << info.startCount;
00184 
00185     emit applicationAdded(service,info.startCount);
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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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