akonadi/contact
soundeditwidget.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "soundeditwidget.h"
00023
00024 #include <kabc/addressee.h>
00025 #include <kfiledialog.h>
00026 #include <kicon.h>
00027 #include <kio/netaccess.h>
00028 #include <klocale.h>
00029 #include <kmessagebox.h>
00030 #include <phonon/mediaobject.h>
00031
00032 #include <QtCore/QBuffer>
00033 #include <QtGui/QContextMenuEvent>
00034 #include <QtGui/QMenu>
00035
00039 class SoundLoader
00040 {
00041 public:
00042 SoundLoader( QWidget *parent = 0 );
00043
00044 QByteArray loadSound( const KUrl &url, bool *ok );
00045
00046 private:
00047 QByteArray mSound;
00048 QWidget *mParent;
00049 };
00050
00051
00052 SoundLoader::SoundLoader( QWidget *parent )
00053 : mParent( parent )
00054 {
00055 }
00056
00057 QByteArray SoundLoader::loadSound( const KUrl &url, bool *ok )
00058 {
00059 QByteArray sound;
00060 QString tempFile;
00061
00062 if ( url.isEmpty() )
00063 return sound;
00064
00065 (*ok) = false;
00066
00067 if ( url.isLocalFile() ) {
00068 QFile file( url.toLocalFile() );
00069 if ( file.open( QIODevice::ReadOnly ) ) {
00070 sound = file.readAll();
00071 file.close();
00072 (*ok) = true;
00073 }
00074 } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
00075 QFile file( tempFile );
00076 if ( file.open( QIODevice::ReadOnly ) ) {
00077 sound = file.readAll();
00078 file.close();
00079 (*ok) = true;
00080 }
00081 KIO::NetAccess::removeTempFile( tempFile );
00082 }
00083
00084 if ( !(*ok) ) {
00085 KMessageBox::sorry( mParent, i18n( "This contact's sound cannot be found." ) );
00086 return sound;
00087 }
00088
00089 (*ok) = true;
00090
00091 return sound;
00092 }
00093
00094
00095
00096
00097 SoundEditWidget::SoundEditWidget( QWidget *parent )
00098 : QToolButton( parent ),
00099 mHasSound( false ),
00100 mReadOnly( false ),
00101 mSoundLoader( 0 )
00102 {
00103 connect( this, SIGNAL( clicked() ), SLOT( playSound() ) );
00104
00105 updateView();
00106 }
00107
00108 SoundEditWidget::~SoundEditWidget()
00109 {
00110 delete mSoundLoader;
00111 }
00112
00113 void SoundEditWidget::loadContact( const KABC::Addressee &contact )
00114 {
00115 const KABC::Sound sound = contact.sound();
00116 if ( sound.isIntern() && !sound.data().isEmpty() ) {
00117 mHasSound = true;
00118 mSound = sound.data();
00119 }
00120
00121 updateView();
00122 }
00123
00124 void SoundEditWidget::storeContact( KABC::Addressee &contact ) const
00125 {
00126 KABC::Sound sound( contact.sound() );
00127 sound.setData( mSound );
00128 contact.setSound( sound );
00129 }
00130
00131 void SoundEditWidget::setReadOnly( bool readOnly )
00132 {
00133 mReadOnly = readOnly;
00134 }
00135
00136 void SoundEditWidget::updateView()
00137 {
00138 if ( mHasSound ) {
00139 setIcon( KIcon( QLatin1String( "audio-volume-medium" ) ) );
00140 setToolTip( i18n( "Click to play pronunciation" ) );
00141 } else {
00142 setIcon( KIcon( QLatin1String( "audio-volume-muted" ) ) );
00143 setToolTip( i18n( "No pronunciation available" ) );
00144 }
00145 }
00146
00147 void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
00148 {
00149 QMenu menu;
00150
00151 if ( mHasSound )
00152 menu.addAction( i18n( "Play" ), this, SLOT( playSound() ) );
00153
00154 if ( !mReadOnly )
00155 menu.addAction( i18n( "Change..." ), this, SLOT( changeSound() ) );
00156
00157 if ( mHasSound ) {
00158 menu.addAction( i18n( "Save..." ), this, SLOT( saveSound() ) );
00159
00160 if ( !mReadOnly )
00161 menu.addAction( i18n( "Remove" ), this, SLOT( deleteSound() ) );
00162 }
00163
00164 menu.exec( event->globalPos() );
00165 }
00166
00167 void SoundEditWidget::playSound()
00168 {
00169 if ( !mHasSound )
00170 return;
00171
00172 Phonon::MediaObject* player = Phonon::createPlayer( Phonon::NotificationCategory );
00173 QBuffer* soundData = new QBuffer( player );
00174 soundData->setData( mSound );
00175 player->setCurrentSource( soundData );
00176 player->setParent( this );
00177 connect( player, SIGNAL( finished() ), player, SLOT( deleteLater() ) );
00178 player->play();
00179 }
00180
00181 void SoundEditWidget::changeSound()
00182 {
00183 const KUrl url = KFileDialog::getOpenUrl( QString(), QLatin1String( "*.wav" ), this );
00184 if ( url.isValid() ) {
00185 bool ok = false;
00186 const QByteArray sound = soundLoader()->loadSound( url, &ok );
00187 if ( ok ) {
00188 mSound = sound;
00189 mHasSound = true;
00190 updateView();
00191 }
00192 }
00193 }
00194
00195 void SoundEditWidget::saveSound()
00196 {
00197 const QString fileName = KFileDialog::getSaveFileName( KUrl(), QLatin1String( "*.wav" ), this );
00198 if ( !fileName.isEmpty() ) {
00199 QFile file( fileName );
00200 if ( file.open( QIODevice::WriteOnly ) ) {
00201 file.write( mSound );
00202 file.close();
00203 }
00204 }
00205 }
00206
00207 void SoundEditWidget::deleteSound()
00208 {
00209 mHasSound = false;
00210 mSound = QByteArray();
00211 updateView();
00212 }
00213
00214 SoundLoader* SoundEditWidget::soundLoader()
00215 {
00216 if ( !mSoundLoader )
00217 mSoundLoader = new SoundLoader;
00218
00219 return mSoundLoader;
00220 }
00221
00222 #include "soundeditwidget.moc"