# # SVN commit 911787 by sebsauer: # # be able to set the number of recently used applications at the model rather then only within the recentlyapplication-singleton what allows us to define the number of displayed items within the classic menu without changing what is displayed in kickoff's recently used tab. # --- plasma/applets/kickoff/simpleapplet/simpleapplet.cpp~ 2009-03-09 21:33:13.000000000 +0100 +++ plasma/applets/kickoff/simpleapplet/simpleapplet.cpp 2009-03-09 21:54:01.000000000 +0100 @@ -93,6 +93,7 @@ MenuLauncherApplet::ViewType viewtype; MenuLauncherApplet::FormatType formattype; + int maxRecentApps; QComboBox *viewComboBox; QComboBox *formatComboBox; @@ -117,6 +118,13 @@ delete menuview; } + void setMaxRecentApps(int num) { + maxRecentApps = qMax(0, num); + if (maxRecentApps > Kickoff::RecentApplications::self()->maximum()) { + Kickoff::RecentApplications::self()->setMaximum(maxRecentApps); + } + } + void addItem(QComboBox* combo, const QString& caption, int index, const QString& icon = QString()) { if (icon.isEmpty()) { combo->addItem(caption, index); @@ -234,16 +242,16 @@ KConfigGroup cg = config(); - { - QMetaEnum e = metaObject()->enumerator(metaObject()->indexOfEnumerator("ViewType")); - QByteArray ba = cg.readEntry("view", QByteArray(e.valueToKey(d->viewtype))); - d->viewtype = (MenuLauncherApplet::ViewType) e.keyToValue(ba); - } - { - QMetaEnum e = metaObject()->enumerator(metaObject()->indexOfEnumerator("FormatType")); - QByteArray ba = cg.readEntry("format", QByteArray(e.valueToKey(d->formattype))); - d->formattype = (MenuLauncherApplet::FormatType) e.keyToValue(ba); - } + QMetaEnum vte = metaObject()->enumerator(metaObject()->indexOfEnumerator("ViewType")); + QByteArray vtb = cg.readEntry("view", QByteArray(vte.valueToKey(d->viewtype))); + d->viewtype = (MenuLauncherApplet::ViewType) vte.keyToValue(vtb); + + QMetaEnum fte = metaObject()->enumerator(metaObject()->indexOfEnumerator("FormatType")); + QByteArray ftb = cg.readEntry("format", QByteArray(fte.valueToKey(d->formattype))); + d->formattype = (MenuLauncherApplet::FormatType) fte.keyToValue(ftb); + + d->setMaxRecentApps(cg.readEntry("maxRecentApps", qMin(5, Kickoff::RecentApplications::self()->maximum()))); + KConfig *cfg = new KConfig("mandrivarc"); KConfigGroup grp = cfg->group("menu"); @@ -355,7 +363,7 @@ d->recentApplicationsSpinBox = new QSpinBox(p); d->recentApplicationsSpinBox->setMaximum(10); d->recentApplicationsSpinBox->setMinimum(0); - d->recentApplicationsSpinBox->setValue(Kickoff::RecentApplications::self()->maximum()); + d->recentApplicationsSpinBox->setValue(d->maxRecentApps); recentLabel->setBuddy(d->recentApplicationsSpinBox); l->addWidget(d->recentApplicationsSpinBox, 2, 1); @@ -376,7 +384,7 @@ bool needssaving = false; KConfigGroup cg = config(); - int vt = d->viewComboBox->itemData(d->viewComboBox->currentIndex()).toInt(); + const int vt = d->viewComboBox->itemData(d->viewComboBox->currentIndex()).toInt(); if (vt != d->viewtype) { d->viewtype = (MenuLauncherApplet::ViewType) vt; needssaving = true; @@ -388,7 +396,7 @@ d->icon->update(); } - int ft = d->formatComboBox->itemData(d->formatComboBox->currentIndex()).toInt(); + const int ft = d->formatComboBox->itemData(d->formatComboBox->currentIndex()).toInt(); if (ft != d->formattype) { d->formattype = (MenuLauncherApplet::FormatType) ft; needssaving = true; @@ -396,7 +404,12 @@ QMetaEnum e = metaObject()->enumerator(metaObject()->indexOfEnumerator("FormatType")); cg.writeEntry("format", QByteArray(e.valueToKey(d->formattype))); } - Kickoff::RecentApplications::self()->setMaximum(d->recentApplicationsSpinBox->value()); + const int maxRecentApps = d->recentApplicationsSpinBox->value(); + if (maxRecentApps != d->maxRecentApps) { + needssaving = true; + d->setMaxRecentApps(maxRecentApps); + cg.writeEntry("maxRecentApps", maxRecentApps); + } if (needssaving) { emit configNeedsSaving(); @@ -423,9 +436,12 @@ switch (d->viewtype) { case Combined: { - if (Kickoff::RecentApplications::self()->recentApplications().size() > 0) { + + if (Kickoff::RecentApplications::self()->recentApplications().size() > 0) { + if (d->maxRecentApps > 0) { d->menuview->addTitle(i18n("Recently Used Applications")); - Kickoff::MenuView *recentlyview = d->createMenuView(new Kickoff::RecentlyUsedModel(d->menuview, Kickoff::RecentlyUsedModel::ApplicationsOnly)); + Kickoff::RecentlyUsedModel* recentlymodel = new Kickoff::RecentlyUsedModel(d->menuview, Kickoff::RecentlyUsedModel::ApplicationsOnly, d->maxRecentApps); + Kickoff::MenuView *recentlyview = d->createMenuView(recentlymodel); d->addMenu(recentlyview, true); } d->menuview->addTitle(i18n("All Applications")); --- plasma/applets/kickoff/core/recentapplications.cpp~ 2009-03-09 21:33:13.000000000 +0100 +++ plasma/applets/kickoff/core/recentapplications.cpp 2009-03-09 21:59:34.000000000 +0100 @@ -38,12 +38,11 @@ { public: class ServiceInfo; - - Private() - : maxServices(DEFAULT_MAX_SERVICES) { + + Private() : defaultMaxServices(DEFAULT_MAX_SERVICES) { KConfigGroup recentGroup = componentData().config()->group("RecentlyUsed"); QList<QString> recentApplications = recentGroup.readEntry("Applications", QList<QString>()); - maxServices = recentGroup.readEntry("MaxApplications", maxServices); + defaultMaxServices = maxServices = recentGroup.readEntry("MaxApplications", defaultMaxServices); // TESTING // the actual last date/time is not currently recorded, instead we just use @@ -76,8 +75,6 @@ recentGroup.writeEntry("Applications", recentApplications); - recentGroup.writeEntry("MaxApplications", maxServices); - } void addEntry(const QString& id, ServiceInfo& info) { // if this service is already in the list then remove the existing @@ -90,12 +87,14 @@ serviceQueue.append(id); info.queueIter = --serviceQueue.end(); serviceInfo.insert(id, info); + } + void removeExpiredEntries() { // if more than the maximum number of services have been added // remove the least recently used service - if (serviceQueue.count() > maxServices) { + while (serviceQueue.count() > maxServices) { QString removeId = serviceQueue.takeFirst(); - kDebug() << "More than max services added. Removing" << removeId << "from queue."; + kDebug() << "Removing" << removeId << "from queue."; serviceInfo.remove(removeId); emit instance.applicationRemoved(KService::serviceByStorageId(removeId)); } @@ -117,7 +116,7 @@ }; static const int DEFAULT_MAX_SERVICES = 5; - int maxServices; + int defaultMaxServices, maxServices; // queue to keep track of the order in which services have been used // (most recently used at the back) QLinkedList<QString> serviceQueue; @@ -162,17 +161,16 @@ Q_ASSERT(maximum >= 0); privateSelf->maxServices = maximum; - while (privateSelf->serviceQueue.count() > privateSelf->maxServices) { - QString removeId = privateSelf->serviceQueue.takeFirst(); - kDebug() << "More than max services added. Removing" << removeId << "from queue."; - privateSelf->serviceInfo.remove(removeId); - emit applicationRemoved(KService::serviceByStorageId(removeId)); - } + privateSelf->removeExpiredEntries(); } int RecentApplications::maximum() const { return privateSelf->maxServices; } +int RecentApplications::defaultMaximum() const +{ + return privateSelf->defaultMaxServices; +} void RecentApplications::add(KService::Ptr service) { Private::ServiceInfo info = privateSelf->serviceInfo.value(service->storageId()); @@ -185,6 +183,8 @@ kDebug() << "Recent app added" << info.storageId << info.startCount; emit applicationAdded(service, info.startCount); + + privateSelf->removeExpiredEntries(); } void RecentApplications::clear() { --- plasma/applets/kickoff/core/recentapplications.h~ 2009-03-09 21:33:13.000000000 +0100 +++ plasma/applets/kickoff/core/recentapplications.h 2009-03-09 22:01:06.000000000 +0100 @@ -61,6 +61,9 @@ void setMaximum(int max); /** Returns the maximum number of recently used applications that are remembered. */ int maximum() const; + /** Returns the default maximum number of recently used applications that are remembered as defined + either in the configfile as "MaxApplications" or via the DEFAULT_MAX_SERVICES macro. */ + int defaultMaximum() const; public Q_SLOTS: /** --- plasma/applets/kickoff/core/recentlyusedmodel.h~ 2009-03-09 21:33:13.000000000 +0100 +++ plasma/applets/kickoff/core/recentlyusedmodel.h 2009-03-09 22:02:30.000000000 +0100 @@ -50,7 +50,7 @@ }; /** Construct a new RecentlyUsedModel with the specified parent. */ - explicit RecentlyUsedModel(QObject *parent = 0, RecentType recenttype = DocumentsAndApplications); + explicit RecentlyUsedModel(QObject *parent = 0, RecentType recenttype = DocumentsAndApplications, int maxRecentApps = -1); virtual ~RecentlyUsedModel(); public Q_SLOTS: --- plasma/applets/kickoff/simpleapplet/simpleapplet.cpp~ 2009-03-09 21:54:01.000000000 +0100 +++ plasma/applets/kickoff/simpleapplet/simpleapplet.cpp 2009-03-09 22:03:44.000000000 +0100 @@ -444,6 +444,7 @@ Kickoff::MenuView *recentlyview = d->createMenuView(recentlymodel); d->addMenu(recentlyview, true); } + } d->menuview->addTitle(i18n("All Applications")); Kickoff::ApplicationModel *appModel = new Kickoff::ApplicationModel(d->menuview); appModel->setDuplicatePolicy(Kickoff::ApplicationModel::ShowLatestOnlyPolicy); --- plasma/applets/kickoff/core/recentlyusedmodel.cpp~ 2009-03-09 21:33:13.000000000 +0100 +++ plasma/applets/kickoff/core/recentlyusedmodel.cpp 2009-03-09 22:12:06.000000000 +0100 @@ -42,9 +42,10 @@ class RecentlyUsedModel::Private { public: - Private(RecentlyUsedModel *parent, RecentType recenttype) + Private(RecentlyUsedModel *parent, RecentType recenttype, int maxRecentApps) : q(parent) , recenttype(recenttype) + , maxRecentApps(maxRecentApps >= 0 ? maxRecentApps : Kickoff::RecentApplications::self()->defaultMaximum()) , recentDocumentItem(0) , recentAppItem(0) { @@ -71,6 +72,9 @@ recentAppItem->appendRow(appItem); } else { recentAppItem->insertRow(0, appItem); + } + while (recentAppItem->rowCount() > maxRecentApps) { + recentAppItem->removeRow(recentAppItem->rowCount() - 1); } } void addRecentDocument(const QString& desktopPath, bool append) { @@ -105,21 +109,24 @@ QList<KService::Ptr> services = RecentApplications::self()->recentApplications(); foreach(const KService::Ptr& service, services) { addRecentApplication(service, true); + for(int i = 0; i < maxRecentApps && i < services.count(); ++i) { + addRecentApplication(services[i], true); } q->appendRow(recentAppItem); } RecentlyUsedModel * const q; RecentType recenttype; + int maxRecentApps; QStandardItem *recentDocumentItem; QStandardItem *recentAppItem; QHash<QString, QStandardItem*> itemsByPath; }; -RecentlyUsedModel::RecentlyUsedModel(QObject *parent, RecentType recenttype) +RecentlyUsedModel::RecentlyUsedModel(QObject *parent, RecentType recenttype, int maxRecentApps) : KickoffModel(parent) - , d(new Private(this, recenttype)) + , d(new Private(this, recenttype, maxRecentApps)) { QDBusConnection dbus = QDBusConnection::sessionBus(); (void)new RecentAdaptor(this); --- plasma/applets/kickoff/core/recentlyusedmodel.cpp~ 2009-03-09 22:12:06.000000000 +0100 +++ plasma/applets/kickoff/core/recentlyusedmodel.cpp 2009-03-09 22:16:28.000000000 +0100 @@ -107,8 +107,6 @@ void loadRecentApplications() { recentAppItem = new QStandardItem(i18n("Applications")); QList<KService::Ptr> services = RecentApplications::self()->recentApplications(); - foreach(const KService::Ptr& service, services) { - addRecentApplication(service, true); for(int i = 0; i < maxRecentApps && i < services.count(); ++i) { addRecentApplication(services[i], true); }