From f2b34d08b40dd00b754e0853d0d6b7cd525a707d Mon Sep 17 00:00:00 2001 From: JuanJakobo <34421964+JuanJakobo@users.noreply.github.com> Date: Tue, 10 Nov 2020 10:27:00 +0100 Subject: [PATCH] Feature: also shown local files --- src/handler/eventHandler.cpp | 2 +- src/ui/listViewEntry.cpp | 33 +++++++++----- src/util/item.cpp | 6 +-- src/util/item.h | 17 +++++--- src/util/nextcloud.cpp | 85 ++++++++++++++++++++---------------- src/util/nextcloud.h | 2 + 6 files changed, 86 insertions(+), 59 deletions(-) diff --git a/src/handler/eventHandler.cpp b/src/handler/eventHandler.cpp index d3d02ab..e1625b4 100644 --- a/src/handler/eventHandler.cpp +++ b/src/handler/eventHandler.cpp @@ -151,7 +151,7 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) else { int dialogResult = 2; - if (_nextcloud.getItems()->at(itemID).isDownloaded()) + if (_nextcloud.getItems()->at(itemID).getState() != FileState::ICLOUD) { dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Open", "Sync", "Remove"); } diff --git a/src/ui/listViewEntry.cpp b/src/ui/listViewEntry.cpp index 7829e3a..0db3848 100644 --- a/src/ui/listViewEntry.cpp +++ b/src/ui/listViewEntry.cpp @@ -23,20 +23,29 @@ void ListViewEntry::draw(const Item &item) DrawTextRect(_position.x, _position.y, _position.w, _fontHeight, item.getTitle().c_str(), ALIGN_LEFT); SetFont(_entryFont.get(), BLACK); - if (item.getType() == IFILE) + + if (item.getState() == FileState::ILOCAL) { - DrawTextRect(_position.x, _position.y + _fontHeight, _position.w, _fontHeight, item.getFiletype().c_str(), ALIGN_LEFT); - if (item.isDownloaded()) - { - DrawTextRect(_position.x, _position.y + 3 * _fontHeight, _position.w, _fontHeight, "Synced", ALIGN_RIGHT); - } - else - { - DrawTextRect(_position.x, _position.y + 3 * _fontHeight, _position.w, _fontHeight, "Click to Download", ALIGN_RIGHT); - } + DrawTextRect(_position.x, _position.y + 3 * _fontHeight, _position.w, _fontHeight, "Local", ALIGN_RIGHT); + } + else + { + if (item.getType() == IFILE) + { + DrawTextRect(_position.x, _position.y + _fontHeight, _position.w, _fontHeight, item.getFiletype().c_str(), ALIGN_LEFT); + + if (item.getState() == FileState::ISYNCED) + { + DrawTextRect(_position.x, _position.y + 3 * _fontHeight, _position.w, _fontHeight, "Synced", ALIGN_RIGHT); + } + else + { + DrawTextRect(_position.x, _position.y + 3 * _fontHeight, _position.w, _fontHeight, "Click to Download", ALIGN_RIGHT); + } + } + DrawTextRect(_position.x, _position.y + 2 * _fontHeight, _position.w, _fontHeight, item.getLastEditDate().c_str(), ALIGN_LEFT); + DrawTextRect(_position.x, _position.y + 3 * _fontHeight, _position.w, _fontHeight, item.getSize().c_str(), ALIGN_LEFT); } - DrawTextRect(_position.x, _position.y + 2 * _fontHeight, _position.w, _fontHeight, item.getLastEditDate().c_str(), ALIGN_LEFT); - DrawTextRect(_position.x, _position.y + 3 * _fontHeight, _position.w, _fontHeight, item.getSize().c_str(), ALIGN_LEFT); int line = (_position.y + _position.h) - 1; DrawLine(0, line, ScreenWidth(), line, BLACK); diff --git a/src/util/item.cpp b/src/util/item.cpp index e727791..4c6aac5 100644 --- a/src/util/item.cpp +++ b/src/util/item.cpp @@ -45,11 +45,11 @@ Item::Item(const string &xmlItem) if (iv_access(_localPath.c_str(), W_OK) != 0) { - _downloaded = false; + _state = FileState::ICLOUD; } else { - _downloaded = true; + _state = FileState::ISYNCED; } } @@ -57,7 +57,7 @@ Item::Item(const string &xmlItem) Util::decodeUrl(_title); } -Item::Item(const string &localPath, bool downloaded) : _localPath(localPath), _downloaded(downloaded) +Item::Item(const string &localPath, FileState state) : _localPath(localPath), _state(state) { _title = _localPath; _title = _title.substr(_title.find_last_of("/") + 1, _title.length()); diff --git a/src/util/item.h b/src/util/item.h index 4d73857..67de9d0 100644 --- a/src/util/item.h +++ b/src/util/item.h @@ -21,12 +21,19 @@ enum Itemtype IFOLDER }; +enum FileState +{ + ICLOUD, + ISYNCED, + ILOCAL +}; + class Item { public: Item(const string &xmlItem); - Item(const string &localPath, bool downloaded); + Item(const string &localPath, FileState state); void setPath(const string &path) { _path = path; }; string getPath() const { return _path; }; @@ -38,11 +45,11 @@ public: void setTitle(const string &title) { _title = title; }; string getTitle() const { return _title; }; - void setDownloaded(bool downloaded) { _downloaded = downloaded; }; - bool isDownloaded() const { return _downloaded; }; + void setState(FileState state) { _state = state; }; + FileState getState() const { return _state; }; string getLastEditDate() const { return _lastEditDate; }; - void setLastEditDate(const string &date){ _lastEditDate = date;}; + void setLastEditDate(const string &date) { _lastEditDate = date; }; string getSize() const { return _size; }; @@ -56,7 +63,7 @@ private: string _path; Itemtype _type; string _title; - bool _downloaded{false}; + FileState _state{FileState::ICLOUD}; string _localPath; string _lastEditDate{"Error"}; string _size{"Error"}; diff --git a/src/util/nextcloud.cpp b/src/util/nextcloud.cpp index 860f803..91f9d94 100644 --- a/src/util/nextcloud.cpp +++ b/src/util/nextcloud.cpp @@ -166,7 +166,7 @@ void Nextcloud::downloadItem(int itemID) { case 200: Log::writeLog("finished download of " + _items->at(itemID).getPath() + " to " + _items->at(itemID).getLocalPath()); - _items->at(itemID).setDownloaded(true); + _items->at(itemID).setState(FileState::ISYNCED); break; case 401: Message(ICON_ERROR, "Error", "Username/password incorrect.", 1200); @@ -256,43 +256,7 @@ bool Nextcloud::getDataStructure(const string &pathUrl, const string &Username, //TODO if has files that are not online, add to _items } - //get local files, https://stackoverflow.com/questions/306533/how-do-i-get-a-list-of-files-in-a-directory-in-c - DIR *dir; - class dirent *ent; - class stat st; - - dir = opendir(localPath.c_str()); - while ((ent = readdir(dir)) != NULL) - { - const string file_name = ent->d_name; - const string full_file_name = localPath + file_name; - - if (file_name[0] == '.') - continue; - - if (stat(full_file_name.c_str(), &st) == -1) - continue; - - const bool is_directory = (st.st_mode & S_IFDIR) != 0; - - if (is_directory) - continue; - bool found = false; - for (auto i = 0; i < _items->size(); i++) - { - //TODO compare last edit local and in cloud and display to user - if (_items->at(i).getLocalPath().compare(full_file_name) == 0) - { - found = true; - break; - } - } - if (!found) - { - _items->push_back(Item(full_file_name, true)); - } - } - closedir(dir); + getLocalFileStructure(localPath); } //TODO structure as CSV? @@ -434,6 +398,8 @@ bool Nextcloud::getOfflineStructure(const string &pathUrl) if (!readInXML(buffer.str())) return false; + + getLocalFileStructure(this->getLocalPath(pathUrl)); } else { @@ -451,3 +417,46 @@ bool Nextcloud::getOfflineStructure(const string &pathUrl) return false; } + +void Nextcloud::getLocalFileStructure(const string &localPath) +{ + //TODO also get folders + //get local files, https://stackoverflow.com/questions/306533/how-do-i-get-a-list-of-files-in-a-directory-in-c + DIR *dir; + class dirent *ent; + class stat st; + + dir = opendir(localPath.c_str()); + while ((ent = readdir(dir)) != NULL) + { + const string file_name = ent->d_name; + const string full_file_name = localPath + file_name; + + if (file_name[0] == '.') + continue; + + if (stat(full_file_name.c_str(), &st) == -1) + continue; + + //also include directory + const bool is_directory = (st.st_mode & S_IFDIR) != 0; + if (is_directory) + continue; + + bool found = false; + for (auto i = 0; i < _items->size(); i++) + { + //TODO compare last edit local and in cloud and display to user + if (_items->at(i).getLocalPath().compare(full_file_name) == 0) + { + found = true; + break; + } + } + if (!found) + { + _items->push_back(Item(full_file_name, FileState::ILOCAL)); + } + } + closedir(dir); +} diff --git a/src/util/nextcloud.h b/src/util/nextcloud.h index 47adbd6..bb4ff2d 100644 --- a/src/util/nextcloud.h +++ b/src/util/nextcloud.h @@ -67,6 +67,8 @@ public: static string getLocalPath(string path); + void getLocalFileStructure(const string &localPath); + private: static Nextcloud *nextcloudStatic;