TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
7.6KB

  1. /*
  2. This file is part of libtdeabc.
  3. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <tqlayout.h>
  18. #include <tqpushbutton.h>
  19. #include <tqgroupbox.h>
  20. #include <tqregexp.h>
  21. #include <tdelocale.h>
  22. #include <kdebug.h>
  23. #include "stdaddressbook.h"
  24. #include "addresseedialog.h"
  25. #include "addresseedialog.moc"
  26. using namespace TDEABC;
  27. AddresseeItem::AddresseeItem( TQListView *parent, const Addressee &addressee ) :
  28. TQListViewItem( parent ),
  29. mAddressee( addressee )
  30. {
  31. setText( Name, addressee.realName() );
  32. setText( Email, addressee.preferredEmail() );
  33. }
  34. TQString AddresseeItem::key( int column, bool ) const
  35. {
  36. if (column == Email) {
  37. TQString value = text(Email);
  38. TQRegExp emailRe("<\\S*>");
  39. int match = emailRe.search(value);
  40. if (match > -1)
  41. value = value.mid(match + 1, emailRe.matchedLength() - 2);
  42. return value.lower();
  43. }
  44. return text(column).lower();
  45. }
  46. AddresseeDialog::AddresseeDialog( TQWidget *parent, bool multiple ) :
  47. KDialogBase( KDialogBase::Plain, i18n("Select Addressee"),
  48. Ok|Cancel, Ok, parent ), mMultiple( multiple )
  49. {
  50. TQWidget *topWidget = plainPage();
  51. TQBoxLayout *topLayout = new TQHBoxLayout( topWidget );
  52. TQBoxLayout *listLayout = new TQVBoxLayout;
  53. topLayout->addLayout( listLayout );
  54. mAddresseeList = new TDEListView( topWidget );
  55. mAddresseeList->addColumn( i18n("Name") );
  56. mAddresseeList->addColumn( i18n("Email") );
  57. mAddresseeList->setAllColumnsShowFocus( true );
  58. mAddresseeList->setFullWidth( true );
  59. listLayout->addWidget( mAddresseeList );
  60. connect( mAddresseeList, TQT_SIGNAL( doubleClicked( TQListViewItem * ) ),
  61. TQT_SLOT( slotOk() ) );
  62. connect( mAddresseeList, TQT_SIGNAL( selectionChanged( TQListViewItem * ) ),
  63. TQT_SLOT( updateEdit( TQListViewItem * ) ) );
  64. mAddresseeEdit = new KLineEdit( topWidget );
  65. mAddresseeEdit->setCompletionMode( TDEGlobalSettings::CompletionAuto );
  66. connect( mAddresseeEdit->completionObject(), TQT_SIGNAL( match( const TQString & ) ),
  67. TQT_SLOT( selectItem( const TQString & ) ) );
  68. mAddresseeEdit->setFocus();
  69. mAddresseeEdit->completionObject()->setIgnoreCase( true );
  70. listLayout->addWidget( mAddresseeEdit );
  71. setInitialSize( TQSize( 450, 300 ) );
  72. if ( mMultiple ) {
  73. TQBoxLayout *selectedLayout = new TQVBoxLayout;
  74. topLayout->addLayout( selectedLayout );
  75. topLayout->setSpacing( spacingHint() );
  76. TQGroupBox *selectedGroup = new TQGroupBox( 1, Qt::Horizontal, i18n("Selected"),
  77. topWidget );
  78. selectedLayout->addWidget( selectedGroup );
  79. mSelectedList = new TDEListView( selectedGroup );
  80. mSelectedList->addColumn( i18n("Name") );
  81. mSelectedList->addColumn( i18n("Email") );
  82. mSelectedList->setAllColumnsShowFocus( true );
  83. mSelectedList->setFullWidth( true );
  84. connect( mSelectedList, TQT_SIGNAL( doubleClicked( TQListViewItem * ) ),
  85. TQT_SLOT( removeSelected() ) );
  86. TQPushButton *unselectButton = new TQPushButton( i18n("Unselect"), selectedGroup );
  87. connect ( unselectButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeSelected() ) );
  88. connect( mAddresseeList, TQT_SIGNAL( clicked( TQListViewItem * ) ),
  89. TQT_SLOT( addSelected( TQListViewItem * ) ) );
  90. setInitialSize( TQSize( 650, 350 ) );
  91. }
  92. mAddressBook = StdAddressBook::self( true );
  93. connect( mAddressBook, TQT_SIGNAL( addressBookChanged( AddressBook* ) ),
  94. TQT_SLOT( addressBookChanged() ) );
  95. connect( mAddressBook, TQT_SIGNAL( loadingFinished( Resource* ) ),
  96. TQT_SLOT( addressBookChanged() ) );
  97. loadAddressBook();
  98. }
  99. AddresseeDialog::~AddresseeDialog()
  100. {
  101. }
  102. void AddresseeDialog::loadAddressBook()
  103. {
  104. mAddresseeList->clear();
  105. mItemDict.clear();
  106. mAddresseeEdit->completionObject()->clear();
  107. AddressBook::Iterator it;
  108. for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
  109. AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
  110. addCompletionItem( (*it).realName(), item );
  111. addCompletionItem( (*it).preferredEmail(), item );
  112. }
  113. }
  114. void AddresseeDialog::addCompletionItem( const TQString &str, TQListViewItem *item )
  115. {
  116. if ( str.isEmpty() ) return;
  117. mItemDict.insert( str, item );
  118. mAddresseeEdit->completionObject()->addItem( str );
  119. }
  120. void AddresseeDialog::selectItem( const TQString &str )
  121. {
  122. if ( str.isEmpty() ) return;
  123. TQListViewItem *item = mItemDict.find( str );
  124. if ( item ) {
  125. mAddresseeList->blockSignals( true );
  126. mAddresseeList->setSelected( item, true );
  127. mAddresseeList->ensureItemVisible( item );
  128. mAddresseeList->blockSignals( false );
  129. }
  130. }
  131. void AddresseeDialog::updateEdit( TQListViewItem *item )
  132. {
  133. mAddresseeEdit->setText( item->text( 0 ) );
  134. mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
  135. }
  136. void AddresseeDialog::addSelected( TQListViewItem *item )
  137. {
  138. AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
  139. if ( !addrItem ) return;
  140. Addressee a = addrItem->addressee();
  141. TQListViewItem *selectedItem = mSelectedDict.find( a.uid() );
  142. if ( !selectedItem ) {
  143. selectedItem = new AddresseeItem( mSelectedList, a );
  144. mSelectedDict.insert( a.uid(), selectedItem );
  145. }
  146. }
  147. void AddresseeDialog::removeSelected()
  148. {
  149. TQListViewItem *item = mSelectedList->selectedItem();
  150. AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
  151. if ( !addrItem ) return;
  152. mSelectedDict.remove( addrItem->addressee().uid() );
  153. delete addrItem;
  154. }
  155. Addressee AddresseeDialog::addressee()
  156. {
  157. AddresseeItem *aItem = 0;
  158. if ( mMultiple )
  159. aItem = dynamic_cast<AddresseeItem *>( mSelectedList->firstChild() );
  160. else
  161. aItem = dynamic_cast<AddresseeItem *>( mAddresseeList->selectedItem() );
  162. if (aItem) return aItem->addressee();
  163. return Addressee();
  164. }
  165. Addressee::List AddresseeDialog::addressees()
  166. {
  167. Addressee::List al;
  168. AddresseeItem *aItem = 0;
  169. if ( mMultiple ) {
  170. TQListViewItem *item = mSelectedList->firstChild();
  171. while( item ) {
  172. aItem = dynamic_cast<AddresseeItem *>( item );
  173. if ( aItem ) al.append( aItem->addressee() );
  174. item = item->nextSibling();
  175. }
  176. }
  177. else
  178. {
  179. aItem = dynamic_cast<AddresseeItem *>( mAddresseeList->selectedItem() );
  180. if (aItem) al.append( aItem->addressee() );
  181. }
  182. return al;
  183. }
  184. Addressee AddresseeDialog::getAddressee( TQWidget *parent )
  185. {
  186. AddresseeDialog *dlg = new AddresseeDialog( parent );
  187. Addressee addressee;
  188. int result = dlg->exec();
  189. if ( result == TQDialog::Accepted ) {
  190. addressee = dlg->addressee();
  191. }
  192. delete dlg;
  193. return addressee;
  194. }
  195. Addressee::List AddresseeDialog::getAddressees( TQWidget *parent )
  196. {
  197. AddresseeDialog *dlg = new AddresseeDialog( parent, true );
  198. Addressee::List addressees;
  199. int result = dlg->exec();
  200. if ( result == TQDialog::Accepted ) {
  201. addressees = dlg->addressees();
  202. }
  203. delete dlg;
  204. return addressees;
  205. }
  206. void AddresseeDialog::addressBookChanged()
  207. {
  208. loadAddressBook();
  209. }