1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/**
* Copyright (C) 2005 Benjamin C Meyer (ben+kcmodulemenu at meyerhome dot net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kcmodulemenu.h"
#include <kapplication.h>
#include <kservicegroup.h>
#include <kdebug.h>
#include <qdict.h>
class KCModuleMenuPrivate {
public:
KCModuleMenuPrivate(){
}
QMap<QString, QValueList<MenuItem> > menus;
QString basePath;
};
KCModuleMenu::KCModuleMenu( const QString &menuName ) :
d( new KCModuleMenuPrivate )
{
kdDebug() << "MenuName: \"" << menuName << "\"." << endl;
// Make sure we can find the menu
KServiceGroup::Ptr serviceGroup = KServiceGroup::baseGroup( menuName );
if( !serviceGroup ){
kdDebug() << "Unable to load menu \"" << menuName <<
"\" from KServiceGroup." << endl;
return;
}
d->basePath = serviceGroup->relPath();
readMenu( d->basePath );
}
KCModuleMenu::~KCModuleMenu()
{
delete d;
}
void KCModuleMenu::readMenu( const QString &pathName )
{
KServiceGroup::Ptr group = KServiceGroup::group( pathName );
if ( !group || !group->isValid() )
return;
KServiceGroup::List list = group->entries( true, true );
if( list.isEmpty() )
return;
caption = group->caption();
QValueList<MenuItem> currentMenu;
for( KServiceGroup::List::ConstIterator it = list.begin();
it != list.end(); it++)
{
KSycocaEntry *entry = (*it);
if( addEntry(entry) ) {
KCModuleInfo module((KService*)entry);
append(module);
MenuItem infoItem(false);
infoItem.caption = this->deriveCaptionFromPath(entry->name());
infoItem.item = module;
currentMenu.append( infoItem );
}
if ( entry->isType(KST_KServiceGroup) ){
MenuItem menuItem(true);
menuItem.caption = this->deriveCaptionFromPath(entry->name());
menuItem.subMenu = entry->entryPath();
currentMenu.append( menuItem );
readMenu( entry->entryPath() );
}
}
d->menus.insert( pathName, currentMenu );
}
bool KCModuleMenu::addEntry( KSycocaEntry *entry ){
if( !entry->isType(KST_KService) )
return false;
KService *service = static_cast<KService*>( entry );
if ( !kapp->authorizeControlModule( service->menuId()) )
return false;
KCModuleInfo module( service );
if ( module.library().isEmpty() )
return false;
return true;
}
QValueList<KCModuleInfo> KCModuleMenu::modules( const QString &menuPath )
{
QValueList<KCModuleInfo> list;
QValueList<MenuItem> subMenu = menuList(menuPath);
QValueList<MenuItem>::iterator it;
for ( it = subMenu.begin(); it != subMenu.end(); ++it ){
if ( !(*it).menu )
list.append( (*it).item );
}
return list;
}
QStringList KCModuleMenu::submenus( const QString &menuPath )
{
QStringList list;
QValueList<MenuItem> subMenu = menuList(menuPath);
QValueList<MenuItem>::iterator it;
for ( it = subMenu.begin(); it != subMenu.end(); ++it ){
if ( (*it).menu )
list.append( (*it).subMenu );
}
return list;
}
QValueList<MenuItem> KCModuleMenu::menuList( const QString &menuPath )
{
if( menuPath.isEmpty() ) {
if( d->basePath.isEmpty())
return QValueList<MenuItem>();
else
return menuList( d->basePath );
}
return d->menus[menuPath];
}
/*
* Okay, I think there could be a much more elegant way of doing
* this... but I'm having a hell fo a time figuring it out.
*
* The purpose of this function is to take a menu path and turn it
* into a caption that we can put in a tab. Why do it this way? I
* don't know, you tell me. Before I started hacking this we used a
* radio control with two buttons (or so it seemed, I could be wrong)
* with General and Advanced in a ui.rc file.
*
* Now that we're using tabs, we no longer have that UI file giving us
* the names for the tabs, and since I didn't want to hard-code
* anything, and since KSycocaEntry stuff doesn't give you a nice way
* (that I noticed anyway) to figure out what your caption should be,
* I decided that cleverness is lost on this problem. So screw it,
* I'll just parse the stupid path and be done with it.
*
* This function is certainly nothing short of dull and boring and
* routine, but I figured that this might require a bit of explanation
* since it just seems kinda silly to do it this way to me. I guess I
* never know... I could be doing it the best way.
*
* "Michael D. Stemle, Jr." <[email protected]>
*/
QString KCModuleMenu::deriveCaptionFromPath( const QString &menuPath )
{
QStringList parts(QStringList::split("/",menuPath));
QString result("");
QStringList::Iterator it = parts.end(); // Start at the end
// Find the last non-empty string in the split.
for (; it != parts.begin(); --it) {
if (!((*it).isNull()) && !((*it).isEmpty())) {
result += *it;
return result;
}
}
}
|