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/recentlyusedmodel.h"
27 #include <KDesktopFile>
30 #include <KLocalizedString>
31 #include <KRecentDocument>
36 #include "core/models.h"
37 #include "core/recentapplications.h"
39 using namespace Kickoff
;
41 class RecentlyUsedModel::Private
44 Private(RecentlyUsedModel
*parent
)
46 , recentDocumentItem(0)
49 void removeExistingItem(const QString
& path
)
51 if (!itemsByPath
.contains(path
)) {
55 QStandardItem
*existingItem
= itemsByPath
[path
];
56 //kDebug() << "Removing existing item" << existingItem;
57 Q_ASSERT(existingItem
->parent());
58 existingItem
->parent()->removeRow(existingItem
->row());
59 itemsByPath
.remove(path
);
61 void addRecentApplication(KService::Ptr service
,bool append
)
63 // remove existing item if any
64 removeExistingItem(service
->entryPath());
66 QStandardItem
*appItem
= StandardItemFactory::createItemForService(service
);
67 itemsByPath
.insert(service
->entryPath(),appItem
);
70 recentAppItem
->appendRow(appItem
);
72 recentAppItem
->insertRow(0,appItem
);
75 void addRecentDocument(const QString
& desktopPath
,bool append
)
77 // remove existing item if any
78 KDesktopFile
desktopFile(desktopPath
);
79 KUrl documentUrl
= desktopFile
.readUrl();
81 removeExistingItem(documentUrl
.url());
83 QStandardItem
*documentItem
= StandardItemFactory::createItemForUrl(desktopPath
);
84 documentItem
->setData(true, Kickoff::SubTitleMandatoryRole
);
85 itemsByPath
.insert(desktopPath
,documentItem
);
87 //kDebug() << "Document item" << documentItem << "text" << documentItem->text() << "url" << documentUrl.url();
89 recentDocumentItem
->appendRow(documentItem
);
91 recentDocumentItem
->insertRow(0,documentItem
);
94 void loadRecentDocuments()
96 // create branch for documents and add existing items
97 recentDocumentItem
= new QStandardItem(i18n("Documents"));
98 QStringList documents
= KRecentDocument::recentDocuments();
99 foreach (const QString
& document
,documents
) {
100 addRecentDocument(document
,true);
102 q
->appendRow(recentDocumentItem
);
104 void loadRecentApplications()
106 recentAppItem
= new QStandardItem(i18n("Applications"));
107 QList
<KService::Ptr
> services
= RecentApplications::self()->recentApplications();
108 foreach (const KService::Ptr
& service
, services
) {
109 addRecentApplication(service
,true);
111 q
->appendRow(recentAppItem
);
114 RecentlyUsedModel
* const q
;
115 QStandardItem
*recentDocumentItem
;
116 QStandardItem
*recentAppItem
;
118 QHash
<QString
, QStandardItem
*> itemsByPath
;
121 RecentlyUsedModel::RecentlyUsedModel(QObject
*parent
)
122 : KickoffModel(parent
)
123 , d(new Private(this))
125 d
->loadRecentApplications();
126 d
->loadRecentDocuments();
128 // listen for changes to the list of recent documents
129 KDirWatch
*recentDocWatch
= new KDirWatch(this);
130 recentDocWatch
->addDir(KRecentDocument::recentDocumentDirectory(),KDirWatch::WatchFiles
);
131 connect(recentDocWatch
,SIGNAL(created(QString
)),this,SLOT(recentDocumentAdded(QString
)));
132 connect(recentDocWatch
,SIGNAL(deleted(QString
)),this,SLOT(recentDocumentRemoved(QString
)));
134 // listen for changes to the list of recent applications
135 connect(RecentApplications::self(),SIGNAL(applicationAdded(KService::Ptr
,int)),
136 this,SLOT(recentApplicationAdded(KService::Ptr
,int)));
137 connect(RecentApplications::self(),SIGNAL(applicationRemoved(KService::Ptr
)),
138 this,SLOT(recentApplicationRemoved(KService::Ptr
)));
139 connect(RecentApplications::self(),SIGNAL(cleared()),
140 this,SLOT(recentApplicationsCleared()));
142 RecentlyUsedModel::~RecentlyUsedModel()
146 void RecentlyUsedModel::recentDocumentAdded(const QString
& path
)
148 kDebug() << "Recent document added" << path
;
149 d
->addRecentDocument(path
,false);
151 void RecentlyUsedModel::recentDocumentRemoved(const QString
& path
)
153 kDebug() << "Recent document removed" << path
;
154 d
->removeExistingItem(path
);
157 void RecentlyUsedModel::recentApplicationAdded(KService::Ptr service
,int)
160 d
->addRecentApplication(service
,false);
164 void RecentlyUsedModel::recentApplicationRemoved(KService::Ptr service
)
167 d
->removeExistingItem(service
->entryPath());
171 void RecentlyUsedModel::recentApplicationsCleared()
173 QSet
<QStandardItem
*> appItems
;
174 const int rows
= d
->recentAppItem
->rowCount();
175 for(int i
=0;i
<rows
;i
++) {
176 appItems
<< d
->recentAppItem
->child(i
);
178 QMutableHashIterator
<QString
,QStandardItem
*> iter(d
->itemsByPath
);
179 while (iter
.hasNext()) {
181 if (appItems
.contains(iter
.value())) {
186 d
->recentAppItem
->removeRows(0,d
->recentAppItem
->rowCount());
188 void RecentlyUsedModel::clearRecentApplications()
190 RecentApplications::self()->clear();
192 void RecentlyUsedModel::clearRecentDocuments()
194 KRecentDocument::clear();
197 #include "recentlyusedmodel.moc"