File indexing completed on 2024-04-14 15:48:57

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 "PkRemovePackageByFiles.h"
0022 
0023 #include "IntroDialog.h"
0024 #include "FilesModel.h"
0025 
0026 #include <Daemon>
0027 
0028 #include <PkStrings.h>
0029 
0030 #include <KLocalizedString>
0031 #include <KService>
0032 
0033 #include <QLoggingCategory>
0034 
0035 PkRemovePackageByFiles::PkRemovePackageByFiles(uint xid,
0036                                                const QStringList &files,
0037                                                const QString &interaction,
0038                                                const QDBusMessage &message,
0039                                                QWidget *parent) :
0040     SessionTask(xid, interaction, message, parent)
0041 {
0042     setWindowTitle(i18n("Remove Packages that Provides Files"));
0043 
0044     m_introDialog = new IntroDialog(this);
0045     m_introDialog->acceptDrops(i18n("You can drop more files in here"));
0046     m_model = new FilesModel(files, QStringList(), this);
0047     connect(m_model, &FilesModel::rowsInserted, this, &PkRemovePackageByFiles::modelChanged);
0048     m_introDialog->setModel(m_model);
0049     setMainWidget(m_introDialog);
0050 
0051     modelChanged();
0052 }
0053 
0054 PkRemovePackageByFiles::~PkRemovePackageByFiles()
0055 {
0056 }
0057 
0058 void PkRemovePackageByFiles::modelChanged()
0059 {
0060     QStringList files = m_model->files();
0061     enableButtonOk(!files.isEmpty());
0062 
0063     QString description;
0064     if (files.isEmpty()) {
0065         description = i18n("No supported files were provided");
0066     } else {
0067         if (m_model->onlyApplications()) {
0068             description = i18np("Do you want to remove the following application?",
0069                                 "Do you want to remove the following applications?",
0070                                 files.size());
0071         } else {
0072             description = i18np("Do you want to search for a package providing this file?",
0073                                 "Do you want to search for a package providing these files?",
0074                                 files.size());
0075         }
0076     }
0077     m_introDialog->setDescription(description);
0078 
0079     QString title;
0080     // this will come from DBus interface
0081     if (!files.isEmpty() && parentTitle.isNull()) {
0082         if (m_model->onlyApplications()) {
0083             title = i18np("An application is asking to remove an application",
0084                           "An application is asking to remove applications",
0085                           files.size());
0086         } else {
0087             title = i18np("An application is asking to remove a file",
0088                           "An application is asking to remove files",
0089                           files.size());
0090         }
0091     } else if (!files.isEmpty()) {
0092         if (m_model->onlyApplications()) {
0093             title = i18np("The application <i>%2</i> is asking to remove an application",
0094                           "The application <i>%2</i> is asking to remove applications",
0095                           files.size(),
0096                           parentTitle);
0097         } else {
0098             title = i18np("The application <i>%2</i> is asking to remove a file",
0099                           "The application <i>%2</i> is asking to remove files",
0100                           files.size(),
0101                           parentTitle);
0102         }
0103     } else {
0104         title = i18n("No application was found");
0105     }
0106     setTitle(title);
0107 }
0108 
0109 void PkRemovePackageByFiles::search()
0110 {
0111     m_files = m_model->files();
0112     searchFinished(PkTransaction::Success);
0113 }
0114 
0115 void PkRemovePackageByFiles::searchFinished(PkTransaction::ExitStatus status)
0116 {
0117     if (status == PkTransaction::Success) {
0118         if (!m_files.isEmpty()) {
0119             QString file = m_files.takeFirst();
0120 
0121             auto transaction = new PkTransaction(this);
0122             transaction->setupTransaction(Daemon::searchFiles(file, Transaction::FilterInstalled));
0123             setTransaction(Transaction::RoleSearchFile, transaction);
0124             connect(transaction, &PkTransaction::finished, this, &PkRemovePackageByFiles::searchFinished, Qt::UniqueConnection);
0125             connect(transaction, &PkTransaction::package, this, &PkRemovePackageByFiles::addPackage);
0126         } else {
0127             // we are done resolving
0128             SessionTask::searchFinished(status);
0129         }
0130     } else {
0131         // we got an error...
0132         SessionTask::searchFinished(status);
0133     }
0134 }
0135 
0136 void PkRemovePackageByFiles::notFound()
0137 {
0138     if (showWarning()) {
0139         QStringList files = m_model->files();
0140         setInfo(i18n("Could not find %1", files.join(QLatin1String(", "))),
0141                 i18np("The file could not be found in any installed package",
0142                       "The files could not be found in any installed package",
0143                       files.size()));
0144     }
0145     sendErrorFinished(NoPackagesFound, QLatin1String("no package found"));
0146 }
0147 
0148 #include "moc_PkRemovePackageByFiles.cpp"