apm.cpp 5.23 KB
Newer Older
Lukas Appelhans's avatar
Lukas Appelhans committed
1 2 3 4 5 6 7 8 9
/* This file is part of the Chakra project

   Copyright (C) 2011 Lukas Appelhans <l.appelhans@gmx.de>

   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.
*/
10 11
#include <apm.h>
#include <akabeioptions.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
12

13 14 15 16 17
#include <queryoperation.h>
#include <syncoperation.h>
#include <removeoperation.h>
#include <upgradeoperation.h>
#include <performoperation.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
18

19
#include <akabeiclientbackend.h>
20
#include <akabeiconfig.h>
21

Lukas Appelhans's avatar
Lukas Appelhans committed
22 23
#include <QStringList>
#include <QTextStream>
24 25
#include <QCoreApplication>
#include <QTimer>
Lisa's avatar
Lisa committed
26
#include <QLocale>
Lukas Appelhans's avatar
Lukas Appelhans committed
27

28
OperationManager::OperationManager(APM::OperationType type, QList<APM::OperationName> operations, QHash<APM::OptionName, QStringList> options, QStringList args, QObject * parent)
Lisa's avatar
Lisa committed
29 30
  : QObject(parent),
    m_type(type),
Lisa's avatar
Lisa committed
31
    m_operations(operations),
Lisa's avatar
Lisa committed
32 33
    m_options(options),
    m_args(args)
34
{    
Lukas Appelhans's avatar
Lukas Appelhans committed
35
    Akabei::ErrorQueue::instance()->registerForAllErrors(this, "showErrors");
Lukas Appelhans's avatar
Lukas Appelhans committed
36
    connect(AkabeiClient::Backend::instance(), SIGNAL(statusChanged(Akabei::Backend::Status)), SLOT(statusChanged(Akabei::Backend::Status)), Qt::QueuedConnection);
Lisa's avatar
Lisa committed
37
    
Lisa's avatar
Lisa committed
38 39 40
    QString root;
    QString cache;
    QString dbs;
41
    QString config;
42

43
    if (options.contains(APM::RootDir)) {
44
        root = options[APM::RootDir].at(0);
45 46
    }
    if (options.contains(APM::CacheDir)) {
47
        cache = options[APM::CacheDir].at(0);
48 49
    }
    if (options.contains(APM::DBPath)) {
50
        dbs = options[APM::DBPath].at(0);
51
    }
52 53 54
    if (options.contains(APM::ConfigFile)) {
        config = options[APM::ConfigFile].at(0);
    }
Lukas Appelhans's avatar
Lukas Appelhans committed
55

56 57
    /* Used to determine whether debugging prints are to be displayed later */
    Akabei::Config::instance()->setDebug(m_options.contains(APM::Debug));
Lukas Appelhans's avatar
Lukas Appelhans committed
58

Lisa's avatar
Lisa committed
59 60
    QLocale systemLocale = QLocale::system();
    AkabeiClient::Backend::instance()->setLocale( systemLocale.name() );
61
    AkabeiClient::Backend::instance()->initialize(root, cache, dbs, QUrl(config));
62 63
}

64 65 66 67
OperationManager::~OperationManager()
{
}

Lisa's avatar
Lisa committed
68 69 70 71 72 73 74
void OperationManager::showErrors()
{
    QTextStream err(stderr);
    Akabei::Error error = Akabei::ErrorQueue::instance()->takeError();
    
    switch (error.type()) {
        case Akabei::Error::BackendInitializationError: {
75 76 77
            err << Akabei::errorPrefix << QObject::tr("The backend seems to be in a broken state. ");
            err << error.description().toUtf8().data() << endl;
            err << Akabei::errorPrefix << QObject::tr("This often means that you don't have enough permissions to perform this operation.");
Lisa's avatar
Lisa committed
78 79 80 81
            break;
        }
        
        default:
82
            err << Akabei::errorPrefix << error.description().toUtf8().data();
Lisa's avatar
Lisa committed
83 84 85 86 87 88 89
            break;
    }
    
    err << endl;
    Akabei::Backend::instance()->deInit();
    QCoreApplication::instance()->quit();
}
90

