File indexing completed on 2024-04-21 16:29:39

0001 /***************************************************************************
0002  *   Copyright (C) 2009-2011 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 "PkInstallProvideFiles.h"
0022 #include "IntroDialog.h"
0023 #include "FilesModel.h"
0024 
0025 #include <Daemon>
0026 
0027 #include <PkStrings.h>
0028 
0029 #include <KLocalizedString>
0030 
0031 #include <QLoggingCategory>
0032 
0033 PkInstallProvideFiles::PkInstallProvideFiles(uint xid,
0034                                              const QStringList &files,
0035                                              const QString &interaction,
0036                                              const QDBusMessage &message,
0037                                              QWidget *parent)
0038  : SessionTask(xid, interaction, message, parent),
0039    m_args(files)
0040 {
0041     setWindowTitle(i18n("Install Packages that Provides Files"));
0042 
0043     auto introDialog = new IntroDialog(this);
0044     auto model = new FilesModel(files, QStringList(), this);
0045     introDialog->setModel(model);
0046     connect(introDialog, &IntroDialog::continueChanged, this, &PkInstallProvideFiles::enableButtonOk);
0047     setMainWidget(introDialog);
0048 
0049     if (m_args.isEmpty()) {
0050         introDialog->setDescription(i18n("No files were provided"));
0051     } else {
0052         QString description;
0053         description = i18np("Do you want to search for this now?",
0054                             "Do you want to search for these now?",
0055                             m_args.size());
0056         introDialog->setDescription(description);
0057         enableButtonOk(true);
0058     }
0059 
0060     QString title;
0061     // this will come from DBus interface
0062     if (parentTitle.isNull()) {
0063         title = i18np("A program wants to install a file",
0064                       "A program wants to install files",
0065                       m_args.size());
0066     } else {
0067         title = i18np("The application %2 is asking to install a file",
0068                       "The application %2 is asking to install files",
0069                       m_args.size(),
0070                       parentTitle);
0071     }
0072     setTitle(title);
0073 }
0074 
0075 PkInstallProvideFiles::~PkInstallProvideFiles()
0076 {
0077 }
0078 
0079 void PkInstallProvideFiles::search()
0080 {
0081     auto transaction = new PkTransaction(this);
0082     transaction->setupTransaction(Daemon::searchFiles(m_args,
0083                                                       Transaction::FilterArch | Transaction::FilterNewest));
0084     setTransaction(Transaction::RoleSearchFile, transaction);
0085     connect(transaction, &PkTransaction::finished, this, &PkInstallProvideFiles::searchFinished, Qt::UniqueConnection);
0086     connect(transaction, &PkTransaction::package, this, &PkInstallProvideFiles::addPackage);
0087 }
0088 
0089 void PkInstallProvideFiles::notFound()
0090 {
0091     if (m_alreadyInstalled.size()) {
0092         if (showWarning()) {
0093             setInfo(i18n("Failed to install file"),
0094                     i18n("The %1 package already provides this file",
0095                          m_alreadyInstalled));
0096         }
0097         sendErrorFinished(Failed, QLatin1String("already provided"));
0098     } else {
0099         if (showWarning()) {
0100             setInfo(i18n("Failed to find package"),
0101                     i18np("The file could not be found in any packages",
0102                           "The files could not be found in any packages",
0103                           m_args.size()));
0104         }
0105         sendErrorFinished(NoPackagesFound, QLatin1String("no files found"));
0106     }
0107 }
0108 
0109 void PkInstallProvideFiles::addPackage(Transaction::Info info, const QString &packageID, const QString &summary)
0110 {
0111     if (info != Transaction::InfoInstalled) {
0112         SessionTask::addPackage(info, packageID, summary);
0113     } else {
0114         m_alreadyInstalled = Transaction::packageName(packageID);
0115     }
0116 }
0117 
0118 #include "moc_PkInstallProvideFiles.cpp"