diff src/gui/pages/torrents.cc @ 370:ea3a74ed2ef9

*: hm, last commit wasn't quite finished?
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:22:04 -0400
parents 8d45d892be88
children
line wrap: on
line diff
--- a/src/gui/pages/torrents.cc	Fri Jul 25 10:16:02 2025 -0400
+++ b/src/gui/pages/torrents.cc	Fri Jul 25 10:22:04 2025 -0400
@@ -32,10 +32,12 @@
  * maintain over multiple platforms.
  */
 
-TorrentsPageListSortFilter::TorrentsPageListSortFilter(QObject* parent) : QSortFilterProxyModel(parent) {
+TorrentsPageListSortFilter::TorrentsPageListSortFilter(QObject *parent) : QSortFilterProxyModel(parent)
+{
 }
 
-bool TorrentsPageListSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const {
+bool TorrentsPageListSortFilter::lessThan(const QModelIndex &l, const QModelIndex &r) const
+{
 	QVariant left = sourceModel()->data(l, sortRole());
 	QVariant right = sourceModel()->data(r, sortRole());
 
@@ -52,13 +54,15 @@
 
 /* -------------------------------------------- */
 
-TorrentsPageListModel::TorrentsPageListModel(QObject* parent) : QAbstractListModel(parent) {
+TorrentsPageListModel::TorrentsPageListModel(QObject *parent) : QAbstractListModel(parent)
+{
 }
 
-void TorrentsPageListModel::DownloadTorrents(QItemSelection selection) {
+void TorrentsPageListModel::DownloadTorrents(QItemSelection selection)
+{
 	const auto indexes = selection.indexes();
 
-	for (const auto& index : indexes) {
+	for (const auto &index : indexes) {
 		/* a torrent file IS literally text... */
 		const std::string link = list.at(index.row()).GetLink();
 		const std::string filename = list.at(index.row()).GetFilename() + ".torrent";
@@ -67,27 +71,30 @@
 		std::filesystem::create_directories(torrents_dir);
 
 		/* this sucks */
-		HTTP::RequestThread* thread = new HTTP::RequestThread(link, {}, "", HTTP::Type::Get, this);
+		HTTP::RequestThread *thread = new HTTP::RequestThread(link, {}, "", HTTP::Type::Get, this);
 
-		connect(thread, &HTTP::RequestThread::ReceivedData, this, [this, torrents_dir, filename](const QByteArray& data) {
-			std::ofstream file(torrents_dir / filename, std::ofstream::out | std::ofstream::trunc);
-			if (!file)
-				return; // wat
+		connect(thread, &HTTP::RequestThread::ReceivedData, this,
+		        [this, torrents_dir, filename](const QByteArray &data) {
+			        std::ofstream file(torrents_dir / filename, std::ofstream::out | std::ofstream::trunc);
+			        if (!file)
+				        return; // wat
 
-			file.write(data.data(), data.size());
-			file.close();
-		});
+			        file.write(data.data(), data.size());
+			        file.close();
+		        });
 		connect(thread, &HTTP::RequestThread::finished, thread, &HTTP::RequestThread::deleteLater);
 
 		thread->start();
 	}
 }
 
-QByteArray TorrentsPageListModel::DownloadTorrentList() {
+QByteArray TorrentsPageListModel::DownloadTorrentList()
+{
 	return HTTP::Request(session.config.torrents.feed_link);
 }
 
-void TorrentsPageListModel::ParseFeedDescription(const std::string& description, Torrent& torrent) {
+void TorrentsPageListModel::ParseFeedDescription(const std::string &description, Torrent &torrent)
+{
 	/* Parse description... */
 	enum class Keys {
 		SIZE,
@@ -126,7 +133,8 @@
 	}
 }
 
-void TorrentsPageListModel::ParseTorrentList(const QByteArray& ba) {
+void TorrentsPageListModel::ParseTorrentList(const QByteArray &ba)
+{
 	QDomDocument doc;
 	QDomNode node;
 	QDomNodeList node_nodes;
@@ -136,7 +144,9 @@
 		int err_col;
 
 		if (!doc.setContent(ba, &err, &err_ln, &err_col)) {
-			session.SetStatusBar(Strings::ToUtf8String(tr("Torrents: Failed to parse XML with error %1 at line %2, column %3").arg(err, QString::number(err_ln), QString::number(err_col))));
+			session.SetStatusBar(
+			    Strings::ToUtf8String(tr("Torrents: Failed to parse XML with error %1 at line %2, column %3")
+			                              .arg(err, QString::number(err_ln), QString::number(err_col))));
 			return; // peace out
 		}
 	}
@@ -153,12 +163,18 @@
 
 	node = doc;
 
-	for (const auto& n : {"rss", "channel"}) {
+	for (const auto &n : {"rss", "channel"}) {
 		node = node.namedItem(n);
-		if (node.isNull()) { std::cout << n << std::endl; goto end; }
+		if (node.isNull()) {
+			std::cout << n << std::endl;
+			goto end;
+		}
 	}
 
-	if (!node.hasChildNodes()) { std::cout << "no child nodes" << std::endl; goto end; }
+	if (!node.hasChildNodes()) {
+		std::cout << "no child nodes" << std::endl;
+		goto end;
+	}
 
 	node_nodes = node.childNodes();
 
@@ -168,15 +184,20 @@
 			continue;
 
 		const QDomNode title = item.namedItem("title");
-		if (!title.isElement()) continue;
+		if (!title.isElement())
+			continue;
 		const QDomNode description = item.namedItem("description");
-		if (!description.isElement()) continue;
+		if (!description.isElement())
+			continue;
 		const QDomNode link = item.namedItem("link");
-		if (!link.isElement()) continue;
+		if (!link.isElement())
+			continue;
 		const QDomNode guid = item.namedItem("guid");
-		if (!guid.isElement()) continue;
+		if (!guid.isElement())
+			continue;
 		const QDomNode pubDate = item.namedItem("pubDate");
-		if (!pubDate.isElement()) continue;
+		if (!pubDate.isElement())
+			continue;
 
 		TorrentModelItem torrent;
 		torrent.SetFilename(Strings::ToUtf8String(title.toElement().text())); /* "title" == filename */
@@ -184,7 +205,7 @@
 			anitomy::Anitomy anitomy;
 			anitomy.Parse(torrent.GetFilename());
 
-			const auto& elements = anitomy.elements();
+			const auto &elements = anitomy.elements();
 
 			/* todo: patch Anitomy so that it doesn't use wide strings */
 			torrent.SetTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle)));
@@ -209,21 +230,25 @@
 	endResetModel();
 }
 
-void TorrentsPageListModel::RefreshTorrentList() {
+void TorrentsPageListModel::RefreshTorrentList()
+{
 	ParseTorrentList(DownloadTorrentList());
 }
 
-int TorrentsPageListModel::rowCount(const QModelIndex& parent) const {
+int TorrentsPageListModel::rowCount(const QModelIndex &parent) const
+{
 	return list.size();
 	(void)(parent);
 }
 
-int TorrentsPageListModel::columnCount(const QModelIndex& parent) const {
+int TorrentsPageListModel::columnCount(const QModelIndex &parent) const
+{
 	return NB_COLUMNS;
 	(void)(parent);
 }
 
-QVariant TorrentsPageListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const {
+QVariant TorrentsPageListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const
+{
 	switch (role) {
 		case Qt::DisplayRole: {
 			switch (section) {
@@ -263,8 +288,9 @@
 	return QAbstractListModel::headerData(section, orientation, role);
 }
 
-bool TorrentsPageListModel::setData(const QModelIndex& index, const QVariant& value, int role) {
-	TorrentModelItem& item = list.at(index.row());
+bool TorrentsPageListModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+	TorrentModelItem &item = list.at(index.row());
 
 	if (index.column() == 0) {
 		switch (role) {
@@ -279,11 +305,12 @@
 	return QAbstractItemModel::setData(index, value, role);
 }
 
-QVariant TorrentsPageListModel::data(const QModelIndex& index, int role) const {
+QVariant TorrentsPageListModel::data(const QModelIndex &index, int role) const
+{
 	if (!index.isValid())
 		return QVariant();
 
-	const TorrentModelItem& item = list.at(index.row());
+	const TorrentModelItem &item = list.at(index.row());
 
 	switch (role) {
 		case Qt::DisplayRole:
@@ -344,24 +371,26 @@
 	return QVariant();
 }
 
-Qt::ItemFlags TorrentsPageListModel::flags(const QModelIndex& index) const {
+Qt::ItemFlags TorrentsPageListModel::flags(const QModelIndex &index) const
+{
 	if (!index.isValid())
 		return Qt::NoItemFlags;
 
 	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 }
 
-TorrentsPage::TorrentsPage(QWidget* parent) : QFrame(parent) {
+TorrentsPage::TorrentsPage(QWidget *parent) : QFrame(parent)
+{
 	setFrameShape(QFrame::Box);
 	setFrameShadow(QFrame::Sunken);
 
-	QVBoxLayout* layout = new QVBoxLayout(this);
+	QVBoxLayout *layout = new QVBoxLayout(this);
 	layout->setContentsMargins(0, 0, 0, 0);
 	layout->setSpacing(0);
 
 	{
 		/* Toolbar */
-		QToolBar* toolbar = new QToolBar(this);
+		QToolBar *toolbar = new QToolBar(this);
 		toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
 		toolbar->setIconSize(QSize(16, 16));
 		toolbar->setMovable(false);
@@ -380,17 +409,21 @@
 			                   [this] { DownloadSelection(); });
 		}
 
-		{ toolbar->addAction(QIcon(":/icons/16x16/cross-button.png"), tr("&Discard all")); }
+		{
+			toolbar->addAction(QIcon(":/icons/16x16/cross-button.png"), tr("&Discard all"));
+		}
 
 		toolbar->addSeparator();
 
-		{ toolbar->addAction(QIcon(":/icons/16x16/gear.png"), tr("&Settings")); }
+		{
+			toolbar->addAction(QIcon(":/icons/16x16/gear.png"), tr("&Settings"));
+		}
 
 		layout->addWidget(toolbar);
 	}
 
 	{
-		QFrame* line = new QFrame(this);
+		QFrame *line = new QFrame(this);
 		line->setFrameShape(QFrame::HLine);
 		line->setFrameShadow(QFrame::Sunken);
 		line->setLineWidth(1);
@@ -437,7 +470,8 @@
 	}
 }
 
-void TorrentsPage::DownloadSelection() {
+void TorrentsPage::DownloadSelection()
+{
 	if (!model)
 		return;
 
@@ -446,13 +480,14 @@
 	model->DownloadTorrents(selection);
 }
 
-void TorrentsPage::Refresh() {
+void TorrentsPage::Refresh()
+{
 	if (!model)
 		return;
 
-	HTTP::RequestThread* thread = new HTTP::RequestThread(session.config.torrents.feed_link);
+	HTTP::RequestThread *thread = new HTTP::RequestThread(session.config.torrents.feed_link);
 
-	connect(thread, &HTTP::RequestThread::ReceivedData, this, [&](const QByteArray& ba) {
+	connect(thread, &HTTP::RequestThread::ReceivedData, this, [&](const QByteArray &ba) {
 		/* This is to make sure we aren't in a different thread
 		 * messing around with GUI stuff
 		 */