Index: kservice.cpp =================================================================== --- kservice.cpp (revision 720772) +++ kservice.cpp (working copy) @@ -268,6 +268,10 @@ //// +KService::KService() +{ +} + KService::KService( const QString & _name, const QString &_exec, const QString &_icon) : KSycocaEntry(*new KServicePrivate(QString())) { Index: kservicegroup.cpp =================================================================== --- kservicegroup.cpp (revision 720772) +++ kservicegroup.cpp (working copy) @@ -32,6 +32,10 @@ #include +KServiceGroup::KServiceGroup() +{ +} + KServiceGroup::KServiceGroup( const QString & name ) : KSycocaEntry(*new KServiceGroupPrivate(name)) { @@ -319,18 +323,63 @@ sortOrder <<_showEmptyMenu < KServiceGroup::groupEntries(EntriesOptions options) +{ + Q_D(KServiceGroup); + bool sort = options & SortEntries || options & AllowSeparators; + QList list; + List tmp = d->entries(this, sort, options & ExcludeNoDisplay, options & AllowSeparators, options & SortByGenericName); + foreach(const SPtr &ptr, tmp) { + if (ptr->isType(KST_KServiceGroup)) + list.append(Ptr::staticCast(ptr)); + else if (ptr->isType(KST_KServiceSeparator)) + list.append(new KServiceGroup()); + else if (sort && ptr->isType(KST_KService)) + break; + } + return list; +} + +KService::List KServiceGroup::serviceEntries(EntriesOptions options) +{ + Q_D(KServiceGroup); + bool sort = options & SortEntries || options & AllowSeparators; + QList list; + List tmp = d->entries(this, sort, options & ExcludeNoDisplay, options & AllowSeparators, options & SortByGenericName); + bool foundService = false; + foreach(const SPtr &ptr, tmp) { + if (ptr->isType(KST_KService)) { + list.append(KService::Ptr::staticCast(ptr)); + foundService = true; + } + else if (ptr->isType(KST_KServiceSeparator) && foundService) { + list.append(new KService()); + } + } + return list; +} + KServiceGroup::List KServiceGroup::entries(bool sort) { - return entries(sort, true); + Q_D(KServiceGroup); + return d->entries(this, sort, true, false, false); } KServiceGroup::List KServiceGroup::entries(bool sort, bool excludeNoDisplay) { - return entries(sort, excludeNoDisplay, false); + Q_D(KServiceGroup); + return d->entries(this, sort, excludeNoDisplay, false, false); } +KServiceGroup::List +KServiceGroup::entries(bool sort, bool excludeNoDisplay, bool allowSeparators, bool sortByGenericName) +{ + Q_D(KServiceGroup); + return d->entries(this, sort, excludeNoDisplay, allowSeparators, sortByGenericName); +} + static void addItem(KServiceGroup::List &sorted, const KSycocaEntry::Ptr &p, bool &addSeparator) { if (addSeparator && !sorted.isEmpty()) @@ -340,23 +389,21 @@ } KServiceGroup::List -KServiceGroup::entries(bool sort, bool excludeNoDisplay, bool allowSeparators, bool sortByGenericName) +KServiceGroupPrivate::entries(KServiceGroup *group, bool sort, bool excludeNoDisplay, bool allowSeparators, bool sortByGenericName) { - Q_D(KServiceGroup); KServiceGroup::Ptr grp; - KServiceGroup* group = this; // If the entries haven't been loaded yet, we have to reload ourselves // together with the entries. We can't only load the entries afterwards // since the offsets could have been changed if the database has changed. - if (!d->m_bDeep) { + if (!m_bDeep) { - grp = KServiceGroupFactory::self()->findGroupByDesktopPath(relPath(), true); + grp = KServiceGroupFactory::self()->findGroupByDesktopPath(path, true); group = grp.data(); if (0 == group) // No guarantee that we still exist! - return List(); + return KServiceGroup::List(); } if (!sort) @@ -365,9 +412,9 @@ // Sort the list alphabetically, according to locale. // Groups come first, then services. - KSortableList slist; - KSortableList glist; - for (List::ConstIterator it(group->d_func()->m_serviceList.begin()); it != group->d_func()->m_serviceList.end(); ++it) + KSortableList slist; + KSortableList glist; + for (KServiceGroup::List::ConstIterator it(group->d_func()->m_serviceList.begin()); it != group->d_func()->m_serviceList.end(); ++it) { KSycocaEntry::Ptr p = (*it); bool noDisplay = p->isType(KST_KServiceGroup) ? @@ -376,7 +423,7 @@ if (excludeNoDisplay && noDisplay) continue; // Choose the right list - KSortableList & list = p->isType(KST_KServiceGroup) ? glist : slist; + KSortableList & list = p->isType(KST_KServiceGroup) ? glist : slist; QString name; if (p->isType(KST_KServiceGroup)) name = static_cast(p.data())->caption(); @@ -405,25 +452,25 @@ { key = name.toLocal8Bit(); } - list.insert(key,SPtr(*it)); + list.insert(key,KServiceGroup::SPtr(*it)); } // Now sort slist.sort(); glist.sort(); - if (d->sortOrder.isEmpty()) + if (sortOrder.isEmpty()) { - d->sortOrder << ":M"; - d->sortOrder << ":F"; - d->sortOrder << ":OIH IL[4]"; //just inline header + sortOrder << ":M"; + sortOrder << ":F"; + sortOrder << ":OIH IL[4]"; //just inline header } - QString rp = relPath(); + QString rp = path; if(rp == "/") rp.clear(); // Iterate through the sort spec list. // If an entry gets mentioned explicitly, we remove it from the sorted list - for (QStringList::ConstIterator it(d->sortOrder.begin()); it != d->sortOrder.end(); ++it) + for (QStringList::ConstIterator it(sortOrder.begin()); it != sortOrder.end(); ++it) { const QString &item = *it; if (item.isEmpty()) continue; @@ -431,7 +478,7 @@ { QString groupPath = rp + item.mid(1) + '/'; // Remove entry from sorted list of services. - for(KSortableList::Iterator it2 = glist.begin(); it2 != glist.end(); ++it2) + for(KSortableList::Iterator it2 = glist.begin(); it2 != glist.end(); ++it2) { const KServiceGroup::Ptr group = KServiceGroup::Ptr::staticCast( (*it2).value() ); if (group->relPath() == groupPath) @@ -446,7 +493,7 @@ // Remove entry from sorted list of services. // TODO: Remove item from sortOrder-list if not found // TODO: This prevents duplicates - for(KSortableList::Iterator it2 = slist.begin(); it2 != slist.end(); ++it2) + for(KSortableList::Iterator it2 = slist.begin(); it2 != slist.end(); ++it2) { const KService::Ptr service = KService::Ptr::staticCast( (*it2).value() ); if (service->menuId() == item) @@ -458,12 +505,12 @@ } } - List sorted; + KServiceGroup::List sorted; bool needSeparator = false; // Iterate through the sort spec list. // Add the entries to the list according to the sort spec. - for (QStringList::ConstIterator it(d->sortOrder.begin()); it != d->sortOrder.end(); ++it) + for (QStringList::ConstIterator it(sortOrder.begin()); it != sortOrder.end(); ++it) { const QString &item = *it; if (item.isEmpty()) continue; @@ -493,7 +540,7 @@ { parseAttribute( *it3, showEmptyMenu, showInline, showInlineHeader, showInlineAlias, inlineValue ); } - for(KSortableList::Iterator it2 = glist.begin(); it2 != glist.end(); ++it2) + for(KSortableList::Iterator it2 = glist.begin(); it2 != glist.end(); ++it2) { KServiceGroup::Ptr group = KServiceGroup::Ptr::staticCast( (*it2).value() ); group->setShowEmptyMenu( showEmptyMenu ); @@ -507,7 +554,7 @@ else if (item == ":M") { // Add sorted list of sub-menus - for(KSortableList::const_iterator it2 = glist.begin(); it2 != glist.end(); ++it2) + for(KSortableList::const_iterator it2 = glist.begin(); it2 != glist.end(); ++it2) { addItem(sorted, (*it2).value(), needSeparator); } @@ -515,7 +562,7 @@ else if (item == ":F") { // Add sorted list of services - for(KSortableList::const_iterator it2 = slist.begin(); it2 != slist.end(); ++it2) + for(KSortableList::const_iterator it2 = slist.begin(); it2 != slist.end(); ++it2) { addItem(sorted, (*it2).value(), needSeparator); } @@ -523,8 +570,8 @@ else if (item == ":A") { // Add sorted lists of services and submenus - KSortableList::Iterator it_s = slist.begin(); - KSortableList::Iterator it_g = glist.begin(); + KSortableList::Iterator it_s = slist.begin(); + KSortableList::Iterator it_g = glist.begin(); while(true) { @@ -562,7 +609,7 @@ { QString groupPath = rp + item.mid(1) + '/'; - for (List::ConstIterator it2(group->d_func()->m_serviceList.begin()); it2 != group->d_func()->m_serviceList.end(); ++it2) + for (KServiceGroup::List::ConstIterator it2(group->d_func()->m_serviceList.begin()); it2 != group->d_func()->m_serviceList.end(); ++it2) { if (!(*it2)->isType(KST_KServiceGroup)) continue; @@ -573,7 +620,7 @@ { ++it; const QString &nextItem = - (it == d->sortOrder.end()) ? QString() : *it; + (it == sortOrder.end()) ? QString() : *it; if ( nextItem.startsWith( ":O" ) ) { @@ -600,7 +647,7 @@ else it--; - addItem(sorted, SPtr::staticCast(group), needSeparator); + addItem(sorted, KServiceGroup::SPtr::staticCast(group), needSeparator); } break; } @@ -608,7 +655,7 @@ } else { - for (List::ConstIterator it2(group->d_func()->m_serviceList.begin()); it2 != group->d_func()->m_serviceList.end(); ++it2) + for (KServiceGroup::List::ConstIterator it2(group->d_func()->m_serviceList.begin()); it2 != group->d_func()->m_serviceList.end(); ++it2) { if (!(*it2)->isType(KST_KService)) continue; @@ -626,7 +673,7 @@ return sorted; } -void KServiceGroup::parseAttribute( const QString &item , bool &showEmptyMenu, bool &showInline, bool &showInlineHeader, bool & showInlineAlias , int &inlineValue ) +void KServiceGroupPrivate::parseAttribute( const QString &item , bool &showEmptyMenu, bool &showInline, bool &showInlineHeader, bool & showInlineAlias , int &inlineValue ) { if( item == "ME") //menu empty showEmptyMenu=true; @@ -730,3 +777,4 @@ { } + Index: kservicegroup.h =================================================================== --- kservicegroup.h (revision 720772) +++ kservicegroup.h (working copy) @@ -20,7 +20,8 @@ #define KSERVICEGROUP_H #include -#include "ksycocaentry.h" +#include +#include class KBuildServiceGroupFactory; @@ -66,6 +67,11 @@ typedef QList List; public: /** + * Constructs an invalid service object + */ + KServiceGroup(); + + /** * Construct a dummy servicegroup indexed with @p name. * @param name the name of the service group */ @@ -198,6 +204,29 @@ List entries(bool sorted = false); /** + * options for groupEntries and serviceEntries + */ + enum EntriesOption + { + NoOptions = 0x0, + SortEntries = 0x1, /*< sort items */ + ExcludeNoDisplay = 0x2, /*< exclude items marked "NoDisplay" */ + AllowSeparators = 0x4, /*< allow separator items to be included */ + SortByGenericName = 0x8 /*< sort by GenericName+Name instead of Name+GenericName */ + }; + Q_DECLARE_FLAGS(EntriesOptions, EntriesOption) + + /** + * subgroups for this service group + */ + QList groupEntries(EntriesOptions options = ExcludeNoDisplay); + + /** + * entries of this service group + */ + KService::List serviceEntries(EntriesOptions options = ExcludeNoDisplay); + + /** * Returns a non-empty string if the group is a special base group. * By default, "Settings/" is the kcontrol base group ("settings") * and "System/Screensavers/" is the screensavers base group ("screensavers"). @@ -243,11 +272,6 @@ */ static Ptr childGroup(const QString &parent); - /** - * This function parse attributes into menu - */ - void parseAttribute( const QString &item , bool &showEmptyMenu, bool &showInline, bool &showInlineHeader, bool & showInlineAlias ,int &inlineValue ); - protected: /** * @internal Index: kservicegroup_p.h =================================================================== --- kservicegroup_p.h (revision 720772) +++ kservicegroup_p.h (working copy) @@ -56,6 +56,14 @@ int childCount() const; + KServiceGroup::List + entries(KServiceGroup *group, bool sort, bool excludeNoDisplay, bool allowSeparators, bool sortByGenericName); + /** + * This function parse attributes into menu + */ + void parseAttribute( const QString &item , bool &showEmptyMenu, bool &showInline, bool &showInlineHeader, bool & showInlineAlias ,int &inlineValue ); + + bool m_bNoDisplay; bool m_bShowEmptyMenu; bool m_bShowInlineHeader;