utils.cpp 6.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* This file is part of the Chakra project

   Copyright (C) 2012 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 "utils.h"
#include "fieldnames.h"

#include <akabeiclientbackend.h>
#include <akabeigroup.h>
#include <akabeidatabase.h>
#include <akabeipackage.h>
17
#include <akabeihelpers.h>
18
#include <akabeigroup.h>
Lisa's avatar
Lisa committed
19

20
#include <QDateTime>
21
#include <QDebug>
Lisa's avatar
Lisa committed
22
#include <QStringList>
23 24

namespace Utils {
25
void printPackageInformation(Akabei::Package * p, QTextStream &out, bool showLess, bool showMakeDeps)
26 27 28
{
    /* TODO: discover how to disable these features: reset() doesn't work */
    out.setFieldAlignment(QTextStream::AlignLeft);
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    AkabeiClient::DatabaseHandler * db = 0;
    foreach (AkabeiClient::DatabaseHandler * database, AkabeiClient::Backend::instance()->databases()) {
        if (database->database() == p->database()) {
            db = database;
            break;
        }
    }

    QStringList groups;
    foreach (Akabei::Group const* g, p->groups()) {
        groups << g->name();
    }

    FieldNames const& fields( FieldNames::instance() );
    unsigned int n( 1 + fields.getMaxFieldLength() );

Lisa's avatar
Lisa committed
46
    out << "*~  " << QObject::tr("Package: %1 %2").arg(p->name(), p->version().toByteArray().data()) << endl;
47 48 49 50
    if (db) {
        out << "\t" << qSetFieldWidth(n) << fields["Available version:"] << qSetFieldWidth(0) << QString(p->version().toByteArray().data());
        Akabei::Package::List result = Akabei::Backend::instance()->localDatabase()->searchPackages(p->name(), Akabei::SearchNameEqual);
        if (!result.isEmpty()) {
Lisa's avatar
Lisa committed
51
            out << qSetFieldWidth(4) << QString() << qSetFieldWidth(0) << fields["Installed version:"] << " " << QString(result.first()->version().toByteArray());
52 53
        }
    } else if (p->database() == Akabei::Backend::instance()->localDatabase()) {
Lisa's avatar
Lisa committed
54
        out << "\t" << qSetFieldWidth(n) << fields["Installed version:"] << qSetFieldWidth(0) << QString(p->version().toByteArray());
55 56 57 58 59 60 61 62 63 64
    }
    out << endl;

    out  << "\t";
    if (db) {
        out << qSetFieldWidth(n) << fields["Repository:"] << qSetFieldWidth(0) << db->name() << qSetFieldWidth(4) << QString();
        out << qSetFieldWidth(0) << fields["Groups:"] << " " << groups.join(" ") << endl;
    } else {
        out << qSetFieldWidth(n) << fields["Groups:"] << qSetFieldWidth(0) << groups.join(" ") << endl;
    }
65

66 67
    if (!showLess) {
        out << "\t" << qSetFieldWidth(n) << fields["Description:"] << qSetFieldWidth(0) << p->description() << endl;
AlmAck's avatar
AlmAck committed
68 69
        out << "\t" << qSetFieldWidth(n) << fields["Installation size:"] << qSetFieldWidth(0) << Akabei::Helpers::formatByteSize(p->size()) << endl;
        out << "\t" << qSetFieldWidth(n) << fields["Architecture:"] << qSetFieldWidth(0) << p->arch() << endl;
70
        out << "\t" << qSetFieldWidth(n) << fields["Homepage:"] << qSetFieldWidth(0) << p->url().toString() << endl;
Lisa's avatar
Lisa committed
71 72
        out << "\t" << qSetFieldWidth(n) << fields["Licenses:"] << qSetFieldWidth(0) << (p->licenses().isEmpty() ? QObject::tr("None") : p->licenses().join(" ")) << endl;
        out << "\t" << qSetFieldWidth(n) << fields["Depends on:"] << qSetFieldWidth(0) << (p->dependencies().isEmpty() ? QObject::tr("None") : p->dependencies().join(" ")) << endl;
73

74
        if (showMakeDeps) {
Lisa's avatar
Lisa committed
75
            out << "\t" << qSetFieldWidth(n) << fields["Make dependencies:"] << qSetFieldWidth(0) << (p->makeDependencies().isEmpty() ? QObject::tr("None") : p->makeDependencies().join(" ")) << endl;
76
        }
77

Lisa's avatar
Lisa committed
78 79
        out << "\t" << qSetFieldWidth(n) << fields["Optional dependencies:"] << qSetFieldWidth(0) << (p->optionalDependencies().isEmpty() ? QObject::tr("None") : p->optionalDependencies().join(" ")) << endl;
        out << "\t" << qSetFieldWidth(n) << fields["Replaces:"] << qSetFieldWidth(0) << (p->replaces().isEmpty() ? QObject::tr("None") : p->replaces().join(" ")) << endl;
80 81 82 83 84 85 86 87 88 89 90 91
        Akabei::Package::List rb;
        if (p->database() == Akabei::Backend::instance()->localDatabase()) {
            rb = p->computeRequiredByInstalled();
        } else {
            rb = p->computeRequiredBy();
        }
        QString requiredBy;
        foreach (Akabei::Package * p, rb) {
            requiredBy.append(p->name() + ' ');
        }        
        out << "\t" << qSetFieldWidth(n) << fields["Required by:"] << qSetFieldWidth(0) << (requiredBy.isEmpty() ? QObject::tr("None") : requiredBy) << endl;

92 93
        out << "\t" << qSetFieldWidth(n) << fields["Packager:"] << qSetFieldWidth(0) << p->packager() << endl;
        out << "\t" << qSetFieldWidth(n) << fields["Creation time:"] << qSetFieldWidth(0) << p->buildDate().toString() << endl;
94 95 96
        if (p->database() == Akabei::Backend::instance()->localDatabase()) {
            out << "\t" << qSetFieldWidth(n) << fields["Installation time:"] << qSetFieldWidth(0) << p->installDate().toString() << endl;
        }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        out << "\t" << qSetFieldWidth(n) << fields["MD5 sum:"] << qSetFieldWidth(0) << p->md5sum().data() << endl;
    }
}
void printGroup(Akabei::Group const * group, QTextStream &out, bool showLess)
{
    QMap<QString, Akabei::Package*> sorted;

    foreach (Akabei::Package * pkg, group->packages()) {
        sorted[pkg->name()] = pkg;
    }

    out.setFieldAlignment(QTextStream::AlignLeft);

    FieldNames const& fields( FieldNames::instance() );
    unsigned int n( 1 + fields.getMaxFieldLength() );

Lisa's avatar
Lisa committed
113
    out << "*~  " << QObject::tr("Group:") << " " << group->name() << endl;
114
    if (!showLess) {
Lisa's avatar
Lisa committed
115
        out << "\t" << qSetFieldWidth(n) << QObject::tr("Description:") << qSetFieldWidth(0) << (group->description().isEmpty() ? QObject::tr("None") : group->description()) << endl;
116
    }
Lisa's avatar
Lisa committed
117
    out << "\t" << qSetFieldWidth(n) << QObject::tr("Packages:") << qSetFieldWidth(0);
118 119 120 121 122 123 124 125 126 127 128

    QList<Akabei::Package*> packages = sorted.values();
    foreach (Akabei::Package * pkg, packages) {
        out << pkg->name();
        if (!packages.endsWith(pkg))
            out << ", ";
        else
            out << endl;
    }
}
};