added work offline functionality

pull/23/head
JuanJakobo 2020-10-22 21:48:29 +02:00
parent 316758e94e
commit b090624405
3 changed files with 89 additions and 60 deletions

View File

@ -11,6 +11,7 @@
#include "menuHandler.h" #include "menuHandler.h"
#include "listView.h" #include "listView.h"
#include "item.h" #include "item.h"
#include "util.h"
#include <string> #include <string>
#include <memory> #include <memory>
@ -67,8 +68,29 @@ void EventHandler::mainMenuHandler(const int index)
{ {
switch (index) switch (index)
{ {
//Logout //offlineModus
case 101: case 101:
{
if (_nextcloud->isWorkOffline())
{
if (Util::connectToNetwork())
{
_nextcloud->switchWorkOffline();
}
else
{
Message(ICON_WARNING, "Warning", "Could not connect to the internet.", 600);
}
}
else
{
_nextcloud->switchWorkOffline();
}
break;
}
//Logout
case 102:
{ {
int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "Do you want to delete local files?", "Yes", "No", "Cancel"); int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "Do you want to delete local files?", "Yes", "No", "Cancel");
if (dialogResult == 1) if (dialogResult == 1)
@ -88,7 +110,7 @@ void EventHandler::mainMenuHandler(const int index)
break; break;
} }
//Exit //Exit
case 102: case 103:
CloseApp(); CloseApp();
break; break;
default: default:
@ -102,7 +124,7 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
{ {
if (IsInRect(par1, par2, _menu->getMenuButtonRect()) == 1) if (IsInRect(par1, par2, _menu->getMenuButtonRect()) == 1)
{ {
return _menu->createMenu(_nextcloud->isLoggedIn(), EventHandler::mainMenuHandlerStatic); return _menu->createMenu(_nextcloud->isLoggedIn(), _nextcloud->isWorkOffline(), EventHandler::mainMenuHandlerStatic);
} }
else if (_listView != nullptr) else if (_listView != nullptr)
{ {
@ -127,8 +149,15 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
} }
else else
{ {
int dialogResult = 4;
int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Download/Sync", "Open", "Remove"); if (_nextcloud->getItems()[itemID].isDownloaded())
{
dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Sync and open", "Remove", "Cancel");
}
else
{
dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Download and open", "Remove", "Cancel");
}
switch (dialogResult) switch (dialogResult)
{ {
@ -137,22 +166,17 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
{ {
Message(ICON_WARNING, "Warning", "Could not download the file, please try again.", 600); Message(ICON_WARNING, "Warning", "Could not download the file, please try again.", 600);
} }
break; else
case 2:
if (_nextcloud->getItems()[itemID].isDownloaded())
{ {
_nextcloud->getItems()[itemID].open(); _nextcloud->getItems()[itemID].open();
} }
else
{ break;
if (!_nextcloud->downloadItem(itemID)) case 2:
{ _nextcloud->getItems()[itemID].removeFile();
Message(ICON_WARNING, "Warning", "Could not download the file, please try again.", 600);
}
}
break; break;
case 3: case 3:
_nextcloud->getItems()[itemID].removeFile(); CloseDialog();
break; break;
default: default:

View File

@ -92,18 +92,24 @@ void Nextcloud::logout()
remove((NEXTCLOUD_CONFIG_PATH + ".back.").c_str()); remove((NEXTCLOUD_CONFIG_PATH + ".back.").c_str());
_url.clear(); _url.clear();
_loggedIn = false; _loggedIn = false;
//TODO remove files
} }
bool Nextcloud::downloadItem(int itemID) bool Nextcloud::downloadItem(int itemID)
{ {
Log::writeLog("started download of " + _items[itemID].getPath() + " to " + _items[itemID].getLocalPath()); Log::writeLog("started download of " + _items[itemID].getPath() + " to " + _items[itemID].getLocalPath());
//TODO if is working offline ask if switch to online modus if (_workOffline)
{
int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "You are in offline modus. Go back online?", "Yes", "No", "Cancel");
if (dialogResult == 2 || dialogResult == 3)
return false;
}
if (!Util::connectToNetwork()) if (!Util::connectToNetwork())
{ {
Message(3, "Warning", "cannot connect to Internet.", 200); Message(3, "Warning", "Can not connect to the Internet. Switching to offline modus.", 200);
_workOffline = true;
return false; return false;
} }
@ -157,46 +163,17 @@ bool Nextcloud::getDataStructure(string &pathUrl)
return getDataStructure(pathUrl, this->getUsername(), this->getPassword()); return getDataStructure(pathUrl, this->getUsername(), this->getPassword());
} }
bool Nextcloud::getDataStructure(const string &pathUrl, const string &Username, const string &Pass) bool Nextcloud::getDataStructure(const string &pathUrl, const string &Username, const string &Pass)
{ {
//could not connect to internet, therefore offline modus //could not connect to internet, therefore offline modus
//while (!Util::connectToNetwork())
if(!Util::connectToNetwork())
{
//Dialog(2, "Warning", "Cannot connect to the internet.", "Try again", "Work offline", Nextcloud::DialogHandlerStatic);
_workOffline = true;
//TODO show in menu "working offline"
//TODO add to menu
if (_workOffline) if (_workOffline)
{ return getOfflineStructure(pathUrl);
string localPath = this->getLocalPath(pathUrl) + NEXTCLOUD_STRUCTURE_EXTENSION;
if (iv_access(localPath.c_str(), W_OK) == 0)
{
ifstream inFile(localPath);
std::stringstream buffer;
buffer << inFile.rdbuf();
if (!readInXML(buffer.str())) if (!Util::connectToNetwork())
return false;
}
else
{ {
if (localPath.find(NEXTCLOUD_ROOT_PATH) != string::npos) Message(3, "Warning", "Cannot connect to the internet. Switching to offline modus. To work online turn on online modus in the menu.", 200);
{ _workOffline = true;
//TODO what to display if ROOT_PATH is not available offline? return getOfflineStructure(pathUrl);
return false;
}
else
{
//Structure is not available offline, stay at the tree
Message(ICON_ERROR, "Error", "The selected structure is offline not available.", 1200);
}
}
return true;
}
} }
if (_url.empty()) if (_url.empty())
@ -371,3 +348,32 @@ string Nextcloud::getLocalPath(string path)
return NEXTCLOUD_FILE_PATH + "/" + path; return NEXTCLOUD_FILE_PATH + "/" + path;
} }
bool Nextcloud::getOfflineStructure(const string &pathUrl)
{
string localPath = this->getLocalPath(pathUrl) + NEXTCLOUD_STRUCTURE_EXTENSION;
if (iv_access(localPath.c_str(), W_OK) == 0)
{
ifstream inFile(localPath);
std::stringstream buffer;
buffer << inFile.rdbuf();
if (!readInXML(buffer.str()))
return false;
}
else
{
if (localPath.find(NEXTCLOUD_ROOT_PATH) != string::npos)
{
Message(ICON_ERROR, "Error", "The root structure is not available offline. To try again to connect turn on online modus in the menu.", 1200);
}
else
{
//Structure is not available offline, stay at the tree
Message(ICON_ERROR, "Error", "The selected structure is not available offline.", 1200);
return true;
}
}
}

View File

@ -59,10 +59,11 @@ public:
vector<Item> getItems() { return _items; }; vector<Item> getItems() { return _items; };
bool isLoggedIn() { return _loggedIn; }; bool isLoggedIn() { return _loggedIn; };
bool isWorkOffline() { return _workOffline; };
void switchWorkOffline() { _workOffline = !_workOffline; };
static string getLocalPath(string path); static string getLocalPath(string path);
private: private:
static Nextcloud *nextcloudStatic; static Nextcloud *nextcloudStatic;
@ -71,10 +72,6 @@ private:
string _url; string _url;
bool _workOffline{false}; bool _workOffline{false};
//TODO make username and password local variables or get each time? --> it cant change during login??
/** /**
* gets the dataStructure of the given URL and writes its WEBDAV items to the items vector * gets the dataStructure of the given URL and writes its WEBDAV items to the items vector
* *
@ -99,6 +96,8 @@ private:
static void DialogHandlerStatic(int Button); static void DialogHandlerStatic(int Button);
bool readInXML(string xml); bool readInXML(string xml);
bool getOfflineStructure(const string &pathUrl);
}; };
#endif #endif