From c615dafacf2d253310afbc0577edbcd066cf581c Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> Date: Tue, 10 Mar 2015 15:16:50 -0400 Subject: [PATCH] account: Add an alternate sequence to create accounts Refs #68173 --- src/account.cpp | 2 +- src/account.h | 1 + src/accountmodel.cpp | 23 +++++++++++++++++++---- src/accountmodel.h | 25 ++++++++++++++----------- src/private/account_p.h | 2 +- src/private/accountmodel_p.h | 2 ++ src/protocolmodel.cpp | 9 ++++++--- src/protocolmodel.h | 6 +++++- 8 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/account.cpp b/src/account.cpp index 90bd71cf..74a9b946 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 0bb4f8b9..4ea0b842 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 673996c2..1bcee85f 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 8f95e2b7..bb286177 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 d515f872..38da290a 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 a3049dd7..e98db48f 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 4cb24c14..719e3cc1 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 ecfdccfe..09ac7cc7 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; -- GitLab