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( QWidget *parent, const char *name )
00042   : QWidget( 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            SIGNAL( changed() ) );
00066   connect( mSoundUrl, SIGNAL( textChanged( const QString& ) ),
00067            SLOT( urlChanged( const QString& ) ) );
00068   connect( mUseSoundUrl, SIGNAL( toggled( bool ) ),
00069            SIGNAL( changed() ) );
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::setReadOnly( bool readOnly )
00085 {
00086   mReadOnly = readOnly;
00087   mSoundUrl->setEnabled( !mReadOnly );
00088 }
00089 
00090 void SoundWidget::setSound( const KABC::Sound &sound )
00091 {
00092   bool blocked = signalsBlocked();
00093   blockSignals( true );
00094 
00095   if ( sound.isIntern() ) {
00096     mSound.setData( sound.data() );
00097     mPlayButton->setEnabled( true );
00098     mUseSoundUrl->setChecked( false );
00099   } else {
00100     mSoundUrl->setURL( sound.url() );
00101     mPlayButton->setEnabled( false );
00102     if ( !sound.url().isEmpty() )
00103       mUseSoundUrl->setChecked( true );
00104   }
00105 
00106   blockSignals( blocked );
00107 }
00108 
00109 KABC::Sound SoundWidget::sound() const
00110 {
00111   KABC::Sound sound;
00112 
00113   if ( mUseSoundUrl->isChecked() )
00114     sound.setUrl( mSoundUrl->url() );
00115   else
00116     sound.setData( mSound.data() );
00117 
00118   return sound;
00119 }
00120 
00121 void SoundWidget::playSound()
00122 {
00123   KTempFile tmp;
00124 
00125   tmp.file()->writeBlock( mSound.data() );
00126   tmp.close();
00127 
00128   KAudioPlayer::play( tmp.name() );
00129 
00130   // we can't remove the sound file from within the program, because
00131   // KAudioPlay uses a async dcop call... :(
00132 }
00133 
00134 void SoundWidget::loadSound()
00135 {
00136   QString fileName;
00137 
00138   KURL url( mSoundUrl->url() );
00139 
00140   if ( url.isEmpty() )
00141     return;
00142 
00143   if ( url.isLocalFile() )
00144     fileName = url.path();
00145   else if ( !KIO::NetAccess::download( url, fileName, this ) )
00146     return;
00147 
00148   QFile file( fileName );
00149   if ( !file.open( IO_ReadOnly ) )
00150     return;
00151 
00152   mSound.setData( file.readAll() );
00153 
00154   file.close();
00155 
00156   if ( !url.isLocalFile() )
00157     KIO::NetAccess::removeTempFile( fileName );
00158 }
00159 
00160 void SoundWidget::updateGUI()
00161 {
00162   mUseSoundUrl->setEnabled( !mReadOnly );
00163 }
00164 
00165 void SoundWidget::urlChanged( const QString &url )
00166 {
00167   if ( !mUseSoundUrl->isChecked() ) {
00168     bool state = !url.isEmpty();
00169     mPlayButton->setEnabled( state );
00170     mUseSoundUrl->setEnabled( state && !mSound.isIntern() );
00171   }
00172 }
00173 
00174 #include "soundwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:53 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003