upgradeoperation.cpp 5.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* This file is part of the Chakra project

   Copyright (C) 2011 Lukas Appelhans

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
*/

#include "upgradeoperation.h"

#include <kdebug.h>
#include <akabeiclientbackend.h>
#include <akabeiclientqueue.h>
#include <akabeiclienttransactionhandler.h>
#include <klocale.h>
#include <akabeiconfig.h>
#include <QFile>
#include <kio/global.h>
#include <QDir>
#include <akabeioperationrunner.h>
#include <QCoreApplication>
Lukas Appelhans's avatar
Lukas Appelhans committed
24
#include <iostream>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

//FIXME: There is actually a bunch of code duplication with SyncOperation and RemoveOperation... maybe we can create something that is used in all of them :)

UpgradeOperation::UpgradeOperation(QObject * parent)
  : QObject(parent)
{

}

UpgradeOperation::~UpgradeOperation()
{

}

void UpgradeOperation::start(QList< APM::Operation > operations, QMultiHash< APM::Operation, QString > options, QStringList args)
{
    if (args.isEmpty()) {
        return;
    }
    QList<Akabei::Package*> pkgs;
    foreach (const QString &p, args)
        pkgs << Akabei::Backend::instance()->loadPackageFromFile(p);
    
    foreach (Akabei::Package * pkg, pkgs)
        AkabeiClient::Backend::instance()->queue()->addPackage(pkg, AkabeiClient::Install);
    
    Akabei::ProcessingOptions processingOpts;
    if (operations.contains(APM::Force))
        processingOpts |= Akabei::Force;
    if (operations.contains(APM::AsDeps))
        processingOpts |= Akabei::InstallAsDependencies;
    if (operations.contains(APM::AsExplicit))
        processingOpts |= Akabei::InstallAsExplicit;
    if (operations.contains(APM::SkipDependencyCheck))
        processingOpts |= Akabei::SkipDependencies;
    if (operations.contains(APM::DownloadOnly))
        processingOpts |= Akabei::DownloadOnly;
    
    connect(AkabeiClient::Backend::instance()->transactionHandler(), SIGNAL(validationFinished(bool)), SLOT(validationFinished(bool)));
    connect(AkabeiClient::Backend::instance()->transactionHandler(), SIGNAL(errorsOccurred(QList<Akabei::Error*>&)), SLOT(errors(QList<Akabei::Error*>&)));
    connect(AkabeiClient::Backend::instance()->transactionHandler(), SIGNAL(newTransactionMessage(QString)), SLOT(transactionMessage(QString)));
    AkabeiClient::Backend::instance()->transactionHandler()->validate(processingOpts);
}

void UpgradeOperation::validationFinished(bool valid)
{
    QTextStream out(stdout);
    if (!valid) {
        out << i18n("The transaction could not be validated") << endl;
        out.flush();
        return;
    }
    int downloadSize = 0;
    int installSize = 0;    
    out << i18n("Packages: ");
    foreach (AkabeiClient::QueueItem * item, AkabeiClient::Backend::instance()->queue()->items()) {
        if (item != AkabeiClient::Backend::instance()->queue()->items().first())
            out << ", ";
        out << item->package()->name();
        if (item->package()->database() != Akabei::Backend::instance()->localDatabase() && !QFile::exists(Akabei::Config::instance()->cacheDir().absoluteFilePath(item->package()->filename())))
            downloadSize += item->package()->size();
        installSize += item->package()->installedSize();
        if (item->action() == AkabeiClient::Install && !(Akabei::Backend::instance()->operationRunner()->processingOptions() & Akabei::SkipDependencies)) { //FIXME: Why this?
            foreach (Akabei::Package * dep, item->dependencyTree()) {
                out << ", " << dep->name();
                if (dep->database() != Akabei::Backend::instance()->localDatabase() && !QFile::exists(Akabei::Config::instance()->cacheDir().absoluteFilePath(dep->filename())))
                    downloadSize += dep->size();
                installSize += dep->installedSize();
            }
        }
    }
    out << endl << endl;
    out << i18n("Download size: %1", KIO::convertSize(downloadSize)) << endl;
    out << i18n("Installed size: %1", KIO::convertSize(installSize)) << endl;
    out.flush();
    
Lukas Appelhans's avatar
Lukas Appelhans committed
101
    out << i18n("Continue with installation?[Y/n]");
102
    out.flush();
Lukas Appelhans's avatar
Lukas Appelhans committed
103 104 105 106 107 108
    std::string input;
    getline(std::cin, input);
    if (!input.empty() && input != i18n("y").toStdString()) {
        QCoreApplication::instance()->quit();
        return;
    }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    connect(AkabeiClient::Backend::instance()->transactionHandler(), SIGNAL(progressChanged(int)), SLOT(showProgress(int)));
    connect(AkabeiClient::Backend::instance()->transactionHandler(), SIGNAL(finished()), SLOT(finished()));
    AkabeiClient::Backend::instance()->transactionHandler()->process();
}

void UpgradeOperation::showProgress(int progress)
{
    QTextStream out(stdout);
    out.reset();
    out << " [";
    for (int i = 1; i != 11; i++) {
        if (progress / i >= 10) {
            out << "#";
        } else {
            out << "-";
        }
    }
    out << "] " << progress << "%";//FIXME
    if (progress == 100) {
        out << endl;
    } else {
        out << "\r";
    }
    out.flush();
}

void UpgradeOperation::finished()
{
    QTextStream out(stdout);
    if (Akabei::Backend::instance()->operationRunner()->errors().isEmpty()) {
        out << i18n("The transaction finished successfully!") << endl;
    } else {
        out << i18np("There has been an error during the transaction.", "There have been errors during the transaction.", Akabei::Backend::instance()->operationRunner()->errors().count());
    }
    QCoreApplication::instance()->quit();
}

void UpgradeOperation::errors(QList<Akabei::Error*>& errors)
{
    kDebug() << "yoyoyo";
    QTextStream out(stdout);
    out << i18n("Errors occurred: ") << endl;
    foreach (Akabei::Error * err, errors) {
        out << err->description() << endl;
    }
    out.flush();
}

void UpgradeOperation::transactionMessage(const QString& message)
{
    QTextStream out(stdout);
    out << message << endl;
}