Applets
recentlyusedmodel.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/recentlyusedmodel.h"
00022
00023
00024 #include <QFileInfo>
00025
00026
00027 #include <KDesktopFile>
00028 #include <KDirWatch>
00029 #include <KIcon>
00030 #include <KLocalizedString>
00031 #include <KRecentDocument>
00032 #include <KUrl>
00033 #include <KDebug>
00034
00035
00036 #include "core/models.h"
00037 #include "core/recentapplications.h"
00038 #include "recentadaptor.h"
00039
00040 using namespace Kickoff;
00041
00042 class RecentlyUsedModel::Private
00043 {
00044 public:
00045 Private(RecentlyUsedModel *parent)
00046 : q(parent)
00047 , recentDocumentItem(0)
00048 {
00049 }
00050 void removeExistingItem(const QString& path)
00051 {
00052 if (!itemsByPath.contains(path)) {
00053 return;
00054 }
00055
00056 QStandardItem *existingItem = itemsByPath[path];
00057
00058 Q_ASSERT(existingItem->parent());
00059 existingItem->parent()->removeRow(existingItem->row());
00060 itemsByPath.remove(path);
00061 }
00062 void addRecentApplication(KService::Ptr service,bool append)
00063 {
00064
00065 removeExistingItem(service->entryPath());
00066
00067 QStandardItem *appItem = StandardItemFactory::createItemForService(service);
00068 itemsByPath.insert(service->entryPath(),appItem);
00069
00070 if (append) {
00071 recentAppItem->appendRow(appItem);
00072 } else {
00073 recentAppItem->insertRow(0,appItem);
00074 }
00075 }
00076 void addRecentDocument(const QString& desktopPath,bool append)
00077 {
00078
00079 KDesktopFile desktopFile(desktopPath);
00080 KUrl documentUrl = desktopFile.readUrl();
00081
00082 removeExistingItem(documentUrl.url());
00083
00084 QStandardItem *documentItem = StandardItemFactory::createItemForUrl(desktopPath);
00085 documentItem->setData(true, Kickoff::SubTitleMandatoryRole);
00086 itemsByPath.insert(desktopPath,documentItem);
00087
00088
00089 if (append) {
00090 recentDocumentItem->appendRow(documentItem);
00091 } else {
00092 recentDocumentItem->insertRow(0,documentItem);
00093 }
00094 }
00095 void loadRecentDocuments()
00096 {
00097
00098 recentDocumentItem = new QStandardItem(i18n("Documents"));
00099 QStringList documents = KRecentDocument::recentDocuments();
00100 foreach (const QString& document,documents) {
00101 addRecentDocument(document,true);
00102 }
00103 q->appendRow(recentDocumentItem);
00104 }
00105 void loadRecentApplications()
00106 {
00107 recentAppItem = new QStandardItem(i18n("Applications"));
00108 QList<KService::Ptr> services = RecentApplications::self()->recentApplications();
00109 foreach (const KService::Ptr& service, services) {
00110 addRecentApplication(service,true);
00111 }
00112 q->appendRow(recentAppItem);
00113 }
00114
00115 RecentlyUsedModel * const q;
00116 QStandardItem *recentDocumentItem;
00117 QStandardItem *recentAppItem;
00118
00119 QHash<QString, QStandardItem*> itemsByPath;
00120 };
00121
00122 RecentlyUsedModel::RecentlyUsedModel(QObject *parent)
00123 : KickoffModel(parent)
00124 , d(new Private(this))
00125 {
00126 QDBusConnection dbus = QDBusConnection::sessionBus();
00127 (void)new RecentAdaptor(this);
00128 QDBusConnection::sessionBus().registerObject("/kickoff/RecentAppDoc", this);
00129 dbus.connect(QString(), "/kickoff/RecentAppDoc", "org.kde.plasma", "cleanRecentDocumentsAndDocuments", this, SLOT(clearRecentDocumentsAndApplications()));
00130
00131 d->loadRecentApplications();
00132 d->loadRecentDocuments();
00133
00134
00135 KDirWatch *recentDocWatch = new KDirWatch(this);
00136 recentDocWatch->addDir(KRecentDocument::recentDocumentDirectory(),KDirWatch::WatchFiles);
00137 connect(recentDocWatch,SIGNAL(created(QString)),this,SLOT(recentDocumentAdded(QString)));
00138 connect(recentDocWatch,SIGNAL(deleted(QString)),this,SLOT(recentDocumentRemoved(QString)));
00139
00140
00141 connect(RecentApplications::self(),SIGNAL(applicationAdded(KService::Ptr,int)),
00142 this,SLOT(recentApplicationAdded(KService::Ptr,int)));
00143 connect(RecentApplications::self(),SIGNAL(applicationRemoved(KService::Ptr)),
00144 this,SLOT(recentApplicationRemoved(KService::Ptr)));
00145 connect(RecentApplications::self(),SIGNAL(cleared()),
00146 this,SLOT(recentApplicationsCleared()));
00147 }
00148 RecentlyUsedModel::~RecentlyUsedModel()
00149 {
00150 delete d;
00151 }
00152 void RecentlyUsedModel::recentDocumentAdded(const QString& path)
00153 {
00154 kDebug() << "Recent document added" << path;
00155 d->addRecentDocument(path,false);
00156 }
00157 void RecentlyUsedModel::recentDocumentRemoved(const QString& path)
00158 {
00159 kDebug() << "Recent document removed" << path;
00160 d->removeExistingItem(path);
00161 }
00162
00163 void RecentlyUsedModel::recentApplicationAdded(KService::Ptr service,int)
00164 {
00165 if (service) {
00166 d->addRecentApplication(service,false);
00167 }
00168 }
00169
00170 void RecentlyUsedModel::recentApplicationRemoved(KService::Ptr service)
00171 {
00172 if (service) {
00173 d->removeExistingItem(service->entryPath());
00174 }
00175 }
00176
00177 void RecentlyUsedModel::recentApplicationsCleared()
00178 {
00179 QSet<QStandardItem*> appItems;
00180 const int rows = d->recentAppItem->rowCount();
00181 for(int i=0;i<rows;i++) {
00182 appItems << d->recentAppItem->child(i);
00183 }
00184 QMutableHashIterator<QString,QStandardItem*> iter(d->itemsByPath);
00185 while (iter.hasNext()) {
00186 iter.next();
00187 if (appItems.contains(iter.value())) {
00188 iter.remove();
00189 }
00190 }
00191
00192 d->recentAppItem->removeRows(0,d->recentAppItem->rowCount());
00193 }
00194 void RecentlyUsedModel::clearRecentApplications()
00195 {
00196 RecentApplications::self()->clear();
00197 }
00198 void RecentlyUsedModel::clearRecentDocuments()
00199 {
00200 KRecentDocument::clear();
00201 }
00202
00203 void RecentlyUsedModel::clearRecentDocumentsAndApplications()
00204 {
00205 clearRecentDocuments();
00206 clearRecentApplications();
00207 }
00208
00209
00210 #include "recentlyusedmodel.moc"
00211