File: zeroconfplugin.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 (103 lines) | stat: -rw-r--r-- 3,008 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
/*
    SPDX-FileCopyrightText: 2005-2007 Joris Guisson <joris.guisson@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "zeroconfplugin.h"

#include <KLocalizedString>
#include <KPluginFactory>

#include "torrentservice.h"
#include <torrent/queuemanager.h>
#include <util/log.h>
#include <util/logsystemmanager.h>

K_PLUGIN_CLASS_WITH_JSON(kt::ZeroConfPlugin, "ktorrent_zeroconf.json")

using namespace bt;

namespace kt
{
ZeroConfPlugin::ZeroConfPlugin(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
    : Plugin(parent, data, args)
{
    services.setAutoDelete(true);
}

ZeroConfPlugin::~ZeroConfPlugin()
{
}

void ZeroConfPlugin::load()
{
    LogSystemManager::instance().registerSystem(i18n("ZeroConf"), SYS_ZCO);
    CoreInterface *core = getCore();
    connect(core, &CoreInterface::torrentAdded, this, &ZeroConfPlugin::torrentAdded);
    connect(core, &CoreInterface::torrentRemoved, this, &ZeroConfPlugin::torrentRemoved);

    // go over existing torrents and add them
    const kt::QueueManager *const qman = core->getQueueManager();
    for (bt::TorrentInterface *tor : *qman) {
        torrentAdded(tor);
    }
}

void ZeroConfPlugin::unload()
{
    LogSystemManager::instance().unregisterSystem(i18n("ZeroConf"));
    CoreInterface *core = getCore();
    disconnect(core, &CoreInterface::torrentAdded, this, &ZeroConfPlugin::torrentAdded);
    disconnect(core, &CoreInterface::torrentRemoved, this, &ZeroConfPlugin::torrentRemoved);

    bt::PtrMap<bt::TorrentInterface *, TorrentService>::iterator i = services.begin();
    while (i != services.end()) {
        TorrentService *av = i->second;
        bt::TorrentInterface *ti = i->first;
        ti->removePeerSource(av);
        i++;
    }
    services.clear();
}

void ZeroConfPlugin::torrentAdded(bt::TorrentInterface *tc)
{
    if (services.contains(tc))
        return;

    TorrentService *av = new TorrentService(tc);
    services.insert(tc, av);
    tc->addPeerSource(av);
    Out(SYS_ZCO | LOG_NOTICE) << "ZeroConf service added for " << tc->getStats().torrent_name << endl;
    connect(av, &TorrentService::serviceDestroyed, this, &ZeroConfPlugin::avahiServiceDestroyed);
}

void ZeroConfPlugin::torrentRemoved(bt::TorrentInterface *tc)
{
    TorrentService *av = services.find(tc);
    if (!av)
        return;
    Out(SYS_ZCO | LOG_NOTICE) << "ZeroConf service removed for " << tc->getStats().torrent_name << endl;
    tc->removePeerSource(av);
    services.erase(tc);
}

void ZeroConfPlugin::avahiServiceDestroyed(TorrentService *av)
{
    services.setAutoDelete(false);

    Out(SYS_ZCO | LOG_NOTICE) << "ZeroConf service destroyed " << endl;
    bt::PtrMap<bt::TorrentInterface *, TorrentService>::iterator i = services.begin();
    while (i != services.end()) {
        if (i->second == av) {
            services.erase(i->first);
            break;
        }
        i++;
    }
    services.setAutoDelete(true);
}

}
#include "zeroconfplugin.moc"

#include "moc_zeroconfplugin.cpp"