22 #include "soundeditwidget.h"
24 #include <kabc/addressee.h>
25 #include <kfiledialog.h>
27 #include <kio/netaccess.h>
28 #include <klocalizedstring.h>
29 #include <kmessagebox.h>
31 #include <phonon/mediaobject.h>
33 #include <QtCore/QBuffer>
34 #include <QContextMenuEvent>
43 SoundLoader(
QWidget *parent = 0);
45 QByteArray loadSound(
const KUrl &url,
bool *ok);
52 SoundLoader::SoundLoader(
QWidget *parent)
57 QByteArray SoundLoader::loadSound(
const KUrl &url,
bool *ok)
68 if (url.isLocalFile()) {
69 QFile file(url.toLocalFile());
70 if (file.open(QIODevice::ReadOnly)) {
71 sound = file.readAll();
75 }
else if (KIO::NetAccess::download(url, tempFile, mParent)) {
77 if (file.open(QIODevice::ReadOnly)) {
78 sound = file.readAll();
82 KIO::NetAccess::removeTempFile(tempFile);
86 KMessageBox::sorry(mParent, i18n(
"This contact's sound cannot be found."));
95 SoundEditWidget::SoundEditWidget(
QWidget *parent)
101 connect(
this, SIGNAL(clicked()), SLOT(playSound()));
106 SoundEditWidget::~SoundEditWidget()
111 void SoundEditWidget::loadContact(
const KABC::Addressee &contact)
113 const KABC::Sound sound = contact.sound();
114 if (sound.isIntern() && !sound.data().isEmpty()) {
116 mSound = sound.
data();
122 void SoundEditWidget::storeContact(KABC::Addressee &contact)
const
124 KABC::Sound sound(contact.sound());
125 sound.setData(mSound);
126 contact.setSound(sound);
129 void SoundEditWidget::setReadOnly(
bool readOnly)
131 mReadOnly = readOnly;
134 void SoundEditWidget::updateView()
138 setToolTip(i18n(
"Click to play pronunciation"));
141 setToolTip(i18n(
"No pronunciation available"));
150 menu.
addAction(i18n(
"Play"),
this, SLOT(playSound()));
154 menu.
addAction(i18n(
"Change..."),
this, SLOT(changeSound()));
158 menu.
addAction(i18n(
"Save..."),
this, SLOT(saveSound()));
161 menu.
addAction(i18n(
"Remove"),
this, SLOT(deleteSound()));
168 void SoundEditWidget::playSound()
179 connect(player, SIGNAL(finished()), player, SLOT(deleteLater()));
183 void SoundEditWidget::changeSound()
188 const QByteArray sound = soundLoader()->loadSound(url, &ok);
197 void SoundEditWidget::saveSound()
199 const QString fileName = KFileDialog::getSaveFileName(KUrl(),
QLatin1String(
"*.wav"),
this,
QString(), KFileDialog::ConfirmOverwrite);
201 QFile file(fileName);
202 if (file.open(QIODevice::WriteOnly)) {
209 void SoundEditWidget::deleteSound()
216 SoundLoader *SoundEditWidget::soundLoader()
219 mSoundLoader =
new SoundLoader;
void setData(const QByteArray &data)
MediaObject * createPlayer(Phonon::Category category, const MediaSource &source)
void setParent(QObject *parent)