summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/themesdlg.cpp
blob: bf4cd938a65a7ff5ffce36da694c2704be484e64 (plain)
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * Copyright (C) 2005 Petri Damstén <petri.damsten@iki.fi>
 *
 * This file is part of SuperKaramba.
 *
 *  SuperKaramba 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.
 *
 *  SuperKaramba 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 SuperKaramba; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ****************************************************************************/
#include "karambaapp.h"
#include "dcopinterface_stub.h"
#include "karambainterface.h"
#include "themesdlg.h"
#include "themewidget.h"
#include "kwidgetlistbox.h"
#include "karamba.h"

#ifdef HAVE_CONFIG_H
  #include <config.h>
#endif

#ifdef HAVE_KNEWSTUFF
  #include "sknewstuff.h"
#endif

#include "superkarambasettings.h"
#include <karchive.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <klocale.h>
#include <tqlineedit.h>
#include <tqtable.h>
#include <tqdir.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqptrlist.h>
#include <kio/job.h>
#include <kprotocolinfo.h>

ThemesDlg::ThemesDlg(TQWidget *tqparent, const char *name)
 : ThemesLayout(tqparent, name)
{
  populateListbox();
#ifdef HAVE_KNEWSTUFF
  mNewStuff = 0;
#endif
}

ThemesDlg::~ThemesDlg()
{
  //kdDebug() << k_funcinfo << endl;
  saveUserAddedThemes();
#ifdef HAVE_KNEWSTUFF
  if(mNewStuff)
  {
    delete mNewStuff;
  }
#endif
}

void ThemesDlg::saveUserAddedThemes()
{
  KStandardDirs ksd;
  TQStringList t = themes();
  TQStringList dirs = ksd.findDirs("data", TQString(kapp->name()) + "/themes");
  TQStringList::Iterator it = t.begin();
  bool remove;

  while(it != t.end())
  {
    remove = false;
    TQStringList::Iterator jtend( dirs.end() );
    for(TQStringList::Iterator jt = dirs.begin(); jt != jtend; ++jt)
    {
      if(TQFileInfo(*it).dir().path() + "/" == *jt)
      {
        remove = true;
        break;
      }
    }
    if(remove)
      it = t.remove(it);
    else
      ++it;
  }
  SuperKarambaSettings::setUserAddedThemes(t);
  SuperKarambaSettings::writeConfig();
}

TQStringList ThemesDlg::themes()
{
  TQStringList result;
  ThemeWidget* w;

  for(uint i = 2; i < tableThemes->count(); ++i)
  {
    w = static_cast<ThemeWidget*>(tableThemes->item(i));

    result.append(w->themeFile()->file());
  }
  return result;
}

