~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to kde/src/lib/presencestatusmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 *   Copyright (C) 2013-2014 by Savoir-Faire Linux                          *
 
3
 *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
 
4
 *                                                                          *
 
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.     *
 
9
 *                                                                          *
 
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.                        *
 
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
#include "presencestatusmodel.h"
 
19
 
 
20
//Qt
 
21
#include <QtCore/QCoreApplication>
 
22
 
 
23
//SFLPhone
 
24
#include "accountlistmodel.h"
 
25
#include "dbus/presencemanager.h"
 
26
#include "visitors/presenceserializationvisitor.h"
 
27
 
 
28
//Static
 
29
PresenceStatusModel* PresenceStatusModel::m_spInstance = nullptr;
 
30
 
 
31
///Constructor
 
32
PresenceStatusModel::PresenceStatusModel(QObject* parent) : QAbstractTableModel(parent?parent:QCoreApplication::instance()),
 
33
m_pCurrentStatus(nullptr),m_pDefaultStatus(nullptr),m_UseCustomStatus(false),m_CustomStatus(false),m_pVisitor(nullptr)
 
34
{
 
35
   setObjectName("PresenceStatusModel");
 
36
}
 
37
 
 
38
PresenceStatusModel::~PresenceStatusModel()
 
39
{
 
40
   foreach (StatusData* data, m_lStatuses) {
 
41
      delete data;
 
42
   }
 
43
   if (m_pVisitor) delete m_pVisitor;
 
44
}
 
45
 
 
46
///Get model data
 
47
QVariant PresenceStatusModel::data(const QModelIndex& index, int role ) const
 
48
{
 
49
   if (index.isValid()) {
 
50
      switch (static_cast<PresenceStatusModel::Columns>(index.column())) {
 
51
         case PresenceStatusModel::Columns::Name:
 
52
            switch (role) {
 
53
               case Qt::DisplayRole:
 
54
               case Qt::EditRole:
 
55
                  return m_lStatuses[index.row()]->name;
 
56
               case Qt::ToolTipRole:
 
57
                  return m_lStatuses[index.row()]->message;
 
58
            }
 
59
            break;
 
60
         case PresenceStatusModel::Columns::Message:
 
61
            switch (role) {
 
62
               case Qt::DisplayRole:
 
63
               case Qt::EditRole:
 
64
                  return m_lStatuses[index.row()]->message;
 
65
            }
 
66
            break;
 
67
         case PresenceStatusModel::Columns::Color:
 
68
            switch (role) {
 
69
               case Qt::BackgroundColorRole:
 
70
                  return m_lStatuses[index.row()]->color;
 
71
            }
 
72
            break;
 
73
         case PresenceStatusModel::Columns::Status:
 
74
            switch (role) {
 
75
               case Qt::CheckStateRole:
 
76
                  return m_lStatuses[index.row()]->status?Qt::Checked:Qt::Unchecked;
 
77
               case Qt::TextAlignmentRole:
 
78
                  return Qt::AlignCenter;
 
79
            }
 
80
            break;
 
81
         case PresenceStatusModel::Columns::Default:
 
82
            switch (role) {
 
83
               case Qt::CheckStateRole:
 
84
                  return m_lStatuses[index.row()]->defaultStatus?Qt::Checked:Qt::Unchecked;
 
85
               case Qt::TextAlignmentRole:
 
86
                  return Qt::AlignCenter;
 
87
            }
 
88
            break;
 
89
      }
 
90
   }
 
91
   return QVariant();
 
92
}
 
93
 
 
94
///Return the number of pre defined status
 
95
int PresenceStatusModel::rowCount(const QModelIndex& parent ) const
 
96
{
 
97
   if (parent.isValid()) return 0;
 
98
   return m_lStatuses.size();
 
99
}
 
100
 
 
101
///Return the number of column (static: {"Name","Message","Color","Present","Default"})
 
102
int PresenceStatusModel::columnCount(const QModelIndex& parent ) const
 
103
{
 
104
   if (parent.isValid()) return 0;
 
105
   return 5;
 
106
}
 
107
 
 
108
///All the items are enabled, selectable and editable
 
109
Qt::ItemFlags PresenceStatusModel::flags(const QModelIndex& index ) const
 
110
{
 
111
   const int col = index.column();
 
112
   return Qt::ItemIsEnabled
 
113
      | Qt::ItemIsSelectable
 
114
      | (col<2||col>=3?Qt::ItemIsEditable:Qt::NoItemFlags)
 
115
      | (col>=3?Qt::ItemIsUserCheckable:Qt::NoItemFlags);
 
116
}
 
117
 
 
118
///Set model data
 
119
bool PresenceStatusModel::setData(const QModelIndex& index, const QVariant &value, int role )
 
