~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/desktoptheme/desktopthemedetails.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2008 Andrew Lake <jamboarder@yahoo.com>
 
3
  Copyright (c) 2010 Jeremy Whiting <jpwhiting@kde.org>
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU General Public License as published by
 
7
  the Free Software Foundation; either version 2 of the License, or
 
8
  (at your option) any later version.
 
9
*/
 
10
 
 
11
#include "desktopthemedetails.h"
 
12
 
 
13
#include "thememodel.h"
 
14
 
 
15
#include <QtGui/QComboBox>
 
16
#include <QtCore/QDir>
 
17
#include <QtCore/QTextStream>
 
18
 
 
19
#include <KDesktopFile>
 
20
#include <KFileDialog>
 
21
#include <KMessageBox>
 
22
#include <KStandardDirs>
 
23
#include <KZip>
 
24
#include <kio/netaccess.h>
 
25
#include <kio/copyjob.h>
 
26
#include <kio/deletejob.h>
 
27
#include <kio/job.h>
 
28
 
 
29
struct ThemeItemNameType {
 
30
        const char* m_type;
 
31
        const char* m_displayItemName;
 
32
        const char* m_themeItemPath;
 
33
        const char* m_iconName;
 
34
};
 
35
 
 
36
const ThemeItemNameType themeCollectionName[] = {
 
37
    { "Color Scheme", I18N_NOOP2("plasma name", "Color Scheme"),"colors", "preferences-desktop-color"},
 
38
    { "Panel Background", I18N_NOOP2("plasma name", "Panel Background"),"widgets/panel-background", "plasma"},
 
39
    { "Kickoff", I18N_NOOP2("plasma name", "Kickoff"), "dialogs/kickoff", "kde"},
 
40
    { "Task Items", I18N_NOOP2("plasma name", "Task Items"), "widgets/tasks", "preferences-system-windows"},
 
41
    { "Widget Background", I18N_NOOP2("plasma name", "Widget Background"), "widgets/background", "plasma"},
 
42
    { "Translucent Background", I18N_NOOP2("plasma name", "Translucent Background"), "widgets/translucentbackground", "plasma"},
 
43
    { "Dialog Background", I18N_NOOP2("plasma name", "Dialog Background"), "dialogs/background", "plasma"},
 
44
    { "Analog Clock", I18N_NOOP2("plasma name", "Analog Clock"), "widgets/clock", "chronometer"},
 
45
    { "Notes", I18N_NOOP2("plasma name", "Notes"), "widgets/notes", "view-pim-notes"},
 
46
    { "Tooltip", I18N_NOOP2("plasma name", "Tooltip"), "widgets/tooltip", "plasma"},
 
47
    { "Pager", I18N_NOOP2("plasma name", "Pager"), "widgets/pager", "plasma"},
 
48
    { "Run Command Dialog", I18N_NOOP2("plasma name", "Run Command Dialog"), "dialogs/krunner", "system-run"},
 
49
    { "Shutdown Dialog", I18N_NOOP2("plasma name", "Shutdown Dialog"), "dialogs/shutdowndialog", "system-shutdown"},
 
50
    { "Icons", I18N_NOOP2("plasma name", "Icons"), "icons/", "preferences-desktop-icons"},
 
51
    { 0, 0,0,0 } // end of data
 
52
};
 
53
 
 
54
 
 
55
DesktopThemeDetails::DesktopThemeDetails(QWidget* parent)
 
56
    : QWidget(parent),
 
57
      m_themeModel(0)
 
