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