120
{
 
121
   Q_UNUSED(index)
 
122
   Q_UNUSED(value)
 
123
   Q_UNUSED(role)
 
124
   if (index.isValid()) {
 
125
      StatusData* dat = m_lStatuses[index.row()];
 
126
      switch(static_cast<PresenceStatusModel::Columns>(index.column())) {
 
127
         case PresenceStatusModel::Columns::Name:
 
128
            if (role == Qt::EditRole) {
 
129
               dat->name = value.toString();
 
130
               emit dataChanged(index,index);
 
131
               return true;
 
132
            }
 
133
            break;
 
134
         case PresenceStatusModel::Columns::Message:
 
135
            if (role == Qt::EditRole) {
 
136
               dat->message = value.toString();
 
137
               emit dataChanged(index,index);
 
138
               return true;
 
139
            }
 
140
            break;
 
141
         case PresenceStatusModel::Columns::Color:
 
142
            if (role == Qt::EditRole) {
 
143
               
 
144
            }
 
145
            break;
 
146
         case PresenceStatusModel::Columns::Status:
 
147
            if (role == Qt::CheckStateRole) {
 
148
               dat->status = value.toBool();
 
149
               emit dataChanged(index,index);
 
150
               return true;
 
151
            }
 
152
            break;
 
153
         case PresenceStatusModel::Columns::Default:
 
154
            if (role == Qt::CheckStateRole) {
 
155
               dat->defaultStatus = value.toBool();
 
156
               setDefaultStatus(index);
 
157
               return true;
 
158
            }
 
159
            break;
 
160
      };
 
161
   }
 
162
   return false;
 
163
}
 
164
 
 
165
///Return header data
 
166
QVariant PresenceStatusModel::headerData(int section, Qt::Orientation orientation, int role ) const
 
167
{
 
168
   Q_UNUSED(section)
 
169
   Q_UNUSED(orientation)
 
170
   static const QString rows[] = {tr("Name"),tr("Message"),tr("Color"),tr("Present"),tr("Default")};
 
171
   if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
 
172
      return QVariant(rows[section]);
 
173
   }
 
174
   return QVariant();
 
175
}
 
176
 
 
177
///Add a status to the model
 
178
void PresenceStatusModel::addStatus(StatusData* status)
 
179
{
 
180
   m_lStatuses << status;
 
181
   if (status->defaultStatus) {
 
182
      m_pDefaultStatus = status;
 
183
      if (!m_pCurrentStatus)
 
184
         setCurrentIndex(index(m_lStatuses.size()-1,0));
 
185
   }
 
186
}
 
187
 
 
188
 
 
189
void PresenceStatusModel::setPresenceVisitor(PresenceSerializationVisitor* visitor)
 
190
{
 
191
   m_pVisitor = visitor;
 
192
   if (m_pVisitor)
 
193
      m_pVisitor->load();
 
194
}
 
195
 
 
196
///Add a new status
 
197
void PresenceStatusModel::addRow()
 
198
{
 
199
   StatusData* newRow = new StatusData();
 
200
   newRow->status = false;
 
201
   m_lStatuses << newRow;
 
202
   emit layoutChanged();
 
203
}
 
204
 
 
205
///Remove status[index]
 
206
void PresenceStatusModel::removeRow(const QModelIndex& index)
 
207
{
 
208
   StatusData* toDel = m_lStatuses[index.row()];
 
209
   m_lStatuses.remove(index.row());
 
210
   emit layoutChanged();
 
211
   delete toDel;
 
212
}
 
213
 
 
214
///Serialize model TODO a backend visitor need to be created
 
215
void PresenceStatusModel::save()
 
216
{
 
217
   if (m_pVisitor)
 
218
      m_pVisitor->serialize();
 
219
}
 
220
 
 
221
///Singleton
 
222
PresenceStatusModel* PresenceStatusModel::instance()
 
223
{
 
224
   if (!m_spInstance) {
 
225
      m_spInstance = new PresenceStatusModel();
 
226
   }
 
227
   return m_spInstance;
 
228
}
 
229
 
 
230
///Move idx up
 
231
void PresenceStatusModel::moveUp(const QModelIndex& idx)
 
232
{
 
233
   const int row = idx.row();
 
234
   if (row > 0) {
 
235
      StatusData* tmp      = m_lStatuses[row-1];
 
236
      m_lStatuses[ row-1 ] = m_lStatuses[row  ];
 
237
      m_lStatuses[ row]    = tmp;
 
238
      emit dataChanged(index(row-1,0),index(row,0));
 
239
   }
 
240
}
 
241
 
 
242
///Move idx down
 
243
void PresenceStatusModel::moveDown(const QModelIndex& idx)
 
244
{
 
245
   const int row = idx.row();
 
246
   if (row-1 < m_lStatuses.size()) {
 
247
      StatusData* tmp      = m_lStatuses[row+1];
 
248
      m_lStatuses[ row+1 ] = m_lStatuses[row  ];
 
249
      m_lStatuses[ row   ] = tmp;
 
250
      emit dataChanged(index(row,0),index(row+1,0));
 
251
   }
 
252
}
 
253
 
 
254
///Return the (user defined) custom message;
 
255
QString PresenceStatusModel::customMessage() const
 
256
{
 
257
   return m_CustomMessage;
 
258
}
 
259
 
 
260
///Set the (user defined) custom message
 
261
void PresenceStatusModel::setCustomMessage(const QString& message)
 
