Ejemplo n.º 1
0
	void Plugin::RequestRecentReleases (int num, bool withRecs)
	{
		auto nam = Proxy_->GetNetworkAccessManager ();
		connect (new RecentReleasesFetcher (withRecs, num, nam, this),
				SIGNAL (gotRecentReleases (QList<Media::AlbumRelease>)),
				this,
				SIGNAL (gotRecentReleases (QList<Media::AlbumRelease>)));
	}
	void RecentReleasesFetcher::handleReplyFinished ()
	{
		auto reply = qobject_cast<QNetworkReply*> (sender ());
		reply->deleteLater ();

		const auto& data = reply->readAll ();
		qDebug () << data;
		QDomDocument doc;
		if (!doc.setContent (data))
		{
			qWarning () << Q_FUNC_INFO
					<< "error parsing reply";
			return;
		}

		const auto& docElem = doc.documentElement ();
		if (docElem.attribute ("status") != "ok")
		{
			qWarning () << Q_FUNC_INFO
					<< "reply is not ok:"
					<< docElem.attribute ("status");
			return;
		}

		QList<Media::AlbumRelease> releases;

		static auto months = { "Jan", "Feb", "Mar",
				"Apr", "May", "Jun",
				"Jul", "Aug", "Sep",
				"Oct", "Nov", "Dec" };
		const auto monthsBegin = months.begin ();
		const auto monthsEnd = months.end ();
		auto album = docElem.firstChildElement ("albums").firstChildElement ("album");
		while (!album.isNull ())
		{
			const auto& strs = album.attribute ("releasedate").split (' ', QString::SkipEmptyParts);
			const int day = strs.value (1).toInt ();
			const int month = std::distance (monthsBegin,
						std::find (monthsBegin, monthsEnd, strs.value (2))) + 1;
			const int year = strs.value (3).toInt ();

			const QUrl& thumb = GetImage (album, "large");
			const QUrl& full = GetImage (album, "extralarge");

			Media::AlbumRelease release =
			{
				album.firstChildElement ("name").text (),
				album.firstChildElement ("artist").firstChildElement ("name").text (),
				QDateTime (QDate (year, month, day)),
				thumb,
				full,
				QUrl (album.firstChildElement ("url").text ())
			};
			releases << release;

			album = album.nextSiblingElement ("album");
		}

		emit gotRecentReleases (releases);
	}