File: feedwidgetmodel.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 (182 lines) | stat: -rw-r--r-- 4,785 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
/*
    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 <QDateTime>
#include <QDomElement>
#include <QIcon>
#include <QLocale>

#include <KLocalizedString>

#include <Syndication/Enclosure>
#include <Syndication/Item>

#include "feedwidgetmodel.h"
#include "ktfeed.h"
#include <util/log.h>

using namespace bt;

namespace kt
{
FeedWidgetModel::FeedWidgetModel(QObject *parent)
    : QAbstractTableModel(parent)
    , feed(nullptr)
{
}

FeedWidgetModel::~FeedWidgetModel()
{
}

void FeedWidgetModel::setCurrentFeed(Feed *f)
{
    beginResetModel();
    items.clear();
    if (feed)
        disconnect(feed, &Feed::updated, this, &FeedWidgetModel::updated);

    feed = f;
    if (feed) {
        Syndication::FeedPtr ptr = feed->feedData();
        if (ptr)
            items = ptr->items();
        connect(feed, &Feed::updated, this, &FeedWidgetModel::updated);
    }
    endResetModel();
}

int FeedWidgetModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return items.count();
    else
        return 0;
}

int FeedWidgetModel::columnCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return 3;
    else
        return 0;
}

QVariant FeedWidgetModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole || section < 0 || section >= 3 || orientation != Qt::Horizontal)
        return QVariant();

    switch (section) {
    case 0:
        return i18n("Title");
    case 1:
        return i18n("Date Published");
    case 2:
        return i18n("Torrent");
    default:
        return QVariant();
    }
}

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

    if (index.row() < 0 || index.row() >= items.count())
        return QVariant();

    Syndication::ItemPtr item = items.at(index.row());
    if (role == Qt::DisplayRole) {
        switch (index.column()) {
        case 0:
            return item->title();
        case 1:
            return QLocale().toString(QDateTime::fromSecsSinceEpoch(item->datePublished()), QLocale::ShortFormat);
        case 2:
            return TorrentUrlFromItem(item);
        default:
            return QVariant();
        }
    } else if (role == Qt::DecorationRole && index.column() == 0 && feed->downloaded(item))
        return QIcon::fromTheme(QStringLiteral("go-down"));

    return QVariant();
}

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

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

Syndication::ItemPtr FeedWidgetModel::itemForIndex(const QModelIndex &index)
{
    if (index.row() < 0 || index.row() >= items.count())
        return Syndication::ItemPtr();

    return items.at(index.row());
}

QString TorrentUrlFromItem(Syndication::ItemPtr item)
{
    const QList<Syndication::EnclosurePtr> encs = item->enclosures();
    for (const Syndication::EnclosurePtr &e : encs) {
        if (e->type() == QStringLiteral("application/x-bittorrent") || e->url().endsWith(QStringLiteral(".torrent")))
            return e->url();
    }

    // Search for magnets on item's link as some RSS feeds only use this item to post the magnet link.
    QString link = item->link();
    if (!link.isEmpty()) {
        // Note that syndication library prepends the channel link to the item link by default, so
        // we need to extract the magnet from the string.
        int magnetStartIndex = link.indexOf(QStringLiteral("magnet:"));
        if (magnetStartIndex >= 0)
            return link.right(link.size() - magnetStartIndex);
    }

    QMultiMap<QString, QDomElement> props = item->additionalProperties();
    QMultiMap<QString, QDomElement>::iterator itr = props.begin();
    while (itr != props.end()) {
        const QDomElement &elem = itr.value();
        if (elem.nodeName() == QStringLiteral("torrent")) {
            QDomElement uri = elem.firstChildElement(QStringLiteral("magnetURI"));
            if (!uri.isNull())
                return uri.text();
        }
        itr++;
    }

    return QString();
}

void FeedWidgetModel::updated()
{
    if (!feed)
        return;

    beginResetModel();
    items.clear();
    Syndication::FeedPtr ptr = feed->feedData();
    if (ptr)
        items = ptr->items();
    endResetModel();
}
}

#include "moc_feedwidgetmodel.cpp"