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

akonadi

  • 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 <klocale.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 <QtGui/QContextMenuEvent>
37 #include <QtGui/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  (*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 
98 
99 
100 SoundEditWidget::SoundEditWidget( QWidget *parent )
101  : QToolButton( parent ),
102  mHasSound( false ),
103  mReadOnly( false ),
104  mSoundLoader( 0 )
105 {
106  connect( this, SIGNAL(clicked()), SLOT(playSound()) );
107 
108  updateView();
109 }
110 
111 SoundEditWidget::~SoundEditWidget()
112 {
113  delete mSoundLoader;
114 }
115 
116 void SoundEditWidget::loadContact( const KABC::Addressee &contact )
117 {
118  const KABC::Sound sound = contact.sound();
119  if ( sound.isIntern() && !sound.data().isEmpty() ) {
120  mHasSound = true;
121  mSound = sound.data();
122  }
123 
124  updateView();
125 }
126 
127 void SoundEditWidget::storeContact( KABC::Addressee &contact ) const
128 {
129  KABC::Sound sound( contact.sound() );
130  sound.setData( mSound );
131  contact.setSound( sound );
132 }
133 
134 void SoundEditWidget::setReadOnly( bool readOnly )
135 {
136  mReadOnly = readOnly;
137 }
138 
139 void SoundEditWidget::updateView()
140 {
141  if ( mHasSound ) {
142  setIcon( KIcon( QLatin1String( "audio-volume-medium" ) ) );
143  setToolTip( i18n( "Click to play pronunciation" ) );
144  } else {
145  setIcon( KIcon( QLatin1String( "audio-volume-muted" ) ) );
146  setToolTip( i18n( "No pronunciation available" ) );
147  }
148 }
149 
150 void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
151 {
152  QMenu menu;
153 
154  if ( mHasSound )
155  menu.addAction( i18n( "Play" ), this, SLOT(playSound()) );
156 
157  if ( !mReadOnly )
158  menu.addAction( i18n( "Change..." ), this, SLOT(changeSound()) );
159 
160  if ( mHasSound ) {
161  menu.addAction( i18n( "Save..." ), this, SLOT(saveSound()) );
162 
163  if ( !mReadOnly )
164  menu.addAction( i18n( "Remove" ), this, SLOT(deleteSound()) );
165  }
166 
167  menu.exec( event->globalPos() );
168 }
169 
170 void SoundEditWidget::playSound()
171 {
172  if ( !mHasSound )
173  return;
174 
175 #ifndef Q_OS_WINCE
176  Phonon::MediaObject* player = Phonon::createPlayer( Phonon::NotificationCategory );
177  QBuffer* soundData = new QBuffer( player );
178  soundData->setData( mSound );
179  player->setCurrentSource( soundData );
180  player->setParent( this );
181  connect( player, SIGNAL(finished()), player, SLOT(deleteLater()) );
182  player->play();
183 #endif
184 }
185 
186 void SoundEditWidget::changeSound()
187 {
188  const KUrl url = KFileDialog::getOpenUrl( QString(), QLatin1String( "*.wav" ), this );
189  if ( url.isValid() ) {
190  bool ok = false;
191  const QByteArray sound = soundLoader()->loadSound( url, &ok );
192  if ( ok ) {
193  mSound = sound;
194  mHasSound = true;
195  updateView();
196  }
197  }
198 }
199 
200 void SoundEditWidget::saveSound()
201 {
202  const QString fileName = KFileDialog::getSaveFileName( KUrl(), QLatin1String( "*.wav" ), this );
203  if ( !fileName.isEmpty() ) {
204  QFile file( fileName );
205  if ( file.open( QIODevice::WriteOnly ) ) {
206  file.write( mSound );
207  file.close();
208  }
209  }
210 }
211 
212 void SoundEditWidget::deleteSound()
213 {
214  mHasSound = false;
215  mSound = QByteArray();
216  updateView();
217 }
218 
219 SoundLoader* SoundEditWidget::soundLoader()
220 {
221  if ( !mSoundLoader )
222  mSoundLoader = new SoundLoader;
223 
224  return mSoundLoader;
225 }
226 
227 #include "soundeditwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:08 by doxygen 1.8.1.2 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-4.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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