~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to kcontactmanager/editor/soundeditwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi, Alessandro Ghersi, Harald Sitter
  • Date: 2009-06-27 04:40:05 UTC
  • mfrom: (1.1.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090627044005-4y2vm9xz7rvmzi4p
Tags: 4:4.2.95svn20090701-0ubuntu1
[ Alessandro Ghersi ]
* New upstream release
  - Bump build-deps
* Remove akonadi-kde and libmaildir4 packages
  - remove akonadi-kde.install and libmaildir4.install
  - remove libmaildir4 from debian/rules
  - remove akonadi-kde and libmaildir4 from depends
  - remove akonadi-kde and libmaildir4 from installgen
* Update kdepim-dev.install
* Update kpilot.install
* Add akonadi-kde and libmaildir4 transitional packages

[ Harald Sitter ]
* KAddressbook replaces Kontact << 4.2.85 (LP: #378373)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of KContactManager.
3
 
 
4
 
    Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5
 
 
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.
10
 
 
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.
15
 
 
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.
19
 
*/
20
 
 
21
 
#include "soundeditwidget.h"
22
 
 
23
 
#include <kabc/addressee.h>
24
 
#include <kfiledialog.h>
25
 
#include <kicon.h>
26
 
#include <kio/netaccess.h>
27
 
#include <klocale.h>
28
 
#include <kmessagebox.h>
29
 
#include <phonon/mediaobject.h>
30
 
 
31
 
#include <QtCore/QBuffer>
32
 
#include <QtGui/QContextMenuEvent>
33
 
#include <QtGui/QMenu>
34
 
 
35
 
/**
36
 
 * @short Small helper class to load sound from network
37
 
 */
38
 
class SoundLoader
39
 
{
40
 
  public:
41
 
    SoundLoader( QWidget *parent = 0 );
42
 
 
43
 
    QByteArray loadSound( const KUrl &url, bool *ok );
44
 
 
45
 
  private:
46
 
    QByteArray mSound;
47
 
    QWidget *mParent;
48
 
};
49
 
 
50
 
 
51
 
SoundLoader::SoundLoader( QWidget *parent )
52
 
  : mParent( parent )
53
 
{
54
 
}
55
 
 
56
 
QByteArray SoundLoader::loadSound( const KUrl &url, bool *ok )
57
 
{
58
 
  QByteArray sound;
59
 
  QString tempFile;
60
 
 
61
 
  if ( url.isEmpty() )
62
 
    return sound;
63
 
 
64
 
  (*ok) = false;
65
 
 
66
 
  if ( url.isLocalFile() ) {
67
 
    QFile file( url.path() );
68
 
    if ( file.open( QIODevice::ReadOnly ) ) {
69
 
      sound = file.readAll();
70
 
      file.close();
71
 
      (*ok) = true;
72
 
    }
73
 
  } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
74
 
    QFile file( tempFile );
75
 
    if ( file.open( QIODevice::ReadOnly ) ) {
76
 
      sound = file.readAll();
77
 
      file.close();
78
 
      (*ok) = true;
79
 
    }
80
 
    KIO::NetAccess::removeTempFile( tempFile );
81
 
  }
82
 
 
83
 
  if ( !(*ok) ) {
84
 
    KMessageBox::sorry( mParent, i18n( "This contact's sound cannot be found." ) );
85
 
    return sound;
86
 
  }
87
 
 
88
 
  (*ok) = true;
89
 
 
90
 
  return sound;
91
 
}
92
 
 
93
 
 
94
 
 
95
 
 
96
 
SoundEditWidget::SoundEditWidget( QWidget *parent )
97
 
  : QToolButton( parent ),
98
 
    mHasSound( false ),
99
 
    mReadOnly( false ),
100
 
    mSoundLoader( 0 )
101
 
{
102
 
  connect( this, SIGNAL( clicked() ), SLOT( playSound() ) );
103
 
 
104
 
  updateView();
105
 
}
106
 
 
107
 
SoundEditWidget::~SoundEditWidget()
108
 
{
109
 
  delete mSoundLoader;
110
 
}
111
 
 
112
 
void SoundEditWidget::loadContact( const KABC::Addressee &contact )
113
 
{
114
 
  const KABC::Sound sound = contact.sound();
115
 
  if ( sound.isIntern() && !sound.data().isEmpty() ) {
116
 
    mHasSound = true;
117
 
    mSound = sound.data();
118
 
  }
119
 
 
120
 
  updateView();
121
 
}
122
 
 
123
 
void SoundEditWidget::storeContact( KABC::Addressee &contact ) const
124
 
{
125
 
  KABC::Sound sound( contact.sound() );
126
 
  sound.setData( mSound );
127
 
  contact.setSound( sound );
128
 
}
129
 
 
130
 
void SoundEditWidget::setReadOnly( bool readOnly )
131
 
{
132
 
  mReadOnly = readOnly;
133
 
}
134
 
 
135
 
void SoundEditWidget::updateView()
136
 
{
137
 
  if ( mHasSound ) {
138
 
    setIcon( KIcon( "audio-volume-medium" ) );
139
 
    setToolTip( i18n( "Click to play pronunciation" ) );
140
 
  } else {
141
 
    setIcon( KIcon( "audio-volume-muted" ) );
142
 
    setToolTip( i18n( "No pronunciation available" ) );
143
 
  }
144
 
}
145
 
 
146
 
void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
147
 
{
148
 
  QMenu menu;
149
 
 
150
 
  if ( mHasSound )
151
 
    menu.addAction( i18n( "Play" ), this, SLOT( playSound() ) );
152
 
 
153
 
  if ( !mReadOnly )
154
 
    menu.addAction( i18n( "Change..." ), this, SLOT( changeSound() ) );
155
 
 
156
 
  if ( mHasSound ) {
157
 
    menu.addAction( i18n( "Save..." ), this, SLOT( saveSound() ) );
158
 
 
159
 
    if ( !mReadOnly )
160
 
      menu.addAction( i18n( "Remove" ), this, SLOT( deleteSound() ) );
161
 
  }
162
 
 
163
 
  menu.exec( event->globalPos() );
164
 
}
165
 
 
166
 
void SoundEditWidget::playSound()
167
 
{
168
 
  if ( !mHasSound )
169
 
    return;
170
 
 
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() ) );
177
 
  player->play();
178
 
}
179
 
 
180
 
void SoundEditWidget::changeSound()
181
 
{
182
 
  const KUrl url = KFileDialog::getOpenUrl( QString(), "*.wav", this );
183
 
  if ( url.isValid() ) {
184
 
    bool ok = false;
185
 
    const QByteArray sound = soundLoader()->loadSound( url, &ok );
186
 
    if ( ok ) {
187
 
      mSound = sound;
188
 
      mHasSound = true;
189
 
      updateView();
190
 
    }
191
 
  }
192
 
}
193
 
 
194
 
void SoundEditWidget::saveSound()
195
 
{
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 );
201
 
      file.close();
202
 
    }
203
 
  }
204
 
}
205
 
 
206
 
void SoundEditWidget::deleteSound()
207
 
{
208
 
  mHasSound = false;
209
 
  mSound = QByteArray();
210
 
  updateView();
211
 
}
212
 
 
213
 
SoundLoader* SoundEditWidget::soundLoader()
214
 
{
215
 
  if ( !mSoundLoader )
216
 
    mSoundLoader = new SoundLoader;
217
 
 
218
 
  return mSoundLoader;
219
 
}