void ThemesDlg::populateListbox()
{
  ThemeWidget* item;
  TQDir dir;
  TQStringList dirs;
  TQStringList t;
  KStandardDirs ksd;

  tableThemes->clear();

  item = new ThemeWidget;
  item->icon->setPixmap(KGlobal::iconLoader()->loadIcon("knewstuff",
                        KIcon::NoGroup, KIcon::SizeHuge));
  item->setHeaderText(i18n("Get New Stuff"));
  item->setDescriptionText(i18n("Download new themes."));

  item->buttonGo->setText(i18n("New Stuff..."));
#ifdef HAVE_KNEWSTUFF
  item->buttonGo->setEnabled(true);
  connect(item->buttonGo, TQT_SIGNAL(clicked()),
          this, TQT_SLOT(getNewStuff()));
#else
  item->buttonGo->setEnabled(false);
#endif
  tableThemes->insertItem(item);

  item = new ThemeWidget;
  item->icon->setPixmap(KGlobal::iconLoader()->loadIcon("ksysguard",
                        KIcon::NoGroup, KIcon::SizeHuge));
  item->setHeaderText(i18n("Open Local Theme"));
  item->setDescriptionText(i18n("Add local theme to the list."));
  item->buttonGo->setProperty("stdItem", 18);
  item->buttonGo->setText(i18n("Open..."));
  connect(item->buttonGo, TQT_SIGNAL(clicked()),
          this, TQT_SLOT(openLocalTheme()));
  tableThemes->insertItem(item);

  dirs = ksd.findDirs("data", TQString(kapp->name()) + "/themes");
  // Get custom dirs from config here?
  TQStringList::Iterator itend( dirs.end() );
  for(TQStringList::Iterator it = dirs.begin(); it != itend; ++it )
  {
    dir.setPath(*it);
    t = dir.entryList("*.skz; *.theme");
    for(TQStringList::Iterator it = t.begin(); it != t.end(); ++it )
    {
      item = new ThemeWidget(new ThemeFile(dir.filePath(*it)));
      tableThemes->insertItem(item);
      item->buttonGo->setText(i18n("Uninstall"));
      connect(item->buttonGo, TQT_SIGNAL(clicked()),
              this, TQT_SLOT(uninstall()));
    }
  }
  t = SuperKarambaSettings::userAddedThemes();
  for(TQStringList::Iterator it = t.begin(); it != t.end(); ++it )
  {
    ThemeFile* file = new ThemeFile(*it);

    if(file->isValid())
    {
      item = new ThemeWidget(file);
      tableThemes->insertItem(item);
      item->buttonGo->setText(i18n("Uninstall"));
      connect(item->buttonGo, TQT_SIGNAL(clicked()),
              this, TQT_SLOT(uninstall()));
    }
    else
      delete file;
  }
  tableThemes->setSelected(0);
}

void ThemesDlg::addToDesktop()
{
  ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->selectedItem());
  if(w)
  {
    ThemeFile* tf = w->themeFile();
    if(tf)
    {
      (new karamba(tf->file(), TQString()))->show();
    }
  }
}

void ThemesDlg::openLocalTheme()
{
  TQStringList fileNames;
  fileNames = KFileDialog::getOpenFileNames(":<themes>",
                  i18n("*.theme *.skz|Themes"),
                  this, i18n("Open Themes"));
  for(TQStringList::Iterator it = fileNames.begin(); it != fileNames.end(); ++it)
  {
    ThemeFile file(*it);
    if(file.isValid())
      (new karamba(*it, TQString()))->show();
  }
}

void ThemesDlg::getNewStuff()
{
#ifdef HAVE_KNEWSTUFF
  KConfig* config = KGlobal::config();
  config->setGroup("KNewStuff");
  config->writePathEntry("ProvidersUrl",
      TQString::tqfromLatin1("http://download.kde.org/khotnewstuff/karamba-providers.xml"));
  config->sync();
  m_newStufftqStatus = config->entryMap("KNewStufftqStatus").keys();
  //This check is b/c KNewStuff will download, throw an error, 
  //and still have the entry in the config that it was successful
  configSanityCheck();

  if ( !mNewStuff )
  {
    mNewStuff = new SKNewStuff(this);
  }
  mNewStuff->download();
#endif
}

void ThemesDlg::selectionChanged(int index)
{
  buttonAddToDesktop->setEnabled(index > 1);

  for(uint i=2; i < tableThemes->count(); ++i)
  {
    ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(i));
    w->showButton(false);
  }
  ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(index));
  ThemeFile* themeFile = w->themeFile();
  if(themeFile && themeFile->canUninstall())
      w->showButton(true);
}

int ThemesDlg::themeIndex(TQString file)
{
  ThemeWidget* w;
  file = ThemeFile::canonicalFile(file);

  for(uint i = 2; i < tableThemes->count(); ++i)
  {
    w = static_cast<ThemeWidget*>(tableThemes->item(i));

    if(w->themeFile()->file() == file)
      return i;
  }
  return -1;
}

void ThemesDlg::addSkzThemeToDialog(const TQString &file)
{
  kdDebug() << "addSkzThemeToDialog(): file = " << file << endl;
  addThemeToList(file);
  writeNewStuffConfig(file);
}

