2 Copyright 2007 Robert Knight <robertknight@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "core/recentapplications.h"
25 #include <QLinkedList>
28 #include <KConfigGroup>
33 #include "core/models.h"
35 using namespace Kickoff
;
37 class RecentApplications::Private
43 : maxServices(DEFAULT_MAX_SERVICES
)
45 KConfigGroup recentGroup
= componentData().config()->group("RecentlyUsed");
46 QList
<QString
> recentApplications
= recentGroup
.readEntry("Applications",QList
<QString
>());
47 maxServices
= recentGroup
.readEntry("MaxApplications",maxServices
);
50 // the actual last date/time is not currently recorded, instead we just use
51 // the current date/time and adjust it by one second after each item is added
52 // to preserve the order of the applications in the list loaded from the KConfig
54 QDateTime dateTime
= QDateTime::currentDateTime();
55 foreach(const QString
& application
,recentApplications
) {
57 info
.storageId
= application
;
59 info
.lastStartedTime
= dateTime
;
60 addEntry(info
.storageId
,info
);
61 dateTime
= dateTime
.addSecs(1);
66 KConfigGroup recentGroup
= componentData().config()->group("RecentlyUsed");
68 QList
<ServiceInfo
> services
= serviceInfo
.values();
69 qSort(services
.begin(),services
.end());
72 // only the desktop file used is currently recorded, information such
73 // as start count and date/time of last used is lost
74 QList
<QString
> recentApplications
;
75 foreach(const ServiceInfo
& info
,services
) {
76 recentApplications
<< info
.storageId
;
79 recentGroup
.writeEntry("Applications",recentApplications
);
81 if (maxServices
!= DEFAULT_MAX_SERVICES
) {
82 recentGroup
.writeEntry("MaxApplications",maxServices
);
85 void addEntry(const QString
& id
,ServiceInfo
& info
)
87 // if this service is already in the list then remove the existing
88 // queue entry (so that there are no duplicates in the queue)
89 if (serviceInfo
.contains(id
)) {
90 kDebug() << "Duplicate entry added. Removing existing entry from queue.";
91 serviceQueue
.erase(serviceInfo
[id
].queueIter
);
94 serviceQueue
.append(id
);
95 info
.queueIter
= --serviceQueue
.end();
96 serviceInfo
.insert(id
,info
);
98 // if more than the maximum number of services have been added
99 // remove the least recently used service
100 if (serviceQueue
.count() > maxServices
) {
101 QString removeId
= serviceQueue
.takeFirst();
102 kDebug() << "More than max services added. Removing" << removeId
<< "from queue.";
103 serviceInfo
.remove(removeId
);
104 emit instance
.applicationRemoved(KService::serviceByStorageId(removeId
));
111 ServiceInfo() : startCount(0){}
115 QDateTime lastStartedTime
;
116 QLinkedList
<QString
>::iterator queueIter
;
118 bool operator<(const ServiceInfo
& rhs
) const
120 return this->lastStartedTime
< rhs
.lastStartedTime
;
124 static const int DEFAULT_MAX_SERVICES
= 5;
126 // queue to keep track of the order in which services have been used
127 // (most recently used at the back)
128 QLinkedList
<QString
> serviceQueue
;
129 QHash
<QString
,ServiceInfo
> serviceInfo
;
130 RecentApplications instance
;
132 K_GLOBAL_STATIC(RecentApplications::Private
,privateSelf
)
134 RecentApplications
*RecentApplications::self()
136 return &privateSelf
->instance
;
139 RecentApplications::RecentApplications()
142 QList
<KService::Ptr
> RecentApplications::recentApplications() const
144 QList
<Private::ServiceInfo
> services
= privateSelf
->serviceInfo
.values();
145 qSort(services
.begin(),services
.end(),qGreater
<Private::ServiceInfo
>());
147 QList
<KService::Ptr
> servicePtrs
;
148 foreach (const Private::ServiceInfo
& info
,services
) {
149 KService::Ptr s
= KService::serviceByStorageId(info
.storageId
);
157 int RecentApplications::startCount(KService::Ptr service
) const
159 return privateSelf
->serviceInfo
[service
->storageId()].startCount
;
161 QDateTime
RecentApplications::lastStartedTime(KService::Ptr service
) const
163 return privateSelf
->serviceInfo
[service
->storageId()].lastStartedTime
;
165 void RecentApplications::setMaximum(int maximum
)
167 Q_ASSERT(maximum
> 0);
168 privateSelf
->maxServices
= maximum
;
170 int RecentApplications::maximum() const
172 return privateSelf
->maxServices
;
174 void RecentApplications::add(KService::Ptr service
)
176 Private::ServiceInfo info
= privateSelf
->serviceInfo
.value(service
->storageId());
177 info
.storageId
= service
->storageId();
179 info
.lastStartedTime
= QDateTime::currentDateTime();
181 privateSelf
->addEntry(info
.storageId
,info
);
183 kDebug() << "Recent app added" << info
.storageId
<< info
.startCount
;
185 emit
applicationAdded(service
,info
.startCount
);
187 void RecentApplications::clear()
189 privateSelf
->serviceInfo
.clear();
193 #include "recentapplications.moc"