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 );
55 SoundLoader::SoundLoader( QWidget *parent )
60 QByteArray SoundLoader::loadSound(
const KUrl &url,
bool *ok )
65 if ( url.isEmpty() ) {
71 if ( url.isLocalFile() ) {
72 QFile file( url.toLocalFile() );
73 if ( file.open( QIODevice::ReadOnly ) ) {
74 sound = file.readAll();
78 }
else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
79 QFile file( tempFile );
80 if ( file.open( QIODevice::ReadOnly ) ) {
81 sound = file.readAll();
85 KIO::NetAccess::removeTempFile( tempFile );
89 KMessageBox::sorry( mParent, i18n(
"This contact's sound cannot be found." ) );
101 SoundEditWidget::SoundEditWidget( QWidget *parent )
102 : QToolButton( parent ),
107 connect(
this, SIGNAL(clicked()), SLOT(playSound()) );
112 SoundEditWidget::~SoundEditWidget()
117 void SoundEditWidget::loadContact(
const KABC::Addressee &contact )
119 const KABC::Sound sound = contact.sound();
120 if ( sound.isIntern() && !sound.data().isEmpty() ) {
122 mSound = sound.data();
128 void SoundEditWidget::storeContact( KABC::Addressee &contact )
const
130 KABC::Sound sound( contact.sound() );
131 sound.setData( mSound );
132 contact.setSound( sound );
135 void SoundEditWidget::setReadOnly(
bool readOnly )
137 mReadOnly = readOnly;
140 void SoundEditWidget::updateView()
143 setIcon( KIcon( QLatin1String(
"audio-volume-medium" ) ) );
144 setToolTip( i18n(
"Click to play pronunciation" ) );
146 setIcon( KIcon( QLatin1String(
"audio-volume-muted" ) ) );
147 setToolTip( i18n(
"No pronunciation available" ) );
151 void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
156 menu.addAction( i18n(
"Play" ),
this, SLOT(playSound()) );
160 menu.addAction( i18n(
"Change..." ),
this, SLOT(changeSound()) );
164 menu.addAction( i18n(
"Save..." ),
this, SLOT(saveSound()) );
167 menu.addAction( i18n(
"Remove" ),
this, SLOT(deleteSound()) );
171 menu.exec( event->globalPos() );
174 void SoundEditWidget::playSound()
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()) );
191 void SoundEditWidget::changeSound()
193 const KUrl url = KFileDialog::getOpenUrl( QUrl(), QLatin1String(
"*.wav" ),
this );
194 if ( url.isValid() ) {
196 const QByteArray sound = soundLoader()->loadSound( url, &ok );
205 void SoundEditWidget::saveSound()
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 );
217 void SoundEditWidget::deleteSound()
220 mSound = QByteArray();
224 SoundLoader* SoundEditWidget::soundLoader()
226 if ( !mSoundLoader ) {
227 mSoundLoader =
new SoundLoader;