File: ipblockingprefpage.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 (208 lines) | stat: -rw-r--r-- 6,160 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
/*
    SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <KLocalizedString>

#include "downloadandconvertjob.h"
#include "ipblockingprefpage.h"
#include "ipfilterplugin.h"
#include "ipfilterpluginsettings.h"
#include <util/log.h>

using namespace bt;

namespace kt
{
IPBlockingPrefPage::IPBlockingPrefPage(IPFilterPlugin *p)
    : PrefPageInterface(IPBlockingPluginSettings::self(), i18n("IP Filter"), QStringLiteral("view-filter"), nullptr)
    , m_plugin(p)
{
    setupUi(this);
    connect(kcfg_useLevel1, &QCheckBox::toggled, this, &IPBlockingPrefPage::checkUseLevel1Toggled);
    connect(m_download, &QPushButton::clicked, this, &IPBlockingPrefPage::downloadClicked);
    connect(kcfg_autoUpdate, &QCheckBox::toggled, this, &IPBlockingPrefPage::autoUpdateToggled);
    connect(kcfg_autoUpdateInterval, &KPluralHandlingSpinBox::valueChanged, this, &IPBlockingPrefPage::autoUpdateIntervalChanged);
    kcfg_autoUpdateInterval->setSuffix(ki18np(" day", " days"));
    m_job = nullptr;
    m_verbose = true;
}

IPBlockingPrefPage::~IPBlockingPrefPage()
{
}

void IPBlockingPrefPage::checkUseLevel1Toggled(bool check)
{
    if (check) {
        kcfg_filterURL->setEnabled(true);
        m_download->setEnabled(true);
        m_plugin->loadAntiP2P();
    } else {
        m_status->setText(QString());
        kcfg_filterURL->setEnabled(false);
        m_download->setEnabled(false);
        m_plugin->unloadAntiP2P();
    }

    if (m_plugin->loadedAndRunning() && check)
        m_status->setText(i18n("Status: Loaded and running."));
    else
        m_status->setText(i18n("Status: Not loaded."));

    updateAutoUpdate();
}

void IPBlockingPrefPage::loadDefaults()
{
    loadSettings();
}

void IPBlockingPrefPage::updateSettings()
{
    m_plugin->checkAutoUpdate();
}

void IPBlockingPrefPage::loadSettings()
{
    if (IPBlockingPluginSettings::useLevel1()) {
        if (m_plugin->loadedAndRunning())
            m_status->setText(i18n("Status: Loaded and running."));
        else
            m_status->setText(i18n("Status: Not loaded."));

        kcfg_filterURL->setEnabled(true);
        m_download->setEnabled(true);
        m_last_updated->clear();
        m_next_update->clear();
        kcfg_autoUpdateInterval->setEnabled(IPBlockingPluginSettings::autoUpdate());
        m_auto_update_group_box->setEnabled(true);
    } else {
        m_status->setText(i18n("Status: Not loaded."));
        kcfg_filterURL->setEnabled(false);
        m_download->setEnabled(false);
        m_last_updated->clear();
        m_next_update->clear();
        kcfg_autoUpdateInterval->setEnabled(IPBlockingPluginSettings::autoUpdate());
        m_auto_update_group_box->setEnabled(false);
    }

    updateAutoUpdate();
}

void IPBlockingPrefPage::downloadClicked()
{
    QUrl url = kcfg_filterURL->url();

    // block GUI so you cannot do stuff during conversion
    m_download->setEnabled(false);
    m_status->setText(i18n("Status: Downloading and converting new block list..."));
    kcfg_useLevel1->setEnabled(false);
    kcfg_filterURL->setEnabled(false);

    m_plugin->unloadAntiP2P();
    m_job = new DownloadAndConvertJob(url, m_verbose ? DownloadAndConvertJob::Verbose : DownloadAndConvertJob::Quietly);
    connect(m_job, &DownloadAndConvertJob::result, this, &IPBlockingPrefPage::downloadAndConvertFinished);
    connect(m_job, &DownloadAndConvertJob::notification, m_plugin, &IPFilterPlugin::notification);
    m_job->start();
}

bool IPBlockingPrefPage::doAutoUpdate()
{
    if (m_job) {
        if (m_job->isAutoUpdate())
            return true; // if we are already auto updating, lets not start it again
        else
            return false;
    }

    m_verbose = false;
    Out(SYS_IPF | LOG_NOTICE) << "Doing ipfilter auto update !" << endl;
    downloadClicked();
    m_verbose = true;
    return true;
}

void IPBlockingPrefPage::restoreGUI()
{
    m_download->setEnabled(true);
    kcfg_useLevel1->setEnabled(true);
    kcfg_filterURL->setEnabled(true);

    if (m_plugin->loadedAndRunning())
        m_status->setText(i18n("Status: Loaded and running."));
    else
        m_status->setText(i18n("Status: Not loaded."));
}

void IPBlockingPrefPage::downloadAndConvertFinished(KJob *j)
{
    if (j != m_job)
        return;

    KConfigGroup g = KSharedConfig::openConfig()->group(QStringLiteral("IPFilterAutoUpdate"));
    if (!j->error()) {
        g.writeEntry("last_updated", QDateTime::currentDateTime());
        g.writeEntry("last_update_ok", true);
    } else {
        g.writeEntry("last_update_attempt", QDateTime::currentDateTime());
        g.writeEntry("last_update_ok", false);
    }

    g.sync();

    m_job = nullptr;
    m_plugin->loadAntiP2P();
    restoreGUI();
    updateAutoUpdate();
    Q_EMIT updateFinished();
}

void IPBlockingPrefPage::updateAutoUpdate()
{
    if (!kcfg_useLevel1->isChecked()) {
        m_next_update->clear();
        m_last_updated->clear();
        return;
    }

    KConfigGroup g = KSharedConfig::openConfig()->group(QStringLiteral("IPFilterAutoUpdate"));
    bool ok = g.readEntry("last_update_ok", true);
    QDate last_updated = g.readEntry("last_updated", QDate());

    if (last_updated.isNull())
        m_last_updated->setText(i18n("No update done yet."));
    else if (ok)
        m_last_updated->setText(last_updated.toString());
    else
        m_last_updated->setText(i18n("%1 (Last update attempt failed.)", last_updated.toString()));

    if (kcfg_autoUpdate->isChecked()) {
        QDate next_update;
        if (last_updated.isNull())
            next_update = QDate::currentDate().addDays(kcfg_autoUpdateInterval->value());
        else
            next_update = last_updated.addDays(kcfg_autoUpdateInterval->value());

        m_next_update->setText(next_update.toString());
    } else {
        m_next_update->setText(i18n("Never"));
    }
}

void IPBlockingPrefPage::autoUpdateToggled(bool on)
{
    Q_UNUSED(on);
    updateAutoUpdate();
}

void IPBlockingPrefPage::autoUpdateIntervalChanged(int val)
{
    Q_UNUSED(val);
    updateAutoUpdate();
}

}

#include "moc_ipblockingprefpage.cpp"