]> git.sur5r.net Git - openldap/blob - contrib/gtk-tool/Gtk_LdapTreeItem.cc
update project settings
[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         }
33         counter--;
34         Gtk_LdapTree *subtree = NULL, *tree = NULL;
35         Gtk_LdapTreeItem *subtreeitem = NULL;
36         LDAPMessage *r_i = NULL, *entry = NULL;
37         gchar *c;
38         char **s;
39         int entriesCount = 0, error;
40
41         this->ld = ld;
42         error = ldap_search_s(this->ld, this->dn, LDAP_SCOPE_ONELEVEL, "objectclass=*", NULL, 0, &r_i);
43 //      printf("%s\n", ldap_err2string(error));
44         entriesCount = ldap_count_entries(this->ld, r_i);
45         debug("%i results\n", entriesCount);
46         if (entriesCount != 0) { 
47                 tree = new Gtk_LdapTree();
48                 tree->set_selection_mode(GTK_SELECTION_BROWSE);
49                 tree->set_view_mode(GTK_TREE_VIEW_ITEM);
50                 tree->set_view_lines(false);
51                 entry = ldap_first_entry(this->ld, r_i);
52         //      float i = 1;
53         //      float percent = 100/entriesCount;
54         //      cout << "percent is " << percent << endl;
55         //      this->par->progress.update(0);
56         //      this->par->progress.show();
57                 while (entry != NULL) {
58                         subtreeitem = new Gtk_LdapTreeItem(ldap_get_dn(this->ld, entry), this->par, this->ld);
59                         subtree = subtreeitem->getSubtree(this->ld, counter);
60                         debug("inserting %s into %s\n",subtreeitem->rdn,this->rdn);
61                         tree->append(*subtreeitem);
62                         subtreeitem->show();
63                         if (subtree != NULL) subtreeitem->set_subtree(*subtree);
64                         debug("done\n");
65                         entry = ldap_next_entry(this->ld, entry);
66                 //      gfloat pvalue = (i*percent)/100;
67                 //      cout << pvalue << " %" << endl;
68                 //      this->par->progress.update(pvalue);
69                 //      this->par->progress.show();
70                 //      i++;
71                 }
72         //      this->set_subtree(*tree);
73         //      this->par->progress.update(0);
74         //      this->par->progress->show();
75         }
76 //      this->getDetails();
77         debug("done\n");
78         return tree;
79 }
80
81 void Gtk_LdapTreeItem::setType(int t) {
82         debug("Gtk_LdapTreeItem::setType(%s)\n", this->objectClass);
83         Gtk_Pixmap *xpm_icon;
84         Gtk_Label *label;
85         if (this->get_child() != NULL) {
86                 xpm_label = new Gtk_HBox(GTK_HBOX(this->get_child()->gtkobj()));
87                 xpm_label->remove_c(xpm_label->children()->nth_data(0));
88                 xpm_label->remove_c(xpm_label->children()->nth_data(0));
89         }
90         else xpm_label = new Gtk_HBox();
91         if (strcasecmp(this->objectClass,"organization") == 0)
92                 xpm_icon=new Gtk_Pixmap(*xpm_label, root_node);
93         else if (strcasecmp(this->objectClass,"organizationalunit") == 0)
94                 xpm_icon=new Gtk_Pixmap(*xpm_label, branch_node);
95         else if (strcasecmp(this->objectClass,"person") == 0)
96                 xpm_icon=new Gtk_Pixmap(*xpm_label, leaf_node);
97         else if (strcasecmp(this->objectClass,"alias") == 0)
98                 xpm_icon=new Gtk_Pixmap(*xpm_label, alias_node);
99         else if (strcasecmp(this->objectClass,"rfc822mailgroup") == 0)
100                 xpm_icon=new Gtk_Pixmap(*xpm_label, rfc822mailgroup_node);
101         else xpm_icon=new Gtk_Pixmap(*xpm_label, general_node);
102         label = new Gtk_Label(this->rdn);
103         xpm_label->pack_start(*xpm_icon, false, false, 1);
104         xpm_label->pack_start(*label, false, false, 1);
105         if (this->get_child() == NULL) this->add(xpm_label);
106         label->show();
107         xpm_label->show();
108         xpm_icon->show();
109 }
110
111 int Gtk_LdapTreeItem::showDetails() {
112         debug("Gtk_LdapTreeItem::showDetails()\n");
113         if (this->notebook == NULL) this->getDetails();
114         if (this->notebook != NULL) {
115                 debug("Have a notebook here");
116                 if (par->viewport2->get_child() != NULL) {
117                         debug(" and the viewport has children");
118                         par->viewport2->remove(par->viewport2->get_child());
119                         debug(" which have been removed");
120                 }
121                 else debug(" and viewport has no children");
122                 par->viewport2->add(*this->notebook);
123                 this->notebook->show();
124                 par->viewport2->show();
125                 return 0;
126         }
127         else debug("No notebook and no details");
128         return 0;
129 }
130
131 int Gtk_LdapTreeItem::getDetails() {
132         debug("Gtk_LdapTreeItem::getDetails()\n");
133         int error, entriesCount;
134         BerElement *ber;
135         LDAPMessage *entry;
136         char *attribute, **values;
137         char attrib[32];
138         Gtk_CList *table;
139         Gtk_Label *label;
140         GList *child_list;
141 //      Gtk_Notebook *g;
142         Gtk_Viewport *viewport;
143         error = ldap_search_s(this->ld, this->dn, LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, &this->result_identifier);
144         entriesCount = ldap_count_entries(this->ld, this->result_identifier);
145         if (entriesCount == 0) return 0;
146         this->notebook = new Gtk_Notebook();
147         this->notebook->set_tab_pos(GTK_POS_LEFT);
148         const gchar *titles[] = { "values" };
149         
150         for (entry = ldap_first_entry(ld, result_identifier); entry != NULL; entry = ldap_next_entry(ld, result_identifier)) {
151                 for (attribute = ldap_first_attribute(ld, entry, &ber); attribute != NULL; attribute = ldap_next_attribute(ld, entry, ber)) {
152                         values = ldap_get_values(ld, entry, attribute);
153                         if (strcasecmp(attribute, "objectclass") == 0) {
154                         //      debug("processing objectclass\n");
155                                 if (strcasecmp(values[0],"top") == 0)
156                                         this->objectClass = strdup(values[1]);
157                                 else this->objectClass = strdup(values[0]);
158                         }
159                         table = new Gtk_CList(1, titles);
160                         for (int i=0; i<ldap_count_values(values); i++) {
161                         //      debug("%i:%s\n",i, values[i]);
162                                 const gchar *t[] = { values[i] };
163                                 table->append(t);
164                         }
165                         ldap_value_free(values);
166                         sprintf(attrib, "%s", attribute);
167                         label = new Gtk_Label(attrib);
168                         label->set_alignment(0, 0);
169                         label->set_justify(GTK_JUSTIFY_LEFT);
170                         this->notebook->append_page(*table, *label);
171                         table->show();
172                         label->show();
173                 }
174         }
175         this->setType(1);
176         debug("done\n");
177         return 0;
178 }
179 /*
180 void Gtk_LdapTreeItem::show_impl() {
181         debug("%s showed\n", this->dn);
182 //      Gtk_c_signals_Base *sig=(Gtk_c_signals_Base *)internal_getsignalbase();
183 //      sig->show(GTK_WIDGET(gtkobj()));
184 }
185 */
186 void Gtk_LdapTreeItem::select_impl() {
187         debug("%s selected\n", this->dn);
188         this->showDetails();
189         Gtk_TreeItem::select_impl();
190 }
191
192 void Gtk_LdapTreeItem::collapse_impl() {
193         debug("%s collapsed\n", this->dn);
194         Gtk_TreeItem::collapse_impl();
195 }
196
197 void Gtk_LdapTreeItem::expand_impl() {
198         debug("%s expanded\n",this->dn);
199         Gtk_LdapTreeItem *item;
200         G_List<GtkWidget> *list;
201         Gtk_Tree *tree;
202         Gtk_TreeItem::expand_impl();
203 }