22 #include "soundeditwidget.h"
24 #include <kabc/addressee.h>
25 #include <kfiledialog.h>
27 #include <kio/netaccess.h>
28 #include <klocalizedstring.h>
29 #include <kmessagebox.h>
32 #include <phonon/mediaobject.h>
35 #include <QtCore/QBuffer>
36 #include <QContextMenuEvent>
45 SoundLoader( QWidget *parent = 0 );
47 QByteArray loadSound(
const KUrl &url,
bool *ok );
54 SoundLoader::SoundLoader( QWidget *parent )
59 QByteArray SoundLoader::loadSound(
const KUrl &url,
bool *ok )
64 if ( url.isEmpty() ) {
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." ) );
97 SoundEditWidget::SoundEditWidget( QWidget *parent )
98 : QToolButton( parent ),
103 connect(
this, SIGNAL(clicked()), SLOT(playSound()) );
108 SoundEditWidget::~SoundEditWidget()
113 void SoundEditWidget::loadContact(
const KABC::Addressee &contact )
115 const KABC::Sound sound = contact.sound();
116 if ( sound.isIntern() && !sound.data().isEmpty() ) {
118 mSound = sound.data();
124 void SoundEditWidget::storeContact( KABC::Addressee &contact )
const
126 KABC::Sound sound( contact.sound() );
127 sound.setData( mSound );
128 contact.setSound( sound );
131 void SoundEditWidget::setReadOnly(
bool readOnly )
133 mReadOnly = readOnly;
136 void SoundEditWidget::updateView()
139 setIcon( KIcon( QLatin1String(
"audio-volume-medium" ) ) );
140 setToolTip( i18n(
"Click to play pronunciation" ) );
142 setIcon( KIcon( QLatin1String(
"audio-volume-muted" ) ) );
143 setToolTip( i18n(
"No pronunciation available" ) );
147 void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
152 menu.addAction( i18n(
"Play" ),
this, SLOT(playSound()) );
156 menu.addAction( i18n(
"Change..." ),
this, SLOT(changeSound()) );
160 menu.addAction( i18n(
"Save..." ),
this, SLOT(saveSound()) );
163 menu.addAction( i18n(
"Remove" ),
this, SLOT(deleteSound()) );
167 menu.exec( event->globalPos() );
170 void SoundEditWidget::playSound()
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()) );
187 void SoundEditWidget::changeSound()
189 const KUrl url = KFileDialog::getOpenUrl( QUrl(), QLatin1String(
"*.wav" ),
this );
190 if ( url.isValid() ) {
192 const QByteArray sound = soundLoader()->loadSound( url, &ok );
201 void SoundEditWidget::saveSound()
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 );
213 void SoundEditWidget::deleteSound()
216 mSound = QByteArray();
220 SoundLoader* SoundEditWidget::soundLoader()
222 if ( !mSoundLoader ) {
223 mSoundLoader =
new SoundLoader;