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

akonadi/contact

  • 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 #ifndef Q_OS_WINCE
32 #include <phonon/mediaobject.h>
33 #endif
34 
35 #include <QtCore/QBuffer>
36 #include <QContextMenuEvent>
37 #include <QMenu>
38 
42 class SoundLoader
43 {
44  public:
45  SoundLoader( QWidget *parent = 0 );
46 
47  QByteArray loadSound( const KUrl &url, bool *ok );
48 
49  private:
50  QByteArray mSound;
51  QWidget *mParent;
52 };
53 
54 
55 SoundLoader::SoundLoader( QWidget *parent )
56  : mParent( parent )
57 {
58 }
59 
60 QByteArray SoundLoader::loadSound( const KUrl &url, bool *ok )
61 {
62  QByteArray sound;
63  QString tempFile;
64 
65  if ( url.isEmpty() ) {
66  return sound;
67  }
68 
69  ( *ok ) = false;
70 
71  if ( url.isLocalFile() ) {
72  QFile file( url.toLocalFile() );
73  if ( file.open( QIODevice::ReadOnly ) ) {
74  sound = file.readAll();
75  file.close();
76  ( *ok ) = true;
77  }
78  } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
79  QFile file( tempFile );
80  if ( file.open( QIODevice::ReadOnly ) ) {
81  sound = file.readAll();
82  file.close();
83  ( *ok ) = true;
84  }
85  KIO::NetAccess::removeTempFile( tempFile );
86  }
87 
88  if ( !( *ok ) ) {
89  KMessageBox::sorry( mParent, i18n( "This contact's sound cannot be found." ) );
90  return sound;
91  }
92 
93  ( *ok ) = true;
94 
95  return sound;
96 }
97 
98 
99 
100 
101 SoundEditWidget::SoundEditWidget( QWidget *parent )
102  : QToolButton( parent ),
103  mHasSound( false ),
104  mReadOnly( false ),
105  mSoundLoader( 0 )
106 {
107  connect( this, SIGNAL(clicked()), SLOT(playSound()) );
108 
109  updateView();
110 }
111 
112 SoundEditWidget::~SoundEditWidget()
113 {
114  delete mSoundLoader;
115 }
116 
117 void SoundEditWidget::loadContact( const KABC::Addressee &contact )
118 {
119  const KABC::Sound sound = contact.sound();
120  if ( sound.isIntern() && !sound.data().isEmpty() ) {
121  mHasSound = true;
122  mSound = sound.data();
123  }
124 
125  updateView();
126 }
127 
128 void SoundEditWidget::storeContact( KABC::Addressee &contact ) const
129 {
130  KABC::Sound sound( contact.sound() );
131  sound.setData( mSound );
132  contact.setSound( sound );
133 }
134 
135 void SoundEditWidget::setReadOnly( bool readOnly )
136 {
137  mReadOnly = readOnly;
138 }
139 
140 void SoundEditWidget::updateView()
141 {
142  if ( mHasSound ) {
143  setIcon( KIcon( QLatin1String( "audio-volume-medium" ) ) );
144  setToolTip( i18n( "Click to play pronunciation" ) );
145  } else {
146  setIcon( KIcon( QLatin1String( "audio-volume-muted" ) ) );
147  setToolTip( i18n( "No pronunciation available" ) );
148  }
149 }
150 
151 void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
152 {
153  QMenu menu;
154 
155  if ( mHasSound ) {
156  menu.addAction( i18n( "Play" ), this, SLOT(playSound()) );
157  }
158 
159  if ( !mReadOnly ) {
160  menu.addAction( i18n( "Change..." ), this, SLOT(changeSound()) );
161  }
162 
163  if ( mHasSound ) {
164  menu.addAction( i18n( "Save..." ), this, SLOT(saveSound()) );
165 
166  if ( !mReadOnly ) {
167  menu.addAction( i18n( "Remove" ), this, SLOT(deleteSound()) );
168  }
169  }
170 
171  menu.exec( event->globalPos() );
172 }
173 
174 void SoundEditWidget::playSound()
175 {
176  if ( !mHasSound ) {
177  return;
178  }
179 
180 #ifndef Q_OS_WINCE
181  Phonon::MediaObject* player = Phonon::createPlayer( Phonon::NotificationCategory );
182  QBuffer* soundData = new QBuffer( player );
183  soundData->setData( mSound );
184  player->setCurrentSource( soundData );
185  player->setParent( this );
186  connect( player, SIGNAL(finished()), player, SLOT(deleteLater()) );
187  player->play();
188 #endif
189 }
190 
191 void SoundEditWidget::changeSound()
192 {
193  const KUrl url = KFileDialog::getOpenUrl( QUrl(), QLatin1String( "*.wav" ), this );
194  if ( url.isValid() ) {
195  bool ok = false;
196  const QByteArray sound = soundLoader()->loadSound( url, &ok );
197  if ( ok ) {
198  mSound = sound;
199  mHasSound = true;
200  updateView();
201  }
202  }
203 }
204 
205 void SoundEditWidget::saveSound()
206 {
207  const QString fileName = KFileDialog::getSaveFileName( KUrl(), QLatin1String( "*.wav" ), this );
208  if ( !fileName.isEmpty() ) {
209  QFile file( fileName );
210  if ( file.open( QIODevice::WriteOnly ) ) {
211  file.write( mSound );
212  file.close();
213  }
214  }
215 }
216 
217 void SoundEditWidget::deleteSound()
218 {
219  mHasSound = false;
220  mSound = QByteArray();
221  updateView();
222 }
223 
224 SoundLoader* SoundEditWidget::soundLoader()
225 {
226  if ( !mSoundLoader ) {
227  mSoundLoader = new SoundLoader;
228  }
229 
230  return mSoundLoader;
231 }
232 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:48 by doxygen 1.8.5 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-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 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
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