Commit 8f75bc24 authored by Paweł Wegner's avatar Paweł Wegner
Browse files

Replace std::vector<IItem::Pointer> with IItem::List.

parent b686c620
......@@ -140,7 +140,7 @@ void CloudContext::loadCachedDirectories() {
QJsonObject json = QJsonDocument::fromBinaryData(file.readAll()).object();
for (auto directory : json["directory"].toArray()) {
auto json = directory.toObject();
std::vector<IItem::Pointer> items;
IItem::List items;
for (auto item : json["list"].toArray()) {
try {
items.push_back(IItem::fromString(item.toString().toStdString()));
......@@ -327,7 +327,7 @@ void CloudContext::add(std::shared_ptr<ICloudProvider> p,
}
void CloudContext::cacheDirectory(ListDirectoryCacheKey directory,
const std::vector<IItem::Pointer>& lst) {
const IItem::List& lst) {
{
std::lock_guard<std::mutex> lock(mutex_);
std::vector<std::string> data;
......@@ -339,8 +339,7 @@ void CloudContext::cacheDirectory(ListDirectoryCacheKey directory,
});
}
std::vector<IItem::Pointer> CloudContext::cachedDirectory(
ListDirectoryCacheKey key) {
IItem::List CloudContext::cachedDirectory(ListDirectoryCacheKey key) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = list_directory_cache_.find(key);
if (it == std::end(list_directory_cache_))
......
......@@ -86,8 +86,7 @@ class CloudContext : public QObject {
void cacheDirectory(ListDirectoryCacheKey directory,
const std::vector<cloudstorage::IItem::Pointer>&);
std::vector<cloudstorage::IItem::Pointer> cachedDirectory(
ListDirectoryCacheKey);
cloudstorage::IItem::List cachedDirectory(ListDirectoryCacheKey);
void schedule(std::function<void()>);
signals:
......
......@@ -15,7 +15,7 @@ class ListDirectory : public IListDirectoryCallback {
notifier_->addedItem(item);
}
void done(EitherError<std::vector<IItem::Pointer>> r) override {
void done(EitherError<IItem::List> r) override {
emit notifier_->finishedList(r);
notifier_->deleteLater();
}
......@@ -62,7 +62,7 @@ void ListDirectoryModel::remove(int idx) {
endRemoveRows();
}
void ListDirectoryModel::match(const std::vector<IItem::Pointer>& lst) {
void ListDirectoryModel::match(const IItem::List& lst) {
std::unordered_set<std::string> id;
for (size_t i = lst.size(); i-- > 0;) {
auto idx = find(lst[i]);
......@@ -120,14 +120,14 @@ void ListDirectoryRequest::update(CloudContext* context, CloudItem* item) {
description);
});
connect(this, &ListDirectoryRequest::finished, context,
[=](ListDirectoryCacheKey key, const std::vector<IItem::Pointer>& r) {
[=](ListDirectoryCacheKey key, const IItem::List& r) {
context->cacheDirectory(key, r);
});
auto object = new RequestNotifier;
auto provider = item->provider().variant();
connect(object, &RequestNotifier::finishedList, this,
[=](EitherError<std::vector<IItem::Pointer>> r) {
[=](EitherError<IItem::List> r) {
set_done(true);
if (r.left())
return errorOccurred(provider, r.left()->code_,
......
......@@ -75,7 +75,7 @@ int main(int, char**) {
std::string command_line;
ICloudProvider::Pointer current_provider;
IItem::Pointer current_directory;
std::vector<IItem::Pointer> directory_stack;
IItem::List directory_stack;
std::string prompt = "/";
while (std::cout << prompt << ": " && std::getline(std::cin, command_line)) {
std::stringstream line(command_line);
......
......@@ -367,7 +367,7 @@ IHttpRequest::Pointer AmazonS3::downloadFileRequest(const IItem& item,
return http()->create(endpoint() + "/" + escapePath(item.id()), "GET");
}
std::vector<IItem::Pointer> AmazonS3::listDirectoryResponse(
IItem::List AmazonS3::listDirectoryResponse(
const IItem& parent, std::istream& stream,
std::string& next_page_token) const {
std::stringstream sstream;
......@@ -376,7 +376,7 @@ std::vector<IItem::Pointer> AmazonS3::listDirectoryResponse(
if (document.Parse(sstream.str().c_str(), sstream.str().size()) !=
tinyxml2::XML_SUCCESS)
throw std::logic_error("invalid xml");
std::vector<IItem::Pointer> result;
IItem::List result;
if (auto name_element = document.RootElement()->FirstChildElement("Name")) {
std::string bucket = name_element->GetText();
for (auto child = document.RootElement()->FirstChildElement("Contents");
......
......@@ -74,7 +74,7 @@ class AmazonS3 : public CloudProvider {
IHttpRequest::Pointer downloadFileRequest(
const IItem&, std::ostream& input_stream) const override;
std::vector<IItem::Pointer> listDirectoryResponse(
IItem::List listDirectoryResponse(
const IItem&, std::istream&, std::string& next_page_token) const override;
IItem::Pointer createDirectoryResponse(const IItem& parent,
const std::string& name,
......
......@@ -200,10 +200,10 @@ IItem::Pointer Box::getItemDataResponse(std::istream& stream) const {
return toItem(util::json::from_stream(stream));
}
std::vector<IItem::Pointer> Box::listDirectoryResponse(
const IItem&, std::istream& stream, std::string& next_page_token) const {
IItem::List Box::listDirectoryResponse(const IItem&, std::istream& stream,
std::string& next_page_token) const {
auto response = util::json::from_stream(stream);
std::vector<IItem::Pointer> result;
IItem::List result;
for (const Json::Value& v : response["entries"]) result.push_back(toItem(v));
int offset = response["offset"].asInt();
int limit = response["limit"].asInt();
......
......@@ -66,7 +66,7 @@ class Box : public CloudProvider {
IHttpRequest::Pointer getGeneralDataRequest(std::ostream&) const override;
IItem::Pointer getItemDataResponse(std::istream& response) const override;
std::vector<IItem::Pointer> listDirectoryResponse(
IItem::List listDirectoryResponse(
const IItem&, std::istream&, std::string& next_page_token) const override;
std::string getItemUrlResponse(const IItem& item,
const IHttpRequest::HeaderParameters&,
......
......@@ -619,8 +619,8 @@ std::string CloudProvider::getItemUrlResponse(
return std::static_pointer_cast<Item>(getItemDataResponse(stream))->url();
}
std::vector<IItem::Pointer> CloudProvider::listDirectoryResponse(
const IItem&, std::istream&, std::string&) const {
IItem::List CloudProvider::listDirectoryResponse(const IItem&, std::istream&,
std::string&) const {
return {};
}
......
......@@ -231,9 +231,9 @@ class CloudProvider : public ICloudProvider,
*
* @return item set
*/
virtual std::vector<IItem::Pointer> listDirectoryResponse(
const IItem& directory, std::istream& response,
std::string& next_page_token) const;
virtual IItem::List listDirectoryResponse(const IItem& directory,
std::istream& response,
std::string& next_page_token) const;
virtual IItem::Pointer renameItemResponse(const IItem& old_item,
const std::string& name,
......
......@@ -291,10 +291,10 @@ IHttpRequest::Pointer Dropbox::renameItemRequest(const IItem& item,
return request;
}
std::vector<IItem::Pointer> Dropbox::listDirectoryResponse(
const IItem&, std::istream& stream, std::string& next_page_token) const {
IItem::List Dropbox::listDirectoryResponse(const IItem&, std::istream& stream,
std::string& next_page_token) const {
auto response = util::json::from_stream(stream);
std::vector<IItem::Pointer> result;
IItem::List result;
for (const Json::Value& v : response["entries"]) result.push_back(toItem(v));
if (response["has_more"].asBool()) {
next_page_token = response["cursor"].asString();
......
......@@ -66,7 +66,7 @@ class Dropbox : public CloudProvider {
const std::string& name,
std::ostream&) const override;
std::vector<IItem::Pointer> listDirectoryResponse(
IItem::List listDirectoryResponse(
const IItem&, std::istream&, std::string& next_page_token) const override;
std::string getItemUrlResponse(const IItem& item,
const IHttpRequest::HeaderParameters&,
......
......@@ -293,11 +293,11 @@ IItem::Pointer GoogleDrive::getItemDataResponse(std::istream& response) const {
return toItem(util::json::from_stream(response));
}
std::vector<IItem::Pointer> GoogleDrive::listDirectoryResponse(
IItem::List GoogleDrive::listDirectoryResponse(
const IItem& item, std::istream& stream,
std::string& next_page_token) const {
auto response = util::json::from_stream(stream);
std::vector<IItem::Pointer> result;
IItem::List result;
for (Json::Value v : response["files"]) result.push_back(toItem(v));
if (item.id() == rootDirectory()->id())
result.push_back(util::make_unique<Item>(
......
......@@ -71,7 +71,7 @@ class GoogleDrive : public CloudProvider {
std::string getItemUrlResponse(const IItem& item,
const IHttpRequest::HeaderParameters&,
std::istream& response) const override;
std::vector<IItem::Pointer> listDirectoryResponse(
IItem::List listDirectoryResponse(
const IItem&, std::istream&, std::string& next_page_token) const override;
GeneralData getGeneralDataResponse(std::istream& response) const override;
......
......@@ -153,15 +153,16 @@ IHttpRequest::Pointer GooglePhotos::uploadFileRequest(
return request;
}
std::vector<IItem::Pointer> GooglePhotos::listDirectoryResponse(
const IItem &, std::istream &stream, std::string &) const {
IItem::List GooglePhotos::listDirectoryResponse(const IItem &,
std::istream &stream,
std::string &) const {
std::stringstream sstream;
sstream << stream.rdbuf();
tinyxml2::XMLDocument document;
if (document.Parse(sstream.str().c_str(), sstream.str().size()) !=
tinyxml2::XML_SUCCESS)
throw std::logic_error("invalid xml");
std::vector<IItem::Pointer> result;
IItem::List result;
for (auto child = document.RootElement()->FirstChildElement("entry"); child;
child = child->NextSiblingElement("entry")) {
result.push_back(toItem(child));
......
......@@ -47,7 +47,7 @@ class GooglePhotos : public CloudProvider {
IHttpRequest::Pointer uploadFileRequest(
const IItem& directory, const std::string& filename,
std::ostream& prefix_stream, std::ostream& suffix_stream) const override;
std::vector<IItem::Pointer> listDirectoryResponse(
IItem::List listDirectoryResponse(
const IItem&, std::istream&, std::string& next_page_token) const override;
DownloadFileRequest::Pointer downloadFileAsync(IItem::Pointer,
IDownloadFileCallback::Pointer,
......
......@@ -317,10 +317,10 @@ IHttpRequest::Pointer HubiC::getItemUrlRequest(const IItem &item,
return r;
}
std::vector<IItem::Pointer> HubiC::listDirectoryResponse(
IItem::List HubiC::listDirectoryResponse(
const IItem &, std::istream &stream, std::string &next_page_token) const {
auto json = util::json::from_stream(stream);
std::vector<IItem::Pointer> result;
IItem::List result;
for (auto &&v : json)
if (!v.isMember("subdir")) {
result.push_back(toItem(v));
......
......@@ -75,7 +75,7 @@ class HubiC : public CloudProvider {
std::string getItemUrlResponse(const IItem& item,
const IHttpRequest::HeaderParameters&,
std::istream& response) const override;
std::vector<IItem::Pointer> listDirectoryResponse(
IItem::List listDirectoryResponse(
const IItem&, std::istream&, std::string& next_page_token) const override;
IItem::Pointer toItem(const Json::Value&) const;
......
......@@ -63,11 +63,11 @@ fs::path from_string(const std::string &string) {
return fs::path(string, std::codecvt_utf8<wchar_t>());
}
void list_directory(
const LocalDrive &p, IItem::Pointer item,
std::function<void(EitherError<std::vector<IItem::Pointer>>)> done,
std::function<void(IItem::Pointer)> received_item = [](IItem::Pointer) {}) {
std::vector<IItem::Pointer> vector;
void list_directory(const LocalDrive &p, IItem::Pointer item,
std::function<void(EitherError<IItem::List>)> done,
std::function<void(IItem::Pointer)> received_item =
[](IItem::Pointer) {}) {
IItem::List result;
error_code ec;
auto directory = fs::directory_iterator(p.path(item), ec);
if (ec) return done(Error{ec.value(), ec.message()});
......@@ -84,10 +84,10 @@ void list_directory(
to_string(e.path().filename()), to_string(e.path()), size, timestamp,
is_directory ? IItem::FileType::Directory : IItem::FileType::Unknown);
received_item(item);
vector.push_back(item);
result.push_back(item);
}
}
done(vector);
done(result);
}
} // namespace
......@@ -118,7 +118,7 @@ AuthorizeRequest::Pointer LocalDrive::authorizeAsync() {
LocalDrive::ListDirectoryRequest::Pointer LocalDrive::listDirectoryAsync(
IItem::Pointer item, IListDirectoryCallback::Pointer callback) {
using ItemList = EitherError<std::vector<IItem::Pointer>>;
using ItemList = EitherError<IItem::List>;
return request<ItemList>(
[=](ItemList e) { callback->done(e); },
[=](Request<ItemList>::Pointer r) {
......@@ -133,13 +133,12 @@ LocalDrive::listDirectoryPageAsync(IItem::Pointer item, const std::string &,
return request<EitherError<PageData>>(
[=](EitherError<PageData> e) { callback(e); },
[=](Request<EitherError<PageData>>::Pointer r) {
list_directory(*this, item,
[=](EitherError<std::vector<IItem::Pointer>> e) {
if (e.left())
r->done(e.left());
else
r->done(PageData{*e.right(), ""});
});
list_directory(*this, item, [=](EitherError<IItem::List> e) {
if (e.left())
r->done(e.left());
else
r->done(PageData{*e.right(), ""});
});
});
}
......
......@@ -563,13 +563,13 @@ ICloudProvider::GetItemDataRequest::Pointer MegaNz::getItemDataAsync(
ICloudProvider::ListDirectoryRequest::Pointer MegaNz::listDirectoryAsync(
IItem::Pointer item, IListDirectoryCallback::Pointer cb) {
using ItemList = EitherError<std::vector<IItem::Pointer>>;
using ItemList = EitherError<IItem::List>;
auto callback = cb.get();
auto resolver = [=](Request<ItemList>::Pointer r) {
ensureAuthorized<ItemList>(r, [=] {
auto node = this->node(item->id());
if (node) {
std::vector<IItem::Pointer> result;
IItem::List result;
std::unique_ptr<mega::MegaNodeList> lst(mega_->getChildren(node.get()));
if (lst) {
for (int i = 0; i < lst->size(); i++) {
......@@ -791,7 +791,7 @@ MegaNz::listDirectoryPageAsync(IItem::Pointer item, const std::string&,
ensureAuthorized<EitherError<PageData>>(r, [=] {
auto node = this->node(item->id());
if (node) {
std::vector<IItem::Pointer> result;
IItem::List result;
std::unique_ptr<mega::MegaNodeList> lst(mega_->getChildren(node.get()));
if (lst) {
for (int i = 0; i < lst->size(); i++) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment