1
/****************************************************************************
2
* Copyright (C) 2014 by Savoir-Faire Linux *
3
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
5
* This library is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU Lesser General Public *
7
* License as published by the Free Software Foundation; either *
8
* version 2.1 of the License, or (at your option) any later version. *
10
* This library is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13
* Lesser General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17
***************************************************************************/
18
#ifndef ABSTRACTITEMBACKENDBASE_H
19
#define ABSTRACTITEMBACKENDBASE_H
23
#include <QStringList>
25
#include <QtCore/QAbstractItemModel>
35
class LIB_EXPORT AbstractItemBackendBase {
37
virtual ~AbstractItemBackendBase(){}
39
enum SupportedFeatures {
41
LOAD = 0x1 << 0, /* Load this backend, DO NOT load anything before "load" is called */
42
SAVE = 0x1 << 1, /* Save an item */
43
EDIT = 0x1 << 2, /* Edit, but **DOT NOT**, save an item) */
44
PROBE = 0x1 << 3, /* Check if the backend has new items (some backends do this automagically)*/
45
ADD = 0x1 << 4, /* Add (and save) a new item to the backend */
46
SAVE_ALL = 0x1 << 5, /* Save all items at once, this may or may not be faster than "add" */
47
CLEAR = 0x1 << 6, /* Clear all items from this backend */
48
REMOVE = 0x1 << 7, /* Remove a single item */
49
EXPORT = 0x1 << 8, /* Export all items, format and output need to be defined by each backends */
50
IMPORT = 0x1 << 9, /* Import items from an external source, details defined by each backends */
51
ENABLEABLE = 0x1 << 10, /*Can be enabled, I know, it is not a word, but Java use it too */
52
DISABLEABLE = 0x1 << 11, /*Can be disabled, I know, it is not a word, but Java use it too */
53
MANAGEABLE = 0x1 << 12, /* Can be managed the config GUI */
57
virtual QString name () const =0;
58
virtual QVariant icon() const =0;
59
virtual bool isEnabled() const = 0;
60
virtual bool enable (bool);
61
virtual QByteArray id() const = 0;
63
virtual SupportedFeatures supportedFeatures() const = 0;
65
virtual bool load() = 0;
66
virtual bool reload() = 0;
69
QVector<AbstractItemBackendBase*> baseChildrenBackends() const;
71
QVector<AbstractItemBackendBase*> m_lBaseChildren;
74
///AbstractItemBackendInterface: Allow different way to handle contact without poluting the library
75
template <class T> class LIB_EXPORT AbstractItemBackendInterface : public AbstractItemBackendBase
79
explicit AbstractItemBackendInterface(AbstractItemBackendInterface<T>* parent = nullptr);
80
virtual ~AbstractItemBackendInterface() {}
82
virtual bool save(const T* item) =0;
83
virtual bool append(const T* item) =0;
84
virtual bool batchSave(const QList<T*> contacts);
85
virtual bool remove(T* item);
87
///Edit 'item', the implementation may be a GUI or something else
88
virtual bool edit ( T* item ) = 0;
89
///Add a new item to the backend
90
virtual bool addNew ( T* item ) = 0;
92
///Add a new phone number to an existing item
93
virtual bool addPhoneNumber( T* item , PhoneNumber* number )=0;
95
AbstractItemBackendInterface<T>* parentBackend() const;
97
QVector<AbstractItemBackendInterface<T>*> childrenBackends() const;
98
void addChildren(AbstractItemBackendInterface<T>* c);
100
virtual QList<T*> items() const = 0;
103
AbstractItemBackendInterface<T>* m_pParent;
104
QVector<AbstractItemBackendInterface<T>*> m_lChildren;
107
// those classes cannot be typedefs because Qt doesn't support template QObjects
109
class LIB_EXPORT AbstractContactBackend : public QObject, public AbstractItemBackendInterface<Contact>
111
#pragma GCC diagnostic push
112
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
114
#pragma GCC diagnostic pop
116
explicit AbstractContactBackend(AbstractItemBackendInterface<Contact>* parentBackend = nullptr,
117
QObject* parent = nullptr);
118
virtual ~AbstractContactBackend();
123
void newContactAdded(Contact* c);
126
class LIB_EXPORT AbstractHistoryBackend : public QObject, public AbstractItemBackendInterface<Call>
128
#pragma GCC diagnostic push
129
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
131
#pragma GCC diagnostic pop
133
explicit AbstractHistoryBackend(AbstractItemBackendInterface<Call>* parentBackend = nullptr,
134
QObject* parent = nullptr);
135
virtual ~AbstractHistoryBackend();
139
void newHistoryCallAdded(Call* c);
142
template <class T> AbstractItemBackendInterface<T>* AbstractItemBackendInterface<T>::parentBackend() const
147
template <class T> QVector<AbstractItemBackendInterface<T>*> AbstractItemBackendInterface<T>::childrenBackends() const
152
template <class T> void AbstractItemBackendInterface<T>::addChildren(AbstractItemBackendInterface<T>* c)
155
m_lBaseChildren << c;
159
template <class T> AbstractItemBackendInterface<T>::AbstractItemBackendInterface(AbstractItemBackendInterface<T>* parent):
163
parent->addChildren(this);