58
 
 
59
{
 
60
    setWindowIcon(KIcon("preferences-desktop"));
 
61
    setupUi(this);
 
62
 
 
63
    QFont font = QFont();
 
64
    font.setBold(true);
 
65
    font.setPointSize(1.2*font.pointSize());
 
66
    m_themeInfoName->setFont(font);
 
67
 
 
68
    m_enableAdvanced->setChecked(false);
 
69
    toggleAdvancedVisible();
 
70
 
 
71
 
 
72
    m_themeModel = new ThemeModel(this);
 
73
    m_theme->setModel(m_themeModel);
 
74
    m_theme->setItemDelegate(new ThemeDelegate(m_theme));
 
75
 
 
76
    reloadConfig();
 
77
 
 
78
    connect(m_theme->selectionModel(), SIGNAL(selectionChanged(const QItemSelection, const QItemSelection)), this, SLOT(themeSelectionChanged(const QItemSelection, const QItemSelection)));
 
79
 
 
80
    connect(m_enableAdvanced, SIGNAL(toggled(bool)), this, SLOT(toggleAdvancedVisible()));
 
81
    connect(m_removeThemeButton, SIGNAL(clicked()), this, SLOT(removeTheme()));
 
82
    connect(m_exportThemeButton, SIGNAL(clicked()), this, SLOT(exportTheme()));
 
83
 
 
84
    connect(m_newThemeName, SIGNAL(editingFinished()), this, SLOT(newThemeInfoChanged()));
 
85
 
 
86
    m_baseTheme = "default";
 
87
    m_themeCustomized = false;
 
88
    resetThemeDetails();
 
89
    adjustSize();
 
90
}
 
91
 
 
92
DesktopThemeDetails::~DesktopThemeDetails()
 
93
{
 
94
    cleanup();
 
95
}
 
96
 
 
97
void DesktopThemeDetails::cleanup()
 
98
{
 
99
}
 
100
 
 
101
void DesktopThemeDetails::reloadConfig()
 
102
{
 
103
    // Theme
 
104
    KConfigGroup cfg = KConfigGroup(KSharedConfig::openConfig("plasmarc"), "Theme");
 
105
    const QString theme = cfg.readEntry("name", "default");
 
106
    m_theme->setCurrentIndex(m_themeModel->indexOf(theme));
 
107
 
 
108
}
 
109
 
 
110
void DesktopThemeDetails::save()
 
