queryoperation.cpp 11.7 KB
Newer Older
Lukas Appelhans's avatar
Lukas Appelhans committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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.
*/

#include "queryoperation.h"

#include <akabeibackend.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
14
#include <akabeigroup.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
15
#include <akabeidatabase.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
16
#include <akabeiconfig.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
17 18
#include <klocale.h>
#include <QTextStream>
Lukas Appelhans's avatar
Lukas Appelhans committed
19
#include <kio/global.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
20
#include <kdebug.h>
Lukas Appelhans's avatar
Lukas Appelhans committed
21
#include <QDate>
Lukas Appelhans's avatar
Lukas Appelhans committed
22 23
#include <QFile>
#include <QDir>
Lukas Appelhans's avatar
Lukas Appelhans committed
24

Lukas Appelhans's avatar
Lukas Appelhans committed
25 26
//TODO: Make the -p stuff work!

Lukas Appelhans's avatar
Lukas Appelhans committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
QueryOperation::QueryOperation(QObject * parent)
  : QObject(parent)
{

}

QueryOperation::~QueryOperation()
{

}

void QueryOperation::start(QList<APM::Operation> operations, QStringList args)
{
    if (operations.contains(APM::ShowInformation)) {
        if (args.isEmpty())
            return;
        connect(Akabei::Backend::instance(), SIGNAL(queryPackagesCompleted(QUuid,QList<Akabei::Package*>)), 
                                             SLOT(showInformation(QUuid,QList<Akabei::Package*>)));
        Akabei::Backend::instance()->searchPackages(args.first());
Lukas Appelhans's avatar
Lukas Appelhans committed
46 47 48 49 50
    } else if (operations.contains(APM::ShowPackagesOfGroup)) {
        if (args.isEmpty())
            return;
        connect(Akabei::Backend::instance(), SIGNAL(queryGroupsCompleted(QUuid,QList<Akabei::Group*>)), 
                                             SLOT(showGroup(QUuid,QList<Akabei::Group*>)));
Lukas Appelhans's avatar
Lukas Appelhans committed
51
        Akabei::Backend::instance()->queryGroups("SELECT * FROM groups WHERE Name LIKE \"" + args.first() + "\"");
Lukas Appelhans's avatar
Lukas Appelhans committed
52 53 54 55 56 57
    } else if (operations.contains(APM::ShowChangelog)) {
        if (args.isEmpty())
            return;
        connect(Akabei::Backend::instance(), SIGNAL(queryPackagesCompleted(QUuid,QList<Akabei::Package*>)), 
                                             SLOT(showChangelog(QUuid,QList<Akabei::Package*>)));
        Akabei::Backend::instance()->searchPackages(args.first());
58
    } else if (operations.contains(APM::ShowInstalledAsDeps) || operations.contains(APM::ShowInstalledExplicitely)) {
Lukas Appelhans's avatar
Lukas Appelhans committed
59 60 61
        QString query = "SELECT * FROM packages WHERE";
        if (!args.isEmpty())
            query = query + " Name LIKE \"" + args.first() + "\" AND";
62 63 64 65
        if (operations.contains(APM::ShowInstalledAsDeps))
            query = query + " InstallReason LIKE 0";// + QString((int)Akabei::Package::InstalledAsDependencyReason);
        else
            query = query + " InstallReason LIKE 1";
Lukas Appelhans's avatar
Lukas Appelhans committed
66
        showInstalledAsDeps(Akabei::Backend::instance()->localDatabase()->queryPackages(query));
Lukas Appelhans's avatar
Lukas Appelhans committed
67 68 69 70 71 72
    } else if (operations.contains(APM::CheckFiles)) {
        if (args.isEmpty())
            return;
        connect(Akabei::Backend::instance(), SIGNAL(queryPackagesCompleted(QUuid,QList<Akabei::Package*>)), 
                                             SLOT(checkFiles(QUuid,QList<Akabei::Package*>)));
        Akabei::Backend::instance()->searchPackages(args.first());
Lukas Appelhans's avatar
Lukas Appelhans committed
73 74 75 76 77
    } else if (operations.contains(APM::ShowLocal)) {
        if (args.isEmpty())
            showLocalPackages(QString());
        else
            showLocalPackages(args.first());
Lukas Appelhans's avatar
Lukas Appelhans committed
78 79 80 81 82
    } else if (operations.contains(APM::ShowOwner)) {
        if (args.isEmpty())
            showOwner(QString());
        else
            showOwner(args.first());
Lukas Appelhans's avatar
Lukas Appelhans committed
83 84 85 86 87
    } else if (operations.contains(APM::SearchLocal)) {
        QString query = "SELECT * FROM packages";
        if (!args.isEmpty())
            query = query + " WHERE Name LIKE \"" + args.first() + "\"";
        showLocalQuery(Akabei::Backend::instance()->localDatabase()->queryPackages(query));
Lukas Appelhans's avatar
Lukas Appelhans committed
88 89
    } else if (operations.contains(APM::ShowNotRequired)) {
        showNotRequired();
Lukas Appelhans's avatar
Lukas Appelhans committed
90 91
    } else if (operations.contains(APM::ShowUpgradeable)) {
        showUpgradeable();
Lukas Appelhans's avatar
Lukas Appelhans committed
92 93 94 95 96 97 98 99 100 101
    }
}

