Mercurial > minori
annotate src/gui/pages/torrents.cc @ 367:8d45d892be88
*: instead of pugixml, use Qt XML features
this means we have one extra Qt dependency though...
| author | Paper <paper@tflc.us> |
|---|---|
| date | Sun, 17 Nov 2024 22:55:47 -0500 |
| parents | a0aa8c8c4307 |
| children | ea3a74ed2ef9 |
| rev | line source |
|---|---|
|
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "gui/pages/torrents.h" |
| 258 | 2 #include "core/filesystem.h" |
| 114 | 3 #include "core/http.h" |
| 4 #include "core/session.h" | |
| 258 | 5 #include "core/strings.h" |
| 114 | 6 #include "gui/widgets/text.h" |
| 7 #include "track/media.h" | |
|
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
154
diff
changeset
|
8 |
| 114 | 9 #include <QByteArray> |
| 10 #include <QDataStream> | |
| 258 | 11 #include <QDebug> |
| 12 #include <QHeaderView> | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
13 #include <QThread> |
| 258 | 14 #include <QToolBar> |
| 15 #include <QTreeView> | |
| 16 #include <QVBoxLayout> | |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
17 #include <QtGlobal> |
|
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
154
diff
changeset
|
18 |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
19 #include <QDomDocument> |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
20 |
| 258 | 21 #include <algorithm> |
| 22 #include <fstream> | |
| 114 | 23 #include <iostream> |
| 24 #include <sstream> | |
| 25 | |
| 258 | 26 #include "anitomy/anitomy.h" |
| 154 | 27 |
|
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
117
diff
changeset
|
28 /* This file is very, very similar to the anime list page. |
| 250 | 29 * |
| 30 * It differs from Taiga in that it uses tabs instead of | |
| 31 * those "groups", but those are custom painted and a pain in the ass to | |
| 32 * maintain over multiple platforms. | |
| 258 | 33 */ |
|
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
117
diff
changeset
|
34 |
| 114 | 35 TorrentsPageListSortFilter::TorrentsPageListSortFilter(QObject* parent) : QSortFilterProxyModel(parent) { |
| 36 } | |
| 37 | |
| 38 bool TorrentsPageListSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const { | |
| 39 QVariant left = sourceModel()->data(l, sortRole()); | |
| 40 QVariant right = sourceModel()->data(r, sortRole()); | |
| 41 | |
| 42 switch (left.userType()) { | |
| 43 case QMetaType::Int: | |
| 44 case QMetaType::UInt: | |
| 45 case QMetaType::LongLong: | |
| 46 case QMetaType::ULongLong: return left.toInt() < right.toInt(); | |
| 47 case QMetaType::QDate: return left.toDate() < right.toDate(); | |
| 48 case QMetaType::QString: | |
| 49 default: return QString::compare(left.toString(), right.toString(), Qt::CaseInsensitive) < 0; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 /* -------------------------------------------- */ | |
| 54 | |
| 55 TorrentsPageListModel::TorrentsPageListModel(QObject* parent) : QAbstractListModel(parent) { | |
| 56 } | |
| 57 | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
58 void TorrentsPageListModel::DownloadTorrents(QItemSelection selection) { |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
59 const auto indexes = selection.indexes(); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
60 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
61 for (const auto& index : indexes) { |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
62 /* a torrent file IS literally text... */ |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
63 const std::string link = list.at(index.row()).GetLink(); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
64 const std::string filename = list.at(index.row()).GetFilename() + ".torrent"; |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
65 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
66 const std::filesystem::path torrents_dir = Filesystem::GetTorrentsPath(); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
67 std::filesystem::create_directories(torrents_dir); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
68 |
| 291 | 69 /* this sucks */ |
| 70 HTTP::RequestThread* thread = new HTTP::RequestThread(link, {}, "", HTTP::Type::Get, this); | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
71 |
| 291 | 72 connect(thread, &HTTP::RequestThread::ReceivedData, this, [this, torrents_dir, filename](const QByteArray& data) { |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
73 std::ofstream file(torrents_dir / filename, std::ofstream::out | std::ofstream::trunc); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
74 if (!file) |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
75 return; // wat |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
76 |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
77 file.write(data.data(), data.size()); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
78 file.close(); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
79 }); |
| 291 | 80 connect(thread, &HTTP::RequestThread::finished, thread, &HTTP::RequestThread::deleteLater); |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
81 |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
82 thread->start(); |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
83 } |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
84 } |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
85 |
| 114 | 86 QByteArray TorrentsPageListModel::DownloadTorrentList() { |
| 291 | 87 return HTTP::Request(session.config.torrents.feed_link); |
| 114 | 88 } |
| 89 | |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
90 void TorrentsPageListModel::ParseFeedDescription(const std::string& description, Torrent& torrent) { |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
91 /* Parse description... */ |
| 258 | 92 enum class Keys { |
| 93 SIZE, | |
| 94 AUTHORIZED, | |
| 95 SUBMITTER, | |
| 96 COMMENT | |
| 97 }; | |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
98 |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
99 const std::unordered_map<std::string, Keys> KeyMap = { |
| 258 | 100 {"Size", Keys::SIZE }, |
| 101 {"Authorized", Keys::AUTHORIZED}, | |
| 102 {"Submitter", Keys::SUBMITTER }, | |
| 103 {"Comment", Keys::COMMENT } | |
| 104 }; | |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
105 |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
106 /* Parse size from description */ |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
107 std::istringstream descstream(description); |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
108 |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
109 for (std::string line; std::getline(descstream, line);) { |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
110 const size_t pos = line.find_first_of(':', 0); |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
111 if (pos == std::string::npos) |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
112 continue; |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
113 |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
114 const std::string key = line.substr(0, pos); |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
115 const std::string value = line.substr(line.find_first_not_of(": ", pos)); |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
116 |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
117 switch (KeyMap.at(key)) { |
| 258 | 118 case Keys::COMMENT: torrent.SetDescription(value); break; |
| 119 case Keys::SIZE: torrent.SetSize(Strings::HumanReadableSizeToBytes(value)); break; | |
|
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
120 case Keys::AUTHORIZED: |
|
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
121 if (torrent.GetGroup().empty() && value != "N/A") |
|
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
122 torrent.SetGroup(value); |
|
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
123 break; |
| 258 | 124 default: break; |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
125 } |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
126 } |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
127 } |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
128 |
| 114 | 129 void TorrentsPageListModel::ParseTorrentList(const QByteArray& ba) { |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
130 QDomDocument doc; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
131 QDomNode node; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
132 QDomNodeList node_nodes; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
133 { |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
134 QString err; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
135 int err_ln; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
136 int err_col; |
| 114 | 137 |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
138 if (!doc.setContent(ba, &err, &err_ln, &err_col)) { |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
139 session.SetStatusBar(Strings::ToUtf8String(tr("Torrents: Failed to parse XML with error %1 at line %2, column %3").arg(err, QString::number(err_ln), QString::number(err_col)))); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
140 return; // peace out |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
141 } |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
142 } |
| 114 | 143 |
| 144 /* my extra special dumb hack. */ | |
| 145 if (!rowCount(index(0))) { | |
| 146 beginInsertRows(QModelIndex(), 0, 0); | |
| 147 endInsertRows(); | |
| 148 } | |
| 149 | |
| 150 beginResetModel(); | |
| 151 | |
| 152 list.clear(); | |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
153 |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
154 node = doc; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
155 |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
156 for (const auto& n : {"rss", "channel"}) { |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
157 node = node.namedItem(n); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
158 if (node.isNull()) { std::cout << n << std::endl; goto end; } |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
159 } |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
160 |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
161 if (!node.hasChildNodes()) { std::cout << "no child nodes" << std::endl; goto end; } |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
162 |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
163 node_nodes = node.childNodes(); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
164 |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
165 for (int c = 0; c < node_nodes.count(); c++) { |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
166 const QDomNode item = node_nodes.at(c); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
167 if (!item.isElement() || item.nodeName() != "item") |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
168 continue; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
169 |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
170 const QDomNode title = item.namedItem("title"); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
171 if (!title.isElement()) continue; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
172 const QDomNode description = item.namedItem("description"); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
173 if (!description.isElement()) continue; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
174 const QDomNode link = item.namedItem("link"); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
175 if (!link.isElement()) continue; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
176 const QDomNode guid = item.namedItem("guid"); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
177 if (!guid.isElement()) continue; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
178 const QDomNode pubDate = item.namedItem("pubDate"); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
179 if (!pubDate.isElement()) continue; |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
180 |
| 114 | 181 TorrentModelItem torrent; |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
182 torrent.SetFilename(Strings::ToUtf8String(title.toElement().text())); /* "title" == filename */ |
| 114 | 183 { |
| 154 | 184 anitomy::Anitomy anitomy; |
|
347
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
185 anitomy.Parse(torrent.GetFilename()); |
| 154 | 186 |
| 187 const auto& elements = anitomy.elements(); | |
| 188 | |
| 189 /* todo: patch Anitomy so that it doesn't use wide strings */ | |
| 190 torrent.SetTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle))); | |
| 264 | 191 std::string episode = Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)); |
| 192 Strings::RemoveLeadingChars(episode, '0'); | |
| 193 torrent.SetEpisode(episode); | |
| 154 | 194 torrent.SetGroup(Strings::ToUtf8String(elements.get(anitomy::kElementReleaseGroup))); |
| 195 torrent.SetResolution(Strings::ToUtf8String(elements.get(anitomy::kElementVideoResolution))); | |
| 114 | 196 } |
| 197 | |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
198 std::string description_s = Strings::ToUtf8String(description.toElement().text()); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
199 Strings::TextifySynopsis(description_s); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
200 ParseFeedDescription(description_s, torrent); |
| 114 | 201 |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
202 torrent.SetLink(Strings::ToUtf8String(link.toElement().text())); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
203 torrent.SetGuid(Strings::ToUtf8String(guid.toElement().text())); |
|
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
204 torrent.SetDate(QDateTime::fromString(pubDate.toElement().text(), "ddd, dd MMM yyyy HH:mm:ss t")); |
| 114 | 205 list.push_back(torrent); |
| 206 } | |
| 207 | |
|
367
8d45d892be88
*: instead of pugixml, use Qt XML features
Paper <paper@tflc.us>
parents:
347
diff
changeset
|
208 end: |
| 114 | 209 endResetModel(); |
| 210 } | |
| 211 | |
| 212 void TorrentsPageListModel::RefreshTorrentList() { | |
| 213 ParseTorrentList(DownloadTorrentList()); | |
| 214 } | |
| 215 | |
| 216 int TorrentsPageListModel::rowCount(const QModelIndex& parent) const { | |
| 217 return list.size(); | |
| 218 (void)(parent); | |
| 219 } | |
| 220 | |
| 221 int TorrentsPageListModel::columnCount(const QModelIndex& parent) const { | |
| 222 return NB_COLUMNS; | |
| 223 (void)(parent); | |
| 224 } | |
| 225 | |
| 226 QVariant TorrentsPageListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const { | |
| 227 switch (role) { | |
| 228 case Qt::DisplayRole: { | |
| 229 switch (section) { | |
| 230 case TL_TITLE: return tr("Anime title"); | |
| 231 case TL_EPISODE: return tr("Episode"); | |
| 232 case TL_GROUP: return tr("Group"); | |
| 233 case TL_SIZE: return tr("Size"); | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
234 case TL_RESOLUTION: return tr("Resolution"); /* "Video" in Taiga */ |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
235 case TL_SEEDERS: return tr("S"); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
236 case TL_LEECHERS: return tr("L"); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
237 case TL_DOWNLOADS: return tr("D"); |
| 114 | 238 case TL_DESCRIPTION: return tr("Description"); |
| 239 case TL_FILENAME: return tr("Filename"); | |
| 240 case TL_RELEASEDATE: return tr("Release date"); | |
| 241 default: return {}; | |
| 242 } | |
| 243 break; | |
| 244 } | |
| 245 case Qt::TextAlignmentRole: { | |
| 246 switch (section) { | |
| 247 case TL_FILENAME: | |
| 248 case TL_GROUP: | |
| 249 case TL_DESCRIPTION: | |
| 250 case TL_RESOLUTION: | |
| 251 case TL_TITLE: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); | |
| 252 case TL_SEEDERS: | |
| 253 case TL_LEECHERS: | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
254 case TL_DOWNLOADS: |
| 114 | 255 case TL_SIZE: |
| 256 case TL_EPISODE: | |
| 257 case TL_RELEASEDATE: return QVariant(Qt::AlignRight | Qt::AlignVCenter); | |
| 258 default: return {}; | |
| 259 } | |
| 260 break; | |
| 261 } | |
| 262 } | |
| 263 return QAbstractListModel::headerData(section, orientation, role); | |
| 264 } | |
| 265 | |
| 266 bool TorrentsPageListModel::setData(const QModelIndex& index, const QVariant& value, int role) { | |
| 267 TorrentModelItem& item = list.at(index.row()); | |
|
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
268 |
| 114 | 269 if (index.column() == 0) { |
| 270 switch (role) { | |
| 258 | 271 case Qt::EditRole: return false; |
| 114 | 272 case Qt::CheckStateRole: |
| 273 item.SetChecked(value.toBool()); | |
| 274 emit dataChanged(index, index); | |
| 275 return true; | |
| 276 } | |
| 277 } | |
| 278 | |
| 279 return QAbstractItemModel::setData(index, value, role); | |
| 280 } | |
| 281 | |
| 282 QVariant TorrentsPageListModel::data(const QModelIndex& index, int role) const { | |
| 283 if (!index.isValid()) | |
| 284 return QVariant(); | |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
285 |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
286 const TorrentModelItem& item = list.at(index.row()); |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
287 |
| 114 | 288 switch (role) { |
| 289 case Qt::DisplayRole: | |
| 290 switch (index.column()) { | |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
291 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
|
292 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
|
293 case TL_GROUP: return Strings::ToQString(item.GetGroup()); |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
294 case TL_SIZE: return Strings::ToQString(Strings::BytesToHumanReadableSize(item.GetSize())); |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
295 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
|
296 case TL_SEEDERS: return item.GetSeeders(); |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
297 case TL_LEECHERS: return item.GetLeechers(); |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
298 case TL_DOWNLOADS: return item.GetDownloads(); |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
299 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
|
300 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
|
301 case TL_RELEASEDATE: return item.GetDate(); |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
302 default: return {}; |
| 114 | 303 } |
| 304 break; | |
| 305 case Qt::UserRole: | |
| 306 switch (index.column()) { | |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
307 case TL_EPISODE: return Strings::ToInt(item.GetEpisode(), -1); |
|
117
2c1b6782e1d0
pages/torrents: work around conversion error on Linux
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
308 /* We have to use this to work around some stupid |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
309 * "conversion ambiguous" error on Linux |
| 258 | 310 */ |
|
117
2c1b6782e1d0
pages/torrents: work around conversion error on Linux
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
311 case TL_SIZE: return QVariant::fromValue(item.GetSize()); |
| 114 | 312 default: return data(index, Qt::DisplayRole); |
| 313 } | |
| 314 break; | |
| 315 case Qt::SizeHintRole: { | |
| 316 switch (index.column()) { | |
| 317 default: { | |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
318 /* max horizontal size of 100, height size = size of current font */ |
| 114 | 319 const QString d = data(index, Qt::DisplayRole).toString(); |
| 320 const QFontMetrics metric = QFontMetrics(QFont()); | |
| 321 | |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
322 return QSize(std::max(metric.boundingRect(d).width(), 100), metric.height()); |
| 114 | 323 } |
| 324 } | |
| 325 break; | |
| 326 } | |
| 327 case Qt::TextAlignmentRole: | |
| 328 switch (index.column()) { | |
| 329 case TL_FILENAME: | |
| 330 case TL_GROUP: | |
| 331 case TL_DESCRIPTION: | |
| 332 case TL_RESOLUTION: | |
| 333 case TL_TITLE: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); | |
| 334 case TL_SEEDERS: | |
| 335 case TL_LEECHERS: | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
336 case TL_DOWNLOADS: |
| 114 | 337 case TL_SIZE: |
| 338 case TL_EPISODE: | |
| 339 case TL_RELEASEDATE: return QVariant(Qt::AlignRight | Qt::AlignVCenter); | |
| 340 default: return {}; | |
| 341 } | |
| 342 break; | |
| 343 } | |
| 344 return QVariant(); | |
| 345 } | |
| 346 | |
| 347 Qt::ItemFlags TorrentsPageListModel::flags(const QModelIndex& index) const { | |
| 348 if (!index.isValid()) | |
| 349 return Qt::NoItemFlags; | |
| 350 | |
|
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
117
diff
changeset
|
351 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
| 114 | 352 } |
| 353 | |
| 354 TorrentsPage::TorrentsPage(QWidget* parent) : QFrame(parent) { | |
| 355 setFrameShape(QFrame::Box); | |
| 356 setFrameShadow(QFrame::Sunken); | |
| 357 | |
| 358 QVBoxLayout* layout = new QVBoxLayout(this); | |
| 359 layout->setContentsMargins(0, 0, 0, 0); | |
| 360 layout->setSpacing(0); | |
| 361 | |
| 362 { | |
| 363 /* Toolbar */ | |
| 364 QToolBar* toolbar = new QToolBar(this); | |
| 365 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
| 366 toolbar->setIconSize(QSize(16, 16)); | |
| 367 toolbar->setMovable(false); | |
| 368 | |
| 369 { | |
| 370 /* this needs to be stored somewhere to replicate Taiga's | |
| 371 "timer" feature */ | |
| 258 | 372 toolbar->addAction(QIcon(":/icons/16x16/arrow-circle-315.png"), tr("&Check new torrents"), |
| 373 [this] { Refresh(); }); | |
| 114 | 374 } |
| 375 | |
| 376 toolbar->addSeparator(); | |
| 377 | |
| 378 { | |
| 258 | 379 toolbar->addAction(QIcon(":/icons/16x16/navigation-270-button.png"), tr("Download &marked torrents"), |
| 380 [this] { DownloadSelection(); }); | |
| 114 | 381 } |
| 382 | |
| 258 | 383 { toolbar->addAction(QIcon(":/icons/16x16/cross-button.png"), tr("&Discard all")); } |
| 114 | 384 |
| 385 toolbar->addSeparator(); | |
| 386 | |
| 258 | 387 { toolbar->addAction(QIcon(":/icons/16x16/gear.png"), tr("&Settings")); } |
| 114 | 388 |
| 389 layout->addWidget(toolbar); | |
| 390 } | |
| 391 | |
| 392 { | |
| 393 QFrame* line = new QFrame(this); | |
| 394 line->setFrameShape(QFrame::HLine); | |
| 395 line->setFrameShadow(QFrame::Sunken); | |
| 396 line->setLineWidth(1); | |
| 397 layout->addWidget(line); | |
| 398 } | |
| 399 | |
| 400 { | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
401 treeview = new QTreeView(this); |
| 114 | 402 treeview->setUniformRowHeights(true); |
| 403 treeview->setAllColumnsShowFocus(false); | |
| 404 treeview->setAlternatingRowColors(true); | |
| 405 treeview->setSortingEnabled(true); | |
| 406 treeview->setSelectionMode(QAbstractItemView::ExtendedSelection); | |
| 407 treeview->setItemsExpandable(false); | |
| 408 treeview->setRootIsDecorated(false); | |
| 409 treeview->setContextMenuPolicy(Qt::CustomContextMenu); | |
| 410 treeview->setFrameShape(QFrame::NoFrame); | |
| 411 | |
| 412 { | |
| 413 sort_model = new TorrentsPageListSortFilter(treeview); | |
| 414 model = new TorrentsPageListModel(treeview); | |
| 415 sort_model->setSourceModel(model); | |
| 416 sort_model->setSortRole(Qt::UserRole); | |
| 417 sort_model->setSortCaseSensitivity(Qt::CaseInsensitive); | |
| 418 treeview->setModel(sort_model); | |
| 419 } | |
| 420 | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
421 // set column sizes |
| 258 | 422 treeview->setColumnWidth(TorrentsPageListModel::TL_TITLE, 240); |
| 423 treeview->setColumnWidth(TorrentsPageListModel::TL_EPISODE, 60); | |
| 424 treeview->setColumnWidth(TorrentsPageListModel::TL_GROUP, 100); | |
| 425 treeview->setColumnWidth(TorrentsPageListModel::TL_SIZE, 70); | |
| 426 treeview->setColumnWidth(TorrentsPageListModel::TL_RESOLUTION, 100); | |
| 427 treeview->setColumnWidth(TorrentsPageListModel::TL_SEEDERS, 20); | |
| 428 treeview->setColumnWidth(TorrentsPageListModel::TL_LEECHERS, 20); | |
| 429 treeview->setColumnWidth(TorrentsPageListModel::TL_DOWNLOADS, 20); | |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
430 treeview->setColumnWidth(TorrentsPageListModel::TL_DESCRIPTION, 200); |
| 258 | 431 treeview->setColumnWidth(TorrentsPageListModel::TL_FILENAME, 200); |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
432 treeview->setColumnWidth(TorrentsPageListModel::TL_RELEASEDATE, 190); |
|
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
433 |
| 250 | 434 treeview->header()->setStretchLastSection(false); |
| 435 | |
| 114 | 436 layout->addWidget(treeview); |
| 437 } | |
| 438 } | |
| 439 | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
440 void TorrentsPage::DownloadSelection() { |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
441 if (!model) |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
442 return; |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
443 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
444 const QItemSelection selection = sort_model->mapSelectionToSource(treeview->selectionModel()->selection()); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
445 |
|
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
446 model->DownloadTorrents(selection); |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
447 } |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
448 |
| 114 | 449 void TorrentsPage::Refresh() { |
| 450 if (!model) | |
| 451 return; | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
452 |
| 291 | 453 HTTP::RequestThread* thread = new HTTP::RequestThread(session.config.torrents.feed_link); |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
454 |
| 291 | 455 connect(thread, &HTTP::RequestThread::ReceivedData, this, [&](const QByteArray& ba) { |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
456 /* This is to make sure we aren't in a different thread |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
457 * messing around with GUI stuff |
| 258 | 458 */ |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
459 treeview->setUpdatesEnabled(false); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
460 model->ParseTorrentList(ba); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
461 treeview->setUpdatesEnabled(true); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
462 }); |
| 291 | 463 connect(thread, &HTTP::RequestThread::finished, thread, &HTTP::RequestThread::deleteLater); |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
464 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
465 thread->start(); |
|
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
466 } |
