File indexing completed on 2022-09-27 16:24:38

0001 /***************************************************************************
0002  *   Copyright (C) 2008-2018 by Daniel Nicoletti                           *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "PkInstallPackageFiles.h"
0022 
0023 #include "IntroDialog.h"
0024 #include "FilesModel.h"
0025 
0026 #include <KLocalizedString>
0027 #include <QLoggingCategory>
0028 
0029 #include <Daemon>
0030 
0031 Q_DECLARE_LOGGING_CATEGORY(APPER_SESSION)
0032 
0033 PkInstallPackageFiles::PkInstallPackageFiles(uint xid,
0034                                              const QStringList &files,
0035                                              const QString &interaction,
0036                                              const QDBusMessage &message,
0037                                              QWidget *parent)
0038     : SessionTask(xid, interaction, message, parent)
0039 {
0040     setWindowTitle(i18n("Install Packages Files"));
0041 
0042     if (Daemon::global()->roles() & Transaction::RoleInstallFiles) {
0043         m_introDialog = new IntroDialog(this);
0044         m_introDialog->acceptDrops(i18n("You can drop more files in here"));
0045 
0046         m_model = new FilesModel(files, Daemon::global()->mimeTypes(), this);
0047         m_introDialog->setModel(m_model);
0048         connect(m_model, &FilesModel::rowsInserted, this, &PkInstallPackageFiles::modelChanged);
0049         setMainWidget(m_introDialog);
0050 
0051         modelChanged();
0052     } else {
0053         setError(i18n("Not Supported"),
0054                  i18n("Your current backend does not support installing files"));
0055     }
0056 }
0057 
0058 PkInstallPackageFiles::~PkInstallPackageFiles()
0059 {
0060 }
0061 
0062 void PkInstallPackageFiles::modelChanged()
0063 {
0064     const QStringList files = m_model->files();
0065     enableButtonOk(!files.isEmpty());
0066 
0067     QString description;
0068     if (files.isEmpty()) {
0069         description = i18n("No supported files were provided");
0070     } else {
0071         description = i18np("Press <i>Continue</i> if you want to install this file",
0072                             "Press <i>Continue</i> if you want to install these files",
0073                             files.size());
0074     }
0075     m_introDialog->setDescription(description);
0076 
0077     QString title;
0078     // this will come from DBus interface
0079     if (parentTitle.isNull()) {
0080         title = i18np("An application wants to install a package",
0081                       "An application wants to install packages",
0082                       files.size());
0083     } else {
0084         title = i18np("The application <i>%2</i> wants to install a package",
0085                       "The application <i>%2</i> wants to install packages",
0086                       files.size(),
0087                       parentTitle);
0088     }
0089     setTitle(title);
0090 }
0091 
0092 void PkInstallPackageFiles::commit()
0093 {
0094     auto transaction = new PkTransaction(this);
0095     setTransaction(Transaction::RoleInstallFiles, transaction);
0096     connect(transaction, &PkTransaction::finished, this, &PkInstallPackageFiles::transactionFinished, Qt::UniqueConnection);
0097     transaction->installFiles(m_model->files());
0098 }
0099 
0100 void PkInstallPackageFiles::transactionFinished(PkTransaction::ExitStatus status)
0101 {
0102      qCDebug(APPER_SESSION) << "Finished.";
0103      switch (status) {
0104      case PkTransaction::Success :
0105          if (showFinished()) {
0106              setFinish(i18n("Installation Complete"),
0107                        i18np("File was installed successfully",
0108                              "Files were installed successfully",
0109                              m_model->files().count()));
0110          }
0111          finishTaskOk();
0112          break;
0113      case PkTransaction::Cancelled :
0114          sendErrorFinished(Cancelled, QLatin1String("Aborted"));
0115          break;
0116      case PkTransaction::Failed :
0117          if (showWarning()) {
0118              setError(i18n("Failed to install files"),
0119                       i18n("Could not install files"));
0120          }
0121          sendErrorFinished(Failed, i18n("Could not install files"));
0122          break;
0123      }
0124 }
0125 
0126 #include "moc_PkInstallPackageFiles.cpp"