From dd6df5de91f298effa2dcc349d3d3b38e3abd8cc Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>
Date: Wed, 8 Apr 2015 14:24:23 -0400
Subject: [PATCH] accountmodel: Track enabled protocols

Refs #70364
---
 src/account.cpp              |  9 +++++-
 src/account.h                |  2 ++
 src/accountmodel.cpp         | 56 ++++++++++++++++++++++++++++++++++--
 src/accountmodel.h           | 20 +++++++++----
 src/private/account_p.h      |  1 +
 src/private/accountmodel_p.h | 26 ++++++++++-------
 6 files changed, 96 insertions(+), 18 deletions(-)

diff --git a/src/account.cpp b/src/account.cpp
index f2bb300a..41b46039 100644
--- a/src/account.cpp
+++ b/src/account.cpp
@@ -73,7 +73,7 @@ m_CurrentState(Account::EditState::READY),
 m_pAccountNumber(nullptr),m_pKeyExchangeModel(nullptr),m_pSecurityEvaluationModel(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_pBootstrapModel(nullptr)
+m_UseDefaultPort(false),m_pProtocolModel(nullptr),m_pBootstrapModel(nullptr),m_RemoteEnabledState(false)
 {
    Q_Q(Account);
 }
@@ -96,6 +96,7 @@ Account* AccountPrivate::buildExistingAccountFromId(const QByteArray& _accountId
    Account* a = new Account();
    a->d_ptr->m_AccountId = _accountId;
    a->d_ptr->setObjectName(_accountId);
+   a->d_ptr->m_RemoteEnabledState = true;
 
    a->performAction(Account::EditAction::RELOAD);
 
@@ -139,6 +140,7 @@ Account* AccountPrivate::buildNewAccountFromAlias(Account::Protocol proto, const
    }
    a->setHostname(a->d_ptr->m_hAccountDetails[DRing::Account::ConfProperties::HOSTNAME]);
    a->d_ptr->setAccountProperty(DRing::Account::ConfProperties::ALIAS,alias);
+   a->d_ptr->m_RemoteEnabledState = a->isEnabled();
    //a->setObjectName(a->id());
    return a;
 }
@@ -1655,6 +1657,10 @@ void AccountPrivate::save()
          tmp[iter.key()] = iter.value();
       }
       configurationManager.setAccountDetails(q_ptr->id(), tmp);
+      if (m_RemoteEnabledState != q_ptr->isEnabled()) {
+         m_RemoteEnabledState = q_ptr->isEnabled();
+         emit q_ptr->enabled(m_RemoteEnabledState);
+      }
    }
 
    if (!q_ptr->id().isEmpty()) {
@@ -1696,6 +1702,7 @@ void AccountPrivate::reload()
             m_hAccountDetails[iter.key()] = iter.value();
          }
          q_ptr->setHostname(m_hAccountDetails[DRing::Account::ConfProperties::HOSTNAME]);
+         m_RemoteEnabledState = q_ptr->isEnabled();
       }
       m_CurrentState = Account::EditState::READY;
 
diff --git a/src/account.h b/src/account.h
index 8e4cb1d1..5bccfcf1 100644
--- a/src/account.h
+++ b/src/account.h
@@ -404,6 +404,8 @@ class LIB_EXPORT Account : public QObject {
       void aliasChanged(const QString&);
       ///The presence support changed
       void presenceEnabledChanged(bool);
+      ///The account has been enabled/disabled
+      void enabled(bool);
 };
 // Q_DISABLE_COPY(Account)
 Q_DECLARE_METATYPE(Account*)
diff --git a/src/accountmodel.cpp b/src/accountmodel.cpp
index 8aedef08..220e440b 100644
--- a/src/accountmodel.cpp
+++ b/src/accountmodel.cpp
@@ -44,7 +44,12 @@ QHash<QByteArray,AccountPlaceHolder*> AccountModelPrivate::m_hsPlaceHolder;
 AccountModel*     AccountModelPrivate::m_spAccountList;
 
 AccountModelPrivate::AccountModelPrivate(AccountModel* parent) : QObject(parent),q_ptr(parent),
-m_pIP2IP(nullptr),m_pProtocolModel(nullptr),m_pSelectionModel(nullptr),m_lMimes({RingMimes::ACCOUNT})
+m_pIP2IP(nullptr),m_pProtocolModel(nullptr),m_pSelectionModel(nullptr),m_lMimes({RingMimes::ACCOUNT}),
+m_lSupportedProtocols {{
+   /* SIP  */ false,
+   /* IAX  */ false,
+   /* RING */ false,
+}}
 {
 }
 
