File indexing completed on 2024-05-19 05:49:21

0001 /***************************************************************************
0002  *   Copyright © 2009 Jonathan Thomas <echidnaman@kubuntu.org>             *
0003  *   Copyright © 2009 Harald Sitter <apachelogger@ubuntu.com>              *
0004  *   Copyright © 2009 Amichai Rothman <amichai2@amichais.net>              *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or         *
0007  *   modify it under the terms of the GNU General Public License as        *
0008  *   published by the Free Software Foundation; either version 2 of        *
0009  *   the License or (at your option) version 3 or any later version        *
0010  *   accepted by the membership of KDE e.V. (or its successor approved     *
0011  *   by the membership of KDE e.V.), which shall act as a proxy            *
0012  *   defined in Section 14 of version 3 of the license.                    *
0013  *                                                                         *
0014  *   This program is distributed in the hope that it will be useful,       *
0015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0017  *   GNU General Public License for more details.                          *
0018  *                                                                         *
0019  *   You should have received a copy of the GNU General Public License     *
0020  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0021  ***************************************************************************/
0022 
0023 #include "hookgui.h"
0024 
0025 // Qt includes
0026 #include <QIcon>
0027 #include <QLabel>
0028 #include <QPushButton>
0029 #include <QSignalMapper>
0030 #include <QVBoxLayout>
0031 
0032 // KDE includes
0033 #include <KLocalizedString>
0034 #include <KPageDialog>
0035 #include <KWindowSystem>
0036 
0037 HookGui::HookGui(QObject* parent)
0038         : QObject(parent)
0039         , m_dialog(0)
0040 {}
0041 
0042 void HookGui::showDialog(QList<Hook*> hooks)
0043 {
0044     if (!m_dialog) {
0045         createDialog();
0046     }
0047     updateDialog(hooks);
0048 }
0049 
0050 void HookGui::createDialog()
0051 {
0052     m_dialog = new KPageDialog;
0053     m_dialog->setWindowTitle(i18n("Update Information"));
0054     m_dialog->setWindowIcon(QIcon::fromTheme("help-hint"));
0055     m_dialog->setStandardButtons(QDialogButtonBox::Close);
0056 }
0057 
0058 void HookGui::updateDialog(QList<Hook*> hooks)
0059 {
0060     if (!m_pages.isEmpty()) {
0061         m_dialog->hide();
0062         // remove old pages
0063         foreach (KPageWidgetItem *page, m_pages) {
0064             m_dialog->removePage(page);
0065         }
0066         m_pages.clear();
0067     }
0068 
0069     // Take the parsed upgrade hook(s) and put them in pages
0070     QSignalMapper *signalMapper = new QSignalMapper(m_dialog);
0071     foreach(const Hook *hook, hooks) {
0072         QWidget *content = new QWidget();
0073         content->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
0074         QVBoxLayout *layout = new QVBoxLayout(content);
0075         layout->setMargin(0);
0076 
0077         QString name = hook->getField("Name");
0078         KPageWidgetItem *page = new KPageWidgetItem(content, name);
0079         page->setIcon(QIcon::fromTheme("help-hint"));
0080         page->setProperty("hook", qVariantFromValue((QObject *)hook));
0081 
0082         QString desc = hook->getField("Description");
0083         QLabel *descLabel = new QLabel(content);
0084         descLabel->setWordWrap(true);
0085         descLabel->setText(desc);
0086         layout->addWidget(descLabel);
0087 
0088         if (!hook->getField("Command").isEmpty()) {
0089 #warning fixme do we need this?
0090 //             layout->addSpacing(2 * KDialog::spacingHint());
0091             QString label = hook->getField("ButtonText");
0092             if (label.isEmpty())
0093                 label = i18n("Run this action now");
0094             QPushButton *runButton = new QPushButton(QIcon::fromTheme("system-run"), label, content);
0095             runButton->setFixedHeight(runButton->sizeHint().height() * 2);
0096             runButton->setObjectName("runButton");
0097 
0098             QHBoxLayout *buttonLayout = new QHBoxLayout();
0099             buttonLayout->addStretch();
0100             buttonLayout->addWidget(runButton);
0101             buttonLayout->addStretch();
0102             layout->addItem(buttonLayout);
0103 
0104             signalMapper->setMapping(runButton, page);
0105             connect(runButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
0106         }
0107 
0108         m_dialog->addPage(page);
0109         m_pages << page;
0110     }
0111 
0112     connect(signalMapper, SIGNAL(mapped(QObject *)),
0113             this, SLOT(runCommand(QObject *)));
0114 
0115     m_dialog->show();
0116     KWindowSystem::forceActiveWindow(m_dialog->winId());
0117 }
0118 
0119 HookGui::~HookGui()
0120 {
0121     delete m_dialog;
0122 }
0123 
0124 void HookGui::runCommand(QObject *obj) {
0125     KPageWidgetItem *page = (KPageWidgetItem *)obj;
0126     Hook *hook = (Hook *)qvariant_cast<QObject *>(page->property("hook"));
0127     QWidget *widget = page->widget();
0128 
0129     QPushButton *runButton = widget->findChild<QPushButton *>("runButton");
0130     runButton->setEnabled(false);
0131 
0132     hook->runCommand();
0133     hook->setFinished();
0134 }
0135 
0136 void HookGui::closeDialog()
0137 {
0138     m_dialog->deleteLater();
0139     m_dialog = 0;
0140     m_pages.clear();
0141 }