void FotoBilderAccount::GetPicsRequest (const QString& challenge)
	{
		auto reply = Proxy_->GetNetworkAccessManager ()->
				get (CreateRequest (Util::MakeMap<QByteArray, QByteArray> ({
						{ "X-FB-User", Login_.toUtf8 () },
						{ "X-FB-Mode", "GetPics" },
						{ "X-FB-Auth", ("crp:" + challenge + ":" +
								GetHashedChallenge (GetPassword (), challenge))
									.toUtf8 () } })));
		connect (reply,
				SIGNAL (finished ()),
				this,
				SLOT (handleGotPhotos ()));
		connect (reply,
				SIGNAL (error (QNetworkReply::NetworkError)),
				this,
				SLOT (handleNetworkError (QNetworkReply::NetworkError)));
	}
Exemple #2
0
	void VkAccount::handleGotPhotos ()
	{
		auto reply = qobject_cast<QNetworkReply*> (sender ());
		reply->deleteLater ();

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

		bool finishReached = false;

		auto photoElem = doc
				.documentElement ()
				.firstChildElement ("photo");
		while (!photoElem.isNull ())
		{
			auto mkItem = [&photoElem] () -> QStandardItem*
			{
				const auto& idText = photoElem.firstChildElement ("pid").text ();

				auto item = new QStandardItem (idText);
				item->setData (ItemType::Image, CollectionRole::Type);
				item->setData (idText, CollectionRole::ID);
				item->setData (QString (), CollectionRole::Name);

				const auto& sizesElem = photoElem.firstChildElement ("sizes");
				auto getType = [&sizesElem] (const QString& type) -> QPair<QUrl, QSize>
				{
					auto sizeElem = sizesElem.firstChildElement ("size");
					while (!sizeElem.isNull ())
					{
						if (sizeElem.firstChildElement ("type").text () != type)
						{
							sizeElem = sizeElem.nextSiblingElement ("size");
							continue;
						}

						const auto& src = sizeElem.firstChildElement ("src").text ();
						const auto width = sizeElem.firstChildElement ("width").text ().toInt ();
						const auto height = sizeElem.firstChildElement ("height").text ().toInt ();

						return { src, { width, height } };
					}

					return {};
				};

				const auto& small = getType ("m");
				const auto& mid = getType ("x");
				auto orig = getType ("w");
				QStringList sizeCandidates { "z", "y", "x", "r" };
				while (orig.second.width () <= 0)
				{
					if (sizeCandidates.isEmpty ())
						return nullptr;

					orig = getType (sizeCandidates.takeFirst ());
				}

				item->setData (small.first, CollectionRole::SmallThumb);
				item->setData (small.second, CollectionRole::SmallThumbSize);

				item->setData (mid.first, CollectionRole::MediumThumb);
				item->setData (mid.second, CollectionRole::MediumThumbSize);

				item->setData (orig.first, CollectionRole::Original);
				item->setData (orig.second, CollectionRole::OriginalSize);

				return item;
			};

			auto allItem = mkItem ();
			if (!allItem)
			{
				finishReached = true;
				break;
			}

			AllPhotosItem_->appendRow (allItem);

			const auto aid = photoElem.firstChildElement ("aid").text ().toInt ();
			if (Albums_.contains (aid))
				Albums_ [aid]->appendRow (mkItem ());

			photoElem = photoElem.nextSiblingElement ("photo");
		}

		if (finishReached)
			return;

		const auto count = doc.documentElement ().firstChildElement ("count").text ().toInt ();
		if (count == AllPhotosItem_->rowCount ())
			return;

		const auto offset = AllPhotosItem_->rowCount ();

		CallQueue_.append ([this, offset] (const QString& authKey) -> void
			{
				QUrl photosUrl ("https://api.vk.com/method/photos.getAll.xml");
				photosUrl.addQueryItem ("access_token", authKey);
				photosUrl.addQueryItem ("count", "100");
				photosUrl.addQueryItem ("offset", QString::number (offset));
				photosUrl.addQueryItem ("photo_sizes", "1");
				RequestQueue_->Schedule ([this, photosUrl]
					{
						connect (Proxy_->GetNetworkAccessManager ()->get (QNetworkRequest (photosUrl)),
								SIGNAL (finished ()),
								this,
								SLOT (handleGotPhotos ()));
					}, this);
			});

		AuthMgr_->GetAuthKey ();

	}