Exemple #1
0
	void DriveManager::handleCreateDirectory ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		const auto& res = QJson::Parser ().parse (reply->readAll (), &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		if (!res.toMap ().contains ("error"))
		{
			qDebug () << Q_FUNC_INFO
					<< "directory created successfully";

			emit gotNewItem (CreateDriveItem (res));
			return;
		}

		ParseError (res.toMap ());
	}
Exemple #2
0
	void DriveManager::handleItemRenamed ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		const auto& res = QJson::Parser ().parse (reply->readAll (), &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		const QVariantMap& map = res.toMap ();

		if (!map.contains ("error"))
		{
			DriveItem it = CreateDriveItem (res);
			qDebug () << Q_FUNC_INFO
					<< "entry renamed successfully";
			emit gotNewItem (it);

			return;
		}

		ParseError (map);
	}
Exemple #3
0
	void DriveManager::handleUploadFinished ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		const auto& res = QJson::Parser ().parse (reply->readAll (), &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		const auto& map = res.toMap ();
		const auto& id = map ["id"].toString ();

		if (!map.contains ("error"))
		{
			qDebug () << Q_FUNC_INFO
					<< "file uploaded successfully";
			RefreshListing ();
			emit gotNewItem (CreateDriveItem (res));
			emit finished (id, Reply2FilePath_.take (reply));
			return;
		}

		 ParseError (map);
	}
Exemple #4
0
	void DriveManager::handleGetFileInfo ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		const auto& res = QJson::Parser ().parse (reply->readAll (), &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		const QVariantMap& map = res.toMap ();
		QString access_token = Reply2DownloadAccessToken_.take (reply);

		if (!map.contains ("error"))
		{
			DriveItem it = CreateDriveItem (res);
			if (!access_token.isEmpty ())
				it.DownloadUrl_.addQueryItem ("access_token", access_token);

			if (!DownloadsQueue_.isEmpty ())
				DownloadsQueue_.dequeue () (it.DownloadUrl_);
			return;
		}

		ParseError (map);
	}
	void DriveManager::handleItemRenamed ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		const auto& res = QJson::Parser ().parse (reply->readAll (), &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		const QVariantMap& map = res.toMap ();

		if (!map.contains ("error"))
		{
			DriveItem it = CreateDriveItem (res);
			qDebug () << Q_FUNC_INFO
					<< "entry renamed successfully";
			RequestFileChanges (XmlSettingsManager::Instance ().Property ("largestChangeId", 0)
					.toLongLong ());

			return;
		}

		ParseError (map);
	}
	void DriveManager::handleGetFileChanges ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		auto ba = reply->readAll ();
		const auto& res = QJson::Parser ().parse (ba, &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		const QVariantMap& map = res.toMap ();
		if (map.contains ("error"))
		{
			ParseError (map);
			return;
		}

		QList<DriveChanges> changes;

		if (!map.contains ("items") ||
				map ["items"].toList ().isEmpty ())
			return;

		const QString nextPageTokent = map ["nextPageToken"].toString ();
		qlonglong largestId = map ["largestChangeId"].toLongLong ();
		XmlSettingsManager::Instance ().setProperty ("largestChangeId",
				largestId);
		for (auto itemVar : map ["items"].toList ())
		{
			QVariantMap itemMap = itemVar.toMap ();
			DriveChanges change;
			change.FileId_ = itemMap ["fileId"].toString ();
			change.Id_ = itemMap ["id"].toString ();
			change.Deleted_ = itemMap ["deleted"].toBool ();
			if (!change.Deleted_)
				change.FileResource_ =CreateDriveItem (itemMap ["file"]);

			changes << change;
		}

		emit gotChanges (changes);

		if (!nextPageTokent.isEmpty ())
			RequestFileChanges (largestId, nextPageTokent);
	}
	void DriveManager::handleGotFiles ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		const auto& res = QJson::Parser ().parse (reply->readAll (), &ok);

		if (!ok)
		{
			qDebug () << Q_FUNC_INFO << "parse error";
			return;
		}

		const auto& resMap = res.toMap ();
		if (!resMap.contains ("items"))
		{
			qDebug () << Q_FUNC_INFO << "there are no items";
			if (SecondRequestIfNoItems_)
			{
				SecondRequestIfNoItems_ = false;
				RefreshListing ();
			}
			return;
		}

		if (resMap.contains ("error"))
		{
			ParseError (res.toMap ());
			return;
		}

		SecondRequestIfNoItems_ = true;
		QList<DriveItem> resList;
		Q_FOREACH (const auto& item, resMap ["items"].toList ())
		{
			const auto& driveItem = CreateDriveItem (item);
			if (driveItem.Name_.isEmpty ())
				continue;
			resList << driveItem;
		}

		emit gotFiles (resList);
	}
Exemple #8
0
	void DriveManager::handleGetFileChanges ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		auto ba = reply->readAll ();
		const auto& res = QJson::Parser ().parse (ba, &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		const QVariantMap& map = res.toMap ();
		if (map.contains ("error"))
		{
			ParseError (map);
			return;
		}

		QList<DriveChanges> changes;

		if (!map.contains ("items") ||
				map ["items"].toList ().isEmpty ())
			return;

		for (auto itemVar : map ["items"].toList ())
		{
			QVariantMap itemMap = itemVar.toMap ();
			DriveItem driveItem = CreateDriveItem (itemMap ["file"]);
			DriveChanges change;
			change.FileId_ = itemMap ["fileId"].toString ();
			change.Id_ = itemMap ["id"].toString ();
			change.Deleted_ = itemMap ["deleted"].toBool ();
			change.FileResource_ = driveItem;

			changes << change;
		}

		gotChanges (changes, map ["largestChangeId"].toLongLong () + 1);
	}
	void DriveManager::handleGetFileInfo ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());
		if (!reply)
			return;

		reply->deleteLater ();

		bool ok = false;
		const auto& res = QJson::Parser ().parse (reply->readAll (), &ok);
		if (!ok)
		{
			qDebug () << Q_FUNC_INFO
					<< "parse error";
			return;
		}

		const QVariantMap& map = res.toMap ();
		QString access_token = Reply2DownloadAccessToken_.take (reply);

		if (!map.contains ("error"))
		{
			DriveItem it = CreateDriveItem (res);
			if (it.DownloadUrl_.isEmpty ())
			{
				QMessageBox::warning (Core::Instance ().GetProxy ()->GetRootWindowsManager ()->GetPreferredWindow (),
						"LeechCraft",
						tr ("This file cannot be downloaded. Use export instead of Download or Open File action"));
				DownloadsQueue_.removeFirst ();
				return;
			}

			if (!access_token.isEmpty ())
				it.DownloadUrl_.addQueryItem ("access_token", access_token);

			if (!DownloadsQueue_.isEmpty ())
				DownloadsQueue_.dequeue () (it.DownloadUrl_);
			return;
		}

		ParseError (map);
	}