24 #include <tdeabc/sound.h>
25 #include <kaudioplayer.h>
28 #include <kiconloader.h>
29 #include <tdeio/netaccess.h>
30 #include <tdelocale.h>
31 #include <tdetempfile.h>
32 #include <kurlrequester.h>
34 #include <tqcheckbox.h>
37 #include <tqpushbutton.h>
38 #include <tqwhatsthis.h>
40 #include "soundwidget.h"
42 SoundWidget::SoundWidget( TDEABC::AddressBook *ab, TQWidget *parent,
const char *name )
43 : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false )
45 TQGridLayout *topLayout =
new TQGridLayout(
this, 2, 3, KDialog::marginHint(),
46 KDialog::spacingHint() );
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 );
54 mPlayButton =
new TQPushButton( 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 TQCheckBox( i18n(
"Store as URL" ),
this );
62 mUseSoundUrl->setEnabled(
false );
63 topLayout->addWidget( mUseSoundUrl, 1, 2 );
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() ) );
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." ) );
84 SoundWidget::~SoundWidget()
88 void SoundWidget::loadContact( TDEABC::Addressee *addr )
90 bool blocked = signalsBlocked();
93 TDEABC::Sound sound = addr->sound();
94 if ( sound.isIntern() ) {
95 mSound.setData( sound.data() );
96 mPlayButton->setEnabled(
true );
97 mUseSoundUrl->setChecked(
false );
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( TDEABC::Addressee *addr )
112 if ( mUseSoundUrl->isChecked() )
113 sound.setUrl( mSoundUrl->url() );
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()
130 tmp.file()->writeBlock( mSound.data() );
133 KAudioPlayer::play( tmp.name() );
139 void SoundWidget::loadSound()
143 KURL url( mSoundUrl->url() );
148 if ( url.isLocalFile() )
149 fileName = url.path();
150 else if ( !TDEIO::NetAccess::download( url, fileName,
this ) )
153 TQFile file( fileName );
154 if ( !file.open( IO_ReadOnly ) )
157 mSound.setData( file.readAll() );
161 if ( !url.isLocalFile() )
162 TDEIO::NetAccess::removeTempFile( fileName );
165 void SoundWidget::updateGUI()
167 mUseSoundUrl->setEnabled( !mReadOnly );
170 void SoundWidget::urlChanged(
const TQString &url )
172 if ( !mUseSoundUrl->isChecked() ) {
173 bool state = !url.isEmpty();
174 mPlayButton->setEnabled( state );
175 mUseSoundUrl->setEnabled( state && !mSound.isIntern() );
179 #include "soundwidget.moc"