Mercurial > minori
annotate src/gui/pages/torrents.cc @ 116:254b1d2b7096
settings: add torrents page, make rss feed configurable
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 07 Nov 2023 13:52:13 -0500 |
parents | ab191e28e69d |
children | 2c1b6782e1d0 |
rev | line source |
---|---|
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "gui/pages/torrents.h" |
114 | 2 #include "core/strings.h" |
3 #include "core/http.h" | |
4 #include "core/session.h" | |
5 #include "gui/widgets/text.h" | |
6 #include "track/media.h" | |
7 #include "pugixml.hpp" | |
8 #include <QVBoxLayout> | |
9 #include <QToolBar> | |
10 #include <QTreeView> | |
11 #include <QMainWindow> | |
12 #include <QByteArray> | |
13 #include <QDataStream> | |
14 #include <QThreadPool> | |
15 #include <QDebug> | |
16 #include <iostream> | |
17 #include <sstream> | |
18 #include <algorithm> | |
19 | |
20 TorrentsPageListSortFilter::TorrentsPageListSortFilter(QObject* parent) : QSortFilterProxyModel(parent) { | |
21 } | |
22 | |
23 bool TorrentsPageListSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const { | |
24 QVariant left = sourceModel()->data(l, sortRole()); | |
25 QVariant right = sourceModel()->data(r, sortRole()); | |
26 | |
27 switch (left.userType()) { | |
28 case QMetaType::Int: | |
29 case QMetaType::UInt: | |
30 case QMetaType::LongLong: | |
31 case QMetaType::ULongLong: return left.toInt() < right.toInt(); | |
32 case QMetaType::QDate: return left.toDate() < right.toDate(); | |
33 case QMetaType::QString: | |
34 default: return QString::compare(left.toString(), right.toString(), Qt::CaseInsensitive) < 0; | |
35 } | |
36 } | |
37 | |
38 /* -------------------------------------------- */ | |
39 | |
40 TorrentsPageListModel::TorrentsPageListModel(QObject* parent) : QAbstractListModel(parent) { | |
41 } | |
42 | |
43 QByteArray TorrentsPageListModel::DownloadTorrentList() { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
44 return HTTP::Get(session.config.torrents.feed_link); |
114 | 45 } |
46 | |
47 void TorrentsPageListModel::ParseTorrentList(const QByteArray& ba) { | |
48 std::istringstream stdstream(Strings::ToUtf8String(ba)); | |
49 | |
50 pugi::xml_document doc; | |
51 if (!doc.load(stdstream)) | |
52 return; // peace out | |
53 | |
54 /* my extra special dumb hack. */ | |
55 if (!rowCount(index(0))) { | |
56 beginInsertRows(QModelIndex(), 0, 0); | |
57 endInsertRows(); | |
58 } | |
59 | |
60 beginResetModel(); | |
61 | |
62 list.clear(); | |
63 /* this is just an rss parser; it should be in a separate class... */ | |
64 for (pugi::xml_node item : doc.child("rss").child("channel").children("item")) { | |
65 TorrentModelItem torrent; | |
66 torrent.SetFilename(item.child_value("title")); /* "title" == filename */ | |
67 { | |
68 /* Use Anitomy to parse the file's elements (we should *really* not be doing this this way!) */ | |
69 std::unordered_map<std::string, std::string> elements = Track::Media::GetFileElements(torrent.GetFilename()); | |
70 torrent.SetTitle(elements["title"]); | |
71 torrent.SetEpisode(Strings::RemoveLeadingChars(elements["episode"], '0')); | |
72 torrent.SetGroup(elements["group"]); | |
73 torrent.SetResolution(elements["resolution"]); | |
74 } | |
75 torrent.SetDescription(Strings::TextifySynopsis(item.child_value("description"))); | |
76 { | |
77 /* Parse size from description */ | |
78 std::istringstream descstream(torrent.GetDescription()); | |
79 | |
80 for (std::string line; std::getline(descstream, line);) { | |
81 const std::string match = "Size: "; | |
82 size_t pos = line.find(match); | |
83 | |
84 if (!pos) { | |
85 const std::string size = line.substr(pos + match.length()); | |
86 torrent.SetSize(Strings::HumanReadableSizeToBytes(size)); | |
87 } | |
88 } | |
89 } | |
90 torrent.SetLink(item.child_value("link")); | |
91 torrent.SetGuid(item.child_value("guid")); | |
92 { | |
93 const QString date_str = Strings::ToQString(item.child_value("pubDate")); | |
94 torrent.SetDate(QDateTime::fromString(date_str, "ddd, dd MMM yyyy HH:mm:ss t")); | |
95 } | |
96 list.push_back(torrent); | |
97 } | |
98 | |
99 endResetModel(); | |
100 } | |
101 | |
102 void TorrentsPageListModel::RefreshTorrentList() { | |
103 ParseTorrentList(DownloadTorrentList()); | |
104 } | |
105 | |
106 int TorrentsPageListModel::rowCount(const QModelIndex& parent) const { | |
107 return list.size(); | |
108 (void)(parent); | |
109 } | |
110 | |
111 int TorrentsPageListModel::columnCount(const QModelIndex& parent) const { | |
112 return NB_COLUMNS; | |
113 (void)(parent); | |
114 } | |
115 | |
116 QVariant TorrentsPageListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const { | |
117 switch (role) { | |
118 case Qt::DisplayRole: { | |
119 switch (section) { | |
120 case TL_TITLE: return tr("Anime title"); | |
121 case TL_EPISODE: return tr("Episode"); | |
122 case TL_GROUP: return tr("Group"); | |
123 case TL_SIZE: return tr("Size"); | |
124 case TL_RESOLUTION: return tr("Resolution"); /* this is named "Video" in Taiga */ | |
125 case TL_SEEDERS: return tr("Seeding"); /* named "S" in Taiga */ | |
126 case TL_LEECHERS: return tr("Leeching"); /* named "L" in Taiga */ | |
127 case TL_DOWNLOADERS: return tr("Downloading"); /* named "D" in Taiga */ | |
128 case TL_DESCRIPTION: return tr("Description"); | |
129 case TL_FILENAME: return tr("Filename"); | |
130 case TL_RELEASEDATE: return tr("Release date"); | |
131 default: return {}; | |
132 } | |
133 break; | |
134 } | |
135 case Qt::TextAlignmentRole: { | |
136 switch (section) { | |
137 case TL_FILENAME: | |
138 case TL_GROUP: | |
139 case TL_DESCRIPTION: | |
140 case TL_RESOLUTION: | |
141 case TL_TITLE: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); | |
142 case TL_SEEDERS: | |
143 case TL_LEECHERS: | |
144 case TL_DOWNLOADERS: | |
145 case TL_SIZE: | |
146 case TL_EPISODE: | |
147 case TL_RELEASEDATE: return QVariant(Qt::AlignRight | Qt::AlignVCenter); | |
148 default: return {}; | |
149 } | |
150 break; | |
151 } | |
152 } | |
153 return QAbstractListModel::headerData(section, orientation, role); | |
154 } | |
155 | |
156 bool TorrentsPageListModel::setData(const QModelIndex& index, const QVariant& value, int role) { | |
157 TorrentModelItem& item = list.at(index.row()); | |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
158 |
114 | 159 if (index.column() == 0) { |
160 switch (role) { | |
161 case Qt::EditRole: | |
162 return false; | |
163 case Qt::CheckStateRole: | |
164 item.SetChecked(value.toBool()); | |
165 emit dataChanged(index, index); | |
166 return true; | |
167 } | |
168 } | |
169 | |
170 return QAbstractItemModel::setData(index, value, role); | |
171 } | |
172 | |
173 QVariant TorrentsPageListModel::data(const QModelIndex& index, int role) const { | |
174 if (!index.isValid()) | |
175 return QVariant(); | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
176 |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
177 const TorrentModelItem& item = list.at(index.row()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
178 |
114 | 179 switch (role) { |
180 case Qt::DisplayRole: | |
181 switch (index.column()) { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
182 case TL_TITLE: return Strings::ToQString(item.GetTitle()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
183 case TL_EPISODE: return Strings::ToQString(item.GetEpisode()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
184 case TL_GROUP: return Strings::ToQString(item.GetGroup()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
185 case TL_SIZE: return session.config.locale.GetLocale().formattedDataSize(item.GetSize()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
186 case TL_RESOLUTION: return Strings::ToQString(item.GetResolution()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
187 case TL_SEEDERS: return item.GetSeeders(); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
188 case TL_LEECHERS: return item.GetLeechers(); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
189 case TL_DOWNLOADERS: return item.GetDownloaders(); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
190 case TL_DESCRIPTION: return Strings::ToQString(item.GetDescription()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
191 case TL_FILENAME: return Strings::ToQString(item.GetFilename()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
192 case TL_RELEASEDATE: return item.GetDate(); |
114 | 193 default: return ""; |
194 } | |
195 break; | |
196 case Qt::UserRole: | |
197 switch (index.column()) { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
198 case TL_EPISODE: return Strings::ToInt(item.GetEpisode(), -1); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
199 case TL_SIZE: return item.GetSize(); |
114 | 200 default: return data(index, Qt::DisplayRole); |
201 } | |
202 break; | |
203 case Qt::CheckStateRole: | |
204 switch (index.column()) { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
205 case 0: return item.GetChecked() ? Qt::Checked : Qt::Unchecked; |
114 | 206 default: return {}; |
207 } | |
208 case Qt::SizeHintRole: { | |
209 switch (index.column()) { | |
210 default: { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
211 /* max horizontal size of 100, height size = size of current font */ |
114 | 212 const QString d = data(index, Qt::DisplayRole).toString(); |
213 const QFontMetrics metric = QFontMetrics(QFont()); | |
214 | |
215 return QSize(std::max(metric.horizontalAdvance(d), 100), metric.height()); | |
216 } | |
217 } | |
218 break; | |
219 } | |
220 case Qt::TextAlignmentRole: | |
221 switch (index.column()) { | |
222 case TL_FILENAME: | |
223 case TL_GROUP: | |
224 case TL_DESCRIPTION: | |
225 case TL_RESOLUTION: | |
226 case TL_TITLE: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); | |
227 case TL_SEEDERS: | |
228 case TL_LEECHERS: | |
229 case TL_DOWNLOADERS: | |
230 case TL_SIZE: | |
231 case TL_EPISODE: | |
232 case TL_RELEASEDATE: return QVariant(Qt::AlignRight | Qt::AlignVCenter); | |
233 default: return {}; | |
234 } | |
235 break; | |
236 } | |
237 return QVariant(); | |
238 } | |
239 | |
240 Qt::ItemFlags TorrentsPageListModel::flags(const QModelIndex& index) const { | |
241 if (!index.isValid()) | |
242 return Qt::NoItemFlags; | |
243 | |
244 const TorrentModelItem& item = list.at(index.row()); | |
245 | |
246 if (item.GetChecked() || index.column() == 0) | |
247 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; | |
248 else | |
249 return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; | |
250 } | |
251 | |
252 TorrentsPage::TorrentsPage(QWidget* parent) : QFrame(parent) { | |
253 setFrameShape(QFrame::Box); | |
254 setFrameShadow(QFrame::Sunken); | |
255 | |
256 QVBoxLayout* layout = new QVBoxLayout(this); | |
257 layout->setContentsMargins(0, 0, 0, 0); | |
258 layout->setSpacing(0); | |
259 | |
260 { | |
261 /* Toolbar */ | |
262 QToolBar* toolbar = new QToolBar(this); | |
263 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
264 toolbar->setIconSize(QSize(16, 16)); | |
265 toolbar->setMovable(false); | |
266 | |
267 { | |
268 /* this needs to be stored somewhere to replicate Taiga's | |
269 "timer" feature */ | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
270 toolbar->addAction(QIcon(":/icons/16x16/arrow-circle-315.png"), tr("&Check new torrents"), [this]{ |
114 | 271 QThreadPool::globalInstance()->start([this] { |
272 Refresh(); | |
273 }); | |
274 }); | |
275 } | |
276 | |
277 toolbar->addSeparator(); | |
278 | |
279 { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
280 toolbar->addAction(QIcon(":/icons/16x16/navigation-270-button.png"), tr("Download &marked torrents")); |
114 | 281 } |
282 | |
283 { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
284 toolbar->addAction(QIcon(":/icons/16x16/cross-button.png"), tr("&Discard all")); |
114 | 285 } |
286 | |
287 toolbar->addSeparator(); | |
288 | |
289 { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
290 toolbar->addAction(QIcon(":/icons/16x16/gear.png"), tr("&Settings")); |
114 | 291 } |
292 | |
293 layout->addWidget(toolbar); | |
294 } | |
295 | |
296 { | |
297 QFrame* line = new QFrame(this); | |
298 line->setFrameShape(QFrame::HLine); | |
299 line->setFrameShadow(QFrame::Sunken); | |
300 line->setLineWidth(1); | |
301 layout->addWidget(line); | |
302 } | |
303 | |
304 { | |
305 QTreeView* treeview = new QTreeView(this); | |
306 treeview->setUniformRowHeights(true); | |
307 treeview->setAllColumnsShowFocus(false); | |
308 treeview->setAlternatingRowColors(true); | |
309 treeview->setSortingEnabled(true); | |
310 treeview->setSelectionMode(QAbstractItemView::ExtendedSelection); | |
311 treeview->setItemsExpandable(false); | |
312 treeview->setRootIsDecorated(false); | |
313 treeview->setContextMenuPolicy(Qt::CustomContextMenu); | |
314 treeview->setFrameShape(QFrame::NoFrame); | |
315 | |
316 { | |
317 sort_model = new TorrentsPageListSortFilter(treeview); | |
318 model = new TorrentsPageListModel(treeview); | |
319 sort_model->setSourceModel(model); | |
320 sort_model->setSortRole(Qt::UserRole); | |
321 sort_model->setSortCaseSensitivity(Qt::CaseInsensitive); | |
322 treeview->setModel(sort_model); | |
323 } | |
324 | |
325 layout->addWidget(treeview); | |
326 } | |
327 } | |
328 | |
329 void TorrentsPage::Refresh() { | |
330 if (!model) | |
331 return; | |
332 model->RefreshTorrentList(); | |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
333 } |
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
334 |
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
335 #include "gui/pages/moc_torrents.cpp" |