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