• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

soundeditwidget.cpp
00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "soundeditwidget.h"
00023 
00024 #include <kabc/addressee.h>
00025 #include <kfiledialog.h>
00026 #include <kicon.h>
00027 #include <kio/netaccess.h>
00028 #include <klocale.h>
00029 #include <kmessagebox.h>
00030 
00031 #ifndef Q_OS_WINCE
00032 #include <phonon/mediaobject.h>
00033 #endif
00034 
00035 #include <QtCore/QBuffer>
00036 #include <QtGui/QContextMenuEvent>
00037 #include <QtGui/QMenu>
00038 
00042 class SoundLoader
00043 {
00044   public:
00045     SoundLoader( QWidget *parent = 0 );
00046 
00047     QByteArray loadSound( const KUrl &url, bool *ok );
00048 
00049   private:
00050     QByteArray mSound;
00051     QWidget *mParent;
00052 };
00053 
00054 
00055 SoundLoader::SoundLoader( QWidget *parent )
00056   : mParent( parent )
00057 {
00058 }
00059 
00060 QByteArray SoundLoader::loadSound( const KUrl &url, bool *ok )
00061 {
00062   QByteArray sound;
00063   QString tempFile;
00064 
00065   if ( url.isEmpty() )
00066     return sound;
00067 
00068   (*ok) = false;
00069 
00070   if ( url.isLocalFile() ) {
00071     QFile file( url.toLocalFile() );
00072     if ( file.open( QIODevice::ReadOnly ) ) {
00073       sound = file.readAll();
00074       file.close();
00075       (*ok) = true;
00076     }
00077   } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
00078     QFile file( tempFile );
00079     if ( file.open( QIODevice::ReadOnly ) ) {
00080       sound = file.readAll();
00081       file.close();
00082       (*ok) = true;
00083     }
00084     KIO::NetAccess::removeTempFile( tempFile );
00085   }
00086 
00087   if ( !(*ok) ) {
00088     KMessageBox::sorry( mParent, i18n( "This contact's sound cannot be found." ) );
00089     return sound;
00090   }
00091 
00092   (*ok) = true;
00093 
00094   return sound;
00095 }
00096 
00097 
00098 
00099 
00100 SoundEditWidget::SoundEditWidget( QWidget *parent )
00101   : QToolButton( parent ),
00102     mHasSound( false ),
00103     mReadOnly( false ),
00104     mSoundLoader( 0 )
00105 {
00106   connect( this, SIGNAL(clicked()), SLOT(playSound()) );
00107 
00108   updateView();
00109 }
00110 
00111 SoundEditWidget::~SoundEditWidget()
00112 {
00113   delete mSoundLoader;
00114 }
00115 
00116 void SoundEditWidget::loadContact( const KABC::Addressee &contact )
00117 {
00118   const KABC::Sound sound = contact.sound();
00119   if ( sound.isIntern() && !sound.data().isEmpty() ) {
00120     mHasSound = true;
00121     mSound = sound.data();
00122   }
00123 
00124   updateView();
00125 }
00126 
00127 void SoundEditWidget::storeContact( KABC::Addressee &contact ) const
00128 {
00129   KABC::Sound sound( contact.sound() );
00130   sound.setData( mSound );
00131   contact.setSound( sound );
00132 }
00133 
00134 void SoundEditWidget::setReadOnly( bool readOnly )
00135 {
00136   mReadOnly = readOnly;
00137 }
00138 
00139 void SoundEditWidget::updateView()
00140 {
00141   if ( mHasSound ) {
00142     setIcon( KIcon( QLatin1String( "audio-volume-medium" ) ) );
00143     setToolTip( i18n( "Click to play pronunciation" ) );
00144   } else {
00145     setIcon( KIcon( QLatin1String( "audio-volume-muted" ) ) );
00146     setToolTip( i18n( "No pronunciation available" ) );
00147   }
00148 }
00149 
00150 void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
00151 {
00152   QMenu menu;
00153 
00154   if ( mHasSound )
00155     menu.addAction( i18n( "Play" ), this, SLOT(playSound()) );
00156 
00157   if ( !mReadOnly )
00158     menu.addAction( i18n( "Change..." ), this, SLOT(changeSound()) );
00159 
00160   if ( mHasSound ) {
00161     menu.addAction( i18n( "Save..." ), this, SLOT(saveSound()) );
00162 
00163     if ( !mReadOnly )
00164       menu.addAction( i18n( "Remove" ), this, SLOT(deleteSound()) );
00165   }
00166 
00167   menu.exec( event->globalPos() );
00168 }
00169 
00170 void SoundEditWidget::playSound()
00171 {
00172   if ( !mHasSound )
00173     return;
00174 
00175 #ifndef Q_OS_WINCE
00176   Phonon::MediaObject* player = Phonon::createPlayer( Phonon::NotificationCategory );
00177   QBuffer* soundData = new QBuffer( player );
00178   soundData->setData( mSound );
00179   player->setCurrentSource( soundData );
00180   player->setParent( this );
00181   connect( player, SIGNAL(finished()), player, SLOT(deleteLater()) );
00182   player->play();
00183 #endif
00184 }
00185 
00186 void SoundEditWidget::changeSound()
00187 {
00188   const KUrl url = KFileDialog::getOpenUrl( QString(), QLatin1String( "*.wav" ), this );
00189   if ( url.isValid() ) {
00190     bool ok = false;
00191     const QByteArray sound = soundLoader()->loadSound( url, &ok );
00192     if ( ok ) {
00193       mSound = sound;
00194       mHasSound = true;
00195       updateView();
00196     }
00197   }
00198 }
00199 
00200 void SoundEditWidget::saveSound()
00201 {
00202   const QString fileName = KFileDialog::getSaveFileName( KUrl(), QLatin1String( "*.wav" ), this );
00203   if ( !fileName.isEmpty() ) {
00204     QFile file( fileName );
00205     if ( file.open( QIODevice::WriteOnly ) ) {
00206       file.write( mSound );
00207       file.close();
00208     }
00209   }
00210 }
00211 
00212 void SoundEditWidget::deleteSound()
00213 {
00214   mHasSound = false;
00215   mSound = QByteArray();
00216   updateView();
00217 }
00218 
00219 SoundLoader* SoundEditWidget::soundLoader()
00220 {
00221   if ( !mSoundLoader )
00222     mSoundLoader = new SoundLoader;
00223 
00224   return mSoundLoader;
00225 }
00226 
00227 #include "soundeditwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:53:00 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal