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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/* Ekiga -- A VoIP and Video-Conferencing application
* Copyright (C) 2000-2009 Damien Sandras <dsandras@seconix.com>
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* Ekiga is licensed under the GPL license and as a special exception,
* you have permission to link or otherwise combine this program with the
* programs OPAL, OpenH323 and PWLIB, and distribute the combination,
* without applying the requirements of the GNU GPL to the OPAL, OpenH323
* and PWLIB programs, as long as you do follow the requirements of the
* GNU GPL for all the rest of the software thus combined.
*/
/*
* ldap-source.cpp - description
* ------------------------------------------
* begin : written in 2007 by Julien Puydt
* : completed in 2008 by Howard Chu
* copyright : (c) 2007 by Julien Puydt
* description : implementation of a LDAP source
*
*/
#include <cstdlib>
#include <string.h>
#include "config.h"
#include "gmconf.h"
#include "ldap-source.h"
#define KEY "/apps/" PACKAGE_NAME "/contacts/ldap_servers"
OPENLDAP::Source::Source (Ekiga::ServiceCore &_core):
core(_core), doc(), should_add_ekiga_net_book(false)
{
xmlNodePtr root;
gchar *c_raw = gm_conf_get_string (KEY);
if (c_raw != NULL && g_strcmp0 (c_raw, "")) {
const std::string raw = c_raw;
doc = boost::shared_ptr<xmlDoc> (xmlRecoverMemory (raw.c_str (), raw.length ()), xmlFreeDoc);
if ( !doc)
doc = boost::shared_ptr<xmlDoc> (xmlNewDoc (BAD_CAST "1.0"), xmlFreeDoc);
root = xmlDocGetRootElement (doc.get ());
if (root == NULL) {
root = xmlNewDocNode (doc.get (), NULL, BAD_CAST "list", NULL);
xmlDocSetRootElement (doc.get (), root);
}
migrate_from_3_0_0 ();
for (xmlNodePtr child = root->children ;
child != NULL ;
child = child->next)
if (child->type == XML_ELEMENT_NODE
&& child->name != NULL
&& xmlStrEqual (BAD_CAST "server", child->name))
add (child);
g_free (c_raw);
} else {
doc = boost::shared_ptr<xmlDoc> (xmlNewDoc (BAD_CAST "1.0"), xmlFreeDoc);
root = xmlNewDocNode (doc.get (), NULL, BAD_CAST "list", NULL);
xmlDocSetRootElement (doc.get (), root);
should_add_ekiga_net_book = true;
}
if (should_add_ekiga_net_book)
new_ekiga_net_book ();
}
OPENLDAP::Source::~Source ()
{
}
void
OPENLDAP::Source::add (xmlNodePtr node)
{
common_add (BookPtr(new Book (core, doc, node)));
}
void
OPENLDAP::Source::add (struct BookInfo bookinfo)
{
xmlNodePtr root;
root = xmlDocGetRootElement (doc.get ());
BookPtr book (new Book (core, doc, bookinfo));
xmlAddChild (root, book->get_node ());
common_add (book);
}
void
OPENLDAP::Source::common_add (BookPtr book)
{
book->trigger_saving.connect (boost::bind (&OPENLDAP::Source::save, this));
add_book (book);
save ();
}
bool
OPENLDAP::Source::populate_menu (Ekiga::MenuBuilder &builder)
{
builder.add_action ("add", _("Add an LDAP Address Book"),
boost::bind (&OPENLDAP::Source::new_book, this));
if (!has_ekiga_net_book ()) {
builder.add_action ("add", _("Add the Ekiga.net Directory"),
boost::bind (&OPENLDAP::Source::new_ekiga_net_book, this));
}
return true;
}
void
OPENLDAP::Source::new_book ()
{
boost::shared_ptr<Ekiga::FormRequestSimple> request = boost::shared_ptr<Ekiga::FormRequestSimple> (new Ekiga::FormRequestSimple (boost::bind (&OPENLDAP::Source::on_new_book_form_submitted, this, _1, _2)));
struct BookInfo bookinfo;
bookinfo.name = "";
bookinfo.uri = "ldap://localhost/dc=net?cn,telephoneNumber?sub?(cn=$)";
bookinfo.authcID = "";
bookinfo.password = "";
bookinfo.saslMech = "";
bookinfo.sasl = false;
bookinfo.starttls = false;
OPENLDAP::BookInfoParse (bookinfo);
OPENLDAP::BookForm (request, bookinfo, _("Create LDAP directory"));
questions (request);
}
void
OPENLDAP::Source::new_ekiga_net_book ()
{
struct BookInfo bookinfo;
bookinfo.name = _("Ekiga.net Directory");
bookinfo.uri =
"ldap://ekiga.net/dc=ekiga,dc=net?givenName,telephoneNumber?sub?(cn=$)";
bookinfo.authcID = "";
bookinfo.password = "";
bookinfo.saslMech = "";
bookinfo.sasl = false;
bookinfo.starttls = false;
add (bookinfo);
}
void
OPENLDAP::Source::on_new_book_form_submitted (bool submitted,
Ekiga::Form &result)
{
if (!submitted)
return;
std::string errmsg;
struct BookInfo bookinfo;
if (OPENLDAP::BookFormInfo (result, bookinfo, errmsg)) {
boost::shared_ptr<Ekiga::FormRequestSimple> request = boost::shared_ptr<Ekiga::FormRequestSimple> (new Ekiga::FormRequestSimple (boost::bind (&OPENLDAP::Source::on_new_book_form_submitted, this, _1, _2)));
result.visit (*request);
request->error (errmsg);
questions (request);
return;
}
add (bookinfo);
}
void
OPENLDAP::Source::save ()
{
xmlChar *buffer = NULL;
int size = 0;
xmlDocDumpMemory (doc.get (), &buffer, &size);
gm_conf_set_string (KEY, (const char *)buffer);
xmlFree (buffer);
}
bool
OPENLDAP::Source::has_ekiga_net_book () const
{
bool result = false;
for (const_iterator iter = begin ();
iter != end () && !result;
++iter)
result = (*iter)->is_ekiga_net_book ();
return result;
}
void
OPENLDAP::Source::migrate_from_3_0_0 ()
{
gboolean found = false;
xmlNodePtr root = xmlDocGetRootElement (doc.get ()); // can't be NULL
for (xmlNodePtr server = root->children ;
server != NULL && !found;
server = server->next) {
if (server->type == XML_ELEMENT_NODE
&& server->name != NULL
&& xmlStrEqual (BAD_CAST "server", server->name)) {
for (xmlNodePtr child = server->children;
child != NULL && !found;
child = child->next) {
if (child->type == XML_ELEMENT_NODE
&& child->name != NULL
&& xmlStrEqual (BAD_CAST "hostname", child->name)) {
// if it has a hostname, it's already an old config item
xmlChar* xml_str = xmlNodeGetContent (child);
if (xml_str != NULL) {
if (xmlStrEqual (BAD_CAST "ekiga.net", xml_str)) {
// ok, that's the one : let's get rid of it!
found = true;
xmlUnlinkNode (server);
xmlFreeNode (server);
}
xmlFree (xml_str);
}
}
}
}
}
// eh, we removed it, but we should add it back!
if (found)
should_add_ekiga_net_book = true;
}
|