]> git.sur5r.net Git - openldap/blob - contrib/gtk-tool/Gtk_LdapServer.cc
a4459814b5b812404e7cd0dc97af44d1e0f2a3c1
[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         this->par = w;
13         this->hostname = h;
14         this->port = p;
15         cout << this->hostname << this->port << endl;
16         this->getConfig();
17 }
18
19 Gtk_LdapServer::Gtk_LdapServer(GtkTreeItem *t) : Gtk_TreeItem(t) {
20 }
21
22 Gtk_LdapServer::~Gtk_LdapServer() {
23         cout << "Bye" << endl;
24         delete this;
25 }
26
27 void Gtk_LdapServer::setType(int t) {
28         cout << "Gtk_LdapServer::setType(" << t << ")" << endl;
29         Gtk_Pixmap *xpm_icon;
30         Gtk_Label *label;
31         if (this->getchild() != NULL) {
32                 xpm_label = new Gtk_HBox(GTK_HBOX(this->getchild()->gtkobj()));
33                 xpm_label->remove_c(xpm_label->children()->nth_data(0));
34                 xpm_label->remove_c(xpm_label->children()->nth_data(0));
35         }
36         else xpm_label = new Gtk_HBox();
37         cout << this->hostname << endl;
38         if (strcasecmp(this->hostname,"localhost") == 0)
39                 xpm_icon=new Gtk_Pixmap(*xpm_label, local_server);
40         else xpm_icon=new Gtk_Pixmap(*xpm_label, remote_server);
41         label = new Gtk_Label(this->hostname);
42         xpm_label->pack_start(*xpm_icon, false, false, 1);
43         xpm_label->pack_start(*label, false, false, 1);
44         if (this->getchild() == NULL) this->add(xpm_label);
45         label->show();
46         xpm_label->show();
47         xpm_icon->show();
48 }
49
50 int Gtk_LdapServer::showDetails() {
51         cout << "Gtk_LdapServer::showDetails()" << endl;
52         this->getDetails();
53         /*
54         if (this->notebook != NULL) {
55                 if (par->viewport->getchild() != NULL) {
56                         par->viewport->remove_c(par->viewport->getchild()->gtkobj());
57                 }
58                 par->viewport->add(this->notebook);
59                 this->notebook->show();
60                 par->viewport->show();
61                 return 0;
62         }
63         else this->getDetails();
64         this->showDetails();
65         */
66         return 0;
67 }
68
69 int Gtk_LdapServer::getConfig() {
70         cout << "Gtk_LdapServer::getConfig()" << endl;
71         int error, entriesCount;
72         LDAPMessage *entry, *result_identifier;
73         BerElement *ber;
74         char *attribute, **t;
75
76         if ((this->ld = ldap_open(this->hostname, this->port)) == NULL) {
77                 perror("connection");
78         }
79
80         error = ldap_search_s(this->ld, "cn=config", LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, &result_identifier);    
81         entriesCount = ldap_count_entries(this->ld, result_identifier);
82         if (entriesCount == 0) {
83                 return 0;
84         }
85
86         cout << entriesCount << " entry" << endl;
87         for (entry = ldap_first_entry(this->ld, result_identifier); entry != NULL; entry = ldap_next_entry(this->ld, result_identifier)) {
88                 for (attribute = ldap_first_attribute(this->ld, entry, &ber); attribute != NULL; attribute = ldap_next_attribute(this->ld, entry, ber)) {
89                         cout << "Attrib: " << attribute << endl;
90                         if (strcasecmp(attribute, "database") == 0) {
91                                 cout << "have database here" << endl;
92                                 this->databases = new G_List<char>;
93                                 t = ldap_get_values(this->ld, entry, attribute);
94                                 for (int i=0; i<ldap_count_values(t); i++) {
95                                         this->databases->append(strdup(t[i]));
96                                 }
97                                 ldap_value_free(t);
98                                 cout << "databases loaded" << endl;
99                                 for (int i=0; i<this->databases->length(); i++) {
100                                         cout << "database(" << i << ") " << this->databases->nth_data(i) << endl;
101                                 }       
102                         }
103                 }
104                 cout << "entry done" << endl;
105         }
106 //      cout << "got " << entriesCount << " entries" << endl;
107         return entriesCount;
108 }
109
110 int Gtk_LdapServer::getDetails() {
111         cout << "Gtk_LdapServer::getDetails()" << endl;
112         Gtk_HBox *hbox;
113         Gtk_VBox *vbox;
114         Gtk_Label *label;       
115         Gtk_RadioButton *radio1, *radio2;
116         char *val;
117         int ival;
118
119         if (GTK_TREE_ITEM(this->gtkobj())->subtree == NULL) {
120                 this->getSubtree();
121         }
122
123 /*
124         cout << "getting ldap options";
125         vbox = new Gtk_VBox();
126         opt_util = new LdapOpts();
127
128         for (int i=0; i<sizeof(things); i++) {
129                 cout << i << endl;
130                 hbox = new Gtk_HBox();
131                 label = new Gtk_Label(LdapOpts->getOption(things[i]);
132                 hbox->pack_start(*label);
133                 label->show();
134                 int tipus = opt_util->getType(things[i]);
135                 switch (tipus) {
136                         case 0:
137                                 ldap_get_option(NULL, things[i], &val);
138                                 label = new Gtk_Label(val);
139                                 break;
140                         case 1:
141                                 ldap_get_option(NULL, numerals[i], &ival);
142                                 sprintf(val, "%i", ival);
143                                 label = new Gtk_Label(val);
144                                 break;
145                         case 2:
146                                 ldap_get_option(NULL, booleans[i], &ival);
147                                 sprintf(val, "%s", ival == (int) LDAP_OPT_ON ? "on" : "off");
148                                 label = new Gtk_Label(val);
149                                 break;
150                         default:
151                                 break;
152                 }
153
154                 hbox->pack_start(*label);
155                 label->show();
156                 vbox->pack_start(*hbox);
157                 hbox->show();
158                 
159         }
160
161         vbox->border_width(2);
162         this->notebook = new Gtk_Viewport();
163         this->notebook->add(*vbox);
164         vbox->show();
165 */
166         this->setType(1);
167         return 0;
168 }
169
170 int Gtk_LdapServer::getSubtree() {
171         cout << "Gtk_LdapServer::getSubtree()" << endl;
172         Gtk_LdapItem *treeresult;
173         Gtk_Tree *tree, *subtree;
174         Gtk_LdapTreeItem *treeitem;
175         int entries;
176
177         cout << "this->hostname=" << this->hostname << endl;
178         cout << "this->port=" << this->port << endl;
179 /*      if ((this->ld = ldap_open(this->hostname, this->port)) == NULL) {
180                 perror("connection");
181         }
182 */
183
184         char *c;
185         char *tok;
186
187         int len = this->databases->length();
188         cout << "this->databases->length()=" << len << endl;
189
190         tree = new Gtk_Tree();
191         for (int i=0; i<len; i++) {
192                 tok = strdup(this->databases->nth_data(i));
193                 tok = strtok(tok, ":");
194                 c = strtok(NULL, "\0");
195                 cout << "database " << i << " " << c << endl;
196                 treeresult = this->par->make_tree(this->par, this->ld, c);
197                 treeitem = new Gtk_LdapTreeItem(*treeresult->treeitem);
198                 tree->append(*treeitem);
199                 if (treeresult->tree != NULL) {
200                         subtree = new Gtk_Tree(*treeresult->tree);
201                         treeitem->set_subtree(*subtree);
202                 }
203                 treeitem->show();
204         //      tree->show();
205         }
206         this->set_subtree(*tree);
207         cout << "getTree() done" << endl;
208         return 0;
209 }
210
211 void Gtk_LdapServer::select_impl() {
212         cout << this->hostname << " selected" << endl;
213 //      gtk_item_select(GTK_ITEM(GTK_TREE_ITEM(this->gtkobj())));
214         Gtk_c_signals_Item *sig=(Gtk_c_signals_Item *)internal_getsignalbase();
215         if (!sig->select) return;
216         sig->select(GTK_ITEM(gtkobj()));
217         this->showDetails();
218 }
219
220 void Gtk_LdapServer::collapse_impl() {
221 //      cout << this->dn << " collapsed" << endl;
222         Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
223         if (!sig->collapse) return;
224         sig->collapse(GTK_TREE_ITEM(gtkobj()));
225 //      gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
226 }
227
228 void Gtk_LdapServer::expand_impl() {
229 //      cout << this->dn << " expanded" << endl;
230         Gtk_c_signals_TreeItem *sig=(Gtk_c_signals_TreeItem *)internal_getsignalbase();
231         if (!sig->expand) return;
232         sig->expand(GTK_TREE_ITEM(gtkobj()));
233 //      Gtk_Tree *t;
234 //      t = new Gtk_Tree(GTK_TREE(GTK_TREE_ITEM(this->gtkobj())->subtree));
235 //      bool vis = t->visible();
236 //      if (vis == false) {
237 //              gtk_widget_show(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
238 //              cout << this->dn << " expanded" << endl;
239 //      }
240 //      else {
241 //              gtk_widget_hide(GTK_WIDGET(GTK_TREE(GTK_TREE_ITEM (this->gtkobj())->subtree)));
242 //              cout << this->dn << " collapsed" << endl;
243 //      }
244 }