closes #6 Sync of certain folders

pull/23/head
JuanJakobo 2021-02-13 12:09:15 +01:00
parent a3dc3a6b73
commit ffb46fa47a
5 changed files with 108 additions and 109 deletions

View File

@ -109,7 +109,7 @@ void EventHandler::mainMenuHandler(const int index)
_nextcloud.logout(); _nextcloud.logout();
break; break;
} }
_listView.reset(); _listView.release();
_loginView = std::unique_ptr<LoginView>(new LoginView(_menu.getContentRect())); _loginView = std::unique_ptr<LoginView>(new LoginView(_menu.getContentRect()));
FullUpdate(); FullUpdate();
break; break;
@ -142,11 +142,18 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
int itemID = _listView->listClicked(par1, par2); int itemID = _listView->listClicked(par1, par2);
if (itemID != -1) if (itemID != -1)
{ {
int dialogResult = 0;
if (_nextcloud.getItems().at(itemID).getType() == Itemtype::IFOLDER) if (_nextcloud.getItems().at(itemID).getType() == Itemtype::IFOLDER)
{ {
//TODO temp solution --> remove solution if (_nextcloud.getItems().at(itemID).getTitle().compare("...") == 0)
//TODO if is the first option, go back and dont ask for sync {
int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to to do?", "Open folder", "Sync Folder", "Cancel"); dialogResult = 1;
}
else
{
dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to to do?", "Open folder", "Sync Folder", "Cancel");
}
switch (dialogResult) switch (dialogResult)
{ {
case 1: case 1:
@ -156,52 +163,52 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
_tempPath = _nextcloud.getItems().at(itemID).getPath(); _tempPath = _nextcloud.getItems().at(itemID).getPath();
if (!_tempPath.empty()) if (!_tempPath.empty())
_nextcloud.setItems(_nextcloud.getDataStructure(_tempPath)); _nextcloud.setItems(_nextcloud.getDataStructure(_tempPath));
_listView.reset(new ListView(_menu.getContentRect(), _nextcloud.getItems())); _listView.release();
_listView = std::unique_ptr<ListView>(new ListView(_menu.getContentRect(), _nextcloud.getItems()));
_listView->drawHeader(_tempPath.substr(NEXTCLOUD_ROOT_PATH.length())); _listView->drawHeader(_tempPath.substr(NEXTCLOUD_ROOT_PATH.length()));
break; break;
case 2: case 2:
//Sync folder dialogResult = 0;
_nextcloud.downloadFolder(_nextcloud.getItems(), itemID);
//update the entry and say --> folder is synced
//entries in visual and in nextlcoud are out of sync
default: default:
break; break;
} }
} }
else else
{ {
int dialogResult = 0;
if (_nextcloud.getItems().at(itemID).getState() != FileState::ICLOUD) if (_nextcloud.getItems().at(itemID).getState() != FileState::ICLOUD)
{ {
dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Open", "Remove", "Cancel"); dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Open", "Remove", "Cancel");
}
switch (dialogResult) switch (dialogResult)
{
case 1:
_nextcloud.getItems().at(itemID).open();
break;
case 2:
if (!_nextcloud.getItems().at(itemID).removeFile())
Message(ICON_WARNING, "Warning", "Could not delete the file, please try again.", 1200);
break;
case 3:
break;
default:
if (_nextcloud.isWorkOffline())
{ {
int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "You are in offline modus. Go back online?", "Yes", "No", "Cancel"); case 1:
if (dialogResult == 2 || dialogResult == 3) _nextcloud.getItems().at(itemID).open();
return 0; break;
_nextcloud.switchWorkOffline(); case 2:
if (!_nextcloud.removeItem(itemID))
Message(ICON_WARNING, "Warning", "Could not delete the file, please try again.", 1200);
_listView->drawEntry(itemID);
break;
default:
break;
} }
OpenProgressbar(1, "Downloading...", "Check network connection", 0, EventHandler::DialogHandlerStatic);
_nextcloud.downloadItem(itemID);
CloseProgressbar();
break;
} }
}
if (dialogResult == 0)
{
if (_nextcloud.isWorkOffline())
{
int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "You are in offline modus. Go back online?", "Yes", "No", "Cancel");
if (dialogResult == 2 || dialogResult == 3)
return 1;
_nextcloud.switchWorkOffline();
}
OpenProgressbar(1, "Downloading...", "Check network connection", 0, EventHandler::DialogHandlerStatic);
_nextcloud.download(itemID);
CloseProgressbar();
//TODO Include Sync notice for folders
_listView->drawEntry(itemID); _listView->drawEntry(itemID);
} }
} }

