loginView added

pull/23/head
JuanJakobo 2020-08-26 17:26:21 +02:00
parent ef842c94f1
commit 0408821e46
8 changed files with 446 additions and 173 deletions

View File

@ -57,6 +57,7 @@ set(SOURCES ${CMAKE_SOURCE_DIR}/src/main.cpp
${CMAKE_SOURCE_DIR}/src/util/item.cpp
${CMAKE_SOURCE_DIR}/src/ui/listView.cpp
${CMAKE_SOURCE_DIR}/src/ui/listViewEntry.cpp
${CMAKE_SOURCE_DIR}/src/ui/loginView.cpp
${CMAKE_SOURCE_DIR}/src/util/util.cpp
${CMAKE_SOURCE_DIR}/src/util/log.cpp

View File

@ -13,85 +13,117 @@
#include "item.h"
#include <string>
#include <memory>
EventHandler * EventHandler::eventHandlerStatic;
using std::string;
EventHandler *EventHandler::_eventHandlerStatic;
EventHandler::EventHandler()
{
//create a event to create handlers
eventHandlerStatic = this;
_eventHandlerStatic = this;
menu = new MenuHandler("Nextcloud");
_menu = new MenuHandler("nextcloud");
_nextcloud = new Nextcloud();
_loginView = nullptr;
_listView = nullptr;
nextcloud = new Nextcloud();
//TODO SET USER, PASSWORD
nextcloud->login("USER", "PASSWORD");
listView = new ListView(menu->getContentRect(),nextcloud->getItems());
if (iv_access(NEXTCLOUD_CONFIG_PATH.c_str(), W_OK) == 0)
{
if (_nextcloud->login())
{
_listView = new ListView(_menu->getContentRect(), _nextcloud->getItems());
FullUpdate();
return;
}
}
_loginView = new LoginView(_menu->getContentRect());
_loginView->drawLoginView();
FullUpdate();
}
EventHandler::~EventHandler()
{
delete nextcloud;
delete menu;
delete listView;
delete _nextcloud;
delete _listView;
delete _menu;
}
int EventHandler::eventDistributor(int type, int par1, int par2)
int EventHandler::eventDistributor(const int type, const int par1, const int par2)
{
if (ISPOINTEREVENT(type))
return EventHandler::pointerHandler(type,par1,par2);
return EventHandler::pointerHandler(type, par1, par2);
return 0;
}
void EventHandler::mainMenuHandlerStatic(int index)
void EventHandler::mainMenuHandlerStatic(const int index)
{
eventHandlerStatic->mainMenuHandler(index);
_eventHandlerStatic->mainMenuHandler(index);
}
void EventHandler::mainMenuHandler(int index)
void EventHandler::mainMenuHandler(const int index)
{
switch(index)
{
//Exit
case 101:
CloseApp();
break;
default:
break;
}
}
int EventHandler::pointerHandler(int type, int par1, int par2)
{
if(type==EVT_POINTERDOWN)
switch (index)
{
if(IsInRect(par1,par2,menu->getMenuButtonRect())==1)
//Logout
case 101:
_nextcloud->logout();
delete _listView;
_listView = nullptr;
_loginView = new LoginView(_menu->getContentRect());
_loginView->drawLoginView();
FullUpdate();
break;
//Exit
case 102:
CloseApp();
break;
default:
break;
}
}
int EventHandler::pointerHandler(const int type, const int par1, const int par2)
{
if (type == EVT_POINTERDOWN)
{
if (IsInRect(par1, par2, _menu->getMenuButtonRect()) == 1)
{
return menu->createMenu(true,EventHandler::mainMenuHandlerStatic);
return _menu->createMenu(_nextcloud->isLoggedIn(), EventHandler::mainMenuHandlerStatic);
}
else if(listView!= NULL)
else if (_listView != nullptr)
{
int itemID = listView->listClicked(par1,par2);
if(itemID!=-1)
int itemID = _listView->listClicked(par1, par2);
if (itemID != -1)
{
string tempPath = nextcloud->getItems()[itemID].isClicked();
string tempPath = _nextcloud->getItems()[itemID].isClicked();
if(!tempPath.empty())
nextcloud->getDataStructure(tempPath);
if (!tempPath.empty())
_nextcloud->getDataStructure(tempPath);
delete listView;
listView = new ListView(menu->getContentRect(),nextcloud->getItems());
listView->drawHeader(tempPath.substr(NEXTCLOUD_ROOT_PATH.length()));
delete _listView;
_listView = new ListView(_menu->getContentRect(), _nextcloud->getItems());
_listView->drawHeader(tempPath.substr(NEXTCLOUD_ROOT_PATH.length()));
}
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;
}
else if (_loginView != nullptr)
{
if (_loginView->logginClicked(par1, par2) == 2)
{
if (_nextcloud->login(_loginView->getURL(), _loginView->getUsername(), _loginView->getPassword()))
{
_listView = new ListView(_menu->getContentRect(), _nextcloud->getItems());
delete _loginView;
FullUpdate();
}
return 1;
}
}
}
return 0;
}

View File

@ -12,24 +12,24 @@
#include "menuHandler.h"
#include "nextcloud.h"
#include "listView.h"
#include "loginView.h"
const string CONFIG_PATH = "/mnt/ext1/system/config/nextcloud";
class EventHandler
{
public:
/**
public:
/**
* Defines fonds, sets global Event Handler and starts new content
*/
EventHandler();
EventHandler();
/**
/**
* Destructor destroys pointer to game and menu
*/
~EventHandler();
~EventHandler();
/**
/**
* Handles events and redirects them
*
* @param type event type
@ -37,16 +37,17 @@ class EventHandler
* @param par2 second argument of the event
* @return int returns if the event was handled
*/
int eventDistributor(int type, int par1, int par2);
int eventDistributor(const int type, const int par1, const int par2);
private:
private:
static EventHandler *_eventHandlerStatic;
MenuHandler *_menu;
Nextcloud *_nextcloud;
ListView *_listView;
LoginView *_loginView;
static EventHandler *eventHandlerStatic;
MenuHandler *menu;
Nextcloud *nextcloud;
ListView* listView;
/**
/**
* Functions needed to call C function, redirects to real function
*
* @param type event type
@ -54,8 +55,8 @@ class EventHandler
* @param par2 second argument of the event
* @return int returns if the event was handled
*/
static void mainMenuHandlerStatic(int index);
/**
static void mainMenuHandlerStatic(const int index);
/**
* Handles menu events and redirects them
*
* @param type event type
@ -63,9 +64,9 @@ class EventHandler
* @param par2 second argument of the event
* @return int returns if the event was handled
*/
void mainMenuHandler(int index);
void mainMenuHandler(const int index);
/**
/**
* Handles pointer Events
*
* @param type event type
@ -73,6 +74,6 @@ class EventHandler
* @param par2 second argument of the event
* @return int returns if the event was handled
*/
int pointerHandler(int type, int par1, int par2);
int pointerHandler(const int type, const int par1, const int par2);
};
#endif

View File

@ -52,13 +52,15 @@ int MenuHandler::createMenu(bool loggedIn, iv_menuhandler handler)
imenu mainMenu[] =
{
{ITEM_HEADER, 0, "Menu", NULL},
//show logged in
{loggedIn ? ITEM_ACTIVE : ITEM_HIDDEN, 101, "Logout", NULL},
//show always
{ITEM_ACTIVE, 101, "Exit"},
{ITEM_ACTIVE, 102, "Exit"},
{0, 0, NULL, NULL}};
if (loggedIn)
{
mainMenu[1].type = ITEM_ACTIVE;
mainMenu[2].type = ITEM_ACTIVE;
}
else
{

View File

@ -0,0 +1,135 @@
//------------------------------------------------------------------
// loginView.cpp
//
// Author: JuanJakobo
// Date: 26.08.2020
//
//-------------------------------------------------------------------
#include "inkview.h"
#include "loginView.h"
#include "eventHandler.h"
#include <string>
using std::string;
LoginView *LoginView::_loginViewStatic;
LoginView::LoginView(irect *contentRect) : _contentRect(contentRect)
{
_loginViewStatic = this;
_loginFont = OpenFont("LiberationMono", 40, 1);
}
LoginView::~LoginView()
{
CloseFont(_loginFont);
}
void LoginView::drawLoginView()
{
FillAreaRect(_contentRect, WHITE);
_urlButton = iRect(50, 200, (ScreenWidth() - 50), 75, ALIGN_CENTER);
DrawLine(20, 275, (ScreenWidth() - 20), 275, BLACK);
SetFont(_loginFont, BLACK);
DrawTextRect2(&_urlButton, "Url");
_usernameButton = iRect(50, 400, ScreenWidth() - 50, 75, ALIGN_CENTER);
DrawLine(20, 475, (ScreenWidth() - 20), 475, BLACK);
SetFont(_loginFont, BLACK);
DrawTextRect2(&_usernameButton, "Username");
_passwordButton = iRect(50, 600, (ScreenWidth() - 50), 75, ALIGN_CENTER);
DrawLine(20, 675, (ScreenWidth() - 20), 675, BLACK);
SetFont(_loginFont, BLACK);
DrawTextRect2(&_passwordButton, "Password");
_loginButton = iRect(ScreenWidth() / 2 - (200 / 2), 700, 200, 50, ALIGN_CENTER);
FillAreaRect(&_loginButton, BLACK);
SetFont(_loginFont, WHITE);
DrawTextRect2(&_loginButton, "Login");
}
void LoginView::keyboardHandlerStatic(char *text)
{
_loginViewStatic->keyboardHandler(text);
}
void LoginView::keyboardHandler(char *text)
{
if (!text)
return;
string s(text);
if (s.empty())
return;
if (_test == 1)
{
_url = s.c_str();
FillAreaRect(&_urlButton, WHITE);
DrawTextRect2(&_urlButton, s.c_str());
}
else if (_test == 2)
{
_username = s.c_str();
FillAreaRect(&_usernameButton, WHITE);
DrawTextRect2(&_usernameButton, s.c_str());
}
else
{
_password = s.c_str();
FillAreaRect(&_passwordButton, WHITE);
string pass;
for (auto i = 0; i < s.length(); i++)
{
pass += "*";
}
DrawTextRect2(&_passwordButton, pass.c_str());
}
_charBuffer = NULL;
}
int LoginView::logginClicked(int x, int y)
{
_charBuffer = new char[4 * MAX_CHAR_BUFF_LENGHT + 1];
if (IsInRect(x, y, &_urlButton))
{
_test = 1;
OpenKeyboard("Url", _charBuffer, MAX_CHAR_BUFF_LENGHT - 1, KBD_NORMAL, &keyboardHandlerStatic);
return 1;
}
else if (IsInRect(x, y, &_usernameButton))
{
_test = 2;
OpenKeyboard("Username", _charBuffer, MAX_CHAR_BUFF_LENGHT - 1, KBD_NORMAL, &keyboardHandlerStatic);
return 1;
}
else if (IsInRect(x, y, &_passwordButton))
{
_test = 3;
OpenKeyboard("Password", _charBuffer, MAX_CHAR_BUFF_LENGHT - 1, KBD_PASSWORD, &keyboardHandlerStatic);
return 1;
}
else if (IsInRect(x, y, &_loginButton))
{
if (_username.empty() || _password.empty() || _url.empty())
{
Message(ICON_ERROR, "Error", "Please set url, username and password.", 600);
return 1;
}
return 2;
}
return 0;
}

57
src/ui/loginView.h 100644
View File

@ -0,0 +1,57 @@
//------------------------------------------------------------------
// loginView.h
//
// Author: JuanJakobo
// Date: 26.08.2020
// Description:
//-------------------------------------------------------------------
#ifndef LOGIN_SCREEN
#define LOGIN_SCREEN
#include "inkview.h"
#include <string>
using std::string;
const int MAX_CHAR_BUFF_LENGHT = 256;
class LoginView
{
public:
LoginView(irect *contentRect);
~LoginView();
void drawLoginView();
int logginClicked(int x, int y);
string getUsername() { return _username; };
string getPassword() { return _password; };
string getURL() { return _url; };
private:
static LoginView *_loginViewStatic;
ifont *_loginFont;
irect *_contentRect;
irect _urlButton;
irect _loginButton;
irect _usernameButton;
irect _passwordButton;
int _test;
string _username;
string _password;
string _url;
char *_charBuffer;
/**
* Functions needed to call C function, handles the panel
*
* @return void
*/
static void keyboardHandlerStatic(char *text);
void keyboardHandler(char *text);
};
#endif

View File

@ -14,98 +14,110 @@
#include <string>
#include <curl/curl.h>
using namespace std;
using std::string;
Nextcloud::Nextcloud()
{
loggedIn = false;
_loggedIn = false;
if(iv_access(NEXTCLOUD_PATH.c_str(), W_OK)!=0)
iv_mkdir(NEXTCLOUD_PATH.c_str(),0777);
if (iv_access(NEXTCLOUD_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_PATH.c_str(), 0777);
if(iv_access(NEXTCLOUD_FILE_PATH.c_str(),W_OK)!=0)
iv_mkdir(NEXTCLOUD_FILE_PATH.c_str(),0777);
if (iv_access(NEXTCLOUD_FILE_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_FILE_PATH.c_str(), 0777);
}
void Nextcloud::setUsername(const string& Username)
void Nextcloud::setURL(const string &Url)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(),temp);
WriteString(nextcloudConfig,"username",Username.c_str());
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteString(nextcloudConfig, "url", Url.c_str());
CloseConfig(nextcloudConfig);
}
void Nextcloud::setPassword(const string& Pass)
void Nextcloud::setUsername(const string &Username)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(),temp);
WriteSecret(nextcloudConfig,"password",Pass.c_str());
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteString(nextcloudConfig, "username", Username.c_str());
CloseConfig(nextcloudConfig);
}
string Nextcloud::getUsername()
void Nextcloud::setPassword(const string &Pass)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(),temp);
string user = ReadString(nextcloudConfig,"username","");
CloseConfigNoSave(nextcloudConfig);
return user;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteSecret(nextcloudConfig, "password", Pass.c_str());
CloseConfig(nextcloudConfig);
}
string Nextcloud::getPassword()
bool Nextcloud::login()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(),temp);
string pass = ReadSecret(nextcloudConfig,"password","");
CloseConfigNoSave(nextcloudConfig);
return pass;
}
bool Nextcloud::login(const string& Username, const string& Pass)
{
if(getDataStructure(NEXTCLOUD_ROOT_PATH + Username + "/",Username,Pass))
if (getDataStructure(NEXTCLOUD_ROOT_PATH + this->getUsername() + "/", this->getUsername(), this->getPassword()))
{
if(iv_access(NEXTCLOUD_CONFIG_PATH.c_str(), W_OK)!=0)
_loggedIn = true;
return true;
}
return false;
}
bool Nextcloud::login(const string &Url, const string &Username, const string &Pass)
{
_url = Url;
if (getDataStructure(NEXTCLOUD_ROOT_PATH + Username + "/", Username, Pass))
{
if (iv_access(NEXTCLOUD_CONFIG_PATH.c_str(), W_OK) != 0)
iv_buildpath(NEXTCLOUD_CONFIG_PATH.c_str());
this->setUsername(Username);
this->setPassword(Pass);
loggedIn = true;
this->setURL(_url);
_loggedIn = true;
return true;
}
return false;
}
bool Nextcloud::getDataStructure(string& pathUrl)
void Nextcloud::logout()
{
return getDataStructure(pathUrl,this->getUsername(),this->getPassword());
remove(NEXTCLOUD_CONFIG_PATH.c_str());
_url.clear();
_loggedIn = false;
}
bool Nextcloud::getDataStructure(const string& pathUrl, const string& Username, const string& Pass)
bool Nextcloud::getDataStructure(string &pathUrl)
{
if(!Util::connectToNetwork())
return getDataStructure(pathUrl, this->getUsername(), this->getPassword());
}
bool Nextcloud::getDataStructure(const string &pathUrl, const string &Username, const string &Pass)
{
if (!Util::connectToNetwork())
return false;
if(Username.empty() || Pass.empty())
if (_url.empty())
_url = this->getUrl();
if (Username.empty() || Pass.empty())
{
Message(ICON_ERROR, "Error", "Username/password not set.", 1200);
return false;
}
items.clear();
_items.clear();
string readBuffer;
CURLcode res;
CURL *curl = curl_easy_init();
if(curl)
if (curl)
{
string post = Username + ":" + Pass;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Depth: 1");
curl_easy_setopt(curl, CURLOPT_URL, (NEXTCLOUD_URL + pathUrl).c_str());
curl_easy_setopt(curl, CURLOPT_URL, (_url + pathUrl).c_str());
curl_easy_setopt(curl, CURLOPT_USERPWD, post.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER,headers);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PROPFIND");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Util::writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@ -113,7 +125,7 @@ bool Nextcloud::getDataStructure(const string& pathUrl, const string& Username,
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res == CURLE_OK)
if (res == CURLE_OK)
{
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
@ -121,48 +133,75 @@ bool Nextcloud::getDataStructure(const string& pathUrl, const string& Username,
switch (response_code)
{
case 207:
{
size_t begin;
size_t end;
string beginItem = "<d:response>";
string endItem = "</d:response>";
begin = readBuffer.find(beginItem);
while (begin != std::string::npos)
{
size_t begin;
size_t end;
string beginItem = "<d:response>";
string endItem = "</d:response>";
end = readBuffer.find(endItem);
this->_items.push_back(Item(readBuffer.substr(begin, end)));
readBuffer = readBuffer.substr(end + endItem.length());
begin = readBuffer.find(beginItem);
while(begin!=std::string::npos)
{
end = readBuffer.find(endItem);
this->items.push_back(Item(readBuffer.substr(begin,end)));
readBuffer = readBuffer.substr(end+endItem.length());
begin = readBuffer.find(beginItem);
}
if(items.size() < 1)
return false;
string tes = items[0].getPath();
tes = tes.substr(0,tes.find_last_of("/"));
tes = tes.substr(0,tes.find_last_of("/")+1);
items[0].setPath(tes);
items[0].setTitle("...");
if(items[0].getPath().compare(NEXTCLOUD_ROOT_PATH) == 0)
items.erase(items.begin());
return true;
break;
}
if (_items.size() < 1)
return false;
string tes = _items[0].getPath();
tes = tes.substr(0, tes.find_last_of("/"));
tes = tes.substr(0, tes.find_last_of("/") + 1);
_items[0].setPath(tes);
_items[0].setTitle("...");
if (_items[0].getPath().compare(NEXTCLOUD_ROOT_PATH) == 0)
_items.erase(_items.begin());
return true;
break;
}
case 401:
Message(ICON_ERROR, "Error","Username/password incorrect.", 1200);
Message(ICON_ERROR, "Error", "Username/password incorrect.", 1200);
break;
default:
Message(ICON_ERROR,"Error","An unknown error occured." + response_code,1200);
Message(ICON_ERROR, "Error", "An unknown error occured." + response_code, 1200);
break;
}
}
}
return false;
}
string Nextcloud::getUrl()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string url = ReadString(nextcloudConfig, "url", "");
CloseConfigNoSave(nextcloudConfig);
return url;
}
string Nextcloud::getUsername()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string user = ReadString(nextcloudConfig, "username", "");
CloseConfigNoSave(nextcloudConfig);
return user;
}
string Nextcloud::getPassword()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string pass = ReadSecret(nextcloudConfig, "password", "");
CloseConfigNoSave(nextcloudConfig);
return pass;
}

