Applets
recentapplications.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "core/recentapplications.h"
00022
00023
00024 #include <QtCore/QHash>
00025 #include <QtCore/QLinkedList>
00026
00027
00028 #include <KConfigGroup>
00029 #include <KGlobal>
00030 #include <KDebug>
00031
00032
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
00048
00049
00050
00051
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
00069
00070
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
00080
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
00093
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
00120
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"