262
{
 
263
   const bool hasChanged = m_CustomMessage != message;
 
264
   m_CustomMessage = message;
 
265
   if (hasChanged) {
 
266
      emit customMessageChanged(message);
 
267
      if (m_UseCustomStatus)
 
268
         emit currentMessageChanged(message);
 
269
   }
 
270
}
 
271
 
 
272
///Set the custom status
 
273
void PresenceStatusModel::setCustomStatus(bool status)
 
274
{
 
275
   const bool hasChanged = status != m_CustomStatus;
 
276
   m_CustomStatus = status;
 
277
   if (hasChanged) {
 
278
      emit customStatusChanged(status);
 
279
      if (m_UseCustomStatus)
 
280
         emit currentStatusChanged(status);
 
281
   }
 
282
}
 
283
 
 
284
///Switch between the pre-defined status list and custom ones
 
285
void PresenceStatusModel::setUseCustomStatus(bool useCustom)
 
286
{
 
287
   const bool changed = m_UseCustomStatus != useCustom;
 
288
   m_UseCustomStatus = useCustom;
 
289
   if (changed) {
 
290
      emit useCustomStatusChanged( useCustom                                                                                );
 
291
      emit currentIndexChanged   ( useCustom||!m_pCurrentStatus?index(-1,-1):index(m_lStatuses.indexOf(m_pCurrentStatus),0) );
 
292
      emit currentNameChanged    ( useCustom?tr("Custom"):(m_pCurrentStatus?m_pCurrentStatus->name:tr("N/A"))               );
 
293
      emit currentStatusChanged  ( useCustom?m_CustomStatus:(m_pCurrentStatus?m_pCurrentStatus->status:false)               );
 
294
      emit currentMessageChanged ( useCustom?m_CustomMessage:(m_pCurrentStatus?m_pCurrentStatus->message:tr("N/A"))         );
 
295
   }
 
296
}
 
297
 
 
298
///Return if the presence status is from the predefined list or custom
 
299
bool PresenceStatusModel::useCustomStatus() const
 
300
{
 
301
   return m_UseCustomStatus;
 
302
}
 
303
 
 
304
///Return the custom status
 
305
bool PresenceStatusModel::customStatus() const
 
306
{
 
307
   return m_CustomStatus;
 
308
}
 
309
 
 
310
///Set the current status and publish it on the network
 
311
void PresenceStatusModel::setCurrentIndex  (const QModelIndex& index)
 
312
{
 
313
   if (!index.isValid()) return;
 
314
   m_pCurrentStatus = m_lStatuses[index.row()];
 
315
   emit currentIndexChanged(index);
 
316
   emit currentNameChanged(m_pCurrentStatus->name);
 
317
   emit currentMessageChanged(m_pCurrentStatus->message);
 
318
   emit currentStatusChanged(m_pCurrentStatus->status);
 
319
   foreach(Account* a, AccountListModel::instance()->getAccounts()) {
 
320
      DBus::PresenceManager::instance().publish(a->id(), m_pCurrentStatus->status,m_pCurrentStatus->message);
 
321
   }
 
322
}
 
323
 
 
324
///Return the current status
 
325
bool PresenceStatusModel::currentStatus() const
 
326
{
 
327
   if (m_UseCustomStatus) return m_CustomStatus;
 
328
   if (!m_pCurrentStatus) return false;
 
329
   return m_UseCustomStatus?m_CustomStatus:m_pCurrentStatus->status;
 
330
}
 
331
 
 
332
///Return the current status message
 
333
QString PresenceStatusModel::currentMessage() const
 
334
{
 
335
   if (m_UseCustomStatus) return m_CustomMessage;
 
336
   if (!m_pCurrentStatus) return tr("N/A");
 
337
   return m_pCurrentStatus->message;
 
338
}
 
339
 
 
340
///Return current name
 
341
QString PresenceStatusModel::currentName() const
 
342
{
 
343
   return m_UseCustomStatus?tr("Custom"):m_pCurrentStatus?m_pCurrentStatus->name:tr("N/A");
 
344
}
 
345
 
 
346
///Return the default status index
 
347
QModelIndex PresenceStatusModel::defaultStatus() const
 
348
{
 
349
   if (!m_pDefaultStatus) return index(-1,-1);
 
350
   return index(m_lStatuses.indexOf(m_pDefaultStatus),0);
 
351
}
 
352
 
 
353
///Set the new default status
 
354
void PresenceStatusModel::setDefaultStatus( const QModelIndex& idx )
 
355
{
 
356
   if (!idx.isValid()) return;
 
357
   if (m_pDefaultStatus) {
 
358
      m_pDefaultStatus->defaultStatus = false;
 
359
      const QModelIndex& oldIdx = index(m_lStatuses.indexOf(m_pDefaultStatus),static_cast<int>(PresenceStatusModel::Columns::Default));
 
360
      emit dataChanged(oldIdx,oldIdx);
 
361
   }
 
362
   m_pDefaultStatus = m_lStatuses[idx.row()];
 
363
   m_pDefaultStatus->defaultStatus = true;
 
364
   emit defaultStatusChanged(idx);
 
365
   emit dataChanged(idx,idx);
 
366
}