File: commandlinepackagedb.cpp

package info (click to toggle)
packagesearch 2.10.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,704 kB
  • sloc: cpp: 9,176; perl: 248; makefile: 15; sh: 11
file content (75 lines) | stat: -rw-r--r-- 2,476 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
#include "commandlinepackagedb.h"
#include "aptcacheparser.h"
#include "dpkgparser.h"
#include "helpers.h"
#include "iprogressobserver.h"
#include "aptpackagebuilder.h"

#include "packagenotfoundexception.h"

namespace NApt {

/** Adds the packages from */
void mergeWithDpkgPackages(
    map<QString, unique_ptr<const IPackage>>& target,
    const map<string, const DpkgParser::PackageInformation>& dpkgPackages
) {
    for (auto const& [name, dpkgPackage] : dpkgPackages)
    {
        QString qName = toQString(name);
        if (target.find(qName) == target.end()) {
            AptPackageBuilder builder;
            builder.setName(qName);
            builder.setVersion(toQString(dpkgPackage.version));
            builder.setDescription(toQString(dpkgPackage.description));
            builder.setArchitecture(toQString(dpkgPackage.architecture));
            target[qName] = builder.createPackage();
        }
    }
}

map<QString, unique_ptr<const IPackage>> getPackages(
    const NPlugin::IProvider& provider,
    NUtil::IProgressObserver& observer
) {
    auto dpkgPackages = DpkgParser::getInstalledPackages();
    auto aptPackages = AptCacheParser::parseDumpAvail(provider.packages().size(), dpkgPackages, observer);
    mergeWithDpkgPackages(aptPackages, dpkgPackages);
    return aptPackages;
}

CommandLinePackageDB::CommandLinePackageDB(
    const NPlugin::IProvider& provider,
    NUtil::IProgressObserver& observer
    ) : _packages(getPackages(provider, observer)) {
}

const IPackage& CommandLinePackageDB::getPackageRecord(const QString &pkg) const {
    auto resultIterator = _packages.find(pkg);
    if (resultIterator == _packages.end()) {
        throw NPlugin::PackageNotFoundException(pkg.toStdString());
    }
    return *(resultIterator->second);
}

PackageDetails CommandLinePackageDB::getPackageDetails(const string& name) const {
    return AptCacheParser::parseCacheShow(name);
}

const IPackage& CommandLinePackageDB::getPackageRecord(const string &package) const {
    return getPackageRecord(toQString(package));
}

const QString CommandLinePackageDB::getShortDescription(const string &package) const {
    return getPackageRecord(package).shortDescription();
}

IPackage::InstalledState CommandLinePackageDB::getState(const string &package) const {
    return getPackageRecord(package).installedState();
}

void CommandLinePackageDB::reloadPackageInformation(NUtil::IProgressObserver *pObserver) {
    pObserver->setProgress(100);
}

}