TDE personal information management applications
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.

ldapoptionswidget.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. This file is part of KAddressBook.
  3. Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. As a special exception, permission is given to link this program
  16. with any edition of TQt, and distribute the resulting executable,
  17. without including the source code for TQt in the source distribution.
  18. */
  19. #include <tqgroupbox.h>
  20. #include <tqheader.h>
  21. #include <tqlabel.h>
  22. #include <tqlayout.h>
  23. #include <tqpushbutton.h>
  24. #include <tqtoolbutton.h>
  25. #include <tqstring.h>
  26. #include <kapplication.h>
  27. #include <kbuttonbox.h>
  28. #include <tdeconfig.h>
  29. #include <klistview.h>
  30. #include <klocale.h>
  31. #include "addhostdialog.h"
  32. #include "ldapoptionswidget.h"
  33. #include <tqvgroupbox.h>
  34. #include <tqhbox.h>
  35. #include <tqvbox.h>
  36. #include <kiconloader.h>
  37. class LDAPItem : public TQCheckListItem
  38. {
  39. public:
  40. LDAPItem( TQListView *parent, const KPIM::LdapServer &server, bool isActive = false )
  41. : TQCheckListItem( parent, parent->lastItem(), TQString(), TQCheckListItem::CheckBox ),
  42. mIsActive( isActive )
  43. {
  44. setServer( server );
  45. }
  46. void setServer( const KPIM::LdapServer &server )
  47. {
  48. mServer = server;
  49. setText( 0, mServer.host() );
  50. }
  51. const KPIM::LdapServer &server() const { return mServer; }
  52. void setIsActive( bool isActive ) { mIsActive = isActive; }
  53. bool isActive() const { return mIsActive; }
  54. private:
  55. KPIM::LdapServer mServer;
  56. bool mIsActive;
  57. };
  58. LDAPOptionsWidget::LDAPOptionsWidget( TQWidget* parent, const char* name )
  59. : TQWidget( parent, name )
  60. {
  61. initGUI();
  62. mHostListView->setSorting( -1 );
  63. mHostListView->setAllColumnsShowFocus( true );
  64. mHostListView->setFullWidth( true );
  65. mHostListView->addColumn( TQString() );
  66. mHostListView->header()->hide();
  67. connect( mHostListView, TQT_SIGNAL( selectionChanged( TQListViewItem* ) ),
  68. TQT_SLOT( slotSelectionChanged( TQListViewItem* ) ) );
  69. connect( mHostListView, TQT_SIGNAL(doubleClicked( TQListViewItem *, const TQPoint &, int )), this, TQT_SLOT(slotEditHost()));
  70. connect( mHostListView, TQT_SIGNAL( clicked( TQListViewItem* ) ),
  71. TQT_SLOT( slotItemClicked( TQListViewItem* ) ) );
  72. connect( mUpButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotMoveUp() ) );
  73. connect( mDownButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotMoveDown() ) );
  74. }
  75. LDAPOptionsWidget::~LDAPOptionsWidget()
  76. {
  77. }
  78. void LDAPOptionsWidget::slotSelectionChanged( TQListViewItem *item )
  79. {
  80. bool state = ( item != 0 );
  81. mEditButton->setEnabled( state );
  82. mRemoveButton->setEnabled( state );
  83. mDownButton->setEnabled( item && item->itemBelow() );
  84. mUpButton->setEnabled( item && item->itemAbove() );
  85. }
  86. void LDAPOptionsWidget::slotItemClicked( TQListViewItem *item )
  87. {
  88. LDAPItem *ldapItem = dynamic_cast<LDAPItem*>( item );
  89. if ( !ldapItem )
  90. return;
  91. if ( ldapItem->isOn() != ldapItem->isActive() ) {
  92. emit changed( true );
  93. ldapItem->setIsActive( ldapItem->isOn() );
  94. }
  95. }
  96. void LDAPOptionsWidget::slotAddHost()
  97. {
  98. KPIM::LdapServer server;
  99. AddHostDialog dlg( &server, this );
  100. if ( dlg.exec() && !server.host().isEmpty() ) {
  101. new LDAPItem( mHostListView, server );
  102. emit changed( true );
  103. }
  104. }
  105. void LDAPOptionsWidget::slotEditHost()
  106. {
  107. LDAPItem *item = dynamic_cast<LDAPItem*>( mHostListView->currentItem() );
  108. if ( !item )
  109. return;
  110. KPIM::LdapServer server = item->server();
  111. AddHostDialog dlg( &server, this );
  112. dlg.setCaption( i18n( "Edit Host" ) );
  113. if ( dlg.exec() && !server.host().isEmpty() ) {
  114. item->setServer( server );
  115. emit changed( true );
  116. }
  117. }
  118. void LDAPOptionsWidget::slotRemoveHost()
  119. {
  120. TQListViewItem *item = mHostListView->currentItem();
  121. if ( !item )
  122. return;
  123. mHostListView->takeItem( item );
  124. delete item;
  125. slotSelectionChanged( mHostListView->currentItem() );
  126. emit changed( true );
  127. }
  128. static void swapItems( LDAPItem *item, LDAPItem *other )
  129. {
  130. KPIM::LdapServer server = item->server();
  131. bool isActive = item->isActive();
  132. item->setServer( other->server() );
  133. item->setIsActive( other->isActive() );
  134. item->setOn( other->isActive() );
  135. other->setServer( server );
  136. other->setIsActive( isActive );
  137. other->setOn( isActive );
  138. }
  139. void LDAPOptionsWidget::slotMoveUp()
  140. {
  141. LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
  142. if ( !item ) return;
  143. LDAPItem *above = static_cast<LDAPItem *>( item->itemAbove() );
  144. if ( !above ) return;
  145. swapItems( item, above );
  146. mHostListView->setCurrentItem( above );
  147. mHostListView->setSelected( above, true );
  148. emit changed( true );
  149. }
  150. void LDAPOptionsWidget::slotMoveDown()
  151. {
  152. LDAPItem *item = static_cast<LDAPItem *>( mHostListView->selectedItem() );
  153. if ( !item ) return;
  154. LDAPItem *below = static_cast<LDAPItem *>( item->itemBelow() );
  155. if ( !below ) return;
  156. swapItems( item, below );
  157. mHostListView->setCurrentItem( below );
  158. mHostListView->setSelected( below, true );
  159. emit changed( true );
  160. }
  161. void LDAPOptionsWidget::restoreSettings()
  162. {
  163. mHostListView->clear();
  164. TDEConfig *config = KPIM::LdapSearch::config();
  165. TDEConfigGroupSaver saver( config, "LDAP" );
  166. TQString host;
  167. uint count = config->readUnsignedNumEntry( "NumSelectedHosts");
  168. for ( uint i = 0; i < count; ++i ) {
  169. KPIM::LdapServer server;
  170. KPIM::LdapSearch::readConfig( server, config, i, true );
  171. LDAPItem *item = new LDAPItem( mHostListView, server, true );
  172. item->setOn( true );
  173. }
  174. count = config->readUnsignedNumEntry( "NumHosts" );
  175. for ( uint i = 0; i < count; ++i ) {
  176. KPIM::LdapServer server;
  177. KPIM::LdapSearch::readConfig( server, config, i, false );
  178. new LDAPItem( mHostListView, server );
  179. }
  180. emit changed( false );
  181. }
  182. void LDAPOptionsWidget::saveSettings()
  183. {
  184. TDEConfig *config = KPIM::LdapSearch::config();
  185. config->deleteGroup( "LDAP" );
  186. TDEConfigGroupSaver saver( config, "LDAP" );
  187. uint selected = 0; uint unselected = 0;
  188. TQListViewItemIterator it( mHostListView );
  189. for ( ; it.current(); ++it ) {
  190. LDAPItem *item = dynamic_cast<LDAPItem*>( it.current() );
  191. if ( !item )
  192. continue;
  193. KPIM::LdapServer server = item->server();
  194. if ( item->isOn() ) {
  195. KPIM::LdapSearch::writeConfig( server, config, selected, true );
  196. selected++;
  197. } else {
  198. KPIM::LdapSearch::writeConfig( server, config, unselected, false );
  199. unselected++;
  200. }
  201. }
  202. config->writeEntry( "NumSelectedHosts", selected );
  203. config->writeEntry( "NumHosts", unselected );
  204. config->sync();
  205. emit changed( false );
  206. }
  207. void LDAPOptionsWidget::defaults()
  208. {
  209. // add default configuration here
  210. }
  211. void LDAPOptionsWidget::initGUI()
  212. {
  213. TQVBoxLayout *layout = new TQVBoxLayout( this, 0, KDialog::spacingHint() );
  214. TQVGroupBox *groupBox = new TQVGroupBox( i18n( "LDAP Servers" ), this );
  215. groupBox->setInsideSpacing( KDialog::spacingHint() );
  216. groupBox->setInsideMargin( KDialog::marginHint() );
  217. // Contents of the TQVGroupBox: label and hbox
  218. /*TQLabel *label =*/ new TQLabel( i18n( "Check all servers that should be used:" ), groupBox );
  219. TQHBox* hBox = new TQHBox( groupBox );
  220. hBox->setSpacing( 6 );
  221. // Contents of the hbox: listview and up/down buttons on the right (vbox)
  222. mHostListView = new KListView( hBox );
  223. TQVBox* upDownBox = new TQVBox( hBox );
  224. upDownBox->setSpacing( 6 );
  225. mUpButton = new TQToolButton( upDownBox, "mUpButton" );
  226. mUpButton->setIconSet( BarIconSet( "up", KIcon::SizeSmall ) );
  227. mUpButton->setEnabled( false ); // b/c no item is selected yet
  228. mDownButton = new TQToolButton( upDownBox, "mDownButton" );
  229. mDownButton->setIconSet( BarIconSet( "down", KIcon::SizeSmall ) );
  230. mDownButton->setEnabled( false ); // b/c no item is selected yet
  231. TQWidget* spacer = new TQWidget( upDownBox );
  232. upDownBox->setStretchFactor( spacer, 100 );
  233. layout->addWidget( groupBox );
  234. KButtonBox *buttons = new KButtonBox( this );
  235. buttons->addButton( i18n( "&Add Host..." ), TQT_TQOBJECT(this), TQT_SLOT( slotAddHost() ) );
  236. mEditButton = buttons->addButton( i18n( "&Edit Host..." ), TQT_TQOBJECT(this), TQT_SLOT( slotEditHost() ) );
  237. mEditButton->setEnabled( false );
  238. mRemoveButton = buttons->addButton( i18n( "&Remove Host" ), TQT_TQOBJECT(this), TQT_SLOT( slotRemoveHost() ) );
  239. mRemoveButton->setEnabled( false );
  240. buttons->layout();
  241. layout->addWidget( buttons );
  242. resize( TQSize( 460, 300 ).expandedTo( sizeHint() ) );
  243. }
  244. #include "ldapoptionswidget.moc"