111
{
 
112
    QString themeRoot;
 
113
    KStandardDirs dirs;
 
114
    if (m_newThemeName->text().isEmpty()) {
 
115
        themeRoot = ".customized";
 
116
        //Toggle customized theme directory name to ensure theme reload
 
117
        if (QDir(dirs.locateLocal("data", "desktoptheme/" + themeRoot + '/', false)).exists()) {
 
118
            themeRoot = themeRoot + '1';
 
119
        }
 
120
    } else {
 
121
        themeRoot = m_newThemeName->text().replace(' ',"_").remove(QRegExp("[^A-Za-z0-9_]"));
 
122
    }
 
123
 
 
124
    //Save theme items
 
125
    QFile customSettingsFile;
 
126
    bool customSettingsFileOpen = false;
 
127
 
 
128
    if (m_themeCustomized || !m_newThemeName->text().isEmpty()) {
 
129
 
 
130
        clearCustomized(themeRoot);
 
131
 
 
132
        //Copy all files from the base theme
 
133
        QString baseSource = dirs.locate("data", "desktoptheme/" + m_baseTheme + "/metadata.desktop");
 
134
        baseSource = baseSource.left(baseSource.lastIndexOf('/', -1));
 
135
        KIO::CopyJob *copyBaseTheme = KIO::copyAs(KUrl(baseSource), KUrl(dirs.locateLocal("data", "desktoptheme/" + themeRoot, true)), KIO::HideProgressInfo);
 
136
        KIO::NetAccess::synchronousRun(copyBaseTheme, this);
 
137
 
 
138
        //Prepare settings file for customized theme
 
139
        if (isCustomized(themeRoot)) {
 
140
            customSettingsFile.setFileName(dirs.locateLocal("data", "desktoptheme/" + themeRoot + "/settings"));
 
141
            customSettingsFileOpen = customSettingsFile.open(QFile::WriteOnly);
 
142
            if (customSettingsFileOpen) {
 
143
                QTextStream out(&customSettingsFile);
 
144
                out << "baseTheme=" + m_baseTheme + "\r\n";;
 
145
            }
 
146
        }
 
147
        QString settingsSource;
 
148
 
 
149
        //Copy each item theme file to new theme folder
 
150
        QHashIterator<int, int> i(m_itemThemeReplacements);
 
151
        while (i.hasNext()) {
 
152
            i.next();
 
153
            //Get root directory where item should reside (relative to theme root)
 
154
            QString itemRoot = "";
 
155
            if (m_itemPaths[i.key()].lastIndexOf('/', -1) != -1) {
 
156
                itemRoot= m_itemPaths[i.key()].left(m_itemPaths[i.key()].lastIndexOf('/', -1));
 
157
            }
 
158
            //Setup source and destination
 
159
            QString source;
 
160
            QString dest;
 
161
            if (i.value() != -1) {
 
162
                //Source is a theme
 
163
                source = "desktoptheme/" + m_themeRoots[i.value()] + '/' + m_itemPaths[i.key()] + '*';
 
164
                dest = "desktoptheme/" + themeRoot + '/' + itemRoot + '/';
 
165
                settingsSource = m_themeRoots[i.value()];
 
166
            } else {
 
167
               //Source is a file
 
168
                source = m_itemFileReplacements[i.key()];
 
169
                dest = "desktoptheme/" + themeRoot + '/' + itemRoot + '/';
 
170
                settingsSource = m_itemFileReplacements[i.key()];
 
171
            }
 
172
 
 
173
 
 
174
            //Delete item files at destination before copying (possibly there from base theme copy)
 
175
            const QStringList deleteFiles = dirs.findAllResources("data", "desktoptheme/" + themeRoot + '/' + m_itemPaths[i.key()] + '*',
 
176
                                            KStandardDirs::NoDuplicates);
 
177
            for (int j = 0; j < deleteFiles.size(); ++j) {
 
178
                KIO::DeleteJob *dj = KIO::del(KUrl(deleteFiles.at(j)), KIO::HideProgressInfo);
 
179
                KIO::NetAccess::synchronousRun(dj, this);
 
180
            }
 
181
 
 
182
            //Copy item(s)
 
183
            dest = dirs.locateLocal("data", dest, true);
 
184
            QStringList copyFiles;
 
185
            if (i.value() != -1) {
 
186
                copyFiles = dirs.findAllResources("data", source, KStandardDirs::NoDuplicates); //copy from theme
 
187
            } else {
 
188
                copyFiles << source; //copy from file
 
189
            }
 
190
            for (int j = 0; j < copyFiles.size(); ++j) {
 
191
                KIO::CopyJob *cj = KIO::copy(KUrl(copyFiles.at(j)), KUrl(dest), KIO::HideProgressInfo);
 
192
                KIO::NetAccess::synchronousRun(cj, this);
 
193
            }
 
194
 
 
195
            //Record settings file
 
196
            if (customSettingsFileOpen) {
 
197
                QTextStream out(&customSettingsFile);
 
198
                out << m_items.key(i.key()) + "=" + settingsSource +"\r\n";
 
199
            }
 
200
        }
 
201
        if (customSettingsFileOpen) customSettingsFile.close();
 
202
 
 
203
        // Create new theme FDO desktop file
 
204
        QFile::remove(dirs.locateLocal("data", "desktoptheme/" + themeRoot + "/metadata.desktop", false));
 
205
        KDesktopFile df(dirs.locateLocal("data", "desktoptheme/" + themeRoot + "/metadata.desktop"));
 
206
        KConfigGroup cg = df.desktopGroup();
 
207
        if (isCustomized(themeRoot)) {
 
208
            cg.writeEntry("Name",i18n("(Customized)"));
 
209
            cg.writeEntry("Comment", i18n("User customized theme"));
 
210
            cg.writeEntry("X-KDE-PluginInfo-Name", themeRoot);
 
211
        } else {
 
212
            cg.writeEntry("Name", m_newThemeName->text());
 
213
            cg.writeEntry("Comment", m_newThemeDescription->text());
 
214
            cg.writeEntry("X-KDE-PluginInfo-Author", m_newThemeAuthor->text());
 
215
            cg.writeEntry("X-KDE-PluginInfo-Version", m_newThemeVersion->text());
 
216
        }
 
217
        cg.sync();
 
218
 
 
219
        m_themeCustomized = false;
 
220
    }
 
221
 
 
222
    m_themeModel->reload();
 
223
    if (m_themeModel->indexOf(themeRoot).isValid()) {
 
224
        m_theme->setCurrentIndex(m_themeModel->indexOf(themeRoot));
 
225
        QString themeName = m_theme->currentIndex().data(Qt::DisplayRole).toString();
 
226
        setDesktopTheme(themeRoot);
 
227
    }
 
228
    resetThemeDetails();
 
229
}
 
