Impelemnt recent list

pull/53/head
michal.szwaj 2018-03-25 01:29:36 +01:00
parent 42c15ec009
commit 258a6a3c59
5 changed files with 248 additions and 9 deletions

View File

@ -0,0 +1,46 @@
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <deque>
#include <string>
namespace f1x
{
namespace openauto
{
namespace autoapp
{
namespace configuration
{
class IRecentAddressesList
{
public:
typedef std::deque<std::string> RecentAddresses;
virtual void read() = 0;
virtual void insertAddress(const std::string& address) = 0;
virtual RecentAddresses getList() const = 0;
};
}
}
}
}

View File

@ -0,0 +1,57 @@
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <deque>
#include <f1x/openauto/autoapp/Configuration/IRecentAddressesList.hpp>
namespace f1x
{
namespace openauto
{
namespace autoapp
{
namespace configuration
{
class RecentAddressesList: public IRecentAddressesList
{
public:
RecentAddressesList(size_t maxListSize);
void read() override;
void insertAddress(const std::string& address) override;
RecentAddresses getList() const override;
private:
void load();
void save();
size_t maxListSize_;
RecentAddresses list_;
static const std::string cConfigFileName;
static const std::string cRecentEntiresCount;
static const std::string cRecentEntryPrefix;
};
}
}
}
}

View File

@ -1,8 +1,10 @@
#pragma once
#include <QDialog>
#include <QStringListModel>
#include <f1x/aasdk/TCP/ITCPEndpoint.hpp>
#include <f1x/aasdk/TCP/ITCPWrapper.hpp>
#include <f1x/openauto/autoapp/Configuration/IRecentAddressesList.hpp>
namespace Ui {
class ConnectDialog;
@ -22,25 +24,31 @@ class ConnectDialog : public QDialog
Q_OBJECT
public:
explicit ConnectDialog(boost::asio::io_service& ioService, aasdk::tcp::ITCPWrapper& tcpWrapper, QWidget *parent = nullptr);
explicit ConnectDialog(boost::asio::io_service& ioService, aasdk::tcp::ITCPWrapper& tcpWrapper, openauto::autoapp::configuration::IRecentAddressesList& recentAddressesList, QWidget *parent = nullptr);
~ConnectDialog() override;
signals:
void connectToDevice(const QString& ipAddress);
void connectionSucceed(aasdk::tcp::ITCPEndpoint::SocketPointer socket);
void connectionSucceed(aasdk::tcp::ITCPEndpoint::SocketPointer socket, std::string ipAddress);
void connectionFailed(const QString& message);
private slots:
void onConnectButtonClicked();
void onConnectionFailed(const QString& message);
void onConnectionSucceed();
void onConnectionSucceed(aasdk::tcp::ITCPEndpoint::SocketPointer socket, std::string ipAddress);
void setControlsEnabledStatus(bool status);
void connectHandler(const boost::system::error_code& ec, aasdk::tcp::ITCPEndpoint::SocketPointer socket);
void connectHandler(const boost::system::error_code& ec, std::string ipAddress, aasdk::tcp::ITCPEndpoint::SocketPointer socket);
private:
void insertIpAddress(std::string ipAddress);
void loadRecentList();
boost::asio::io_service& ioService_;
aasdk::tcp::ITCPWrapper& tcpWrapper_;
openauto::autoapp::configuration::IRecentAddressesList& recentAddressesList_;
Ui::ConnectDialog *ui_;
QStringListModel recentAddressesModel_;
QStringList recentAddressesModelList_;
};
}

View File

@ -0,0 +1,106 @@
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/property_tree/ini_parser.hpp>
#include <f1x/openauto/Common/Log.hpp>
#include <f1x/openauto/autoapp/Configuration/RecentAddressesList.hpp>
namespace f1x
{
namespace openauto
{
namespace autoapp
{
namespace configuration
{
const std::string RecentAddressesList::cConfigFileName = "openauto_wifi_recent.ini";
const std::string RecentAddressesList::cRecentEntiresCount = "Recent.EntiresCount";
const std::string RecentAddressesList::cRecentEntryPrefix = "Recent.Entry_";
RecentAddressesList::RecentAddressesList(size_t maxListSize)
: maxListSize_(maxListSize)
{
}
void RecentAddressesList::read()
{
this->load();
}
void RecentAddressesList::insertAddress(const std::string& address)
{
list_.push_front(address);
this->save();
}
RecentAddressesList::RecentAddresses RecentAddressesList::getList() const
{
return list_;
}
void RecentAddressesList::load()
{
boost::property_tree::ptree iniConfig;
try
{
boost::property_tree::ini_parser::read_ini(cConfigFileName, iniConfig);
const auto listSize = std::min(maxListSize_, iniConfig.get<size_t>(cRecentEntiresCount, 0));
for(size_t i = 0; i < listSize; ++i)
{
const auto key = cRecentEntryPrefix + std::to_string(i);
const auto address = iniConfig.get<RecentAddresses::value_type>(key, RecentAddresses::value_type());
if(!address.empty())
{
list_.push_back(address);
}
}
}
catch(const boost::property_tree::ini_parser_error& e)
{
OPENAUTO_LOG(warning) << "[RecentAddressesList] failed to read configuration file: " << cConfigFileName
<< ", error: " << e.what()
<< ". Empty list will be used.";
}
}
void RecentAddressesList::save()
{
boost::property_tree::ptree iniConfig;
const auto entiresCount = std::min(maxListSize_, list_.size());
iniConfig.put<size_t>(cRecentEntiresCount, entiresCount);
for(size_t i = 0; i < entiresCount; ++i)
{
const auto key = cRecentEntryPrefix + std::to_string(i);
iniConfig.put<RecentAddresses::value_type>(key, list_.at(i));
}
boost::property_tree::ini_parser::write_ini(cConfigFileName, iniConfig);
}
}
}
}
}

