00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <kabc/sound.h>
00025 #include <kaudioplayer.h>
00026 #include <kdebug.h>
00027 #include <kdialog.h>
00028 #include <kiconloader.h>
00029 #include <kio/netaccess.h>
00030 #include <klocale.h>
00031 #include <ktempfile.h>
00032 #include <kurlrequester.h>
00033
00034 #include <qcheckbox.h>
00035 #include <qlabel.h>
00036 #include <qlayout.h>
00037 #include <qpushbutton.h>
00038 #include <qwhatsthis.h>
00039
00040 #include "soundwidget.h"
00041
00042 SoundWidget::SoundWidget( KABC::AddressBook *ab, QWidget *parent, const char *name )
00043 : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false )
00044 {
00045 QGridLayout *topLayout = new QGridLayout( this, 2, 3, KDialog::marginHint(),
00046 KDialog::spacingHint() );
00047
00048 QLabel *label = new QLabel( this );
00049 label->setPixmap( KGlobal::iconLoader()->loadIcon( "multimedia",
00050 KIcon::Desktop, KIcon::SizeMedium ) );
00051 label->setAlignment( Qt::AlignTop );
00052 topLayout->addMultiCellWidget( label, 0, 1, 0, 0 );
00053
00054 mPlayButton = new QPushButton( i18n( "Play" ), this );
00055 mPlayButton->setEnabled( false );
00056 topLayout->addWidget( mPlayButton, 0, 1 );
00057
00058 mSoundUrl = new KURLRequester( this );
00059 topLayout->addWidget( mSoundUrl, 0, 2 );
00060
00061 mUseSoundUrl = new QCheckBox( i18n( "Store as URL" ), this );
00062 mUseSoundUrl->setEnabled( false );
00063 topLayout->addWidget( mUseSoundUrl, 1, 2 );
00064
00065 connect( mSoundUrl, SIGNAL( textChanged( const QString& ) ),
00066 SLOT( setModified() ) );
00067 connect( mSoundUrl, SIGNAL( textChanged( const QString& ) ),
00068 SLOT( urlChanged( const QString& ) ) );
00069 connect( mUseSoundUrl, SIGNAL( toggled( bool ) ),
00070 SLOT( setModified() ) );
00071 connect( mUseSoundUrl, SIGNAL( toggled( bool ) ),
00072 mPlayButton, SLOT( setDisabled( bool ) ) );
00073 connect( mSoundUrl, SIGNAL( urlSelected( const QString& ) ),
00074 SLOT( loadSound() ) );
00075 connect( mSoundUrl, SIGNAL( urlSelected( const QString& ) ),
00076 SLOT( updateGUI() ) );
00077 connect( mPlayButton, SIGNAL( clicked() ),
00078 SLOT( playSound() ) );
00079
00080 QWhatsThis::add( this, i18n( "This field stores a sound file which contains the name of the contact to clarify the pronunciation." ) );
00081 QWhatsThis::add( mUseSoundUrl, i18n( "Save only the URL to the sound file, not the whole object." ) );
00082 }
00083
00084 SoundWidget::~SoundWidget()
00085 {
00086 }
00087
00088 void SoundWidget::loadContact( KABC::Addressee *addr )
00089 {
00090 bool blocked = signalsBlocked();
00091 blockSignals( true );
00092
00093 KABC::Sound sound = addr->sound();
00094 if ( sound.isIntern() ) {
00095 mSound.setData( sound.data() );
00096 mPlayButton->setEnabled( true );
00097 mUseSoundUrl->setChecked( false );
00098 } else {
00099 mSoundUrl->setURL( sound.url() );
00100 mPlayButton->setEnabled( false );
00101 if ( !sound.url().isEmpty() )
00102 mUseSoundUrl->setChecked( true );
00103 }
00104
00105 blockSignals( blocked );
00106 }
00107
00108 void SoundWidget::storeContact( KABC::Addressee *addr )
00109 {
00110 KABC::Sound sound;
00111
00112 if ( mUseSoundUrl->isChecked() )
00113 sound.setUrl( mSoundUrl->url() );
00114 else
00115 sound.setData( mSound.data() );
00116
00117 addr->setSound( sound );
00118 }
00119
00120 void SoundWidget::setReadOnly( bool readOnly )
00121 {
00122 mReadOnly = readOnly;
00123 mSoundUrl->setEnabled( !mReadOnly );
00124 }
00125
00126 void SoundWidget::playSound()
00127 {
00128 KTempFile tmp;
00129
00130 tmp.file()->writeBlock( mSound.data() );
00131 tmp.close();
00132
00133 KAudioPlayer::play( tmp.name() );
00134
00135
00136
00137 }
00138
00139 void SoundWidget::loadSound()
00140 {
00141 QString fileName;
00142
00143 KURL url( mSoundUrl->url() );
00144
00145 if ( url.isEmpty() )
00146 return;
00147
00148 if ( url.isLocalFile() )
00149 fileName = url.path();
00150 else if ( !KIO::NetAccess::download( url, fileName, this ) )
00151 return;
00152
00153 QFile file( fileName );
00154 if ( !file.open( IO_ReadOnly ) )
00155 return;
00156
00157 mSound.setData( file.readAll() );
00158
00159 file.close();
00160
00161 if ( !url.isLocalFile() )
00162 KIO::NetAccess::removeTempFile( fileName );
00163 }
00164
00165 void SoundWidget::updateGUI()
00166 {
00167 mUseSoundUrl->setEnabled( !mReadOnly );
00168 }
00169
00170 void SoundWidget::urlChanged( const QString &url )
00171 {
00172 if ( !mUseSoundUrl->isChecked() ) {
00173 bool state = !url.isEmpty();
00174 mPlayButton->setEnabled( state );
00175 mUseSoundUrl->setEnabled( state && !mSound.isIntern() );
00176 }
00177 }
00178
00179 #include "soundwidget.moc"