Mercurial > minori
diff src/gui/pages/torrents.cc @ 367:8d45d892be88 default tip
*: instead of pugixml, use Qt XML features
this means we have one extra Qt dependency though...
author | Paper <paper@tflc.us> |
---|---|
date | Sun, 17 Nov 2024 22:55:47 -0500 |
parents | a0aa8c8c4307 |
children |
line wrap: on
line diff
--- a/src/gui/pages/torrents.cc Sun Nov 17 19:56:01 2024 -0500 +++ b/src/gui/pages/torrents.cc Sun Nov 17 22:55:47 2024 -0500 @@ -16,13 +16,14 @@ #include <QVBoxLayout> #include <QtGlobal> +#include <QDomDocument> + #include <algorithm> #include <fstream> #include <iostream> #include <sstream> #include "anitomy/anitomy.h" -#include "pugixml.hpp" /* This file is very, very similar to the anime list page. * @@ -126,11 +127,19 @@ } void TorrentsPageListModel::ParseTorrentList(const QByteArray& ba) { - std::istringstream stdstream(Strings::ToUtf8String(ba)); + QDomDocument doc; + QDomNode node; + QDomNodeList node_nodes; + { + QString err; + int err_ln; + int err_col; - pugi::xml_document doc; - if (!doc.load(stdstream)) - return; // peace out + 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)))); + return; // peace out + } + } /* my extra special dumb hack. */ if (!rowCount(index(0))) { @@ -141,10 +150,36 @@ beginResetModel(); list.clear(); - /* this is just an rss parser; it should be in a separate class... */ - for (pugi::xml_node item : doc.child("rss").child("channel").children("item")) { + + node = doc; + + for (const auto& n : {"rss", "channel"}) { + node = node.namedItem(n); + if (node.isNull()) { std::cout << n << std::endl; goto end; } + } + + if (!node.hasChildNodes()) { std::cout << "no child nodes" << std::endl; goto end; } + + node_nodes = node.childNodes(); + + for (int c = 0; c < node_nodes.count(); c++) { + const QDomNode item = node_nodes.at(c); + if (!item.isElement() || item.nodeName() != "item") + continue; + + const QDomNode title = item.namedItem("title"); + if (!title.isElement()) continue; + const QDomNode description = item.namedItem("description"); + if (!description.isElement()) continue; + const QDomNode link = item.namedItem("link"); + if (!link.isElement()) continue; + const QDomNode guid = item.namedItem("guid"); + if (!guid.isElement()) continue; + const QDomNode pubDate = item.namedItem("pubDate"); + if (!pubDate.isElement()) continue; + TorrentModelItem torrent; - torrent.SetFilename(item.child_value("title")); /* "title" == filename */ + torrent.SetFilename(Strings::ToUtf8String(title.toElement().text())); /* "title" == filename */ { anitomy::Anitomy anitomy; anitomy.Parse(torrent.GetFilename()); @@ -160,19 +195,17 @@ torrent.SetResolution(Strings::ToUtf8String(elements.get(anitomy::kElementVideoResolution))); } - std::string description = item.child_value("description"); - Strings::TextifySynopsis(description); - ParseFeedDescription(description, torrent); + std::string description_s = Strings::ToUtf8String(description.toElement().text()); + Strings::TextifySynopsis(description_s); + ParseFeedDescription(description_s, torrent); - torrent.SetLink(item.child_value("link")); - torrent.SetGuid(item.child_value("guid")); - { - const QString date_str = Strings::ToQString(item.child_value("pubDate")); - torrent.SetDate(QDateTime::fromString(date_str, "ddd, dd MMM yyyy HH:mm:ss t")); - } + torrent.SetLink(Strings::ToUtf8String(link.toElement().text())); + torrent.SetGuid(Strings::ToUtf8String(guid.toElement().text())); + torrent.SetDate(QDateTime::fromString(pubDate.toElement().text(), "ddd, dd MMM yyyy HH:mm:ss t")); list.push_back(torrent); } +end: endResetModel(); }