1 /*
2  * Presence Model - A model of settable presences.
3  *
4  * Copyright (C) 2016 James D. Smith <smithjd15@gmail.com>
5  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef PRESENCEMODEL_H
23 #define PRESENCEMODEL_H
24 
25 #include <QAbstractListModel>
26 #include <QVariant>
27 
28 #include <KConfigGroup>
29 
30 #include <KTp/presence.h>
31 #include "ktpmodels_export.h"
32 
33 namespace KTp
34 {
35 
36 class KTPMODELS_EXPORT PresenceModel : public QAbstractListModel
37 {
38     Q_OBJECT
39     Q_PROPERTY(int count READ rowCount)
40 
41 public:
42     explicit PresenceModel(QObject *parent = nullptr);
43     ~PresenceModel() override;
44 
45     enum Roles {
46         //Also supplies Qt::DisplayRole and Qt::DecorationRole
47         PresenceRole = Qt::UserRole,
48         IconNameRole
49     };
50 
51     /** Adds a custom presence to the model, writes it to the config file, and
52       * propagates it to other models.
53       * @return the newly added item
54       */
55     QModelIndex addPresence(const KTp::Presence &presence);
56 
57     void removePresence(const KTp::Presence &presence);
58 
59     /** Load all presences from disk */
60     void loadPresences();
61 
62     /** Write all presences to disk */
63     void syncCustomPresencesToDisk();
64 
65     Q_SCRIPTABLE QVariant get(int row, const QByteArray& role) const;
66 
67     //protected:
68     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
69     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
70     QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits = 1, Qt::MatchFlags flags =  Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap)) const override;
71     QHash<int, QByteArray> roleNames() const override;
72 
73 public Q_SLOTS:
74     /** Incoming changes from other models */
75     void propagationChange(const QVariantList modelChange);
76 
77 private:
78     void modifyModel(const KTp::Presence &presence);
79     void propagateChange(const KTp::Presence &presence);
80 
81     /** Loads standard presences (online, away etc) into the model */
82     void loadDefaultPresences();
83 
84     /** Loads any user custom presences into the model */
85     void loadCustomPresences();
86 
87     QList<KTp::Presence> m_presences;
88 
89     //this is wrong, KConfigGroup is a sharedptr..
90     KConfigGroup m_presenceGroup;
91 };
92 
93 }
94 
95 #endif // PRESENCEMODEL_H
96