void ThemesDlg::addThemeToDialog(const KArchiveDirectory *archiveDir,
                           const TQString& destDir)
{
  kdDebug() << "addThemeToDialog(): destDir = " << destDir << endl;
  TQStringList entries = archiveDir->entries();

  TQStringList::Iterator end( entries.end() );
  for(TQStringList::Iterator it = entries.begin(); it != end; ++it)
  {
    if(archiveDir->entry(*it)->isDirectory())
    {
      addThemeToDialog(static_cast<const KArchiveDirectory*>(archiveDir->entry(*it)),
                destDir + *it + "/");
    }
    else
    {
      TQFileInfo fi(*it);
      if(fi.extension( FALSE ) == "theme")
      {
        addThemeToList(destDir + *it);
        writeNewStuffConfig(destDir);
      }
    }
  }
}

void ThemesDlg::writeNewStuffConfig(const TQString &file)
{
#ifdef HAVE_KNEWSTUFF
  KConfig* config = KGlobal::config();
  TQStringList keys = config->entryMap("KNewStufftqStatus").keys();

  for(TQStringList::Iterator it = m_newStufftqStatus.begin();
      it != m_newStufftqStatus.end(); ++it)
  {
    keys.remove(*it);
  }
  if(!keys.isEmpty())
  {
    config->setGroup("KNewStuffNames");
    config->writeEntry(file, keys[0]);
    config->sync();
  }
#endif
}

void ThemesDlg::configSanityCheck()
{
#ifdef HAVE_KNEWSTUFF
  KConfig* config = KGlobal::config();
  TQStringList statusKeys = config->entryMap("KNewStufftqStatus").keys();
  TQStringList nameKeys = config->entryMap("KNewStuffNames").keys();
  TQStringList removeList;

  for(TQStringList::Iterator it = statusKeys.begin();
      it != statusKeys.end(); ++it)
  {
    TQString keyName(*it);
    bool removeKey = true;
    config->setGroup("KNewStuffNames");
    for(TQStringList::Iterator it2 = nameKeys.begin();
        it2 != nameKeys.end(); ++it2)
    {
      TQString tempName(config->readEntry(*it2));
      if( tempName.compare(keyName) == 0)
      {
        removeKey = false;
      }
      
    }
    if( removeKey )
    {
      kdDebug() << "sanityCheck() deleting entry " << keyName << endl;
      config->setGroup("KNewStufftqStatus");
      config->deleteEntry( keyName );
    }
  }
  config->sync();
#endif
}

int ThemesDlg::addThemeToList(const TQString &file)
{
  kdDebug() << "addThemeToList() file: " << file << endl;
  int i = themeIndex(file);
  if(i < 0)
  {
    ThemeWidget* item = new ThemeWidget(new ThemeFile(file));

    i = tableThemes->insertItem(item);
    item->buttonGo->setText(i18n("Uninstall"));
    connect(item->buttonGo, TQT_SIGNAL(clicked()),
            this, TQT_SLOT(uninstall()));
  }
  tableThemes->setSelected(i);
  return i;
}

int ThemesDlg::addTheme(const TQString& , const TQString &file)
{
  int i = addThemeToList(file);
  int result = -1;

  ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(i));
  if(w)
    result = w->addInstance();
  karambaApp->buildToolTip();
  return result;
}

void ThemesDlg::removeTheme(const TQString&, const TQString& file, int instance)
{
  int i = themeIndex(file);

  ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->item(i));
  if(w)
    w->removeInstance(instance);
  karambaApp->buildToolTip();
}

void ThemesDlg::search(const TQString&)
{
  tableThemes->showItems(&filter, this);
}

bool ThemesDlg::filter(int index, TQWidget* widget, void* data)
{
  if(index < 2)
    return true;

  ThemesDlg* dlg = static_cast<ThemesDlg*>(data);
  ThemeWidget* w = static_cast<ThemeWidget*>(widget);

  if(dlg->comboShow->currentItem() == 1) // Running themes
    if(w->instances() == 0)
      return false;

  TQString searchText = dlg->editSearch->text().lower();
  if(searchText.isEmpty())
  {
    return true;
  }
  else
  {
    if(w->themeName->text().lower().contains(searchText))
      return true;
    if(w->description->text().lower().contains(searchText))
      return true;
  }
  return false;
}

