kmail

kmlineeditspell.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kmcomposewin.cpp
00003 // Author: Markus Wuebben <markus.wuebben@kde.org>
00004 // This code is published under the GPL.
00005 
00006 #include "kmlineeditspell.h"
00007 
00008 #include "recentaddresses.h"
00009 #include "kmkernel.h"
00010 #include "globalsettings.h"
00011 
00012 #include <libkdepim/kvcarddrag.h>
00013 #include <libemailfunctions/email.h>
00014 
00015 #include <kabc/vcardconverter.h>
00016 #include <kio/netaccess.h>
00017 
00018 #include <kpopupmenu.h>
00019 #include <kurl.h>
00020 #include <kurldrag.h>
00021 #include <kmessagebox.h>
00022 #include <kcompletionbox.h>
00023 #include <klocale.h>
00024 
00025 #include <qevent.h>
00026 #include <qfile.h>
00027 #include <qcstring.h>
00028 #include <qcursor.h>
00029 
00030 
00031 KMLineEdit::KMLineEdit(bool useCompletion,
00032                        QWidget *parent, const char *name)
00033     : KPIM::AddresseeLineEdit(parent,useCompletion,name)
00034 {
00035    allowSemiColonAsSeparator( GlobalSettings::allowSemicolonAsAddressSeparator() );
00036 }
00037 
00038 
00039 //-----------------------------------------------------------------------------
00040 void KMLineEdit::keyPressEvent(QKeyEvent *e)
00041 {
00042   if ((e->key() == Key_Enter || e->key() == Key_Return) &&
00043       !completionBox()->isVisible())
00044   {
00045     emit focusDown();
00046     AddresseeLineEdit::keyPressEvent(e);
00047     return;
00048   }
00049   if (e->key() == Key_Up)
00050   {
00051     emit focusUp();
00052     return;
00053   }
00054   if (e->key() == Key_Down)
00055   {
00056     emit focusDown();
00057     return;
00058   }
00059   AddresseeLineEdit::keyPressEvent(e);
00060 }
00061 
00062 
00063 void KMLineEdit::insertEmails( const QStringList & emails )
00064 {
00065   if ( emails.empty() )
00066     return;
00067 
00068   QString contents = text();
00069   if ( !contents.isEmpty() )
00070     contents += ',';
00071   // only one address, don't need kpopup to choose
00072   if ( emails.size() == 1 ) {
00073     setText( contents + emails.front() );
00074     return;
00075   }
00076   //multiple emails, let the user choose one
00077   KPopupMenu menu( this, "Addresschooser" );
00078   for ( QStringList::const_iterator it = emails.begin(), end = emails.end() ; it != end; ++it )
00079     menu.insertItem( *it );
00080   const int result = menu.exec( QCursor::pos() );
00081   if ( result < 0 )
00082     return;
00083   setText( contents + menu.text( result ) );
00084 }
00085 
00086 void KMLineEdit::dropEvent(QDropEvent *event)
00087 {
00088   QString vcards;
00089   KVCardDrag::decode( event, vcards );
00090   if ( !vcards.isEmpty() ) {
00091     KABC::VCardConverter converter;
00092     KABC::Addressee::List list = converter.parseVCards( vcards );
00093     KABC::Addressee::List::Iterator ait;
00094     for ( ait = list.begin(); ait != list.end(); ++ait ){
00095       insertEmails( (*ait).emails() );
00096     }
00097   } else {
00098     KURL::List urls;
00099     if ( KURLDrag::decode( event, urls) ) {
00100       //kdDebug(5006) << "urlList" << endl;
00101       KURL::List::Iterator it = urls.begin();
00102       KABC::VCardConverter converter;
00103       KABC::Addressee::List list;
00104       QString fileName;
00105       QString caption( i18n( "vCard Import Failed" ) );
00106       for ( it = urls.begin(); it != urls.end(); ++it ) {
00107         if ( KIO::NetAccess::download( *it, fileName, parentWidget() ) ) {
00108           QFile file( fileName );
00109           file.open( IO_ReadOnly );
00110           QByteArray rawData = file.readAll();
00111           file.close();
00112           QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
00113           list += converter.parseVCards( data );
00114           KIO::NetAccess::removeTempFile( fileName );
00115         } else {
00116           QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" );
00117           KMessageBox::error( parentWidget(), text.arg( (*it).url() ), caption );
00118         }
00119         KABC::Addressee::List::Iterator ait;
00120         for ( ait = list.begin(); ait != list.end(); ++ait )
00121           insertEmails((*ait).emails());
00122       }
00123     } else {
00124       KPIM::AddresseeLineEdit::dropEvent( event );
00125     }
00126   }
00127 }
00128 
00129 QPopupMenu *KMLineEdit::createPopupMenu()
00130 {
00131     QPopupMenu *menu = KPIM::AddresseeLineEdit::createPopupMenu();
00132     if ( !menu )
00133         return 0;
00134 
00135     menu->insertSeparator();
00136     menu->insertItem( i18n( "Edit Recent Addresses..." ),
00137                       this, SLOT( editRecentAddresses() ) );
00138 
00139     return menu;
00140 }
00141 
00142 void KMLineEdit::editRecentAddresses()
00143 {
00144   KRecentAddress::RecentAddressDialog dlg( this );
00145   dlg.setAddresses( KRecentAddress::RecentAddresses::self( KMKernel::config() )->addresses() );
00146   if ( !dlg.exec() )
00147     return;
00148   KRecentAddress::RecentAddresses::self( KMKernel::config() )->clear();
00149   const QStringList addrList = dlg.addresses();
00150   for ( QStringList::const_iterator it = addrList.begin(), end = addrList.end() ; it != end ; ++it )
00151     KRecentAddress::RecentAddresses::self( KMKernel::config() )->add( *it );
00152   loadContacts();
00153 }
00154 
00155 
00156 //-----------------------------------------------------------------------------
00157 void KMLineEdit::loadContacts()
00158 {
00159   AddresseeLineEdit::loadContacts();
00160 
00161   if ( GlobalSettings::self()->showRecentAddressesInComposer() ){
00162     if ( KMKernel::self() ) {
00163       QStringList recent =
00164         KRecentAddress::RecentAddresses::self( KMKernel::config() )->addresses();
00165       QStringList::Iterator it = recent.begin();
00166       QString name, email;
00167 
00168       KConfig config( "kpimcompletionorder" );
00169       config.setGroup( "CompletionWeights" );
00170       int weight = config.readEntry( "Recent Addresses", "10" ).toInt();
00171       int idx = addCompletionSource( i18n( "Recent Addresses" ), weight );
00172       for ( ; it != recent.end(); ++it ) {
00173         KABC::Addressee addr;
00174         KPIM::getNameAndMail(*it, name, email);
00175         name = KPIM::quoteNameIfNecessary( name );
00176         if ( ( name[0] == '"' ) && ( name[name.length() - 1] == '"' ) ) {
00177           name.remove( 0, 1 );
00178           name.truncate( name.length() - 1 );
00179         }
00180         addr.setNameFromString( name );
00181         addr.insertEmail( email, true );
00182         addContact( addr, weight, idx );
00183       }
00184     }
00185   }
00186 }
00187 
00188 
00189 KMLineEditSpell::KMLineEditSpell(bool useCompletion,
00190                        QWidget *parent, const char *name)
00191     : KMLineEdit(useCompletion,parent,name)
00192 {
00193 }
00194 
00195 
00196 void KMLineEditSpell::highLightWord( unsigned int length, unsigned int pos )
00197 {
00198     setSelection ( pos, length );
00199 }
00200 
00201 void KMLineEditSpell::spellCheckDone( const QString &s )
00202 {
00203     if( s != text() )
00204         setText( s );
00205 }
00206 
00207 void KMLineEditSpell::spellCheckerMisspelling( const QString &_text, const QStringList&, unsigned int pos)
00208 {
00209      highLightWord( _text.length(),pos );
00210 }
00211 
00212 void KMLineEditSpell::spellCheckerCorrected( const QString &old, const QString &corr, unsigned int pos)
00213 {
00214     if( old!= corr )
00215     {
00216         setSelection ( pos, old.length() );
00217         insert( corr );
00218         setSelection ( pos, corr.length() );
00219         emit subjectTextSpellChecked();
00220     }
00221 }
00222 
00223 
00224 #include "kmlineeditspell.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys