diff --git a/src/account.cpp b/src/account.cpp index 90bd71cfc53bd009b6149129c5f945bde0b05fc8..74a9b94657239b7e425531d207ab70e05d876816 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -107,7 +107,7 @@ Account* AccountPrivate::buildExistingAccountFromId(const QByteArray& _accountId } //buildExistingAccountFromId ///Build an account from it's name / alias -Account* AccountPrivate::buildNewAccountFromAlias(const QString& alias) +Account* AccountPrivate::buildNewAccountFromAlias(Account::Protocol proto, const QString& alias) { qDebug() << "Building an account from alias: " << alias; ConfigurationManagerInterface& configurationManager = DBus::ConfigurationManager::instance(); diff --git a/src/account.h b/src/account.h index 0bb4f8b9a2ae5462b144ba4e29b247992824f64d..4ea0b84242cc779389bd61d3543ad4a5c1e65cb6 100644 --- a/src/account.h +++ b/src/account.h @@ -413,6 +413,7 @@ class LIB_EXPORT Account : public QObject { Q_DECLARE_METATYPE(Account*) Q_DECLARE_METATYPE(Account::RegistrationState) Q_DECLARE_METATYPE(Account::EditAction) +Q_DECLARE_METATYPE(Account::Protocol) Account* operator<<(Account* a, Account::EditAction action); diff --git a/src/accountmodel.cpp b/src/accountmodel.cpp index 673996c2137b5b810e9d8ae7873bb622e1675286..1bcee85f16fc49fa358a80a6d794a2a79bb506d0 100644 --- a/src/accountmodel.cpp +++ b/src/accountmodel.cpp @@ -29,6 +29,7 @@ //Ring library #include "account.h" #include "profilemodel.h" +#include "protocolmodel.h" #include "private/account_p.h" #include "private/accountmodel_p.h" #include "accountstatusmodel.h" @@ -40,7 +41,7 @@ QHash<QByteArray,AccountPlaceHolder*> AccountModelPrivate::m_hsPlaceHolder; AccountModel* AccountModelPrivate::m_spAccountList; AccountModelPrivate::AccountModelPrivate(AccountModel* parent) : QObject(parent),q_ptr(parent), -m_pIP2IP(nullptr) +m_pIP2IP(nullptr),m_pProtocolModel(nullptr) { } @@ -571,16 +572,23 @@ bool AccountModel::isPresenceSubscribeSupported() const } +ProtocolModel* AccountModel::protocolModel() const +{ + if (!d_ptr->m_pProtocolModel) + d_ptr->m_pProtocolModel = new ProtocolModel(); + return d_ptr->m_pProtocolModel; +} + + /***************************************************************************** * * * Setters * * * ****************************************************************************/ -///Add an account -Account* AccountModel::add(const QString& alias) +Account* AccountModel::add(const QString& alias, const Account::Protocol proto) { - Account* a = AccountPrivate::buildNewAccountFromAlias(alias); + Account* a = AccountPrivate::buildNewAccountFromAlias(proto,alias); connect(a,SIGNAL(changed(Account*)),d_ptr,SLOT(slotAccountChanged(Account*))); beginInsertRows(QModelIndex(),d_ptr->m_lAccounts.size(),d_ptr->m_lAccounts.size()); d_ptr->m_lAccounts += a; @@ -592,6 +600,13 @@ Account* AccountModel::add(const QString& alias) return a; } +Account* AccountModel::add(const QString& alias, const QModelIndex& idx) +{ + Account* a = add(alias); + a->setProtocol(qvariant_cast<Account::Protocol>(idx.data((int)ProtocolModel::Role::Protocol))); + return a; +} + ///Remove an account void AccountModel::remove(Account* account) { diff --git a/src/accountmodel.h b/src/accountmodel.h index 8f95e2b74bb379c0865c2a3667e94d4999fa31ff..bb28617708b7dda53f626dc2460721fbd918f8ff 100644 --- a/src/accountmodel.h +++ b/src/accountmodel.h @@ -37,10 +37,11 @@ class LIB_EXPORT AccountModel : public QAbstractListModel { #pragma GCC diagnostic pop public: - Q_PROPERTY(Account* ip2ip READ ip2ip ) - Q_PROPERTY(bool presenceEnabled READ isPresenceEnabled ) - Q_PROPERTY(bool presencePublishSupported READ isPresencePublishSupported ) - Q_PROPERTY(bool presenceSubscribeSupported READ isPresenceSubscribeSupported ) + Q_PROPERTY(Account* ip2ip READ ip2ip ) + Q_PROPERTY(bool presenceEnabled READ isPresenceEnabled ) + Q_PROPERTY(bool presencePublishSupported READ isPresencePublishSupported ) + Q_PROPERTY(bool presenceSubscribeSupported READ isPresenceSubscribeSupported ) + Q_PROPERTY(ProtocolModel* protocolModel READ protocolModel ) friend class Account; friend class AccountPrivate; @@ -59,6 +60,7 @@ public: bool isPresenceEnabled ( ) const; bool isPresencePublishSupported ( ) const; bool isPresenceSubscribeSupported( ) const; + ProtocolModel* protocolModel ( ) const; //Abstract model accessors virtual QVariant data ( const QModelIndex& index, int role = Qt::DisplayRole ) const override; @@ -68,13 +70,14 @@ public: virtual QHash<int,QByteArray> roleNames( ) const override; //Mutators - Q_INVOKABLE Account* add ( const QString& alias ); - Q_INVOKABLE void remove ( Account* account ); - void remove ( const QModelIndex& index ); - void save ( ); - Q_INVOKABLE bool moveUp ( const QModelIndex& idx ); - Q_INVOKABLE bool moveDown ( const QModelIndex& idx ); - Q_INVOKABLE void cancel ( ); + Q_INVOKABLE Account* add ( const QString& alias, const Account::Protocol protocol = Account::Protocol::SIP); + Q_INVOKABLE Account* add ( const QString& alias, const QModelIndex& protocol ); + Q_INVOKABLE void remove ( Account* account ); + void remove ( const QModelIndex& index ); + void save ( ); + Q_INVOKABLE bool moveUp ( const QModelIndex& idx ); + Q_INVOKABLE bool moveDown ( const QModelIndex& idx ); + Q_INVOKABLE void cancel ( ); //Operators Account* operator[] (int i) ; diff --git a/src/private/account_p.h b/src/private/account_p.h index d515f8722817e5543909729f7a4379dd8cfa3f86..38da290ac1c3be090845ae2c73a9c1d91856f276 100644 --- a/src/private/account_p.h +++ b/src/private/account_p.h @@ -81,7 +81,7 @@ public: bool merge(Account* account); //Constructors static Account* buildExistingAccountFromId(const QByteArray& _accountId); - static Account* buildNewAccountFromAlias (const QString& alias ); + static Account* buildNewAccountFromAlias (Account::Protocol proto, const QString& alias); //Helpers inline void changeState(Account::EditState state); diff --git a/src/private/accountmodel_p.h b/src/private/accountmodel_p.h index a3049dd7edf23ac2e19674e481af86d57524eadf..e98db48fc9674e548c0e583d20c0f16795c59527 100644 --- a/src/private/accountmodel_p.h +++ b/src/private/accountmodel_p.h @@ -23,6 +23,7 @@ #include <account.h> class AccountModel; class AccountListColorDelegate; +class ProtocolModel; class AccountModelPrivate : public QObject { @@ -47,6 +48,7 @@ public: Account* m_pIP2IP ; QList<Account*> m_pRemovedAccounts; static AccountModel* m_spAccountList ; + ProtocolModel* m_pProtocolModel ; //Future account cache static QHash<QByteArray,AccountPlaceHolder*> m_hsPlaceHolder; diff --git a/src/protocolmodel.cpp b/src/protocolmodel.cpp index 4cb24c14cad5227f64d1e591d0ffd032580adb5a..719e3cc179fe04a617d22353c93f591325a6101c 100644 --- a/src/protocolmodel.cpp +++ b/src/protocolmodel.cpp @@ -92,6 +92,9 @@ QVariant ProtocolModel::data( const QModelIndex& index, int role) const break; }; } + else if (role == Qt::UserRole) { + return QVariant::fromValue(proto); + } return QVariant(); } @@ -102,7 +105,7 @@ int ProtocolModel::rowCount( const QModelIndex& parent ) const Qt::ItemFlags ProtocolModel::flags( const QModelIndex& index ) const { - const bool isNew = d_ptr->m_pAccount->isNew(); + const bool isNew = d_ptr->m_pAccount ? d_ptr->m_pAccount->isNew() : true; if (!index.isValid() || (!isNew)) return Qt::NoItemFlags; //Account type cannot be changed, the daemon doesn't support that and crash @@ -124,7 +127,7 @@ 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 Account::Protocol proto = d_ptr->m_pAccount ? d_ptr->m_pAccount->protocol() : Account::Protocol::RING; const QModelIndex& idx = index(static_cast<int>(proto),0); d_ptr->m_pSelectionModel->setCurrentIndex(idx,QItemSelectionModel::ClearAndSelect); @@ -137,7 +140,7 @@ QItemSelectionModel* ProtocolModel::selectionModel() const void ProtocolModelPrivate::slotSelectionChanged(const QModelIndex& idx) { - if (!idx.isValid()) + if (!m_pAccount || !idx.isValid()) return; m_pAccount->setProtocol(static_cast<Account::Protocol>(idx.row())); diff --git a/src/protocolmodel.h b/src/protocolmodel.h index ecfdccfebf03c84a770760f375c290792ddc925b..09ac7cc7245dca2efcb1ef1603b4a737aef33dc7 100644 --- a/src/protocolmodel.h +++ b/src/protocolmodel.h @@ -40,8 +40,12 @@ class LIB_EXPORT ProtocolModel : public QAbstractListModel { public: + enum class Role { + Protocol = Qt::UserRole + }; + //Private constructor, can only be called by 'Account' - explicit ProtocolModel(Account* a); + explicit ProtocolModel(Account* a = nullptr); //Model functions virtual QVariant data ( const QModelIndex& index, int role = Qt::DisplayRole ) const override;