View File

@ -16,43 +16,54 @@
#include <string>
#include <vector>
using namespace std;
using std::string;
using std::vector;
const string NEXTCLOUD_PATH = "/mnt/ext1/system/config/nextcloud";
const string NEXTCLOUD_CONFIG_PATH = NEXTCLOUD_PATH + "/nextcloud.cfg";
const string NEXTCLOUD_FILE_PATH = "/mnt/ext1/nextcloud/";
const string NEXTCLOUD_URL = "https://cloud.jjohannssen.de";
const string NEXTCLOUD_ROOT_PATH = "/remote.php/dav/files/";
class Nextcloud {
public:
class Nextcloud
{
public:
explicit Nextcloud();
explicit Nextcloud();
void setUsername(const string& Username);
void setPassword(const string& Pass);
vector<Item> &getItems(){return items;};
bool isLoggedIn(){return loggedIn;};
/**
/**
* Handles first login to nextcloud, if sucessfull saves userdata
*
* @param Username the username of the Nextcloud instance
* @param Pass the pass of the Nextcloud instance
* @return true - sucessfull login, false - failed login
*/
bool login(const string& Username, const string& Pass);
bool login(const string &Url, const string &Username, const string &Pass);
/**
bool login();
void logout();
/**
* gets the dataStructure of the given URL and writes its WEBDAV items to the items vector, reads Userdata from configfile
*
* @param pathUrl URL to get the dataStructure of
* @return true - sucessfull, false - error
*/
bool getDataStructure(string& pathUrl);
bool getDataStructure(string &pathUrl);
/**
void setURL(const string &Url);
void setUsername(const string &Username);
void setPassword(const string &Pass);
vector<Item> getItems() { return _items; };
bool isLoggedIn() { return _loggedIn; };
private:
vector<Item> _items;
bool _loggedIn;
string _url;
//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
*
* @param pathUrl URL to get the dataStructure of
@ -60,16 +71,11 @@ class Nextcloud {
* @param Pass the pass of the Nextcloud instance
* @return true - sucessfull, false - error
*/
bool getDataStructure(const string& pathUrl, const string& Username, const string& Pass);
bool getDataStructure(const string &pathUrl, const string &Username, const string &Pass);
private:
vector<Item> items;
bool loggedIn;
string getUsername();
string getPassword();
string getUsername();
string getPassword();
string getUrl();
};
#endif