diff --git a/CMakeLists.txt b/CMakeLists.txt index 43ce104a534376d1e029310bcc7384a57e75fd31..f0c0721dba0338e829adacaad11e77eae961e0e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -190,6 +190,7 @@ SET( libringclient_LIB_SRCS src/numbercategorymodel.cpp src/keyexchangemodel.cpp src/tlsmethodmodel.cpp + src/protocolmodel.cpp src/numbercompletionmodel.cpp src/profilemodel.cpp src/ringtonemodel.cpp @@ -270,6 +271,7 @@ SET( libringclient_LIB_HDRS src/numbercategorymodel.h src/keyexchangemodel.h src/tlsmethodmodel.h + src/protocolmodel.h src/numbercompletionmodel.h src/profilemodel.h src/numbercategory.h diff --git a/src/account.cpp b/src/account.cpp index f97bd42504094f011d893606668baf7c6e81bb49..3ce0a9cb12ebf99555524db20c4b74d8c1373b44 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -40,6 +40,7 @@ #include "private/accountmodel_p.h" #include "credentialmodel.h" #include "ciphermodel.h" +#include "protocolmodel.h" #include "accountstatusmodel.h" #include "audio/codecmodel.h" #include "video/codecmodel2.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_UseDefaultPort(false),m_pProtocolModel(nullptr) { Q_Q(Account); } @@ -392,6 +393,14 @@ TlsMethodModel* Account::tlsMethodModel() const return d_ptr->m_pTlsMethodModel; } +ProtocolModel* Account::protocolModel() const +{ + if (!d_ptr->m_pProtocolModel ) { + d_ptr->m_pProtocolModel = new ProtocolModel(const_cast<Account*>(this)); + } + return d_ptr->m_pProtocolModel; +} + void Account::setAlias(const QString& detail) { const bool accChanged = detail != alias(); @@ -447,7 +456,7 @@ QString Account::password() const case Account::Protocol::IAX: return d_ptr->accountDetail(DRing::Account::ConfProperties::PASSWORD); break; - case Account::Protocol::DHT: + case Account::Protocol::RING: return tlsPassword(); break; case Account::Protocol::COUNT__: @@ -667,7 +676,7 @@ int Account::localPort() const return d_ptr->accountDetail(DRing::Account::ConfProperties::TLS::LISTENER_PORT).toInt(); else return d_ptr->accountDetail(DRing::Account::ConfProperties::LOCAL_PORT).toInt(); - case Account::Protocol::DHT: + case Account::Protocol::RING: return d_ptr->accountDetail(DRing::Account::ConfProperties::TLS::LISTENER_PORT).toInt(); case Account::Protocol::COUNT__: break; @@ -697,12 +706,13 @@ Account::RegistrationState Account::registrationState() const Account::Protocol Account::protocol() const { const QString str = d_ptr->accountDetail(DRing::Account::ConfProperties::TYPE); + if (str.isEmpty() || str == DRing::Account::ProtocolNames::SIP) return Account::Protocol::SIP; else if (str == DRing::Account::ProtocolNames::IAX) return Account::Protocol::IAX; - else if (str == DRing::Account::ProtocolNames::DHT) - return Account::Protocol::DHT; + else if (str == DRing::Account::ProtocolNames::RING) + return Account::Protocol::RING; qDebug() << "Warning: unhandled protocol name" << str << ", defaulting to SIP"; return Account::Protocol::SIP; } @@ -938,13 +948,13 @@ void Account::setProtocol(Account::Protocol proto) //TODO prevent this if the protocol has been saved switch (proto) { case Account::Protocol::SIP: - d_ptr->setAccountProperty(DRing::Account::ConfProperties::TYPE ,DRing::Account::ProtocolNames::SIP); + d_ptr->setAccountProperty(DRing::Account::ConfProperties::TYPE ,DRing::Account::ProtocolNames::SIP ); break; case Account::Protocol::IAX: - d_ptr->setAccountProperty(DRing::Account::ConfProperties::TYPE ,DRing::Account::ProtocolNames::IAX); + d_ptr->setAccountProperty(DRing::Account::ConfProperties::TYPE ,DRing::Account::ProtocolNames::IAX ); break; - case Account::Protocol::DHT: - d_ptr->setAccountProperty(DRing::Account::ConfProperties::TYPE ,DRing::Account::ProtocolNames::DHT); + case Account::Protocol::RING: + d_ptr->setAccountProperty(DRing::Account::ConfProperties::TYPE ,DRing::Account::ProtocolNames::RING); break; case Account::Protocol::COUNT__: break; @@ -993,7 +1003,7 @@ void Account::setPassword(const QString& detail) case Account::Protocol::IAX: d_ptr->setAccountProperty(DRing::Account::ConfProperties::PASSWORD, detail); break; - case Account::Protocol::DHT: + case Account::Protocol::RING: setTlsPassword(detail); case Account::Protocol::COUNT__: break; @@ -1103,7 +1113,7 @@ void Account::setLocalPort(unsigned short detail) d_ptr->setAccountProperty(DRing::Account::ConfProperties::TLS::LISTENER_PORT, QString::number(detail)); else d_ptr->setAccountProperty(DRing::Account::ConfProperties::LOCAL_PORT, QString::number(detail)); - case Account::Protocol::DHT: + case Account::Protocol::RING: d_ptr->setAccountProperty(DRing::Account::ConfProperties::TLS::LISTENER_PORT, QString::number(detail)); case Account::Protocol::COUNT__: break; @@ -1280,7 +1290,7 @@ void Account::setUseDefaultPort(bool value) case Account::Protocol::IAX: setLocalPort(5060); break; - case Account::Protocol::DHT: + case Account::Protocol::RING: setLocalPort(5061); break; case Account::Protocol::COUNT__: diff --git a/src/account.h b/src/account.h index 7f21fc3cbf94bd0f33b4ec0c5fefe832bdd2be0d..af78c492965075d3fd1a7e0903177a1263f9bff1 100644 --- a/src/account.h +++ b/src/account.h @@ -38,6 +38,7 @@ class SecurityValidationModel; class Certificate ; class CipherModel ; class AccountStatusModel ; +class ProtocolModel ; namespace Audio { class CodecModel; @@ -76,6 +77,7 @@ class LIB_EXPORT Account : public QObject { friend class TlsMethodModel; //Properties + Q_PROPERTY(QByteArray id READ id ) Q_PROPERTY(QString alias READ alias WRITE setAlias ) Q_PROPERTY(Account::Protocol protocol READ protocol WRITE setProtocol ) Q_PROPERTY(QString hostname READ hostname WRITE setHostname ) @@ -210,9 +212,9 @@ class LIB_EXPORT Account : public QObject { }; enum class Protocol { - SIP = 0, - IAX = 1, - DHT = 2, + SIP = 0, + IAX = 1, + RING = 2, COUNT__, }; Q_ENUMS(Protocol) @@ -243,6 +245,7 @@ class LIB_EXPORT Account : public QObject { Q_INVOKABLE AccountStatusModel* statusModel () const; Q_INVOKABLE SecurityValidationModel* securityValidationModel() const; Q_INVOKABLE TlsMethodModel* tlsMethodModel () const; + Q_INVOKABLE ProtocolModel* protocolModel () const; //Getters QString hostname () const; @@ -298,7 +301,7 @@ class LIB_EXPORT Account : public QObject { QString userAgent () const; bool useDefaultPort () const; RegistrationState registrationState () const; - Account::Protocol protocol () const; + Protocol protocol () const; KeyExchangeModel::Type keyExchange () const; QVariant roleData (int role) const; diff --git a/src/private/account_p.h b/src/private/account_p.h index ddc94a76e3b07fa0acca2ced13a16da52f1810fb..98649a0f2e4e9bbbf5dc9bf7281b8199b38b6319 100644 --- a/src/private/account_p.h +++ b/src/private/account_p.h @@ -27,6 +27,7 @@ class ContactMethod; class CipherModel; class AccountStatusModel; class TlsMethodModel; +class ProtocolModel; typedef void (AccountPrivate::*account_function)(); @@ -105,6 +106,7 @@ public: AccountStatusModel* m_pStatusModel ; SecurityValidationModel* m_pSecurityValidationModel; TlsMethodModel* m_pTlsMethodModel ; + ProtocolModel* m_pProtocolModel ; Account::EditState m_CurrentState; // State machines diff --git a/src/protocolmodel.cpp b/src/protocolmodel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cb24c14cad5227f64d1e591d0ffd032580adb5a --- /dev/null +++ b/src/protocolmodel.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** + * 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 "protocolmodel.h" + +//Qt +#include <QtCore/QCoreApplication> +#include <QtCore/QItemSelectionModel> + +//Ring daemon +#include <account_const.h> + +//Ring +#include <account.h> +#include <private/account_p.h> + +class ProtocolModelPrivate : public QObject { + Q_OBJECT +public: + + ProtocolModelPrivate(Account* a); + + mutable QItemSelectionModel* m_pSelectionModel; + Account* m_pAccount; + + //Constants + struct ToolTips { + static const QString RING_ACCOUNT_TOOLTIP; + static const QString SIP_ACCOUNT_TOOLTIP ; + static const QString IAX2_ACCOUNT_TOOLTIP; + }; + +public Q_SLOTS: + void slotSelectionChanged(const QModelIndex& idx); +}; + +const QString ProtocolModelPrivate::ToolTips::RING_ACCOUNT_TOOLTIP = QObject::tr("Ring Account"); +const QString ProtocolModelPrivate::ToolTips::SIP_ACCOUNT_TOOLTIP = QObject::tr("SIP Account" ); +const QString ProtocolModelPrivate::ToolTips::IAX2_ACCOUNT_TOOLTIP = QObject::tr("IAX2 Account"); + + +ProtocolModelPrivate::ProtocolModelPrivate(Account* a) : m_pSelectionModel(nullptr), m_pAccount(a) +{ + +} + +ProtocolModel::ProtocolModel(Account* a) : QAbstractListModel(QCoreApplication::instance()), +d_ptr(new ProtocolModelPrivate(a)) +{ + +} + +QHash<int,QByteArray> ProtocolModel::roleNames() const +{ + static QHash<int, QByteArray> roles = QAbstractItemModel::roleNames(); + /*static bool initRoles = false; + if (!initRoles) { + initRoles = true; + + }*/ + return roles; +} + +//Model functions +QVariant ProtocolModel::data( const QModelIndex& index, int role) const +{ + if (!index.isValid()) return QVariant(); + Account::Protocol proto = static_cast<Account::Protocol>(index.row()); + if (role == Qt::DisplayRole) { + switch (proto) { + case Account::Protocol::SIP : + return DRing::Account::ProtocolNames::SIP; + case Account::Protocol::IAX : + return DRing::Account::ProtocolNames::IAX; + case Account::Protocol::RING : + return DRing::Account::ProtocolNames::RING; + case Account::Protocol::COUNT__: + break; + }; + } + return QVariant(); +} + +int ProtocolModel::rowCount( const QModelIndex& parent ) const +{ + return parent.isValid()?0:enum_class_size<Account::Protocol>(); +} + +Qt::ItemFlags ProtocolModel::flags( const QModelIndex& index ) const +{ + const bool isNew = d_ptr->m_pAccount->isNew(); + if (!index.isValid() || (!isNew)) return Qt::NoItemFlags; + + //Account type cannot be changed, the daemon doesn't support that and crash + //it was considered a client responsibility to disallow it. It is not worth + //fixing + return Qt::ItemIsEnabled|Qt::ItemIsSelectable; +} + +bool ProtocolModel::setData( const QModelIndex& index, const QVariant &value, int role) +{ + Q_UNUSED(index) + Q_UNUSED(value) + Q_UNUSED(role ) + return false; +} + +QItemSelectionModel* ProtocolModel::selectionModel() const +{ + if (!d_ptr->m_pSelectionModel) { + d_ptr->m_pSelectionModel = new QItemSelectionModel(const_cast<ProtocolModel*>(this)); + + const Account::Protocol proto = d_ptr->m_pAccount->protocol(); + + const QModelIndex& idx = index(static_cast<int>(proto),0); + d_ptr->m_pSelectionModel->setCurrentIndex(idx,QItemSelectionModel::ClearAndSelect); + + connect(d_ptr->m_pSelectionModel,&QItemSelectionModel::currentChanged,d_ptr,&ProtocolModelPrivate::slotSelectionChanged); + } + + return d_ptr->m_pSelectionModel; +} + +void ProtocolModelPrivate::slotSelectionChanged(const QModelIndex& idx) +{ + if (!idx.isValid()) + return; + + m_pAccount->setProtocol(static_cast<Account::Protocol>(idx.row())); +} + +#include <protocolmodel.moc> diff --git a/src/protocolmodel.h b/src/protocolmodel.h new file mode 100644 index 0000000000000000000000000000000000000000..ecfdccfebf03c84a770760f375c290792ddc925b --- /dev/null +++ b/src/protocolmodel.h @@ -0,0 +1,62 @@ +/**************************************************************************** + * 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 PROTOCOLMODEL_H +#define PROTOCOLMODEL_H + +#include "typedefs.h" +#include <QtCore/QAbstractListModel> + +//Qt +class QItemSelectionModel; + +//Ring +class ProtocolModelPrivate; +class Account; + +/** + * This model is used to select the account protocol when creating a new account. + * It then become a simple readonly model. + */ +class LIB_EXPORT ProtocolModel : public QAbstractListModel { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" + Q_OBJECT + #pragma GCC diagnostic pop + +public: + + //Private constructor, can only be called by 'Account' + explicit ProtocolModel(Account* a); + + //Model functions + 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 bool setData ( const QModelIndex& index, const QVariant &value, int role) override; + virtual QHash<int,QByteArray> roleNames() const override; + + //Getters + QItemSelectionModel* selectionModel() const; + +private: + ProtocolModelPrivate* d_ptr; + Q_DECLARE_PRIVATE(ProtocolModel) + +}; +Q_DECLARE_METATYPE(ProtocolModel*) +#endif