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

« back to all changes in this revision

Viewing changes to kde/src/lib/video/videocodecmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 *   Copyright (C) 2012-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 "videocodecmodel.h"
 
19
#include <call.h>
 
20
#include <account.h>
 
21
#include <video/videocodec.h>
 
22
#include "../dbus/videomanager.h"
 
23
 
 
24
#include <QtCore/QCoreApplication>
 
25
 
 
26
///Get data from the model
 
27
QVariant VideoCodecModel::data( const QModelIndex& idx, int role) const
 
28
{
 
29
   if(idx.column() == 0 && role == Qt::DisplayRole)
 
30
      return QVariant(m_lCodecs[idx.row()]->name());
 
31
   else if(idx.column() == 0 && role == Qt::CheckStateRole) {
 
32
      return QVariant(m_lCodecs[idx.row()]->isEnabled()?Qt::Checked:Qt::Unchecked);
 
33
   }
 
34
   else if (idx.column() == 0 && role == VideoCodecModel::BITRATE_ROLE)
 
35
      return QVariant(m_lCodecs[idx.row()]->bitrate());
 
36
   return QVariant();
 
37
}
 
38
 
 
39
///The number of codec
 
40
int VideoCodecModel::rowCount( const QModelIndex& par ) const
 
41
{
 
42
   Q_UNUSED(par)
 
43
   return m_lCodecs.size();
 
44
}
 
45
 
 
46
///Items flag
 
47
Qt::ItemFlags VideoCodecModel::flags( const QModelIndex& idx ) const
 
48
{
 
49
   if (idx.column() == 0)
 
50
      return QAbstractItemModel::flags(idx) | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
51
   return QAbstractItemModel::flags(idx);
 
52
}
 
53
 
 
54
///Set the codec data (codecs can't be added or removed that way)
 
55
bool VideoCodecModel::setData(const QModelIndex& idx, const QVariant &value, int role)
 
56
{
 
57
 
 
58
   if (idx.column() == 0 && role == Qt::CheckStateRole) {
 
59
      bool changed = m_lCodecs[idx.row()]->isEnabled() != (value == Qt::Checked);
 
60
      m_lCodecs[idx.row()]->setEnabled(value == Qt::Checked);
 
61
      if (changed)
 
62
         emit dataChanged(idx, idx);
 
63
      return true;
 
64
   }
 
65
   else if (idx.column() == 0 && role == VideoCodecModel::BITRATE_ROLE) {
 
66
      bool changed = m_lCodecs[idx.row()]->bitrate() != value.toUInt();
 
67
      m_lCodecs[idx.row()]->setBitrate(value.toInt());
 
68
      if (changed)
 
69
         emit dataChanged(idx, idx);
 
70
      return true;
 
71
   }
 
72
   return false;
 
73
}
 
74
 
 
75
///Constructor
 
76
VideoCodecModel::VideoCodecModel(Account* account) : QAbstractListModel(QCoreApplication::instance()),m_pAccount(account)
 
77
{
 
78
   reload();
 
79
}
 
80
 
 
81
///Destructor
 
82
VideoCodecModel::~VideoCodecModel()
 
83
{
 
84
   while (m_lCodecs.size()) {
 
85
      VideoCodec* c = m_lCodecs[0];
 
86
      m_lCodecs.removeAt(0);
 
87
      delete c;
 
88
   }
 
89
}
 
90
 
 
91
///Force a model reload from dbus
 
92
void VideoCodecModel::reload()
 
93
{
 
94
   while (m_lCodecs.size()) {
 
95
      VideoCodec* c = m_lCodecs[0];
 
96
      m_lCodecs.removeAt(0);
 
97
      delete c;
 
98
   }
 
99
   VideoManagerInterface& interface = DBus::VideoManager::instance();
 
100
   const VectorMapStringString codecs =  interface.getCodecs(m_pAccount->id());
 
101
   foreach(const MapStringString& h,codecs) {
 
102
      VideoCodec* c = new VideoCodec(h[VideoCodec::CodecFields::NAME],
 
103
                                     h[VideoCodec::CodecFields::BITRATE].toInt(),
 
104
                                     h[VideoCodec::CodecFields::ENABLED]=="true");
 
105
      c->setParamaters(h[VideoCodec::CodecFields::PARAMETERS]);
 
106
      m_lCodecs << c;
 
107
   }
 
108
   emit dataChanged(index(0,0), index(m_lCodecs.size()-1,0));
 
109
}
 
110
 
 
111
///Save the current model over dbus
 
112
void VideoCodecModel::save()
 
113
{
 
114
   VideoManagerInterface& interface = DBus::VideoManager::instance();
 
115
   VectorMapStringString toSave;
 
116
   foreach(VideoCodec* vc,m_lCodecs) {
 
117
      toSave << vc->toMap();
 
118
   }
 
119
   interface.setCodecs(m_pAccount->id(),toSave);
 
120
}
 
121
 
 
122
///Increase codec priority
 
123
bool VideoCodecModel::moveUp(QModelIndex idx)
 
124
{
 
125
   if(idx.row() > 0 && idx.row() <= rowCount()) {
 
126
      VideoCodec* data2 = m_lCodecs[idx.row()];
 
127
      m_lCodecs.removeAt(idx.row());
 
128
      m_lCodecs.insert(idx.row() - 1, data2);
 
129
      emit dataChanged(index(idx.row() - 1, 0, QModelIndex()), index(idx.row(), 0, QModelIndex()));
 
130
      return true;
 
131
   }
 
132
   return false;
 
133
}
 
134
 
 
135
///Decrease codec priority
 
136
bool VideoCodecModel::moveDown(QModelIndex idx)
 
137
{
 
138
   if(idx.row() >= 0 && idx.row() < rowCount()) {
 
139
      VideoCodec* data2 = m_lCodecs[idx.row()];
 
140
      m_lCodecs.removeAt(idx.row());
 
141
      m_lCodecs.insert(idx.row() + 1, data2);
 
142
      emit dataChanged(index(idx.row(), 0, QModelIndex()), index(idx.row() + 1, 0, QModelIndex()));
 
143
      return true;
 
144
   }
 
145
   return false;
 
146
}