230
 
 
231
void DesktopThemeDetails::removeTheme()
 
232
{
 
233
    bool removeTheme = true;
 
234
    KConfigGroup cfg = KConfigGroup(KSharedConfig::openConfig("plasmarc"), "Theme");
 
235
    QString activeTheme = cfg.readEntry("name", "default");
 
236
    const QString theme = m_theme->currentIndex().data(ThemeModel::PackageNameRole).toString();
 
237
    const QString themeName = m_theme->currentIndex().data(Qt::DisplayRole).toString();
 
238
 
 
239
 
 
240
    if (m_themeCustomized) {
 
241
        if(KMessageBox::questionYesNo(this, i18n("Theme items have been changed.  Do you still wish remove the \"%1\" theme?", themeName), i18n("Remove Desktop Theme")) == KMessageBox::No) {
 
242
            removeTheme = false;
 
243
        }
 
244
    } else {
 
245
        if (theme == "default") {
 
246
            KMessageBox::information(this, i18n("Removal of the default desktop theme is not allowed."), i18n("Remove Desktop Theme"));
 
247
            removeTheme = false;
 
248
        } else {
 
249
            if(KMessageBox::questionYesNo(this, i18n("Are you sure you wish remove the \"%1\" theme?", themeName), i18n("Remove Desktop Theme")) == KMessageBox::No) {
 
250
                removeTheme = false;
 
251
            }
 
252
        }
 
253
 
 
254
    }
 
255
    KStandardDirs dirs;
 
256
    if (removeTheme) {
 
257
        if (theme == activeTheme) {
 
258
            setDesktopTheme("default");
 
259
            activeTheme = "default";
 
260
        }
 
261
        if (QDir(dirs.locateLocal("data", "desktoptheme/" + theme, false)).exists()) {
 
262
            KIO::DeleteJob *deleteTheme = KIO::del(KUrl(dirs.locateLocal("data", "desktoptheme/" + theme, false)), KIO::HideProgressInfo);
 
263
            KIO::NetAccess::synchronousRun(deleteTheme, this);
 
264
        }
 
265
    }
 
266
    m_themeModel->reload();
 
267
    reloadConfig();
 
268
    m_theme->setCurrentIndex(m_themeModel->indexOf(activeTheme));
 
269
}
 
270
 
 
271
void DesktopThemeDetails::exportTheme()
 
272
{
 
273
    const QString theme = m_theme->currentIndex().data(ThemeModel::PackageNameRole).toString();
 
274
 
 
275
    if (m_themeCustomized ||
 
276
        (isCustomized(theme) && m_newThemeName->text() == "")) {
 
277
        KMessageBox::information(this, i18n("Please apply theme item changes (with a new theme name) before attempting to export theme."), i18n("Export Desktop Theme"));
 
278
    } else {
 
279
        QString themeStoragePath = theme;
 
280
 
 
281
        KStandardDirs dirs;
 
282
        const QString themePath = dirs.findResource("data", "desktoptheme/" + themeStoragePath + "/metadata.desktop");
 
283
        if (!themePath.isEmpty()){
 
284
            QString expFileName = KFileDialog::getSaveFileName(KUrl(), "*.zip", this, i18n("Export theme to file"));
 
285
            if (!expFileName.endsWith(".zip"))
 
286
                expFileName = expFileName + ".zip";
 
287
            if (!expFileName.isEmpty()) {
 
288
                KUrl path(themePath);
 
289
                KZip expFile(expFileName);
 
290
                expFile.open(QIODevice::WriteOnly);
 
291
                expFile.addLocalDirectory(path.directory (), themeStoragePath);
 
292
                expFile.close();
 
293
            }
 
294
        }
 
295
    }
 
296
 
 
297
}
 
