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

0001 /***************************************************************************
0002  *   Copyright (C) 2012 by Daniel Nicoletti dantti12@gmail.com             *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; see the file COPYING. If not, write to       *
0016  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0017  *   Boston, MA 02110-1301, USA.                                           *
0018  ***************************************************************************/
0019 
0020 #include "RefreshCacheTask.h"
0021 
0022 #include <PkStrings.h>
0023 #include <PkIcons.h>
0024 
0025 #include <Daemon>
0026 
0027 #include <KLocalizedString>
0028 
0029 #include <QLoggingCategory>
0030 
0031 RefreshCacheTask::RefreshCacheTask(QObject *parent) :
0032     QObject(parent),
0033     m_transaction(nullptr),
0034     m_notification(nullptr),
0035     m_lastError(Transaction::ErrorUnknown),
0036     m_cacheAge(3600)
0037 {
0038 }
0039 
0040 void RefreshCacheTask::refreshCache()
0041 {
0042 //    kDebug();
0043     if (!m_transaction) {
0044         // Refresh Cache is false otherwise it will rebuild
0045         // the whole cache on Fedora
0046         Daemon::setHints (QLatin1String("cache-age=") + QString::number(m_cacheAge));
0047         m_transaction = Daemon::refreshCache(false);
0048         connect(m_transaction, &Transaction::finished, this, &RefreshCacheTask::refreshCacheFinished);
0049         connect(m_transaction, &Transaction::errorCode, this, &RefreshCacheTask::errorCode);
0050     }
0051 }
0052 
0053 void RefreshCacheTask::refreshCacheFinished(PackageKit::Transaction::Exit status, uint runtime)
0054 {
0055     Q_UNUSED(runtime)
0056 
0057     m_transaction = nullptr;
0058     if (status == Transaction::ExitSuccess) {
0059         m_lastError = Transaction::ErrorUnknown;
0060         m_lastErrorString.clear();
0061     }
0062 }
0063 
0064 void RefreshCacheTask::errorCode(Transaction::Error error, const QString &errorMessage)
0065 {
0066     if (m_notification || (m_lastError == error && m_lastErrorString == errorMessage)) {
0067         return;
0068     }
0069 
0070     m_notification = new KNotification(QLatin1String("TransactionFailed"), KNotification::Persistent, this);
0071     m_notification->setComponentName(QLatin1String("apperd"));
0072     connect(m_notification, &KNotification::closed, this, &RefreshCacheTask::notificationClosed);
0073     QIcon icon = QIcon::fromTheme(QLatin1String("dialog-cancel"));
0074     // use of QSize does the right thing
0075     m_notification->setPixmap(icon.pixmap(QSize(KPK_ICON_SIZE, KPK_ICON_SIZE)));
0076     m_notification->setTitle(PkStrings::error(error));
0077     m_notification->setText(errorMessage);
0078     m_notification->sendEvent();
0079 }
0080 
0081 void RefreshCacheTask::notificationClosed()
0082 {
0083     m_notification->deleteLater();
0084     m_notification = nullptr;
0085 }
0086 
0087 #include "moc_RefreshCacheTask.cpp"