From ff27551524e94a6553da87e3108d1651d3dc1062 Mon Sep 17 00:00:00 2001 From: JuanJakobo <34421964+JuanJakobo@users.noreply.github.com> Date: Sun, 21 Feb 2021 19:49:25 +0100 Subject: [PATCH] add filestate status outsync depending on filesize --- src/handler/eventHandler.cpp | 2 +- src/ui/listViewEntry.cpp | 6 +++++- src/util/item.cpp | 20 ++++++++--------- src/util/item.h | 6 ++++-- src/util/nextcloud.cpp | 42 ++++++++++++++++++++++-------------- src/util/nextcloud.h | 2 +- 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/handler/eventHandler.cpp b/src/handler/eventHandler.cpp index a9a62cb..6bc8c58 100644 --- a/src/handler/eventHandler.cpp +++ b/src/handler/eventHandler.cpp @@ -176,7 +176,7 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) } else { - if (_nextcloud.getItems().at(itemID).getState() != FileState::ICLOUD) + if (_nextcloud.getItems().at(itemID).getState() == FileState::ISYNCED || _nextcloud.getItems().at(itemID).getState() == FileState::ILOCAL) { dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Open", "Remove", "Cancel"); diff --git a/src/ui/listViewEntry.cpp b/src/ui/listViewEntry.cpp index 0f15819..ad2cd33 100644 --- a/src/ui/listViewEntry.cpp +++ b/src/ui/listViewEntry.cpp @@ -36,6 +36,10 @@ void ListViewEntry::draw(const Item &item, ifont *entryFont, ifont *entryFontBol { DrawTextRect(_position.x, _position.y + 3 * fontHeight, _position.w, fontHeight, "Synced", ALIGN_RIGHT); } + else if(item.getState() == FileState::IOUTSYNCED) + { + DrawTextRect(_position.x, _position.y + 3 * fontHeight, _position.w, fontHeight, "Out of sync", ALIGN_RIGHT); + } else { DrawTextRect(_position.x, _position.y + 3 * fontHeight, _position.w, fontHeight, "Click to Download", ALIGN_RIGHT); @@ -50,7 +54,7 @@ void ListViewEntry::draw(const Item &item, ifont *entryFont, ifont *entryFontBol } 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 + 3 * fontHeight, _position.w, fontHeight, item.getSizeString().c_str(), ALIGN_LEFT); } int line = (_position.y + _position.h) - 1; diff --git a/src/util/item.cpp b/src/util/item.cpp index 92025e9..304d0f1 100644 --- a/src/util/item.cpp +++ b/src/util/item.cpp @@ -31,12 +31,12 @@ Item::Item(const string &xmlItem) { _type = IFOLDER; _title = _title.substr(0, _path.length() - 1); - setSize(atof(Util::getXMLAttribute(xmlItem, "d:quota-used-bytes").c_str())); + _size = atof(Util::getXMLAttribute(xmlItem, "d:quota-used-bytes").c_str()); } else { _type = IFILE; - setSize(atof(Util::getXMLAttribute(xmlItem, "d:getcontentlength").c_str())); + _size = atof(Util::getXMLAttribute(xmlItem, "d:getcontentlength").c_str()); _fileType = Util::getXMLAttribute(xmlItem, "d:getcontenttype"); //set local path and test if exists @@ -116,24 +116,22 @@ bool Item::removeFile() return true; } -void Item::setSize(double tempSize) +string Item::getSizeString() const { - if (tempSize < 1024) - { - _size = "< 1 KB"; - return; - } + if (_size < 1024) + return "< 1 KB"; double departBy; + double tempSize; string unit; - if (tempSize < 1048576) + if (_size < 1048576) { departBy = 1024; unit = "KB"; } - else if (tempSize < 1073741824) + else if (_size < 1073741824) { departBy = 1048576; unit = "MB"; @@ -145,5 +143,5 @@ void Item::setSize(double tempSize) } tempSize = round((tempSize / departBy) * 10.0) / 10.0; - _size = Util::valueToString(tempSize) + " " + unit; + return Util::valueToString(tempSize) + " " + unit; } \ No newline at end of file diff --git a/src/util/item.h b/src/util/item.h index 08a6707..d8e2941 100644 --- a/src/util/item.h +++ b/src/util/item.h @@ -25,6 +25,7 @@ enum FileState { ICLOUD, ISYNCED, + IOUTSYNCED, ILOCAL }; @@ -71,7 +72,8 @@ public: string getLastEditDate() const { return _lastEditDate; }; void setLastEditDate(const string &date) { _lastEditDate = date; }; - string getSize() const { return _size; }; + double getSize() const { return _size;}; + string getSizeString() const; string getFiletype() const { return _fileType; }; @@ -82,7 +84,7 @@ private: FileState _state{FileState::ICLOUD}; string _localPath; string _lastEditDate{"Error"}; - string _size{"Error"}; + double _size; string _fileType; /** diff --git a/src/util/nextcloud.cpp b/src/util/nextcloud.cpp index 4e9bbb8..3edd323 100644 --- a/src/util/nextcloud.cpp +++ b/src/util/nextcloud.cpp @@ -142,6 +142,12 @@ void Nextcloud::logout(bool deleteFiles) void Nextcloud::downloadItem(vector &tempItems, int itemID) { + if(tempItems.at(itemID).getState() == FileState::ISYNCED) + { + Message(ICON_INFORMATION, "INFO", ("The newest version of file " + tempItems.at(itemID).getPath() + " is already downloaded.").c_str(), 2000); + return; + } + if (tempItems.at(itemID).getPath().empty()) { Message(ICON_ERROR, "Error", "Download path is not set, therefore cannot download the file.", 2000); @@ -215,7 +221,6 @@ bool Nextcloud::downloadFolder(vector &tempItems, int itemID) } else { - //TODO do only if file is newer --> check status Log::writeLog("started download of " + _items.at(itemID).getPath() + " to " + _items.at(itemID).getLocalPath()); downloadItem(tempItems, itemID); } @@ -327,7 +332,7 @@ vector Nextcloud::getDataStructure(const string &pathUrl, const string &Us Log::writeLog("Local structure of " + localPath + " found."); } - getLocalFileStructure(localPath); + getLocalFileStructure(tempItems, localPath); } //update the .structure file acording to items in the folder @@ -445,7 +450,7 @@ vector Nextcloud::getOfflineStructure(const string &pathUrl) if (tempItems.empty()) return {}; - getLocalFileStructure(this->getLocalPath(pathUrl)); + getLocalFileStructure(tempItems, this->getLocalPath(pathUrl)); return tempItems; } else @@ -464,9 +469,8 @@ vector Nextcloud::getOfflineStructure(const string &pathUrl) return {}; } -void Nextcloud::getLocalFileStructure(const string &localPath) +void Nextcloud::getLocalFileStructure(vector &tempItems, const string &localPath) { - //TODO also show local folders that are not synced to the cloud yet //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; @@ -475,33 +479,39 @@ void Nextcloud::getLocalFileStructure(const string &localPath) 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; + const string fileName = ent->d_name; + const string fullFileName = localPath + fileName; - if (file_name[0] == '.') + if (fileName[0] == '.') continue; - if (stat(full_file_name.c_str(), &st) == -1) + if (stat(fullFileName.c_str(), &st) == -1) continue; - //also include directory - const bool is_directory = (st.st_mode & S_IFDIR) != 0; - if (is_directory) + const bool isDirectory = (st.st_mode & S_IFDIR) != 0; + if (isDirectory) continue; bool found = false; - for (auto i = 0; i < _items.size(); i++) + for (auto i = 0; i < tempItems.size(); i++) { - //TODO compare last edit local and in cloud and display to user - if (_items.at(i).getLocalPath().compare(full_file_name) == 0) + if (tempItems.at(i).getLocalPath().compare(fullFileName) == 0) { + std::ifstream in(fullFileName, std::ifstream::binary | std::ifstream::ate ); + Log::writeLog(Util::valueToString(in.tellg())); + Log::writeLog(Util::valueToString(tempItems.at(i).getSize())); + if(in.tellg() != tempItems.at(i).getSize()) + { + tempItems.at(i).setState(FileState::IOUTSYNCED); + } found = true; break; } } if (!found) { - _items.push_back(Item(full_file_name, FileState::ILOCAL)); + //TODO push to different items list and then ask if shall be deleted later on? --> just in case of folder sync + tempItems.push_back(Item(fullFileName, FileState::ILOCAL)); } } closedir(dir); diff --git a/src/util/nextcloud.h b/src/util/nextcloud.h index 89d23d2..4bbdf59 100644 --- a/src/util/nextcloud.h +++ b/src/util/nextcloud.h @@ -90,7 +90,7 @@ public: static string getLocalPath(string path); - void getLocalFileStructure(const string &localPath); + void getLocalFileStructure(vector &tempItems, const string &localPath); private: vector _items;