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

akonadi

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

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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