void QueryOperation::showInformation(QUuid uuid,QList<Akabei::Package*> packages)
{
    Q_UNUSED(uuid)
    if (packages.isEmpty())
        return;
    Akabei::Package * pkg = packages.first();
    QTextStream out(stdout);
Lukas Appelhans's avatar
Lukas Appelhans committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    out.setFieldWidth(30);
    out.setFieldAlignment(QTextStream::AlignLeft);
    out << i18n("Name") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->name() << endl;
    out << i18n("Version") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << QString(pkg->version().toByteArray().data()) << endl;
    out << i18n("URL") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->url().toString() << endl;
    out << i18n("Licenses") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->license() << endl;
    QStringList groups;
    foreach (Akabei::Group * g, pkg->groups())
        groups << g->name();
    out << i18n("Groups") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << groups.join(" ") << endl;
    out << i18n("Provides") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->provides().join(" ") << endl;
    out << i18n("Depends on") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->dependencies().join(" ") << endl;
    out << i18n("Optional dependencies") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->optionalDependencies().join(" ") << endl;
    //out << i18n("Required by:") << pkg->
    out << i18n("Replaces") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->replaces().join(" ") << endl;
    out << i18n("Installation size") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << KIO::convertSize(pkg->size()) << endl;
    out << i18n("Packager") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->packager() << endl;
    out << i18n("Architecture") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->arch() << endl;
    out << i18n("Creation time") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->buildDate().toString() << endl;
    out << i18n("Installation time") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->installDate().toString() << endl;
    out << i18n("Install reason") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << ((pkg->installReason() == Akabei::Package::InstalledAsDependencyReason) ? i18n("Installed as dependency") : i18n("Installed explicitely")) << endl;
    out << i18n("Installation script") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << (pkg->hasScriptlet() ? i18n("Yes") : i18n("No")) << endl;
    out << i18n("Description") << qSetFieldWidth(2) << ":" << qSetFieldWidth(20) << pkg->description() << endl;
Lukas Appelhans's avatar
Lukas Appelhans committed
125 126
    out.flush();
}
Lukas Appelhans's avatar
Lukas Appelhans committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

void QueryOperation::showGroup(QUuid uuid,QList<Akabei::Group*> groups)
{
    Q_UNUSED(uuid)
    if (groups.isEmpty())
        return;
    Akabei::Group * group = groups.first();
    
    QTextStream out(stdout);
    QMap<QString, Akabei::Package*> sorted;
    foreach (Akabei::Package * pkg, group->packages())
        sorted[pkg->name()] = pkg;
    foreach (Akabei::Package * pkg, sorted.values())
        out << group->name() << ' ' << pkg->name() << endl;
    out.flush();
}
Lukas Appelhans's avatar
Lukas Appelhans committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

void QueryOperation::showChangelog(QUuid uuid, QList< Akabei::Package* > packages)
{
    Q_UNUSED(uuid)
    if (packages.isEmpty())
        return;
    Akabei::Package * pkg = packages.first();
    QTextStream out(stdout);
    out << pkg->retrieveChangelog() << endl;
    out.flush();
}

void QueryOperation::showInstalledAsDeps(QList<Akabei::Package*> packages)
{
    if (packages.isEmpty())
        return;
    QTextStream out(stdout);
160
    QMap<QString, Akabei::Package*> sorted;
Lukas Appelhans's avatar
Lukas Appelhans committed
161
    foreach (Akabei::Package * pkg, packages)
162 163
        sorted[pkg->name()] = pkg;
    foreach (Akabei::Package * pkg, sorted.values())
Lukas Appelhans's avatar
Lukas Appelhans committed
164 165 166 167
        out << pkg->name() << ' ' << pkg->version().toByteArray().data() << endl;
    out.flush();
}

