removeoperation.cpp 5.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 "removeoperation.h"
#include "apm.h"
#include <akabeiclientbackend.h>
#include <qtextstream.h>
#include <akabeidatabase.h>
#include <akabeiclientqueue.h>
#include <akabeiclienttransactionhandler.h>
18
#include <akabeioperationrunner.h>
19 20
#include <kdebug.h>
#include <kio/global.h>
21
#include <qcoreapplication.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
22 23
#include <klocale.h>
#include <iostream>
24 25 26 27 28 29 30 31 32 33 34 35 36 37

RemoveOperation::RemoveOperation(QObject * parent)
  : QObject(parent)
{

}

RemoveOperation::~RemoveOperation()
{

}

void RemoveOperation::start(QList<APM::Operation> operations, QMultiHash<APM::Operation, QString> options, QStringList args)
{
38 39 40
    m_operations = operations;
    m_options = options;
    m_args = args;
41 42 43 44 45
    APM::Operation operation = APM::NoOperation;
    if (!operations.isEmpty())
        operation = operations.first();
    if (operation == APM::Unneeded) { //TODO: Implement those!
    } else if (operation == APM::Recursive) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    } else {
        QString query = "SELECT * FROM packages WHERE";
        foreach (const QString &pkg, args) {
            query = query + " Name LIKE \"" + pkg + "\"";
            if (args.last() != pkg)
                query = query + " OR";
        }
        remove(Akabei::Backend::instance()->localDatabase()->queryPackages(query));
    }
}

void RemoveOperation::remove(QList<Akabei::Package*> packages)
{
    QTextStream out(stdout);
    foreach (Akabei::Package * pkg, packages) {
        AkabeiClient::Backend::instance()->queue()->addPackage(pkg, AkabeiClient::Remove);
    }
63
    Akabei::ProcessingOptions options;
64 65
    if (m_operations.contains(APM::SkipDependencyCheck))
        options |= Akabei::SkipDependencies;
Lukas Appelhans's avatar
Lukas Appelhans committed
66 67 68 69
    if (m_operations.contains(APM::RemoveConfig))
        options |= Akabei::RemoveConfigs;
    if (m_operations.contains(APM::DatabaseOnly))
        options |= Akabei::DatabaseOnly;
70 71
    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*>&)));
72
    AkabeiClient::Backend::instance()->transactionHandler()->validate(options);
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 101 102 103
}

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

void RemoveOperation::validationFinished(bool valid)
{
    QTextStream out(stdout);
    if (!valid) {
        //foreach (Akabei::Operation * op, Akabei::Backend::instance()->operationRunner()->operations()) {
            //kDebug() << op->fileSystemRemovals();
        //}
        out << "Queue not valid" << endl;
        out.flush();
        return;
    }
    int removeSize = 0;
    out << "Packages: ";
    foreach (AkabeiClient::QueueItem * item, AkabeiClient::Backend::instance()->queue()->items()) {
        if (item != AkabeiClient::Backend::instance()->queue()->items().first())
            out << ", ";
        out << item->package()->name();
        removeSize += item->package()->installedSize();
104
        if (item->action() == AkabeiClient::Remove && !(Akabei::Backend::instance()->operationRunner()->processingOptions() & Akabei::SkipDependencies)) { //FIXME: Why this?
105 106 107 108 109 110 111 112 113 114
            foreach (Akabei::Package * dep, item->requiredByTree()) {
                out << ", " << dep->name();
                removeSize += dep->installedSize();
            }
        }
    }
    out << endl << endl;
    out << "Size of packages about to be removed: " << KIO::convertSize(removeSize) << endl;
    out.flush();
    
Lukas Appelhans's avatar
Lukas Appelhans committed
115
    out << "Continue with installation?[Y/n]";
116
    out.flush();
Lukas Appelhans's avatar
Lukas Appelhans committed
117 118 119 120 121 122
    std::string input;
    getline(std::cin, input);
    if (!input.empty() && input != i18n("y").toStdString()) {
        QCoreApplication::instance()->quit();
        return;
    }
123 124
    connect(AkabeiClient::Backend::instance()->transactionHandler(), SIGNAL(progressChanged(int)), SLOT(showProgress(int)));
    connect(AkabeiClient::Backend::instance()->transactionHandler(), SIGNAL(finished()), SLOT(finished()));
125
    AkabeiClient::Backend::instance()->transactionHandler()->process();
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
}

void RemoveOperation::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 RemoveOperation::finished()
{
    QTextStream out(stdout);
    out << "The transaction finished!" << endl;
153
    QCoreApplication::instance()->quit();
154
}