@@ -147,8 +152,12 @@ Account* AccountModel::ip2ip() const
 {
    if (!d_ptr->m_pIP2IP) {
       foreach(Account* a, d_ptr->m_lAccounts) {
-         if (a->id() == DRing::Account::ProtocolNames::IP2IP)
+         if (a->id() == DRing::Account::ProtocolNames::IP2IP) {
             d_ptr->m_pIP2IP = a;
+            connect(a,&Account::enabled,[this]() {
+               emit supportedProtocolsChanged();
+            });
+         }
       }
    }
    return d_ptr->m_pIP2IP;
@@ -234,6 +243,9 @@ void AccountModelPrivate::slotDaemonAccountChanged(const QString& account, const
             connect(acc,SIGNAL(presenceEnabledChanged(bool)),this,SLOT(slotAccountPresenceEnabledChanged(bool)));
             emit q_ptr->dataChanged(q_ptr->index(i,0),q_ptr->index(q_ptr->size()-1));
             emit q_ptr->layoutChanged();
+
+            enabledProtocol(acc->protocol());
+
          }
       }
       foreach (Account* acc, m_lAccounts) {
@@ -354,6 +366,8 @@ void AccountModel::update()
          //connect(a,SIGNAL(propertyChanged(Account*,QString,QString,QString)),d_ptr,SLOT(slotAccountChanged(Account*)));
          connect(a,SIGNAL(presenceEnabledChanged(bool)),d_ptr,SLOT(slotAccountPresenceEnabledChanged(bool)));
          emit layoutChanged();
+
+         d_ptr->enabledProtocol(a->protocol());
       }
    }
 } //update
@@ -376,6 +390,8 @@ void AccountModel::updateAccounts()
          //connect(a,SIGNAL(propertyChanged(Account*,QString,QString,QString)),d_ptr,SLOT(slotAccountChanged(Account*)));
          connect(a,SIGNAL(presenceEnabledChanged(bool)),d_ptr,SLOT(slotAccountPresenceEnabledChanged(bool)));
          emit dataChanged(index(size()-1,0),index(size()-1,0));
+
+         d_ptr->enabledProtocol(a->protocol());
       }
       else {
          acc->performAction(Account::EditAction::RELOAD);
@@ -452,6 +468,19 @@ void AccountModel::cancel() {
 }
 
 
+void AccountModelPrivate::enabledProtocol(Account::Protocol proto)
+{
+   const bool cache = m_lSupportedProtocols[proto];
+
+   //Set the supported protocol bits, for now, this intentionally ignore account states
+   m_lSupportedProtocols.setAt(proto, true);
+
+   if (!cache) {
+      emit q_ptr->supportedProtocolsChanged();
+   }
+}
+
+
 /*****************************************************************************
  *                                                                           *
  *                                  Getters                                  *
@@ -578,6 +607,27 @@ bool AccountModel::isPresenceSubscribeSupported() const
    return false;
 }
 
+bool AccountModel::isSipSupported() const
+{
+   return d_ptr->m_lSupportedProtocols[Account::Protocol::SIP];
+}
+
+bool AccountModel::isAixSupported() const
+{
+   return d_ptr->m_lSupportedProtocols[Account::Protocol::IAX];
+}
+
+bool AccountModel::isIP2IPSupported() const
+{
+   //When this account isn't enable, it is as it wasn't there at all
+   return ip2ip()->isEnabled();
+}
+
+bool AccountModel::isRingSupported() const
+{
+   return d_ptr->m_lSupportedProtocols[Account::Protocol::RING];
+}
+
 
 ProtocolModel* AccountModel::protocolModel() const
 {
@@ -609,6 +659,8 @@ Account* AccountModel::add(const QString& alias, const Account::Protocol proto)
       d_ptr->m_pSelectionModel->setCurrentIndex(index(d_ptr->m_lAccounts.size()-1,0), QItemSelectionModel::ClearAndSelect);
    }
 
+   d_ptr->enabledProtocol(proto);
+
    return a;
 }
 
diff --git a/src/accountmodel.h b/src/accountmodel.h
index c8cd6e2b..be5011b9 100644
--- a/src/accountmodel.h
+++ b/src/accountmodel.h
@@ -37,11 +37,15 @@ 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(ProtocolModel* protocolModel              READ protocolModel                )
+   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                                    )
+   Q_PROPERTY(bool           isSipSupported             READ isSipSupported   NOTIFY supportedProtocolsChanged)
+   Q_PROPERTY(bool           isAixSupported             READ isAixSupported   NOTIFY supportedProtocolsChanged)
+   Q_PROPERTY(bool           isIP2IPSupported           READ isIP2IPSupported NOTIFY supportedProtocolsChanged)
+   Q_PROPERTY(bool           isRingSupported            READ isRingSupported  NOTIFY supportedProtocolsChanged)
 
    friend class Account;
    friend class AccountPrivate;
@@ -61,6 +65,10 @@ public:
    bool                 isPresencePublishSupported  (                                      ) const;
    bool                 isPresenceSubscribeSupported(                                      ) const;
    ProtocolModel*       protocolModel               (                                      ) const;
+   bool                 isSipSupported              (                                      ) const;
+   bool                 isAixSupported              (                                      ) const;
+   bool                 isIP2IPSupported            (                                      ) const;
+   bool                 isRingSupported             (                                      ) const;
 
    QItemSelectionModel* selectionModel              (                                      ) const;
 
@@ -125,6 +133,8 @@ Q_SIGNALS:
    void presenceEnabledChanged(bool isPresent                        );
    ///An account has been removed
    void accountRemoved(Account* account                              );
+   ///Emitted when an account using a previously unsupported protocol is added
+   void supportedProtocolsChanged(                                   );
    ///Emitted when an account state change
    void accountStateChanged  ( Account* account, const Account::RegistrationState state);
 };
diff --git a/src/private/account_p.h b/src/private/account_p.h
index a7725794..28cc5a77 100644
--- a/src/private/account_p.h
+++ b/src/private/account_p.h
@@ -69,6 +69,7 @@ public:
    Account::RegistrationState m_RegistrationState        ;
    QString                    m_LastSipRegistrationStatus;
    unsigned short             m_UseDefaultPort           ;
+   bool                       m_RemoteEnabledState       ;
 
    //Setters
    void setAccountProperties(const QHash<QString,QString>& m          );
diff --git a/src/private/accountmodel_p.h b/src/private/accountmodel_p.h
index ff5ed1a3..caf4fe3c 100644
--- a/src/private/accountmodel_p.h
+++ b/src/private/accountmodel_p.h
@@ -18,9 +18,13 @@
 #ifndef ACCOUNTMODELPRIVATE_H
 #define ACCOUNTMODELPRIVATE_H
 
+//Qt
 #include <QtCore/QObject>
 #include <QtCore/QStringList>
+
+//Ring
 #include <account.h>
+#include "matrixutils.h"
 class AccountModel;
 class AccountListColorDelegate;
 class ProtocolModel;
@@ -40,18 +44,20 @@ public:
 
    //Helpers
    static Account::RegistrationState fromDaemonName(const QString& st);
+   void enabledProtocol(Account::Protocol proto);
 
    //Attributes
-   AccountModel*            q_ptr             ;
-   QVector<Account*>        m_lAccounts       ;
-   AccountListColorDelegate* m_pColorDelegate ;
-   QStringList              m_lDeletedAccounts;
-   Account*                 m_pIP2IP          ;
-   QList<Account*>          m_pRemovedAccounts;
-   static AccountModel*     m_spAccountList   ;
-   ProtocolModel*           m_pProtocolModel  ;
-   QItemSelectionModel*     m_pSelectionModel ;
-   QStringList              m_lMimes          ;
+   AccountModel*                     q_ptr                ;
+   QVector<Account*>                 m_lAccounts          ;
+   AccountListColorDelegate*         m_pColorDelegate     ;
+   QStringList                       m_lDeletedAccounts   ;
+   Account*                          m_pIP2IP             ;
+   QList<Account*>                   m_pRemovedAccounts   ;
+   static AccountModel*              m_spAccountList      ;
+   ProtocolModel*                    m_pProtocolModel     ;
+   QItemSelectionModel*              m_pSelectionModel    ;
+   QStringList                       m_lMimes             ;
+   Matrix1D<Account::Protocol, bool> m_lSupportedProtocols;
 
    //Future account cache
    static  QHash<QByteArray,AccountPlaceHolder*> m_hsPlaceHolder;
-- 
GitLab