add more spacing
[personal-kdebase.git] / workspace / plasma / applets / kickoff / core / favoritesmodel.cpp
blob202e9074d17c16e3b6c20c9416fe0a44abe13a14
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>
26 #include <QMimeData>
27 #include <QFileInfo>
29 // KDE
30 #include <KConfigGroup>
31 #include <KService>
32 #include <kdebug.h>
34 // Local
35 #include "core/models.h"
37 using namespace Kickoff;
39 class FavoritesModel::Private
41 public:
42 Private(FavoritesModel *parent)
43 : q(parent) {
44 headerItem = new QStandardItem(i18n("Favorites"));
45 q->appendRow(headerItem);
48 void addFavoriteItem(const QString& url) {
49 QStandardItem *item = StandardItemFactory::createItemForUrl(url);
50 headerItem->appendRow(item);
52 void moveFavoriteItem(int startRow, int destRow) {
53 if (destRow == startRow)
54 return;
56 QStandardItem *item = headerItem->takeChild(startRow);
58 headerItem->removeRow(startRow);
59 headerItem->insertRow(destRow, item);
61 void removeFavoriteItem(const QString& url) {
62 QModelIndexList matches = q->match(q->index(0, 0), UrlRole,
63 url, -1,
64 Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap | Qt::MatchRecursive));
66 kDebug() << "Removing item matches" << matches;
68 foreach(const QModelIndex& index, matches) {
69 QStandardItem *item = q->itemFromIndex(index);
70 if (item->parent()) {
71 item->parent()->removeRow(item->row());
72 } else {
73 qDeleteAll(q->takeRow(item->row()));
78 FavoritesModel * const q;
79 QStandardItem *headerItem;
81 static void loadFavorites() {
82 KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
83 QList<QString> favoriteList = favoritesGroup.readEntry("FavoriteURLs", QList<QString>());
84 if (favoriteList.isEmpty()) {
85 favoriteList = defaultFavorites();
88 foreach(const QString &favorite, favoriteList) {
89 FavoritesModel::add(favorite);
92 static QList<QString> defaultFavorites() {
93 QList<QString> applications;
94 applications << "konqbrowser" << "kmail" << "systemsettings" << "dolphin";
96 QList<QString> desktopFiles;
98 foreach(const QString& application, applications) {
99 KService::Ptr service = KService::serviceByStorageId("kde4-" + application + ".desktop");
100 if (service) {
101 desktopFiles << service->entryPath();
105 return desktopFiles;
107 static void saveFavorites() {
108 KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
109 favoritesGroup.writeEntry("FavoriteURLs", globalFavoriteList);
110 favoritesGroup.config()->sync();
112 static QList<QString> globalFavoriteList;
113 static QSet<QString> globalFavoriteSet;
114 static QSet<FavoritesModel*> models;
117 QList<QString> FavoritesModel::Private::globalFavoriteList;
118 QSet<QString> FavoritesModel::Private::globalFavoriteSet;
119 QSet<FavoritesModel*> FavoritesModel::Private::models;
121 FavoritesModel::FavoritesModel(QObject *parent)
122 : KickoffModel(parent)
123 , d(new Private(this))
125 Private::models << this;
126 if (Private::models.count() == 1 && Private::globalFavoriteList.isEmpty()) {
127 Private::loadFavorites();
128 } else {
129 foreach(const QString &url, Private::globalFavoriteList) {
130 d->addFavoriteItem(url);
135 FavoritesModel::~FavoritesModel()
137 Private::models.remove(this);
139 if (Private::models.isEmpty()) {
140 Private::saveFavorites();
143 delete d;
145 void FavoritesModel::add(const QString& url)
147 Private::globalFavoriteList << url;
148 Private::globalFavoriteSet << url;
150 foreach(FavoritesModel* model, Private::models) {
151 model->d->addFavoriteItem(url);
154 // save after each add in case we crash
155 Private::saveFavorites();
158 void FavoritesModel::move(int startRow, int destRow)
160 // just move the item
161 Private::globalFavoriteList.move(startRow, destRow);
163 foreach(FavoritesModel* model, Private::models) {
164 model->d->moveFavoriteItem(startRow, destRow);
167 // save after each add in case we crash
168 Private::saveFavorites();
171 void FavoritesModel::remove(const QString& url)
173 Private::globalFavoriteList.removeAll(url);
174 Private::globalFavoriteSet.remove(url);
176 foreach(FavoritesModel* model, Private::models) {
177 model->d->removeFavoriteItem(url);
180 // save after each remove in case of crash or other mishaps
181 Private::saveFavorites();
184 bool FavoritesModel::isFavorite(const QString& url)
186 return Private::globalFavoriteSet.contains(url);
189 int FavoritesModel::numberOfFavorites()
191 foreach(FavoritesModel* model, Private::models) {
192 return model->d->headerItem->rowCount() - 1;
195 return 0;
198 void FavoritesModel::sortFavorites(Qt::SortOrder order)
200 foreach(FavoritesModel *model, Private::models) {
201 model->d->headerItem->sortChildren(0, order);
205 void FavoritesModel::sortFavoritesAscending()
207 sortFavorites(Qt::AscendingOrder);
210 void FavoritesModel::sortFavoritesDescending()
212 sortFavorites(Qt::DescendingOrder);
215 bool FavoritesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
216 int row, int column, const QModelIndex & parent)
218 Q_UNUSED(parent);
220 if (action == Qt::IgnoreAction) {
221 return true;
224 if (column > 0) {
225 return false;
228 if (action == Qt::MoveAction) {
229 QModelIndex modelIndex;
230 QStandardItem *startItem;
231 int startRow = 0, destRow;
233 destRow = row;
235 // look for the favorite that was dragged
236 for (int i = 0; i < d->headerItem->rowCount(); i++) {
237 startItem = d->headerItem->child(i, 0);
238 if (QFileInfo(startItem->data(Kickoff::UrlRole).toString()).completeBaseName()
239 == QFileInfo(data->text()).completeBaseName()) {
240 startRow = i;
241 break;
245 if (destRow < 0)
246 return false;
248 // now move the item to it's new location
249 FavoritesModel::move(startRow, destRow);
251 return true;
254 return true;
256 #include "favoritesmodel.moc"