298
 
 
299
void DesktopThemeDetails::loadThemeItems()
 
300
{
 
301
    // Set up items, item paths and  icons
 
302
    m_items.clear(); // clear theme items
 
303
    m_itemPaths.clear(); // clear theme item paths
 
304
    m_itemIcons.clear();
 
305
    for (int i = 0; themeCollectionName[i].m_type; ++i) {
 
306
        m_items[themeCollectionName[i].m_type] = i;
 
307
        m_itemPaths[i] = themeCollectionName[i].m_themeItemPath;
 
308
        m_itemIcons[i] = themeCollectionName[i].m_iconName;
 
309
    }
 
310
 
 
311
    // Get installed themes
 
312
    m_themes.clear(); // clear installed theme list
 
313
    m_themeRoots.clear(); // clear installed theme root paths
 
314
    KStandardDirs dirs;
 
315
    QStringList themes = dirs.findAllResources("data", "desktoptheme/*/metadata.desktop",
 
316
                                               KStandardDirs::NoDuplicates);
 
317
    themes.sort();
 
318
    int j=0;
 
319
    for (int i = 0; i < themes.size(); ++i) {
 
320
        QString theme = themes.at(i);
 
321
        int themeSepIndex = theme.lastIndexOf('/', -1);
 
322
        QString themeRoot = theme.left(themeSepIndex);
 
323
        int themeNameSepIndex = themeRoot.lastIndexOf('/', -1);
 
324
        QString packageName = themeRoot.right(themeRoot.length() - themeNameSepIndex - 1);
 
325
        KDesktopFile df(theme);
 
326
        QString name = df.readName();
 
327
        if (name.isEmpty()) {
 
328
            name = packageName;
 
329
        }
 
330
 
 
331
        if (!isCustomized(packageName) && (m_themeRoots.key(packageName, -1) == -1)) {
 
332
            m_themes[name] = j;
 
333
            m_themeRoots[j] = packageName;
 
334
            ++j;
 
335
        }
 
336
    }
 
337
 
 
338
    // Set up default item replacements
 
339
    m_itemThemeReplacements.clear();
 
340
    m_itemFileReplacements.clear();
 
341
    QString currentTheme = m_theme->currentIndex().data(ThemeModel::PackageNameRole).toString();
 
342
 
 
343
    if (!isCustomized(currentTheme)) {
 
344
        // Set default replacements to current theme
 
345
        QHashIterator<QString, int> i(m_items);
 
346
        while (i.hasNext()) {
 
347
            i.next();
 
348
            m_itemThemeReplacements[i.value()] = m_themeRoots.key(currentTheme);
 
349
        }
 
350
        m_baseTheme = currentTheme;
 
351
    } else {
 
352
        // Set default replacements to customized theme settings
 
353
        QFile customSettingsFile(dirs.locateLocal("data", "desktoptheme/" + currentTheme +"/settings"));
 
354
        if (customSettingsFile.open(QFile::ReadOnly)) {
 
355
            QTextStream in(&customSettingsFile);
 
356
            QString line;
 
357
            QStringList settingsPair;
 
358
            while (!in.atEnd()) {
 
359
                line = in.readLine();
 
360
                settingsPair = line.split('=');
 
361
                if (settingsPair.at(0) == "baseTheme") {
 
362
                    m_baseTheme = settingsPair.at(1);
 
363
                } else {
 
364
                    if (m_themeRoots.key(settingsPair.at(1), -1) != -1) { // theme for current item exists
 
365
                        m_itemThemeReplacements[m_items[settingsPair.at(0)]] = m_themeRoots.key(settingsPair.at(1));
 
366
                    } else if (QFile::exists(settingsPair.at(1))) {
 
367
                        m_itemThemeReplacements[m_items[settingsPair.at(0)]] = -1;
 
368
                        m_itemFileReplacements[m_items[settingsPair.at(0)]] = settingsPair.at(1);
 
369
                    }
 
370
                }
 
371
            }
 
372
            customSettingsFile.close();
 
373
        }
 
374
    }
 
375
 
 
376
    // Build displayed list of theme items
 
377
    m_themeItemList->setRowCount(m_items.size());
 
378
    m_themeItemList->setColumnCount(2);
 
379
    m_themeItemList->setHorizontalHeaderLabels(QStringList()<< i18n("Theme Item")<<i18n("Source"));
 
380
    QString displayedItem;
 
381
    QHashIterator<QString, int> i(m_items);
 
382
    while (i.hasNext()) {
 
383
        i.next();
 
384
        displayedItem = displayedItemText(i.value());
 
385
        m_themeItemList->setItem(i.value(), 0, new QTableWidgetItem(displayedItem));
 
386
        m_themeItemList->item(i.value(),0)->setIcon(KIcon(m_itemIcons[i.value()]));
 
387
        m_themeItemList->setCellWidget(i.value(), 1, new QComboBox());
 
388
        updateReplaceItemList(i.value());
 
389
        m_themeItemList->resizeColumnToContents(1);
 
390
    }
 
391
    m_themeItemList->setSelectionBehavior(QAbstractItemView::SelectRows);
 
392
    m_themeItemList->verticalHeader()->hide();
 
393
    m_themeItemList->horizontalHeader()->setStretchLastSection(true);
 
394
    m_themeItemList->horizontalHeader()->setMinimumSectionSize(120);
 
395
    m_themeItemList->horizontalHeader()->setResizeMode(1, QHeaderView::ResizeToContents);;
 
396
    m_themeItemList->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);;
 
