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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*
extras.cpp - Extras
Copyright (C) 2004 Konrad Twardowski <kdtonline@poczta.onet.pl>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include "extras.h"
#include "miscutils.h"
#include "mmainwindow.h"
#include <tqdir.h>
#include <kdebug.h>
#include <kdesktopfile.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kmimetype.h>
#include <tdepopupmenu.h>
#include <kpushbutton.h>
#include <krun.h>
#include <kstandarddirs.h>
#include <kstringhandler.h>
Extras *Extras::_instance = 0;
// public
Extras::~Extras()
{
if (_idToFileMap)
delete _idToFileMap;
if (_menu)
delete _menu;
}
void Extras::createButton(TQWidget *parent)
{
_button = new KPushButton(parent, "KPushButton::_button");
_button->hide();
_button->setPopup(_menu);
MiscUtils::setHint(_button, i18n("More actions..."));
setAction(TQString::null, TQString::null); /* evil */
}
bool Extras::execAction() const
{
if (_fileToExecute.isNull())
return false; // error
if (!execFile())
{
MiscUtils::showRunErrorMessage(_fileToExecute);
return false; // error
}
return true; // ok
}
TQString Extras::getActionDescription() const
{
return
_description.isEmpty()
? TQString::null
// "Substitute characters at the middle of a string by \"...\""
: KStringHandler::csqueeze(_description, 100);
}
void Extras::setAction(const TQString &fileToExecute, const TQString &description, TQIconSet *icon)
{
_description = description;
_fileToExecute = fileToExecute;
if (_fileToExecute.isNull())
{
_button->setText(i18n("Select a command..."));
_button->setIconSet(ks_actions->getIcon(Action::Extras));
}
else
{
_button->setText(getActionDescription());
if (!icon)
_button->setIconSet(ks_actions->getIcon(Action::Extras));
else
_button->setIconSet(*icon);
}
}
// private
Extras::Extras()
: TQObject(ks_main, "Extras"),
_idToFileMap(0) // initialized in getIdToFileMap()
{
_menu = new TDEPopupMenu(0, "TDEPopupMenu::_menu");
connect(_menu, TQ_SIGNAL(aboutToShow()), TQ_SLOT(slotShowMenu()));
connect(_menu, TQ_SIGNAL(activated(int)), TQ_SLOT(slotActivated(int)));
}
// TODO: 2.0: recent items
void Extras::buildMenu(TDEPopupMenu *parentMenu, const TQString &subdir)
{
int id;
TQDir d(subdir);
TQFileInfo fi;
TQString
description,
icon,
mimeType,
name,
path;
TQStringList list = d.entryList(TQDir::DefaultFilter, TQDir::Name | TQDir::DirsFirst);
list.remove(".");
list.remove("..");
uint count = list.count();
for (uint i = 0; i < count; i++)
{
fi.setFile(d, list[i]);
path = fi.filePath();
// dir
if (fi.isDir())
{
TDEPopupMenu *submenu = new TDEPopupMenu(parentMenu, "TDEPopupMenu::submenu");
connect(submenu, TQ_SIGNAL(activated(int)), TQ_SLOT(slotActivated(int)));
KDesktopFile *desktopFile = new KDesktopFile(path + "/.directory", true);
if (desktopFile->getConfigState() != TDEConfigBase::NoAccess)
{
icon = desktopFile->readIcon();
if (icon.isEmpty())
icon = "folder";
name = desktopFile->readName();
if (name.isEmpty())
name = fi.baseName();
description = desktopFile->readComment();
// add item
parentMenu->insertItem(
SmallIcon(icon),
description.isEmpty() ? name : description,
submenu
);
}
else
{
// add item
parentMenu->insertItem(SmallIcon("folder"), fi.baseName(), submenu);
}
delete desktopFile;
// recurse
buildMenu(submenu, fi.filePath());
}
// file
else if (fi.isFile())
{
mimeType = KMimeType::findByPath(path)->name();
if (mimeType == "application/x-trash")
continue;
if (mimeType != "application/x-desktop")
{
// add item
id = parentMenu->insertItem(
KMimeType::pixmapForURL(KURL::fromPathOrURL(path)),
fi.baseName()
);
// map item
getIdToFileMap()->insert(id, path);
continue;
}
// application/x-desktop
KDesktopFile *desktopFile = new KDesktopFile(path, true);
if (desktopFile->getConfigState() != TDEConfigBase::NoAccess)
{
name = desktopFile->readName();
if (name.isEmpty())
name = desktopFile->readEntry("Exec");
description = desktopFile->readComment();
// add item
id = parentMenu->insertItem(
SmallIcon(desktopFile->readIcon()),
description.isEmpty()
? name
: (description + " (" + name + ")")
);
// map item
getIdToFileMap()->insert(id, path);
}
delete desktopFile;
}
}
}
bool Extras::execFile() const
{
if (_fileToExecute.isNull())
{
// kdDebug() << "Extras::execFile(): No file to execute" << endl;
return false;
}
// HACK: It seems that KRun::runURL sometimes does not work for hibernate :/ ?
TQFileInfo fi(_fileToExecute);
if (fi.fileName() == "hibernate.desktop") {
kdDebug() << "Extras::execFile(): Using hacked run..." << endl;
pid_t pid = KRun::runCommand("kfmclient exec \"" + _fileToExecute + "\"");
return pid;
}
else {
pid_t pid = KRun::runURL(
KURL::fromPathOrURL(_fileToExecute),
KMimeType::findByPath(_fileToExecute)->name()
);
return pid;
}
}
TQMap<int, TQString> *Extras::getIdToFileMap()
{
if (!_idToFileMap)
_idToFileMap = new TQMap<int, TQString>;
return _idToFileMap;
}
// public slots
void Extras::slotModify()
{
KMessageBox::information(
0,
MiscUtils::HTML(
i18n("Use context menu to add/edit/remove links.") +
"<ul>" +
"<li>" + i18n("Use <b>Context Menu</b> to create a new link to application") + "</li>" +
"<li>" + i18n("Use <b>Create New|Folder...</b> to create a new submenu") + "</li>" +
"<li>" + i18n("Use <b>Properties</b> to change icon, name, or comment") + "</li>" +
"</ul>"
),
i18n("Extras"), // title
"ModifyExtras" // config key
);
// launch Konqueror as an item editor
MiscUtils::runCommand("konqueror \"" + TDEGlobal::dirs()->saveLocation("data", "kshutdown/extras") + "\"");
}
// private slots
void Extras::slotActivated(int id)
{
TQMap<int, TQString>::iterator i = getIdToFileMap()->find(id);
if (i == getIdToFileMap()->end())
{
setAction(TQString::null, TQString::null);
KMessageBox::error(
0,
"Internal error!\nSelected menu item is broken.",
i18n("Extras")
);
}
else
{
setAction(i.data(), _menu->text(id), _menu->iconSet(id));
}
}
void Extras::slotShowMenu()
{
setAction(TQString::null, TQString::null);
_menu->clear(); // reset menu
getIdToFileMap()->clear(); // reset map
TQStringList extrasDirs(TDEGlobal::dirs()->findDirs("data", "kshutdown/extras"));
TQStringList::ConstIterator
begin = extrasDirs.begin(),
end = extrasDirs.end();
for (TQStringList::ConstIterator it = begin; it != end; ++it)
{
if ((it != begin) && (_menu->count() > 0))
_menu->insertSeparator();
buildMenu(_menu, *it);
}
}
#include "extras.moc"
|