File indexing completed on 2024-04-28 17:04:34

0001 /*****************************************************************************
0002  *   Copyright 2003 - 2010 Craig Drummond <craig.p.drummond@gmail.com>       *
0003  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0004  *                                                                           *
0005  *   This program is free software; you can redistribute it and/or modify    *
0006  *   it under the terms of the GNU Lesser General Public License as          *
0007  *   published by the Free Software Foundation; either version 2.1 of the    *
0008  *   License, or (at your option) version 3, or any later version accepted   *
0009  *   by the membership of KDE e.V. (or its successor approved by the         *
0010  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0011  *   Section 6 of version 3 of the license.                                  *
0012  *                                                                           *
0013  *   This program is distributed in the hope that it will be useful,         *
0014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0016  *   Lesser General Public License for more details.                         *
0017  *                                                                           *
0018  *   You should have received a copy of the GNU Lesser General Public        *
0019  *   License along with this library. If not,                                *
0020  *   see <http://www.gnu.org/licenses/>.                                     *
0021  *****************************************************************************/
0022 
0023 #include "exportthemedialog.h"
0024 
0025 #include <common/config_file.h>
0026 #include <common/kf5_utils.h>
0027 
0028 #include <klocale.h>
0029 #include <kurlrequester.h>
0030 #include <klineedit.h>
0031 #include <kmessagebox.h>
0032 #include <KConfig>
0033 #include <KConfigGroup>
0034 
0035 #include <QDir>
0036 #include <QGridLayout>
0037 #include <QLabel>
0038 
0039 CExportThemeDialog::CExportThemeDialog(QWidget *parent)
0040                   : QDialog(parent)
0041 {
0042     auto mainLayout = new QVBoxLayout(this);
0043     auto page = new QWidget(this);
0044     auto layout = new QGridLayout(page);
0045     auto buttonBox = QtCurve::createDialogButtonBox(this);
0046     if (QWidget *win = window()) {
0047         win->setWindowTitle(i18n("Export Theme"));
0048     }
0049     layout->setSpacing(QApplication::style()
0050                        ->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
0051     layout->setMargin(0);
0052     layout->addWidget(new QLabel(i18n("Name:"), page), 0, 0);
0053     layout->addWidget(new QLabel(i18n("Comment:"), page), 1, 0);
0054     layout->addWidget(new QLabel(i18n("Destination folder:"), page), 2, 0);
0055     layout->addWidget(themeName = new QLineEdit(page), 0, 1);
0056     layout->addWidget(themeComment = new QLineEdit(i18n("QtCurve based theme"),
0057                                                    page), 1, 1);
0058     layout->addWidget(themeUrl=new KUrlRequester(page), 2, 1);
0059     layout->addItem(new QSpacerItem(2, 2, QSizePolicy::Minimum,
0060                                     QSizePolicy::Expanding), 3, 1);
0061 
0062     themeUrl->setMode(KFile::Directory | KFile::ExistingOnly |
0063                       KFile::LocalOnly);
0064     themeUrl->lineEdit()->setReadOnly(true);
0065     themeUrl->setUrl(QDir::homePath());
0066 
0067     mainLayout->addWidget(page);
0068     mainLayout->addWidget(buttonBox);
0069 }
0070 
0071 void CExportThemeDialog::run(const Options &o)
0072 {
0073     opts = o;
0074     exec();
0075 }
0076 
0077 QSize CExportThemeDialog::sizeHint() const
0078 {
0079     return QSize(400, 120);
0080 }
0081 
0082 void
0083 CExportThemeDialog::accept()
0084 {
0085     QString name(themeName->text().trimmed().toLower());
0086 
0087     if (name.isEmpty()) {
0088         KMessageBox::error(this, i18n("Name is empty!"));
0089     } else {
0090         QString fileName(themeUrl->url().path() + "/" THEME_PREFIX + name +
0091                          ".themerc");
0092 
0093         KConfig cfg(fileName, KConfig::SimpleConfig);
0094         bool rv(cfg.isConfigWritable(false));
0095 
0096         if (rv) {
0097             cfg.group("Misc").writeEntry("Name", themeName->text().trimmed());
0098             cfg.group("Misc").writeEntry("Comment", themeComment->text());
0099             cfg.group("KDE").writeEntry("WidgetStyle", THEME_PREFIX + name);
0100 
0101             rv = qtcWriteConfig(&cfg, opts, opts, true);
0102         }
0103 
0104         if (rv) {
0105             QDialog::accept();
0106             KMessageBox::information(this, i18n("Successfully created:\n%1",
0107                                                 fileName));
0108         } else {
0109             KMessageBox::error(this, i18n("Failed to create file: %1",
0110                                           fileName));
0111         }
0112     }
0113 }