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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/***************************************************************************
* Copyright (C) 2003-2004 Adam Geitgey <adam@rootnode.org> *
* Copyright (C) 2003 Hans Karlsson <karlsson.h@home.se> *
* *
* 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. *
***************************************************************************/
#include <qstring.h>
#include <qstringlist.h>
#include <qdir.h>
#include <kfiledialog.h>
#include <kcmdlineargs.h>
#include <fcntl.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <khelpmenu.h>
#include <qtooltip.h>
#include "themesdlg.h"
#include "karambainterface.h"
#include "karambaapp.h"
#include "dcopinterface_stub.h"
#include "karamba.h"
#include "superkarambasettings.h"
#include "qwidgetlist.h"
int KarambaApplication::fd = -1;
KarambaApplication::KarambaApplication() :
m_helpMenu(0), iface(0), themeListWindow(0), dcopIfaceStub(0),
karambaList(0), sysTrayIcon(0)
{
iface = new KarambaIface();
karambaList = new QObjectList();
// register ourselves as a dcop client
dcopClient()->registerAs(name());
dcopClient()->setDefaultObject(dcopIface()->objId());
}
KarambaApplication::~KarambaApplication()
{
delete iface;
delete karambaList;
delete themeListWindow;
delete dcopIfaceStub;
//delete m_helpMenu;
}
void KarambaApplication::initDcopStub(QCString app)
{
if(app.isEmpty())
app = dcopClient()->appId();
dcopIfaceStub = new dcopIface_stub(app, iface->objId());
}
QString KarambaApplication::getMainKaramba()
{
QStringList karambas = getKarambas();
QStringList::Iterator it;
for (it = karambas.begin(); it != karambas.end(); ++it)
{
if((*it).ascii() == dcopClient()->appId())
continue;
dcopIface_stub dcop((*it).ascii(), iface->objId());
if (dcop.isMainKaramba())
return *it;
}
return QString::null;
}
bool KarambaApplication::themeExists(QString pretty_name)
{
QWidgetList *list = QApplication::allWidgets();
QWidgetListIt it( *list ); // iterate over the widgets
QWidget * w;
while ( (w=it.current()) != 0 ) // for each widget...
{
++it;
if (QString(w->name()).startsWith("karamba"))
{
karamba* k = (karamba*) w;
if (k->getPrettyName() == pretty_name)
return true;
}
}
delete list; // delete the list, not the widgets
return false;
}
QStringList KarambaApplication::getKarambas()
{
QCStringList applst = dcopClient()->registeredApplications();
QCStringList::Iterator it;
QCString s;
QStringList result;
for (it = applst.begin(); (s = *it) != 0; ++it)
{
if (s.left(strlen(name())) == name())
result.append(s);
}
return result;
}
void KarambaApplication::checkSuperKarambaDir()
{
// Create ~/.superkaramba if necessary
QDir configDir(QDir::home().absPath() + "/.superkaramba");
if (!configDir.exists())
{
qWarning("~/.superkaramba doesn't exist");
if(!configDir.mkdir(QDir::home().absPath() + "/.superkaramba"))
{
qWarning("Couldn't create Directory ~/.superkaramba");
}
else
{
qWarning("created ~/.superkaramba");
}
}
}
void KarambaApplication::setUpSysTray(KAboutData* about)
{
//kdDebug() << k_funcinfo << endl;
KAction* action;
//Create theme list window.
//This will function as the main window for the tray icon
themeListWindow = new ThemesDlg();
//Set up systray icon
sysTrayIcon = new KSystemTray(themeListWindow);
KPopupMenu *menu = sysTrayIcon->contextMenu();
menu->insertItem(SmallIconSet("superkaramba"),
i18n("Hide System Tray Icon"), this,
SLOT(globalHideSysTray()));
menu->insertSeparator();
m_helpMenu = new KHelpMenu(themeListWindow, about);
action = KStdAction::help(m_helpMenu, SLOT(appHelpActivated()),
sysTrayIcon->actionCollection());
action->plug(menu);
action = KStdAction::aboutApp(m_helpMenu, SLOT(aboutApplication()),
sysTrayIcon->actionCollection());
action->plug(menu);
action = KStdAction::aboutKDE(m_helpMenu, SLOT(aboutKDE()),
sysTrayIcon->actionCollection());
action->plug(menu);
sysTrayIcon->setPixmap(sysTrayIcon->loadIcon("superkaramba"));
setToolTip();
if(SuperKarambaSettings::showSysTray())
sysTrayIcon->show();
else
sysTrayIcon->hide();
//Connect Systray icon's quit event
QObject::connect(sysTrayIcon, SIGNAL(quitSelected()),
this, SLOT(globalQuitSuperKaramba()));
}
void KarambaApplication::showKarambaMenuExtension(bool show)
{
QObject *k;
if(show)
{
for (k = karambaList->first(); k; k = karambaList->next())
{
((karamba*)k)->showMenuExtension();
}
}
else
{
for (k = karambaList->first(); k; k = karambaList->next())
{
((karamba*)k)->hideMenuExtension();
}
}
}
void KarambaApplication::setToolTip(const QString &tip)
{
QToolTip::remove(sysTrayIcon);
if(tip.isNull())
QToolTip::add(sysTrayIcon, i18n("SuperKaramba"));
else
QToolTip::add(sysTrayIcon, tip);
}
void KarambaApplication::buildToolTip()
{
if(!sysTrayIcon || !themeListWindow)
return;
QStringList list = themeListWindow->runningThemes();
if(list.isEmpty())
{
setToolTip();
return;
}
QString toolTip("<b><center>" + i18n("SuperKaramba") + "</center></b>");
toolTip += "<table width=300>";
bool firstRun = true;
for(QStringList::Iterator it = list.begin(); it != list.end(); ++it )
{
if(firstRun)
{
toolTip +=
"<tr><td align=right>" +
i18n("1 Running Theme:", "%n Running Themes:", list.count()) +
"</td><td align=left>" + (*it) + "</td></tr>";
firstRun = false;
}
else
{
toolTip += "<tr><td></td><td align=left>" + (*it) + "</td></tr>";
}
}
toolTip += "</table>";
setToolTip(toolTip);
}
void KarambaApplication::checkPreviousSession(KApplication &app,
QStringList &lst)
{
/******
Try to restore a previous session if applicable.
*/
if (app.isSessionRestored())
{
KConfig* config = app.sessionConfig();
config->setGroup("General Options");
QString restartThemes = config->readEntry("OpenThemes");
//Get themes that were running
lst = QStringList::split(QString(";"), restartThemes);
}
}
void KarambaApplication::checkCommandLine(KCmdLineArgs *args, QStringList &lst)
{
/******
Not a saved session - check for themes given on command line
*/
if(args->count() > 0)
{
for(int i = 0; i < (args->count()); i++)
{
if( args->arg(i) && *args->arg(i) )
{
KURL url = args->url(i);
lst.push_back(url.path());
}
}
}
}
bool KarambaApplication::startThemes(QStringList &lst)
{
bool result = false;
for(QStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
{
karamba *mainWin = 0;
mainWin = new karamba(*it , QString());
mainWin->show();
result = true;
}
buildToolTip();
return result;
}
void KarambaApplication::addKaramba(karamba* k, bool reloading)
{
if(!reloading && karambaApp->dcopStub())
{
int instance = karambaApp->dcopStub()->themeAdded(
karambaApp->dcopClient()->appId(), k->theme().file());
k->setInstance(instance);
}
karambaList->append(k);
}
void KarambaApplication::deleteKaramba(karamba* k, bool reloading)
{
if(!reloading && karambaApp->dcopStub())
karambaApp->dcopStub()->themeClosed(
karambaApp->dcopClient()->appId(), k->theme().file(), k->instance());
karambaList->removeRef(k);
}
bool KarambaApplication::hasKaramba(karamba* k)
{
return karambaList->containsRef(k) > 0;
}
// XXX: I guess this should be made with mutex/semaphores
// but this is good for now...
bool KarambaApplication::lockKaramba()
{
QString file = QDir::home().absPath() + "/.superkaramba/.lock";
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
fd = open(file.ascii(), O_CREAT | O_RDWR | O_TRUNC, mode);
if (fd < 0)
{
qWarning("Open failed in lock.");
return false;
}
//qDebug("lock %d", getpid());
if(lockf(fd, F_LOCK, 0))
{
qWarning("Lock failed.");
return false;
}
return true;
}
void KarambaApplication::unlockKaramba()
{
if(fd > 0)
{
lockf(fd, F_ULOCK, 0);
//qDebug("Unlock %d", getpid());
close(fd);
fd = -1;
}
}
void KarambaApplication::hideSysTray(bool hide)
{
//kdDebug() << k_funcinfo << endl;
if(hide)
{
if(sysTrayIcon)
{
KMessageBox::information(0,
i18n("<qt>Hiding the system tray icon will keep SuperKaramba running "
"in background. To show it again use the theme menu.</qt>"),
i18n("Hiding System Tray Icon"), "hideIcon");
sysTrayIcon->hide();
}
showKarambaMenuExtension();
}
else
{
showKarambaMenuExtension(false);
if(sysTrayIcon)
sysTrayIcon->show();
}
}
void KarambaApplication::showThemeDialog()
{
//kdDebug() << k_funcinfo << endl;
if(themeListWindow)
themeListWindow->show();
}
void KarambaApplication::quitSuperKaramba()
{
if(themeListWindow)
themeListWindow->saveUserAddedThemes();
qApp->closeAllWindows();
qApp->quit();
}
void KarambaApplication::globalQuitSuperKaramba()
{
QStringList apps = getKarambas();
QStringList::Iterator it;
for (it = apps.begin(); it != apps.end(); ++it)
{
dcopIface_stub dcop((*it).ascii(), dcopIface()->objId());
dcop.quit();
}
}
void KarambaApplication::globalShowThemeDialog()
{
QStringList apps = getKarambas();
QStringList::Iterator it;
for (it = apps.begin(); it != apps.end(); ++it)
{
dcopIface_stub dcop((*it).ascii(), dcopIface()->objId());
dcop.showThemeDialog();
}
}
void KarambaApplication::globalHideSysTray(bool hide)
{
//kdDebug() << k_funcinfo << endl;
QStringList apps = getKarambas();
QStringList::Iterator it;
SuperKarambaSettings::setShowSysTray(!hide);
SuperKarambaSettings::writeConfig();
for (it = apps.begin(); it != apps.end(); ++it)
{
dcopIface_stub dcop((*it).ascii(), dcopIface()->objId());
dcop.hideSystemTray(hide);
}
}
#include "karambaapp.moc"
|