• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

favoritesmodel.cpp

Go to the documentation of this file.
00001 /*  
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 //Own
00021 #include "core/favoritesmodel.h"
00022 
00023 // Qt
00024 #include <QHash>
00025 #include <QList>
00026 
00027 // KDE
00028 #include <KConfigGroup>
00029 #include <KService>
00030 #include <kdebug.h>
00031 
00032 // Local
00033 #include "core/models.h"
00034 
00035 using namespace Kickoff;
00036 
00037 class FavoritesModel::Private
00038 {
00039 public:
00040     Private(FavoritesModel *parent)
00041         :q(parent)
00042     {
00043         headerItem = new QStandardItem(i18n("Favorites"));
00044         q->appendRow(headerItem);
00045     }
00046 
00047     void addFavoriteItem(const QString& url)
00048     {
00049         QStandardItem *item = StandardItemFactory::createItemForUrl(url);
00050         headerItem->appendRow(item);
00051     }
00052     void removeFavoriteItem(const QString& url)
00053     {
00054        QModelIndexList matches = q->match(q->index(0,0),UrlRole,
00055                                           url,-1,
00056                                           Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap | Qt::MatchRecursive));
00057 
00058        kDebug() << "Removing item matches" << matches;
00059 
00060        foreach(const QModelIndex& index,matches) {
00061          QStandardItem *item = q->itemFromIndex(index);
00062          if (item->parent()) {
00063             item->parent()->removeRow(item->row());
00064          } else {
00065             qDeleteAll(q->takeRow(item->row()));
00066          }
00067        }
00068     }
00069 
00070     FavoritesModel * const q;
00071     QStandardItem *headerItem;
00072 
00073     static void loadFavorites()
00074     {
00075        KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
00076        QList<QString> favoriteList = favoritesGroup.readEntry("FavoriteURLs",QList<QString>());
00077        if (favoriteList.isEmpty()) {
00078            favoriteList = defaultFavorites();
00079        }
00080 
00081        foreach(const QString &favorite,favoriteList) {
00082            FavoritesModel::add(favorite);
00083        }
00084     }
00085     static QList<QString> defaultFavorites()
00086     {
00087         QList<QString> applications;
00088         applications << "konqbrowser" << "kmail" << "systemsettings" << "dolphin";
00089 
00090         QList<QString> desktopFiles;
00091 
00092         foreach(const QString& application,applications) {
00093             KService::Ptr service = KService::serviceByStorageId("kde4-" + application + ".desktop");
00094             if (service) {
00095                 desktopFiles << service->entryPath();
00096             }
00097         }
00098 
00099         return desktopFiles;
00100     }
00101     static void saveFavorites()
00102     {
00103         KConfigGroup favoritesGroup = componentData().config()->group("Favorites");
00104         favoritesGroup.writeEntry("FavoriteURLs",globalFavoriteList);
00105         favoritesGroup.config()->sync();
00106     }
00107     static QList<QString> globalFavoriteList;
00108     static QSet<QString> globalFavoriteSet;
00109     static QSet<FavoritesModel*> models;
00110 };
00111 
00112 QList<QString> FavoritesModel::Private::globalFavoriteList;
00113 QSet<QString> FavoritesModel::Private::globalFavoriteSet;
00114 QSet<FavoritesModel*> FavoritesModel::Private::models;
00115 
00116 FavoritesModel::FavoritesModel(QObject *parent)
00117     : KickoffModel(parent)
00118     , d(new Private(this))
00119 {
00120     Private::models << this;
00121     if (Private::models.count() == 1 && Private::globalFavoriteList.isEmpty()) {
00122         Private::loadFavorites();
00123     } else {
00124         foreach (const QString &url, Private::globalFavoriteList) {
00125             d->addFavoriteItem(url);
00126         }
00127     }
00128 }
00129 FavoritesModel::~FavoritesModel()
00130 {
00131     Private::models.remove(this);
00132 
00133     if (Private::models.isEmpty()) {
00134         Private::saveFavorites();
00135     }
00136 
00137     delete d;
00138 }
00139 void FavoritesModel::add(const QString& url)
00140 {
00141     Private::globalFavoriteList << url;
00142     Private::globalFavoriteSet << url;
00143 
00144     foreach(FavoritesModel* model,Private::models) {
00145         model->d->addFavoriteItem(url);
00146     }
00147 
00148     // save after each add in case we crash
00149     Private::saveFavorites();
00150 }
00151 void FavoritesModel::remove(const QString& url)
00152 {
00153    Private::globalFavoriteList.removeAll(url);
00154    Private::globalFavoriteSet.remove(url);
00155 
00156    foreach(FavoritesModel* model,Private::models) {
00157        model->d->removeFavoriteItem(url);
00158    }
00159 
00160     // save after each remove in case of crash or other mishaps
00161     Private::saveFavorites();
00162 }
00163 bool FavoritesModel::isFavorite(const QString& url)
00164 {
00165     return Private::globalFavoriteSet.contains(url);
00166 }
00167 
00168 #include "favoritesmodel.moc"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal