File indexing completed on 2024-05-12 05:49:26

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 #include <klocale.h>
0025 #include <kurlrequester.h>
0026 #include <klineedit.h>
0027 #include <kmessagebox.h>
0028 #include <kconfig.h>
0029 #include <QDir>
0030 #include <QGridLayout>
0031 #include <QLabel>
0032 #include <common/config_file.h>
0033 
0034 CExportThemeDialog::CExportThemeDialog(QWidget *parent)
0035                   : KDialog(parent)
0036 {
0037     QWidget     *page = new QWidget(this);
0038     QGridLayout *layout = new QGridLayout(page);
0039 
0040     setButtons(Ok|Cancel);
0041     setDefaultButton(Ok);
0042     setCaption(i18n("Export Theme"));
0043     layout->setSpacing(spacingHint());
0044     layout->setMargin(0);
0045     layout->addWidget(new QLabel(i18n("Name:"), page), 0, 0);
0046     layout->addWidget(new QLabel(i18n("Comment:"), page), 1, 0);
0047     layout->addWidget(new QLabel(i18n("Destination folder:"), page), 2, 0);
0048     layout->addWidget(themeName=new QLineEdit(page), 0, 1);
0049     layout->addWidget(themeComment=new QLineEdit(i18n("QtCurve based theme"), page), 1, 1);
0050     layout->addWidget(themeUrl=new KUrlRequester(page), 2, 1);
0051     layout->addItem(new QSpacerItem(2, 2, QSizePolicy::Minimum, QSizePolicy::Expanding), 3, 1);
0052 
0053     themeUrl->setMode(KFile::Directory|KFile::ExistingOnly|KFile::LocalOnly);
0054     themeUrl->lineEdit()->setReadOnly(true);
0055     themeUrl->setUrl(QDir::homePath());
0056     setMainWidget(page);
0057 }
0058 
0059 void CExportThemeDialog::run(const Options &o)
0060 {
0061     opts=o;
0062     exec();
0063 }
0064 
0065 QSize CExportThemeDialog::sizeHint() const
0066 {
0067     return QSize(400, 120);
0068 }
0069 
0070 void CExportThemeDialog::slotButtonClicked(int button)
0071 {
0072     if(Ok==button)
0073     {
0074         QString name(themeName->text().trimmed().toLower());
0075 
0076         if(name.isEmpty())
0077             KMessageBox::error(this, i18n("Name is empty!"));
0078         else
0079         {
0080             QString fileName(themeUrl->url().path()+"/"THEME_PREFIX+name+".themerc");
0081 
0082             KConfig cfg(fileName, KConfig::SimpleConfig);
0083             bool    rv(cfg.isConfigWritable(false));
0084 
0085             if(rv)
0086             {
0087                 cfg.group("Misc").writeEntry("Name", themeName->text().trimmed());
0088                 cfg.group("Misc").writeEntry("Comment", themeComment->text());
0089                 cfg.group("KDE").writeEntry("WidgetStyle", THEME_PREFIX+name);
0090 
0091                 rv=qtcWriteConfig(&cfg, opts, opts, true);
0092             }
0093 
0094             if(rv)
0095             {
0096                 QDialog::accept();
0097                 KMessageBox::information(this, i18n("Successfully created:\n%1", fileName));
0098             }
0099             else
0100                 KMessageBox::error(this, i18n("Failed to create file: %1", fileName));
0101         }
0102     }
0103     else
0104         QDialog::reject();
0105 }