File indexing completed on 2024-04-28 17:02:40

0001 /***************************************************************************
0002  *   Copyright (C) 2008-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 "OriginModel.h"
0022 
0023 #include <Daemon>
0024 #include <PkStrings.h>
0025 
0026 #include <KMessageBox>
0027 #include <KLocalizedString>
0028 
0029 #include <QLoggingCategory>
0030 
0031 using namespace PackageKit;
0032 
0033 OriginModel::OriginModel(QObject *parent) :
0034     QStandardItemModel(parent),
0035     m_finished(true)
0036 {
0037     setHorizontalHeaderLabels({ i18n("Origin of Packages") });
0038 }
0039 
0040 
0041 OriginModel::~OriginModel()
0042 {
0043 }
0044 
0045 bool OriginModel::setData(const QModelIndex &index, const QVariant &value, int role)
0046 {
0047     if (role == Qt::CheckStateRole && index.isValid()) {
0048         Transaction *transaction = Daemon::repoEnable(index.data(RepoId).toString(),
0049                                                       value.toBool());
0050         connect(transaction, &Transaction::errorCode, this, &OriginModel::errorCode);
0051         connect(transaction, &Transaction::finished, this, &OriginModel::setRepoFinished);
0052     }
0053     return false;
0054 }
0055 
0056 QVariantHash OriginModel::changes() const
0057 {
0058     QVariantHash ret;
0059     for (int i = 0; i < rowCount(); ++i) {
0060         QStandardItem *repo = item(i);
0061         bool currentState = repo->checkState();
0062         if (currentState != repo->data(RepoInitialState).toBool()) {
0063             ret[repo->data(RepoId).toString()] = currentState;
0064         }
0065     }
0066     return ret;
0067 }
0068 
0069 void OriginModel::addOriginItem(const QString &repo_id, const QString &details, bool enabled)
0070 {
0071     if (m_finished) {
0072         // if we received a finished signal this is a new query
0073         removeRows(0, rowCount());
0074         m_finished = false;
0075     }
0076 
0077     auto item = new QStandardItem(details);
0078     item->setCheckable(true);
0079     item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
0080     item->setData(repo_id, RepoId);
0081     item->setData(enabled, RepoInitialState);
0082     appendRow(item);
0083 }
0084 
0085 void OriginModel::finished()
0086 {
0087     m_finished = true;
0088 }
0089 
0090 void OriginModel::errorCode(PackageKit::Transaction::Error error, const QString &details)
0091 {
0092     if (error != Transaction::ErrorTransactionCancelled) {
0093         KMessageBox::detailedError(nullptr, PkStrings::errorMessage(error), details, PkStrings::error(error), KMessageBox::Notify);
0094     }
0095 }
0096 
0097 void OriginModel::setRepoFinished(Transaction::Exit exit)
0098 {
0099     if (exit == Transaction::ExitSuccess) {
0100         emit refreshRepoList();
0101     }
0102     sender()->deleteLater();
0103 }
0104 
0105 #include "moc_OriginModel.cpp"