aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/e-ldap-storage.c
blob: ab3fc21fdb710e49e458cb0d8fb64562c7fb11b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-ldap-storage.c
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Chris Toshok
 */

/* The ldap server file goes like this:

   <?xml version="1.0"?>
   <contactservers>
     <contactserver>
           <name>LDAP Server</name>
       <description>This is my company address book.</description>
       <uri>ldap://ldap.somewhere.net/</uri>
     </contactserver>
   </contactservers>

   FIXME: Do we want to use a namespace for this?
   FIXME: Do we want to have an internationalized description?
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <bonobo.h>

#include "evolution-shell-component.h"
#include "evolution-storage.h"

#include <gnome-xml/parser.h>
#include <gnome-xml/xmlmemory.h>

#include "e-ldap-storage.h"
#include "e-ldap-server-dialog.h"

#include "e-util/e-util.h"
#include "e-util/e-xml-utils.h"

#define LDAPSERVER_XML "ldapservers.xml"

static gboolean load_ldap_data (EvolutionStorage *storage, const char *file_path);
static gboolean save_ldap_data (const char *file_path);

GHashTable *servers;
EvolutionStorage *storage;

void
setup_ldap_storage (EvolutionShellComponent *shell_component)
{
    EvolutionShellClient *shell_client;
    Evolution_Shell corba_shell;
    char *path;

    shell_client = evolution_shell_component_get_owner (shell_component);
    if (shell_client == CORBA_OBJECT_NIL) {
        g_warning ("We have no shell!?");
        return;
    }

    corba_shell = bonobo_object_corba_objref (BONOBO_OBJECT (shell_client));

    storage = evolution_storage_new (_("External Directories"));
    if (evolution_storage_register_on_shell (storage, corba_shell) != EVOLUTION_STORAGE_OK) {
        g_warning ("Cannot register storage");
        return;
    }

    /* save the storage for later */
    servers = g_hash_table_new (g_str_hash, g_str_equal);

    gtk_object_set_data (GTK_OBJECT (shell_component), "e-storage", storage);

    path = g_strdup_printf ("%s/evolution/" LDAPSERVER_XML, g_get_home_dir());
    load_ldap_data (storage, path);
    g_free (path);
}

static char *
get_string_value (xmlNode *node,
          const char *name)
{
    xmlNode *p;
    xmlChar *xml_string;
    char *retval;

    p = e_xml_get_child_by_name (node, (xmlChar *) name);
    if (p == NULL)
        return NULL;

    p = e_xml_get_child_by_name (p, (xmlChar *) "text");
    if (p == NULL)
        return NULL;

    xml_string = xmlNodeListGetString (node->doc, p, 1);
    retval = g_strdup ((char *) xml_string);
    xmlFree (xml_string);

    return retval;
}

static gboolean
load_ldap_data (EvolutionStorage *storage,
        const char *file_path)
{
    xmlDoc *doc;
    xmlNode *root;
    xmlNode *child;

    doc = xmlParseFile (file_path);
    if (doc == NULL) {
        return FALSE;
    }

    root = xmlDocGetRootElement (doc);
    if (root == NULL || strcmp (root->name, "contactservers") != 0) {
        xmlFreeDoc (doc);
        return FALSE;
    }

    for (child = root->childs; child; child = child->next) {
        char *path;
        ELDAPServer *server;

        if (strcmp (child->name, "contactserver")) {
            g_warning ("unknown node '%s' in %s", child->name, file_path);
            continue;
        }

        server = g_new (ELDAPServer, 1);

        server->name = get_string_value (child, "name");
        server->uri = get_string_value (child, "uri");
        server->description = get_string_value (child, "description");

        path = g_strdup_printf ("/%s", server->name);
        evolution_storage_new_folder (storage, path, "contacts", server->uri, server->description);

        g_hash_table_insert (servers, server->name, server);

        g_free (path);
    }

    xmlFreeDoc (doc);

    return TRUE;
}

static void
ldap_server_foreach(gpointer key, gpointer value, gpointer user_data)
{
    ELDAPServer *server = (ELDAPServer*)value;
    xmlNode *root = (xmlNode*)user_data;
    xmlNode *server_root = xmlNewDocNode (root, NULL,
                          (xmlChar *) "contactserver", NULL);

    xmlAddChild (root, server_root);

    xmlNewChild (server_root, NULL, (xmlChar *) "name",
             (xmlChar *) server->name);

    xmlNewChild (server_root, NULL, (xmlChar *) "uri",
             (xmlChar *) server->uri);

    if (server->description)
        xmlNewChild (server_root, NULL, (xmlChar *) "description",
                 (xmlChar *) server->description);
}

static gboolean
save_ldap_data (const char *file_path)
{
    xmlDoc *doc;
    xmlNode *root;

    doc = xmlNewDoc ((xmlChar *) "1.0");
    root = xmlNewDocNode (doc, NULL, (xmlChar *) "contactservers", NULL);
    xmlDocSetRootElement (doc, root);

    g_hash_table_foreach (servers, ldap_server_foreach, root);

    if (xmlSaveFile (file_path, doc) < 0) {
        unlink (file_path);
        xmlFreeDoc (doc);
        return FALSE;
    }

    xmlFreeDoc (doc);
    return TRUE;
}

void
e_ldap_storage_add_server (ELDAPServer *server)
{
    char *path;
    /* add it to our hashtable */
    g_hash_table_insert (servers, server->name, server);

    /* and then to the ui */
    path = g_strdup_printf ("/%s", server->name);
    evolution_storage_new_folder (storage, path, "contacts", server->uri, server->description);

    g_free (path);

    path = g_strdup_printf ("%s/evolution/" LDAPSERVER_XML, g_get_home_dir());
    save_ldap_data (path);
    g_free (path);
}

void
e_ldap_storage_remove_server (char *name)
{
}