kmail

kmlineeditspell.cpp

Go to the documentation of this file.
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   // was: KABC::AddressLineEdit::loadAddresses()
00160   AddresseeLineEdit::loadContacts();
00161 
00162   if ( GlobalSettings::self()->showRecentAddressesInComposer() ){
00163     if ( KMKernel::self() ) {
00164       QStringList recent =
00165         KRecentAddress::RecentAddresses::self( KMKernel::config() )->addresses();
00166       QStringList::Iterator it = recent.begin();
00167       QString name, email;
00168       int idx = addCompletionSource( i18n( "Recent Addresses" ) );
00169       for ( ; it != recent.end(); ++it ) {
00170         KABC::Addressee addr;
00171         KPIM::getNameAndMail(*it, name, email);
00172         addr.setNameFromString( KPIM::quoteNameIfNecessary( name ));
00173         addr.insertEmail( email, true );
00174         addContact( addr, 120, idx ); // more weight than kabc entries and more than ldap results
00175       }
00176     }
00177   }
00178 }
00179 
00180 
00181 KMLineEditSpell::KMLineEditSpell(bool useCompletion,
00182                        QWidget *parent, const char *name)
00183     : KMLineEdit(useCompletion,parent,name)
00184 {
00185 }
00186 
00187 
00188 void KMLineEditSpell::highLightWord( unsigned int length, unsigned int pos )
00189 {
00190     setSelection ( pos, length );
00191 }
00192 
00193 void KMLineEditSpell::spellCheckDone( const QString &s )
00194 {
00195     if( s != text() )
00196         setText( s );
00197 }
00198 
00199 void KMLineEditSpell::spellCheckerMisspelling( const QString &_text, const QStringList&, unsigned int pos)
00200 {
00201      highLightWord( _text.length(),pos );
00202 }
00203 
00204 void KMLineEditSpell::spellCheckerCorrected( const QString &old, const QString &corr, unsigned int pos)
00205 {
00206     if( old!= corr )
00207     {
00208         setSelection ( pos, old.length() );
00209         insert( corr );
00210         setSelection ( pos, corr.length() );
00211         emit subjectTextSpellChecked();
00212     }
00213 }
00214 
00215 
00216 #include "kmlineeditspell.moc"