kmail

kmlineeditspell.cpp
1 // kmcomposewin.cpp
2 // Author: Markus Wuebben <markus.wuebben@kde.org>
3 // This code is published under the GPL.
4 
5 #include "kmlineeditspell.h"
6 
7 #include "recentaddresses.h"
8 #include "kmkernel.h"
9 #include "globalsettings.h"
10 #include "stringutil.h"
11 
12 #include <libtdepim/kvcarddrag.h>
13 #include <libemailfunctions/email.h>
14 
15 #include <tdeabc/vcardconverter.h>
16 #include <tdeio/netaccess.h>
17 
18 #include <tdepopupmenu.h>
19 #include <kurl.h>
20 #include <kurldrag.h>
21 #include <tdemessagebox.h>
22 #include <tdecompletionbox.h>
23 #include <tdelocale.h>
24 
25 #include <tqevent.h>
26 #include <tqfile.h>
27 #include <tqcstring.h>
28 #include <tqcursor.h>
29 
30 
31 KMLineEdit::KMLineEdit(bool useCompletion,
32  TQWidget *parent, const char *name)
33  : KPIM::AddresseeLineEdit(parent,useCompletion,name)
34 {
35  allowSemiColonAsSeparator( GlobalSettings::allowSemicolonAsAddressSeparator() );
36 }
37 
38 
39 //-----------------------------------------------------------------------------
40 void KMLineEdit::keyPressEvent(TQKeyEvent *e)
41 {
42  if ((e->key() == Key_Enter || e->key() == Key_Return) &&
43  !completionBox()->isVisible())
44  {
45  emit focusDown();
46  AddresseeLineEdit::keyPressEvent(e);
47  return;
48  }
49  if (e->key() == Key_Up)
50  {
51  emit focusUp();
52  return;
53  }
54  if (e->key() == Key_Down)
55  {
56  emit focusDown();
57  return;
58  }
59  AddresseeLineEdit::keyPressEvent(e);
60 }
61 
62 
63 void KMLineEdit::insertEmails( const TQStringList & emails )
64 {
65  if ( emails.empty() )
66  return;
67 
68  TQString contents = text();
69  if ( !contents.isEmpty() )
70  contents += ',';
71  // only one address, don't need tdepopup to choose
72  if ( emails.size() == 1 ) {
73  setText( contents + emails.front() );
74  return;
75  }
76  //multiple emails, let the user choose one
77  TDEPopupMenu menu( this, "Addresschooser" );
78  for ( TQStringList::const_iterator it = emails.begin(), end = emails.end() ; it != end; ++it )
79  menu.insertItem( *it );
80  const int result = menu.exec( TQCursor::pos() );
81  if ( result == -1 )
82  return;
83  setText( contents + menu.text( result ) );
84 }
85 
86 void KMLineEdit::dropEvent( TQDropEvent *event )
87 {
88  KURL::List urls;
89 
90  // Case one: The user dropped a text/directory (i.e. vcard), so decode its
91  // contents
92  if ( KVCardDrag::canDecode( event ) ) {
93  TDEABC::Addressee::List list;
94  KVCardDrag::decode( event, list );
95 
96  TDEABC::Addressee::List::Iterator ait;
97  for ( ait = list.begin(); ait != list.end(); ++ait ){
98  insertEmails( (*ait).emails() );
99  }
100  }
101 
102  // Case two: The user dropped a list or Urls.
103  // Iterate over that list. For mailto: Urls, just add the addressee to the list,
104  // and for other Urls, download the Url and assume it points to a vCard
105  else if ( KURLDrag::decode( event, urls ) ) {
106  KURL::List::Iterator it = urls.begin();
107  TDEABC::Addressee::List list;
108  for ( it = urls.begin(); it != urls.end(); ++it ) {
109 
110  // First, let's deal with mailto Urls. The path() part contains the
111  // email-address.
112  if ( (*it).protocol() == "mailto" ) {
113  TDEABC::Addressee addressee;
114  addressee.insertEmail( KMail::StringUtil::decodeMailtoUrl( (*it).path() ), true /* preferred */ );
115  list += addressee;
116  }
117  // Otherwise, download the vCard to which the Url points
118  else {
119  TDEABC::VCardConverter converter;
120  TQString fileName;
121  if ( TDEIO::NetAccess::download( (*it), fileName, parentWidget() ) ) {
122  TQFile file( fileName );
123  file.open( IO_ReadOnly );
124  const TQByteArray data = file.readAll();
125  file.close();
126 #if defined(KABC_VCARD_ENCODING_FIX)
127  list += converter.parseVCardsRaw( data.data() );
128 #else
129  list += converter.parseVCards( data );
130 #endif
131  TDEIO::NetAccess::removeTempFile( fileName );
132  } else {
133  TQString caption( i18n( "vCard Import Failed" ) );
134  TQString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" ).arg( (*it).url() );
135  KMessageBox::error( parentWidget(), text, caption );
136  }
137  }
138  // Now, let the user choose which addressee to add.
139  TDEABC::Addressee::List::Iterator ait;
140  for ( ait = list.begin(); ait != list.end(); ++ait )
141  insertEmails( (*ait).emails() );
142  }
143  }
144 
145  // Case three: Let AddresseeLineEdit deal with the rest
146  else {
147  KPIM::AddresseeLineEdit::dropEvent( event );
148  }
149 }
150 
151 TQPopupMenu *KMLineEdit::createPopupMenu()
152 {
153  TQPopupMenu *menu = KPIM::AddresseeLineEdit::createPopupMenu();
154  if ( !menu )
155  return 0;
156 
157  menu->insertSeparator();
158  menu->insertItem( i18n( "Edit Recent Addresses..." ),
159  this, TQ_SLOT( editRecentAddresses() ) );
160 
161  return menu;
162 }
163 
164 void KMLineEdit::editRecentAddresses()
165 {
166  TDERecentAddress::RecentAddressDialog dlg( this );
167  dlg.setAddresses( TDERecentAddress::RecentAddresses::self( KMKernel::config() )->addresses() );
168  if ( !dlg.exec() )
169  return;
170  TDERecentAddress::RecentAddresses::self( KMKernel::config() )->clear();
171  const TQStringList addrList = dlg.addresses();
172  for ( TQStringList::const_iterator it = addrList.begin(), end = addrList.end() ; it != end ; ++it )
173  TDERecentAddress::RecentAddresses::self( KMKernel::config() )->add( *it );
174  loadContacts();
175 }
176 
177 
178 //-----------------------------------------------------------------------------
179 void KMLineEdit::loadContacts()
180 {
181  AddresseeLineEdit::loadContacts();
182 
183  if ( GlobalSettings::self()->showRecentAddressesInComposer() ){
184  if ( KMKernel::self() ) {
185  TQStringList recent =
186  TDERecentAddress::RecentAddresses::self( KMKernel::config() )->addresses();
187  TQStringList::Iterator it = recent.begin();
188  TQString name, email;
189 
190  TDEConfig config( "kpimcompletionorder" );
191  config.setGroup( "CompletionWeights" );
192  int weight = config.readEntry( "Recent Addresses", "10" ).toInt();
193  int idx = addCompletionSource( i18n( "Recent Addresses" ), weight );
194  for ( ; it != recent.end(); ++it ) {
195  TDEABC::Addressee addr;
196  KPIM::getNameAndMail(*it, name, email);
197  name = KPIM::quoteNameIfNecessary( name );
198  if ( ( name[0] == '"' ) && ( name[name.length() - 1] == '"' ) ) {
199  name.remove( 0, 1 );
200  name.truncate( name.length() - 1 );
201  }
202  addr.setNameFromString( name );
203  addr.insertEmail( email, true );
204  addContact( addr, weight, idx );
205  }
206  }
207  }
208 }
209 
210 
211 KMLineEditSpell::KMLineEditSpell(bool useCompletion,
212  TQWidget *parent, const char *name)
213  : KMLineEdit(useCompletion,parent,name)
214 {
215 }
216 
217 
218 void KMLineEditSpell::highLightWord( unsigned int length, unsigned int pos )
219 {
220  setSelection ( pos, length );
221 }
222 
223 void KMLineEditSpell::spellCheckDone( const TQString &s )
224 {
225  if( s != text() )
226  setText( s );
227 }
228 
229 void KMLineEditSpell::spellCheckerMisspelling( const TQString &_text, const TQStringList&, unsigned int pos)
230 {
231  highLightWord( _text.length(),pos );
232 }
233 
234 void KMLineEditSpell::spellCheckerCorrected( const TQString &old, const TQString &corr, unsigned int pos)
235 {
236  if( old!= corr )
237  {
238  setSelection ( pos, old.length() );
239  insert( corr );
240  setSelection ( pos, corr.length() );
241  emit subjectTextSpellChecked();
242  }
243 }
244 
245 
246 #include "kmlineeditspell.moc"
static KMKernel * self()
normal control stuff
Definition: kmkernel.h:259
TQString decodeMailtoUrl(const TQString &url)
Decodes a mailto URL.
Definition: stringutil.cpp:38