397
    m_themeItemList->setCurrentCell(0, 1);
 
398
}
 
399
 
 
400
void DesktopThemeDetails::updateReplaceItemList(const int& item)
 
401
{
 
402
    QString currentTheme = m_theme->currentIndex().data(ThemeModel::PackageNameRole).toString();
 
403
 
 
404
 
 
405
    // Repopulate combobox droplist
 
406
    QComboBox *itemComboBox = static_cast<QComboBox*>(m_themeItemList->cellWidget(item,1));
 
407
    disconnect(itemComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(replacementItemChanged()));
 
408
    itemComboBox->clear();
 
409
    for (int i = 0; i < m_themes.size(); ++i) {
 
410
       QString displayedDropListItem = i18n("%1 %2", m_themes.key(i), displayedItemText(item));
 
411
       itemComboBox->addItem(displayedDropListItem);
 
412
    }
 
413
    itemComboBox->addItem(i18n("File..."));
 
414
 
 
415
    // Set combobox value to current replacement
 
416
    if (m_itemThemeReplacements[item] != -1) {
 
417
        itemComboBox->setCurrentIndex(m_itemThemeReplacements[item]);
 
418
    } else {
 
419
        itemComboBox->addItem(m_itemFileReplacements[item]);
 
420
        itemComboBox->setCurrentIndex(itemComboBox->findText(m_itemFileReplacements[item]));
 
421
    }
 
422
    connect(itemComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(replacementItemChanged()));
 
423
}
 
424
 
 
425
void DesktopThemeDetails::replacementItemChanged()
 