View File

@ -19,7 +19,7 @@
using std::string; using std::string;
using std::vector; using std::vector;
ListView::ListView(const irect *contentRect, const vector<Item> items) : _contentRect(contentRect), _items(items) ListView::ListView(const irect *contentRect, const vector<Item> &items) : _contentRect(contentRect), _items(&items)
{ {
FillAreaRect(_contentRect, WHITE); FillAreaRect(_contentRect, WHITE);
@ -42,7 +42,7 @@ ListView::ListView(const irect *contentRect, const vector<Item> items) : _conten
_page = 1; _page = 1;
_shownPage = _page; _shownPage = _page;
auto i = _items.size(); auto i = _items->size();
auto z = 0; auto z = 0;
_entries.reserve(i); _entries.reserve(i);
@ -107,7 +107,7 @@ void ListView::drawFooter()
void ListView::drawEntry(int itemID) void ListView::drawEntry(int itemID)
{ {
FillAreaRect(_entries[itemID].getPosition(), WHITE); FillAreaRect(_entries[itemID].getPosition(), WHITE);
_entries[itemID].draw(_items.at(itemID), _entryFont, _entryFontBold, _entryFontHeight); _entries[itemID].draw(_items->at(itemID), _entryFont, _entryFontBold, _entryFontHeight);
} }
void ListView::drawEntries() void ListView::drawEntries()
@ -115,7 +115,7 @@ void ListView::drawEntries()
for (auto i = 0; i < _entries.size(); i++) for (auto i = 0; i < _entries.size(); i++)
{ {
if (_entries[i].getPage() == _shownPage) if (_entries[i].getPage() == _shownPage)
_entries[i].draw(_items.at(i), _entryFont, _entryFontBold, _entryFontHeight); _entries[i].draw(_items->at(i), _entryFont, _entryFontBold, _entryFontHeight);
} }
} }

View File

