kaddressbook

soundwidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2003 - 2004 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
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   // we can't remove the sound file from within the program, because
00136   // KAudioPlay uses a async dcop call... :(
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"
KDE Home | KDE Accessibility Home | Description of Access Keys