changed pointer usage

pull/23/head
JuanJakobo 2020-11-05 09:18:47 +01:00
parent 78606e42b3
commit 2b6f985c33
6 changed files with 70 additions and 70 deletions

View File

@ -24,23 +24,20 @@ EventHandler::EventHandler()
//create a event to create handlers
_eventHandlerStatic = this;
//TODO use pointer for menu?
_menu = std::unique_ptr<MenuHandler>(new MenuHandler("Nextcloud"));
_nextcloud = std::unique_ptr<Nextcloud>(new Nextcloud());
_loginView = nullptr;
_listView = nullptr;
if (iv_access(NEXTCLOUD_CONFIG_PATH.c_str(), W_OK) == 0)
{
if (_nextcloud->login())
if (_nextcloud.login())
{
_listView = std::unique_ptr<ListView>(new ListView(_menu->getContentRect(), _nextcloud->getItems()));
_listView = std::unique_ptr<ListView>(new ListView(_menu.getContentRect(), _nextcloud.getItems()));
FullUpdate();
return;
}
}
_loginView = std::unique_ptr<LoginView>(new LoginView(_menu->getContentRect()));
_loginView = std::unique_ptr<LoginView>(new LoginView(_menu.getContentRect()));
_loginView->drawLoginView();
FullUpdate();
@ -66,11 +63,11 @@ void EventHandler::mainMenuHandler(const int index)
//offlineModus
case 101:
{
if (_nextcloud->isWorkOffline())
if (_nextcloud.isWorkOffline())
{
if (Util::connectToNetwork())
{
_nextcloud->switchWorkOffline();
_nextcloud.switchWorkOffline();
}
else
{
@ -79,7 +76,7 @@ void EventHandler::mainMenuHandler(const int index)
}
else
{
_nextcloud->switchWorkOffline();
_nextcloud.switchWorkOffline();
}
break;
@ -87,19 +84,20 @@ void EventHandler::mainMenuHandler(const int index)
//Logout
case 102:
{
int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "Do you want to delete local files?", "Yes", "No", "Cancel");
if (dialogResult == 1)
{
remove(NEXTCLOUD_FILE_PATH.c_str());
}
else if (dialogResult == 3)
{
return;
}
_nextcloud->logout();
//TODO implement removal of files
//int dialogResult = DialogSynchro(ICON_QUESTION, "Action", "Do you want to delete local files?", "Yes", "No", "Cancel");
//if (dialogResult == 1)
//{
// remove(NEXTCLOUD_FILE_PATH.c_str());
//}
//else if (dialogResult == 3)
//{
// return;
//}
_nextcloud.logout();
_listView.reset();
_loginView = std::unique_ptr<LoginView>(new LoginView(_menu->getContentRect()));
_loginView = std::unique_ptr<LoginView>(new LoginView(_menu.getContentRect()));
_loginView->drawLoginView();
FullUpdate();
break;
@ -117,32 +115,32 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
{
if (type == EVT_POINTERDOWN)
{
if (IsInRect(par1, par2, _menu->getMenuButtonRect()) == 1)
if (IsInRect(par1, par2, _menu.getMenuButtonRect()) == 1)
{
return _menu->createMenu(_nextcloud->isLoggedIn(), _nextcloud->isWorkOffline(), EventHandler::mainMenuHandlerStatic);
return _menu.createMenu(_nextcloud.isLoggedIn(), _nextcloud.isWorkOffline(), EventHandler::mainMenuHandlerStatic);
}
else if (_listView != nullptr)
{
int itemID = _listView->listClicked(par1, par2);
if (itemID != -1)
{
if (_nextcloud->getItems()[itemID].getType() == Itemtype::IFOLDER)
if (_nextcloud.getItems()->at(itemID).getType() == Itemtype::IFOLDER)
{
FillAreaRect(_menu->getContentRect(), WHITE);
_menu->drawLoadingScreen();
FillAreaRect(_menu.getContentRect(), WHITE);
_menu.drawLoadingScreen();
string tempPath = _nextcloud->getItems()[itemID].getPath();
string tempPath = _nextcloud.getItems()->at(itemID).getPath();
if (!tempPath.empty())
_nextcloud->getDataStructure(tempPath);
_nextcloud.getDataStructure(tempPath);
_listView.reset(new ListView(_menu->getContentRect(), _nextcloud->getItems()));
_listView.reset(new ListView(_menu.getContentRect(), _nextcloud.getItems()));
_listView->drawHeader(tempPath.substr(NEXTCLOUD_ROOT_PATH.length()));
}
else
{
int dialogResult = 2;
if (_nextcloud->getItems()[itemID].isDownloaded())
if (_nextcloud.getItems()->at(itemID).isDownloaded())
{
dialogResult = DialogSynchro(ICON_QUESTION, "Action", "What do you want to do?", "Open", "Sync", "Remove");
}
@ -150,13 +148,12 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
switch (dialogResult)
{
case 1:
_nextcloud->getItems()[itemID].open();
_nextcloud.getItems()->at(itemID).open();
break;
case 2:
OpenProgressbar(1, "Downloading...", "Check network connection", 0, EventHandler::DialogHandlerStatic);
if (!_nextcloud->downloadItem(itemID))
if (!_nextcloud.downloadItem(itemID))
{
CloseProgressbar();
Message(ICON_WARNING, "Warning", "Could not download the file, please try again.", 600);
@ -167,20 +164,18 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
}
break;
case 3:
_nextcloud->removeFile(itemID);
if (!_nextcloud.removeFile(itemID))
Message(ICON_WARNING, "Warning", "Could not delete the file, please try again.", 600);
break;
default:
break;
}
//TODO pass items only as ref, so this is not necessary and items exist only once
_listView.reset(new ListView(_menu->getContentRect(), _nextcloud->getItems()));
//_listView->drawEntry(itemID);
_listView->drawEntry(itemID);
}
}
PartialUpdate(_menu->getContentRect()->x, _menu->getContentRect()->y, _menu->getContentRect()->w, _menu->getContentRect()->h);
PartialUpdate(_menu.getContentRect()->x, _menu.getContentRect()->y, _menu.getContentRect()->w, _menu.getContentRect()->h);
return 1;
}
@ -188,9 +183,9 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
{
if (_loginView->logginClicked(par1, par2) == 2)
{
if (_nextcloud->login(_loginView->getURL(), _loginView->getUsername(), _loginView->getPassword()))
if (_nextcloud.login(_loginView->getURL(), _loginView->getUsername(), _loginView->getPassword()))
{
_listView = std::unique_ptr<ListView>(new ListView(_menu->getContentRect(), _nextcloud->getItems()));
_listView = std::unique_ptr<ListView>(new ListView(_menu.getContentRect(), _nextcloud.getItems()));
_loginView.reset();
FullUpdate();
}

View File

@ -38,8 +38,8 @@ public:
private:
static EventHandler *_eventHandlerStatic;
std::unique_ptr<MenuHandler> _menu;
std::unique_ptr<Nextcloud> _nextcloud;
MenuHandler _menu = MenuHandler("Nextcloud");
Nextcloud _nextcloud = Nextcloud();
std::unique_ptr<ListView> _listView;
std::unique_ptr<LoginView> _loginView;

View File

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

View File

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

View File

@ -96,7 +96,7 @@ void Nextcloud::logout()
bool Nextcloud::downloadItem(int itemID)
{
Log::writeLog("started download of " + _items[itemID].getPath() + " to " + _items[itemID].getLocalPath());
Log::writeLog("started download of " + _items->at(itemID).getPath() + " to " + _items->at(itemID).getLocalPath());
if (_workOffline)
{
@ -121,9 +121,9 @@ bool Nextcloud::downloadItem(int itemID)
string post = this->getUsername() + std::string(":") + this->getPassword();
FILE *fp;
fp = iv_fopen(_items[itemID].getLocalPath().c_str(), "wb");
fp = iv_fopen(_items->at(itemID).getLocalPath().c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, (_url + _items[itemID].getPath()).c_str());
curl_easy_setopt(curl, CURLOPT_URL, (_url + _items->at(itemID).getPath()).c_str());
curl_easy_setopt(curl, CURLOPT_USERPWD, post.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Util::writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
@ -142,8 +142,8 @@ bool Nextcloud::downloadItem(int itemID)
switch (response_code)
{
case 200:
Log::writeLog("finished download of " + _items[itemID].getPath() + " to " + _items[itemID].getLocalPath());
_items[itemID].setDownloaded(true);
Log::writeLog("finished download of " + _items->at(itemID).getPath() + " to " + _items->at(itemID).getLocalPath());
_items->at(itemID).setDownloaded(true);
return true;
case 401:
Message(ICON_ERROR, "Error", "Username/password incorrect.", 1200);
@ -157,11 +157,12 @@ bool Nextcloud::downloadItem(int itemID)
return false;
}
bool Nextcloud::removeFile(int itemID)
{
remove(_items[itemID].getLocalPath().c_str());
_items[itemID].setDownloaded(false);
if (remove(_items->at(itemID).getLocalPath().c_str()) != 0)
return false;
_items->at(itemID).setDownloaded(false);
return true;
}
bool Nextcloud::getDataStructure(string &pathUrl)
@ -313,8 +314,10 @@ bool Nextcloud::readInXML(string xml)
size_t end;
string beginItem = "<d:response>";
string endItem = "</d:response>";
vector<Item> tempItems;
_items.clear();
if (_items)
_items->clear();
begin = xml.find(beginItem);
@ -323,26 +326,28 @@ bool Nextcloud::readInXML(string xml)
end = xml.find(endItem);
//TODO copy array and here only temp
this->_items.push_back(Item(xml.substr(begin, end)));
tempItems.push_back(Item(xml.substr(begin, end)));
xml = xml.substr(end + endItem.length());
begin = xml.find(beginItem);
}
if (_items.size() < 1)
_items = std::make_shared<vector<Item>>(std::move(tempItems));
if (_items->size() < 1)
return false;
//resize item 1
string header = _items[0].getPath();
string header = _items->at(0).getPath();
header = header.substr(0, header.find_last_of("/"));
header = header.substr(0, header.find_last_of("/") + 1);
_items[0].setPath(header);
_items[0].setTitle("...");
_items[0].setLastEditDate("");
_items->at(0).setPath(header);
_items->at(0).setTitle("...");
_items->at(0).setLastEditDate("");
if (_items[0].getPath().compare(NEXTCLOUD_ROOT_PATH) == 0)
_items.erase(_items.begin());
if (_items->at(0).getPath().compare(NEXTCLOUD_ROOT_PATH) == 0)
_items->erase(_items->begin());
return true;
}
@ -372,14 +377,13 @@ bool Nextcloud::getOfflineStructure(const string &pathUrl)
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;
}
}
return true;
}

View File

@ -15,6 +15,7 @@
#include <string>
#include <vector>
#include <memory>
using std::string;
using std::vector;
@ -60,7 +61,7 @@ public:
void setUsername(const string &Username);
void setPassword(const string &Pass);
vector<Item> getItems() { return _items; };
vector<Item> *getItems() { return _items.get(); };
bool isLoggedIn() { return _loggedIn; };
bool isWorkOffline() { return _workOffline; };
void switchWorkOffline() { _workOffline = !_workOffline; };
@ -70,7 +71,7 @@ public:
private:
static Nextcloud *nextcloudStatic;
vector<Item> _items;
std::shared_ptr<vector<Item>> _items = nullptr;
bool _loggedIn{false};
string _url;
bool _workOffline{false};