kaddressbook Library API Documentation

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 
00039 #include "soundwidget.h"
00040 
00041 SoundWidget::SoundWidget( KABC::AddressBook *ab, QWidget *parent, const char *name )
00042   : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false )
00043 {
00044   QGridLayout *topLayout = new QGridLayout( this, 2, 3, KDialog::marginHint(),
00045                                             KDialog::spacingHint() );
00046 
00047   QLabel *label = new QLabel( this );
00048   label->setPixmap( KGlobal::iconLoader()->loadIcon( "multimedia",
00049                     KIcon::Desktop, KIcon::SizeMedium ) );
00050   label->setAlignment( Qt::AlignTop );
00051   topLayout->addMultiCellWidget( label, 0, 1, 0, 0 );
00052 
00053   mPlayButton = new QPushButton( i18n( "Play" ), this );
00054   mPlayButton->setEnabled( false );
00055   topLayout->addWidget( mPlayButton, 0, 1 );
00056 
00057   mSoundUrl = new KURLRequester( this );
00058   topLayout->addWidget( mSoundUrl, 0, 2 );
00059 
00060   mUseSoundUrl = new QCheckBox( i18n( "Store as URL" ), this );
00061   mUseSoundUrl->setEnabled( false );
00062   topLayout->addWidget( mUseSoundUrl, 1, 2 );
00063 
00064   connect( mSoundUrl, SIGNAL( textChanged( const QString& ) ),
00065            SLOT( setModified() ) );
00066   connect( mSoundUrl, SIGNAL( textChanged( const QString& ) ),
00067            SLOT( urlChanged( const QString& ) ) );
00068   connect( mUseSoundUrl, SIGNAL( toggled( bool ) ),
00069            SLOT( setModified() ) );
00070   connect( mUseSoundUrl, SIGNAL( toggled( bool ) ),
00071            mPlayButton, SLOT( setDisabled( bool ) ) );
00072   connect( mSoundUrl, SIGNAL( urlSelected( const QString& ) ),
00073            SLOT( loadSound() ) );
00074   connect( mSoundUrl, SIGNAL( urlSelected( const QString& ) ),
00075            SLOT( updateGUI() ) );
00076   connect( mPlayButton, SIGNAL( clicked() ),
00077            SLOT( playSound() ) );
00078 }
00079 
00080 SoundWidget::~SoundWidget()
00081 {
00082 }
00083 
00084 void SoundWidget::loadContact( KABC::Addressee *addr )
00085 {
00086   bool blocked = signalsBlocked();
00087   blockSignals( true );
00088 
00089   KABC::Sound sound = addr->sound();
00090   if ( sound.isIntern() ) {
00091     mSound.setData( sound.data() );
00092     mPlayButton->setEnabled( true );
00093     mUseSoundUrl->setChecked( false );
00094   } else {
00095     mSoundUrl->setURL( sound.url() );
00096     mPlayButton->setEnabled( false );
00097     if ( !sound.url().isEmpty() )
00098       mUseSoundUrl->setChecked( true );
00099   }
00100 
00101   blockSignals( blocked );
00102 }
00103 
00104 void SoundWidget::storeContact( KABC::Addressee *addr )
00105 {
00106   KABC::Sound sound;
00107 
00108   if ( mUseSoundUrl->isChecked() )
00109     sound.setUrl( mSoundUrl->url() );
00110   else
00111     sound.setData( mSound.data() );
00112 
00113   addr->setSound( sound );
00114 }
00115 
00116 void SoundWidget::setReadOnly( bool readOnly )
00117 {
00118   mReadOnly = readOnly;
00119   mSoundUrl->setEnabled( !mReadOnly );
00120 }
00121 
00122 void SoundWidget::playSound()
00123 {
00124   KTempFile tmp;
00125 
00126   tmp.file()->writeBlock( mSound.data() );
00127   tmp.close();
00128 
00129   KAudioPlayer::play( tmp.name() );
00130 
00131   // we can't remove the sound file from within the program, because
00132   // KAudioPlay uses a async dcop call... :(
00133 }
00134 
00135 void SoundWidget::loadSound()
00136 {
00137   QString fileName;
00138 
00139   KURL url( mSoundUrl->url() );
00140 
00141   if ( url.isEmpty() )
00142     return;
00143 
00144   if ( url.isLocalFile() )
00145     fileName = url.path();
00146   else if ( !KIO::NetAccess::download( url, fileName, this ) )
00147     return;
00148 
00149   QFile file( fileName );
00150   if ( !file.open( IO_ReadOnly ) )
00151     return;
00152 
00153   mSound.setData( file.readAll() );
00154 
00155   file.close();
00156 
00157   if ( !url.isLocalFile() )
00158     KIO::NetAccess::removeTempFile( fileName );
00159 }
00160 
00161 void SoundWidget::updateGUI()
00162 {
00163   mUseSoundUrl->setEnabled( !mReadOnly );
00164 }
00165 
00166 void SoundWidget::urlChanged( const QString &url )
00167 {
00168   if ( !mUseSoundUrl->isChecked() ) {
00169     bool state = !url.isEmpty();
00170     mPlayButton->setEnabled( state );
00171     mUseSoundUrl->setEnabled( state && !mSound.isIntern() );
00172   }
00173 }
00174 
00175 #include "soundwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:54:51 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003