Lukas Appelhans's avatar
Lukas Appelhans committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181
void QueryOperation::checkFiles(QUuid,QList<Akabei::Package*> packages)
{
    if (packages.isEmpty())
        return;
    Akabei::Package * pkg = packages.first();
    QTextStream out(stdout);
    int all = 0;
    int missing = 0;
    foreach (const QString &file, pkg->retrieveFiles()) {
        all++;
        if (!QFile::exists(Akabei::Config::instance()->rootDir().absoluteFilePath(file)))
            missing++;
    }
    out << pkg->name() << ": " << all << " overall files, " << missing << " file(s) missing" << endl;
Lukas Appelhans's avatar
Lukas Appelhans committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    out.flush();
}

void QueryOperation::showLocalPackages(const QString &query)
{
    QTextStream out(stdout);
    QList<Akabei::Package*> packages;
    if (!query.isEmpty())
        packages = Akabei::Backend::instance()->localDatabase()->searchPackages(query);
    else
        packages = Akabei::Backend::instance()->localDatabase()->packages();
    foreach (Akabei::Package* pkg, packages) {
        bool found = false;
        foreach (Akabei::Database* db, Akabei::Backend::instance()->databases()) {
            foreach (Akabei::Package* p, db->searchPackages(pkg->name())) {
                if (p->name() == pkg->name()) {
                    found = true;
                    break;
                }
            }
            if (found)
                break;
        }
        if (!found)
            out << pkg->name() << ' ' << pkg->version().toByteArray().data() << endl;
    }
    out.flush();
Lukas Appelhans's avatar
Lukas Appelhans committed
209 210
}

Lukas Appelhans's avatar
Lukas Appelhans committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
void QueryOperation::showOwner(const QString &query)
{
    QTextStream out(stdout);
    if (!QFile::exists(query)) {
        out << "File does not exists: " << query << endl;
        out.flush();
        return;
    }
    foreach (Akabei::Package * pkg, Akabei::Backend::instance()->localDatabase()->packages()) {
        foreach (const QString &file, pkg->retrieveFiles()) {
            if (Akabei::Config::instance()->rootDir().absoluteFilePath(file) == query) {
                out << query  << " is owned by " << pkg->name() << ' ' << pkg->version().toByteArray().data() << endl;
                out.flush();
                return;
            }
        }
    }
    out << "Error: No packages owns " << query << endl;
    out.flush();
}
Lukas Appelhans's avatar
Lukas Appelhans committed
231 232 233 234 235 236 237 238 239 240 241 242 243

void QueryOperation::showLocalQuery(QList<Akabei::Package*> packages)
{
    QTextStream out(stdout);
    QMap<QString, Akabei::Package*> sorted;
    foreach (Akabei::Package * pkg, packages)
        sorted[pkg->name()] = pkg;
    foreach (Akabei::Package * pkg, sorted.values()) {
        out << "local/" << pkg->name() << ' ' << pkg->version().toByteArray().data() << endl;
        out << "    " << pkg->description() << endl;
    }
    out.flush();
}
Lukas Appelhans's avatar
Lukas Appelhans committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

void QueryOperation::showNotRequired()
{
    QTextStream out(stdout);
    QList<Akabei::Package*> packages = Akabei::Backend::instance()->localDatabase()->packages();
    QMap<QString, Akabei::Package*> pkgs;
    foreach (Akabei::Package* pkg, packages)
        pkgs[pkg->name()] = pkg;
    foreach (Akabei::Package* pkg, packages) {
        foreach (const QString &dep, pkg->dependencies())
            pkgs.remove(dep);
    }
    foreach (Akabei::Package * pkg, pkgs.values())
        out << pkg->name() << ' ' << pkg->version().toByteArray().data() << endl;
    out.flush();
}
Lukas Appelhans's avatar
Lukas Appelhans committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

void QueryOperation::showUpgradeable()
{
    //FIXME: Maybe don't even check for the highest version... redundant?
    QTextStream out(stdout);
    QList<Akabei::Package*> packages = Akabei::Backend::instance()->localDatabase()->packages();
    QMap<QString, Akabei::Package*> upgradeable;
    foreach (Akabei::Package * pkg, packages) {
        foreach (Akabei::Database * db, Akabei::Backend::instance()->databases()) {
            Akabei::Package::List list = db->queryPackages("SELECT * FROM packages WHERE Name LIKE \"" + pkg->name() + "\"");
            if (!list.isEmpty()) {
                foreach (Akabei::Package * p, list) {
                    if (p->version() > pkg->version() && p->name() == pkg->name()) {
                        if (upgradeable[p->name()] && upgradeable[p->name()]->version() > p->version())
                            continue;
                        upgradeable[p->name()] = p;
                    }
                }
            }
        }
    }
    
    foreach (Akabei::Package * pkg, upgradeable.values())
        out << pkg->name() << ' ' << pkg->version().toByteArray().data() << endl;
    out.flush();
}