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 }
00036 
00037 
00038 //-----------------------------------------------------------------------------
00039 void KMLineEdit::keyPressEvent(QKeyEvent *e)
00040 {
00041   if ((e->key() == Key_Enter || e->key() == Key_Return) &&
00042       !completionBox()->isVisible())
00043   {
00044     emit focusDown();
00045     AddresseeLineEdit::keyPressEvent(e);
00046     return;
00047   }
00048   if (e->key() == Key_Up)
00049   {
00050     emit focusUp();
00051     return;
00052   }
00053   if (e->key() == Key_Down)
00054   {
00055     emit focusDown();
00056     return;
00057   }
00058   AddresseeLineEdit::keyPressEvent(e);
00059 }
00060 
00061 
00062 void KMLineEdit::insertEmails( const QStringList & emails )
00063 {
00064   if ( emails.empty() )
00065     return;
00066 
00067   QString contents = text();
00068   if ( !contents.isEmpty() )
00069     contents += ',';
00070   // only one address, don't need kpopup to choose
00071   if ( emails.size() == 1 ) {
00072     setText( contents + emails.front() );
00073     return;
00074   }
00075   //multiple emails, let the user choose one
00076   KPopupMenu menu( this, "Addresschooser" );
00077   for ( QStringList::const_iterator it = emails.begin(), end = emails.end() ; it != end; ++it )
00078     menu.insertItem( *it );
00079   const int result = menu.exec( QCursor::pos() );
00080   if ( result < 0 )
00081     return;
00082   setText( contents + menu.text( result ) );
00083 }
00084 
00085 void KMLineEdit::dropEvent(QDropEvent *event)
00086 {
00087   QString vcards;
00088   KVCardDrag::decode( event, vcards );
00089   if ( !vcards.isEmpty() ) {
00090     KABC::VCardConverter converter;
00091     KABC::Addressee::List list = converter.parseVCards( vcards );
00092     KABC::Addressee::List::Iterator ait;
00093     for ( ait = list.begin(); ait != list.end(); ++ait ){
00094       insertEmails( (*ait).emails() );
00095     }
00096   } else {
00097     KURL::List urls;
00098     if ( KURLDrag::decode( event, urls) ) {
00099       //kdDebug(5006) << "urlList" << endl;
00100       KURL::List::Iterator it = urls.begin();
00101       KABC::VCardConverter converter;
00102       KABC::Addressee::List list;
00103       QString fileName;
00104       QString caption( i18n( "vCard Import Failed" ) );
00105       for ( it = urls.begin(); it != urls.end(); ++it ) {
00106         if ( KIO::NetAccess::download( *it, fileName, parentWidget() ) ) {
00107           QFile file( fileName );
00108           file.open( IO_ReadOnly );
00109           QByteArray rawData = file.readAll();
00110           file.close();
00111           QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
00112           list += converter.parseVCards( data );
00113           KIO::NetAccess::removeTempFile( fileName );
00114         } else {
00115           QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" );
00116           KMessageBox::error( parentWidget(), text.arg( (*it).url() ), caption );
00117         }
00118         KABC::Addressee::List::Iterator ait;
00119         for ( ait = list.begin(); ait != list.end(); ++ait )
00120           insertEmails((*ait).emails());
00121       }
00122     } else {
00123       KPIM::AddresseeLineEdit::dropEvent( event );
00124     }
00125   }
00126 }
00127 
00128 QPopupMenu *KMLineEdit::createPopupMenu()
00129 {
00130     QPopupMenu *menu = KPIM::AddresseeLineEdit::createPopupMenu();
00131     if ( !menu )
00132         return 0;
00133 
00134     menu->insertSeparator();
00135     menu->insertItem( i18n( "Edit Recent Addresses..." ),
00136                       this, SLOT( editRecentAddresses() ) );
00137 
00138     return menu;
00139 }
00140 
00141 void KMLineEdit::editRecentAddresses()
00142 {
00143   KRecentAddress::RecentAddressDialog dlg( this );
00144   dlg.setAddresses( KRecentAddress::RecentAddresses::self( KMKernel::config() )->addresses() );
00145   if ( !dlg.exec() )
00146     return;
00147   KRecentAddress::RecentAddresses::self( KMKernel::config() )->clear();
00148   const QStringList addrList = dlg.addresses();
00149   for ( QStringList::const_iterator it = addrList.begin(), end = addrList.end() ; it != end ; ++it )
00150     KRecentAddress::RecentAddresses::self( KMKernel::config() )->add( *it );
00151   loadContacts();
00152 }
00153 
00154 
00155 //-----------------------------------------------------------------------------
00156 void KMLineEdit::loadContacts()
00157 {
00158   // was: KABC::AddressLineEdit::loadAddresses()
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       int idx = addCompletionSource( i18n( "Recent Addresses" ) );
00168       for ( ; it != recent.end(); ++it ) {
00169         KABC::Addressee addr;
00170         KPIM::getNameAndMail(*it, name, email);
00171         addr.setNameFromString( KPIM::quoteNameIfNecessary( name ));
00172         addr.insertEmail( email, true );
00173         addContact( addr, 120, idx ); // more weight than kabc entries and more than ldap results
00174       }
00175     }
00176   }
00177 }
00178 
00179 
00180 KMLineEditSpell::KMLineEditSpell(bool useCompletion,
00181                        QWidget *parent, const char *name)
00182     : KMLineEdit(useCompletion,parent,name)
00183 {
00184 }
00185 
00186 
00187 void KMLineEditSpell::highLightWord( unsigned int length, unsigned int pos )
00188 {
00189     setSelection ( pos, length );
00190 }
00191 
00192 void KMLineEditSpell::spellCheckDone( const QString &s )
00193 {
00194     if( s != text() )
00195         setText( s );
00196 }
00197 
00198 void KMLineEditSpell::spellCheckerMisspelling( const QString &_text, const QStringList&, unsigned int pos)
00199 {
00200      highLightWord( _text.length(),pos );
00201 }
00202 
00203 void KMLineEditSpell::spellCheckerCorrected( const QString &old, const QString &corr, unsigned int pos)
00204 {
00205     if( old!= corr )
00206     {
00207         setSelection ( pos, old.length() );
00208         insert( corr );
00209         setSelection ( pos, corr.length() );
00210         emit subjectTextSpellChecked();
00211     }
00212 }
00213 
00214 
00215 #include "kmlineeditspell.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys