diff --git a/CMakeLists.txt b/CMakeLists.txt index c974ab8168f668b7726b1c95bc13c9226087fe64..631f74f93ced39cd09df69b562c7f9caf05936da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -231,7 +231,6 @@ SET( libringclient_LIB_SRCS #Delegates src/delegates/accountlistcolordelegate.cpp src/delegates/phonenumberselectordelegate.cpp - src/delegates/numbercategorydelegate.cpp src/delegates/pixmapmanipulationdelegate.cpp src/delegates/presenceserializationdelegate.cpp src/delegates/itemmodelstateserializationdelegate.cpp @@ -348,7 +347,6 @@ SET(libringclient_delegates_LIB_HDRS src/delegates/presenceserializationdelegate.h src/delegates/itemmodelstateserializationdelegate.h src/delegates/pixmapmanipulationdelegate.h - src/delegates/numbercategorydelegate.h src/delegates/profilepersisterdelegate.h src/delegates/certificateserializationdelegate.h ) diff --git a/src/collectionmodel.cpp b/src/collectionmodel.cpp index 84e371e0e2245dc62f0d24d2a7c44a1a30df103d..65f90a438c61341867a5f6c7e42a7b4c1b1528cb 100644 --- a/src/collectionmodel.cpp +++ b/src/collectionmodel.cpp @@ -268,12 +268,15 @@ bool CollectionModel::save() //Load newly enabled collections foreach(CollectionModelPrivate::ProxyItem* top, d_ptr->m_lTopLevelBackends) { CollectionInterface* current = top->collection; - bool check = ItemModelStateSerializationDelegate::instance()->isChecked(current); - bool wasChecked = current->isEnabled(); - if (check && !wasChecked) - current->enable(true); - else if ((!check) && wasChecked) - current->enable(false); + bool check,wasChecked; + if (current) { + check = ItemModelStateSerializationDelegate::instance()->isChecked(current); + wasChecked = current->isEnabled(); + if (check && !wasChecked) + current->enable(true); + else if ((!check) && wasChecked) + current->enable(false); + } //TODO implement real tree digging foreach(CollectionModelPrivate::ProxyItem* leaf ,top->m_Children) { diff --git a/src/contactmethod.cpp b/src/contactmethod.cpp index 17787f5ad6326dc2f5be77751044f117f7b2efe6..6adf5898a3f113a55a5d49a316cdd42940961238 100644 --- a/src/contactmethod.cpp +++ b/src/contactmethod.cpp @@ -22,6 +22,7 @@ #include "call.h" #include "dbus/presencemanager.h" #include "numbercategorymodel.h" +#include "private/numbercategorymodel_p.h" #include "numbercategory.h" //Private @@ -137,7 +138,7 @@ d_ptr(new ContactMethodPrivate(number,cat,st)) setObjectName(d_ptr->m_Uri); d_ptr->m_hasType = cat != NumberCategoryModel::other(); if (d_ptr->m_hasType) { - NumberCategoryModel::instance()->registerNumber(this); + NumberCategoryModel::instance()->d_ptr->registerNumber(this); } d_ptr->m_lParents << this; } @@ -252,11 +253,11 @@ void ContactMethod::setCategory(NumberCategory* cat) { if (cat == d_ptr->m_pCategory) return; if (d_ptr->m_hasType) - NumberCategoryModel::instance()->unregisterNumber(this); + NumberCategoryModel::instance()->d_ptr->unregisterNumber(this); d_ptr->m_hasType = cat != NumberCategoryModel::other(); d_ptr->m_pCategory = cat; if (d_ptr->m_hasType) - NumberCategoryModel::instance()->registerNumber(this); + NumberCategoryModel::instance()->d_ptr->registerNumber(this); d_ptr->changed(); } diff --git a/src/delegates/numbercategorydelegate.h b/src/delegates/numbercategorydelegate.h deleted file mode 100644 index f760b238499e6f4621ad5f1ae260d50f2a862092..0000000000000000000000000000000000000000 --- a/src/delegates/numbercategorydelegate.h +++ /dev/null @@ -1,39 +0,0 @@ -/**************************************************************************** - * Copyright (C) 2013-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 NUMBERCATEGORYVISITOR_H -#define NUMBERCATEGORYVISITOR_H - -#include <typedefs.h> - -class NumberCategoryModel; - -class LIB_EXPORT NumberCategoryDelegate { -public: - virtual void serialize(NumberCategoryModel* model) = 0; - virtual void load (NumberCategoryModel* model) = 0; -// virtual QVariant icon (QPixmap* icon ) = 0; - virtual ~NumberCategoryDelegate(){}; - - static NumberCategoryDelegate* instance(); - static void setInstance(NumberCategoryDelegate* ins); - -private: - static NumberCategoryDelegate* m_spInstance; -}; - -#endif //NUMBERCATEGORYVISITOR_H diff --git a/src/numbercategory.cpp b/src/numbercategory.cpp index 744cba5a71d40a0f4bcca4dda27c449c990b7a32..756293642da5f1d846596cdf90e3612d843ab6e7 100644 --- a/src/numbercategory.cpp +++ b/src/numbercategory.cpp @@ -24,17 +24,20 @@ #include "collectioneditor.h" #include "contactmethod.h" #include "numbercategorymodel.h" +#include "private/numbercategorymodel_p.h" class NumberCategoryPrivate { public: NumberCategoryPrivate(); + //Attributes - QString m_Name; + QString m_Name ; QVariant m_pIcon; + int m_Key ; }; -NumberCategoryPrivate::NumberCategoryPrivate() : m_pIcon(), m_Name() +NumberCategoryPrivate::NumberCategoryPrivate() : m_pIcon(), m_Name(), m_Key(-1) { } @@ -85,7 +88,12 @@ bool NumberCategory::load() int NumberCategory::size() const { - return NumberCategoryModel::instance()->getSize(this); + return NumberCategoryModel::instance()->d_ptr->getSize(this); +} + +int NumberCategory::key() const +{ + return d_ptr->m_Key; } CollectionInterface::SupportedFeatures NumberCategory::supportedFeatures() const @@ -105,3 +113,8 @@ void NumberCategory::setName(const QString& name) { d_ptr->m_Name = name; } + +void NumberCategory::setKey(const int key) +{ + d_ptr->m_Key = key; +} diff --git a/src/numbercategory.h b/src/numbercategory.h index 09d87a7c6172c3941fd25d818ef05f5044b7852f..33ff30214476020d1136e3f77dffb7c84a5398cc 100644 --- a/src/numbercategory.h +++ b/src/numbercategory.h @@ -45,15 +45,18 @@ public: virtual QByteArray id () const override; virtual int size () const override; + //Getters + int key ( ) const; QVariant icon(bool isTracked, bool isPresent = false) const; - virtual SupportedFeatures supportedFeatures() const override; - virtual bool load () override; + virtual SupportedFeatures supportedFeatures() const override; + virtual bool load() override; //Setter void setIcon(const QVariant& pixmap ); void setName(const QString& name ); + void setKey (const int key ); private: NumberCategory(CollectionMediator<ContactMethod>* mediator, const QString& name); diff --git a/src/numbercategorymodel.cpp b/src/numbercategorymodel.cpp index fe4c6330b89dd9c4331c5febf83034bed31ebd6f..07e924d31b1f1770a486decb664a9ab8798a260b 100644 --- a/src/numbercategorymodel.cpp +++ b/src/numbercategorymodel.cpp @@ -16,28 +16,12 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * ***************************************************************************/ #include "numbercategorymodel.h" -#include "delegates/numbercategorydelegate.h" +#include "private/numbercategorymodel_p.h" #include "contactmethod.h" #include "numbercategory.h" NumberCategoryModel* NumberCategoryModel::m_spInstance = nullptr; -class NumberCategoryModelPrivate -{ -public: - struct InternalTypeRepresentation { - NumberCategory* category; - int index ; - bool enabled ; - int counter ; - }; - QVector<InternalTypeRepresentation*> m_lCategories; - QHash<int,InternalTypeRepresentation*> m_hByIdx; - QHash<QString,InternalTypeRepresentation*> m_hByName; - QHash<const NumberCategory*,InternalTypeRepresentation*> m_hToInternal; - static NumberCategory* m_spOther ; -}; - NumberCategory* NumberCategoryModelPrivate::m_spOther = nullptr; NumberCategoryModel::NumberCategoryModel(QObject* parent) : QAbstractListModel(parent),CollectionManagerInterface(this),d_ptr(new NumberCategoryModelPrivate()) @@ -55,7 +39,7 @@ QHash<int,QByteArray> NumberCategoryModel::roleNames() const static bool initRoles = false; if (!initRoles) { initRoles = true; - roles[Role::INDEX] = "index"; + roles[Role::KEY] = "key"; } return roles; } @@ -73,10 +57,10 @@ QVariant NumberCategoryModel::data(const QModelIndex& index, int role) const return d_ptr->m_lCategories[index.row()]->category->icon();//m_pDelegate->icon(m_lCategories[index.row()]->icon); case Qt::CheckStateRole: return d_ptr->m_lCategories[index.row()]->enabled?Qt::Checked:Qt::Unchecked; - case Role::INDEX: - return d_ptr->m_lCategories[index.row()]->index; case Qt::UserRole: return 'x'+QString::number(d_ptr->m_lCategories[index.row()]->counter); + case Role::KEY: + return d_ptr->m_lCategories[index.row()]->category->key(); } return QVariant(); } @@ -103,7 +87,10 @@ bool NumberCategoryModel::setData(const QModelIndex& idx, const QVariant &value, return false; } -NumberCategory* NumberCategoryModel::addCategory(const QString& name, const QVariant& icon, int index, bool enabled) +/** + * Manually add a new category + */ +NumberCategory* NumberCategoryModel::addCategory(const QString& name, const QVariant& icon, int key) { NumberCategoryModelPrivate::InternalTypeRepresentation* rep = d_ptr->m_hByName[name]; if (!rep) { @@ -111,14 +98,16 @@ NumberCategory* NumberCategoryModel::addCategory(const QString& name, const QVar rep->counter = 0 ; } NumberCategory* cat = addCollection<NumberCategory,QString>(name,LoadOptions::NONE); - cat->setIcon(icon); - rep->category = cat ; - rep->index = index ; - rep->enabled = enabled; - - d_ptr->m_hToInternal[cat] = rep ; - d_ptr->m_hByIdx[index] = rep ; - d_ptr->m_hByName[name] = rep ; + cat->setKey ( key ); + cat->setIcon( icon ); + + rep->category = cat ; + rep->index = d_ptr->m_lCategories.size(); + rep->enabled = false ; + + d_ptr->m_hToInternal[ cat ] = rep ; + d_ptr->m_hByIdx [ key ] = rep ; + d_ptr->m_hByName [ name.toLower()] = rep ; d_ptr->m_lCategories << rep ; emit layoutChanged() ; return cat; @@ -131,69 +120,70 @@ NumberCategoryModel* NumberCategoryModel::instance() return m_spInstance; } -void NumberCategoryModel::setIcon(int idx, const QVariant& icon) +/*void NumberCategoryModel::setIcon(int idx, const QVariant& icon) { NumberCategoryModelPrivate::InternalTypeRepresentation* rep = d_ptr->m_hByIdx[idx]; if (rep) { rep->category->setIcon(icon); emit dataChanged(index(d_ptr->m_lCategories.indexOf(rep),0),index(d_ptr->m_lCategories.indexOf(rep),0)); } -} - -void NumberCategoryModel::save() -{ - NumberCategoryDelegate::instance()->serialize(this); -} +}*/ QModelIndex NumberCategoryModel::nameToIndex(const QString& name) const { - if (!d_ptr->m_hByName[name]) + const QString lower = name.toLower(); + if (!d_ptr->m_hByName[lower]) return QModelIndex(); else { - return index(d_ptr->m_hByName[name]->index,0); + return index(d_ptr->m_hByName[lower]->index,0); } } ///Be sure the category exist, increment the counter -void NumberCategoryModel::registerNumber(ContactMethod* number) +void NumberCategoryModelPrivate::registerNumber(ContactMethod* number) { - NumberCategoryModelPrivate::InternalTypeRepresentation* rep = d_ptr->m_hByName[number->category()->name()]; + const QString lower = number->category()->name().toLower(); + NumberCategoryModelPrivate::InternalTypeRepresentation* rep = m_hByName[lower]; if (!rep) { - addCategory(number->category()->name(),QVariant(),-1,true); - rep = d_ptr->m_hByName[number->category()->name()]; + NumberCategoryModel::instance()->addCategory(number->category()->name(),QVariant()); + rep = m_hByName[lower]; } rep->counter++; } -void NumberCategoryModel::unregisterNumber(ContactMethod* number) +void NumberCategoryModelPrivate::unregisterNumber(ContactMethod* number) { - NumberCategoryModelPrivate::InternalTypeRepresentation* rep = d_ptr->m_hByName[number->category()->name()]; + const QString lower = number->category()->name().toLower(); + NumberCategoryModelPrivate::InternalTypeRepresentation* rep = m_hByName[lower]; if (rep) rep->counter--; } +///Get the category (case insensitive) NumberCategory* NumberCategoryModel::getCategory(const QString& type) { - NumberCategoryModelPrivate::InternalTypeRepresentation* internal = d_ptr->m_hByName[type]; + const QString lower = type.toLower(); + NumberCategoryModelPrivate::InternalTypeRepresentation* internal = d_ptr->m_hByName[lower]; if (internal) return internal->category; - return addCategory(type,QVariant()); + return addCategory(lower,QVariant()); } NumberCategory* NumberCategoryModel::other() { - QString translated = QObject::tr("Other"); - if (instance()->d_ptr->m_hByName[translated]) - return instance()->d_ptr->m_hByName[translated]->category; + static QString translated = QObject::tr("Other"); + static QString lower = translated.toLower(); + if (instance()->d_ptr->m_hByName[lower]) + return instance()->d_ptr->m_hByName[lower]->category; if (NumberCategoryModelPrivate::m_spOther) NumberCategoryModelPrivate::m_spOther = instance()->addCollection<NumberCategory,QString>(translated,LoadOptions::NONE); return NumberCategoryModelPrivate::m_spOther; } -int NumberCategoryModel::getSize(const NumberCategory* cat) const +int NumberCategoryModelPrivate::getSize(const NumberCategory* cat) const { - NumberCategoryModelPrivate::InternalTypeRepresentation* i = d_ptr->m_hToInternal[cat]; + NumberCategoryModelPrivate::InternalTypeRepresentation* i = m_hToInternal[cat]; return i ? i->counter : 0; } diff --git a/src/numbercategorymodel.h b/src/numbercategorymodel.h index 9154e30729026dd990c0f7363cf423b8fde5019a..b706385bef7ede2962b98f22497fde31ad934986 100644 --- a/src/numbercategorymodel.h +++ b/src/numbercategorymodel.h @@ -28,38 +28,40 @@ class ContactMethod; class NumberCategory; class NumberCategoryModelPrivate; +/** + * This class manage the ContactMethod categories. Those are usually associated + * with PhoneNumber types used by contacts. + * + * The model is mostly for debugging purpose. The information is also available + * in the CollectionModel. + */ class LIB_EXPORT NumberCategoryModel : public QAbstractListModel, public CollectionManagerInterface<ContactMethod> { Q_OBJECT public: + friend class NumberCategory; + friend class ContactMethod ; enum Role { - INDEX = 100, + KEY = 100, }; //Abstract model member - virtual QVariant data (const QModelIndex& index, int role = Qt::DisplayRole ) const override; + 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; //Mutator - NumberCategory* addCategory(const QString& name, const QVariant& icon, int index = -1, bool enabled = true); - void setIcon(int index, const QVariant& icon); - void save(); + NumberCategory* addCategory(const QString& name, const QVariant& icon, int key = -1); //Singleton static NumberCategoryModel* instance(); //Getter - QModelIndex nameToIndex(const QString& name) const; - NumberCategory* getCategory(const QString& type); - static NumberCategory* other(); - int getSize(const NumberCategory* cat) const; - - //Mutator - void registerNumber (ContactMethod* number); //FIXME this should be private - void unregisterNumber(ContactMethod* number); + QModelIndex nameToIndex(const QString& name ) const; + NumberCategory* getCategory(const QString& type ); + static NumberCategory* other ( ); private: explicit NumberCategoryModel(QObject* parent = nullptr); @@ -69,8 +71,8 @@ private: //Re-implementation virtual void collectionAddedCallback(CollectionInterface* collection) override; - virtual bool addItemCallback(const ContactMethod* item) override; - virtual bool removeItemCallback(const ContactMethod* item) override; + virtual bool addItemCallback (const ContactMethod* item ) override; + virtual bool removeItemCallback (const ContactMethod* item ) override; //Singleton static NumberCategoryModel* m_spInstance; diff --git a/src/delegates/numbercategorydelegate.cpp b/src/private/numbercategorymodel_p.h similarity index 57% rename from src/delegates/numbercategorydelegate.cpp rename to src/private/numbercategorymodel_p.h index d80e760844bb3043819e0a1c13c1b982243da8fe..26647602fc2502e707c66a968994880ffbdf687b 100644 --- a/src/delegates/numbercategorydelegate.cpp +++ b/src/private/numbercategorymodel_p.h @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (C) 2013-2015 by Savoir-Faire Linux * + * 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 * @@ -15,39 +15,30 @@ * 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 "numbercategorydelegate.h" -#include "../numbercategorymodel.h" +#ifndef NUMBERCATEGORYPRIVATE_H +#define NUMBERCATEGORYPRIVATE_H -class DummyNumberCategoryDelegate : public NumberCategoryDelegate{ +class NumberCategoryModelPrivate +{ public: - virtual void serialize(NumberCategoryModel* model) override; - virtual void load (NumberCategoryModel* model) override; - virtual ~DummyNumberCategoryDelegate(); + struct InternalTypeRepresentation { + NumberCategory* category; + int index ; + bool enabled ; + int counter ; + }; + + //Attributes + QVector<InternalTypeRepresentation*> m_lCategories ; + QHash<int,InternalTypeRepresentation*> m_hByIdx ; + QHash<QString,InternalTypeRepresentation*> m_hByName ; + QHash<const NumberCategory*,InternalTypeRepresentation*> m_hToInternal ; + static NumberCategory* m_spOther ; + + //Mutator + void registerNumber ( ContactMethod* number ); //FIXME this should be private + void unregisterNumber( ContactMethod* number ); + int getSize ( const NumberCategory* cat ) const; }; -NumberCategoryDelegate* NumberCategoryDelegate::m_spInstance = new DummyNumberCategoryDelegate(); - - -void DummyNumberCategoryDelegate::serialize(NumberCategoryModel* model) -{ - Q_UNUSED(model) -} - -void DummyNumberCategoryDelegate::load(NumberCategoryModel* model) -{ - Q_UNUSED(model) -} - -DummyNumberCategoryDelegate::~DummyNumberCategoryDelegate() -{} - -NumberCategoryDelegate* NumberCategoryDelegate::instance() -{ - return m_spInstance; -} - -void NumberCategoryDelegate::setInstance(NumberCategoryDelegate* ins) -{ - m_spInstance = ins; - ins->load(NumberCategoryModel::instance()); -} +#endif \ No newline at end of file