File indexing completed on 2024-04-21 05:45:00

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 "DistroUpgrade.h"
0022 
0023 #include <PkIcons.h>
0024 
0025 #include <QAction>
0026 
0027 #include <KLocalizedString>
0028 #include <KMessageBox>
0029 #include <KColorScheme>
0030 
0031 //#include <solid/powermanagement.h>
0032 #include <solid/device.h>
0033 
0034 #include <QLoggingCategory>
0035 
0036 DistroUpgrade::DistroUpgrade(QWidget *parent) :
0037     KMessageWidget(parent)
0038 {
0039     auto action = new QAction(i18n("Upgrade"), this);
0040     connect(action, &QAction::triggered, this, &DistroUpgrade::startDistroUpgrade);
0041     addAction(action);
0042 }
0043 
0044 DistroUpgrade::~DistroUpgrade()
0045 {
0046 }
0047 
0048 void DistroUpgrade::setName(const QString &name)
0049 {
0050     setText(i18n("Distribution upgrade available: %1", name));
0051 }
0052 
0053 void DistroUpgrade::startDistroUpgrade()
0054 {
0055     //! QList<Solid::Device> powerPlugs = Solid::Device::listFromType(Solid::DeviceInterface::AcAdapter);
0056     bool pluggedIn = true;
0057     bool hasBattery = Solid::Device::listFromType(Solid::DeviceInterface::Battery).size()>0;
0058 
0059     //! foreach(const Solid::Device &dev, powerPlugs) {
0060     //!    if (!dev.as<Solid::AcAdapter>()->isPlugged()) {
0061     //!        pluggedIn = false;
0062     //!    }
0063     //! }
0064 
0065     QString warning = i18n("You are about to upgrade your distribution to the latest version. "
0066                            "This is usually a very lengthy process and takes a lot longer than "
0067                            "simply upgrading your packages.");
0068 
0069     if (!pluggedIn) {
0070         warning += QLatin1Char(' ') + i18n("It is recommended to plug in your computer before proceeding.");
0071     } else if (hasBattery) {
0072         warning += QLatin1Char(' ') + i18n("It is recommended that you keep your computer plugged in while the upgrade is being performed.");
0073     }
0074 
0075     if (KMessageBox::warningContinueCancel(this,warning) == KMessageBox::Continue) {
0076         m_distroUpgradeProcess = new QProcess;
0077         connect(m_distroUpgradeProcess, &QProcess::errorOccurred, this, &DistroUpgrade::distroUpgradeError);
0078         connect(m_distroUpgradeProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
0079                 this, &DistroUpgrade::distroUpgradeFinished);
0080         QStringList env = QProcess::systemEnvironment();
0081         env << QLatin1String("DESKTOP=kde");
0082         m_distroUpgradeProcess->setEnvironment(env);
0083         m_distroUpgradeProcess->start(QLatin1String("/usr/share/PackageKit/pk-upgrade-distro.sh"));
0084     }
0085 }
0086 
0087 void DistroUpgrade::distroUpgradeFinished(int exitCode, QProcess::ExitStatus exitStatus)
0088 {
0089     if (exitStatus == QProcess::NormalExit && exitCode == 0) {
0090         KMessageBox::information(this, i18n("Distribution upgrade complete."));
0091     } else if (exitStatus == QProcess::NormalExit) {
0092         KMessageBox::error(this, i18n("Distribution upgrade process exited with code %1.", exitCode));
0093     }
0094     m_distroUpgradeProcess->deleteLater();
0095     m_distroUpgradeProcess = nullptr;
0096 }
0097 
0098 void DistroUpgrade::distroUpgradeError(QProcess::ProcessError error)
0099 {
0100     QString text;
0101     switch(error) {
0102         case QProcess::FailedToStart:
0103             KMessageBox::error(this,
0104                     i18n("The distribution upgrade process failed to start."));
0105             break;
0106         case QProcess::Crashed:
0107             KMessageBox::error(this,
0108                     i18n("The distribution upgrade process crashed some time after starting successfully."));
0109             break;
0110         default:
0111             KMessageBox::error(this,
0112                     i18n("The distribution upgrade process failed with an unknown error."));
0113             break;
0114     }
0115 }
0116 
0117 #include "moc_DistroUpgrade.cpp"