kaddressbook

soundwidget.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2003 - 2004 Tobias Koenig <tokoe@kde.org>
4
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.
9
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.
14
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.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tdeabc/sound.h>
25#include <kaudioplayer.h>
26#include <kdebug.h>
27#include <kdialog.h>
28#include <kiconloader.h>
29#include <tdeio/netaccess.h>
30#include <tdelocale.h>
31#include <tdetempfile.h>
32#include <kurlrequester.h>
33
34#include <tqcheckbox.h>
35#include <tqlabel.h>
36#include <tqlayout.h>
37#include <tqpushbutton.h>
38#include <tqwhatsthis.h>
39
40#include "soundwidget.h"
41
42SoundWidget::SoundWidget( TDEABC::AddressBook *ab, TQWidget *parent, const char *name )
43 : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false )
44{
45 TQGridLayout *topLayout = new TQGridLayout( this, 2, 3, KDialog::marginHint(),
46 KDialog::spacingHint() );
47
48 TQLabel *label = new TQLabel( this );
49 label->setPixmap( TDEGlobal::iconLoader()->loadIcon( "multimedia",
50 TDEIcon::Desktop, TDEIcon::SizeMedium ) );
51 label->setAlignment( TQt::AlignTop );
52 topLayout->addMultiCellWidget( label, 0, 1, 0, 0 );
53
54 mPlayButton = new TQPushButton( i18n( "Play" ), this );
55 mPlayButton->setEnabled( false );
56 topLayout->addWidget( mPlayButton, 0, 1 );
57
58 mSoundUrl = new KURLRequester( this );
59 topLayout->addWidget( mSoundUrl, 0, 2 );
60
61 mUseSoundUrl = new TQCheckBox( i18n( "Store as URL" ), this );
62 mUseSoundUrl->setEnabled( false );
63 topLayout->addWidget( mUseSoundUrl, 1, 2 );
64
65 connect( mSoundUrl, TQ_SIGNAL( textChanged( const TQString& ) ),
66 TQ_SLOT( setModified() ) );
67 connect( mSoundUrl, TQ_SIGNAL( textChanged( const TQString& ) ),
68 TQ_SLOT( urlChanged( const TQString& ) ) );
69 connect( mUseSoundUrl, TQ_SIGNAL( toggled( bool ) ),
70 TQ_SLOT( setModified() ) );
71 connect( mUseSoundUrl, TQ_SIGNAL( toggled( bool ) ),
72 mPlayButton, TQ_SLOT( setDisabled( bool ) ) );
73 connect( mSoundUrl, TQ_SIGNAL( urlSelected( const TQString& ) ),
74 TQ_SLOT( loadSound() ) );
75 connect( mSoundUrl, TQ_SIGNAL( urlSelected( const TQString& ) ),
76 TQ_SLOT( updateGUI() ) );
77 connect( mPlayButton, TQ_SIGNAL( clicked() ),
78 TQ_SLOT( playSound() ) );
79
80 TQWhatsThis::add( this, i18n( "This field stores a sound file which contains the name of the contact to clarify the pronunciation." ) );
81 TQWhatsThis::add( mUseSoundUrl, i18n( "Save only the URL to the sound file, not the whole object." ) );
82}
83
84SoundWidget::~SoundWidget()
85{
86}
87
88void SoundWidget::loadContact( TDEABC::Addressee *addr )
89{
90 bool blocked = signalsBlocked();
91 blockSignals( true );
92
93 TDEABC::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 );
103 }
104
105 blockSignals( blocked );
106}
107
108void SoundWidget::storeContact( TDEABC::Addressee *addr )
109{
110 TDEABC::Sound sound;
111
112 if ( mUseSoundUrl->isChecked() )
113 sound.setUrl( mSoundUrl->url() );
114 else
115 sound.setData( mSound.data() );
116
117 addr->setSound( sound );
118}
119
120void SoundWidget::setReadOnly( bool readOnly )
121{
122 mReadOnly = readOnly;
123 mSoundUrl->setEnabled( !mReadOnly );
124}
125
126void SoundWidget::playSound()
127{
128 KTempFile tmp;
129
130 tmp.file()->writeBlock( mSound.data() );
131 tmp.close();
132
133 KAudioPlayer::play( tmp.name() );
134
135 // we can't remove the sound file from within the program, because
136 // KAudioPlay uses a async dcop call... :(
137}
138
139void SoundWidget::loadSound()
140{
141 TQString fileName;
142
143 KURL url( mSoundUrl->url() );
144
145 if ( url.isEmpty() )
146 return;
147
148 if ( url.isLocalFile() )
149 fileName = url.path();
150 else if ( !TDEIO::NetAccess::download( url, fileName, this ) )
151 return;
152
153 TQFile file( fileName );
154 if ( !file.open( IO_ReadOnly ) )
155 return;
156
157 mSound.setData( file.readAll() );
158
159 file.close();
160
161 if ( !url.isLocalFile() )
162 TDEIO::NetAccess::removeTempFile( fileName );
163}
164
165void SoundWidget::updateGUI()
166{
167 mUseSoundUrl->setEnabled( !mReadOnly );
168}
169
170void SoundWidget::urlChanged( const TQString &url )
171{
172 if ( !mUseSoundUrl->isChecked() ) {
173 bool state = !url.isEmpty();
174 mPlayButton->setEnabled( state );
175 mUseSoundUrl->setEnabled( state && !mSound.isIntern() );
176 }
177}
178
179#include "soundwidget.moc"