File indexing completed on 2024-05-12 05:48:44

0001 /***************************************************************************
0002  *   Copyright © 2009-2010 Jonathan Thomas <echidnaman@kubuntu.org>        *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or         *
0005  *   modify it under the terms of the GNU General Public License as        *
0006  *   published by the Free Software Foundation; either version 2 of        *
0007  *   the License or (at your option) version 3 or any later version        *
0008  *   accepted by the membership of KDE e.V. (or its successor approved     *
0009  *   by the membership of KDE e.V.), which shall act as a proxy            *
0010  *   defined in Section 14 of version 3 of the license.                    *
0011  *                                                                         *
0012  *   This program is distributed in the hope that it will be useful,       *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0015  *   GNU General Public License for more details.                          *
0016  *                                                                         *
0017  *   You should have received a copy of the GNU General Public License     *
0018  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0019  ***************************************************************************/
0020 
0021 #include "installgui.h"
0022 
0023 // Qt includes
0024 #include <QDialog>
0025 #include <QLabel>
0026 #include <QProcess>
0027 #include <QPushButton>
0028 #include <QVBoxLayout>
0029 
0030 // KDE includes
0031 #include <KLocalizedString>
0032 #include <KNotification>
0033 
0034 InstallGui::InstallGui(QObject* parent, const QString &application, const QMap<QString, QString> packageList)
0035         : QObject(parent)
0036         , m_dialog(0)
0037         , m_installProcess(0)
0038         , m_applicationName(application)
0039 {
0040     m_dialog = new QDialog();
0041     m_dialog->setWindowIcon(QIcon::fromTheme("muondiscover"));
0042     m_dialog->setWindowTitle(i18n("Install Packages"));
0043     QVBoxLayout *layout = new QVBoxLayout(m_dialog);
0044     m_dialog->setLayout(layout);
0045 
0046     QLabel *label = new QLabel(m_dialog);
0047     label->setWordWrap(true);
0048     label->setText(i18n("Select packages to be installed for extra functionality."
0049                         " These packages are not installed by default due to either patent"
0050                         " issues, restrictive licensing or a lack of space on the installation"
0051                         " media."));
0052     layout->addWidget(label);
0053 
0054     QListWidget *listWidget = new QListWidget(m_dialog);
0055     connect(listWidget, SIGNAL(itemChanged(QListWidgetItem *)), SLOT(packageToggled(QListWidgetItem *)));
0056     layout->addWidget(listWidget);
0057 
0058     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, m_dialog);
0059     connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(runPackageInstall()));
0060     connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(cleanUpDialog()));
0061     layout->addWidget(m_buttonBox);
0062     
0063     QMap<QString, QString>::const_iterator nameIter = packageList.constBegin();
0064     while (nameIter != packageList.constEnd()) {
0065         QListWidgetItem *item = new QListWidgetItem(nameIter.value());
0066         item->setToolTip(nameIter.key());
0067         m_toInstallList << nameIter.key();
0068         item->setCheckState(Qt::Checked);
0069         listWidget->addItem(item);
0070         ++nameIter;
0071     }
0072 
0073     m_dialog->show();
0074 }
0075 
0076 InstallGui::~InstallGui()
0077 {
0078     delete m_dialog;
0079 }
0080 
0081 void InstallGui::packageToggled(QListWidgetItem *item)
0082 {
0083     QString packageName = item->toolTip();
0084     if (item->checkState() == Qt::Checked) {
0085         m_toInstallList << packageName;
0086     } else {
0087         m_toInstallList.removeOne(packageName);
0088     }
0089     m_buttonBox->button(QDialogButtonBox::Ok)->setDisabled(m_toInstallList.isEmpty());
0090 }
0091 
0092 void InstallGui::runPackageInstall()
0093 {
0094     m_dialog->accept();
0095     m_installProcess = new QProcess(this);
0096     connect(m_installProcess, SIGNAL(finished(int)), this, SLOT(installFinished(int result)));
0097 
0098     m_installProcess->start("qapt-batch", QStringList() << "--install" << m_toInstallList);
0099 }
0100 
0101 void InstallGui::installFinished(int result)
0102 {
0103     if (result == 1) {
0104         // QApt Batch will handle error notification, we just need to know to shut up
0105         return;
0106     }
0107 
0108     KNotification *notify = new KNotification("Install", 0);
0109     notify->setComponentName("notificationhelper");
0110 
0111     notify->setPixmap(QIcon::fromTheme("download").pixmap(22,22));
0112     notify->setText(i18n("Installation complete. You will need to restart %1"
0113                          " to use the new functionality", m_applicationName));
0114     notify->sendEvent();
0115 }
0116 
0117 void InstallGui::cleanUpDialog()
0118 {
0119     m_dialog->deleteLater();
0120     m_dialog = 0;
0121 }
0122 
0123 #include "installgui.moc"