File: groupmanager.cpp

package info (click to toggle)
ktorrent 25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,104 kB
  • sloc: cpp: 40,482; xml: 1,163; python: 182; sh: 10; makefile: 5
file content (331 lines) | stat: -rw-r--r-- 10,339 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
    SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "groupmanager.h"

#include <KLocalizedString>

#include "allgroup.h"
#include "functiongroup.h"
#include "torrentgroup.h"
#include "ungroupedgroup.h"
#include <bcodec/bdecoder.h>
#include <bcodec/bencoder.h>
#include <bcodec/bnode.h>
#include <interfaces/functions.h>
#include <interfaces/torrentinterface.h>
#include <util/error.h>
#include <util/file.h>
#include <util/fileops.h>
#include <util/log.h>

using namespace bt;

namespace kt
{
bool upload(TorrentInterface *tor)
{
    return tor->getStats().completed;
}

bool download(TorrentInterface *tor)
{
    return !tor->getStats().completed;
}

bool queued(TorrentInterface *tor)
{
    return tor->getStats().status == bt::QUEUED;
}

bool stalled(TorrentInterface *tor)
{
    return tor->getStats().status == bt::STALLED;
}

bool error(TorrentInterface *tor)
{
    return tor->getStats().status == bt::ERROR;
}

bool not_running(TorrentInterface *tor)
{
    return tor->getStats().running == false;
}

bool running(TorrentInterface *tor)
{
    return tor->getStats().running == true;
}

bool active(TorrentInterface *tor)
{
    const bt::TorrentStats &s = tor->getStats();
    return (s.upload_rate >= 100 || s.download_rate >= 100);
}

bool passive(TorrentInterface *tor)
{
    return !active(tor);
}

template<IsMemberFunction A, IsMemberFunction B>
bool member(TorrentInterface *tor)
{
    return A(tor) && B(tor);
}

GroupManager::GroupManager()
{
    groups.setAutoDelete(true);

    all = new AllGroup();
    groups.insert(all->groupName(), all);

    QList<Group *> defaults;
    // uploads tree
    defaults << new FunctionGroup<upload>(i18n("Uploads"), QStringLiteral("go-up"), Group::UPLOADS_ONLY_GROUP, QStringLiteral("/all/uploads"));
    defaults << new FunctionGroup<member<running, upload>>(i18n("Running Uploads"),
                                                           QStringLiteral("kt-start"),
                                                           Group::UPLOADS_ONLY_GROUP,
                                                           QStringLiteral("/all/uploads/running"));
    defaults << new FunctionGroup<member<not_running, upload>>(i18n("Not Running Uploads"),
                                                               QStringLiteral("kt-stop"),
                                                               Group::UPLOADS_ONLY_GROUP,
                                                               QStringLiteral("/all/uploads/not_running"));

    // downloads tree
    defaults << new FunctionGroup<download>(i18n("Downloads"), QStringLiteral("go-down"), Group::DOWNLOADS_ONLY_GROUP, QStringLiteral("/all/downloads"));
    defaults << new FunctionGroup<member<running, download>>(i18n("Running Downloads"),
                                                             QStringLiteral("kt-start"),
                                                             Group::DOWNLOADS_ONLY_GROUP,
                                                             QStringLiteral("/all/downloads/running"));
    defaults << new FunctionGroup<member<not_running, download>>(i18n("Not Running Downloads"),
                                                                 QStringLiteral("kt-stop"),
                                                                 Group::DOWNLOADS_ONLY_GROUP,
                                                                 QStringLiteral("/all/downloads/not_running"));

    defaults << new FunctionGroup<active>(i18n("Active Torrents"), QStringLiteral("network-connect"), Group::MIXED_GROUP, QStringLiteral("/all/active"));
    defaults << new FunctionGroup<member<active, download>>(i18n("Active Downloads"),
                                                            QStringLiteral("go-down"),
                                                            Group::DOWNLOADS_ONLY_GROUP,
                                                            QStringLiteral("/all/active/downloads"));
    defaults << new FunctionGroup<member<active, upload>>(i18n("Active Uploads"),
                                                          QStringLiteral("go-up"),
                                                          Group::UPLOADS_ONLY_GROUP,
                                                          QStringLiteral("/all/active/uploads"));

    defaults << new FunctionGroup<passive>(i18n("Passive Torrents"), QStringLiteral("network-disconnect"), Group::MIXED_GROUP, QStringLiteral("/all/passive"));
    defaults << new FunctionGroup<member<passive, download>>(i18n("Passive Downloads"),
                                                             QStringLiteral("go-down"),
                                                             Group::DOWNLOADS_ONLY_GROUP,
                                                             QStringLiteral("/all/passive/downloads"));
    defaults << new FunctionGroup<member<passive, upload>>(i18n("Passive Uploads"),
                                                           QStringLiteral("go-up"),
                                                           Group::UPLOADS_ONLY_GROUP,
                                                           QStringLiteral("/all/passive/uploads"));
    defaults << new UngroupedGroup(this);

    for (Group *g : std::as_const(defaults))
        groups.insert(g->groupName(), g);
}

GroupManager::~GroupManager()
{
}

Group *GroupManager::newGroup(const QString &name)
{
    if (groups.find(name))
        return nullptr;

    TorrentGroup *g = new TorrentGroup(name);
    connect(g, &TorrentGroup::torrentAdded, this, &GroupManager::customGroupChanged);
    connect(g, qOverload<Group *>(&TorrentGroup::torrentRemoved), this, &GroupManager::customGroupChanged);
    groups.insert(name, g);
    Q_EMIT groupAdded(g);
    return g;
}

void GroupManager::removeGroup(Group *g)
{
    if (canRemove(g)) {
        Q_EMIT groupRemoved(g);
        groups.setAutoDelete(false);
        groups.erase(g->groupName());
        groups.setAutoDelete(true);
        g->deleteLater();
    }
}

bool GroupManager::canRemove(const Group *g) const
{
    return g && g->groupFlags() & Group::CUSTOM_GROUP;
}

Group *GroupManager::find(const QString &name)
{
    return groups.find(name);
}

QStringList GroupManager::customGroupNames()
{
    QStringList groupNames;
    Itr it = groups.begin();

    while (it != end()) {
        if (it->second->groupFlags() & Group::CUSTOM_GROUP)
            groupNames << it->first;
        ++it;
    }

    return groupNames;
}

void GroupManager::saveGroups()
{
    QString fn = kt::DataDir() + QStringLiteral("groups");
    bt::File fptr;
    if (!fptr.open(fn, QStringLiteral("wb"))) {
        bt::Out(SYS_GEN | LOG_DEBUG) << "Cannot open " << fn << " : " << fptr.errorString() << bt::endl;
        return;
    }

    try {
        bt::BEncoder enc(&fptr);

        enc.beginList();
        for (CItr i = groups.begin(); i != groups.end(); i++) {
            if (i->second->groupFlags() & Group::CUSTOM_GROUP)
                i->second->save(&enc);
        }
        enc.end();
    } catch (bt::Error &err) {
        bt::Out(SYS_GEN | LOG_DEBUG) << "Error : " << err.toString() << endl;
        return;
    }
}

void GroupManager::loadGroups()
{
    QString fn = kt::DataDir() + QStringLiteral("groups");
    bt::File fptr;
    if (!fptr.open(fn, QStringLiteral("rb"))) {
        bt::Out(SYS_GEN | LOG_DEBUG) << "Cannot open " << fn << " : " << fptr.errorString() << bt::endl;
        return;
    }

    bt::BNode *n = nullptr;
    try {
        Uint32 fs = bt::FileSize(fn);
        QByteArray data(fs, 0);
        fptr.read(data.data(), fs);

        BDecoder dec(data, false);
        n = dec.decode();
        if (!n || n->getType() != bt::BNode::LIST)
            throw bt::Error(QStringLiteral("groups file corrupt"));

        BListNode *ln = (BListNode *)n;
        for (Uint32 i = 0; i < ln->getNumChildren(); i++) {
            BDictNode *dn = ln->getDict(i);
            if (!dn)
                continue;

            TorrentGroup *g = new TorrentGroup(QStringLiteral("dummy"));
            connect(g, &TorrentGroup::torrentAdded, this, &GroupManager::customGroupChanged);
            connect(g, qOverload<Group *>(&TorrentGroup::torrentRemoved), this, &GroupManager::customGroupChanged);

            try {
                g->load(dn);
            } catch (...) {
                delete g;
                throw;
            }

            if (!find(g->groupName()))
                groups.insert(g->groupName(), g);
            else
                delete g;
        }

        delete n;
    } catch (bt::Error &err) {
        bt::Out(SYS_GEN | LOG_DEBUG) << "Error : " << err.toString() << endl;
        delete n;
        return;
    }
}

void GroupManager::torrentRemoved(TorrentInterface *ti)
{
    for (CItr i = groups.begin(); i != groups.end(); i++) {
        i->second->torrentRemoved(ti);
    }
}

void GroupManager::renameGroup(const QString &old_name, const QString &new_name)
{
    Group *g = find(old_name);
    if (!g)
        return;

    groups.setAutoDelete(false);
    groups.erase(old_name);
    g->rename(new_name);
    groups.insert(new_name, g);
    groups.setAutoDelete(true);
    saveGroups();

    Q_EMIT groupRenamed(g);
}

void GroupManager::addDefaultGroup(Group *g)
{
    if (find(g->groupName()))
        return;

    groups.insert(g->groupName(), g);
    Q_EMIT groupAdded(g);
}

void GroupManager::removeDefaultGroup(Group *g)
{
    if (g) {
        Q_EMIT groupRemoved(g);
        groups.erase(g->groupName());
    }
}

void GroupManager::torrentsLoaded(QueueManager *qman)
{
    for (CItr i = groups.begin(); i != groups.end(); i++) {
        if (i->second->groupFlags() & Group::CUSTOM_GROUP) {
            TorrentGroup *tg = dynamic_cast<TorrentGroup *>(i->second);
            if (tg)
                tg->loadTorrents(qman);
        }
    }
}

Group *GroupManager::findByPath(const QString &path)
{
    for (CItr i = groups.begin(); i != groups.end(); i++) {
        if (i->second->groupPath() == path)
            return i->second;
    }

    return nullptr;
}

void GroupManager::updateCount(QueueManager *qman)
{
    for (CItr i = groups.begin(); i != groups.end(); i++)
        i->second->updateCount(qman);
}

}

#include "moc_groupmanager.cpp"