Applets
favoritesmodel.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/favoritesmodel.h"
00022
00023
00024 #include <QHash>
00025 #include <QList>
00026 #include <QMimeData>
00027 #include <QFileInfo>
00028
00029
00030 #include <KConfigGroup>
00031 #include <KService>
00032 #include <kdebug.h>
00033
00034
00035 #include "core/models.h"
00036
00037 using namespace Kickoff;
00038
00039 class FavoritesModel::Private
00040 {
00041 public:
00042 Private(FavoritesModel *parent)
00043 : q(parent) {
00044 headerItem = new QStandardItem(i18n("Favorites"));
00045 q->appendRow(headerItem);
00046 }
00047
00048 void addFavoriteItem(const QString& url) {
00049 QStandardItem *item = StandardItemFactory::createItemForUrl(url);
00050 headerItem->appendRow(item);
00051 }
00052 void moveFavoriteItem(int startRow, int destRow) {
00053 if (destRow == startRow)
00054 return;
00055
00056 QStandardItem *item = headerItem->takeChild(startRow);
00057
00058 headerItem->removeRow(startRow);
00059 headerItem->insertRow(destRow, item);
00060 }
00061 void removeFavoriteItem(const QString& url) {
00062 QModelIndexList matches = q->match(q->index(0, 0), UrlRole,
00063 url, -1,
00064 Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap | Qt::MatchRecursive));
00065
00066 kDebug() << "Removing item matches" << matches;
00067
00068 foreach(const QModelIndex& index, matches) {
00069 QStandardItem *item = q->itemFromIndex(index);
00070 if (item->parent()) {
00071 item->parent()->removeRow(item->row());
00072 } else {
00073 qDeleteAll(q->takeRow(item->row()));
00074 }
00075 }
00076 }
00077
00078 FavoritesModel * const q;
00079 QStandardItem *headerItem;
00080
00081 static void loadFavorites() {
00082 KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
00083 QList<QString> favoriteList = favoritesGroup.readEntry("FavoriteURLs", QList<QString>());
00084 if (favoriteList.isEmpty()) {
00085 favoriteList = defaultFavorites();
00086 }
00087
00088 foreach(const QString &favorite, favoriteList) {
00089 FavoritesModel::add(favorite);
00090 }
00091 }
00092 static QList<QString> defaultFavorites() {
00093 QList<QString> applications;
00094 applications << "konqbrowser" << "kmail" << "systemsettings" << "dolphin";
00095
00096 QList<QString> desktopFiles;
00097
00098 foreach(const QString& application, applications) {
00099 KService::Ptr service = KService::serviceByStorageId("kde4-" + application + ".desktop");
00100 if (service) {
00101 desktopFiles << service->entryPath();
00102 }
00103 }
00104
00105 return desktopFiles;
00106 }
00107 static void saveFavorites() {
00108 KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
00109 favoritesGroup.writeEntry("FavoriteURLs", globalFavoriteList);
00110 favoritesGroup.config()->sync();
00111 }
00112 static QList<QString> globalFavoriteList;
00113 static QSet<QString> globalFavoriteSet;
00114 static QSet<FavoritesModel*> models;
00115 };
00116
00117 QList<QString> FavoritesModel::Private::globalFavoriteList;
00118 QSet<QString> FavoritesModel::Private::globalFavoriteSet;
00119 QSet<FavoritesModel*> FavoritesModel::Private::models;
00120
00121 FavoritesModel::FavoritesModel(QObject *parent)
00122 : KickoffModel(parent)
00123 , d(new Private(this))
00124 {
00125 Private::models << this;
00126 if (Private::models.count() == 1 && Private::globalFavoriteList.isEmpty()) {
00127 Private::loadFavorites();
00128 } else {
00129 foreach(const QString &url, Private::globalFavoriteList) {
00130 d->addFavoriteItem(url);
00131 }
00132 }
00133
00134 }
00135 FavoritesModel::~FavoritesModel()
00136 {
00137 Private::models.remove(this);
00138
00139 if (Private::models.isEmpty()) {
00140 Private::saveFavorites();
00141 }
00142
00143 delete d;
00144 }
00145 void FavoritesModel::add(const QString& url)
00146 {
00147 Private::globalFavoriteList << url;
00148 Private::globalFavoriteSet << url;
00149
00150 foreach(FavoritesModel* model, Private::models) {
00151 model->d->addFavoriteItem(url);
00152 }
00153
00154
00155 Private::saveFavorites();
00156 }
00157
00158 void FavoritesModel::move(int startRow, int destRow)
00159 {
00160
00161 Private::globalFavoriteList.move(startRow, destRow);
00162
00163 foreach(FavoritesModel* model, Private::models) {
00164 model->d->moveFavoriteItem(startRow, destRow);
00165 }
00166
00167
00168 Private::saveFavorites();
00169 }
00170
00171 void FavoritesModel::remove(const QString& url)
00172 {
00173 Private::globalFavoriteList.removeAll(url);
00174 Private::globalFavoriteSet.remove(url);
00175
00176 foreach(FavoritesModel* model, Private::models) {
00177 model->d->removeFavoriteItem(url);
00178 }
00179
00180
00181 Private::saveFavorites();
00182 }
00183
00184 bool FavoritesModel::isFavorite(const QString& url)
00185 {
00186 return Private::globalFavoriteSet.contains(url);
00187 }
00188
00189 int FavoritesModel::numberOfFavorites()
00190 {
00191 foreach(FavoritesModel* model, Private::models) {
00192 return model->d->headerItem->rowCount() - 1;
00193 }
00194
00195 return 0;
00196 }
00197
00198 void FavoritesModel::sortFavorites(Qt::SortOrder order)
00199 {
00200 foreach(FavoritesModel *model, Private::models) {
00201 model->d->headerItem->sortChildren(0, order);
00202 }
00203 }
00204
00205 void FavoritesModel::sortFavoritesAscending()
00206 {
00207 sortFavorites(Qt::AscendingOrder);
00208 }
00209
00210 void FavoritesModel::sortFavoritesDescending()
00211 {
00212 sortFavorites(Qt::DescendingOrder);
00213 }
00214
00215 bool FavoritesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
00216 int row, int column, const QModelIndex & parent)
00217 {
00218 Q_UNUSED(parent);
00219
00220 if (action == Qt::IgnoreAction) {
00221 return true;
00222 }
00223
00224 if (column > 0) {
00225 return false;
00226 }
00227
00228 if (action == Qt::MoveAction) {
00229 QModelIndex modelIndex;
00230 QStandardItem *startItem;
00231 int startRow = 0, destRow;
00232
00233 destRow = row;
00234
00235
00236 for (int i = 0; i < d->headerItem->rowCount(); i++) {
00237 startItem = d->headerItem->child(i, 0);
00238 if (QFileInfo(startItem->data(Kickoff::UrlRole).toString()).completeBaseName()
00239 == QFileInfo(data->text()).completeBaseName()) {
00240 startRow = i;
00241 break;
00242 }
00243 }
00244
00245 if (destRow < 0)
00246 return false;
00247
00248
00249 FavoritesModel::move(startRow, destRow);
00250
00251 return true;
00252 }
00253
00254 return true;
00255 }
00256 #include "favoritesmodel.moc"