View File

@ -11,10 +11,11 @@ namespace autoapp
namespace ui
{
ConnectDialog::ConnectDialog(boost::asio::io_service& ioService, aasdk::tcp::ITCPWrapper& tcpWrapper, QWidget *parent)
ConnectDialog::ConnectDialog(boost::asio::io_service& ioService, aasdk::tcp::ITCPWrapper& tcpWrapper, openauto::autoapp::configuration::IRecentAddressesList& recentAddressesList, QWidget *parent)
: QDialog(parent)
, ioService_(ioService)
, tcpWrapper_(tcpWrapper)
, recentAddressesList_(recentAddressesList)
, ui_(new Ui::ConnectDialog)
{
qRegisterMetaType<aasdk::tcp::ITCPEndpoint::SocketPointer>("aasdk::tcp::ITCPEndpoint::SocketPointer");
@ -24,6 +25,9 @@ ConnectDialog::ConnectDialog(boost::asio::io_service& ioService, aasdk::tcp::ITC
connect(ui_->pushButtonConnect, &QPushButton::clicked, this, &ConnectDialog::onConnectButtonClicked);
connect(this, &ConnectDialog::connectionSucceed, this, &ConnectDialog::onConnectionSucceed);
connect(this, &ConnectDialog::connectionFailed, this, &ConnectDialog::onConnectionFailed);
recentAddressesModel_.setStringList(recentAddressesModelList_);
ui_->listViewRecent->setModel(&recentAddressesModel_);
}
ConnectDialog::~ConnectDialog()
@ -40,7 +44,7 @@ void ConnectDialog::onConnectButtonClicked()
try
{
tcpWrapper_.asyncConnect(*socket, ipAddress, 5277, std::bind(&ConnectDialog::connectHandler, this, std::placeholders::_1, socket));
tcpWrapper_.asyncConnect(*socket, ipAddress, 5277, std::bind(&ConnectDialog::connectHandler, this, std::placeholders::_1, ipAddress, socket));
}
catch(const boost::system::system_error& se)
{
@ -48,11 +52,11 @@ void ConnectDialog::onConnectButtonClicked()
}
}
void ConnectDialog::connectHandler(const boost::system::error_code& ec, aasdk::tcp::ITCPEndpoint::SocketPointer socket)
void ConnectDialog::connectHandler(const boost::system::error_code& ec, std::string ipAddress, aasdk::tcp::ITCPEndpoint::SocketPointer socket)
{
if(!ec)
{
emit connectionSucceed(std::move(socket));
emit connectionSucceed(std::move(socket), ipAddress);
this->close();
}
else
@ -61,8 +65,9 @@ void ConnectDialog::connectHandler(const boost::system::error_code& ec, aasdk::t
}
}
void ConnectDialog::onConnectionSucceed()
void ConnectDialog::onConnectionSucceed(aasdk::tcp::ITCPEndpoint::SocketPointer, std::string ipAddress)
{
this->insertIpAddress(ipAddress);
this->setControlsEnabledStatus(true);
}
@ -83,6 +88,23 @@ void ConnectDialog::setControlsEnabledStatus(bool status)
ui_->listViewRecent->setEnabled(status);
}
void ConnectDialog::loadRecentList()
{
recentAddressesModelList_.clear();
const auto& configList = recentAddressesList_.getList();
for(const auto& element : configList)
{
recentAddressesModelList_.append(QString::fromStdString(element));
}
}
void ConnectDialog::insertIpAddress(std::string ipAddress)
{
recentAddressesList_.insertAddress(ipAddress);
this->loadRecentList();
}
}
}
}