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 <QHash>
00025 #include <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()
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
00050
00051
00052
00053
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
00072
00073
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
00088
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
00099
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
00127
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"