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