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
Branches
Tags
No related merge requests found
......@@ -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
)
......
......@@ -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();
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) {
......
......@@ -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();
}
......
/****************************************************************************
* 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 @@
#include "collectioneditor.h"
#include "contactmethod.h"
#include "numbercategorymodel.h"
#include "private/numbercategorymodel_p.h"
class NumberCategoryPrivate
{
public:
NumberCategoryPrivate();
//Attributes
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;
}
......@@ -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;
//Setter
void setIcon(const QVariant& pixmap );
void setName(const QString& name );
void setKey (const int key );
private:
NumberCategory(CollectionMediator<ContactMethod>* mediator, const QString& name);
......
......@@ -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->setKey ( key );
cat->setIcon( icon );
rep->category = cat ;
rep->index = index ;
rep->enabled = enabled;
rep->index = d_ptr->m_lCategories.size();
rep->enabled = false ;
d_ptr->m_hToInternal[ cat ] = rep ;
d_ptr->m_hByIdx[index] = rep ;
d_ptr->m_hByName[name] = 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;
}
......
......@@ -28,12 +28,21 @@ 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
......@@ -44,9 +53,7 @@ public:
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();
......@@ -55,11 +62,6 @@ public:
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);
private:
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> *
* *
* 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 ;
};
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;
}
//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;
};
void NumberCategoryDelegate::setInstance(NumberCategoryDelegate* ins)
{
m_spInstance = ins;
ins->load(NumberCategoryModel::instance());
}
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment