From 2b578f00a75880e8ee7635cbbd4ee794f1801fac Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Mon, 5 Jun 2017 13:33:50 -0400
Subject: [PATCH] cm: replace BLANK() by default ctor

Replace ContactMethod::BLANK() by ContactMethod::ContactMethod(),
the default constructor.

This is the general way to construct a instance with "defaults"
behaviours, like BLANK tries to do.

Also removes the nasty usage of BLANK:
- it returns a constant instance
- the code uses it with a const_cast<>, removing the only protection!
No sense and extremly dangerous to operate with such global and
non protected instance.

Side effect: Call::peerContactMethod() returns a nullptr
and not a valid and !!mutable!! global const instance if no cm exists.

Change-Id: I8a29eacad657b76a7670055f27afeb6db98d78dc
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
---
 src/account.cpp       |  2 +-
 src/call.cpp          |  2 +-
 src/contactmethod.cpp | 14 ++++----------
 src/contactmethod.h   |  6 +++---
 4 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/src/account.cpp b/src/account.cpp
index fabc7931..4754453b 100644
--- a/src/account.cpp
+++ b/src/account.cpp
@@ -197,7 +197,7 @@ Account* Account::buildNewAccountFromAlias(Account::Protocol proto, const QStrin
    a->setProtocol(proto);
    a->d_ptr->m_hAccountDetails.clear();
    a->d_ptr->m_hAccountDetails[DRing::Account::ConfProperties::ENABLED] = "false";
-   a->d_ptr->m_pAccountNumber = const_cast<ContactMethod*>(ContactMethod::BLANK());
+   a->d_ptr->m_pAccountNumber = nullptr;
    MapStringString tmp;
    switch (proto) {
       case Account::Protocol::SIP:
diff --git a/src/call.cpp b/src/call.cpp
index c6f0720f..fc9d2073 100644
--- a/src/call.cpp
+++ b/src/call.cpp
@@ -838,7 +838,7 @@ ContactMethod* Call::peerContactMethod() const
     if (d_ptr->m_pDialNumber)
         return d_ptr->m_pDialNumber;
 
-    return const_cast<ContactMethod*>(ContactMethod::BLANK());
+    return nullptr;
 }
 
 ///Get the peer name
diff --git a/src/contactmethod.cpp b/src/contactmethod.cpp
index 27074995..4b07f6ca 100644
--- a/src/contactmethod.cpp
+++ b/src/contactmethod.cpp
@@ -97,16 +97,6 @@ void ContactMethodPrivate::registeredNameSet(const QString& name)
       emit n->registeredNameSet(name);
 }
 
-const ContactMethod* ContactMethod::BLANK()
-{
-    static auto instance = []{
-        auto instance = new ContactMethod(QString(), NumberCategoryModel::other());
-        instance->d_ptr->m_Type = ContactMethod::Type::BLANK;
-        return instance;
-    }();
-    return instance;
-}
-
 ContactMethodPrivate::ContactMethodPrivate(const URI& uri, NumberCategory* cat, ContactMethod::Type st, ContactMethod* q) :
    m_Uri(uri),m_pCategory(cat),m_Tracked(false),m_Present(false),
    m_Type(st),m_PopularityIndex(-1),m_pPerson(nullptr),m_pAccount(nullptr),
@@ -114,6 +104,10 @@ ContactMethodPrivate::ContactMethodPrivate(const URI& uri, NumberCategory* cat,
    m_Index(-1),m_hasType(false),m_pTextRecording(nullptr), m_pCertificate(nullptr), q_ptr(q)
 {}
 
+ContactMethod::ContactMethod()
+    : ContactMethod(QString(), NumberCategoryModel::other(), ContactMethod::Type::BLANK)
+{}
+
 ///Constructor
 ContactMethod::ContactMethod(const URI& number, NumberCategory* cat, Type st) : ItemBase(&PhoneDirectoryModel::instance()),
 d_ptr(new ContactMethodPrivate(number,cat,st,this))
diff --git a/src/contactmethod.h b/src/contactmethod.h
index ae93c27d..3fff7a6a 100644
--- a/src/contactmethod.h
+++ b/src/contactmethod.h
@@ -104,6 +104,9 @@ public:
    };
    Q_ENUMS(Type)
 
+   //Constructor
+   ContactMethod(); /*! construct an instance set with Type::BLANK */
+
    //Getters
    URI                   uri             () const;
    NumberCategory*       category        () const;
@@ -160,9 +163,6 @@ public:
    Q_INVOKABLE void addCall(Call* call);
    Q_INVOKABLE void incrementAlternativeName(const QString& name, const time_t lastUsed);
 
-   //Static
-   static const ContactMethod* BLANK();
-
    //Helper
    QString toHash() const;
 
-- 
GitLab