• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi/contact

  • sources
  • kde-4.14
  • kdepimlibs
  • akonadi
  • contact
  • editor
soundeditwidget.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "soundeditwidget.h"
23 
24 #include <kabc/addressee.h>
25 #include <kfiledialog.h>
26 #include <kicon.h>
27 #include <kio/netaccess.h>
28 #include <klocalizedstring.h>
29 #include <kmessagebox.h>
30 
31 #include <phonon/mediaobject.h>
32 
33 #include <QtCore/QBuffer>
34 #include <QContextMenuEvent>
35 #include <QMenu>
36 
40 class SoundLoader
41 {
42 public:
43  SoundLoader(QWidget *parent = 0);
44 
45  QByteArray loadSound(const KUrl &url, bool *ok);
46 
47 private:
48  QByteArray mSound;
49  QWidget *mParent;
50 };
51 
52 SoundLoader::SoundLoader(QWidget *parent)
53  : mParent(parent)
54 {
55 }
56 
57 QByteArray SoundLoader::loadSound(const KUrl &url, bool *ok)
58 {
59  QByteArray sound;
60  QString tempFile;
61 
62  if (url.isEmpty()) {
63  return sound;
64  }
65 
66  (*ok) = false;
67 
68  if (url.isLocalFile()) {
69  QFile file(url.toLocalFile());
70  if (file.open(QIODevice::ReadOnly)) {
71  sound = file.readAll();
72  file.close();
73  (*ok) = true;
74  }
75  } else if (KIO::NetAccess::download(url, tempFile, mParent)) {
76  QFile file(tempFile);
77  if (file.open(QIODevice::ReadOnly)) {
78  sound = file.readAll();
79  file.close();
80  (*ok) = true;
81  }
82  KIO::NetAccess::removeTempFile(tempFile);
83  }
84 
85  if (!(*ok)) {
86  KMessageBox::sorry(mParent, i18n("This contact's sound cannot be found."));
87  return sound;
88  }
89 
90  (*ok) = true;
91 
92  return sound;
93 }
94 
95 SoundEditWidget::SoundEditWidget(QWidget *parent)
96  : QToolButton(parent)
97  , mHasSound(false)
98  , mReadOnly(false)
99  , mSoundLoader(0)
100 {
101  connect(this, SIGNAL(clicked()), SLOT(playSound()));
102 
103  updateView();
104 }
105 
106 SoundEditWidget::~SoundEditWidget()
107 {
108  delete mSoundLoader;
109 }
110 
111 void SoundEditWidget::loadContact(const KABC::Addressee &contact)
112 {
113  const KABC::Sound sound = contact.sound();
114  if (sound.isIntern() && !sound.data().isEmpty()) {
115  mHasSound = true;
116  mSound = sound.data();
117  }
118 
119  updateView();
120 }
121 
122 void SoundEditWidget::storeContact(KABC::Addressee &contact) const
123 {
124  KABC::Sound sound(contact.sound());
125  sound.setData(mSound);
126  contact.setSound(sound);
127 }
128 
129 void SoundEditWidget::setReadOnly(bool readOnly)
130 {
131  mReadOnly = readOnly;
132 }
133 
134 void SoundEditWidget::updateView()
135 {
136  if (mHasSound) {
137  setIcon(KIcon(QLatin1String("audio-volume-medium")));
138  setToolTip(i18n("Click to play pronunciation"));
139  } else {
140  setIcon(KIcon(QLatin1String("audio-volume-muted")));
141  setToolTip(i18n("No pronunciation available"));
142  }
143 }
144 
145 void SoundEditWidget::contextMenuEvent(QContextMenuEvent *event)
146 {
147  QMenu menu;
148 
149  if (mHasSound) {
150  menu.addAction(i18n("Play"), this, SLOT(playSound()));
151  }
152 
153  if (!mReadOnly) {
154  menu.addAction(i18n("Change..."), this, SLOT(changeSound()));
155  }
156 
157  if (mHasSound) {
158  menu.addAction(i18n("Save..."), this, SLOT(saveSound()));
159 
160  if (!mReadOnly) {
161  menu.addAction(i18n("Remove"), this, SLOT(deleteSound()));
162  }
163  }
164 
165  menu.exec(event->globalPos());
166 }
167 
168 void SoundEditWidget::playSound()
169 {
170  if (!mHasSound) {
171  return;
172  }
173 
174  Phonon::MediaObject *player = Phonon::createPlayer(Phonon::NotificationCategory);
175  QBuffer *soundData = new QBuffer(player);
176  soundData->setData(mSound);
177  player->setCurrentSource(soundData);
178  player->setParent(this);
179  connect(player, SIGNAL(finished()), player, SLOT(deleteLater()));
180  player->play();
181 }
182 
183 void SoundEditWidget::changeSound()
184 {
185  const KUrl url = KFileDialog::getOpenUrl(QUrl(), QLatin1String("*.wav"), this);
186  if (url.isValid()) {
187  bool ok = false;
188  const QByteArray sound = soundLoader()->loadSound(url, &ok);
189  if (ok) {
190  mSound = sound;
191  mHasSound = true;
192  updateView();
193  }
194  }
195 }
196 
197 void SoundEditWidget::saveSound()
198 {
199  const QString fileName = KFileDialog::getSaveFileName(KUrl(), QLatin1String("*.wav"), this, QString(), KFileDialog::ConfirmOverwrite);
200  if (!fileName.isEmpty()) {
201  QFile file(fileName);
202  if (file.open(QIODevice::WriteOnly)) {
203  file.write(mSound);
204  file.close();
205  }
206  }
207 }
208 
209 void SoundEditWidget::deleteSound()
210 {
211  mHasSound = false;
212  mSound = QByteArray();
213  updateView();
214 }
215 
216 SoundLoader *SoundEditWidget::soundLoader()
217 {
218  if (!mSoundLoader) {
219  mSoundLoader = new SoundLoader;
220  }
221 
222  return mSoundLoader;
223 }
QWidget
QByteArray
Phonon::MediaObject::play
void play()
QMenu::addAction
void addAction(QAction *action)
QBuffer::setData
void setData(const QByteArray &data)
QBuffer
Phonon::createPlayer
MediaObject * createPlayer(Phonon::Category category, const MediaSource &source)
QContextMenuEvent::globalPos
const QPoint & globalPos() const
QFile
QContextMenuEvent
Phonon::MediaObject
QString::isEmpty
bool isEmpty() const
QString
QMenu::exec
QAction * exec()
QToolButton
QMenu
QObject::setParent
void setParent(QObject *parent)
QUrl
Phonon::MediaObject::setCurrentSource
void setCurrentSource(const MediaSource &source)
QLatin1String
QByteArray::data
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal