Need to store config when changing units, if not very bad things happen
[kdepim.git] / kaddressbook / soundwidget.cpp
blob74b8137efa4da5cbe9449b7a48338c046d19c495
1 /*
2 This file is part of KAddressBook.
3 Copyright (c) 2003 - 2004 Tobias Koenig <tokoe@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
24 #include <kabc/sound.h>
25 #include <kaudioplayer.h>
26 #include <kdebug.h>
27 #include <kdialog.h>
28 #include <kiconloader.h>
29 #include <kio/netaccess.h>
30 #include <klocale.h>
31 #include <ktempfile.h>
32 #include <kurlrequester.h>
34 #include <qcheckbox.h>
35 #include <qlabel.h>
36 #include <qlayout.h>
37 #include <qpushbutton.h>
38 #include <qwhatsthis.h>
40 #include "soundwidget.h"
42 SoundWidget::SoundWidget( KABC::AddressBook *ab, QWidget *parent, const char *name )
43 : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false )
45 QGridLayout *topLayout = new QGridLayout( this, 2, 3, KDialog::marginHint(),
46 KDialog::spacingHint() );
48 QLabel *label = new QLabel( this );
49 label->setPixmap( KGlobal::iconLoader()->loadIcon( "multimedia",
50 KIcon::Desktop, KIcon::SizeMedium ) );
51 label->setAlignment( Qt::AlignTop );
52 topLayout->addMultiCellWidget( label, 0, 1, 0, 0 );
54 mPlayButton = new QPushButton( i18n( "Play" ), this );
55 mPlayButton->setEnabled( false );
56 topLayout->addWidget( mPlayButton, 0, 1 );
58 mSoundUrl = new KURLRequester( this );
59 topLayout->addWidget( mSoundUrl, 0, 2 );
61 mUseSoundUrl = new QCheckBox( i18n( "Store as URL" ), this );
62 mUseSoundUrl->setEnabled( false );
63 topLayout->addWidget( mUseSoundUrl, 1, 2 );
65 connect( mSoundUrl, SIGNAL( textChanged( const QString& ) ),
66 SLOT( setModified() ) );
67 connect( mSoundUrl, SIGNAL( textChanged( const QString& ) ),
68 SLOT( urlChanged( const QString& ) ) );
69 connect( mUseSoundUrl, SIGNAL( toggled( bool ) ),
70 SLOT( setModified() ) );
71 connect( mUseSoundUrl, SIGNAL( toggled( bool ) ),
72 mPlayButton, SLOT( setDisabled( bool ) ) );
73 connect( mSoundUrl, SIGNAL( urlSelected( const QString& ) ),
74 SLOT( loadSound() ) );
75 connect( mSoundUrl, SIGNAL( urlSelected( const QString& ) ),
76 SLOT( updateGUI() ) );
77 connect( mPlayButton, SIGNAL( clicked() ),
78 SLOT( playSound() ) );
80 QWhatsThis::add( this, i18n( "This field stores a sound file which contains the name of the contact to clarify the pronunciation." ) );
81 QWhatsThis::add( mUseSoundUrl, i18n( "Save only the URL to the sound file, not the whole object." ) );
84 SoundWidget::~SoundWidget()
88 void SoundWidget::loadContact( KABC::Addressee *addr )
90 bool blocked = signalsBlocked();
91 blockSignals( true );
93 KABC::Sound sound = addr->sound();
94 if ( sound.isIntern() ) {
95 mSound.setData( sound.data() );
96 mPlayButton->setEnabled( true );
97 mUseSoundUrl->setChecked( false );
98 } else {
99 mSoundUrl->setURL( sound.url() );
100 mPlayButton->setEnabled( false );
101 if ( !sound.url().isEmpty() )
102 mUseSoundUrl->setChecked( true );
105 blockSignals( blocked );
108 void SoundWidget::storeContact( KABC::Addressee *addr )
110 KABC::Sound sound;
112 if ( mUseSoundUrl->isChecked() )
113 sound.setUrl( mSoundUrl->url() );
114 else
115 sound.setData( mSound.data() );
117 addr->setSound( sound );
120 void SoundWidget::setReadOnly( bool readOnly )
122 mReadOnly = readOnly;
123 mSoundUrl->setEnabled( !mReadOnly );
126 void SoundWidget::playSound()
128 KTempFile tmp;
130 tmp.file()->writeBlock( mSound.data() );
131 tmp.close();
133 KAudioPlayer::play( tmp.name() );
135 // we can't remove the sound file from within the program, because
136 // KAudioPlay uses a async dcop call... :(
139 void SoundWidget::loadSound()
141 QString fileName;
143 KURL url( mSoundUrl->url() );
145 if ( url.isEmpty() )
146 return;
148 if ( url.isLocalFile() )
149 fileName = url.path();
150 else if ( !KIO::NetAccess::download( url, fileName, this ) )
151 return;
153 QFile file( fileName );
154 if ( !file.open( IO_ReadOnly ) )
155 return;
157 mSound.setData( file.readAll() );
159 file.close();
161 if ( !url.isLocalFile() )
162 KIO::NetAccess::removeTempFile( fileName );
165 void SoundWidget::updateGUI()
167 mUseSoundUrl->setEnabled( !mReadOnly );
170 void SoundWidget::urlChanged( const QString &url )
172 if ( !mUseSoundUrl->isChecked() ) {
173 bool state = !url.isEmpty();
174 mPlayButton->setEnabled( state );
175 mUseSoundUrl->setEnabled( state && !mSound.isIntern() );
179 #include "soundwidget.moc"