bool ThemesDlg::isDownloaded( const TQString& path )
{
  kdDebug() << "isDownloaded path: " << path << endl;
  KConfig* config = KGlobal::config();
  config->setGroup("KNewStuffNames");
  return !config->readEntry(path).isEmpty();
}

void ThemesDlg::uninstall()
{
  ThemeWidget* w = static_cast<ThemeWidget*>(tableThemes->selectedItem());
  ThemeFile* tf = w->themeFile();
  KURL trash("trash:/");
  KURL theme(tf->file());
  TQString tempPath(tf->path());

  karambaApp->dcopIface()->closeTheme(tf->name());
  if(!KProtocolInfo::isKnownProtocol(trash))
    trash = KGlobalSettings::trashPath();

  if(!tf->isZipTheme())
  {
    kdDebug() << "encountered unpacked theme" << endl;
    //Don't move it to the trash if it is a local theme
    if(isDownloaded(tempPath))
    {
      TQFileInfo remPath(tf->path());
      TQDir remDir(remPath.dir());
      remDir.cdUp();
      kdDebug() << "moving " << remDir.path() << " to the trash" << endl;
      KIO::move(remDir.path(), trash);
    }
    tableThemes->removeItem(w);
    
    //some themes have multiple .theme files
    //find all .themes that could be listed in the dialog for the directory removed
    TQPtrList<ThemeWidget> list;
    for(uint i = 2; i < tableThemes->count(); ++i)
    {
      ThemeWidget* tempW = static_cast<ThemeWidget*>(tableThemes->item(i));
      ThemeFile* tempTf = tempW->themeFile();
      if( tempTf->path().compare( tempPath ) == 0 )
      {
        list.append( tempW );
      }
    }
    ThemeWidget *twPtr;
    for ( twPtr = list.first(); twPtr; twPtr = list.next() )
    {
      karambaApp->dcopIface()->closeTheme(twPtr->themeFile()->name());
      tableThemes->removeItem( twPtr );
    }
#ifdef HAVE_KNEWSTUFF
    // Remove theme from KNewStufftqStatus
    KConfig* config = KGlobal::config();
    config->setGroup("KNewStuffNames");
    TQString name = config->readEntry(tempPath);
    if(!name.isEmpty())
    {
      kdDebug() << "removing " << tempPath << " under KNewStuffNames from superkarambarc" 
                << endl;
      kapp->config()->deleteEntry(tempPath);
      config->setGroup("KNewStufftqStatus");
      kdDebug() << "removing " << name << " under KNewStufftqStatus from superkarambarc" 
                << endl;
      kapp->config()->deleteEntry(name);
      kapp->config()->sync();
    }
#endif
    
  }
  else
  {
    kdDebug() << "encountered skz theme" << endl;
    if(isDownloaded(theme.path()))
    {
      TQFileInfo remPath(theme.path());
      TQDir remDir(remPath.dir());
      kdDebug() << "moving " << remDir.path() << " to the trash" << endl;
      KIO::move(remDir.path(), trash);
    }
    tableThemes->removeItem(w);
#ifdef HAVE_KNEWSTUFF
    // Remove theme from KNewStufftqStatus
    KConfig* config = KGlobal::config();
    config->setGroup("KNewStuffNames");
    TQString name = config->readEntry(theme.path());
    if(!name.isEmpty())
    {
      kdDebug() << "removing " << theme.path() << " from superkarambarc" << endl;
      kapp->config()->deleteEntry(theme.path());
      config->setGroup("KNewStufftqStatus");
      kdDebug() << "removing " << name << " from superkarambarc" << endl;
      kapp->config()->deleteEntry(name);
      kapp->config()->sync();
    }
#endif
  }
  selectionChanged(tableThemes->selected());
}

TQStringList ThemesDlg::runningThemes()
{
  TQStringList list;
  ThemeWidget* w;

  for(uint i = 2; i < tableThemes->count(); ++i)
  {
    w = static_cast<ThemeWidget*>(tableThemes->item(i));

    if(w->instances() > 0)
      list.append(w->themeFile()->name());
  }
  return list;
}

#include "themesdlg.moc"