]> git.sur5r.net Git - openldap/blob - contrib/gtk-tool/Gtk_LdapServer.cc
ff7c1fc362d1e0437ed9e046800391c0a1ca26a7
[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->getchild() != NULL) {
36                 xpm_label = new Gtk_HBox(GTK_HBOX(this->getchild()->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->getchild() == 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) {
59 //              debug("Have notebook here");
60                 if (par->viewport->getchild() != NULL) {
61 //                      debug(" and viewport has children");
62                         par->viewport->remove_c(par->viewport->getchild()->gtkobj());
63 //                      debug(" which have been removed");
64                 }
65 //              else debug(" and viewport without children");
66                 par->viewport->add(this->notebook);
67                 this->notebook->show();
68                 par->viewport->show();
69                 return 0;
70         }
71         if (this->getOptions() != 0) return 1;
72         this->showDetails();
73 //      debug("done\n");
74         return 0;
75 }
76
77 int Gtk_LdapServer::getConfig() {
78         debug("Gtk_LdapServer::getConfig()\n");
79         int error, entriesCount;
80         LDAPMessage *entry, *result_identifier;
81         BerElement *ber;
82         char *attribute, **t;
83
84         if ((this->ld = ldap_open(this->hostname, this->port)) == NULL) {
85                 perror("connection");
86         }
87
88         error = ldap_search_s(this->ld, "cn=config", LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, &result_identifier);    
89         entriesCount = ldap_count_entries(this->ld, result_identifier);
90         if (entriesCount == 0) {
91                 return 0;
92         }
93
94         debug("%i entry\n", entriesCount);
95         for (entry = ldap_first_entry(this->ld, result_identifier); entry != NULL; entry = ldap_next_entry(this->ld, result_identifier)) {
96                 for (attribute = ldap_first_attribute(this->ld, entry, &ber); attribute != NULL; attribute = ldap_next_attribute(this->ld, entry, ber)) {
97                         debug("Attrib: %s\n", attribute);
98                         if (strcasecmp(attribute, "database") == 0) {
99                                 debug("have database here\n");
100                                 this->databases = new G_List<char>;
101                                 t = ldap_get_values(this->ld, entry, attribute);
102                                 for (int i=0; i<ldap_count_values(t); i++) {
103                                         this->databases->append(strdup(t[i]));
104                                 }
105                                 ldap_value_free(t);
106                                 debug("databases loaded\n");
107                                 for (int i=0; i<this->databases->length(); i++) {
108                                         debug("database(%i) %s\n", i, this->databases->nth_data(i));
109                                 }       
110                         }
111                 }
112                 debug("entry done\n");
113         }
114 //      debug("got %i entries\n", entriesCount);
115         return entriesCount;
116 }
117
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
176 int Gtk_LdapServer::getOptions() {
177         debug("Gtk_LdapServer::getOptions()\n");
178         if (this->notebook != NULL) return 0;
179         LDAPAPIInfo api;
180         Gtk_HBox *hbox, *mini_hbox;
181         Gtk_VBox *vbox, *mini_vbox;
182         Gtk_Table *table;
183         Gtk_Label *label;       
184         Gtk_RadioButton *radio1, *radio2;
185         Gtk_HScale *scale;
186         Gtk_Adjustment *adjustment;
187         char *description = NULL, *s_value = NULL;
188         int i_value;
189         string label_string;
190
191         int things[10] = {
192                 LDAP_OPT_API_INFO,
193                 LDAP_OPT_CLIENT_CONTROLS,
194         //      LDAP_OPT_DESC,
195         //      LDAP_OPT_DEREF,
196                 LDAP_OPT_DNS,
197         //      LDAP_OPT_ERROR_NUMBER,
198         //      LDAP_OPT_ERROR_STRING,
199                 LDAP_OPT_HOST_NAME,
200                 LDAP_OPT_PROTOCOL_VERSION,
201                 LDAP_OPT_REFERRALS,
202                 LDAP_OPT_RESTART,
203                 LDAP_OPT_SERVER_CONTROLS,
204                 LDAP_OPT_SIZELIMIT,
205                 LDAP_OPT_TIMELIMIT
206         };
207
208 /*      if (GTK_TREE_ITEM(this->gtkobj())->subtree == NULL) {
209                 this->getSubtree();
210         } */
211
212 //      debug("getting ldap options");
213 //      vbox = new Gtk_VBox();
214         table = new Gtk_Table(10, 1, TRUE);
215
216         for (int i=0; i<10; i++) {
217         //      debug("%i\n", i);
218                 hbox = new Gtk_HBox(TRUE, 2);
219                 hbox->border_width(2);
220                 description = this->getOptDescription(things[i]);
221                 label = new Gtk_Label(description);
222                 label->set_justify(GTK_JUSTIFY_LEFT);
223                 label->set_alignment(0, 0);
224                 hbox->pack_start(*label);
225                 label->show();
226                 switch (this->getOptType(things[i])) {
227                         case 0:
228                                 ldap_get_option(this->ld, things[i], &i_value);
229                                 debug("%s value %d\n", description, i_value);
230                                 sprintf(s_value, "%d", i_value);
231                                 label = new Gtk_Label(s_value);
232                                 label->set_justify(GTK_JUSTIFY_LEFT);
233                                 label->set_alignment(0, 0);
234                                 hbox->pack_end(*label);
235                                 label->show();
236                                 break;
237                         case 1:
238                                 ldap_get_option(this->ld, things[i], &s_value);
239                                 label = new Gtk_Label(s_value);
240                                 label->set_justify(GTK_JUSTIFY_LEFT);
241                                 label->set_alignment(0, 0);
242                                 hbox->pack_end(*label);
243                                 label->show();
244                                 break;
245                         case 2:
246                                 ldap_get_option(this->ld, things[i], &i_value);
247                                 radio1 = new Gtk_RadioButton(static_cast<GSList*>(0), "Enabled");
248                                 radio2 = new Gtk_RadioButton(*radio1, "Disabled");
249                                 if (i_value == 1) radio1->set_state(true);
250                                 else radio2->set_state(true);
251                                 mini_hbox = new Gtk_HBox(FALSE, 2);
252                                 mini_hbox->border_width(2);
253                                 mini_hbox->pack_start(*radio1);
254                                 radio1->show();
255                                 mini_hbox->pack_end(*radio2);
256                                 radio2->show();
257                                 hbox->pack_end(*mini_hbox);
258                                 mini_hbox->show();
259                                 break;
260                         case 3:
261                                 ldap_get_option(this->ld, things[i], &i_value);
262                                 adjustment = new Gtk_Adjustment(i_value, 1.0, 20.0, 1.0, 1.0, 0.0);
263                                 scale = new Gtk_HScale(*adjustment);
264                                 scale->set_update_policy(GTK_UPDATE_CONTINUOUS);
265                                 scale->set_value_pos(GTK_POS_TOP);
266                                 scale->set_digits(0);
267                                 scale->set_draw_value(true);
268                                 hbox->pack_end(*scale);
269                                 scale->show();
270                                 break;
271                         case 4:
272 #ifdef LDAP_API_INFO_VERSION
273         api.ldapai_info_version = LDAP_API_INFO_VERSION;
274 #else
275         api.ldapai_info_version = 1;
276 #endif
277                                 if (ldap_get_option(this->ld, things[i], &api) != LDAP_SUCCESS) {
278                                         perror(this->getOptDescription(things[i]));
279                                         break;
280                                 }
281                                 s_value = api.ldapai_vendor_name;
282                                 label = new Gtk_Label(s_value);
283                                 label->set_justify(GTK_JUSTIFY_LEFT);
284                                 label->set_alignment(0, 0);
285                                 hbox->pack_end(*label);
286                                 label->show();
287                                 break;
288                         default:
289                                 label = new Gtk_Label("Not implemented (yet)");
290                                 label->set_justify(GTK_JUSTIFY_LEFT);
291                                 label->set_alignment(0, 0);
292                                 hbox->pack_end(*label);
293                                 label->show();
294                                 break;
295                 }
296         //      hbox->pack_end(*label);
297         //      label->show();
298                 table->attach_defaults(*hbox, 0, 1, i, i+1);
299                 hbox->show();
300         }
301         table->border_width(2);
302         this->notebook = new Gtk_Frame("LDAP Options");
303         this->notebook->add(*table);
304         table->show();
305         return 0;
306 }
307
308 Gtk_Tree* Gtk_LdapServer::getSubtree() {
309         debug("Gtk_LdapServer::getSubtree()\n");
310         Gtk_LdapTree *tree, *subtree;
311         Gtk_LdapTreeItem *treeitem;
312         int entries;
313
314         debug("this->hostname=%s\n", this->hostname);
315         debug("this->port=%i", this->port);
316
317         char *c;
318         char *tok;
319
320         int len = this->databases->length();
321         debug("this->databases->length()=%i\n", len);
322
323         tree = new Gtk_LdapTree();
324         for (int i=0; i<len; i++) {
325                 tok = strdup(this->databases->nth_data(i));
326                 tok = strtok(tok, ":");
327         //      c = strtok(NULL, " ");
328                 c = strtok(NULL, "\0");
329                 debug("database %i %s\n", i, c);
330                 treeitem = new Gtk_LdapTreeItem(c, this->par, this->ld);
331                 subtree = treeitem->getSubtree(this->ld, 1);
332                 debug("inserting %s into %s\n", treeitem->rdn, this->hostname);
333                 tree->append(*treeitem);
334                 treeitem->set_subtree(*subtree);
335                 treeitem->show();
336         //      tree->show();
337         }
338 //      this->set_subtree(*tree);
339         debug("getTree() done\n");
340         return tree;
341 }
342
343 void Gtk_LdapServer::show_impl() {
344         debug("%s showed\n", this->hostname);
345         Gtk_c_signals_Item *sig=(Gtk_c_signals_Item *)internal_getsignalbase();
346         sig->show(GTK_WIDGET(gtkobj()));
347 }
348
349 void Gtk_LdapServer::select_impl() {
350         debug("%s selected\n", this->hostname);
351         Gtk_c_signals_Item *sig=(Gtk_c_signals_Item *)internal_getsignalbase();
352         if (!sig->select) return;
353         this->showDetails();
354         sig->select(GTK_ITEM(gtkobj()));
355 }
356
357 void Gtk_LdapServer::collapse_impl() {
358         debug("%s collapsed\n", this->hostname);
359         Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
360         if (!sig->collapse) return;
361         sig->collapse(GTK_TREE_ITEM(gtkobj()));
362 //      gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
363 }
364
365 void Gtk_LdapServer::expand_impl() {
366         debug("%s expanded\n", this->hostname);
367         Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
368         if (!sig->expand) return;
369         sig->expand(GTK_TREE_ITEM(gtkobj()));
370 //      Gtk_Tree *t;
371 //      t = new Gtk_Tree(GTK_TREE(GTK_TREE_ITEM(this->gtkobj())->subtree));
372 //      bool vis = t->visible();
373 //      if (vis == false) {
374 //              gtk_widget_show(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
375 //              cout << this->dn << " expanded" << endl;
376 //      }
377 //      else {
378 //              gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
379 //              cout << this->dn << " collapsed" << endl;
380 //      }
381 }