Skip to content
Snippets Groups Projects
Commit fec328d6 authored by Emmanuel Lepage Vallee's avatar Emmanuel Lepage Vallee
Browse files

numbercategory: Remove the old serialization delegate

It is now deprecated to the collection system. This commit
also introduce caps insensitive category lookup and clean
the API.

Refs #68098
parent 4f7618ee
No related branches found
No related tags found
No related merge requests found
...@@ -231,7 +231,6 @@ SET( libringclient_LIB_SRCS ...@@ -231,7 +231,6 @@ SET( libringclient_LIB_SRCS
#Delegates #Delegates
src/delegates/accountlistcolordelegate.cpp src/delegates/accountlistcolordelegate.cpp
src/delegates/phonenumberselectordelegate.cpp src/delegates/phonenumberselectordelegate.cpp
src/delegates/numbercategorydelegate.cpp
src/delegates/pixmapmanipulationdelegate.cpp src/delegates/pixmapmanipulationdelegate.cpp
src/delegates/presenceserializationdelegate.cpp src/delegates/presenceserializationdelegate.cpp
src/delegates/itemmodelstateserializationdelegate.cpp src/delegates/itemmodelstateserializationdelegate.cpp
...@@ -348,7 +347,6 @@ SET(libringclient_delegates_LIB_HDRS ...@@ -348,7 +347,6 @@ SET(libringclient_delegates_LIB_HDRS
src/delegates/presenceserializationdelegate.h src/delegates/presenceserializationdelegate.h
src/delegates/itemmodelstateserializationdelegate.h src/delegates/itemmodelstateserializationdelegate.h
src/delegates/pixmapmanipulationdelegate.h src/delegates/pixmapmanipulationdelegate.h
src/delegates/numbercategorydelegate.h
src/delegates/profilepersisterdelegate.h src/delegates/profilepersisterdelegate.h
src/delegates/certificateserializationdelegate.h src/delegates/certificateserializationdelegate.h
) )
......
...@@ -268,12 +268,15 @@ bool CollectionModel::save() ...@@ -268,12 +268,15 @@ bool CollectionModel::save()
//Load newly enabled collections //Load newly enabled collections
foreach(CollectionModelPrivate::ProxyItem* top, d_ptr->m_lTopLevelBackends) { foreach(CollectionModelPrivate::ProxyItem* top, d_ptr->m_lTopLevelBackends) {
CollectionInterface* current = top->collection; CollectionInterface* current = top->collection;
bool check = ItemModelStateSerializationDelegate::instance()->isChecked(current); bool check,wasChecked;
bool wasChecked = current->isEnabled(); if (current) {
check = ItemModelStateSerializationDelegate::instance()->isChecked(current);
wasChecked = current->isEnabled();
if (check && !wasChecked) if (check && !wasChecked)
current->enable(true); current->enable(true);
else if ((!check) && wasChecked) else if ((!check) && wasChecked)
current->enable(false); current->enable(false);
}
//TODO implement real tree digging //TODO implement real tree digging
foreach(CollectionModelPrivate::ProxyItem* leaf ,top->m_Children) { foreach(CollectionModelPrivate::ProxyItem* leaf ,top->m_Children) {
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "call.h" #include "call.h"
#include "dbus/presencemanager.h" #include "dbus/presencemanager.h"
#include "numbercategorymodel.h" #include "numbercategorymodel.h"
#include "private/numbercategorymodel_p.h"
#include "numbercategory.h" #include "numbercategory.h"
//Private //Private
...@@ -137,7 +138,7 @@ d_ptr(new ContactMethodPrivate(number,cat,st)) ...@@ -137,7 +138,7 @@ d_ptr(new ContactMethodPrivate(number,cat,st))
setObjectName(d_ptr->m_Uri); setObjectName(d_ptr->m_Uri);
d_ptr->m_hasType = cat != NumberCategoryModel::other(); d_ptr->m_hasType = cat != NumberCategoryModel::other();
if (d_ptr->m_hasType) { if (d_ptr->m_hasType) {
NumberCategoryModel::instance()->registerNumber(this); NumberCategoryModel::instance()->d_ptr->registerNumber(this);
} }
d_ptr->m_lParents << this; d_ptr->m_lParents << this;
} }
...@@ -252,11 +253,11 @@ void ContactMethod::setCategory(NumberCategory* cat) ...@@ -252,11 +253,11 @@ void ContactMethod::setCategory(NumberCategory* cat)
{ {
if (cat == d_ptr->m_pCategory) return; if (cat == d_ptr->m_pCategory) return;
if (d_ptr->m_hasType) 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_hasType = cat != NumberCategoryModel::other();
d_ptr->m_pCategory = cat; d_ptr->m_pCategory = cat;
if (d_ptr->m_hasType) if (d_ptr->m_hasType)
NumberCategoryModel::instance()->registerNumber(this); NumberCategoryModel::instance()->d_ptr->registerNumber(this);
d_ptr->changed(); d_ptr->changed();
} }
......
/****************************************************************************
* 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
...@@ -24,17 +24,20 @@ ...@@ -24,17 +24,20 @@
#include "collectioneditor.h" #include "collectioneditor.h"
#include "contactmethod.h" #include "contactmethod.h"
#include "numbercategorymodel.h" #include "numbercategorymodel.h"
#include "private/numbercategorymodel_p.h"
class NumberCategoryPrivate class NumberCategoryPrivate
{ {
public: public:
NumberCategoryPrivate(); NumberCategoryPrivate();
//Attributes //Attributes
QString m_Name ; QString m_Name ;
QVariant m_pIcon; 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() ...@@ -85,7 +88,12 @@ bool NumberCategory::load()
int NumberCategory::size() const 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 CollectionInterface::SupportedFeatures NumberCategory::supportedFeatures() const
...@@ -105,3 +113,8 @@ void NumberCategory::setName(const QString& name) ...@@ -105,3 +113,8 @@ void NumberCategory::setName(const QString& name)
{ {
d_ptr->m_Name = name; d_ptr->m_Name = name;
} }
void NumberCategory::setKey(const int key)
{
d_ptr->m_Key = key;
}
...@@ -45,15 +45,18 @@ public: ...@@ -45,15 +45,18 @@ public:
virtual QByteArray id () const override; virtual QByteArray id () const override;
virtual int size () const override; virtual int size () const override;
//Getters
int key ( ) const;
QVariant icon(bool isTracked, bool isPresent = false) const; QVariant icon(bool isTracked, bool isPresent = false) const;
virtual SupportedFeatures supportedFeatures() const override; virtual SupportedFeatures supportedFeatures() const override;
virtual bool load() override; virtual bool load() override;
//Setter //Setter
void setIcon(const QVariant& pixmap ); void setIcon(const QVariant& pixmap );
void setName(const QString& name ); void setName(const QString& name );
void setKey (const int key );
private: private:
NumberCategory(CollectionMediator<ContactMethod>* mediator, const QString& name); NumberCategory(CollectionMediator<ContactMethod>* mediator, const QString& name);
......
...@@ -16,28 +16,12 @@ ...@@ -16,28 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#include "numbercategorymodel.h" #include "numbercategorymodel.h"
#include "delegates/numbercategorydelegate.h" #include "private/numbercategorymodel_p.h"
#include "contactmethod.h" #include "contactmethod.h"
#include "numbercategory.h" #include "numbercategory.h"
NumberCategoryModel* NumberCategoryModel::m_spInstance = nullptr; 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; NumberCategory* NumberCategoryModelPrivate::m_spOther = nullptr;
NumberCategoryModel::NumberCategoryModel(QObject* parent) : QAbstractListModel(parent),CollectionManagerInterface(this),d_ptr(new NumberCategoryModelPrivate()) NumberCategoryModel::NumberCategoryModel(QObject* parent) : QAbstractListModel(parent),CollectionManagerInterface(this),d_ptr(new NumberCategoryModelPrivate())
...@@ -55,7 +39,7 @@ QHash<int,QByteArray> NumberCategoryModel::roleNames() const ...@@ -55,7 +39,7 @@ QHash<int,QByteArray> NumberCategoryModel::roleNames() const
static bool initRoles = false; static bool initRoles = false;
if (!initRoles) { if (!initRoles) {
initRoles = true; initRoles = true;
roles[Role::INDEX] = "index"; roles[Role::KEY] = "key";
} }
return roles; return roles;
} }
...@@ -73,10 +57,10 @@ QVariant NumberCategoryModel::data(const QModelIndex& index, int role) const ...@@ -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); return d_ptr->m_lCategories[index.row()]->category->icon();//m_pDelegate->icon(m_lCategories[index.row()]->icon);
case Qt::CheckStateRole: case Qt::CheckStateRole:
return d_ptr->m_lCategories[index.row()]->enabled?Qt::Checked:Qt::Unchecked; 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: case Qt::UserRole:
return 'x'+QString::number(d_ptr->m_lCategories[index.row()]->counter); 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(); return QVariant();
} }
...@@ -103,7 +87,10 @@ bool NumberCategoryModel::setData(const QModelIndex& idx, const QVariant &value, ...@@ -103,7 +87,10 @@ bool NumberCategoryModel::setData(const QModelIndex& idx, const QVariant &value,
return false; 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]; NumberCategoryModelPrivate::InternalTypeRepresentation* rep = d_ptr->m_hByName[name];
if (!rep) { if (!rep) {
...@@ -111,14 +98,16 @@ NumberCategory* NumberCategoryModel::addCategory(const QString& name, const QVar ...@@ -111,14 +98,16 @@ NumberCategory* NumberCategoryModel::addCategory(const QString& name, const QVar
rep->counter = 0 ; rep->counter = 0 ;
} }
NumberCategory* cat = addCollection<NumberCategory,QString>(name,LoadOptions::NONE); NumberCategory* cat = addCollection<NumberCategory,QString>(name,LoadOptions::NONE);
cat->setKey ( key );
cat->setIcon( icon ); cat->setIcon( icon );
rep->category = cat ; rep->category = cat ;
rep->index = index ; rep->index = d_ptr->m_lCategories.size();
rep->enabled = enabled; rep->enabled = false ;
d_ptr->m_hToInternal[ cat ] = rep ; d_ptr->m_hToInternal[ cat ] = rep ;
d_ptr->m_hByIdx[index] = rep ; d_ptr->m_hByIdx [ key ] = rep ;
d_ptr->m_hByName[name] = rep ; d_ptr->m_hByName [ name.toLower()] = rep ;
d_ptr->m_lCategories << rep ; d_ptr->m_lCategories << rep ;
emit layoutChanged() ; emit layoutChanged() ;
return cat; return cat;
...@@ -131,69 +120,70 @@ NumberCategoryModel* NumberCategoryModel::instance() ...@@ -131,69 +120,70 @@ NumberCategoryModel* NumberCategoryModel::instance()
return m_spInstance; 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]; NumberCategoryModelPrivate::InternalTypeRepresentation* rep = d_ptr->m_hByIdx[idx];
if (rep) { if (rep) {
rep->category->setIcon(icon); rep->category->setIcon(icon);
emit dataChanged(index(d_ptr->m_lCategories.indexOf(rep),0),index(d_ptr->m_lCategories.indexOf(rep),0)); 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 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(); return QModelIndex();
else { 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 ///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) { if (!rep) {
addCategory(number->category()->name(),QVariant(),-1,true); NumberCategoryModel::instance()->addCategory(number->category()->name(),QVariant());
rep = d_ptr->m_hByName[number->category()->name()]; rep = m_hByName[lower];
} }
rep->counter++; 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) if (rep)
rep->counter--; rep->counter--;
} }
///Get the category (case insensitive)
NumberCategory* NumberCategoryModel::getCategory(const QString& type) 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) if (internal)
return internal->category; return internal->category;
return addCategory(type,QVariant()); return addCategory(lower,QVariant());
} }
NumberCategory* NumberCategoryModel::other() NumberCategory* NumberCategoryModel::other()
{ {
QString translated = QObject::tr("Other"); static QString translated = QObject::tr("Other");
if (instance()->d_ptr->m_hByName[translated]) static QString lower = translated.toLower();
return instance()->d_ptr->m_hByName[translated]->category; if (instance()->d_ptr->m_hByName[lower])
return instance()->d_ptr->m_hByName[lower]->category;
if (NumberCategoryModelPrivate::m_spOther) if (NumberCategoryModelPrivate::m_spOther)
NumberCategoryModelPrivate::m_spOther = instance()->addCollection<NumberCategory,QString>(translated,LoadOptions::NONE); NumberCategoryModelPrivate::m_spOther = instance()->addCollection<NumberCategory,QString>(translated,LoadOptions::NONE);
return NumberCategoryModelPrivate::m_spOther; 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; return i ? i->counter : 0;
} }
......
...@@ -28,12 +28,21 @@ class ContactMethod; ...@@ -28,12 +28,21 @@ class ContactMethod;
class NumberCategory; class NumberCategory;
class NumberCategoryModelPrivate; 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> { class LIB_EXPORT NumberCategoryModel : public QAbstractListModel, public CollectionManagerInterface<ContactMethod> {
Q_OBJECT Q_OBJECT
public: public:
friend class NumberCategory;
friend class ContactMethod ;
enum Role { enum Role {
INDEX = 100, KEY = 100,
}; };
//Abstract model member //Abstract model member
...@@ -44,9 +53,7 @@ public: ...@@ -44,9 +53,7 @@ public:
virtual QHash<int,QByteArray> roleNames() const override; virtual QHash<int,QByteArray> roleNames() const override;
//Mutator //Mutator
NumberCategory* addCategory(const QString& name, const QVariant& icon, int index = -1, bool enabled = true); NumberCategory* addCategory(const QString& name, const QVariant& icon, int key = -1);
void setIcon(int index, const QVariant& icon);
void save();
//Singleton //Singleton
static NumberCategoryModel* instance(); static NumberCategoryModel* instance();
...@@ -55,11 +62,6 @@ public: ...@@ -55,11 +62,6 @@ public:
QModelIndex nameToIndex(const QString& name ) const; QModelIndex nameToIndex(const QString& name ) const;
NumberCategory* getCategory(const QString& type ); NumberCategory* getCategory(const QString& type );
static NumberCategory* other ( ); static NumberCategory* other ( );
int getSize(const NumberCategory* cat) const;
//Mutator
void registerNumber (ContactMethod* number); //FIXME this should be private
void unregisterNumber(ContactMethod* number);
private: private:
explicit NumberCategoryModel(QObject* parent = nullptr); explicit NumberCategoryModel(QObject* parent = nullptr);
......
/**************************************************************************** /****************************************************************************
* Copyright (C) 2013-2015 by Savoir-Faire Linux * * Copyright (C) 2015 by Savoir-Faire Linux *
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> * * Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
* * * *
* This library is free software; you can redistribute it and/or * * This library is free software; you can redistribute it and/or *
...@@ -15,39 +15,30 @@ ...@@ -15,39 +15,30 @@
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#include "numbercategorydelegate.h" #ifndef NUMBERCATEGORYPRIVATE_H
#include "../numbercategorymodel.h" #define NUMBERCATEGORYPRIVATE_H
class DummyNumberCategoryDelegate : public NumberCategoryDelegate{ class NumberCategoryModelPrivate
{
public: public:
virtual void serialize(NumberCategoryModel* model) override; struct InternalTypeRepresentation {
virtual void load (NumberCategoryModel* model) override; NumberCategory* category;
virtual ~DummyNumberCategoryDelegate(); int index ;
bool enabled ;
int counter ;
}; };
NumberCategoryDelegate* NumberCategoryDelegate::m_spInstance = new DummyNumberCategoryDelegate(); //Attributes
QVector<InternalTypeRepresentation*> m_lCategories ;
QHash<int,InternalTypeRepresentation*> m_hByIdx ;
void DummyNumberCategoryDelegate::serialize(NumberCategoryModel* model) QHash<QString,InternalTypeRepresentation*> m_hByName ;
{ QHash<const NumberCategory*,InternalTypeRepresentation*> m_hToInternal ;
Q_UNUSED(model) static NumberCategory* m_spOther ;
}
//Mutator
void DummyNumberCategoryDelegate::load(NumberCategoryModel* model) void registerNumber ( ContactMethod* number ); //FIXME this should be private
{ void unregisterNumber( ContactMethod* number );
Q_UNUSED(model) int getSize ( const NumberCategory* cat ) const;
} };
DummyNumberCategoryDelegate::~DummyNumberCategoryDelegate()
{}
NumberCategoryDelegate* NumberCategoryDelegate::instance()
{
return m_spInstance;
}
void NumberCategoryDelegate::setInstance(NumberCategoryDelegate* ins) #endif
{ \ No newline at end of file
m_spInstance = ins;
ins->load(NumberCategoryModel::instance());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment