]> git.sur5r.net Git - openldap/blob - contrib/gtk-tool/Gtk_LdapServer.cc
* configure.in: add tcl8.2 to list of tcl libs to search for, also added new MOD_TCL_LIB
[openldap] / contrib / gtk-tool / Gtk_LdapServer.cc
1 #include "Gtk_LdapServer.h"
2 #include <gtk--/base.h>
3
4 Gtk_LdapServer::Gtk_LdapServer() : Gtk_TreeItem() {
5         this->hostname = NULL;
6         this->par = NULL;
7         this->base_dn = NULL;
8         this->port = 0;
9 }
10
11 Gtk_LdapServer::Gtk_LdapServer(My_Window *w, char *h, int p) : Gtk_TreeItem() {
12         char *s, *s2;
13         this->par = w;
14         this->hostname = h;
15         this->port = p;
16         this->notebook = NULL;
17         debug("%s %i\n", this->hostname, this->port);
18         this->setType(1);
19         this->getConfig();
20 }
21
22 Gtk_LdapServer::Gtk_LdapServer(GtkTreeItem *t) : Gtk_TreeItem(t) {
23 }
24
25 Gtk_LdapServer::~Gtk_LdapServer() {
26         debug("Bye\n");
27         delete this;
28 }
29
30 void Gtk_LdapServer::setType(int t) {
31         debug("Gtk_LdapServer::setType(%i)\n", t);
32         Gtk_Pixmap *xpm_icon;
33         Gtk_Label *label;
34         char *c = NULL;
35         if (this->get_child() != NULL) {
36                 xpm_label = new Gtk_HBox(GTK_HBOX(this->get_child()->gtkobj()));
37                 xpm_label->remove_c(xpm_label->children()->nth_data(0));
38                 xpm_label->remove_c(xpm_label->children()->nth_data(0));
39         }
40         else xpm_label = new Gtk_HBox();
41         debug(this->hostname);
42         if (strcasecmp(this->hostname,"localhost") == 0)
43                 xpm_icon=new Gtk_Pixmap(*xpm_label, local_server);
44         else xpm_icon=new Gtk_Pixmap(*xpm_label, remote_server);
45 //      sprintf(c, "%s:%i", this->hostname, this->port);
46 //      printf("%s\n", c);
47         label = new Gtk_Label(this->hostname);
48         xpm_label->pack_start(*xpm_icon, false, false, 1);
49         xpm_label->pack_start(*label, false, false, 1);
50         if (this->get_child() == NULL) this->add(xpm_label);
51         label->show();
52         xpm_label->show();
53         xpm_icon->show();
54 }
55
56 int Gtk_LdapServer::showDetails() {
57         debug("Gtk_LdapServer::showDetails()\n");
58         if (this->notebook == NULL) this->getOptions();
59         if (this->notebook != NULL) {
60                 debug("Have a notebook here");
61                 if (par->viewport2->get_child() != NULL) {
62                         debug(" and viewport has children");
63                         par->viewport2->remove(par->viewport2->get_child());
64                         debug(" which have been removed");
65                 }
66                 else debug(" and viewport without children");
67                 par->viewport2->add(this->notebook);
68                 this->notebook->show();
69                 par->viewport2->show();
70                 return 0;
71         }
72         debug("done\n");
73         return 0;
74 }
75
76 int Gtk_LdapServer::getConfig() {
77         debug("Gtk_LdapServer::getConfig()\n");
78         int error, entriesCount;
79         LDAPMessage *entry, *result_identifier;
80         BerElement *ber;
81         char *attribute, **t;
82
83         if ((this->ld = ldap_open(this->hostname, this->port)) == NULL) {
84                 perror("connection");
85         }
86
87         error = ldap_search_s(this->ld, "cn=config", LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, &result_identifier);    
88         entriesCount = ldap_count_entries(this->ld, result_identifier);
89         if (entriesCount == 0) {
90                 return 0;
91         }
92
93         debug("%i entry\n", entriesCount);
94         for (entry = ldap_first_entry(this->ld, result_identifier); entry != NULL; entry = ldap_next_entry(this->ld, result_identifier)) {
95                 for (attribute = ldap_first_attribute(this->ld, entry, &ber); attribute != NULL; attribute = ldap_next_attribute(this->ld, entry, ber)) {
96                         debug("Attrib: %s\n", attribute);
97                         if (strcasecmp(attribute, "database") == 0) {
98                                 debug("have database here\n");
99                                 this->databases = new G_List<char>;
100                                 t = ldap_get_values(this->ld, entry, attribute);
101                                 for (int i=0; i<ldap_count_values(t); i++) {
102                                         this->databases->append(strdup(t[i]));
103                                 }
104                                 ldap_value_free(t);
105                                 debug("databases loaded\n");
106                                 for (int i=0; i<this->databases->length(); i++) {
107                                         debug("database(%i) %s\n", i, this->databases->nth_data(i));
108                                 }       
109                         }
110                 }
111                 debug("entry done\n");
112         }
113 //      debug("got %i entries\n", entriesCount);
114         return entriesCount;
115 }
116
117 #ifndef LDAP_GET_OPT /* a temporary fix for usability with (old) U-MICH api */
118 char* Gtk_LdapServer::getOptDescription(int option) {
119         debug("Gtk_LdapServer::getOptDescription(%i) ", option);
120         char *c;
121         switch (option) {
122                 case LDAP_OPT_API_INFO: c = "API info"; break;
123                 case LDAP_OPT_CLIENT_CONTROLS: c = "Client controls"; break;
124                 case LDAP_OPT_DEREF: c = "Dereference"; break;
125                 case LDAP_OPT_DESC: c = "Description"; break;
126                 case LDAP_OPT_DNS: c = "DNS Lookup"; break;
127                 case LDAP_OPT_ERROR_NUMBER: c = "Error number"; break;
128                 case LDAP_OPT_ERROR_STRING: c = "Error string"; break;
129                 case LDAP_OPT_SIZELIMIT: c = "Size limit"; break;
130                 case LDAP_OPT_TIMELIMIT: c = "Time limit"; break;
131                 case LDAP_OPT_REFERRALS: c = "Referrals"; break;
132                 case LDAP_OPT_RESTART: c = "Started"; break;
133                 case LDAP_OPT_PROTOCOL_VERSION: c = "Protocol version"; break;
134                 case LDAP_OPT_HOST_NAME: c = "Host name"; break;
135                 case LDAP_OPT_SERVER_CONTROLS: c = "Server controls"; break;
136                 default: c = "No description"; break;
137         }
138         debug("%s\n", c);
139         return c;
140 }
141
142 int Gtk_LdapServer::getOptType(int option) {
143         debug("Gtk_LdapServer::getOptType(%i) ", option);
144         /* types:
145          * 0 = int, 1 = string, 2 = boolean,
146          * 3 = range, 4 = LDAPAPIInfo, 5 = unknown
147          */
148         int type;
149         switch(option) {
150                 /* ints */
151                 case LDAP_OPT_DEREF:
152                 case LDAP_OPT_DESC:
153                 case LDAP_OPT_ERROR_NUMBER:
154                 case LDAP_OPT_PROTOCOL_VERSION: type = 0; break;
155                 /* strings */
156                 case LDAP_OPT_ERROR_STRING:
157                 case LDAP_OPT_HOST_NAME: type = 1; break;
158                 /* bools */
159                 case LDAP_OPT_REFERRALS:
160                 case LDAP_OPT_DNS:
161                 case LDAP_OPT_RESTART: type = 2; break;
162                 /* range */
163                 case LDAP_OPT_SIZELIMIT:        
164                 case LDAP_OPT_TIMELIMIT: type = 3; break;
165                 /* api */
166                 case LDAP_OPT_API_INFO: type = 4; break;
167                 /* unknowns */
168                 case LDAP_OPT_SERVER_CONTROLS:
169                 case LDAP_OPT_CLIENT_CONTROLS:
170                 default: type = 5; break;
171         }
172         debug("%i\n", type);
173         return type;
174 }
175 #endif /* LDAP_GET_OPT */
176
177 int Gtk_LdapServer::getOptions() {
178         debug("Gtk_LdapServer::getOptions()\n");
179         if (this->notebook != NULL) return 0;
180 #ifdef LDAP_GET_OPT /* a temporary fix for usability with (old) U-MICH api */
181         Gtk_Label *label;
182         label = new Gtk_Label("This tool has been compiled with (old) U-MICH API (no LDAP_GET_OPT)\nCompile with the latest -devel (from OpenLDAP cvs tree)\nto get some nice options here");
183         this->notebook = new Gtk_Frame("LDAP Options");
184         this->notebook->add(*label);
185         label->show();
186         this->notebook->show();
187         return 0;
188 #else
189         LDAPAPIInfo api;
190         Gtk_HBox *hbox, *mini_hbox;
191         Gtk_VBox *vbox, *mini_vbox;
192         Gtk_Table *table;
193         Gtk_Label *label;       
194         Gtk_RadioButton *radio1, *radio2;
195         Gtk_HScale *scale;
196         Gtk_Adjustment *adjustment;
197         char *description = NULL, *s_value = NULL;
198         int i_value;
199         string label_string;
200
201         int things[10] = {
202                 LDAP_OPT_API_INFO,
203                 LDAP_OPT_CLIENT_CONTROLS,
204         //      LDAP_OPT_DESC,
205         //      LDAP_OPT_DEREF,
206                 LDAP_OPT_DNS,
207         //      LDAP_OPT_ERROR_NUMBER,
208         //      LDAP_OPT_ERROR_STRING,
209                 LDAP_OPT_HOST_NAME,
210                 LDAP_OPT_PROTOCOL_VERSION,
211                 LDAP_OPT_REFERRALS,
212                 LDAP_OPT_RESTART,
213                 LDAP_OPT_SERVER_CONTROLS,
214                 LDAP_OPT_SIZELIMIT,
215                 LDAP_OPT_TIMELIMIT
216         };
217
218 /*      if (GTK_TREE_ITEM(this->gtkobj())->subtree == NULL) {
219                 this->getSubtree();
220         } */
221
222 //      debug("getting ldap options");
223 //      vbox = new Gtk_VBox();
224         table = new Gtk_Table(10, 1, TRUE);
225
226         for (int i=0; i<10; i++) {
227         //      debug("%i\n", i);
228                 hbox = new Gtk_HBox(TRUE, 2);
229                 hbox->set_border_width(2);
230                 description = this->getOptDescription(things[i]);
231                 label = new Gtk_Label(description);
232                 label->set_justify(GTK_JUSTIFY_LEFT);
233                 label->set_alignment(0, 0);
234                 hbox->pack_start(*label);
235                 label->show();
236                 switch (this->getOptType(things[i])) {
237                         case 0:
238                                 ldap_get_option(this->ld, things[i], &i_value);
239                                 debug("%s value %d\n", description, i_value);
240                                 sprintf(s_value, "%d", i_value);
241                                 label = new Gtk_Label(s_value);
242                                 label->set_justify(GTK_JUSTIFY_LEFT);
243                                 label->set_alignment(0, 0);
244                                 hbox->pack_end(*label);
245                                 label->show();
246                                 break;
247                         case 1:
248                                 ldap_get_option(this->ld, things[i], &s_value);
249                                 label = new Gtk_Label(s_value);
250                                 label->set_justify(GTK_JUSTIFY_LEFT);
251                                 label->set_alignment(0, 0);
252                                 hbox->pack_end(*label);
253                                 label->show();
254                                 break;
255                         case 2:
256                                 ldap_get_option(this->ld, things[i], &i_value);
257                                 radio1 = new Gtk_RadioButton(static_cast<GSList*>(0), "Enabled");
258                                 radio2 = new Gtk_RadioButton(*radio1, "Disabled");
259                                 if (i_value == 1) radio1->set_active(true);
260                                 else radio2->set_active(true);
261                                 mini_hbox = new Gtk_HBox(FALSE, 2);
262                                 mini_hbox->set_border_width(2);
263                                 mini_hbox->pack_start(*radio1);
264                                 radio1->show();
265                                 mini_hbox->pack_end(*radio2);
266                                 radio2->show();
267                                 hbox->pack_end(*mini_hbox);
268                                 mini_hbox->show();
269                                 break;
270                         case 3:
271                                 ldap_get_option(this->ld, things[i], &i_value);
272                                 adjustment = new Gtk_Adjustment(i_value, 1.0, 20.0, 1.0, 1.0, 0.0);
273                                 scale = new Gtk_HScale(*adjustment);
274                                 scale->set_update_policy(GTK_UPDATE_CONTINUOUS);
275                                 scale->set_value_pos(GTK_POS_TOP);
276                                 scale->set_digits(0);
277                                 scale->set_draw_value(true);
278                                 hbox->pack_end(*scale);
279                                 scale->show();
280                                 break;
281                         case 4:
282 #ifdef LDAP_API_INFO_VERSION
283         api.ldapai_info_version = LDAP_API_INFO_VERSION;
284 #else
285         api.ldapai_info_version = 1;
286 #endif
287                                 if (ldap_get_option(this->ld, things[i], &api) != LDAP_SUCCESS) {
288                                         perror(this->getOptDescription(things[i]));
289                                         break;
290                                 }
291                                 s_value = api.ldapai_vendor_name;
292                                 label = new Gtk_Label(s_value);
293                                 label->set_justify(GTK_JUSTIFY_LEFT);
294                                 label->set_alignment(0, 0);
295                                 hbox->pack_end(*label);
296                                 label->show();
297                                 break;
298                         default:
299                                 label = new Gtk_Label("Not implemented (yet)");
300                                 label->set_justify(GTK_JUSTIFY_LEFT);
301                                 label->set_alignment(0, 0);
302                                 hbox->pack_end(*label);
303                                 label->show();
304                                 break;
305                 }
306         //      hbox->pack_end(*label);
307         //      label->show();
308                 table->attach_defaults(*hbox, 0, 1, i, i+1);
309                 hbox->show();
310         }
311         table->set_border_width(2);
312         this->notebook = new Gtk_Frame("LDAP Options");
313         this->notebook->add(*table);
314         table->show();
315         return 0;
316 #endif /* LDAP_GET_OPT */
317 }
318
319 Gtk_Tree* Gtk_LdapServer::getSubtree() {
320         debug("Gtk_LdapServer::getSubtree()\n");
321         Gtk_LdapTree *tree, *subtree;
322         Gtk_LdapTreeItem *treeitem;
323         int entries;
324
325         debug("this->hostname=%s\n", this->hostname);
326         debug("this->port=%i", this->port);
327
328         char *c;
329         char *tok;
330
331         int len = this->databases->length();
332         debug("this->databases->length()=%i\n", len);
333
334         tree = new Gtk_LdapTree();
335         for (int i=0; i<len; i++) {
336                 tok = strdup(this->databases->nth_data(i));
337                 tok = strtok(tok, ":");
338         //      c = strtok(NULL, " ");
339                 c = strtok(NULL, "\0");
340                 debug("database %i %s\n", i, c);
341                 treeitem = new Gtk_LdapTreeItem(c, this->par, this->ld);
342                 subtree = treeitem->getSubtree(this->ld, 1);
343                 debug("inserting %s into %s\n", treeitem->rdn, this->hostname);
344                 tree->append(*treeitem);
345                 treeitem->set_subtree(*subtree);
346                 treeitem->show();
347         //      tree->show();
348         }
349 //      this->set_subtree(*tree);
350         debug("getSubtree() done\n");
351         return tree;
352 }
353 /*
354 void Gtk_LdapServer::show_impl() {
355         debug("%s showed\n", this->hostname);
356         BaseClassType *sig=static_cast<BaseClassType *>(get_parent_class());
357         if (!sig->show) return;
358         sig->show(gtkobj());
359 //      Gtk_c_signals_Item *sig=(Gtk_c_signals_Item *)internal_getsignalbase();
360 //      sig->show(GTK_WIDGET(gtkobj()));
361 }
362 */
363 void Gtk_LdapServer::select_impl() {
364         debug("%s selected\n", this->hostname);
365 //      Gtk_c_signals_Item *sig=(Gtk_c_signals_Item *)internal_getsignalbase();
366 //      if (!sig->select) return;
367         this->showDetails();
368 //      sig->select(GTK_ITEM(gtkobj()));
369         Gtk_TreeItem::select_impl();
370 }
371
372 void Gtk_LdapServer::collapse_impl() {
373         debug("%s collapsed\n", this->hostname);
374 //      Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
375 //      if (!sig->collapse) return;
376 //      sig->collapse(GTK_TREE_ITEM(gtkobj()));
377 //      gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
378         Gtk_TreeItem::collapse_impl();
379 }
380
381 void Gtk_LdapServer::expand_impl() {
382         debug("%s expanded\n", this->hostname);
383         Gtk_TreeItem::expand_impl();
384 //      BaseClassType *sig=static_cast<BaseClassType *>(get_parent_class());
385 //      if (!sig->expand)
386 //              { return; }
387 //      sig->expand(gtkobj());
388 //      Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
389 //      if (!sig->expand) return;
390 //      sig->expand(GTK_TREE_ITEM(gtkobj()));
391 //      Gtk_Tree *t;
392 //      t = new Gtk_Tree(GTK_TREE(GTK_TREE_ITEM(this->gtkobj())->subtree));
393 //      bool vis = t->visible();
394 //      if (vis == false) {
395 //              gtk_widget_show(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
396 //              cout << this->dn << " expanded" << endl;
397 //      }
398 //      else {
399 //              gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
400 //              cout << this->dn << " collapsed" << endl;
401 //      }
402 }