File: feedlist.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 (259 lines) | stat: -rw-r--r-- 6,797 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
/*
    SPDX-FileCopyrightText: 2008 Joris Guisson <joris.guisson@gmail.com>
    SPDX-FileCopyrightText: 2008 Ivan Vasic <ivasic@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <QDir>
#include <QFile>
#include <QIcon>
#include <QTextStream>
#include <QTime>

#include <KLocalizedString>

#include "feedlist.h"
#include "filterlist.h"
#include "ktfeed.h"
#include "syndicationactivity.h"
#include <interfaces/functions.h>
#include <util/fileops.h>
#include <util/functions.h>
#include <util/log.h>

using namespace bt;

namespace kt
{
FeedList::FeedList(const QString &data_dir, QObject *parent)
    : QAbstractListModel(parent)
    , data_dir(data_dir)
{
}

FeedList::~FeedList()
{
    qDeleteAll(feeds);
}

void FeedList::loadFeeds(FilterList *filter_list, SyndicationActivity *activity)
{
    QDir dir(data_dir);
    QStringList filters;
    filters << QStringLiteral("feed*");
    const QStringList sl = dir.entryList(filters, QDir::Dirs);
    for (const QString &s : sl) {
        QString idir = data_dir + s;
        if (!idir.endsWith(DirSeparator()))
            idir.append(DirSeparator());

        Out(SYS_GEN | LOG_NOTICE) << "Loading feed from directory " << idir << endl;
        Feed *feed = nullptr;
        try {
            feed = new Feed(idir);
            connect(feed, &Feed::downloadLink, activity, &SyndicationActivity::downloadLink);
            feed->load(filter_list);
            addFeed(feed);

        } catch (...) {
            delete feed;
        }
    }
}

void FeedList::importOldFeeds()
{
    QFile fptr(kt::DataDir() + QStringLiteral("rssfeeds.ktr"));
    if (!fptr.open(QIODevice::ReadOnly))
        return;

    QDataStream in(&fptr);
    int num_feeds;
    in >> num_feeds;
    for (int i = 0; i < num_feeds; i++) {
        QUrl feed_url;
        QString title;
        int active;
        int article_age;
        int ignore_ttl;
        QTime auto_refresh;
        QString protocol;
        QString user;
        QString pass;
        QString host;
        QString path;
        QString path_encoded;
        QString query;
        QString ref_encoded;
        unsigned char malf;
        unsigned short port;

        in >> protocol >> user >> pass >> host >> path >> path_encoded >> query >> ref_encoded >> malf >> port;
        feed_url.setScheme(protocol);
        feed_url.setUserName(user);
        feed_url.setPassword(pass);
        feed_url.setHost(host);
        feed_url.setPath(path);
        feed_url.setQuery(query);
        feed_url.setFragment(ref_encoded);
        feed_url.setPort(port == 0 ? -1 : port);

        in >> title >> active >> article_age >> ignore_ttl >> auto_refresh;
        Out(SYS_GEN | LOG_DEBUG) << "Importing " << feed_url.toDisplayString() << " ..." << endl;

        // check for duplicate URL's
        bool found = false;
        for (Feed *f : std::as_const(feeds)) {
            if (f->feedUrl() == feed_url) {
                found = true;
                break;
            }
        }

        if (!found) {
            Feed *f = new Feed(feed_url.toString(), Feed::newFeedDir(data_dir));
            addFeed(f);
        }
    }

    fptr.close();
    // move the rssfeeds.ktr file to a backup location
    // so that it doesn't get immorted twice
    bt::Move(kt::DataDir() + QStringLiteral("rssfeeds.ktr"), kt::DataDir() + QStringLiteral("imported-rssfeeds.ktr"), true, true);
}

void FeedList::addFeed(Feed *f)
{
    feeds.append(f);
    connect(f, &Feed::updated, this, &FeedList::feedUpdated);
    insertRow(feeds.count() - 1);
}

int FeedList::rowCount(const QModelIndex &parent) const
{
    return !parent.isValid() ? feeds.count() : 0;
}

QVariant FeedList::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    Feed *f = feeds.at(index.row());
    if (!f->feedData())
        return QVariant();

    switch (role) {
    case Qt::EditRole:
    case Qt::DisplayRole:
        return f->displayName();
    case Qt::UserRole:
        return i18np("%2\n1 active filter", "%2\n%1 active filters", f->numFilters(), f->displayName());
    case Qt::DecorationRole:
        if (f->feedStatus() == Feed::FAILED_TO_DOWNLOAD)
            return QIcon::fromTheme(QStringLiteral("dialog-error"));
        else
            return QIcon::fromTheme(QStringLiteral("application-rss+xml"));
    case Qt::ToolTipRole:
        if (f->feedStatus() == Feed::FAILED_TO_DOWNLOAD)
            return i18n("<b>%1</b><br/><br/>Download failed: <b>%2</b>", f->feedData()->link(), f->errorString());
        else if (f->ok())
            return i18n("<b>%1</b><br/><br/>%2", f->feedData()->link(), f->feedData()->description());
        break;
    }

    return QVariant();
}

bool FeedList::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() || role != Qt::EditRole || !value.canConvert<QString>())
        return false;

    Feed *f = feeds.at(index.row());
    f->setDisplayName(value.toString());
    Q_EMIT dataChanged(index, index);
    return true;
}

Qt::ItemFlags FeedList::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return {};

    return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
}

Feed *FeedList::feedForIndex(const QModelIndex &idx)
{
    if (!idx.isValid())
        return nullptr;

    return feeds.at(idx.row());
}

Feed *FeedList::feedForDirectory(const QString &dir)
{
    for (Feed *f : std::as_const(feeds))
        if (f->directory() == dir)
            return f;

    return nullptr;
}

bool FeedList::removeRows(int row, int count, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    beginRemoveRows(QModelIndex(), row, row + count - 1);
    endRemoveRows();
    return true;
}

bool FeedList::insertRows(int row, int count, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    beginInsertRows(QModelIndex(), row, row + count - 1);
    endInsertRows();
    return true;
}

void FeedList::removeFeeds(const QModelIndexList &idx)
{
    QList<Feed *> to_remove;
    for (const QModelIndex &i : idx) {
        Feed *f = feedForIndex(i);
        if (f)
            to_remove.append(f);
    }

    beginResetModel();
    for (Feed *f : std::as_const(to_remove)) {
        bt::Delete(f->directory(), true);
        feeds.removeAll(f);
        delete f;
    }
    endResetModel();
}

void FeedList::feedUpdated()
{
    Feed *f = (Feed *)sender();
    int idx = feeds.indexOf(f);
    if (idx >= 0)
        Q_EMIT dataChanged(index(idx, 0), index(idx, 0));
}

void FeedList::filterRemoved(Filter *f)
{
    for (Feed *feed : std::as_const(feeds))
        feed->removeFilter(f);
}

void FeedList::filterEdited(Filter *f)
{
    for (Feed *feed : std::as_const(feeds)) {
        if (feed->usingFilter(f))
            feed->runFilters();
    }
}
}