Mercurial > minori
annotate src/gui/pages/torrents.cc @ 241:06d6c351925c
*: reorganize resources, +docs, build translations with autotools
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 22 Jan 2024 16:36:19 -0500 |
parents | 4d461ef7d424 |
children | c130f47f6f48 |
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 |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
66 HTTP::GetThread* thread = new HTTP::GetThread(link, {}, this); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
67 |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
68 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
|
69 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
|
70 if (!file) |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
71 return; // wat |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
72 |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
73 file.write(data.data(), data.size()); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
74 file.close(); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
75 }); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
76 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
|
77 |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
78 thread->start(); |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
79 } |
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 |
114 | 82 QByteArray TorrentsPageListModel::DownloadTorrentList() { |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
83 return HTTP::Get(session.config.torrents.feed_link); |
114 | 84 } |
85 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
86 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
|
87 /* Parse description... */ |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
88 enum class Keys { SIZE, AUTHORIZED, SUBMITTER, COMMENT }; |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
89 |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
90 const std::unordered_map<std::string, Keys> KeyMap = { |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
91 {"Size", Keys::SIZE}, |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
92 {"Authorized", Keys::AUTHORIZED}, |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
93 {"Submitter", Keys::SUBMITTER}, |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
94 {"Comment", Keys::COMMENT} |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
95 }; |
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 /* Parse size from description */ |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
98 std::istringstream descstream(description); |
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 for (std::string line; std::getline(descstream, line);) { |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
101 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
|
102 if (pos == std::string::npos) |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
103 continue; |
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 const std::string key = line.substr(0, pos); |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
106 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
|
107 |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
108 switch (KeyMap.at(key)) { |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
109 case Keys::COMMENT: |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
110 torrent.SetDescription(value); |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
111 break; |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
112 case Keys::SIZE: |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
113 torrent.SetSize(Strings::HumanReadableSizeToBytes(value)); |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
114 break; |
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
115 case Keys::AUTHORIZED: |
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
116 if (torrent.GetGroup().empty() && value != "N/A") |
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
117 torrent.SetGroup(value); |
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
118 break; |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
119 default: |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
120 break; |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
121 } |
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 |
114 | 125 void TorrentsPageListModel::ParseTorrentList(const QByteArray& ba) { |
126 std::istringstream stdstream(Strings::ToUtf8String(ba)); | |
127 | |
128 pugi::xml_document doc; | |
129 if (!doc.load(stdstream)) | |
130 return; // peace out | |
131 | |
132 /* my extra special dumb hack. */ | |
133 if (!rowCount(index(0))) { | |
134 beginInsertRows(QModelIndex(), 0, 0); | |
135 endInsertRows(); | |
136 } | |
137 | |
138 beginResetModel(); | |
139 | |
140 list.clear(); | |
141 /* this is just an rss parser; it should be in a separate class... */ | |
142 for (pugi::xml_node item : doc.child("rss").child("channel").children("item")) { | |
143 TorrentModelItem torrent; | |
144 torrent.SetFilename(item.child_value("title")); /* "title" == filename */ | |
145 { | |
154 | 146 anitomy::Anitomy anitomy; |
147 anitomy.Parse(Strings::ToWstring(torrent.GetFilename())); | |
148 | |
149 const auto& elements = anitomy.elements(); | |
150 | |
151 /* todo: patch Anitomy so that it doesn't use wide strings */ | |
152 torrent.SetTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle))); | |
153 torrent.SetEpisode(Strings::RemoveLeadingChars(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)), '0')); | |
154 torrent.SetGroup(Strings::ToUtf8String(elements.get(anitomy::kElementReleaseGroup))); | |
155 torrent.SetResolution(Strings::ToUtf8String(elements.get(anitomy::kElementVideoResolution))); | |
114 | 156 } |
157 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
158 ParseFeedDescription(Strings::TextifySynopsis(item.child_value("description")), torrent); |
114 | 159 |
160 torrent.SetLink(item.child_value("link")); | |
161 torrent.SetGuid(item.child_value("guid")); | |
162 { | |
163 const QString date_str = Strings::ToQString(item.child_value("pubDate")); | |
164 torrent.SetDate(QDateTime::fromString(date_str, "ddd, dd MMM yyyy HH:mm:ss t")); | |
165 } | |
166 list.push_back(torrent); | |
167 } | |
168 | |
169 endResetModel(); | |
170 } | |
171 | |
172 void TorrentsPageListModel::RefreshTorrentList() { | |
173 ParseTorrentList(DownloadTorrentList()); | |
174 } | |
175 | |
176 int TorrentsPageListModel::rowCount(const QModelIndex& parent) const { | |
177 return list.size(); | |
178 (void)(parent); | |
179 } | |
180 | |
181 int TorrentsPageListModel::columnCount(const QModelIndex& parent) const { | |
182 return NB_COLUMNS; | |
183 (void)(parent); | |
184 } | |
185 | |
186 QVariant TorrentsPageListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const { | |
187 switch (role) { | |
188 case Qt::DisplayRole: { | |
189 switch (section) { | |
190 case TL_TITLE: return tr("Anime title"); | |
191 case TL_EPISODE: return tr("Episode"); | |
192 case TL_GROUP: return tr("Group"); | |
193 case TL_SIZE: return tr("Size"); | |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
194 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
|
195 case TL_SEEDERS: return tr("S"); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
196 case TL_LEECHERS: return tr("L"); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
197 case TL_DOWNLOADS: return tr("D"); |
114 | 198 case TL_DESCRIPTION: return tr("Description"); |
199 case TL_FILENAME: return tr("Filename"); | |
200 case TL_RELEASEDATE: return tr("Release date"); | |
201 default: return {}; | |
202 } | |
203 break; | |
204 } | |
205 case Qt::TextAlignmentRole: { | |
206 switch (section) { | |
207 case TL_FILENAME: | |
208 case TL_GROUP: | |
209 case TL_DESCRIPTION: | |
210 case TL_RESOLUTION: | |
211 case TL_TITLE: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); | |
212 case TL_SEEDERS: | |
213 case TL_LEECHERS: | |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
214 case TL_DOWNLOADS: |
114 | 215 case TL_SIZE: |
216 case TL_EPISODE: | |
217 case TL_RELEASEDATE: return QVariant(Qt::AlignRight | Qt::AlignVCenter); | |
218 default: return {}; | |
219 } | |
220 break; | |
221 } | |
222 } | |
223 return QAbstractListModel::headerData(section, orientation, role); | |
224 } | |
225 | |
226 bool TorrentsPageListModel::setData(const QModelIndex& index, const QVariant& value, int role) { | |
227 TorrentModelItem& item = list.at(index.row()); | |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
228 |
114 | 229 if (index.column() == 0) { |
230 switch (role) { | |
231 case Qt::EditRole: | |
232 return false; | |
233 case Qt::CheckStateRole: | |
234 item.SetChecked(value.toBool()); | |
235 emit dataChanged(index, index); | |
236 return true; | |
237 } | |
238 } | |
239 | |
240 return QAbstractItemModel::setData(index, value, role); | |
241 } | |
242 | |
243 QVariant TorrentsPageListModel::data(const QModelIndex& index, int role) const { | |
244 if (!index.isValid()) | |
245 return QVariant(); | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
246 |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
247 const TorrentModelItem& item = list.at(index.row()); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
248 |
114 | 249 switch (role) { |
250 case Qt::DisplayRole: | |
251 switch (index.column()) { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 case TL_SEEDERS: return item.GetSeeders(); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
258 case TL_LEECHERS: return item.GetLeechers(); |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
259 case TL_DOWNLOADS: return item.GetDownloads(); |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
260 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
|
261 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
|
262 case TL_RELEASEDATE: return item.GetDate(); |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
263 default: return {}; |
114 | 264 } |
265 break; | |
266 case Qt::UserRole: | |
267 switch (index.column()) { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
268 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
|
269 /* 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
|
270 * "conversion ambiguous" error on Linux |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
271 */ |
117
2c1b6782e1d0
pages/torrents: work around conversion error on Linux
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
272 case TL_SIZE: return QVariant::fromValue(item.GetSize()); |
114 | 273 default: return data(index, Qt::DisplayRole); |
274 } | |
275 break; | |
276 case Qt::SizeHintRole: { | |
277 switch (index.column()) { | |
278 default: { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
279 /* max horizontal size of 100, height size = size of current font */ |
114 | 280 const QString d = data(index, Qt::DisplayRole).toString(); |
281 const QFontMetrics metric = QFontMetrics(QFont()); | |
282 | |
283 return QSize(std::max(metric.horizontalAdvance(d), 100), metric.height()); | |
284 } | |
285 } | |
286 break; | |
287 } | |
288 case Qt::TextAlignmentRole: | |
289 switch (index.column()) { | |
290 case TL_FILENAME: | |
291 case TL_GROUP: | |
292 case TL_DESCRIPTION: | |
293 case TL_RESOLUTION: | |
294 case TL_TITLE: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); | |
295 case TL_SEEDERS: | |
296 case TL_LEECHERS: | |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
297 case TL_DOWNLOADS: |
114 | 298 case TL_SIZE: |
299 case TL_EPISODE: | |
300 case TL_RELEASEDATE: return QVariant(Qt::AlignRight | Qt::AlignVCenter); | |
301 default: return {}; | |
302 } | |
303 break; | |
304 } | |
305 return QVariant(); | |
306 } | |
307 | |
308 Qt::ItemFlags TorrentsPageListModel::flags(const QModelIndex& index) const { | |
309 if (!index.isValid()) | |
310 return Qt::NoItemFlags; | |
311 | |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
117
diff
changeset
|
312 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
114 | 313 } |
314 | |
315 TorrentsPage::TorrentsPage(QWidget* parent) : QFrame(parent) { | |
316 setFrameShape(QFrame::Box); | |
317 setFrameShadow(QFrame::Sunken); | |
318 | |
319 QVBoxLayout* layout = new QVBoxLayout(this); | |
320 layout->setContentsMargins(0, 0, 0, 0); | |
321 layout->setSpacing(0); | |
322 | |
323 { | |
324 /* Toolbar */ | |
325 QToolBar* toolbar = new QToolBar(this); | |
326 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
327 toolbar->setIconSize(QSize(16, 16)); | |
328 toolbar->setMovable(false); | |
329 | |
330 { | |
331 /* this needs to be stored somewhere to replicate Taiga's | |
332 "timer" feature */ | |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
333 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
|
334 Refresh(); |
114 | 335 }); |
336 } | |
337 | |
338 toolbar->addSeparator(); | |
339 | |
340 { | |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
341 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
|
342 DownloadSelection(); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
343 }); |
114 | 344 } |
345 | |
346 { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
347 toolbar->addAction(QIcon(":/icons/16x16/cross-button.png"), tr("&Discard all")); |
114 | 348 } |
349 | |
350 toolbar->addSeparator(); | |
351 | |
352 { | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
353 toolbar->addAction(QIcon(":/icons/16x16/gear.png"), tr("&Settings")); |
114 | 354 } |
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 |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
389 treeview->setColumnWidth(TorrentsPageListModel::TL_TITLE, 240); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
390 treeview->setColumnWidth(TorrentsPageListModel::TL_EPISODE, 60); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
391 treeview->setColumnWidth(TorrentsPageListModel::TL_GROUP, 100); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
392 treeview->setColumnWidth(TorrentsPageListModel::TL_SIZE, 70); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
393 treeview->setColumnWidth(TorrentsPageListModel::TL_RESOLUTION, 100); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
394 treeview->setColumnWidth(TorrentsPageListModel::TL_SEEDERS, 20); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
395 treeview->setColumnWidth(TorrentsPageListModel::TL_LEECHERS, 20); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
396 treeview->setColumnWidth(TorrentsPageListModel::TL_DOWNLOADS, 20); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
397 treeview->setColumnWidth(TorrentsPageListModel::TL_DESCRIPTION, 200); |
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
398 treeview->setColumnWidth(TorrentsPageListModel::TL_FILENAME, 200); |
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 |
114 | 401 layout->addWidget(treeview); |
402 } | |
403 } | |
404 | |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
405 void TorrentsPage::DownloadSelection() { |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
406 if (!model) |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
407 return; |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
408 |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
409 const QItemSelection selection = sort_model->mapSelectionToSource(treeview->selectionModel()->selection()); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
410 |
236
4d461ef7d424
HUGE UPDATE: convert build system to autotools
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
411 model->DownloadTorrents(selection); |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
412 } |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
413 |
114 | 414 void TorrentsPage::Refresh() { |
415 if (!model) | |
416 return; | |
230
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 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
|
419 |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
420 connect(thread, &HTTP::GetThread::ReceivedData, this, [&](const QByteArray& ba) { |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
421 /* 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
|
422 * messing around with GUI stuff |
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 treeview->setUpdatesEnabled(false); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
425 model->ParseTorrentList(ba); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
426 treeview->setUpdatesEnabled(true); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
427 }); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
428 connect(thread, &QThread::finished, thread, &QThread::deleteLater); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
429 |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
430 thread->start(); |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
431 } |