426
{
 
427
    //Check items to see if theme has been customized
 
428
    m_themeCustomized = true;
 
429
    QHashIterator<QString, int> i(m_items);
 
430
    while (i.hasNext()) {
 
431
        i.next();
 
432
        QComboBox *itemComboBox = static_cast<QComboBox*>(m_themeItemList->cellWidget(i.value(), 1));
 
433
        int replacement = itemComboBox->currentIndex();
 
434
        if (replacement <= (m_themes.size() - 1)) {
 
435
            // Item replacement source is a theme
 
436
            m_itemThemeReplacements[i.value()] = itemComboBox->currentIndex();
 
437
        } else if (replacement > (m_themes.size() - 1)) {
 
438
            // Item replacement source is a file
 
439
            if (itemComboBox->currentText() == i18n("File...")) {
 
440
                //Get the filename for the replacement item
 
441
                QString translated_key = i18nc("plasma name", qPrintable( i.key() ) );
 
442
                QString fileReplacement = KFileDialog::getOpenFileName(KUrl(), QString(), this, i18n("Select File to Use for %1",translated_key));
 
443
                if (!fileReplacement.isEmpty()) {
 
444
                    m_itemFileReplacements[i.value()] = fileReplacement;
 
445
                    int index = itemComboBox->findText(fileReplacement);
 
446
                    if (index == -1) itemComboBox->addItem(fileReplacement);
 
447
                    disconnect(itemComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(replacementItemChanged()));
 
448
                    itemComboBox->setCurrentIndex(itemComboBox->findText(fileReplacement));
 
449
                    connect(itemComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(replacementItemChanged()));
 
450
                    m_itemThemeReplacements[i.value()] = -1; //source is not a theme
 
451
                    m_itemFileReplacements[i.value()] = itemComboBox->currentText();
 
452
                } else {
 
453
                    // Reset combobox to previous value if no file is selected
 
454
                    if (m_itemThemeReplacements[i.value()] != -1) {
 
455
                        itemComboBox->setCurrentIndex(m_itemThemeReplacements[i.value()]);
 
456
                    } else {
 
457
                        itemComboBox->setCurrentIndex(itemComboBox->findText(m_itemFileReplacements[i.value()]));
 
458
                    }
 
459
                    m_themeCustomized = false;
 
460
                }
 
461
            } else {
 
462
                m_itemThemeReplacements[i.value()] = -1; //source is not a theme
 
463
                m_itemFileReplacements[i.value()] = itemComboBox->currentText();
 
464
            }
 
465
        }
 
466
    }
 
467
 
 
468
    if (m_themeCustomized) emit changed();
 
469
}
 
470
 
 
471
void DesktopThemeDetails::newThemeInfoChanged()
 
472
{
 
473
    emit changed();
 
474
}
 
475
 
 
476
void DesktopThemeDetails::resetThemeDetails()
 
477
{
 
478
    QString theme = m_theme->currentIndex().data(ThemeModel::PackageNameRole).toString();
 
479
 
 
480
    m_themeInfoName->setText(m_theme->currentIndex().data(Qt::DisplayRole).toString());
 
481
    m_themeInfoDescription->setText(m_theme->currentIndex().data(ThemeModel::PackageDescriptionRole).toString());
 
482
    QString author = m_theme->currentIndex().data(ThemeModel::PackageAuthorRole).toString();
 
483
 
 
484
    if (!author.isEmpty()) {
 
485
        m_themeInfoAuthor->setText(i18n(" Author: %1",author));
 
486
    } else {
 
487
        m_themeInfoAuthor->setText("");
 
488
    }
 
489
    QString version = m_theme->currentIndex().data(ThemeModel::PackageVersionRole).toString();
 
490
    if (!version.isEmpty()) {
 
491
       m_themeInfoVersion->setText(i18n("Version: %1",version));
 
492
    } else {
 
493
       m_themeInfoVersion->setText("");
 
494
    }
 
495
 
 
496
    loadThemeItems();
 
497
 
 
498
    m_newThemeName->clear();
 
499
    m_newThemeAuthor->clear();
 
500
    m_newThemeVersion->clear();
 
501
    m_newThemeDescription->clear();
 
502
    m_themeCustomized = false;
 
503
 }
 
504
 
 
505
void DesktopThemeDetails::toggleAdvancedVisible()
 
