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

« back to all changes in this revision

Viewing changes to kde/src/lib/ringtonemodel.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 "ringtonemodel.h"
 
19
 
 
20
//Qt
 
21
#include <QtCore/QTimer>
 
22
 
 
23
//SFLphone
 
24
#include "dbus/configurationmanager.h"
 
25
#include "dbus/callmanager.h"
 
26
#include "account.h"
 
27
 
 
28
RingToneModel::RingToneModel(Account* a) : QAbstractTableModel(a),m_pAccount(a),m_pTimer(nullptr),
 
29
m_pCurrent(nullptr)
 
30
{
 
31
   ConfigurationManagerInterface& configurationManager = DBus::ConfigurationManager::instance();
 
32
   QMap<QString,QString> m_hRingtonePath = configurationManager.getRingtoneList();
 
33
   QMutableMapIterator<QString, QString> iter(m_hRingtonePath);
 
34
   while (iter.hasNext()) {
 
35
      iter.next();
 
36
      QFileInfo fileinfo(iter.key());
 
37
      RingToneInfo* info = new RingToneInfo();
 
38
      info->name = iter.value();
 
39
      info->path = fileinfo.absoluteFilePath();
 
40
      m_lRingTone << info;
 
41
   }
 
42
}
 
43
 
 
44
RingToneModel::~RingToneModel()
 
45
{
 
46
   while (m_lRingTone.size()) {
 
47
      RingToneInfo* ringtone = m_lRingTone[0];
 
48
      m_lRingTone.removeAt(0);
 
49
      delete ringtone;
 
50
   }
 
51
}
 
52
 
 
53
QVariant RingToneModel::data( const QModelIndex& index, int role ) const
 
54
{
 
55
   if (!index.isValid())
 
56
      return QVariant();
 
57
   RingToneInfo* info = m_lRingTone[index.row()];
 
58
   switch (index.column()) {
 
59
      case 0:
 
60
         switch (role) {
 
61
            case Qt::DisplayRole:
 
62
               return info->name;
 
63
            case Role::IsPlaying:
 
64
               return info->isPlaying;
 
65
            case Role::FullPath:
 
66
               return info->path;
 
67
         };
 
68
         break;
 
69
      case 1:
 
70
         switch (role) {
 
71
            case Role::FullPath:
 
72
               return info->path;
 
73
         };
 
74
         break;
 
75
   };
 
76
   return QVariant();
 
77
}
 
78
 
 
79
int RingToneModel::rowCount( const QModelIndex& parent ) const
 
80
{
 
81
   if (!parent.isValid())
 
82
      return m_lRingTone.size();
 
83
   return 0;
 
84
}
 
85
 
 
86
int RingToneModel::columnCount( const QModelIndex& parent ) const
 
87
{
 
88
   if (parent.isValid())
 
89
      return 0;
 
90
   return 2; //Name, then an empty one for widgets
 
91
}
 
92
 
 
93
Qt::ItemFlags RingToneModel::flags( const QModelIndex& index ) const
 
94
{
 
95
   if (index.isValid() && !index.parent().isValid())
 
96
      return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
97
   return Qt::NoItemFlags;
 
98
}
 
99
 
 
100
///This is a read only model
 
101
bool RingToneModel::setData( const QModelIndex& index, const QVariant &value, int role)
 
102
{
 
103
   Q_UNUSED(index)
 
104
   Q_UNUSED(value)
 
105
   Q_UNUSED(role )
 
106
   return false;
 
107
}
 
108
 
 
109
QString RingToneModel::currentRingTone() const
 
110
{
 
111
   return QFileInfo(m_pAccount->ringtonePath()).absoluteFilePath();
 
112
}
 
113
 
 
114
QModelIndex RingToneModel::currentIndex() const
 
115
{
 
116
   const QString rt = currentRingTone();
 
117
   for (int i=0;i<m_lRingTone.size();i++) {
 
118
      RingToneInfo* info = m_lRingTone[i];
 
119
      if (info->path == rt)
 
120
         return index(i,0);
 
121
   }
 
122
   return QModelIndex();
 
123
}
 
124
 
 
125
void RingToneModel::play(const QModelIndex& idx)
 
126
{
 
127
   if (idx.isValid()) {
 
128
      RingToneInfo* info = m_lRingTone[idx.row()];
 
129
      if (m_pCurrent && info == m_pCurrent) {
 
130
         slotStopTimer();
 
131
         return;
 
132
      }
 
133
      CallManagerInterface& callManager = DBus::CallManager::instance();
 
134
      Q_NOREPLY callManager.startRecordedFilePlayback(info->path);
 
135
      if (!m_pTimer) {
 
136
         m_pTimer = new QTimer(this);
 
137
         m_pTimer->setInterval(10000);
 
138
         connect(m_pTimer,SIGNAL(timeout()),this,SLOT(slotStopTimer()));
 
139
      }
 
140
      else if (m_pTimer->isActive()) {
 
141
         m_pTimer->stop();
 
142
      }
 
143
      m_pTimer->start();
 
144
      info->isPlaying = true;
 
145
      emit dataChanged(index(idx.row(),0),index(idx.row(),1));
 
146
      m_pCurrent = info;
 
147
   }
 
148
}
 
149
 
 
150
void RingToneModel::slotStopTimer()
 
151
{
 
152
   if (m_pCurrent) {
 
153
      CallManagerInterface& callManager = DBus::CallManager::instance();
 
154
      callManager.stopRecordedFilePlayback(m_pCurrent->path);
 
155
      m_pCurrent->isPlaying = false;
 
156
      const QModelIndex& idx = index(m_lRingTone.indexOf(m_pCurrent),0);
 
157
      emit dataChanged(idx,index(idx.row(),1));
 
158
      m_pCurrent = nullptr;
 
159
      m_pTimer->stop();
 
160
   }
 
161
}