Added support for remembering favorites in the plasmarc file

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=723316
This commit is contained in:
Ivan Čukić 2007-10-09 09:51:55 +00:00
parent b5fa2a0936
commit 700fe7d498
3 changed files with 42 additions and 9 deletions

View File

@ -42,7 +42,7 @@ public:
appletList(0), appletList(0),
config("plasmarc"), config("plasmarc"),
configGroup(&config, "Applet Browser"), configGroup(&config, "Applet Browser"),
itemModel(q, &configGroup), itemModel(configGroup, q),
filterModel(q) filterModel(q)
{ {
} }

View File

@ -19,9 +19,9 @@
#include "plasmaappletitemmodel_p.h" #include "plasmaappletitemmodel_p.h"
PlasmaAppletItem::PlasmaAppletItem(QObject *parent, const KPluginInfo& info, PlasmaAppletItem::PlasmaAppletItem(PlasmaAppletItemModel * model, const KPluginInfo& info,
FilterFlags flags, QMap<QString, QVariant> * extraAttrs) : FilterFlags flags, QMap<QString, QVariant> * extraAttrs) :
QObject(parent) QObject(model), m_model(model)
{ {
QMap<QString, QVariant> attrs; QMap<QString, QVariant> attrs;
attrs.insert("name", info.name()); attrs.insert("name", info.name());
@ -57,6 +57,7 @@ void PlasmaAppletItem::setFavorite(bool favorite)
QMap<QString, QVariant> attrs = data().toMap(); QMap<QString, QVariant> attrs = data().toMap();
attrs.insert("favorite", favorite ? true : false); attrs.insert("favorite", favorite ? true : false);
setData(QVariant(attrs)); setData(QVariant(attrs));
m_model->setFavorite(attrs["pluginName"].toString(), favorite);
} }
bool PlasmaAppletItem::passesFiltering( bool PlasmaAppletItem::passesFiltering(
@ -65,13 +66,14 @@ bool PlasmaAppletItem::passesFiltering(
return data().toMap()[filter.first] == filter.second; return data().toMap()[filter.first] == filter.second;
} }
PlasmaAppletItemModel::PlasmaAppletItemModel(QObject * parent, KConfigGroup * configGroup) : PlasmaAppletItemModel::PlasmaAppletItemModel(KConfigGroup configGroup, QObject * parent) :
KCategorizedItemsViewModels::DefaultItemModel(parent) KCategorizedItemsViewModels::DefaultItemModel(parent),
m_configGroup(configGroup)
{ {
// Recommended emblems and filters // Recommended emblems and filters
QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]plugins"); QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]plugins");
QMapIterator<QString, QString> i(configGroup->entryMap()); QMapIterator<QString, QString> i(m_configGroup.entryMap());
QMap < QString, QMap < QString, QVariant > > extraPluginAttrs; QMap < QString, QMap < QString, QVariant > > extraPluginAttrs;
while (i.hasNext()) { while (i.hasNext()) {
i.next(); 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() //TODO: get recommended, favorit, used, etc out of knownApplets()
foreach (const KPluginInfo& info, Plasma::Applet::knownApplets()) { foreach (const KPluginInfo& info, Plasma::Applet::knownApplets()) {
kDebug() << info.pluginName() << " is the name of the plugin\n"; 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; 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();
}

View File

@ -26,6 +26,8 @@
#include "kcategorizeditemsview_p.h" #include "kcategorizeditemsview_p.h"
class PlasmaAppletItemModel;
/** /**
* Implementation of the KCategorizedItemsViewModels::AbstractItem * Implementation of the KCategorizedItemsViewModels::AbstractItem
*/ */
@ -39,7 +41,7 @@ public:
Q_DECLARE_FLAGS(FilterFlags, FilterFlag) Q_DECLARE_FLAGS(FilterFlags, FilterFlag)
PlasmaAppletItem(QObject *parent, const KPluginInfo& info, PlasmaAppletItem(PlasmaAppletItemModel * model, const KPluginInfo& info,
FilterFlags flags = NoFilter, QMap<QString, QVariant> * extraAttrs = NULL); FilterFlags flags = NoFilter, QMap<QString, QVariant> * extraAttrs = NULL);
virtual QString name() const; virtual QString name() const;
@ -48,17 +50,26 @@ public:
virtual void setFavorite(bool favorite); virtual void setFavorite(bool favorite);
virtual bool passesFiltering( virtual bool passesFiltering(
const KCategorizedItemsViewModels::Filter & filter) const; const KCategorizedItemsViewModels::Filter & filter) const;
private:
PlasmaAppletItemModel * m_model;
}; };
class PlasmaAppletItemModel : class PlasmaAppletItemModel :
public KCategorizedItemsViewModels::DefaultItemModel public KCategorizedItemsViewModels::DefaultItemModel
{ {
public: public:
PlasmaAppletItemModel(QObject * parent = 0, KConfigGroup * configGroup = NULL); PlasmaAppletItemModel(KConfigGroup configGroup, QObject * parent = 0);
QStringList mimeTypes() const; QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList & indexes) 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) Q_DECLARE_OPERATORS_FOR_FLAGS(PlasmaAppletItem::FilterFlags)