diff --git a/appletbrowser.cpp b/appletbrowser.cpp index 8dc2c6932..f6795e9c2 100644 --- a/appletbrowser.cpp +++ b/appletbrowser.cpp @@ -42,7 +42,7 @@ public: appletList(0), config("plasmarc"), configGroup(&config, "Applet Browser"), - itemModel(q, &configGroup), + itemModel(configGroup, q), filterModel(q) { } diff --git a/appletbrowser/plasmaappletitemmodel.cpp b/appletbrowser/plasmaappletitemmodel.cpp index f3bd795d2..6e37690c6 100644 --- a/appletbrowser/plasmaappletitemmodel.cpp +++ b/appletbrowser/plasmaappletitemmodel.cpp @@ -19,9 +19,9 @@ #include "plasmaappletitemmodel_p.h" -PlasmaAppletItem::PlasmaAppletItem(QObject *parent, const KPluginInfo& info, +PlasmaAppletItem::PlasmaAppletItem(PlasmaAppletItemModel * model, const KPluginInfo& info, FilterFlags flags, QMap * extraAttrs) : - QObject(parent) + QObject(model), m_model(model) { QMap attrs; attrs.insert("name", info.name()); @@ -57,6 +57,7 @@ void PlasmaAppletItem::setFavorite(bool favorite) QMap attrs = data().toMap(); attrs.insert("favorite", favorite ? true : false); setData(QVariant(attrs)); + m_model->setFavorite(attrs["pluginName"].toString(), favorite); } bool PlasmaAppletItem::passesFiltering( @@ -65,13 +66,14 @@ bool PlasmaAppletItem::passesFiltering( return data().toMap()[filter.first] == filter.second; } -PlasmaAppletItemModel::PlasmaAppletItemModel(QObject * parent, KConfigGroup * configGroup) : - KCategorizedItemsViewModels::DefaultItemModel(parent) +PlasmaAppletItemModel::PlasmaAppletItemModel(KConfigGroup configGroup, QObject * parent) : + KCategorizedItemsViewModels::DefaultItemModel(parent), + m_configGroup(configGroup) { // Recommended emblems and filters QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]plugins"); - QMapIterator i(configGroup->entryMap()); + QMapIterator i(m_configGroup.entryMap()); QMap < QString, QMap < QString, QVariant > > extraPluginAttrs; while (i.hasNext()) { i.next(); @@ -83,10 +85,15 @@ PlasmaAppletItemModel::PlasmaAppletItemModel(QObject * parent, KConfigGroup * co } } + m_favorites = m_configGroup.readEntry("favorites").split(","); + //TODO: get recommended, favorit, used, etc out of knownApplets() foreach (const KPluginInfo& info, Plasma::Applet::knownApplets()) { kDebug() << info.pluginName() << " is the name of the plugin\n"; - appendRow(new PlasmaAppletItem(this, info, PlasmaAppletItem::NoFilter, &(extraPluginAttrs[info.pluginName()]))); + + appendRow(new PlasmaAppletItem(this, info, + ((m_favorites.contains(info.pluginName()))?PlasmaAppletItem::Favorite:PlasmaAppletItem::NoFilter) + , &(extraPluginAttrs[info.pluginName()]))); } } @@ -122,3 +129,18 @@ QMimeData* PlasmaAppletItemModel::mimeData(const QModelIndexList & indexes) cons return data; } + +void PlasmaAppletItemModel::setFavorite(QString plugin, bool favorite) { + if (favorite) { + if (!m_favorites.contains(plugin)) { + m_favorites.append(plugin); + } + } else { + if (m_favorites.contains(plugin)) { + m_favorites.removeAll(plugin); + } + } + m_configGroup.writeEntry("favorites", m_favorites.join(",")); + m_configGroup.sync(); + +} diff --git a/appletbrowser/plasmaappletitemmodel_p.h b/appletbrowser/plasmaappletitemmodel_p.h index 6b5192cb6..cc483f7c3 100644 --- a/appletbrowser/plasmaappletitemmodel_p.h +++ b/appletbrowser/plasmaappletitemmodel_p.h @@ -26,6 +26,8 @@ #include "kcategorizeditemsview_p.h" +class PlasmaAppletItemModel; + /** * Implementation of the KCategorizedItemsViewModels::AbstractItem */ @@ -39,7 +41,7 @@ public: Q_DECLARE_FLAGS(FilterFlags, FilterFlag) - PlasmaAppletItem(QObject *parent, const KPluginInfo& info, + PlasmaAppletItem(PlasmaAppletItemModel * model, const KPluginInfo& info, FilterFlags flags = NoFilter, QMap * extraAttrs = NULL); virtual QString name() const; @@ -48,17 +50,26 @@ public: virtual void setFavorite(bool favorite); virtual bool passesFiltering( const KCategorizedItemsViewModels::Filter & filter) const; +private: + PlasmaAppletItemModel * m_model; }; class PlasmaAppletItemModel : public KCategorizedItemsViewModels::DefaultItemModel { public: - PlasmaAppletItemModel(QObject * parent = 0, KConfigGroup * configGroup = NULL); + PlasmaAppletItemModel(KConfigGroup configGroup, QObject * parent = 0); QStringList mimeTypes() const; QMimeData* mimeData(const QModelIndexList & indexes) const; + + void setFavorite(QString plugin, bool favorite); + +private: + QStringList m_favorites; + KConfigGroup m_configGroup; + }; Q_DECLARE_OPERATORS_FOR_FLAGS(PlasmaAppletItem::FilterFlags)