themesdlg.cpp
00001 /* 00002 * Copyright (C) 2005 Petri Damstén <petri.damsten@iki.fi> 00003 * 00004 * This file is part of SuperKaramba. 00005 * 00006 * SuperKaramba is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * SuperKaramba is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with SuperKaramba; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 ****************************************************************************/ 00020 #include "karambaapp.h" 00021 #include "dcopinterface_stub.h" 00022 #include "karambainterface.h" 00023 #include "themesdlg.h" 00024 #include "themewidget.h" 00025 #include "kwidgetlistbox.h" 00026 #include "karamba.h" 00027 00028 #ifdef HAVE_CONFIG_H 00029 #include <config.h> 00030 #endif 00031 00032 #ifdef HAVE_KNEWSTUFF 00033 #include "sknewstuff.h" 00034 #endif 00035 00036 #include "superkarambasettings.h" 00037 #include <karchive.h> 00038 #include <kdebug.h> 00039 #include <kfiledialog.h> 00040 #include <kpushbutton.h> 00041 #include <kstandarddirs.h> 00042 #include <kapplication.h> 00043 #include <kiconloader.h> 00044 #include <klocale.h> 00045 #include <tqlineedit.h> 00046 #include <tqtable.h> 00047 #include <tqdir.h> 00048 #include <tqlabel.h> 00049 #include <tqcombobox.h> 00050 #include <tqptrlist.h> 00051 #include <kio/job.h> 00052 #include <kprotocolinfo.h> 00053 00054 ThemesDlg::ThemesDlg(TQWidget *parent, const char *name) 00055 : ThemesLayout(parent, name) 00056 { 00057 populateListbox(); 00058 #ifdef HAVE_KNEWSTUFF 00059 mNewStuff = 0; 00060 #endif 00061 } 00062 00063 ThemesDlg::~ThemesDlg() 00064 { 00065 //kdDebug() << k_funcinfo << endl; 00066 saveUserAddedThemes(); 00067 #ifdef HAVE_KNEWSTUFF 00068 if(mNewStuff) 00069 { 00070 delete mNewStuff; 00071 } 00072 #endif 00073 } 00074 00075 void ThemesDlg::saveUserAddedThemes() 00076 { 00077 KStandardDirs ksd; 00078 TQStringList t = themes(); 00079 TQStringList dirs = ksd.findDirs("data", TQString(kapp->name()) + "/themes"); 00080 TQStringList::Iterator it = t.begin(); 00081 bool remove; 00082 00083 while(it != t.end()) 00084 { 00085 remove = false; 00086 TQStringList::Iterator jtend( dirs.end() ); 00087 for(TQStringList::Iterator jt = dirs.begin(); jt != jtend; ++jt) 00088 { 00089 if(TQFileInfo(*it).dir().path() + "/" == *jt) 00090 { 00091 remove = true; 00092 break; 00093 } 00094 } 00095 if(remove) 00096 it = t.remove(it); 00097 else 00098 ++it; 00099 } 00100 SuperKarambaSettings::setUserAddedThemes(t); 00101 SuperKarambaSettings::writeConfig(); 00102 } 00103 00104 TQStringList ThemesDlg::themes() 00105 { 00106 TQStringList result; 00107 ThemeWidget* w; 00108 00109 for(uint i = 2; i < tableThemes->count(); ++i) 00110 { 00111 w = static_cast<ThemeWidget*>(tableThemes->item(i)); 00112 00113 result.append(w->themeFile()->file()); 00114 } 00115 return result; 00116 } 00117 00118 void ThemesDlg::populateListbox() 00119 { 00120 ThemeWidget* item; 00121 TQDir dir; 00122 TQStringList dirs; 00123 TQStringList t; 00124 KStandardDirs ksd; 00125 00126 tableThemes->clear(); 00127 00128 item = new ThemeWidget; 00129 item->icon->setPixmap(KGlobal::iconLoader()->loadIcon("knewstuff", 00130 KIcon::NoGroup, KIcon::SizeHuge)); 00131 item->setHeaderText(i18n("Get New Stuff")); 00132 item->setDescriptionText(i18n("Download new themes.")); 00133 00134 item->buttonGo->setText(i18n("New Stuff...")); 00135 #ifdef HAVE_KNEWSTUFF 00136 item->buttonGo->setEnabled(true); 00137 connect(item->buttonGo, TQT_SIGNAL(clicked()), 00138 this, TQT_SLOT(getNewStuff())); 00139 #else 00140 item->buttonGo->setEnabled(false); 00141 #endif 00142 tableThemes->insertItem(item); 00143 00144 item = new ThemeWidget; 00145 item->icon->setPixmap(KGlobal::iconLoader()->loadIcon("ksysguard", 00146 KIcon::NoGroup, KIcon::SizeHuge)); 00147 item->setHeaderText(i18n("Open Local Theme")); 00148 item->setDescriptionText(i18n("Add local theme to the list.")); 00149 item->buttonGo->setProperty("stdItem", 18); 00150 item->buttonGo->setText(i18n("Open...")); 00151 connect(item->buttonGo, TQT_SIGNAL(clicked()), 00152 this, TQT_SLOT(openLocalTheme())); 00153 tableThemes->insertItem(item); 00154 00155 dirs = ksd.findDirs("data", TQString(kapp->name()) + "/themes"); 00156 // Get custom dirs from config here? 00157 TQStringList::Iterator itend( dirs.end() ); 00158 for(TQStringList::Iterator it = dirs.begin(); it != itend; ++it ) 00159 { 00160 dir.setPath(*it); 00161 t = dir.entryList("*.skz; *.theme"); 00162 for(TQStringList::Iterator it = t.begin(); it != t.end(); ++it ) 00163 { 00164 item = new ThemeWidget(new ThemeFile(dir.filePath(*it))); 00165 tableThemes->insertItem(item); 00166 item->buttonGo->setText(i18n("Uninstall")); 00167 connect(item->buttonGo, TQT_SIGNAL(clicked()), 00168 this, TQT_SLOT(uninstall())); 00169 } 00170 } 00171 t = SuperKarambaSettings::userAddedThemes(); 00172 for(TQStringList::Iterator it = t.begin(); it != t.end(); ++it ) 00173 { 00174 ThemeFile* file = new ThemeFile(*it); 00175 00176 if(file->isValid()) 00177 { 00178 item = new ThemeWidget(file); 00179 tableThemes->insertItem(item); 00180 item->buttonGo->setText(i18n("Uninstall")); 00181 connect(item->buttonGo, TQT_SIGNAL(clicked()), 00182 this, TQT_SLOT(uninstall())); 00183 } 00184 else 00185 delete file; 00186 } 00187 tableThemes->setSelected(0); 00188 } 00189 00190 void ThemesDlg::addToDesktop() 00191 { 00192 ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->selectedItem()); 00193 if(w) 00194 { 00195 ThemeFile* tf = w->themeFile(); 00196 if(tf) 00197 { 00198 (new karamba(tf->file(), TQString()))->show(); 00199 } 00200 } 00201 } 00202 00203 void ThemesDlg::openLocalTheme() 00204 { 00205 TQStringList fileNames; 00206 fileNames = KFileDialog::getOpenFileNames(":<themes>", 00207 i18n("*.theme *.skz|Themes"), 00208 this, i18n("Open Themes")); 00209 for(TQStringList::Iterator it = fileNames.begin(); it != fileNames.end(); ++it) 00210 { 00211 ThemeFile file(*it); 00212 if(file.isValid()) 00213 (new karamba(*it, TQString()))->show(); 00214 } 00215 } 00216 00217 void ThemesDlg::getNewStuff() 00218 { 00219 #ifdef HAVE_KNEWSTUFF 00220 KConfig* config = KGlobal::config(); 00221 config->setGroup("KNewStuff"); 00222 config->writePathEntry("ProvidersUrl", 00223 TQString::fromLatin1("http://download.kde.org/khotnewstuff/karamba-providers.xml")); 00224 config->sync(); 00225 m_newStuffStatus = config->entryMap("KNewStuffStatus").keys(); 00226 //This check is b/c KNewStuff will download, throw an error, 00227 //and still have the entry in the config that it was successful 00228 configSanityCheck(); 00229 00230 if ( !mNewStuff ) 00231 { 00232 mNewStuff = new SKNewStuff(this); 00233 } 00234 mNewStuff->download(); 00235 #endif 00236 } 00237 00238 void ThemesDlg::selectionChanged(int index) 00239 { 00240 buttonAddToDesktop->setEnabled(index > 1); 00241 00242 for(uint i=2; i < tableThemes->count(); ++i) 00243 { 00244 ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(i)); 00245 w->showButton(false); 00246 } 00247 ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(index)); 00248 ThemeFile* themeFile = w->themeFile(); 00249 if(themeFile && themeFile->canUninstall()) 00250 w->showButton(true); 00251 } 00252 00253 int ThemesDlg::themeIndex(TQString file) 00254 { 00255 ThemeWidget* w; 00256 file = ThemeFile::canonicalFile(file); 00257 00258 for(uint i = 2; i < tableThemes->count(); ++i) 00259 { 00260 w = static_cast<ThemeWidget*>(tableThemes->item(i)); 00261 00262 if(w->themeFile()->file() == file) 00263 return i; 00264 } 00265 return -1; 00266 } 00267 00268 void ThemesDlg::addSkzThemeToDialog(const TQString &file) 00269 { 00270 kdDebug() << "addSkzThemeToDialog(): file = " << file << endl; 00271 addThemeToList(file); 00272 writeNewStuffConfig(file); 00273 } 00274 00275 void ThemesDlg::addThemeToDialog(const KArchiveDirectory *archiveDir, 00276 const TQString& destDir) 00277 { 00278 kdDebug() << "addThemeToDialog(): destDir = " << destDir << endl; 00279 TQStringList entries = archiveDir->entries(); 00280 00281 TQStringList::Iterator end( entries.end() ); 00282 for(TQStringList::Iterator it = entries.begin(); it != end; ++it) 00283 { 00284 if(archiveDir->entry(*it)->isDirectory()) 00285 { 00286 addThemeToDialog(static_cast<const KArchiveDirectory*>(archiveDir->entry(*it)), 00287 destDir + *it + "/"); 00288 } 00289 else 00290 { 00291 TQFileInfo fi(*it); 00292 if(fi.extension( FALSE ) == "theme") 00293 { 00294 addThemeToList(destDir + *it); 00295 writeNewStuffConfig(destDir); 00296 } 00297 } 00298 } 00299 } 00300 00301 void ThemesDlg::writeNewStuffConfig(const TQString &file) 00302 { 00303 #ifdef HAVE_KNEWSTUFF 00304 KConfig* config = KGlobal::config(); 00305 TQStringList keys = config->entryMap("KNewStuffStatus").keys(); 00306 00307 for(TQStringList::Iterator it = m_newStuffStatus.begin(); 00308 it != m_newStuffStatus.end(); ++it) 00309 { 00310 keys.remove(*it); 00311 } 00312 if(!keys.isEmpty()) 00313 { 00314 config->setGroup("KNewStuffNames"); 00315 config->writeEntry(file, keys[0]); 00316 config->sync(); 00317 } 00318 #endif 00319 } 00320 00321 void ThemesDlg::configSanityCheck() 00322 { 00323 #ifdef HAVE_KNEWSTUFF 00324 KConfig* config = KGlobal::config(); 00325 TQStringList statusKeys = config->entryMap("KNewStuffStatus").keys(); 00326 TQStringList nameKeys = config->entryMap("KNewStuffNames").keys(); 00327 TQStringList removeList; 00328 00329 for(TQStringList::Iterator it = statusKeys.begin(); 00330 it != statusKeys.end(); ++it) 00331 { 00332 TQString keyName(*it); 00333 bool removeKey = true; 00334 config->setGroup("KNewStuffNames"); 00335 for(TQStringList::Iterator it2 = nameKeys.begin(); 00336 it2 != nameKeys.end(); ++it2) 00337 { 00338 TQString tempName(config->readEntry(*it2)); 00339 if( tempName.compare(keyName) == 0) 00340 { 00341 removeKey = false; 00342 } 00343 00344 } 00345 if( removeKey ) 00346 { 00347 kdDebug() << "sanityCheck() deleting entry " << keyName << endl; 00348 config->setGroup("KNewStuffStatus"); 00349 config->deleteEntry( keyName ); 00350 } 00351 } 00352 config->sync(); 00353 #endif 00354 } 00355 00356 int ThemesDlg::addThemeToList(const TQString &file) 00357 { 00358 kdDebug() << "addThemeToList() file: " << file << endl; 00359 int i = themeIndex(file); 00360 if(i < 0) 00361 { 00362 ThemeWidget* item = new ThemeWidget(new ThemeFile(file)); 00363 00364 i = tableThemes->insertItem(item); 00365 item->buttonGo->setText(i18n("Uninstall")); 00366 connect(item->buttonGo, TQT_SIGNAL(clicked()), 00367 this, TQT_SLOT(uninstall())); 00368 } 00369 tableThemes->setSelected(i); 00370 return i; 00371 } 00372 00373 int ThemesDlg::addTheme(const TQString& , const TQString &file) 00374 { 00375 int i = addThemeToList(file); 00376 int result = -1; 00377 00378 ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(i)); 00379 if(w) 00380 result = w->addInstance(); 00381 karambaApp->buildToolTip(); 00382 return result; 00383 } 00384 00385 void ThemesDlg::removeTheme(const TQString&, const TQString& file, int instance) 00386 { 00387 int i = themeIndex(file); 00388 00389 ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(i)); 00390 if(w) 00391 w->removeInstance(instance); 00392 karambaApp->buildToolTip(); 00393 } 00394 00395 void ThemesDlg::search(const TQString&) 00396 { 00397 tableThemes->showItems(&filter, this); 00398 } 00399 00400 bool ThemesDlg::filter(int index, TQWidget* widget, void* data) 00401 { 00402 if(index < 2) 00403 return true; 00404 00405 ThemesDlg* dlg = static_cast<ThemesDlg*>(data); 00406 ThemeWidget* w = static_cast<ThemeWidget*>(widget); 00407 00408 if(dlg->comboShow->currentItem() == 1) // Running themes 00409 if(w->instances() == 0) 00410 return false; 00411 00412 TQString searchText = dlg->editSearch->text().lower(); 00413 if(searchText.isEmpty()) 00414 { 00415 return true; 00416 } 00417 else 00418 { 00419 if(w->themeName->text().lower().contains(searchText)) 00420 return true; 00421 if(w->description->text().lower().contains(searchText)) 00422 return true; 00423 } 00424 return false; 00425 } 00426 00427 bool ThemesDlg::isDownloaded( const TQString& path ) 00428 { 00429 kdDebug() << "isDownloaded path: " << path << endl; 00430 KConfig* config = KGlobal::config(); 00431 config->setGroup("KNewStuffNames"); 00432 return !config->readEntry(path).isEmpty(); 00433 } 00434 00435 void ThemesDlg::uninstall() 00436 { 00437 ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->selectedItem()); 00438 ThemeFile* tf = w->themeFile(); 00439 KURL trash("trash:/"); 00440 KURL theme(tf->file()); 00441 TQString tempPath(tf->path()); 00442 00443 karambaApp->dcopIface()->closeTheme(tf->name()); 00444 if(!KProtocolInfo::isKnownProtocol(trash)) 00445 trash = KGlobalSettings::trashPath(); 00446 00447 if(!tf->isZipTheme()) 00448 { 00449 kdDebug() << "encountered unpacked theme" << endl; 00450 //Don't move it to the trash if it is a local theme 00451 if(isDownloaded(tempPath)) 00452 { 00453 TQFileInfo remPath(tf->path()); 00454 TQDir remDir(remPath.dir()); 00455 remDir.cdUp(); 00456 kdDebug() << "moving " << remDir.path() << " to the trash" << endl; 00457 KIO::move(remDir.path(), trash); 00458 } 00459 tableThemes->removeItem(w); 00460 00461 //some themes have multiple .theme files 00462 //find all .themes that could be listed in the dialog for the directory removed 00463 TQPtrList<ThemeWidget> list; 00464 for(uint i = 2; i < tableThemes->count(); ++i) 00465 { 00466 ThemeWidget* tempW = static_cast<ThemeWidget*>(tableThemes->item(i)); 00467 ThemeFile* tempTf = tempW->themeFile(); 00468 if( tempTf->path().compare( tempPath ) == 0 ) 00469 { 00470 list.append( tempW ); 00471 } 00472 } 00473 ThemeWidget *twPtr; 00474 for ( twPtr = list.first(); twPtr; twPtr = list.next() ) 00475 { 00476 karambaApp->dcopIface()->closeTheme(twPtr->themeFile()->name()); 00477 tableThemes->removeItem( twPtr ); 00478 } 00479 #ifdef HAVE_KNEWSTUFF 00480 // Remove theme from KNewStuffStatus 00481 KConfig* config = KGlobal::config(); 00482 config->setGroup("KNewStuffNames"); 00483 TQString name = config->readEntry(tempPath); 00484 if(!name.isEmpty()) 00485 { 00486 kdDebug() << "removing " << tempPath << " under KNewStuffNames from superkarambarc" 00487 << endl; 00488 kapp->config()->deleteEntry(tempPath); 00489 config->setGroup("KNewStuffStatus"); 00490 kdDebug() << "removing " << name << " under KNewStuffStatus from superkarambarc" 00491 << endl; 00492 kapp->config()->deleteEntry(name); 00493 kapp->config()->sync(); 00494 } 00495 #endif 00496 00497 } 00498 else 00499 { 00500 kdDebug() << "encountered skz theme" << endl; 00501 if(isDownloaded(theme.path())) 00502 { 00503 TQFileInfo remPath(theme.path()); 00504 TQDir remDir(remPath.dir()); 00505 kdDebug() << "moving " << remDir.path() << " to the trash" << endl; 00506 KIO::move(remDir.path(), trash); 00507 } 00508 tableThemes->removeItem(w); 00509 #ifdef HAVE_KNEWSTUFF 00510 // Remove theme from KNewStuffStatus 00511 KConfig* config = KGlobal::config(); 00512 config->setGroup("KNewStuffNames"); 00513 TQString name = config->readEntry(theme.path()); 00514 if(!name.isEmpty()) 00515 { 00516 kdDebug() << "removing " << theme.path() << " from superkarambarc" << endl; 00517 kapp->config()->deleteEntry(theme.path()); 00518 config->setGroup("KNewStuffStatus"); 00519 kdDebug() << "removing " << name << " from superkarambarc" << endl; 00520 kapp->config()->deleteEntry(name); 00521 kapp->config()->sync(); 00522 } 00523 #endif 00524 } 00525 selectionChanged(tableThemes->selected()); 00526 } 00527 00528 TQStringList ThemesDlg::runningThemes() 00529 { 00530 TQStringList list; 00531 ThemeWidget* w; 00532 00533 for(uint i = 2; i < tableThemes->count(); ++i) 00534 { 00535 w = static_cast<ThemeWidget*>(tableThemes->item(i)); 00536 00537 if(w->instances() > 0) 00538 list.append(w->themeFile()->name()); 00539 } 00540 return list; 00541 } 00542 00543 #include "themesdlg.moc"