1 /*
2  *      Copyright (C) 2015-2019 Jean-Luc Barriere
3  *
4  *  This file is part of Noson-App
5  *
6  *  Noson is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  Noson is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with Noson.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef NOSONAPPSERVICESMODEL_H
22 #define NOSONAPPSERVICESMODEL_H
23 
24 #include "listmodel.h"
25 #include <noson/musicservices.h>
26 
27 #include <QAbstractListModel>
28 
29 namespace nosonapp
30 {
31 
32 class Sonos;
33 
34 class ServiceItem
35 {
36 public:
37   ServiceItem(const SONOS::SMServicePtr& ptr);
38 
~ServiceItem()39   virtual ~ServiceItem() { }
40 
isValid()41   bool isValid() const { return m_valid; }
42 
43   QVariant payload() const;
44 
id()45   const QString& id() const { return m_id; }
46 
title()47   const QString& title() const { return m_title; }
48 
icon()49   const QString& icon() const { return m_icon; }
50 
nickName()51   const QString& nickName() const { return m_nickName; }
52 
normalized()53   const QString& normalized() const { return m_normalized; }
54 
type()55   const QString& type() const { return m_type; }
56 
serialNum()57   const QString& serialNum() const { return m_serialNum; }
58 
auth()59   const QString& auth() const { return m_auth; }
60 
61 private:
62   SONOS::SMServicePtr m_ptr;
63   bool m_valid;
64   QString m_id;
65   QString m_title;
66   QString m_icon;
67   QString m_nickName;
68   QString m_normalized;
69   QString m_type;
70   QString m_serialNum;
71   QString m_auth;
72 };
73 
74 class ServicesModel : public QAbstractListModel, public ListModel<Sonos>
75 {
76   Q_OBJECT
77   Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
78   Q_PROPERTY(bool failure READ dataFailure NOTIFY loaded)
79 
80 public:
81   enum ServiceRoles
82   {
83     PayloadRole,
84     IdRole,
85     TitleRole,
86     IconRole,
87     NickNameRole,
88     NormalizedRole,
89     TypeRole,
90     SerialNumRole,
91     AuthRole,
92   };
93 
94   ServicesModel(QObject* parent = 0);
95   virtual ~ServicesModel();
96 
97   void addItem(ServiceItem* item);
98 
99   int rowCount(const QModelIndex& parent = QModelIndex()) const;
100 
101   QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
102 
103   Q_INVOKABLE QVariantMap get(int row);
104 
isNew()105   Q_INVOKABLE bool isNew() { return m_dataState == DataStatus::DataBlank; }
106 
107   Q_INVOKABLE bool init(Sonos* provider, bool fill = false) { return ListModel<Sonos>::configure(provider, fill); }
108 
109   virtual void clearData();
110 
111   virtual bool loadData();
112 
113   Q_INVOKABLE bool asyncLoad();
114 
115   Q_INVOKABLE void resetModel();
116 
117   virtual void handleDataUpdate();
118 
119 signals:
120   void dataUpdated();
121   void countChanged();
122   void loaded(bool succeeded);
123 
124 protected:
125   QHash<int, QByteArray> roleNames() const;
126 
127 private:
128   QList<ServiceItem*> m_items;
129   QList<ServiceItem*> m_data;
130 };
131 
132 }
133 
134 #endif /* NOSONAPPSERVICESMODEL_H */
135 
136