File: categorypage.cpp

package info (click to toggle)
dde-store 1.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 724 kB
  • sloc: cpp: 2,048; makefile: 5
file content (124 lines) | stat: -rw-r--r-- 4,309 bytes parent folder | download | duplicates (2)
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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "pages/categorypage.h"
#include "backend/sources/packagekit/packagekitsource.h"
#include <QComboBox>
#include <QVBoxLayout>

CategoryPage::CategoryPage(MainWindow *parent, QString name, QString category)
{
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(layout);
    list = new List(name);
    DLabel *sortLabel = new DLabel(tr("Sort by"));
    list->addHeaderWidget(sortLabel);
    QComboBox *sortBox = new QComboBox;
    QString sortAlphabetical = tr("Alphabetical");
    QString sortRatings = tr("Ratings");
    sortBox->addItems(QStringList() << sortAlphabetical << sortRatings);
    connect(sortBox, &QComboBox::currentTextChanged, this, [ = ] (const QString &text) {
        if (text == sortAlphabetical) {
            load(Alphabetical);
        } else if (text == sortRatings) {
            load(Ratings);
        }
    });
    list->addHeaderWidget(sortBox);

    connect(list, &List::currentItemChanged, this, [ = ] (QVariant data) {
        for (App *app : apps) {
            if (app->id == data.toString()) {
                parent->openItem(app);
                break;
            }
        }
    });

    layout->addWidget(list);

    init(category, name);
}

void CategoryPage::init(QString category, QString name)
{
    if (category == "Installed") {
        for (Source *source : SourceManager::instance()->sources()) {
            connect(source, &Source::gotInstalled, this, [ = ] (QList<App*> installed) {
                for (App *app : installed) {
                    insertItem(app);
                }
                if (SourceManager::instance()->sources().endsWith(source))
                    load();
                else
                    SourceManager::instance()->sources()[SourceManager::instance()->sources().indexOf(source) + 1]->getInstalled();
                disconnect(source, &Source::gotInstalled, this, nullptr);
            });
        }
        SourceManager::instance()->sources().first()->getInstalled();
    } else if (name.startsWith("\"")) {
        list->setEmptyText(tr("No results for %1").arg(name));
        for (Source *source : SourceManager::instance()->sources()) {
            connect(source, &Source::searchFinished, this, [ = ] (QList<App*> results) {
                if (SourceManager::instance()->sources().startsWith(source)) {
                    list->clear();
                    apps.clear();
                }
                for (App *app : results) {
                    insertItem(app);
                }
                if (SourceManager::instance()->sources().endsWith(source))
                    load();
                else
                    SourceManager::instance()->sources()[SourceManager::instance()->sources().indexOf(source) + 1]->search(category);
                disconnect(source, &Source::searchFinished, this, nullptr);
            });
        }
        SourceManager::instance()->sources().first()->search(category);
    } else {
        PackageKitSource *pk = static_cast<PackageKitSource*>(SourceManager::instance()->getSource("PackageKit"));
        connect(pk, &PackageKitSource::gotCategory, this, [ = ] (QString cat, QList<App*> items) {
            if (cat == category) {
                for (App *app : items) {
                    insertItem(app);
                }
                load();
                disconnect(pk, &PackageKitSource::gotCategory, this, nullptr);
            }
        });
        pk->getCategory(category);
    }
}

void CategoryPage::insertItem(App *app)
{
    list->addItem(app->name, app->icon, app->id);
    apps << app;
}

void CategoryPage::load(SortType sort)
{
    list->clear();
    if (sort == Alphabetical) {
        QMap<QString, App*> map;
        for (App *app : apps) {
            map.insert(app->name.toLower(), app);
        }
        apps.clear();
        for (App *app : map.values()) {
            insertItem(app);
        }
    } else {
        QMultiMap<int, App*> map;
        for (App *app : apps) {
            map.insert(app->ratings, app);
        }
        QList<App*> appList;
        for (App *app : map.values()) {
            appList.insert(0, app);
        }
        apps.clear();
        for (App *app : appList) {
            insertItem(app);
        }
    }
    list->load();
}