26 #include "addresslineedit.h"
28 #include <tqapplication.h>
30 #include <tqptrlist.h>
33 #include <tqdragobject.h>
35 #include <tdecompletionbox.h>
36 #include <tdeconfig.h>
38 #include <kstandarddirs.h>
39 #include <kstaticdeleter.h>
40 #include <tdestdaccel.h>
43 #include <tdeabc/stdaddressbook.h>
44 #include <tdeabc/distributionlist.h>
45 #include "ldapclient.h"
59 bool AddressLineEdit::s_addressesDirty =
false;
60 TQTimer* AddressLineEdit::s_LDAPTimer = 0L;
61 LdapSearch* AddressLineEdit::s_LDAPSearch = 0L;
62 TQString* AddressLineEdit::s_LDAPText = 0L;
64 TDEConfig *AddressLineEdit::s_config = 0L;
72 AddressLineEdit::AddressLineEdit(TQWidget* parent,
77 m_useCompletion = useCompletion;
78 m_completionInitialized =
false;
88 s_addressesDirty =
true;
93 void AddressLineEdit::init()
95 if ( !s_completion ) {
96 completionDeleter.setObject( s_completion,
new TDECompletion() );
101 if( m_useCompletion ) {
103 ldapTimerDeleter.setObject( s_LDAPTimer,
new TQTimer );
104 ldapSearchDeleter.setObject( s_LDAPSearch,
new LdapSearch );
105 ldapTextDeleter.setObject( s_LDAPText,
new TQString );
107 connect( s_LDAPTimer, TQ_SIGNAL( timeout()), TQ_SLOT( slotStartLDAPLookup()));
108 connect( s_LDAPSearch, TQ_SIGNAL( searchData(
const TQStringList& )),
109 TQ_SLOT( slotLDAPSearchData(
const TQStringList& )));
112 if ( m_useCompletion && !m_completionInitialized )
115 connect(
this, TQ_SIGNAL(
completion(
const TQString&)),
116 this, TQ_SLOT(slotCompletion() ));
119 connect( box, TQ_SIGNAL( highlighted(
const TQString& )),
120 this, TQ_SLOT( slotPopupCompletion(
const TQString& ) ));
124 m_completionInitialized =
true;
133 AddressLineEdit::~AddressLineEdit()
142 configDeleter.setObject( s_config,
new TDEConfig(
"kabldaprc",
false,
false ) );
149 KLineEdit::setFont( font );
150 if ( m_useCompletion )
155 void AddressLineEdit::keyPressEvent(TQKeyEvent *e)
166 int len = text().length();
168 if (len == cursorPosition())
180 if( m_useCompletion && s_LDAPTimer != NULL )
182 if( *s_LDAPText != text())
184 *s_LDAPText = text();
185 s_LDAPLineEdit =
this;
186 s_LDAPTimer->start( 500,
true );
191 void AddressLineEdit::mouseReleaseEvent( TQMouseEvent * e )
193 if (m_useCompletion && (e->button() == TQt::MidButton))
197 m_smartPaste =
false;
203 void AddressLineEdit::insert(
const TQString &t)
207 KLineEdit::insert(t);
210 TQString newText = t.stripWhiteSpace();
211 if (newText.isEmpty())
216 newText.replace( TQRegExp(
"\r?\n"),
", " );
217 if ( newText.startsWith(
"mailto:" ) )
222 else if (newText.find(
" at ") != -1)
225 newText.replace(
" at ",
"@" );
226 newText.replace(
" dot ",
"." );
228 else if (newText.find(
"(at)") != -1)
230 newText.replace( TQRegExp(
"\\s*\\(at\\)\\s*"),
"@" );
233 TQString contents = text();
236 int pos = cursorPosition();
237 if (getSelection(&start_sel, &end_sel))
241 pos -= (end_sel - start_sel);
242 else if (pos > start_sel)
244 contents = contents.left(start_sel) + contents.right(end_sel+1);
247 int eot = contents.length();
248 while ((eot > 0) && contents[eot-1].isSpace()) eot--;
251 contents = TQString::null;
255 if (contents[eot-1] ==
',')
257 contents.truncate(eot);
262 contents = contents.left(pos)+newText+contents.mid(pos);
264 setCursorPosition(pos+newText.length());
267 void AddressLineEdit::paste()
272 m_smartPaste =
false;
278 setCursorPosition( text().length() );
284 m_useCompletion = enable;
288 void AddressLineEdit::doCompletion(
bool ctrlT)
290 if ( !m_useCompletion )
296 int n = s.findRev(
',');
302 int len = s.length();
305 while( n < len && s[n].isSpace() )
308 prevAddr = s.left(n);
309 s = s.mid(n,255).stripWhiteSpace();
312 if ( s_addressesDirty )
318 if (completions.count() > 1) {
319 m_previousAddresses = prevAddr;
322 else if (completions.count() == 1)
323 setText(prevAddr + completions.first());
340 m_previousAddresses = prevAddr;
341 TQStringList items = s_completion->
allMatches( s );
342 items += s_completion->
allMatches(
"\"" + s );
344 uint beforeDollarCompletionCount = items.count();
346 if( s.find(
' ' ) == -1 )
347 items += s_completion->
allMatches(
"$$" + s );
349 if ( !items.isEmpty() )
351 if ( items.count() > beforeDollarCompletionCount )
354 for( TQStringList::Iterator it = items.begin();
358 int pos = (*it).find(
'$', 2 );
361 (*it)=(*it).mid( pos + 1 );
365 items = removeMailDupes( items );
376 int index = items.first().find( s );
377 TQString newText = prevAddr + items.first().mid( index );
391 if ( !match.isNull() && match != s )
405 if ( !match.isNull() && match != s )
407 TQString adds = prevAddr + match;
420 void AddressLineEdit::slotPopupCompletion(
const TQString& completion )
429 s_completion->
clear();
430 s_addressesDirty =
false;
432 TQStringList adrs = addresses();
433 for( TQStringList::ConstIterator it = adrs.begin(); it != adrs.end(); ++it)
437 void AddressLineEdit::addAddress(
const TQString& adr )
440 int pos = adr.find(
'<' );
444 int pos2 = adr.find( pos,
'>' );
446 s_completion->
addItem( adr.mid( pos, pos2 - pos ));
450 void AddressLineEdit::slotStartLDAPLookup()
452 if( !s_LDAPSearch->isAvailable() || s_LDAPLineEdit !=
this )
454 startLoadingLDAPEntries();
457 void AddressLineEdit::stopLDAPLookup()
459 s_LDAPSearch->cancelSearch();
460 s_LDAPLineEdit = NULL;
463 void AddressLineEdit::startLoadingLDAPEntries()
465 TQString s( *s_LDAPText );
468 int n = s.findRev(
',');
471 prevAddr = s.left(n+1) +
' ';
472 s = s.mid(n+1,255).stripWhiteSpace();
474 if( s.length() == 0 )
478 s_LDAPSearch->startSearch( s );
481 void AddressLineEdit::slotLDAPSearchData(
const TQStringList& adrs )
483 if( s_LDAPLineEdit !=
this )
485 for( TQStringList::ConstIterator it = adrs.begin(); it != adrs.end(); ++it ) {
487 int pos =
name.find(
" <" );
488 int pos_comma =
name.find(
',' );
490 if (pos>0 && pos_comma>0 && pos_comma<pos) {
491 name.insert(pos,
'\"');
501 doCompletion(
false );
506 TQStringList AddressLineEdit::removeMailDupes(
const TQStringList& adrs )
508 TQStringList src = adrs;
511 for( TQStringList::Iterator it = src.begin(); it != src.end(); ) {
514 it = src.remove( it );
524 void AddressLineEdit::dropEvent(TQDropEvent *e)
529 TQString ct = text();
530 KURL::List::Iterator it = uriList.begin();
531 for (; it != uriList.end(); ++it)
533 if (!ct.isEmpty()) ct.append(
", ");
535 if ((*it).protocol() ==
"mailto")
536 ct.append( (*it).path() );
538 ct.append( (*it).url() );
546 TQLineEdit::dropEvent(e);
547 m_smartPaste =
false;
552 TQStringList AddressLineEdit::addresses()
558 TQRegExp needQuotes(
"[^ 0-9A-Za-z\\x0080-\\xFFFF]");
559 TQString endQuote(
"\" ");
560 TQString addr, email;
564 for( it = addressBook->
begin(); it != addressBook->
end(); ++it ) {
565 TQStringList emails = (*it).emails();
567 TQString n = (*it).prefix() + space +
568 (*it).givenName() + space +
569 (*it).additionalName() + space +
570 (*it).familyName() + space +
573 n = n.simplifyWhiteSpace();
575 TQStringList::ConstIterator mit;
577 for ( mit = emails.begin(); mit != emails.end(); ++mit ) {
579 if (!email.isEmpty()) {
580 if (n.isEmpty() || (email.find(
'<' ) != -1))
581 addr = TQString::null;
583 if (n.find(needQuotes) != -1)
584 addr =
'"' + n + endQuote;
589 if (!addr.isEmpty() && (email.find(
'<' ) == -1)
590 && (email.find(
'>' ) == -1)
591 && (email.find(
',' ) == -1))
592 addr +=
'<' + email +
'>';
595 addr = addr.stripWhiteSpace();
596 result.append( addr );
603 result += manager.listNames();
605 TQApplication::restoreOverrideCursor();
610 #include "addresslineedit.moc"
static TQCursor waitCursor()
virtual void setCompletedText(const TQString &)
virtual void setText(const TQString &)
virtual void mouseReleaseEvent(TQMouseEvent *)
TDECompletionBox * completionBox(bool create=true)
void setUserSelection(bool userSelection)
virtual void setCompletionObject(TDECompletion *, bool hsig=true)
void completion(const TQString &)
void userCancelled(const TQString &cancelText)
virtual void keyPressEvent(TQKeyEvent *)
void setCompletedItems(const TQStringList &items)
static bool decode(const TQMimeSource *e, KURL::List &urls)
ConstIterator begin() const
Returns an iterator pointing to the first addressee of address book.
ConstIterator end() const
Returns an iterator pointing to the last addressee of address book.
A lineedit with LDAP and tdeabc completion.
virtual void setFont(const TQFont &)
Reimplented for internal reasons.
void cursorAtEnd()
Set cursor to end of line.
virtual void loadAddresses()
Always call AddressLineEdit::loadAddresses() as the first thing.
void enableCompletion(bool enable)
Toggle completion.
Manager of distribution lists.
static StdAddressBook * self()
Returns the standard addressbook object.
TDEGlobalSettings::Completion completionMode() const
TQStringList substringCompletion(const TQString &string) const
virtual void setOrder(CompOrder order)
TQStringList allMatches()
virtual TQString makeCompletion(const TQString &string)
virtual void setIgnoreCase(bool ignoreCase)
void addItem(const TQString &item)
static data, shared by ALL addressee objects
TQString name(StdAccel id)
const TDEShortcut & shortcut(StdAccel id)