1 /*
2 Copyright (C) 2014 Aseman
3 http://aseman.co
4
5 This project is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This project 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
13 GNU General Public License for more details.
14
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
19 #include "telegramwallpapersmodel.h"
20 #include "telegramqml.h"
21 #include "objects/types.h"
22
23 #include <telegram.h>
24
25 #include <QDebug>
26 #include <QPointer>
27
28 class TelegramWallpapersModelPrivate
29 {
30 public:
31 QPointer<TelegramQml> telegram;
32 bool initializing;
33
34 QList<qint64> wallpapers;
35 };
36
TelegramWallpapersModel(QObject * parent)37 TelegramWallpapersModel::TelegramWallpapersModel(QObject *parent) :
38 TgAbstractListModel(parent)
39 {
40 p = new TelegramWallpapersModelPrivate;
41 p->telegram = 0;
42 p->initializing = false;
43 }
44
telegram() const45 TelegramQml *TelegramWallpapersModel::telegram() const
46 {
47 return p->telegram;
48 }
49
setTelegram(TelegramQml * tgo)50 void TelegramWallpapersModel::setTelegram(TelegramQml *tgo)
51 {
52 TelegramQml *tg = static_cast<TelegramQml*>(tgo);
53 if( p->telegram == tg )
54 return;
55 if(p->telegram)
56 {
57 disconnect( p->telegram, SIGNAL(wallpapersChanged()) , this, SLOT(wallpapersChanged()) );
58 disconnect(p->telegram , SIGNAL(authLoggedInChanged()), this, SLOT(recheck()));
59 }
60
61 p->telegram = tg;
62 p->initializing = tg;
63 if(p->telegram)
64 {
65 connect( p->telegram, SIGNAL(wallpapersChanged()) , this, SLOT(wallpapersChanged()) );
66 connect(p->telegram , SIGNAL(authLoggedInChanged()), this, SLOT(recheck()), Qt::QueuedConnection);
67 }
68
69 recheck();
70
71 Q_EMIT telegramChanged();
72 Q_EMIT initializingChanged();
73 }
74
id(const QModelIndex & index) const75 qint64 TelegramWallpapersModel::id(const QModelIndex &index) const
76 {
77 int row = index.row();
78 return p->wallpapers.at(row);
79 }
80
rowCount(const QModelIndex & parent) const81 int TelegramWallpapersModel::rowCount(const QModelIndex &parent) const
82 {
83 Q_UNUSED(parent)
84 return p->wallpapers.count();
85 }
86
data(const QModelIndex & index,int role) const87 QVariant TelegramWallpapersModel::data(const QModelIndex &index, int role) const
88 {
89 QVariant res;
90 const qint64 key = id(index);
91 switch( role )
92 {
93 case ItemRole:
94 res = QVariant::fromValue<WallPaperObject*>(p->telegram->wallpaper(key));
95 break;
96 }
97
98 return res;
99 }
100
roleNames() const101 QHash<qint32, QByteArray> TelegramWallpapersModel::roleNames() const
102 {
103 static QHash<qint32, QByteArray> *res = 0;
104 if( res )
105 return *res;
106
107 res = new QHash<qint32, QByteArray>();
108 res->insert( ItemRole, "item");
109 return *res;
110 }
111
count() const112 int TelegramWallpapersModel::count() const
113 {
114 return p->wallpapers.count();
115 }
116
initializing() const117 bool TelegramWallpapersModel::initializing() const
118 {
119 return p->initializing;
120 }
121
recheck()122 void TelegramWallpapersModel::recheck()
123 {
124 if(!p->telegram || !p->telegram->authLoggedIn())
125 return;
126 Telegram *tgObject = p->telegram->telegram();
127 if(tgObject)
128 tgObject->accountGetWallPapers();
129 }
130
wallpapersChanged()131 void TelegramWallpapersModel::wallpapersChanged()
132 {
133 p->initializing = false;
134 Q_EMIT initializingChanged();
135
136 const QList<qint64> & walls = p->telegram->wallpapers();
137
138 beginResetModel();
139 p->wallpapers.clear();
140 endResetModel();
141
142 for( int i=0 ; i<walls.count() ; i++ )
143 {
144 const qint64 dId = walls.at(i);
145 if( p->wallpapers.contains(dId) )
146 continue;
147
148 beginInsertRows(QModelIndex(), i, i );
149 p->wallpapers.insert( i, dId );
150 endInsertRows();
151 }
152 }
153
~TelegramWallpapersModel()154 TelegramWallpapersModel::~TelegramWallpapersModel()
155 {
156 delete p;
157 }
158