kmail

kmfawidgets.cpp

00001 // kmfawidgets.h - KMFilterAction parameter widgets
00002 // Copyright: (c) 2001 Marc Mutz <mutz@kde.org>
00003 // License: GNU Genaral Public License
00004 
00005 #ifdef HAVE_CONFIG_H
00006 #include <config.h>
00007 #endif
00008 
00009 #include "kmfawidgets.h"
00010 
00011 #include <kabc/addresseedialog.h> // for the button in KMFilterActionWithAddress
00012 #include <kiconloader.h>
00013 #include <klocale.h>
00014 #include <kaudioplayer.h>
00015 #include <kurlrequester.h>
00016 #include <kfiledialog.h>
00017 #include <kstandarddirs.h>
00018 
00019 #include <qlayout.h>
00020 #include <qtooltip.h>
00021 
00022 //=============================================================================
00023 //
00024 // class KMFilterActionWithAddressWidget
00025 //
00026 //=============================================================================
00027 
00028 KMFilterActionWithAddressWidget::KMFilterActionWithAddressWidget( QWidget* parent, const char* name )
00029   : QWidget( parent, name )
00030 {
00031   QHBoxLayout *hbl = new QHBoxLayout(this);
00032   hbl->setSpacing(4);
00033   mLineEdit = new KLineEdit(this);
00034   mLineEdit->setName( "addressEdit" );
00035   hbl->addWidget( mLineEdit, 1 /*stretch*/ );
00036   mBtn = new QPushButton( QString::null ,this );
00037   mBtn->setPixmap( BarIcon( "contents", KIcon::SizeSmall ) );
00038   mBtn->setFixedHeight( mLineEdit->sizeHint().height() );
00039   QToolTip::add( mBtn, i18n( "Open Address Book" ) );
00040   hbl->addWidget( mBtn );
00041 
00042   connect( mBtn, SIGNAL(clicked()),
00043            this, SLOT(slotAddrBook()) );
00044   connect( mLineEdit, SIGNAL( textChanged(const QString&) ),
00045            this, SIGNAL( textChanged(const QString&) ) );
00046 }
00047 
00048 void KMFilterActionWithAddressWidget::slotAddrBook()
00049 {
00050   KABC::Addressee::List lst = KABC::AddresseeDialog::getAddressees( this );
00051 
00052   if ( lst.empty() )
00053     return;
00054 
00055   QStringList addrList;
00056 
00057   for( KABC::Addressee::List::const_iterator it = lst.begin(); it != lst.end(); ++it )
00058     addrList << (*it).fullEmail();
00059 
00060   QString txt = mLineEdit->text().stripWhiteSpace();
00061 
00062   if ( !txt.isEmpty() ) {
00063     if ( !txt.endsWith( "," ) )
00064       txt += ", ";
00065     else
00066       txt += ' ';
00067   }
00068 
00069   mLineEdit->setText( txt + addrList.join(",") );
00070 }
00071 
00072 KMSoundTestWidget::KMSoundTestWidget(QWidget *parent, const char *name)
00073     : QWidget( parent, name)
00074 {
00075     QHBoxLayout *lay1 = new QHBoxLayout( this );
00076     m_playButton = new QPushButton( this, "m_playButton" );
00077     m_playButton->setPixmap( SmallIcon( "1rightarrow" ) );
00078     connect( m_playButton, SIGNAL( clicked() ), SLOT( playSound() ));
00079     lay1->addWidget( m_playButton );
00080 
00081     m_urlRequester = new KURLRequester( this );
00082     lay1->addWidget( m_urlRequester );
00083     connect( m_urlRequester, SIGNAL( openFileDialog( KURLRequester * )),
00084              SLOT( openSoundDialog( KURLRequester * )));
00085     connect( m_urlRequester->lineEdit(), SIGNAL( textChanged ( const QString & )), SLOT( slotUrlChanged(const QString & )));
00086     slotUrlChanged(m_urlRequester->lineEdit()->text() );
00087 }
00088 
00089 KMSoundTestWidget::~KMSoundTestWidget()
00090 {
00091 }
00092 
00093 void KMSoundTestWidget::slotUrlChanged(const QString &_text )
00094 {
00095     m_playButton->setEnabled( !_text.isEmpty());
00096 }
00097 
00098 void KMSoundTestWidget::openSoundDialog( KURLRequester * )
00099 {
00100     static bool init = true;
00101     if ( !init )
00102         return;
00103 
00104     init = false;
00105 
00106     KFileDialog *fileDialog = m_urlRequester->fileDialog();
00107     fileDialog->setCaption( i18n("Select Sound File") );
00108     QStringList filters;
00109     filters << "audio/x-wav" << "audio/x-mp3" << "application/x-ogg"
00110             << "audio/x-adpcm";
00111     fileDialog->setMimeFilter( filters );
00112 
00113    QStringList soundDirs = KGlobal::dirs()->resourceDirs( "sound" );
00114 
00115     if ( !soundDirs.isEmpty() ) {
00116         KURL soundURL;
00117         QDir dir;
00118         dir.setFilter( QDir::Files | QDir::Readable );
00119         QStringList::ConstIterator it = soundDirs.begin();
00120         while ( it != soundDirs.end() ) {
00121             dir = *it;
00122             if ( dir.isReadable() && dir.count() > 2 ) {
00123                 soundURL.setPath( *it );
00124                 fileDialog->setURL( soundURL );
00125                 break;
00126             }
00127             ++it;
00128         }
00129     }
00130 
00131 }
00132 
00133 void KMSoundTestWidget::playSound()
00134 {
00135     QString parameter= m_urlRequester->lineEdit()->text();
00136     if ( parameter.isEmpty() )
00137         return ;
00138     QString play = parameter;
00139     QString file = QString::fromLatin1("file:");
00140     if (parameter.startsWith(file))
00141         play = parameter.mid(file.length());
00142     KAudioPlayer::play(QFile::encodeName(play));
00143 }
00144 
00145 
00146 QString KMSoundTestWidget::url() const
00147 {
00148     return m_urlRequester->lineEdit()->text();
00149 }
00150 
00151 void KMSoundTestWidget::setUrl(const QString & url)
00152 {
00153     m_urlRequester->lineEdit()->setText(url);
00154 }
00155 
00156 void KMSoundTestWidget::clear()
00157 {
00158     m_urlRequester->lineEdit()->clear();
00159 }
00160 
00161 //--------------------------------------------
00162 #include "kmfawidgets.moc"