~ubuntu-branches/ubuntu/trusty/sflphone/trusty

1.1.8 by Mark Purcell
Import upstream version 1.2.0
1
/****************************************************************************
1.1.11 by Mark Purcell
Import upstream version 1.3.0
2
 *   Copyright (C) 2012-2014 by Savoir-Faire Linux                          *
1.1.8 by Mark Purcell
Import upstream version 1.2.0
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 "audiocodecmodel.h"
19
20
//Qt
21
#include <QtCore/QDebug>
1.1.11 by Mark Purcell
Import upstream version 1.3.0
22
#include <QtCore/QCoreApplication>
23
24
//SFLPhone
25
#include "account.h"
26
#include "dbus/configurationmanager.h"
1.1.8 by Mark Purcell
Import upstream version 1.2.0
27
28
///Constructor
1.1.11 by Mark Purcell
Import upstream version 1.3.0
29
AudioCodecModel::AudioCodecModel(Account* account) :
30
QAbstractListModel(account?(QObject*)account:(QObject*)QCoreApplication::instance()),m_pAccount(account)
31
{
32
   setObjectName("AudioCodecModel: "+account->id());
33
   QHash<int, QByteArray> roles = roleNames();
34
   roles.insert(AudioCodecModel::Role::ID        ,QByteArray("id"));
35
   roles.insert(AudioCodecModel::Role::NAME      ,QByteArray("name"));
36
   roles.insert(AudioCodecModel::Role::BITRATE   ,QByteArray("bitrate"));
37
   roles.insert(AudioCodecModel::Role::SAMPLERATE,QByteArray("samplerate"));
38
   setRoleNames(roles);
1.1.8 by Mark Purcell
Import upstream version 1.2.0
39
}
40
41
///Model data
1.1.11 by Mark Purcell
Import upstream version 1.3.0
42
QVariant AudioCodecModel::data(const QModelIndex& idx, int role) const {
43
   if(idx.column() == 0      && role == Qt::DisplayRole                   ) {
44
      return QVariant(m_lAudioCodecs[idx.row()]->name);
45
   }
46
   else if(idx.column() == 0 && role == Qt::CheckStateRole                ) {
47
      return QVariant(m_lEnabledCodecs[m_lAudioCodecs[idx.row()]->id] ? Qt::Checked : Qt::Unchecked);
48
   }
49
   else if (idx.column() == 0 && role == AudioCodecModel::Role::NAME       ) {
50
      return m_lAudioCodecs[idx.row()]->name;
51
   }
52
   else if (idx.column() == 0 && role == AudioCodecModel::Role::BITRATE    ) {
53
      return m_lAudioCodecs[idx.row()]->bitrate;
54
   }
55
   else if (idx.column() == 0 && role == AudioCodecModel::Role::SAMPLERATE ) {
56
      return m_lAudioCodecs[idx.row()]->samplerate;
57
   }
58
   else if (idx.column() == 0 && role == AudioCodecModel::Role::ID         ) {
59
      return m_lAudioCodecs[idx.row()]->id;
1.1.8 by Mark Purcell
Import upstream version 1.2.0
60
   }
61
   return QVariant();
62
}
63
64
///Number of audio codecs
1.1.11 by Mark Purcell
Import upstream version 1.3.0
65
int AudioCodecModel::rowCount(const QModelIndex& par) const {
66
   Q_UNUSED(par)
1.1.8 by Mark Purcell
Import upstream version 1.2.0
67
   return m_lAudioCodecs.size();
68
}
69
70
///Model flags
1.1.11 by Mark Purcell
Import upstream version 1.3.0
71
Qt::ItemFlags AudioCodecModel::flags(const QModelIndex& idx) const {
72
   if (idx.column() == 0)
73
      return QAbstractItemModel::flags(idx) | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
74
   return QAbstractItemModel::flags(idx);
1.1.8 by Mark Purcell
Import upstream version 1.2.0
75
}
76
77
///Set audio codec data
1.1.11 by Mark Purcell
Import upstream version 1.3.0
78
bool AudioCodecModel::setData( const QModelIndex& idx, const QVariant &value, int role) {
79
   if (idx.column() == 0 && role == AudioCodecModel::NAME) {
80
      m_lAudioCodecs[idx.row()]->name = value.toString();
81
      emit dataChanged(idx, idx);
82
      return true;
83
   }
84
   else if (idx.column() == 0 && role == AudioCodecModel::BITRATE) {
85
      m_lAudioCodecs[idx.row()]->bitrate = value.toString();
86
      emit dataChanged(idx, idx);
87
      return true;
88
   }
89
   else if(idx.column() == 0 && role == Qt::CheckStateRole) {
90
      m_lEnabledCodecs[m_lAudioCodecs[idx.row()]->id] = value.toBool();
91
      emit dataChanged(idx, idx);
92
      return true;
93
   }
94
   else if (idx.column() == 0 && role == AudioCodecModel::SAMPLERATE) {
95
      m_lAudioCodecs[idx.row()]->samplerate = value.toString();
96
      emit dataChanged(idx, idx);
97
      return true;
98
   }
99
   else if (idx.column() == 0 && role == AudioCodecModel::ID) {
100
      m_lAudioCodecs[idx.row()]->id = value.toInt();
101
      emit dataChanged(idx, idx);
1.1.8 by Mark Purcell
Import upstream version 1.2.0
102
      return true;
103
   }
104
   return false;
105
}
106
107
///Add a new audio codec
108
QModelIndex AudioCodecModel::addAudioCodec() {
109
   m_lAudioCodecs << new AudioCodecData;
110
   emit dataChanged(index(m_lAudioCodecs.size()-1,0), index(m_lAudioCodecs.size()-1,0));
111
   return index(m_lAudioCodecs.size()-1,0);
112
}
113
114
///Remove audio codec at 'idx'
115
void AudioCodecModel::removeAudioCodec(QModelIndex idx) {
116
   if (idx.isValid()) {
1.1.11 by Mark Purcell
Import upstream version 1.3.0
117
      AudioCodecData* d = m_lAudioCodecs[idx.row()];
1.1.8 by Mark Purcell
Import upstream version 1.2.0
118
      m_lAudioCodecs.removeAt(idx.row());
1.1.11 by Mark Purcell
Import upstream version 1.3.0
119
      delete d;
1.1.8 by Mark Purcell
Import upstream version 1.2.0
120
      emit dataChanged(idx, index(m_lAudioCodecs.size()-1,0));
121
   }
122
   else {
123
      qDebug() << "Failed to remove an invalid audio codec";
124
   }
125
}
126
127
///Remove everything
128
void AudioCodecModel::clear()
129
{
1.1.11 by Mark Purcell
Import upstream version 1.3.0
130
   while(m_lAudioCodecs.size()) {
131
      AudioCodecData* d = m_lAudioCodecs[0];
132
      m_lAudioCodecs.removeAt(0);
133
      delete d;
1.1.8 by Mark Purcell
Import upstream version 1.2.0
134
   }
135
   m_lAudioCodecs.clear  ();
136
   m_lEnabledCodecs.clear();
137
}
138
139
///Increase codec priority
140
bool AudioCodecModel::moveUp(QModelIndex idx)
141
{
142
   if(idx.row() > 0 && idx.row() <= rowCount()) {
1.1.11 by Mark Purcell
Import upstream version 1.3.0
143
      AudioCodecData* data2 = m_lAudioCodecs[idx.row()];
1.1.8 by Mark Purcell
Import upstream version 1.2.0
144
      m_lAudioCodecs.removeAt(idx.row());
1.1.11 by Mark Purcell
Import upstream version 1.3.0
145
      m_lAudioCodecs.insert(idx.row() - 1, data2);
1.1.8 by Mark Purcell
Import upstream version 1.2.0
146
      emit dataChanged(index(idx.row() - 1, 0, QModelIndex()), index(idx.row(), 0, QModelIndex()));
147
      return true;
148
   }
149
   return false;
150
}
151
152
///Decrease codec priority
153
bool AudioCodecModel::moveDown(QModelIndex idx)
154
{
155
   if(idx.row() >= 0 && idx.row() < rowCount()) {
1.1.11 by Mark Purcell
Import upstream version 1.3.0
156
      AudioCodecData* data2 = m_lAudioCodecs[idx.row()];
1.1.8 by Mark Purcell
Import upstream version 1.2.0
157
      m_lAudioCodecs.removeAt(idx.row());
1.1.11 by Mark Purcell
Import upstream version 1.3.0
158
      m_lAudioCodecs.insert(idx.row() + 1, data2);
1.1.8 by Mark Purcell
Import upstream version 1.2.0
159
      emit dataChanged(index(idx.row(), 0, QModelIndex()), index(idx.row() + 1, 0, QModelIndex()));
160
      return true;
161
   }
162
   return false;
163
}
1.1.11 by Mark Purcell
Import upstream version 1.3.0
164
165
///Reload the codeclist
166
void AudioCodecModel::reload()
167
{
168
   ConfigurationManagerInterface& configurationManager = DBus::ConfigurationManager::instance();
169
   QVector<int> codecIdList = configurationManager.getAudioCodecList();
170
   if (!m_pAccount->isNew()) {
171
      QVector<int> activeCodecList = configurationManager.getActiveAudioCodecList(m_pAccount->id());
172
      QStringList tmpNameList;
173
174
      foreach (const int aCodec, activeCodecList) {
175
         if (!findCodec(aCodec)) {
176
            QStringList codec = configurationManager.getAudioCodecDetails(aCodec);
177
            QModelIndex idx = addAudioCodec();
178
            setData(idx,codec[0]     ,AudioCodecModel::Role::NAME       );
179
            setData(idx,codec[1]     ,AudioCodecModel::Role::SAMPLERATE );
180
            setData(idx,codec[2]     ,AudioCodecModel::Role::BITRATE    );
181
            setData(idx,aCodec       ,AudioCodecModel::Role::ID         );
182
            setData(idx, Qt::Checked ,Qt::CheckStateRole               );
183
            if (codecIdList.indexOf(aCodec)!=-1)
184
               codecIdList.remove(codecIdList.indexOf(aCodec));
185
         }
186
      }
187
   }
188
189
   foreach (const int aCodec, codecIdList) {
190
      if (!findCodec(aCodec)) {
191
         const QStringList codec = configurationManager.getAudioCodecDetails(aCodec);
192
         QModelIndex idx = addAudioCodec();
193
         setData(idx,codec[0],AudioCodecModel::Role::NAME       );
194
         setData(idx,codec[1],AudioCodecModel::Role::SAMPLERATE );
195
         setData(idx,codec[2],AudioCodecModel::Role::BITRATE    );
196
         setData(idx,aCodec  ,AudioCodecModel::Role::ID         );
197
         setData(idx, Qt::Unchecked ,Qt::CheckStateRole);
198
      }
199
   }
200
}
201
202
///Save details
203
void AudioCodecModel::save()
204
{
205
   QStringList _codecList;
206
   for (int i=0; i < rowCount();i++) {
207
      QModelIndex idx = index(i,0);
208
      if (data(idx,Qt::CheckStateRole) == Qt::Checked) {
209
         _codecList << data(idx,AudioCodecModel::Role::ID).toString();
210
      }
211
   }
212
213
   ConfigurationManagerInterface & configurationManager = DBus::ConfigurationManager::instance();
214
   configurationManager.setActiveAudioCodecList(_codecList, m_pAccount->id());
215
}
216
217
///Check is a codec is already in the list
218
bool AudioCodecModel::findCodec(int id)
219
{
220
   foreach(AudioCodecData* data, m_lAudioCodecs) {
221
      if (data->id == id)
222
         return true;
223
   }
224
   return false;
225
}