@ -29,7 +29,7 @@ public:
* @param ContentRect area of the screen where the list view is placed * @param ContentRect area of the screen where the list view is placed
* @param Items items that shall be shown in the listview * @param Items items that shall be shown in the listview
*/ */
ListView(const irect *contentRect, const vector<Item> items); ListView(const irect *contentRect, const vector<Item> &items);
/** /**
* Destructor * Destructor
@ -75,7 +75,7 @@ private:
int _footerFontHeight; int _footerFontHeight;
int _entryFontHeight; int _entryFontHeight;
const irect *_contentRect; const irect *_contentRect;
vector<Item> _items; std::unique_ptr<const vector<Item>> _items;
vector<ListViewEntry> _entries; vector<ListViewEntry> _entries;
ifont *_headerFont; ifont *_headerFont;
ifont *_footerFont; ifont *_footerFont;

View File

@ -21,11 +21,8 @@ using std::ifstream;
using std::ofstream; using std::ofstream;
using std::string; using std::string;
//neccesary to use Dialog method
std::unique_ptr<Nextcloud> Nextcloud::_nextcloudStatic;
Nextcloud::Nextcloud() Nextcloud::Nextcloud()
{ {
_nextcloudStatic = std::unique_ptr<Nextcloud>(this);
if (iv_access(NEXTCLOUD_PATH.c_str(), W_OK) != 0) if (iv_access(NEXTCLOUD_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_PATH.c_str(), 0777); iv_mkdir(NEXTCLOUD_PATH.c_str(), 0777);
@ -141,23 +138,15 @@ void Nextcloud::logout(bool deleteFiles)
_loggedIn = false; _loggedIn = false;
} }
void Nextcloud::downloadItem(int itemID) void Nextcloud::downloadItem(vector<Item> &tempItems, int itemID)
{ {
Log::writeLog("started download of " + _items.at(itemID).getPath() + " to " + _items.at(itemID).getLocalPath()); if (tempItems.at(itemID).getPath().empty())
if (!Util::connectToNetwork())
{
Message(ICON_WARNING, "Warning", "Can not connect to the Internet. Switching to offline modus.", 1200);
_workOffline = true;
}
if (_items.at(itemID).getPath().empty())
{ {
Message(ICON_ERROR, "Error", "Download path is not set, therefore cannot download the file.", 1200); Message(ICON_ERROR, "Error", "Download path is not set, therefore cannot download the file.", 1200);
return; return;
} }
UpdateProgressbar("Starting Download", 0); UpdateProgressbar(("Starting Download of " + tempItems.at(itemID).getPath()).c_str(), 0);
CURLcode res; CURLcode res;
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
@ -167,9 +156,9 @@ void Nextcloud::downloadItem(int itemID)
string post = this->getUsername() + std::string(":") + this->getPassword(); string post = this->getUsername() + std::string(":") + this->getPassword();
FILE *fp; FILE *fp;
fp = iv_fopen(_items.at(itemID).getLocalPath().c_str(), "wb"); fp = iv_fopen(tempItems.at(itemID).getLocalPath().c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, (_url + _items.at(itemID).getPath()).c_str()); curl_easy_setopt(curl, CURLOPT_URL, (_url + tempItems.at(itemID).getPath()).c_str());
curl_easy_setopt(curl, CURLOPT_USERPWD, post.c_str()); curl_easy_setopt(curl, CURLOPT_USERPWD, post.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Util::writeData); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Util::writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
@ -189,8 +178,8 @@ void Nextcloud::downloadItem(int itemID)
switch (response_code) switch (response_code)
{ {
case 200: case 200:
Log::writeLog("finished download of " + _items.at(itemID).getPath() + " to " + _items.at(itemID).getLocalPath()); Log::writeLog("finished download of " + tempItems.at(itemID).getPath() + " to " + tempItems.at(itemID).getLocalPath());
_items.at(itemID).setState(FileState::ISYNCED); tempItems.at(itemID).setState(FileState::ISYNCED);
break; break;
case 401: case 401:
Message(ICON_ERROR, "Error", "Username/password incorrect.", 1200); Message(ICON_ERROR, "Error", "Username/password incorrect.", 1200);
@ -203,40 +192,52 @@ void Nextcloud::downloadItem(int itemID)
} }
} }
bool Nextcloud::downloadFolder(const vector<Item> &tempItems, int itemId) bool Nextcloud::downloadFolder(vector<Item> &tempItems, int itemID)
{ {
if (tempItems.at(itemID).getType() == Itemtype::IFOLDER)
//also has to look at the previous items?
//do not change items oxml or use temp here?
if (tempItems.at(itemId).getType() == Itemtype::IFOLDER)
{ {
string temp = tempItems.at(itemID).getPath();
Log::writeLog("Path to look for " + temp);
vector<Item> tempItems = getDataStructure(temp);
//get the data structure under the folder that is requested --> overrides items object and therefore will fail! //first item of the vector is the root path itself
for (auto i = 1; i < tempItems.size(); i++)
//should return items vector?
string temp = tempItems.at(itemId).getPath();
vector<Item> tempItemsNew = getDataStructure(temp);
//this will be the new item list --> where to switch to when this process is over --> open the folder that has t be synced?
//get item id?
for (auto i = 0; i < tempItemsNew.size(); i++)
{ {
Log::writeLog("Item: " + tempItems.at(i).getPath());
downloadFolder(tempItemsNew,itemId); downloadFolder(tempItems,i);
//overrides current item list and therefore nos possible!
// has to be read in again?
// call method again?
} }
} }
else else
{ {
downloadItem(itemId); //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);
} }
return true; return true;
} }
void Nextcloud::download(int itemID)
{
if (!Util::connectToNetwork())
{
Message(ICON_WARNING, "Warning", "Can not connect to the Internet. Switching to offline modus.", 1200);
_workOffline = true;
return;
}
this->downloadFolder(_items,itemID);
}
bool Nextcloud::removeItem(int itemID)
{
Log::writeLog("removing file " + _items.at(itemID).getPath());
if(!_items.at(itemID).removeFile())
return false;
return true;
}
vector<Item> Nextcloud::getDataStructure(string &pathUrl) vector<Item> Nextcloud::getDataStructure(string &pathUrl)
{ {
return getDataStructure(pathUrl, this->getUsername(), this->getPassword()); return getDataStructure(pathUrl, this->getUsername(), this->getPassword());
@ -382,14 +383,6 @@ string Nextcloud::getStartFolder()
return startFolder; return startFolder;
} }
void Nextcloud::DialogHandlerStatic(int Button)
{
if (Button == 2)
{
_nextcloudStatic->_workOffline = true;
}
}
vector<Item> Nextcloud::readInXML(string xml) vector<Item> Nextcloud::readInXML(string xml)
{ {
size_t begin; size_t begin;

View File

@ -32,6 +32,18 @@ class Nextcloud
public: public:
explicit Nextcloud(); explicit Nextcloud();
void setURL(const string &Url);
void setUsername(const string &Username);
void setPassword(const string &Pass);
void setStartFolder(const string &Path);
bool setItems(const vector<Item> &tempItems);
const vector<Item> &getItems() const { return _items; };
bool isLoggedIn() const { return _loggedIn; };
bool isWorkOffline() const { return _workOffline; };
void switchWorkOffline() { _workOffline = !_workOffline; };
/** /**
* Handles first login to nextcloud, if sucessfull saves userdata * Handles first login to nextcloud, if sucessfull saves userdata
* *
@ -56,14 +68,17 @@ public:
* Downloads a certain item from the Nextcloud and saves it locally * Downloads a certain item from the Nextcloud and saves it locally
* @param itemID id of the item * @param itemID id of the item
*/ */
void downloadItem(int itemID); void downloadItem(vector<Item> &tempItems, int itemID);
/** /**
* *
* *
*/ */
bool downloadFolder(const vector<Item> &tempItems, int itemId); bool downloadFolder(vector<Item> &tempItems, int itemId);
void download(int itemId);
bool removeItem(int itemID);
/** /**
* gets the dataStructure of the given URL and writes its WEBDAV items to the items vector, reads Userdata from configfile * gets the dataStructure of the given URL and writes its WEBDAV items to the items vector, reads Userdata from configfile
@ -73,27 +88,11 @@ public:
*/ */
vector<Item> getDataStructure(string &pathUrl); vector<Item> getDataStructure(string &pathUrl);
void setURL(const string &Url);
void setUsername(const string &Username);
void setPassword(const string &Pass);
void setStartFolder(const string &Path);
vector<Item> getItems() const { return _items; };
bool isLoggedIn() const { return _loggedIn; };
bool isWorkOffline() const { return _workOffline; };
void switchWorkOffline() { _workOffline = !_workOffline; };
static string getLocalPath(string path); static string getLocalPath(string path);
void getLocalFileStructure(const string &localPath); void getLocalFileStructure(const string &localPath);
bool setItems(const vector<Item> &tempItems);
private: private:
static std::unique_ptr<Nextcloud> _nextcloudStatic;
vector<Item> _items; vector<Item> _items;
bool _loggedIn{false}; bool _loggedIn{false};
string _url; string _url;