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 <tdestandarddirs.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 ) {
98 s_completion->setIgnoreCase(
true );
101 if( m_useCompletion ) {
103 ldapTimerDeleter.
setObject( s_LDAPTimer,
new TQTimer );
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 )
114 setCompletionObject( s_completion,
false );
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& ) ));
121 connect( box, TQ_SIGNAL( userCancelled(
const TQString& )),
122 TQ_SLOT( userCancelled(
const TQString& )));
124 m_completionInitialized =
true;
133 AddressLineEdit::~AddressLineEdit()
149 KLineEdit::setFont( font );
150 if ( m_useCompletion )
151 completionBox()->setFont( font );
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 )
317 TQStringList completions = s_completion->substringCompletion( s );
318 if (completions.count() > 1) {
319 m_previousAddresses = prevAddr;
320 setCompletedItems( completions );
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 );
343 items += s_completion->substringCompletion(
'<' + 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 );
372 setCompletedItems( items, autoSuggest );
376 int index = items.first().find( s );
377 TQString newText = prevAddr + items.first().mid( index );
380 setUserSelection(
false);
381 setCompletedText(newText,
true);
390 TQString match = s_completion->makeCompletion( s );
391 if ( !match.isNull() && match != s )
393 setText( prevAddr + match );
404 TQString match = s_completion->makeCompletion( s );
405 if ( !match.isNull() && match != s )
407 TQString adds = prevAddr + match;
408 setCompletedText( adds );
420 void AddressLineEdit::slotPopupCompletion(
const TQString& completion )
422 setText( m_previousAddresses + 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 )
439 s_completion->addItem( 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,
'\"');
497 if( hasFocus() || completionBox()->hasFocus())
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 );
605 TQApplication::restoreOverrideCursor();
610 #include "addresslineedit.moc"
virtual void keyPressEvent(TQKeyEvent *)
ConstIterator begin() const
Returns an iterator pointing to the first addressee of address book.
virtual void mouseReleaseEvent(TQMouseEvent *)
void enableCompletion(bool enable)
Toggle completion.
static StdAddressBook * self()
Returns the standard addressbook object.
virtual void setFont(const TQFont &)
Reimplented for internal reasons.
const TDEShortcut & shortcut(StdAccel id)
TQStringList listNames()
Return names of all distribution lists managed by this manager.
static data, shared by ALL addressee objects
void cursorAtEnd()
Set cursor to end of line.
static TQCursor waitCursor()
static bool decode(const TQMimeSource *e, KURL::List &urls)
virtual void loadAddresses()
Always call AddressLineEdit::loadAddresses() as the first thing.
bool load()
Load distribution lists form disk.
A lineedit with LDAP and tdeabc completion.
Manager of distribution lists.
TDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
virtual void setOrder(CompOrder order)
ConstIterator end() const
Returns an iterator pointing to the last addressee of address book.
const TDEShortcut & completion()