karambaapp.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2003-2004 Adam Geitgey <adam@rootnode.org> * 00003 * Copyright (C) 2003 Hans Karlsson <karlsson.h@home.se> * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 ***************************************************************************/ 00010 00011 #include <tqstring.h> 00012 #include <tqstringlist.h> 00013 #include <tqdir.h> 00014 #include <tdefiledialog.h> 00015 #include <tdecmdlineargs.h> 00016 #include <fcntl.h> 00017 #include <tdelocale.h> 00018 #include <tdemessagebox.h> 00019 #include <kdebug.h> 00020 #include <khelpmenu.h> 00021 00022 #include <tqtooltip.h> 00023 00024 #include "themesdlg.h" 00025 #include "karambainterface.h" 00026 #include "karambaapp.h" 00027 #include "dcopinterface_stub.h" 00028 #include "karamba.h" 00029 #include "superkarambasettings.h" 00030 #include "tqwidgetlist.h" 00031 00032 int KarambaApplication::fd = -1; 00033 00034 KarambaApplication::KarambaApplication() : 00035 m_helpMenu(0), iface(0), themeListWindow(0), dcopIfaceStub(0), 00036 karambaList(0), sysTrayIcon(0) 00037 { 00038 iface = new KarambaIface(); 00039 karambaList = new TQObjectList(); 00040 // register ourselves as a dcop client 00041 dcopClient()->registerAs(name()); 00042 dcopClient()->setDefaultObject(dcopIface()->objId()); 00043 } 00044 00045 KarambaApplication::~KarambaApplication() 00046 { 00047 delete iface; 00048 delete karambaList; 00049 delete themeListWindow; 00050 delete dcopIfaceStub; 00051 //delete m_helpMenu; 00052 } 00053 00054 void KarambaApplication::initDcopStub(TQCString app) 00055 { 00056 if(app.isEmpty()) 00057 app = dcopClient()->appId(); 00058 dcopIfaceStub = new dcopIface_stub(app, iface->objId()); 00059 } 00060 00061 TQString KarambaApplication::getMainKaramba() 00062 { 00063 TQStringList karambas = getKarambas(); 00064 TQStringList::Iterator it; 00065 00066 for (it = karambas.begin(); it != karambas.end(); ++it) 00067 { 00068 if((*it).ascii() == dcopClient()->appId()) 00069 continue; 00070 dcopIface_stub dcop((*it).ascii(), iface->objId()); 00071 if (dcop.isMainKaramba()) 00072 return *it; 00073 } 00074 return TQString(); 00075 } 00076 00077 bool KarambaApplication::themeExists(TQString pretty_name) 00078 { 00079 TQWidgetList *list = TQApplication::allWidgets(); 00080 TQWidgetListIt it( *list ); // iterate over the widgets 00081 TQWidget * w; 00082 while ( (w=it.current()) != 0 ) // for each widget... 00083 { 00084 ++it; 00085 if (TQString(w->name()).startsWith("karamba")) 00086 { 00087 karamba* k = (karamba*) w; 00088 if (k->getPrettyName() == pretty_name) 00089 return true; 00090 } 00091 } 00092 delete list; // delete the list, not the widgets 00093 return false; 00094 } 00095 00096 TQStringList KarambaApplication::getKarambas() 00097 { 00098 QCStringList applst = dcopClient()->registeredApplications(); 00099 QCStringList::Iterator it; 00100 TQCString s; 00101 TQStringList result; 00102 00103 for (it = applst.begin(); (s = *it) != 0; ++it) 00104 { 00105 if (s.left(strlen(name())) == name()) 00106 result.append(s); 00107 } 00108 return result; 00109 } 00110 00111 void KarambaApplication::checkSuperKarambaDir() 00112 { 00113 // Create ~/.superkaramba if necessary 00114 TQDir configDir(TQDir::home().absPath() + "/.superkaramba"); 00115 if (!configDir.exists()) 00116 { 00117 tqWarning("~/.superkaramba doesn't exist"); 00118 if(!configDir.mkdir(TQDir::home().absPath() + "/.superkaramba")) 00119 { 00120 tqWarning("Couldn't create Directory ~/.superkaramba"); 00121 } 00122 else 00123 { 00124 tqWarning("created ~/.superkaramba"); 00125 } 00126 } 00127 } 00128 00129 void KarambaApplication::setUpSysTray(TDEAboutData* about) 00130 { 00131 //kdDebug() << k_funcinfo << endl; 00132 TDEAction* action; 00133 00134 //Create theme list window. 00135 //This will function as the main window for the tray icon 00136 themeListWindow = new ThemesDlg(); 00137 00138 //Set up systray icon 00139 sysTrayIcon = new KSystemTray(themeListWindow); 00140 00141 TDEPopupMenu *menu = sysTrayIcon->contextMenu(); 00142 menu->insertItem(SmallIconSet("superkaramba"), 00143 i18n("Hide System Tray Icon"), this, 00144 TQT_SLOT(globalHideSysTray())); 00145 menu->insertSeparator(); 00146 00147 m_helpMenu = new KHelpMenu(themeListWindow, about); 00148 action = KStdAction::help(m_helpMenu, TQT_SLOT(appHelpActivated()), 00149 sysTrayIcon->actionCollection()); 00150 action->plug(menu); 00151 action = KStdAction::aboutApp(m_helpMenu, TQT_SLOT(aboutApplication()), 00152 sysTrayIcon->actionCollection()); 00153 action->plug(menu); 00154 action = KStdAction::aboutKDE(m_helpMenu, TQT_SLOT(aboutKDE()), 00155 sysTrayIcon->actionCollection()); 00156 action->plug(menu); 00157 00158 sysTrayIcon->setPixmap(sysTrayIcon->loadIcon("superkaramba")); 00159 setToolTip(); 00160 00161 if(SuperKarambaSettings::showSysTray()) 00162 sysTrayIcon->show(); 00163 else 00164 sysTrayIcon->hide(); 00165 00166 //Connect Systray icon's quit event 00167 TQObject::connect(sysTrayIcon, TQT_SIGNAL(quitSelected()), 00168 this, TQT_SLOT(globalQuitSuperKaramba())); 00169 } 00170 00171 void KarambaApplication::showKarambaMenuExtension(bool show) 00172 { 00173 TQObject *k; 00174 00175 if(show) 00176 { 00177 for (k = karambaList->first(); k; k = karambaList->next()) 00178 { 00179 ((karamba*)k)->showMenuExtension(); 00180 } 00181 } 00182 else 00183 { 00184 for (k = karambaList->first(); k; k = karambaList->next()) 00185 { 00186 ((karamba*)k)->hideMenuExtension(); 00187 } 00188 } 00189 } 00190 00191 void KarambaApplication::setToolTip(const TQString &tip) 00192 { 00193 TQToolTip::remove(sysTrayIcon); 00194 if(tip.isNull()) 00195 TQToolTip::add(sysTrayIcon, i18n("SuperKaramba")); 00196 else 00197 TQToolTip::add(sysTrayIcon, tip); 00198 } 00199 00200 void KarambaApplication::buildToolTip() 00201 { 00202 if(!sysTrayIcon || !themeListWindow) 00203 return; 00204 00205 TQStringList list = themeListWindow->runningThemes(); 00206 00207 if(list.isEmpty()) 00208 { 00209 setToolTip(); 00210 return; 00211 } 00212 00213 TQString toolTip("<b><center>" + i18n("SuperKaramba") + "</center></b>"); 00214 toolTip += "<table width=300>"; 00215 00216 bool firstRun = true; 00217 for(TQStringList::Iterator it = list.begin(); it != list.end(); ++it ) 00218 { 00219 if(firstRun) 00220 { 00221 toolTip += 00222 "<tr><td align=right>" + 00223 i18n("1 Running Theme:", "%n Running Themes:", list.count()) + 00224 "</td><td align=left>" + (*it) + "</td></tr>"; 00225 firstRun = false; 00226 } 00227 else 00228 { 00229 toolTip += "<tr><td></td><td align=left>" + (*it) + "</td></tr>"; 00230 } 00231 } 00232 00233 toolTip += "</table>"; 00234 00235 setToolTip(toolTip); 00236 } 00237 00238 void KarambaApplication::checkPreviousSession(TDEApplication &app, 00239 TQStringList &lst) 00240 { 00241 /****** 00242 Try to restore a previous session if applicable. 00243 */ 00244 if (app.isSessionRestored()) 00245 { 00246 TDEConfig* config = app.sessionConfig(); 00247 config->setGroup("General Options"); 00248 TQString restartThemes = config->readEntry("OpenThemes"); 00249 00250 //Get themes that were running 00251 lst = TQStringList::split(TQString(";"), restartThemes); 00252 } 00253 } 00254 00255 void KarambaApplication::checkCommandLine(TDECmdLineArgs *args, TQStringList &lst) 00256 { 00257 /****** 00258 Not a saved session - check for themes given on command line 00259 */ 00260 if(args->count() > 0) 00261 { 00262 for(int i = 0; i < (args->count()); i++) 00263 { 00264 if( args->arg(i) && *args->arg(i) ) 00265 { 00266 KURL url = args->url(i); 00267 00268 lst.push_back(url.path()); 00269 } 00270 } 00271 } 00272 } 00273 00274 bool KarambaApplication::startThemes(TQStringList &lst) 00275 { 00276 bool result = false; 00277 00278 for(TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) 00279 { 00280 karamba *mainWin = 0; 00281 00282 mainWin = new karamba(*it , TQString()); 00283 mainWin->show(); 00284 result = true; 00285 } 00286 00287 buildToolTip(); 00288 return result; 00289 } 00290 00291 void KarambaApplication::addKaramba(karamba* k, bool reloading) 00292 { 00293 if(!reloading && karambaApp->dcopStub()) 00294 { 00295 int instance = karambaApp->dcopStub()->themeAdded( 00296 karambaApp->dcopClient()->appId(), k->theme().file()); 00297 k->setInstance(instance); 00298 } 00299 karambaList->append(TQT_TQOBJECT(k)); 00300 } 00301 00302 void KarambaApplication::deleteKaramba(karamba* k, bool reloading) 00303 { 00304 if(!reloading && karambaApp->dcopStub()) 00305 karambaApp->dcopStub()->themeClosed( 00306 karambaApp->dcopClient()->appId(), k->theme().file(), k->instance()); 00307 karambaList->removeRef(TQT_TQOBJECT(k)); 00308 } 00309 00310 bool KarambaApplication::hasKaramba(karamba* k) 00311 { 00312 return karambaList->containsRef(TQT_TQOBJECT(k)) > 0; 00313 } 00314 00315 // XXX: I guess this should be made with mutex/semaphores 00316 // but this is good for now... 00317 00318 bool KarambaApplication::lockKaramba() 00319 { 00320 TQString file = TQDir::home().absPath() + "/.superkaramba/.lock"; 00321 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; 00322 00323 fd = open(file.ascii(), O_CREAT | O_RDWR | O_TRUNC, mode); 00324 if (fd < 0) 00325 { 00326 tqWarning("Open failed in lock."); 00327 return false; 00328 } 00329 //tqDebug("lock %d", getpid()); 00330 if(lockf(fd, F_LOCK, 0)) 00331 { 00332 tqWarning("Lock failed."); 00333 return false; 00334 } 00335 return true; 00336 } 00337 00338 void KarambaApplication::unlockKaramba() 00339 { 00340 if(fd > 0) 00341 { 00342 lockf(fd, F_ULOCK, 0); 00343 //tqDebug("Unlock %d", getpid()); 00344 close(fd); 00345 fd = -1; 00346 } 00347 } 00348 00349 void KarambaApplication::hideSysTray(bool hide) 00350 { 00351 //kdDebug() << k_funcinfo << endl; 00352 if(hide) 00353 { 00354 if(sysTrayIcon) 00355 { 00356 KMessageBox::information(0, 00357 i18n("<qt>Hiding the system tray icon will keep SuperKaramba running " 00358 "in background. To show it again use the theme menu.</qt>"), 00359 i18n("Hiding System Tray Icon"), "hideIcon"); 00360 sysTrayIcon->hide(); 00361 } 00362 showKarambaMenuExtension(); 00363 } 00364 else 00365 { 00366 showKarambaMenuExtension(false); 00367 if(sysTrayIcon) 00368 sysTrayIcon->show(); 00369 } 00370 } 00371 00372 void KarambaApplication::showThemeDialog() 00373 { 00374 //kdDebug() << k_funcinfo << endl; 00375 if(themeListWindow) 00376 themeListWindow->show(); 00377 } 00378 00379 void KarambaApplication::quitSuperKaramba() 00380 { 00381 if(themeListWindow) 00382 themeListWindow->saveUserAddedThemes(); 00383 tqApp->closeAllWindows(); 00384 tqApp->quit(); 00385 } 00386 00387 void KarambaApplication::globalQuitSuperKaramba() 00388 { 00389 TQStringList apps = getKarambas(); 00390 TQStringList::Iterator it; 00391 00392 for (it = apps.begin(); it != apps.end(); ++it) 00393 { 00394 dcopIface_stub dcop((*it).ascii(), dcopIface()->objId()); 00395 dcop.quit(); 00396 } 00397 } 00398 00399 void KarambaApplication::globalShowThemeDialog() 00400 { 00401 TQStringList apps = getKarambas(); 00402 TQStringList::Iterator it; 00403 00404 for (it = apps.begin(); it != apps.end(); ++it) 00405 { 00406 dcopIface_stub dcop((*it).ascii(), dcopIface()->objId()); 00407 dcop.showThemeDialog(); 00408 } 00409 } 00410 00411 void KarambaApplication::globalHideSysTray(bool hide) 00412 { 00413 //kdDebug() << k_funcinfo << endl; 00414 TQStringList apps = getKarambas(); 00415 TQStringList::Iterator it; 00416 00417 SuperKarambaSettings::setShowSysTray(!hide); 00418 SuperKarambaSettings::writeConfig(); 00419 00420 for (it = apps.begin(); it != apps.end(); ++it) 00421 { 00422 dcopIface_stub dcop((*it).ascii(), dcopIface()->objId()); 00423 dcop.hideSystemTray(hide); 00424 } 00425 } 00426 00427 #include "karambaapp.moc"