91
void OperationManager::statusChanged(Akabei::Backend::Status status)
92
{
93
    switch (status) {
94
        case Akabei::Backend::StatusReady: {
95 96
            start();
            break;
97
        }
Lukas Appelhans's avatar
Lukas Appelhans committed
98

99 100
        case Akabei::Backend::StatusWaitingForLock: {
            QTextStream out(stdout);
101
            out << QObject::tr("Acquiring the lock ... (this will wait for other running instances to complete)") << endl;
102 103
            break;
        }
Lukas Appelhans's avatar
Lukas Appelhans committed
104

105
        case Akabei::Backend::StatusBroken: {
Lisa's avatar
Lisa committed
106
            QTextStream err(stderr);
Lisa's avatar
Lisa committed
107
            err << Akabei::errorPrefix << QObject::tr("The backend seems to be in a broken state. ");
Lisa's avatar
Lisa committed
108 109
            err << endl;
            exit(1);
110
        }
111 112 113
        default:
            break;
    }
114 115
}

116
void OperationManager::start()
Lukas Appelhans's avatar
Lukas Appelhans committed
117
{
118
    switch (m_type) {
Lisa's avatar
Lisa committed
119
        case APM::QueryOperationType: {
Lisa's avatar
Lisa committed
120
            QueryOperation * query = new QueryOperation(m_operations, m_options, m_args, this);
121
            connect(query, SIGNAL(queryFinished()), this, SLOT(quit()));
Lisa's avatar
Lisa committed
122
            query->start();
Lisa's avatar
Lisa committed
123 124 125
            break;
        }
        case APM::RemoveOperationType: {
Lisa's avatar
Lisa committed
126
            RemoveOperation * remove = new RemoveOperation(m_operations, m_options, m_args, this);
127
            connect(remove, SIGNAL(removeFinished()), this, SLOT(quit()));
Lisa's avatar
Lisa committed
128
            remove->start();
Lisa's avatar
Lisa committed
129 130 131
            break;
        }
        case APM::SyncOperationType: {
Lisa's avatar
Lisa committed
132
            SyncOperation * sync = new SyncOperation(m_operations, m_options, m_args, this);
133
            connect(sync, SIGNAL(syncFinished()), this, SLOT(quit()));
Lisa's avatar
Lisa committed
134
            sync->start();
Lisa's avatar
Lisa committed
135 136 137 138
            break;
        }
        case APM::UpgradeOperationType: {
            UpgradeOperation * upgrade = new UpgradeOperation(this);
139
            connect(upgrade, SIGNAL(upgradeFinished()), this, SLOT(quit()));
Lisa's avatar
Lisa committed
140
            upgrade->start(m_operations, m_options, m_args);
Lisa's avatar
Lisa committed
141 142
            break;
        }
143 144
        case APM::PerformOperationType: {
            PerformOperation * perform = new PerformOperation(this);
145
            connect(perform, SIGNAL(performFinished()), this, SLOT(quit()));
146 147 148
            perform->start(m_options, m_args);
            break;
        }
Lisa's avatar
Lisa committed
149 150
        default:
            break;
Lisa's avatar
Lisa committed
151
    }
Lukas Appelhans's avatar
Lukas Appelhans committed
152 153
}

154 155 156 157 158 159
void OperationManager::quit()
{
    Akabei::Backend::instance()->deInit();
    QCoreApplication::instance()->quit();
}

160 161 162 163 164 165
void OperationManager::staticQuit(int sig)
{
    Q_UNUSED(sig)
    Akabei::Backend::instance()->deInit();
    QCoreApplication::instance()->quit();
}