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