• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
themesdlg.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Petri Damst� <petri.damsten@iki.fi>
3  *
4  * This file is part of SuperKaramba.
5  *
6  * SuperKaramba is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * SuperKaramba is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with SuperKaramba; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  ****************************************************************************/
20 #include "themesdlg.h"
21 
22 #include "karambaapp.h"
23 #include "themewidget.h"
24 #include "kwidgetlistbox.h"
25 #include "karamba.h"
26 #include "superkarambasettings.h"
27 
28 #include <KArchive>
29 #include <KFileDialog>
30 #include <KStandardDirs>
31 #include <KIconLoader>
32 #include <KIO/CopyJob>
33 #include <KDirWatch>
34 #include <KTar>
35 #include <KIO/NetAccess>
36 #include <KRun>
37 
38 ThemesDlg::ThemesDlg(QWidget *parent, const char *name)
39  : QDialog(parent),
40  m_newStuffInitialized(false)
41 {
42  setupUi(this);
43  setObjectName( QLatin1String( name ) );
44 
45  populateListbox();
46 
47  connect(buttonAddToDesktop, SIGNAL(clicked()), this, SLOT(addToDesktop()));
48  connect(tableThemes, SIGNAL(selected(int)), this, SLOT(selectionChanged(int)));
49  connect(tableThemes, SIGNAL(itemDropped(QPoint,ThemeWidget*)), this, SLOT(addToDesktop(QPoint,ThemeWidget*)));
50  connect(editSearch, SIGNAL(textChanged(QString)), this, SLOT(search(QString)));
51  connect(comboShow, SIGNAL(activated(QString)), this, SLOT(search(QString)));
52 }
53 
54 ThemesDlg::~ThemesDlg()
55 {
56  //kDebug() ;
57  saveUserAddedThemes();
58 }
59 
60 void ThemesDlg::saveUserAddedThemes()
61 {
62  KStandardDirs ksd;
63  QStringList t = themes();
64  QStringList dirs = ksd.findDirs("data", QString(kapp->objectName()) + "/themes");
65  QStringList::Iterator it = t.begin();
66  bool remove;
67 
68  while (it != t.end()) {
69  remove = false;
70  QStringList::Iterator jtend(dirs.end());
71  for (QStringList::Iterator jt = dirs.begin(); jt != jtend; ++jt) {
72  if (QString(QFileInfo(*it).dir().path() + '/') == *jt) {
73  remove = true;
74  break;
75  }
76  }
77  if (remove)
78  it = t.erase(it);
79  else
80  ++it;
81  }
82  SuperKarambaSettings::setUserAddedThemes(t);
83  SuperKarambaSettings::self()->writeConfig();
84 }
85 
86 QStringList ThemesDlg::themes()
87 {
88  QStringList result;
89  ThemeWidget* w;
90 
91  for (uint i = 2; i < tableThemes->count(); ++i) {
92  w = (ThemeWidget*)(tableThemes->item(i));
93 
94  result.append(w->themeFile()->file());
95  }
96  return result;
97 }
98 
99 void ThemesDlg::populateListbox()
100 {
101  ThemeWidget* item;
102  QDir dir;
103  QStringList dirs;
104  QStringList t;
105  KStandardDirs ksd;
106 
107  tableThemes->clear();
108 
109  item = new ThemeWidget;
110 
111  item->iconLabel->setPixmap(KIconLoader::global()->loadIcon("get-hot-new-stuff",
112  KIconLoader::NoGroup, KIconLoader::SizeHuge));
113  item->setHeaderText(i18n("Get New Stuff"));
114  item->setDescriptionText(i18n("Download new themes."));
115 
116  item->buttonGo->setText(i18n("New Stuff..."));
117  item->buttonGo->setEnabled(true);
118  QObject::connect(item->buttonGo, SIGNAL(clicked()),
119  this, SLOT(getNewStuff()));
120 
121  tableThemes->insertItem((QWidget*)(item));
122 
123  item = new ThemeWidget;
124  item->iconLabel->setPixmap(KIconLoader::global()->loadIcon("document-open",
125  KIconLoader::NoGroup, KIconLoader::SizeHuge));
126  item->setHeaderText(i18n("Open Local Theme"));
127  item->setDescriptionText(i18n("Add local theme to the list."));
128  item->buttonGo->setProperty("stdItem", 18);
129  item->buttonGo->setText(i18nc("Open theme button", "Open..."));
130  QObject::connect((QObject*)(item->buttonGo), SIGNAL(clicked()),
131  (QObject*)(this), SLOT(openLocalTheme()));
132  tableThemes->insertItem((QWidget*)item);
133 
134  dirs = ksd.findDirs("data", QString(kapp->objectName()) + "/themes");
135  // Get custom dirs from config here?
136  QStringList::Iterator itend(dirs.end());
137  for (QStringList::Iterator it = dirs.begin(); it != itend; ++it) {
138  QStringList types;
139  types << "*.skz" << "*.theme";
140 
141  dir.setPath(*it);
142 
143  t = dir.entryList(types);
144  for (QStringList::Iterator it = t.begin(); it != t.end(); ++it) {
145  item = new ThemeWidget(new ThemeFile(dir.filePath(*it)));
146  tableThemes->insertItem((QWidget*)item);
147  item->buttonGo->setText(i18n("Uninstall"));
148  QObject::connect((QObject*)item->buttonGo, SIGNAL(clicked()),
149  (QObject*)this, SLOT(uninstall()));
150  }
151  }
152  t = SuperKarambaSettings::userAddedThemes();
153  for (QStringList::Iterator it = t.begin(); it != t.end(); ++it) {
154  ThemeFile* file = new ThemeFile(*it);
155 
156  if (file->isValid()) {
157  item = new ThemeWidget(file);
158  tableThemes->insertItem((QWidget*)item);
159  item->buttonGo->setText(i18n("Uninstall"));
160  QObject::connect((QObject*)item->buttonGo, SIGNAL(clicked()),
161  (QObject*)this, SLOT(uninstall()));
162  } else
163  delete file;
164  }
165  tableThemes->setSelected(0);
166 }
167 
168 void ThemesDlg::addToDesktop()
169 {
170  ThemeWidget* w = (ThemeWidget*)(tableThemes->selectedItem());
171  if (w) {
172  ThemeFile* tf = w->themeFile();
173  if (tf) {
174  new Karamba(tf->file());
175  }
176  }
177 }
178 
179 void ThemesDlg::addToDesktop(QPoint pos, ThemeWidget* w)
180 {
181  if (w) {
182  ThemeFile* tf = w->themeFile();
183  if (tf) {
184  new Karamba(tf->file(), 0, -1, false, pos);
185  }
186  }
187 }
188 
189 void ThemesDlg::openLocalTheme()
190 {
191  QStringList fileNames;
192  fileNames = KFileDialog::getOpenFileNames(KUrl(),
193  i18n("*.theme *.skz|Themes"),
194  (QWidget*)this, i18n("Open Themes"));
195 
196  for (QStringList::Iterator it = fileNames.begin(); it != fileNames.end(); ++it) {
197  ThemeFile file(*it);
198  if (file.isValid()) {
199  new Karamba(*it);
200  }
201  }
202 }
203 
204 void ThemesDlg::getNewStuff()
205 {
206  if (!m_newStuffInitialized) {
207  KDirWatch *dirWatch = KDirWatch::self();
208  connect(dirWatch, SIGNAL(created(QString)),
209  SLOT(installNewTheme(QString)));
210 
211  QString destDir = KStandardDirs::locateLocal("appdata", "themes/");
212  dirWatch->addDir(destDir, KDirWatch::WatchFiles);
213 
214  m_newStuffInitialized = true;
215  }
216 
217  KNS::Engine::download();
218 }
219 
220 void ThemesDlg::installNewTheme(const QString &newTheme)
221 {
222  // The created signal is already emitted before the file is
223  // completely written, so wait until the new theme exists
224  KMimeType::Ptr result;
225  do {
226  result = KMimeType::findByUrl(newTheme);
227  } while (result->name() == "application/x-zerosize");
228 
229  if (result->name() == "application/x-gzip" ||
230  result->name() == "application/x-compressed-tar" ||
231  result->name() == "application/x-bzip" ||
232  result->name() == "application/x-bzip" ||
233  result->name() == "application/x-bzip-compressed-tar" ||
234  result->name() == "application/x-bzip-compressed-tar2" ||
235  result->name() == "application/x-tar" ||
236  result->name() == "application/x-tarz") {
237 
238  kDebug() << "SKNewStuff::install() gzip/bzip2 mimetype encountered" << endl;
239 
240  KTar archive(newTheme);
241  if (!archive.open(QIODevice::ReadOnly)) {
242  return;
243  }
244 
245  QString destDir = KStandardDirs::locateLocal("appdata", "themes/");
246 
247  const KArchiveDirectory *archiveDir = archive.directory();
248  archiveDir->copyTo(destDir);
249  //Add the theme to the Theme Dialog
250  addThemeToDialog(archiveDir, destDir);
251  archive.close();
252  } else if (result->name() == "application/zip" ||
253  result->name() == "application/x-superkaramba") {
254 
255  kDebug() << "SKNewStuff::install() zip mimetype encountered" << endl;
256  //TODO: write a routine to check if this is a valid .skz file
257  //otherwise we need to unpack it like it is an old theme that was packaged
258  //as a .zip instead of .bz2 or .tar.gz
259 
260  //Add the skz theme to the Theme Dialog
261  addSkzThemeToDialog(newTheme);
262  } else if (result->name() == "plain/text") {
263  kDebug() << "SKNewStuff::install() plain text" << endl;
264  } else if (result->name() == "text/html" ||
265  result->name() == "application/x-php") {
266 
267  kDebug() << "SKNewStuff::install() text/html" << endl;
268  KRun::runUrl(newTheme, "text/html", 0);
269  } else {
270  kDebug() << "SKNewStuff::install() Error no compatible mimetype encountered to install" << endl;
271  return;
272  }
273 }
274 
275 void ThemesDlg::selectionChanged(int index)
276 {
277  buttonAddToDesktop->setEnabled(index > 1);
278 
279  for (uint i = 2; i < tableThemes->count(); ++i) {
280  ThemeWidget* w = (ThemeWidget*)(tableThemes->item(i));
281  w->showButton(false);
282  }
283  ThemeWidget* w = (ThemeWidget*)(tableThemes->item(index));
284  ThemeFile* themeFile = w->themeFile();
285  if (themeFile && themeFile->canUninstall())
286  w->showButton(true);
287 }
288 
289 int ThemesDlg::themeIndex(const QString &file)
290 {
291  ThemeWidget* w;
292  QString canonicalFile = ThemeFile::canonicalFile(file);
293 
294  for (uint i = 2; i < tableThemes->count(); ++i) {
295  w = (ThemeWidget*)(tableThemes->item(i));
296 
297  if (w->themeFile()->file() == canonicalFile)
298  return i;
299  }
300  return -1;
301 }
302 
303 void ThemesDlg::addSkzThemeToDialog(const QString &file)
304 {
305  //kDebug() << "addSkzThemeToDialog(): file = " << file ;
306  addThemeToList(file);
307 }
308 
309 void ThemesDlg::addThemeToDialog(const KArchiveDirectory *archiveDir,
310  const QString& destDir)
311 {
312  kDebug() << "addThemeToDialog(): destDir = " << destDir ;
313  QStringList entries = archiveDir->entries();
314 
315  QStringList::Iterator end(entries.end());
316  for (QStringList::Iterator it = entries.begin(); it != end; ++it) {
317  if (archiveDir->entry(*it)->isDirectory()) {
318  addThemeToDialog(static_cast<const KArchiveDirectory*>(archiveDir->entry(*it)),
319  destDir + *it + '/');
320  } else {
321  QFileInfo fi(*it);
322  if (fi.suffix() == "theme") {
323  addThemeToList(destDir + *it);
324  }
325  }
326  }
327 }
328 
329 int ThemesDlg::addThemeToList(const QString &file)
330 {
331  kDebug() << "addThemeToList() file: " << file ;
332  int i = themeIndex(file);
333  if (i < 0) {
334  ThemeWidget* item = new ThemeWidget(new ThemeFile(file));
335 
336  i = tableThemes->insertItem((QWidget*)item);
337  item->buttonGo->setText(i18n("Uninstall"));
338  QObject::connect((QObject*)item->buttonGo, SIGNAL(clicked()),
339  (QObject*)this, SLOT(uninstall()));
340  }
341  tableThemes->setSelected(i);
342  return i;
343 }
344 
345 int ThemesDlg::addTheme(const QString& , const QString &file)
346 {
347  int i = addThemeToList(file);
348  int result = -1;
349 
350  ThemeWidget* w = (ThemeWidget*)(tableThemes->item(i));
351  if (w)
352  result = w->addInstance();
353 
354  return result;
355 }
356 
357 void ThemesDlg::removeTheme(const QString&, const QString& file, int instance)
358 {
359  int i = themeIndex(file);
360 
361  ThemeWidget* w = (ThemeWidget*)(tableThemes->item(i));
362  if (w) {
363  w->removeInstance(instance);
364  }
365 
366 }
367 
368 void ThemesDlg::search(const QString&)
369 {
370  tableThemes->showItems(&filter, this);
371 }
372 
373 bool ThemesDlg::filter(int index, QWidget* widget, void* data)
374 {
375  if (index < 2)
376  return true;
377 
378  ThemesDlg* dlg = (ThemesDlg*)(data);
379  ThemeWidget* w = (ThemeWidget*)(widget);
380 
381  if (dlg->comboShow->currentIndex() == 1) // Running themes
382  if (w->instances() == 0)
383  return false;
384 
385  QString searchText = dlg->editSearch->text().toLower();
386  if (searchText.isEmpty()) {
387  return true;
388  } else {
389  if (w->themeName->text().toLower().contains(searchText))
390  return true;
391  if (w->description->text().toLower().contains(searchText))
392  return true;
393  }
394  return false;
395 }
396 
397 bool ThemesDlg::isDownloaded(const QString& path)
398 {
399  kDebug() << "isDownloaded path: " << path ;
400  KSharedConfigPtr config = KGlobal::config();
401  return !config->group("KNewStuffNames").readEntry(path, QString()).isEmpty();
402 }
403 
404 void ThemesDlg::uninstall()
405 {
406  ThemeWidget* w = (ThemeWidget*)(tableThemes->selectedItem());
407  ThemeFile* tf = w->themeFile();
408  KUrl theme(tf->file());
409  QString tempPath(tf->path());
410 
411  karambaApp->closeTheme(tf->name());
412 
413  if (!tf->isZipTheme()) {
414  kDebug() << "encountered unpacked theme" ;
415  //Don't move it to the trash if it is a local theme
416  if (isDownloaded(tempPath)) {
417  QFileInfo remPath(tf->path());
418  QDir remDir(remPath.dir());
419  remDir.cdUp();
420  kDebug() << "moving " << remDir.path() << " to the trash" ;
421  KIO::trash(remDir.path());
422  }
423  tableThemes->removeItem((QWidget*)w);
424 
425  //some themes have multiple .theme files
426  //find all .themes that could be listed in the dialog for the directory removed
427  QList<ThemeWidget*> list;
428  for (uint i = 2; i < tableThemes->count(); ++i) {
429  ThemeWidget* tempW = (ThemeWidget*)(tableThemes->item(i));
430  ThemeFile* tempTf = tempW->themeFile();
431  if (tempTf->path().compare(tempPath) == 0) {
432  list.append(tempW);
433  }
434  }
435  foreach (ThemeWidget *twPtr, list) {
436  karambaApp->closeTheme(twPtr->themeFile()->name());
437  tableThemes->removeItem((QWidget*)twPtr);
438  }
439  // Remove theme from KNewStuffStatus
440  KSharedConfigPtr config = KGlobal::config();
441  QString name = config->group("KNewStuffNames").readEntry(tempPath, QString());
442  if (!name.isEmpty()) {
443  // kapp-config() -> KGlobal::config()
444  kDebug() << "removing " << tempPath << " under KNewStuffNames from superkarambarc"
445  << endl;
446  KGlobal::config()->group("KNewStuffNames").deleteEntry(tempPath);
447  kDebug() << "removing " << name << " under KNewStuffStatus from superkarambarc"
448  << endl;
449  KGlobal::config()->group("KNewStuffStatus").deleteEntry(name);
450  KGlobal::config()->sync();
451  }
452  } else {
453  kDebug() << "encountered skz theme" ;
454  if (isDownloaded(theme.path())) {
455  QFileInfo remPath(theme.path());
456  QDir remDir(remPath.dir());
457  kDebug() << "moving " << remDir.path() << " to the trash" ;
458  KIO::trash(remDir.path());
459  }
460  tableThemes->removeItem((QWidget*)w);
461  // Remove theme from KNewStuffStatus
462  KSharedConfigPtr config = KGlobal::config();
463  QString name = config->group("KNewStuffNames").readEntry(theme.path(), QString());
464  if (!name.isEmpty()) {
465  kDebug() << "removing " << theme.path() << " from superkarambarc" ;
466  KGlobal::config()->group("KNewStuffNames").deleteEntry(theme.path());
467  kDebug() << "removing " << name << " from superkarambarc" ;
468  KGlobal::config()->group("KNewStuffStatus").deleteEntry(name);
469  KGlobal::config()->sync();
470  }
471  }
472  selectionChanged(tableThemes->selected());
473 }
474 
475 QStringList ThemesDlg::runningThemes()
476 {
477  QStringList list;
478  ThemeWidget* w;
479 
480  for (uint i = 2; i < tableThemes->count(); ++i) {
481  w = (ThemeWidget*)(tableThemes->item(i));
482 
483  if (w->instances() > 0)
484  list.append(w->themeFile()->name());
485  }
486  return list;
487 }
488 
489 #include "themesdlg.moc"
ThemesDlg::uninstall
virtual void uninstall()
Definition: themesdlg.cpp:404
ThemesDlg::addToDesktop
virtual void addToDesktop()
Definition: themesdlg.cpp:168
superkarambasettings.h
ThemeFile
Definition: themefile.h:41
QDialog
ThemeFile::file
const QString & file() const
Definition: themefile.cpp:561
ThemesDlg::saveUserAddedThemes
void saveUserAddedThemes()
Definition: themesdlg.cpp:60
QWidget
ThemeFile::canonicalFile
static QString canonicalFile(const QString &file)
Definition: themefile.cpp:493
ThemesDlg::~ThemesDlg
~ThemesDlg()
Definition: themesdlg.cpp:54
kwidgetlistbox.h
SuperKarambaSettings::userAddedThemes
static QStringList userAddedThemes()
Get Themes that user added to theme list.
Definition: superkarambasettings.h:49
karambaApp
#define karambaApp
Definition: karambaapp.h:22
ThemesDlg::populateListbox
void populateListbox()
Definition: themesdlg.cpp:99
ThemeWidget::showButton
void showButton(bool show)
Definition: themewidget.cpp:93
QObject
ThemeWidget::removeInstance
void removeInstance(int instance)
Definition: themewidget.cpp:68
ThemesDlg
Definition: themesdlg.h:35
ThemesDlg::openLocalTheme
virtual void openLocalTheme()
Definition: themesdlg.cpp:189
Karamba
Definition: karamba.h:52
ThemesDlg::isDownloaded
bool isDownloaded(const QString &path)
Definition: themesdlg.cpp:397
karamba.h
ThemesDlg::ThemesDlg
ThemesDlg(QWidget *parent=0, const char *name=0)
Definition: themesdlg.cpp:38
ThemesDlg::selectionChanged
virtual void selectionChanged(int)
Definition: themesdlg.cpp:275
themewidget.h
ThemesDlg::addThemeToDialog
void addThemeToDialog(const KArchiveDirectory *archiveDir, const QString &destDir)
Definition: themesdlg.cpp:309
ThemeFile::name
const QString & name() const
Definition: themefile.cpp:536
ThemesDlg::installNewTheme
virtual void installNewTheme(const QString &newTheme)
Definition: themesdlg.cpp:220
SuperKarambaSettings::setUserAddedThemes
static void setUserAddedThemes(const QStringList &v)
Set Themes that user added to theme list.
Definition: superkarambasettings.h:39
ThemesDlg::themeIndex
int themeIndex(const QString &file)
Definition: themesdlg.cpp:289
ThemeWidget::instances
int instances() const
Definition: themewidget.h:44
ThemesDlg::addTheme
int addTheme(const QString &appId, const QString &file)
Definition: themesdlg.cpp:345
ThemesDlg::themes
QStringList themes()
Definition: themesdlg.cpp:86
ThemesDlg::removeTheme
void removeTheme(const QString &appId, const QString &file, int instance)
Definition: themesdlg.cpp:357
SuperKarambaSettings::self
static SuperKarambaSettings * self()
Definition: superkarambasettings.cpp:17
ThemesDlg::getNewStuff
virtual void getNewStuff()
Definition: themesdlg.cpp:204
ThemeWidget::themeFile
ThemeFile * themeFile() const
Definition: themewidget.h:38
ThemesDlg::filter
static bool filter(int index, QWidget *widget, void *data)
Definition: themesdlg.cpp:373
ThemeFile::isZipTheme
bool isZipTheme() const
Definition: themefile.cpp:531
ThemeWidget
Definition: themewidget.h:30
ThemeWidget::addInstance
int addInstance()
Definition: themewidget.cpp:57
ThemeFile::path
const QString & path() const
Definition: themefile.cpp:571
ThemeWidget::setDescriptionText
void setDescriptionText(const QString &text)
Definition: themewidget.cpp:83
ThemeFile::isValid
bool isValid() const
Definition: themefile.cpp:252
ThemeFile::canUninstall
bool canUninstall() const
Definition: themefile.cpp:414
ThemesDlg::addSkzThemeToDialog
void addSkzThemeToDialog(const QString &file)
Definition: themesdlg.cpp:303
karambaapp.h
ThemesDlg::search
virtual void search(const QString &text)
Definition: themesdlg.cpp:368
ThemesDlg::runningThemes
QStringList runningThemes()
Definition: themesdlg.cpp:475
ThemesDlg::addThemeToList
int addThemeToList(const QString &file)
Definition: themesdlg.cpp:329
ThemeWidget::setHeaderText
void setHeaderText(const QString &text)
Definition: themewidget.cpp:88
themesdlg.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal