2
This file is part of KContactManager.
4
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation, Inc.,
18
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include "soundeditwidget.h"
23
#include <kabc/addressee.h>
24
#include <kfiledialog.h>
26
#include <kio/netaccess.h>
28
#include <kmessagebox.h>
29
#include <phonon/mediaobject.h>
31
#include <QtCore/QBuffer>
32
#include <QtGui/QContextMenuEvent>
33
#include <QtGui/QMenu>
36
* @short Small helper class to load sound from network
41
SoundLoader( QWidget *parent = 0 );
43
QByteArray loadSound( const KUrl &url, bool *ok );
51
SoundLoader::SoundLoader( QWidget *parent )
56
QByteArray SoundLoader::loadSound( const KUrl &url, bool *ok )
66
if ( url.isLocalFile() ) {
67
QFile file( url.path() );
68
if ( file.open( QIODevice::ReadOnly ) ) {
69
sound = file.readAll();
73
} else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
74
QFile file( tempFile );
75
if ( file.open( QIODevice::ReadOnly ) ) {
76
sound = file.readAll();
80
KIO::NetAccess::removeTempFile( tempFile );
84
KMessageBox::sorry( mParent, i18n( "This contact's sound cannot be found." ) );
96
SoundEditWidget::SoundEditWidget( QWidget *parent )
97
: QToolButton( parent ),
102
connect( this, SIGNAL( clicked() ), SLOT( playSound() ) );
107
SoundEditWidget::~SoundEditWidget()
112
void SoundEditWidget::loadContact( const KABC::Addressee &contact )
114
const KABC::Sound sound = contact.sound();
115
if ( sound.isIntern() && !sound.data().isEmpty() ) {
117
mSound = sound.data();
123
void SoundEditWidget::storeContact( KABC::Addressee &contact ) const
125
KABC::Sound sound( contact.sound() );
126
sound.setData( mSound );
127
contact.setSound( sound );
130
void SoundEditWidget::setReadOnly( bool readOnly )
132
mReadOnly = readOnly;
135
void SoundEditWidget::updateView()
138
setIcon( KIcon( "audio-volume-medium" ) );
139
setToolTip( i18n( "Click to play pronunciation" ) );
141
setIcon( KIcon( "audio-volume-muted" ) );
142
setToolTip( i18n( "No pronunciation available" ) );
146
void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
151
menu.addAction( i18n( "Play" ), this, SLOT( playSound() ) );
154
menu.addAction( i18n( "Change..." ), this, SLOT( changeSound() ) );
157
menu.addAction( i18n( "Save..." ), this, SLOT( saveSound() ) );
160
menu.addAction( i18n( "Remove" ), this, SLOT( deleteSound() ) );
163
menu.exec( event->globalPos() );
166
void SoundEditWidget::playSound()
171
Phonon::MediaObject* player = Phonon::createPlayer( Phonon::NotificationCategory );
172
QBuffer* soundData = new QBuffer( player );
173
soundData->setData( mSound );
174
player->setCurrentSource( soundData );
175
player->setParent( this );
176
connect( player, SIGNAL( finished() ), player, SLOT( deleteLater() ) );
180
void SoundEditWidget::changeSound()
182
const KUrl url = KFileDialog::getOpenUrl( QString(), "*.wav", this );
183
if ( url.isValid() ) {
185
const QByteArray sound = soundLoader()->loadSound( url, &ok );
194
void SoundEditWidget::saveSound()
196
const QString fileName = KFileDialog::getSaveFileName( KUrl(), "*.wav", this );
197
if ( !fileName.isEmpty() ) {
198
QFile file( fileName );
199
if ( file.open( QIODevice::WriteOnly ) ) {
200
file.write( mSound );
206
void SoundEditWidget::deleteSound()
209
mSound = QByteArray();
213
SoundLoader* SoundEditWidget::soundLoader()
216
mSoundLoader = new SoundLoader;