22 #include "soundeditwidget.h"
24 #include <kabc/addressee.h>
25 #include <kfiledialog.h>
27 #include <kio/netaccess.h>
29 #include <kmessagebox.h>
32 #include <phonon/mediaobject.h>
35 #include <QtCore/QBuffer>
36 #include <QtGui/QContextMenuEvent>
37 #include <QtGui/QMenu>
45 SoundLoader( QWidget *parent = 0 );
47 QByteArray loadSound(
const KUrl &url,
bool *ok );
55 SoundLoader::SoundLoader( QWidget *parent )
60 QByteArray SoundLoader::loadSound(
const KUrl &url,
bool *ok )
70 if ( url.isLocalFile() ) {
71 QFile file( url.toLocalFile() );
72 if ( file.open( QIODevice::ReadOnly ) ) {
73 sound = file.readAll();
77 }
else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
78 QFile file( tempFile );
79 if ( file.open( QIODevice::ReadOnly ) ) {
80 sound = file.readAll();
84 KIO::NetAccess::removeTempFile( tempFile );
88 KMessageBox::sorry( mParent, i18n(
"This contact's sound cannot be found." ) );
100 SoundEditWidget::SoundEditWidget( QWidget *parent )
101 : QToolButton( parent ),
106 connect(
this, SIGNAL(clicked()), SLOT(playSound()) );
111 SoundEditWidget::~SoundEditWidget()
116 void SoundEditWidget::loadContact(
const KABC::Addressee &contact )
118 const KABC::Sound sound = contact.sound();
119 if ( sound.isIntern() && !sound.data().isEmpty() ) {
121 mSound = sound.data();
127 void SoundEditWidget::storeContact( KABC::Addressee &contact )
const
129 KABC::Sound sound( contact.sound() );
130 sound.setData( mSound );
131 contact.setSound( sound );
134 void SoundEditWidget::setReadOnly(
bool readOnly )
136 mReadOnly = readOnly;
139 void SoundEditWidget::updateView()
142 setIcon( KIcon( QLatin1String(
"audio-volume-medium" ) ) );
143 setToolTip( i18n(
"Click to play pronunciation" ) );
145 setIcon( KIcon( QLatin1String(
"audio-volume-muted" ) ) );
146 setToolTip( i18n(
"No pronunciation available" ) );
150 void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
155 menu.addAction( i18n(
"Play" ),
this, SLOT(playSound()) );
158 menu.addAction( i18n(
"Change..." ),
this, SLOT(changeSound()) );
161 menu.addAction( i18n(
"Save..." ),
this, SLOT(saveSound()) );
164 menu.addAction( i18n(
"Remove" ),
this, SLOT(deleteSound()) );
167 menu.exec( event->globalPos() );
170 void SoundEditWidget::playSound()
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()) );
186 void SoundEditWidget::changeSound()
188 const KUrl url = KFileDialog::getOpenUrl( QString(), QLatin1String(
"*.wav" ),
this );
189 if ( url.isValid() ) {
191 const QByteArray sound = soundLoader()->loadSound( url, &ok );
200 void SoundEditWidget::saveSound()
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 );
212 void SoundEditWidget::deleteSound()
215 mSound = QByteArray();
219 SoundLoader* SoundEditWidget::soundLoader()
222 mSoundLoader =
new SoundLoader;
227 #include "soundeditwidget.moc"