~ubuntu-branches/ubuntu/utopic/sflphone/utopic-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* 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 "numbercategorymodel.h"
 
19
#include "visitors/numbercategoryvisitor.h"
 
20
#include "phonenumber.h"
 
21
#include "numbercategory.h"
 
22
 
 
23
NumberCategoryModel* NumberCategoryModel::m_spInstance = nullptr;
 
24
NumberCategory*      NumberCategoryModel::m_spOther    = nullptr;
 
25
 
 
26
NumberCategoryModel::NumberCategoryModel(QObject* parent) : QAbstractListModel(parent),m_pVisitor(nullptr)
 
27
{
 
28
   
 
29
}
 
30
 
 
31
//Abstract model member
 
32
QVariant NumberCategoryModel::data(const QModelIndex& index, int role) const
 
33
{
 
34
   if (!index.isValid()) return QVariant();
 
35
   switch (role) {
 
36
      case Qt::DisplayRole:
 
37
         return m_lCategories[index.row()]->category->name();
 
38
      case Qt::DecorationRole:
 
39
         return m_lCategories[index.row()]->category->icon();//m_pVisitor->icon(m_lCategories[index.row()]->icon);
 
40
      case Qt::CheckStateRole:
 
41
         return m_lCategories[index.row()]->enabled?Qt::Checked:Qt::Unchecked;
 
42
      case Role::INDEX:
 
43
         return m_lCategories[index.row()]->index;
 
44
      case Qt::UserRole:
 
45
         return 'x'+QString::number(m_lCategories[index.row()]->counter);
 
46
   }
 
47
   return QVariant();
 
48
}
 
49
 
 
50
int NumberCategoryModel::rowCount(const QModelIndex& parent) const
 
51
{
 
52
   if (parent.isValid()) return 0;
 
53
   return m_lCategories.size();
 
54
}
 
55
 
 
56
Qt::ItemFlags NumberCategoryModel::flags(const QModelIndex& index) const
 
57
{
 
58
   Q_UNUSED(index)
 
59
   return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
 
60
}
 
61
 
 
62
bool NumberCategoryModel::setData(const QModelIndex& idx, const QVariant &value, int role)
 
63
{
 
64
   if (idx.isValid() && role == Qt::CheckStateRole) {
 
65
      m_lCategories[idx.row()]->enabled = value.toBool();
 
66
      emit dataChanged(idx,idx);
 
67
      return true;
 
68
   }
 
69
   return false;
 
70
}
 
71
 
 
72
NumberCategory* NumberCategoryModel::addCategory(const QString& name, QPixmap* icon, int index, bool enabled)
 
73
{
 
74
   InternalTypeRepresentation* rep = m_hByName[name];
 
75
   if (!rep) {
 
76
      rep = new InternalTypeRepresentation();
 
77
      rep->counter = 0      ;
 
78
   }
 
79
   NumberCategory* cat = new NumberCategory(this,name);
 
80
   cat->setIcon(icon);
 
81
   rep->category   = cat    ;
 
82
   rep->index      = index  ;
 
83
   rep->enabled    = enabled;
 
84
   m_hByIdx[index] = rep    ;
 
85
   m_hByName[name] = rep    ;
 
86
   m_lCategories  << rep    ;
 
87
   emit layoutChanged()     ;
 
88
   return cat;
 
89
}
 
90
 
 
91
NumberCategoryModel* NumberCategoryModel::instance()
 
92
{
 
93
   if (!m_spInstance)
 
94
      m_spInstance = new NumberCategoryModel();
 
95
   return m_spInstance;
 
96
}
 
97
 
 
98
void NumberCategoryModel::setIcon(int idx, QPixmap* icon)
 
99
{
 
100
   InternalTypeRepresentation* rep = m_hByIdx[idx];
 
101
   if (rep) {
 
102
      rep->category->setIcon(icon);
 
103
      emit dataChanged(index(m_lCategories.indexOf(rep),0),index(m_lCategories.indexOf(rep),0));
 
104
   }
 
105
}
 
106
 
 
107
void NumberCategoryModel::setVisitor(NumberCategoryVisitor* visitor)
 
108
{
 
109
   m_pVisitor = visitor;
 
110
   m_pVisitor->load(this);
 
111
}
 
112
 
 
113
NumberCategoryVisitor* NumberCategoryModel::visitor() const
 
114
{
 
115
   return m_pVisitor;
 
116
}
 
117
 
 
118
void NumberCategoryModel::save()
 
119
{
 
120
   if (m_pVisitor) {
 
121
      m_pVisitor->serialize(this);
 
122
   }
 
123
   else
 
124
      qDebug() << "Cannot save NumberCategoryModel as there is no defined backend";
 
125
}
 
126
 
 
127
QModelIndex NumberCategoryModel::nameToIndex(const QString& name) const
 
128
{
 
129
   if (!m_hByName[name])
 
130
      return QModelIndex();
 
131
   else {
 
132
      return index(m_hByName[name]->index,0);
 
133
   }
 
134
}
 
135
 
 
136
///Be sure the category exist, increment the counter
 
137
void NumberCategoryModel::registerNumber(PhoneNumber* number)
 
138
{
 
139
   InternalTypeRepresentation* rep = m_hByName[number->category()->name()];
 
140
   if (!rep) {
 
141
      addCategory(number->category()->name(),nullptr,-1,true);
 
142
      rep = m_hByName[number->category()->name()];
 
143
   }
 
144
   rep->counter++;
 
145
}
 
146
 
 
147
void NumberCategoryModel::unregisterNumber(PhoneNumber* number)
 
148
{
 
149
   InternalTypeRepresentation* rep = m_hByName[number->category()->name()];
 
150
   if (rep)
 
151
      rep->counter--;
 
152
}
 
153
 
 
154
NumberCategory* NumberCategoryModel::getCategory(const QString& type)
 
155
{
 
156
   InternalTypeRepresentation* internal = m_hByName[type];
 
157
   if (internal)
 
158
      return internal->category;
 
159
   return addCategory(type,nullptr);
 
160
}
 
161
 
 
162
 
 
163
NumberCategory* NumberCategoryModel::other()
 
164
{
 
165
   if (instance()->m_hByName["Other"])
 
166
      return instance()->m_hByName["Other"]->category;
 
167
   if (!m_spOther)
 
168
      m_spOther = new NumberCategory(instance(),"Other");
 
169
   return m_spOther;
 
170
}