]> git.sur5r.net Git - openldap/blob - contrib/gtk-tool/Gtk_LdapServer.cc
Fix dynamic linking dependencies for NT and Unix
[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                         this->databases = g_list_append(this->databases, "ldbm : cn=schema");
141                                 ldap_value_free(t);
142                                 debug("databases loaded\n");
143                                 GList *t;
144                                 for (int i=0;i<g_list_length(this->databases);i++) {
145                                         t = g_list_nth(this->databases, i);
146                                         debug("database(%i) %s\n", i, (char*) t->data);
147                                 }       
148                         }
149                 }
150                 debug("entry done\n");
151         }
152         return entriesCount;
153 }
154
155 #ifndef LDAP_GET_OPT /* a temporary fix for usability with (old) U-MICH api */
156 char* Gtk_LdapServer::getOptDescription(int option) {
157         debug("Gtk_LdapServer::getOptDescription(%i) ", option);
158         char *c;
159         switch (option) {
160                 case LDAP_OPT_API_INFO: c = "API info"; break;
161                 case LDAP_OPT_CLIENT_CONTROLS: c = "Client controls"; break;
162                 case LDAP_OPT_DEREF: c = "Dereference"; break;
163                 case LDAP_OPT_DESC: c = "Description"; break;
164                 case LDAP_OPT_DNS: c = "DNS Lookup"; break;
165                 case LDAP_OPT_ERROR_NUMBER: c = "Error number"; break;
166                 case LDAP_OPT_ERROR_STRING: c = "Error string"; break;
167                 case LDAP_OPT_SIZELIMIT: c = "Size limit"; break;
168                 case LDAP_OPT_TIMELIMIT: c = "Time limit"; break;
169                 case LDAP_OPT_REFERRALS: c = "Referrals"; break;
170                 case LDAP_OPT_RESTART: c = "Started"; break;
171                 case LDAP_OPT_PROTOCOL_VERSION: c = "Protocol version"; break;
172                 case LDAP_OPT_HOST_NAME: c = "Host name"; break;
173                 case LDAP_OPT_SERVER_CONTROLS: c = "Server controls"; break;
174                 default: c = "No description"; break;
175         }
176         debug("%s\n", c);
177         return c;
178 }
179
180 int Gtk_LdapServer::getOptType(int option) {
181         debug("Gtk_LdapServer::getOptType(%i) ", option);
182         /* types:
183          * 0 = int, 1 = string, 2 = boolean,
184          * 3 = range, 4 = LDAPAPIInfo, 5 = unknown
185          */
186         int type;
187         switch(option) {
188                 /* ints */
189                 case LDAP_OPT_DEREF:
190                 case LDAP_OPT_DESC:
191                 case LDAP_OPT_ERROR_NUMBER:
192                 case LDAP_OPT_PROTOCOL_VERSION: type = 0; break;
193                 /* strings */
194                 case LDAP_OPT_ERROR_STRING:
195                 case LDAP_OPT_HOST_NAME: type = 1; break;
196                 /* bools */
197                 case LDAP_OPT_REFERRALS:
198                 case LDAP_OPT_DNS:
199                 case LDAP_OPT_RESTART: type = 2; break;
200                 /* range */
201                 case LDAP_OPT_SIZELIMIT:        
202                 case LDAP_OPT_TIMELIMIT: type = 3; break;
203                 /* api */
204                 case LDAP_OPT_API_INFO: type = 4; break;
205                 /* unknowns */
206                 case LDAP_OPT_SERVER_CONTROLS:
207                 case LDAP_OPT_CLIENT_CONTROLS:
208                 default: type = 5; break;
209         }
210         debug("%i\n", type);
211         return type;
212 }
213 #endif /* LDAP_GET_OPT */
214
215 int Gtk_LdapServer::getOptions() {
216         debug("Gtk_LdapServer::getOptions()\n");
217         if (this->notebook != NULL) return 0;
218 #ifdef LDAP_GET_OPT /* a temporary fix for usability with (old) U-MICH api */
219         Gtk_Label *label;
220         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");
221         this->notebook = new Gtk_Frame("LDAP Options");
222         this->notebook->add(*label);
223         //label->show();
224         //this->notebook->show();
225         this->notebook->show_all();
226         return 0;
227 #else
228         LDAPAPIInfo api;
229         Gtk_HBox *hbox, *mini_hbox;
230         Gtk_VBox *vbox, *mini_vbox;
231         Gtk_Table *table;
232         Gtk_Label *label;       
233         Gtk_RadioButton *radio1, *radio2;
234         Gtk_HScale *scale;
235         Gtk_Adjustment *adjustment;
236         char *description = NULL, *s_value = NULL;
237 //      int i_value;
238         string label_string;
239
240         int things[10] = {
241                 LDAP_OPT_API_INFO,
242                 LDAP_OPT_CLIENT_CONTROLS,
243         //      LDAP_OPT_DESC,
244         //      LDAP_OPT_DEREF,
245                 LDAP_OPT_DNS,
246         //      LDAP_OPT_ERROR_NUMBER,
247         //      LDAP_OPT_ERROR_STRING,
248                 LDAP_OPT_HOST_NAME,
249                 LDAP_OPT_PROTOCOL_VERSION,
250                 LDAP_OPT_REFERRALS,
251                 LDAP_OPT_RESTART,
252                 LDAP_OPT_SERVER_CONTROLS,
253                 LDAP_OPT_SIZELIMIT,
254                 LDAP_OPT_TIMELIMIT
255         };
256
257 /*      if (GTK_TREE_ITEM(this->gtkobj())->subtree == NULL) {
258                 this->getSubtree();
259         } */
260
261 //      vbox = new Gtk_VBox();
262         table = new Gtk_Table(10, 1, TRUE);
263
264         for (int i=0; i<10; i++) {
265                 int i_value;
266         //      debug("%i\n", i);
267                 hbox = new Gtk_HBox(TRUE, 2);
268                 hbox->set_border_width(2);
269                 description = this->getOptDescription(things[i]);
270                 label = new Gtk_Label(description);
271                 label->set_justify(GTK_JUSTIFY_LEFT);
272                 label->set_alignment(0, 0);
273                 hbox->pack_start(*label);
274                 label->show();
275                 switch (this->getOptType(things[i])) {
276                         case 0:
277                                 ldap_get_option(this->ld, things[i], &i_value);
278                                 debug("%s value %d\n", description, i_value);
279                                 sprintf(s_value, "%d", i_value);
280                                 label = new Gtk_Label(s_value);
281                                 label->set_justify(GTK_JUSTIFY_LEFT);
282                                 label->set_alignment(0, 0);
283                                 hbox->pack_end(*label);
284                                 label->show();
285                                 break;
286                         case 1:
287                                 ldap_get_option(this->ld, things[i], &s_value);
288                                 label = new Gtk_Label(s_value);
289                                 label->set_justify(GTK_JUSTIFY_LEFT);
290                                 label->set_alignment(0, 0);
291                                 hbox->pack_end(*label);
292                                 label->show();
293                                 break;
294                         case 2:
295                                 ldap_get_option(this->ld, things[i], &i_value);
296                                 radio1 = new Gtk_RadioButton("Enabled");
297                                 radio2 = new Gtk_RadioButton("Disabled");
298                                 radio2->set_group(radio1->group());
299                                 if (i_value == 1) radio1->set_active(true);
300                                 else radio2->set_active(true);
301                                 mini_hbox = new Gtk_HBox(FALSE, 2);
302                                 mini_hbox->set_border_width(2);
303                                 mini_hbox->pack_start(*radio1);
304                                 //radio1->show();
305                                 mini_hbox->pack_end(*radio2);
306                                 //radio2->show();
307                                 hbox->pack_end(*mini_hbox);
308                                 //mini_hbox->show();
309                                 break;
310                         case 3:
311                                 ldap_get_option(this->ld, things[i], &i_value);
312                                 debug("i_value: %s\n", i_value);
313                                 adjustment = new Gtk_Adjustment(i_value, 0.0, 20.0, 1.0, 1.0, 0.0);
314                                 scale = new Gtk_HScale(*adjustment);
315                                 scale->set_update_policy(GTK_UPDATE_CONTINUOUS);
316                                 scale->set_value_pos(GTK_POS_TOP);
317                                 scale->set_digits(0);
318                                 scale->set_draw_value(true);
319                                 hbox->pack_end(*scale);
320                                 //scale->show();
321                                 break;
322                         case 4:
323 #ifdef LDAP_API_INFO_VERSION
324         api.ldapai_info_version = LDAP_API_INFO_VERSION;
325 #else
326         api.ldapai_info_version = 1;
327 #endif
328                                 if (ldap_get_option(this->ld, things[i], &api) != LDAP_SUCCESS) {
329                                         perror(this->getOptDescription(things[i]));
330                                         break;
331                                 }
332                                 s_value = api.ldapai_vendor_name;
333                                 label = new Gtk_Label(s_value);
334                                 label->set_justify(GTK_JUSTIFY_LEFT);
335                                 label->set_alignment(0, 0);
336                                 hbox->pack_end(*label);
337                                 //label->show();
338                                 break;
339                         default:
340                                 label = new Gtk_Label("Not implemented (yet)");
341                                 label->set_justify(GTK_JUSTIFY_LEFT);
342                                 label->set_alignment(0, 0);
343                                 hbox->pack_end(*label);
344                                 //label->show();
345                                 break;
346                 }
347         //      hbox->pack_end(*label);
348         //      label->show();
349                 table->attach(*hbox, 0, 1, i, i+1);
350                 hbox->show();
351         }
352         table->set_border_width(2);
353         this->notebook = new Gtk_Frame("LDAP Options");
354         this->notebook->add(*table);
355         //table->show();
356         this->notebook->show_all();
357         return 0;
358 #endif /* LDAP_GET_OPT */
359 }
360
361 Gtk_Tree* Gtk_LdapServer::getSubtree() {
362         debug("Gtk_LdapServer::getSubtree()\n");
363         Gtk_LdapTree *tree, *subtree;
364         Gtk_LdapTreeItem *treeitem;
365         int entries;
366
367         debug("this->hostname=%s\n", this->hostname);
368         debug("this->port=%i", this->port);
369
370         char *c;
371         char *tok;
372
373         int len = g_list_length(this->databases);
374         debug("this->databases->length()=%i\n", len);
375
376         tree = new Gtk_LdapTree();
377         for (int i=0; i<len; i++) {
378                 GList *t = g_list_nth(this->databases, i);
379                 tok = strdup((char*)t->data);
380                 tok = strtok(tok, ":");
381         //      c = strtok(NULL, " ");
382                 c = strtok(NULL, "\0");
383                 debug("database %i %s\n", i, c);
384                 treeitem = new Gtk_LdapTreeItem(c, this->par, this->ld);
385                 subtree = treeitem->getSubtree(this->ld, 1);
386                 debug("inserting %s into %s\n", treeitem->rdn, this->hostname);
387                 tree->append(*treeitem);
388                 if (subtree != NULL) treeitem->set_subtree(*subtree);
389                 treeitem->show();
390         //      treeitem->setDnd();
391         //      tree->show();
392         }
393 //      this->set_subtree(*tree);
394         debug("getSubtree() done\n");
395         return tree;
396 }
397 /*
398 void Gtk_LdapServer::show_impl() {
399         debug("%s showed\n", this->hostname);
400         BaseClassType *sig=static_cast<BaseClassType *>(get_parent_class());
401         if (!sig->show) return;
402         sig->show(gtkobj());
403 //      Gtk_c_signals_Item *sig=(Gtk_c_signals_Item *)internal_getsignalbase();
404 //      sig->show(GTK_WIDGET(gtkobj()));
405 }
406 */
407 void Gtk_LdapServer::select_impl() {
408         debug("%s selected\n", this->hostname);
409 //      Gtk_c_signals_Item *sig=(Gtk_c_signals_Item *)internal_getsignalbase();
410 //      if (!sig->select) return;
411         this->showDetails();
412 //      sig->select(GTK_ITEM(gtkobj()));
413         Gtk_TreeItem::select_impl();
414 }
415
416 void Gtk_LdapServer::collapse_impl() {
417         debug("%s collapsed\n", this->hostname);
418 //      Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
419 //      if (!sig->collapse) return;
420 //      sig->collapse(GTK_TREE_ITEM(gtkobj()));
421 //      gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
422         Gtk_TreeItem::collapse_impl();
423 }
424
425 void Gtk_LdapServer::expand_impl() {
426         debug("%s expanded\n", this->hostname);
427         Gtk_TreeItem::expand_impl();
428 //      BaseClassType *sig=static_cast<BaseClassType *>(get_parent_class());
429 //      if (!sig->expand)
430 //              { return; }
431 //      sig->expand(gtkobj());
432 //      Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
433 //      if (!sig->expand) return;
434 //      sig->expand(GTK_TREE_ITEM(gtkobj()));
435 //      Gtk_Tree *t;
436 //      t = new Gtk_Tree(GTK_TREE(GTK_TREE_ITEM(this->gtkobj())->subtree));
437 //      bool vis = t->visible();
438 //      if (vis == false) {
439 //              gtk_widget_show(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
440 //              cout << this->dn << " expanded" << endl;
441 //      }
442 //      else {
443 //              gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
444 //              cout << this->dn << " collapsed" << endl;
445 //      }
446 }