506
{
 
507
    m_newThemeNameLabel->setVisible(m_enableAdvanced->isChecked());
 
508
    m_newThemeName->setVisible(m_enableAdvanced->isChecked());
 
509
    m_newThemeAuthor->setVisible(m_enableAdvanced->isChecked());
 
510
    m_newThemeAuthorLabel->setVisible(m_enableAdvanced->isChecked());
 
511
    m_newThemeVersion->setVisible(m_enableAdvanced->isChecked());
 
512
    m_newThemeVersionLabel->setVisible(m_enableAdvanced->isChecked());
 
513
    m_newThemeDescriptionLabel->setVisible(m_enableAdvanced->isChecked());
 
514
    m_newThemeDescription->setVisible(m_enableAdvanced->isChecked());
 
515
    m_exportThemeButton->setVisible(m_enableAdvanced->isChecked());
 
516
    m_removeThemeButton->setVisible(m_enableAdvanced->isChecked());
 
517
    m_advancedLine->setVisible(m_enableAdvanced->isChecked());
 
518
}
 
519
 
 
520
bool DesktopThemeDetails::isCustomized(const QString& theme) {
 
521
    if (theme == ".customized" || theme == ".customized1") {
 
522
        return true;
 
523
    } else {
 
524
        return false;
 
525
    }
 
526
}
 
527
 
 
528
void DesktopThemeDetails::clearCustomized(const QString& themeRoot) {
 
529
    KStandardDirs dirs;
 
530
 
 
531
    if ((isCustomized(themeRoot))) {
 
532
        // Remove both possible unnamed customized directories
 
533
        if (QDir(dirs.locateLocal("data", "desktoptheme/.customized", false)).exists()) {
 
534
            KIO::DeleteJob *clearCustom = KIO::del(KUrl(dirs.locateLocal("data", "desktoptheme/.customized", false)), KIO::HideProgressInfo);
 
535
            KIO::NetAccess::synchronousRun(clearCustom, this);
 
536
        }
 
537
        if (QDir(dirs.locateLocal("data", "desktoptheme/.customized1", false)).exists()) {
 
538
            KIO::DeleteJob *clearCustom1 = KIO::del(KUrl(dirs.locateLocal("data", "desktoptheme/.customized1", false)), KIO::HideProgressInfo);
 
539
            KIO::NetAccess::synchronousRun(clearCustom1, this);
 
540
        }
 
541
    } else {
 
542
        if (QDir(dirs.locateLocal("data", "desktoptheme/" + themeRoot, false)).exists()) {
 
543
            KIO::DeleteJob *clearCustom = KIO::del(KUrl(dirs.locateLocal("data", "desktoptheme/" + themeRoot, false)), KIO::HideProgressInfo);
 
544
            KIO::NetAccess::synchronousRun(clearCustom, this);
 
545
        }
 
546
    }
 
547
}
 
548
 
 
549
QString DesktopThemeDetails::displayedItemText(int item) {
 
550
    QString displayedText = m_items.key(item);
 
551
    for (int i = 0; themeCollectionName[i].m_type; ++i) {
 
552
        if (themeCollectionName[i].m_type == m_items.key(item)) {
 
553
            displayedText = i18nc("plasma name", themeCollectionName[i].m_displayItemName);
 
554
        }
 
555
    }
 
556
    return displayedText;
 
557
}
 
558
 
 
559
void DesktopThemeDetails::themeSelectionChanged(const QItemSelection newSelection, const QItemSelection oldSelection)
 
560
{
 
561
    QString theme = m_theme->currentIndex().data(ThemeModel::PackageNameRole).toString();
 
562
    if (theme == "default") {
 
563
        m_removeThemeButton->setEnabled(false);
 
564
    } else {
 
565
        m_removeThemeButton->setEnabled(true);
 
566
    }
 
567
    resetThemeDetails();
 
568
    Q_UNUSED(newSelection);
 
569
    Q_UNUSED(oldSelection);
 
570
}
 
571
 
 
572
void DesktopThemeDetails::setDesktopTheme(QString themeRoot)
 
573
{
 
574
    KConfig config(KStandardDirs::locate("config", "plasmarc"));
 
575
    KConfigGroup cg = KConfigGroup(&config, "Theme");
 
576
    if (themeRoot == "default") {
 
577
        cg.deleteEntry("name");
 
578
    } else {
 
579
        cg.writeEntry("name", themeRoot);
 
580
    }
 
581
    cg.sync();
 
582
}