Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / plasma / applets / kickoff / core / favoritesmodel.cpp
blob5d65e7dfe79c6bfa3db0bf084856c5e07316e9bc
1 /*
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.
20 //Own
21 #include "core/favoritesmodel.h"
23 // Qt
24 #include <QHash>
25 #include <QList>
27 // KDE
28 #include <KConfigGroup>
29 #include <KService>
30 #include <kdebug.h>
32 // Local
33 #include "core/models.h"
35 using namespace Kickoff;
37 class FavoritesModel::Private
39 public:
40 Private(FavoritesModel *parent)
41 :q(parent)
43 headerItem = new QStandardItem(i18n("Favorites"));
44 q->appendRow(headerItem);
47 void addFavoriteItem(const QString& url)
49 QStandardItem *item = StandardItemFactory::createItemForUrl(url);
50 headerItem->appendRow(item);
52 void removeFavoriteItem(const QString& url)
54 QModelIndexList matches = q->match(q->index(0,0),UrlRole,
55 url,-1,
56 Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap | Qt::MatchRecursive));
58 kDebug() << "Removing item matches" << matches;
60 foreach(const QModelIndex& index,matches) {
61 QStandardItem *item = q->itemFromIndex(index);
62 if (item->parent()) {
63 item->parent()->removeRow(item->row());
64 } else {
65 qDeleteAll(q->takeRow(item->row()));
70 FavoritesModel * const q;
71 QStandardItem *headerItem;
73 static void loadFavorites()
75 KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
76 QList<QString> favoriteList = favoritesGroup.readEntry("FavoriteURLs",QList<QString>());
77 if (favoriteList.isEmpty()) {
78 favoriteList = defaultFavorites();
81 foreach(QString favorite,favoriteList) {
82 FavoritesModel::add(favorite);
85 static QList<QString> defaultFavorites()
87 QList<QString> applications;
88 applications << "konqbrowser" << "kmail" << "systemsettings" << "dolphin";
90 QList<QString> desktopFiles;
92 foreach(const QString& application,applications) {
93 KService::Ptr service = KService::serviceByStorageId("kde4-" + application + ".desktop");
94 if (service) {
95 desktopFiles << service->entryPath();
99 return desktopFiles;
101 static void saveFavorites()
103 KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
104 favoritesGroup.writeEntry("FavoriteURLs",globalFavoriteList);
105 favoritesGroup.config()->sync();
107 static QList<QString> globalFavoriteList;
108 static QSet<QString> globalFavoriteSet;
109 static QSet<FavoritesModel*> models;
112 QList<QString> FavoritesModel::Private::globalFavoriteList;
113 QSet<QString> FavoritesModel::Private::globalFavoriteSet;
114 QSet<FavoritesModel*> FavoritesModel::Private::models;
116 FavoritesModel::FavoritesModel(QObject *parent)
117 : KickoffModel(parent)
118 , d(new Private(this))
120 Private::models << this;
121 if (Private::models.count() == 1 && Private::globalFavoriteList.isEmpty()) {
122 Private::loadFavorites();
123 } else {
124 foreach (QString url, Private::globalFavoriteList) {
125 d->addFavoriteItem(url);
129 FavoritesModel::~FavoritesModel()
131 Private::models.remove(this);
133 if (Private::models.isEmpty()) {
134 Private::saveFavorites();
137 delete d;
139 void FavoritesModel::add(const QString& url)
141 Private::globalFavoriteList << url;
142 Private::globalFavoriteSet << url;
144 foreach(FavoritesModel* model,Private::models) {
145 model->d->addFavoriteItem(url);
148 // save after each add in case we crash
149 Private::saveFavorites();
151 void FavoritesModel::remove(const QString& url)
153 Private::globalFavoriteList.removeAll(url);
154 Private::globalFavoriteSet.remove(url);
156 foreach(FavoritesModel* model,Private::models) {
157 model->d->removeFavoriteItem(url);
160 // save after each remove in case of crash or other mishaps
161 Private::saveFavorites();
163 bool FavoritesModel::isFavorite(const QString& url)
165 return Private::globalFavoriteSet.contains(url);
168 #include "favoritesmodel.moc"