]> git.sur5r.net Git - openldap/blob - contrib/gtk-tool/Gtk_LdapTreeItem.cc
7add7b09546df9b0d1033be922d516edb9bbd5e8
[openldap] / contrib / gtk-tool / Gtk_LdapTreeItem.cc
1 #include "Gtk_LdapTreeItem.h"
2
3 Gtk_LdapTreeItem::Gtk_LdapTreeItem() : Gtk_TreeItem() {
4         this->objectClass = NULL;
5 }
6
7 Gtk_LdapTreeItem::Gtk_LdapTreeItem(char *c, My_Window *w, LDAP *ld) : Gtk_TreeItem() {
8         debug("Gtk_LdapTreeItem::Gtk_LdapTreeItem(%s)\n", c);
9         char **s;
10         this->dn = c;
11         s = ldap_explode_dn(this->dn, 1);
12         this->rdn = g_strdup_printf("%s", s[0]);
13         this->par = w;
14         this->ld = ld;
15         this->objectClass = NULL;
16         this->getDetails();
17 }
18
19 Gtk_LdapTreeItem::Gtk_LdapTreeItem(GtkTreeItem *t) : Gtk_TreeItem(t) {
20 }
21
22 Gtk_LdapTreeItem::~Gtk_LdapTreeItem() {
23         cout << "Bye" << endl;
24         delete this;
25 }
26
27 Gtk_LdapTree* Gtk_LdapTreeItem::getSubtree(LDAP *ld, int counter) {
28         debug("Gtk_LdapTreeItem::getSubtree(%s)\n", this->dn);
29         if (counter <= 0) return NULL;
30         if (this->gtkobj()->subtree != NULL) {
31                 //return (Gtk_LdapTree *)GTK_TREE(this->gtkobj()->subtree);
32                 debug("This item has a subtree\n");
33                 return (Gtk_LdapTree *)this->get_subtree(); //gtkobj()->subtree);
34         }
35         counter--;
36         Gtk_LdapTree *subtree = NULL, *tree = NULL;
37         Gtk_LdapTreeItem *subtreeitem = NULL;
38         LDAPMessage *r_i = NULL, *entry = NULL;
39         gchar *c;
40         char **s;
41         int entriesCount = 0, error;
42
43         this->ld = ld;
44         if (this->dn == "cn=config" || this->dn == "cn=monitor") error = ldap_search_s(this->ld, this->dn, LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, &r_i);
45         else {
46                 if (strcasecmp(this->objectClass,"alias") == 0) error = ldap_search_s(this->ld, this->getAttribute("aliasedobjectname"), LDAP_SCOPE_ONELEVEL, "objectclass=*", NULL, 0, &r_i);
47                 else error = ldap_search_s(this->ld, this->dn, LDAP_SCOPE_ONELEVEL, "objectclass=*", NULL, 0, &r_i);
48         }
49 //      printf("%s\n", ldap_err2string(error));
50         entriesCount = ldap_count_entries(this->ld, r_i);
51         debug("%i results\n", entriesCount);
52         if (entriesCount != 0) { 
53                 tree = new Gtk_LdapTree();
54                 tree->set_selection_mode(GTK_SELECTION_BROWSE);
55                 tree->set_view_mode(GTK_TREE_VIEW_ITEM);
56                 tree->set_view_lines(false);
57                 entry = ldap_first_entry(this->ld, r_i);
58         //      float i = 1;
59                 gfloat percent = 100/entriesCount;
60                 cout << "percent is " << percent << endl;
61         //      this->par->progress.set_percentage(percent/100);
62         //      this->par->progress.show();
63                 while (entry != NULL) {
64                         subtreeitem = new Gtk_LdapTreeItem(ldap_get_dn(this->ld, entry), this->par, this->ld);
65                         subtree = subtreeitem->getSubtree(this->ld, counter);
66                         debug("inserting %s into %s\n",subtreeitem->rdn,this->rdn);
67                         tree->append(*subtreeitem);
68                         subtreeitem->show();
69                         if (subtree != NULL) subtreeitem->set_subtree(*subtree);
70                         debug("done\n");
71                         entry = ldap_next_entry(this->ld, entry);
72                 //      gfloat pvalue = (i*percent)/100;
73                 //      cout << pvalue << " %" << endl;
74                 //      this->par->progress.update(pvalue);
75                 //      this->par->progress.show();
76                 //      i++;
77                 }
78         //      this->set_subtree(*tree);
79         //      this->par->progress.update(0);
80         //      this->par->progress->show();
81         }
82 //      this->getDetails();
83         debug("done\n");
84         return tree;
85 }
86
87 void Gtk_LdapTreeItem::setType(int t) {
88         debug("Gtk_LdapTreeItem::setType(%s)\n", this->objectClass);
89         Gtk_Pixmap *xpm_icon;
90         Gtk_Label *label;
91         if (this->get_child() != NULL) {
92                 debug("got a child here");
93                 //xpm_label = new Gtk_HBox(this->get_child());
94                 this->remove();
95                 /*
96                 //xpm_label = new Gtk_HBox(*GTK_HBOX(this->get_child()->gtkobj()));
97                 xpm_label = new Gtk_HBox(this->get_child()); //->gtkobj());
98                 //xpm_label->remove_c(xpm_label->children().nth_data(0));
99                 Gtk_HBox::BoxList &list = xpm_label->children();
100                 Gtk_HBox::BoxList::iterator i = list.begin();
101                 xpm_label->remove(*i);
102                 //xpm_label->remove_c(xpm_label->children().nth_data(0));
103                 xpm_label->remove(*xpm_label->children().begin());
104                 */
105         }
106         xpm_label = new Gtk_HBox();
107         if (strcasecmp(this->objectClass,"organization") == 0)
108                 //xpm_icon=new Gtk_Pixmap(*xpm_label, root_node);
109                 xpm_icon=new Gtk_Pixmap(root_node);
110         else if (strcasecmp(this->objectClass,"organizationalunit") == 0)
111                 //xpm_icon=new Gtk_Pixmap(*xpm_label, branch_node);
112                 xpm_icon=new Gtk_Pixmap(branch_node);
113         else if (strcasecmp(this->objectClass,"person") == 0)
114                 //xpm_icon=new Gtk_Pixmap(*xpm_label, leaf_node);
115                 xpm_icon=new Gtk_Pixmap(leaf_node);
116         else if (strcasecmp(this->objectClass,"alias") == 0)
117                 //xpm_icon=new Gtk_Pixmap(*xpm_label, alias_node);
118                 xpm_icon=new Gtk_Pixmap(alias_node);
119         else if (strcasecmp(this->objectClass,"rfc822mailgroup") == 0)
120                 //xpm_icon=new Gtk_Pixmap(*xpm_label, rfc822mailgroup_node);
121                 xpm_icon=new Gtk_Pixmap(rfc822mailgroup_node);
122         else if (strcasecmp(this->objectClass,"LDAPsubentry") == 0)
123                 xpm_icon=new Gtk_Pixmap(monitor);
124         else //xpm_icon=new Gtk_Pixmap(*xpm_label, general_node);
125                 xpm_icon=new Gtk_Pixmap(general_node);
126         label = new Gtk_Label(this->rdn);
127         xpm_label->pack_start(*xpm_icon, false, false, 1);
128         xpm_label->pack_start(*label, false, false, 1);
129         if (this->get_child() == NULL) {
130                 debug("no children - GREAT!!");
131                 this->add(*xpm_label);
132         }
133         //label->show();
134         //xpm_icon->show();
135         //xpm_label->show();
136         show_all();
137 }
138
139 int Gtk_LdapTreeItem::showDetails() {
140         debug("Gtk_LdapTreeItem::showDetails()\n");
141         if (this->notebook == NULL) this->getDetails();
142         if (this->notebook != NULL) {
143                 debug("Have a notebook here");
144                 if (par->viewport2->get_child() != NULL) {
145                         debug(" and the viewport has children");
146                         //par->viewport2->remove(par->viewport2->get_child());
147                         par->viewport2->remove();
148                         debug(" which have been removed");
149                 }
150                 else debug(" and viewport has no children");
151                 par->viewport2->add(*this->notebook);
152                 this->notebook->show();
153                 par->viewport2->show();
154                 return 0;
155         }
156         else debug("No notebook and no details");
157         return 0;
158 }
159
160 char* Gtk_LdapTreeItem::getAttribute(char *c) {
161         int entriesCount, error;
162         BerElement *ber;
163         LDAPMessage *entry;
164         char *attribute, **values;
165         error = ldap_search_s(this->ld, this->dn, LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, &this->result_identifier);
166         entriesCount = ldap_count_entries(this->ld, this->result_identifier);
167         if (entriesCount == 0) return 0;
168         for (entry = ldap_first_entry(ld, result_identifier); entry != NULL; entry = ldap_next_entry(ld, result_identifier)) {
169                 for (attribute = ldap_first_attribute(ld, entry, &ber); attribute != NULL; attribute = ldap_next_attribute(ld, entry, ber)) {
170                         values = ldap_get_values(ld, entry, attribute);
171                         if (strcasecmp(attribute, "aliasedobjectname") == 0) {
172                                 this->aliasedObjectName = strdup(values[0]);
173                         }
174                 }
175         }
176         return this->aliasedObjectName;
177 }
178
179 int Gtk_LdapTreeItem::getDetails() {
180         debug("Gtk_LdapTreeItem::getDetails()\n");
181         int error, entriesCount;
182         BerElement *ber;
183         LDAPMessage *entry;
184         char *attribute, **values;
185         char attrib[32];
186         Gtk_CList *table;
187         Gtk_Label *label;
188         GList *child_list;
189         Gtk_Viewport *viewport;
190         error = ldap_search_s(this->ld, this->dn, LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, &this->result_identifier);
191         entriesCount = ldap_count_entries(this->ld, this->result_identifier);
192         if (entriesCount == 0) return 0;
193         this->notebook = new Gtk_Notebook();
194         this->notebook->set_tab_pos(GTK_POS_LEFT);
195         const gchar *titles[] = { "values" };
196         
197         for (entry = ldap_first_entry(ld, result_identifier); entry != NULL; entry = ldap_next_entry(ld, result_identifier)) {
198                 for (attribute = ldap_first_attribute(ld, entry, &ber); attribute != NULL; attribute = ldap_next_attribute(ld, entry, ber)) {
199                         values = ldap_get_values(ld, entry, attribute);
200                         if (strcasecmp(attribute, "objectclass") == 0) {
201                         //      debug("processing objectclass\n");
202                                 if (strcasecmp(values[0],"top") == 0)
203                                         this->objectClass = strdup(values[1]);
204                                 else this->objectClass = strdup(values[0]);
205                         }
206                         table = new Gtk_CList(1, titles);
207                         for (int i=0; i<ldap_count_values(values); i++) {
208                         //      debug("%i:%s\n",i, values[i]);
209                                 const gchar *t[] = { values[i] };
210                                 table->append(t);
211                         }
212                         ldap_value_free(values);
213                         sprintf(attrib, "%s", attribute);
214                         label = new Gtk_Label(attrib);
215                         label->set_alignment(0, 0);
216                         label->set_justify(GTK_JUSTIFY_LEFT);
217                         this->notebook->pages().push_back(Gtk_Notebook_Helpers::TabElem(*table, *label));
218                         table->show();
219                         label->show();
220                 }
221         }
222         this->setType(1);
223         debug("done\n");
224         return 0;
225 }
226 /*
227 void Gtk_LdapTreeItem::show_impl() {
228         debug("%s showed\n", this->dn);
229 //      Gtk_c_signals_Base *sig=(Gtk_c_signals_Base *)internal_getsignalbase();
230 //      sig->show(GTK_WIDGET(gtkobj()));
231 }
232 */
233 void Gtk_LdapTreeItem::select_impl() {
234         debug("%s selected\n", this->dn);
235         this->showDetails();
236         Gtk_TreeItem::select_impl();
237 }
238
239 void Gtk_LdapTreeItem::collapse_impl() {
240         debug("%s collapsed\n", this->dn);
241         Gtk_TreeItem::collapse_impl();
242 }
243
244 void Gtk_LdapTreeItem::expand_impl() {
245         debug("%s expanded\n",this->dn);
246         Gtk_LdapTreeItem *item;
247         G_List<GtkWidget> *list;
248         Gtk_Tree *tree;
249         Gtk_TreeItem::expand_impl();
250 }