Skip to content
Snippets Groups Projects
Commit 61a88862 authored by Emmanuel Lepage Vallee's avatar Emmanuel Lepage Vallee
Browse files

dht: Implment the BootStrapModel

The list of bootstrap nodes used by OpenDHT to find peers

Refs #68053
parent f63e9b45
No related branches found
No related tags found
No related merge requests found
......@@ -172,6 +172,7 @@ SET( libringclient_LIB_SRCS
src/certificate.cpp
#Models
src/bootstrapmodel.cpp
src/accountmodel.cpp
src/availableaccountmodel.cpp
src/callmodel.cpp
......@@ -266,6 +267,7 @@ SET( libringclient_LIB_HDRS
src/callmodel.h
src/historymodel.h
src/person.h
src/bootstrapmodel.h
src/collectioninterface.h
src/collectioninterface.hpp
src/bookmarkmodel.h
......
......@@ -41,6 +41,7 @@
#include "credentialmodel.h"
#include "ciphermodel.h"
#include "protocolmodel.h"
#include "bootstrapmodel.h"
#include "accountstatusmodel.h"
#include "codecmodel.h"
#include "ringtonemodel.h"
......@@ -71,7 +72,7 @@ m_CurrentState(Account::EditState::READY),
m_pAccountNumber(nullptr),m_pKeyExchangeModel(nullptr),m_pSecurityValidationModel(nullptr),m_pTlsMethodModel(nullptr),
m_pCaCert(nullptr),m_pTlsCert(nullptr),m_pPrivateKey(nullptr),m_isLoaded(true),m_pCipherModel(nullptr),
m_pStatusModel(nullptr),m_LastTransportCode(0),m_RegistrationState(Account::RegistrationState::UNREGISTERED),
m_UseDefaultPort(false),m_pProtocolModel(nullptr)
m_UseDefaultPort(false),m_pProtocolModel(nullptr),m_pBootstrapModel(nullptr)
{
Q_Q(Account);
}
......@@ -393,6 +394,18 @@ ProtocolModel* Account::protocolModel() const
return d_ptr->m_pProtocolModel;
}
BootstrapModel* Account::bootstrapModel() const
{
if (protocol() != Account::Protocol::RING)
return nullptr;
if (!d_ptr->m_pBootstrapModel ) {
d_ptr->m_pBootstrapModel = new BootstrapModel(const_cast<Account*>(this));
}
return d_ptr->m_pBootstrapModel;
}
void Account::setAlias(const QString& detail)
{
const bool accChanged = detail != alias();
......
......@@ -40,6 +40,7 @@ class CipherModel ;
class AccountStatusModel ;
class ProtocolModel ;
class CodecModel ;
class BootstrapModel ;
//Private
class AccountPrivate;
......@@ -79,6 +80,7 @@ class LIB_EXPORT Account : public QObject {
friend class AccountStatusModel;
friend class TlsMethodModelPrivate;
friend class TlsMethodModel;
friend class BootstrapModelPrivate;
//Properties
Q_PROPERTY(QByteArray id READ id )
......@@ -250,6 +252,7 @@ class LIB_EXPORT Account : public QObject {
Q_INVOKABLE SecurityValidationModel* securityValidationModel() const;
Q_INVOKABLE TlsMethodModel* tlsMethodModel () const;
Q_INVOKABLE ProtocolModel* protocolModel () const;
Q_INVOKABLE BootstrapModel* bootstrapModel () const;
//Getters
QString hostname () const;
......
/****************************************************************************
* Copyright (C) 2015 by Savoir-Faire Linux *
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include "bootstrapmodel.h"
//Ring daemon
#include <account_const.h>
//Ring
#include <account.h>
#include <private/account_p.h>
class BootstrapModelPrivate
{
public:
struct Lines {
QString hostname;
int port ;
};
BootstrapModelPrivate(BootstrapModel* q,Account* a);
//Helper
bool save();
//Attributes
Account* m_pAccount;
QVector<Lines*> m_lines;
BootstrapModel* q_ptr;
};
BootstrapModelPrivate::BootstrapModelPrivate(BootstrapModel* q,Account* a) : q_ptr(q),m_pAccount(a)
{
}
bool BootstrapModelPrivate::save()
{
QString ret;
for(const Lines* l : m_lines) {
if (!l->hostname.trimmed().isEmpty()) {
if (ret.size())
ret += ';';
ret += l->hostname + (l->port > -1? ':'+QString::number(l->port):QString());
}
}
//Clear empty lines
bool val = true;
for(int i=0;i<m_lines.size();i++) {
Lines* l = m_lines[i];
if (l->hostname.isEmpty() && l->port == -1) {
q_ptr->beginRemoveRows(QModelIndex(),i,i);
m_lines.removeAll(l);
q_ptr->endRemoveRows();
val = false;
}
}
m_pAccount->d_ptr->setAccountProperty(DRing::Account::ConfProperties::HOSTNAME,ret);
return val;
}
BootstrapModel::BootstrapModel(Account* a) : QAbstractTableModel(a), d_ptr(new BootstrapModelPrivate(this,a))
{
for(const QString& line : d_ptr->m_pAccount->hostname().split(';')) {
const QStringList& fields = line.split(':');
if (line.size() && fields.size() && !(fields[0].isEmpty() && (fields.size()-1 && fields[1].isEmpty()))) {
BootstrapModelPrivate::Lines* l = new BootstrapModelPrivate::Lines();
l->hostname = fields[0].trimmed();
l->port = fields.size()>1?fields[1].toInt():-1; //-1 == default
d_ptr->m_lines << l;
}
}
}
BootstrapModel::~BootstrapModel()
{
delete d_ptr;
}
QHash<int,QByteArray> BootstrapModel::roleNames() const
{
static QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
/*static bool initRoles = false;
if (!initRoles) {
initRoles = true;
}*/
return roles;
}
bool BootstrapModel::setData( const QModelIndex& index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
BootstrapModelPrivate::Lines* l = index.row() < d_ptr->m_lines.size() ? d_ptr->m_lines[index.row()] : nullptr;
if (!(role == Qt::DisplayRole || role == Qt::EditRole))
return false;
if (!l) {
l = new BootstrapModelPrivate::Lines();
l->port = -1;
beginInsertRows(QModelIndex(),d_ptr->m_lines.size()+1,d_ptr->m_lines.size()+1);
d_ptr->m_lines << l;
endInsertRows();
}
switch (index.column()) {
case static_cast<int>(BootstrapModel::Columns::HOSTNAME):
l->hostname = value.toString();
if (d_ptr->save())
emit dataChanged(index,index);
break;
case static_cast<int>(BootstrapModel::Columns::PORT):
l->port = value.toInt();
if (l->port <= 0 || l->port > 65534)
l->port = -1;
if (d_ptr->save())
emit dataChanged(index,index);
break;
}
return true;
}
QVariant BootstrapModel::data( const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
BootstrapModelPrivate::Lines* l = index.row() < d_ptr->m_lines.size() ?d_ptr->m_lines[index.row()] : nullptr;
if (!l)
return QVariant();
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return index.column()?QVariant(l->port == -1?QVariant():l->port):QVariant(l->hostname);
};
return QVariant();
}
int BootstrapModel::rowCount( const QModelIndex& parent) const
{
return parent.isValid()?0:d_ptr->m_lines.size()+1; //Add one for new entries
}
int BootstrapModel::columnCount( const QModelIndex& parent) const
{
return parent.isValid()?0:2;
}
Qt::ItemFlags BootstrapModel::flags( const QModelIndex& index) const
{
return index.isValid() ? (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable) : Qt::NoItemFlags;
}
QModelIndex BootstrapModel::index( int row, int column, const QModelIndex& parent) const
{
return parent.isValid()? QModelIndex() : createIndex(row,column);
}
QVariant BootstrapModel::headerData( int section, Qt::Orientation ori, int role) const
{
if (role == Qt::DisplayRole) {
if (ori == Qt::Vertical)
return section;
switch (section) {
case static_cast<int>(BootstrapModel::Columns::HOSTNAME):
return tr("Hostname");
case static_cast<int>(BootstrapModel::Columns::PORT):
return tr("Port");
}
}
return QVariant();
}
/****************************************************************************
* Copyright (C) 2015 by Savoir-Faire Linux *
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef BOOTSTTRAP_MODEL_H
#define BOOTSTTRAP_MODEL_H
#include <QtCore/QAbstractTableModel>
#include <typedefs.h>
class Account;
class BootstrapModelPrivate;
class LIB_EXPORT BootstrapModel : public QAbstractTableModel
{
Q_OBJECT
public:
friend class Account;
enum class Columns {
HOSTNAME,
PORT,
};
virtual bool setData ( const QModelIndex& index, const QVariant &value, int role ) override;
virtual QVariant data ( const QModelIndex& index, int role = Qt::DisplayRole ) const override;
virtual int rowCount ( const QModelIndex& parent = QModelIndex() ) const override;
virtual Qt::ItemFlags flags ( const QModelIndex& index ) const override;
virtual int columnCount ( const QModelIndex& parent = QModelIndex() ) const override;
virtual QModelIndex index ( int row, int column, const QModelIndex& parent=QModelIndex()) const override;
virtual QVariant headerData ( int section, Qt::Orientation, int role = Qt::DisplayRole ) const override;
virtual QHash<int,QByteArray> roleNames() const override;
private:
BootstrapModel(Account* a);
virtual ~BootstrapModel();
BootstrapModelPrivate* d_ptr;
Q_DECLARE_PRIVATE(BootstrapModel)
};
#endif
\ No newline at end of file
......@@ -28,6 +28,7 @@ class CipherModel;
class AccountStatusModel;
class TlsMethodModel;
class ProtocolModel;
class BootstrapModel;
typedef void (AccountPrivate::*account_function)();
......@@ -52,6 +53,7 @@ public:
friend class AccountStatusModel;
friend class TlsMethodModelPrivate;
friend class TlsMethodModel;
friend class BootstrapModelPrivate;
//Constructor
explicit AccountPrivate(Account* acc);
......@@ -106,6 +108,7 @@ public:
SecurityValidationModel* m_pSecurityValidationModel;
TlsMethodModel* m_pTlsMethodModel ;
ProtocolModel* m_pProtocolModel ;
BootstrapModel* m_pBootstrapModel ;
Account::EditState m_CurrentState;
// State machines
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment