File: rl-presentity.cpp

package info (click to toggle)
ekiga 4.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 31,612 kB
  • ctags: 14,511
  • sloc: cpp: 72,155; sh: 11,298; xml: 8,904; ansic: 8,572; makefile: 1,738; asm: 453; awk: 16
file content (359 lines) | stat: -rw-r--r-- 9,193 bytes parent folder | download | duplicates (2)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

/*
 * 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.
 */


/*
 *                         rl-presentity.cpp  -  description
 *                         ------------------------------------------
 *   begin                : written in 2008 by Julien Puydt
 *   copyright            : (c) 2008 by Julien Puydt
 *   description          : implementation of a presentity in a resource-list
 *
 */

#include <algorithm>
#include <set>

#include <glib/gi18n.h>

#include "form-request-simple.h"
#include "robust-xml.h"
#include "xcap-core.h"

#include "rl-presentity.h"

/* at one point we will return a smart pointer on this... and if we don't use
 * a false smart pointer, we will crash : the reference count isn't embedded!
 */
struct null_deleter
{
    void operator()(void const *) const
    {
    }
};


RL::Presentity::Presentity (Ekiga::ServiceCore &services_,
			    boost::shared_ptr<XCAP::Path> path_,
			    boost::shared_ptr<xmlDoc> doc_,
			    xmlNodePtr node_,
			    bool writable_) :
  services(services_), doc(doc_), node(node_), writable(writable_),
  name_node(NULL), presence("unknown"), status("")
{
  boost::shared_ptr<Ekiga::PresenceCore> presence_core(services.get<Ekiga::PresenceCore> ("presence-core"));
  xmlChar *xml_str = NULL;
  xmlNsPtr ns = xmlSearchNsByHref (doc.get (), node,
                                   BAD_CAST "http://www.ekiga.org");

  if (ns == NULL) {

    // FIXME: we should handle the case, even if it shouldn't happen
  }

  xml_str = xmlGetProp (node, BAD_CAST "uri");
  if (xml_str != NULL) {

    uri = (const char *)xml_str;
    path = path_->build_child_with_attribute ("entry", "uri", uri);
    xmlFree (xml_str);
  } else {

    // FIXME: we should handle the case, even if it shouldn't happen

  }

  for (xmlNodePtr child = node->children ;
       child != NULL ;
       child = child->next) {

    if (child->type == XML_ELEMENT_NODE
        && child->name != NULL) {

      if (xmlStrEqual (BAD_CAST ("display-name"), child->name)) {

        name_node = child;
        continue;
      }
      else if (xmlStrEqual (BAD_CAST ("group"), child->name)
               && child->ns == ns) {

        xml_str = xmlNodeGetContent (child);
        if (xml_str != NULL)
          group_nodes[(const char *)xml_str] = child;
        else
          group_nodes[""] = child;
        xmlFree (xml_str);
        continue;
      }
    }
  }

  for (std::map<std::string, xmlNodePtr>::const_iterator iter
         = group_nodes.begin ();
       iter != group_nodes.end ();
       iter++)
    groups.insert (iter->first);

  presence_core->fetch_presence (uri);
}

RL::Presentity::~Presentity ()
{
}


const std::string
RL::Presentity::get_name () const
{
  std::string result;

  if (name_node != NULL) {

    xmlChar* str = xmlNodeGetContent (name_node);
    if (str != NULL) {

      result = ((const char*)str);
      xmlFree (str);
    }
  } else {

    result = _("Unnamed");
  }

  return result;
}


const std::string
RL::Presentity::get_presence () const
{
  return presence;
}


const std::string
RL::Presentity::get_status () const
{
  return status;
}


const std::set<std::string>
RL::Presentity::get_groups () const
{
  return groups;
}


const std::string
RL::Presentity::get_uri () const
{
  return uri;
}


void
RL::Presentity::set_presence (const std::string _presence)
{
  presence = _presence;
  updated ();
}

void
RL::Presentity::set_status (const std::string _status)
{
  status = _status;
  updated ();
}


bool
RL::Presentity::has_uri (const std::string _uri) const
{
  return _uri == get_uri ();
}

bool
RL::Presentity::populate_menu (Ekiga::MenuBuilder &builder)
{
  bool populated = false;
  boost::shared_ptr<Ekiga::PresenceCore> presence_core(services.get<Ekiga::PresenceCore> ("presence-core"));

  populated = presence_core->populate_presentity_menu (PresentityPtr (this, null_deleter ()), uri, builder);

  if (writable) {

    if (populated)
      builder.add_separator ();

    builder.add_action ("edit", _("_Edit"),
			boost::bind (&RL::Presentity::edit_presentity, this));
    builder.add_action ("remove", _("_Remove"),
			boost::bind (&RL::Presentity::remove, this));
  }

  return true;
}


void
RL::Presentity::edit_presentity ()
{
  boost::shared_ptr<Ekiga::FormRequestSimple> request = boost::shared_ptr<Ekiga::FormRequestSimple> (new Ekiga::FormRequestSimple (boost::bind (&RL::Presentity::edit_presentity_form_submitted, this, _1, _2)));

  // FIXME: we should be able to know all groups in the heap
  std::set<std::string> all_groups = groups;

  request->title (_("Edit remote contact"));
  request->instructions (_("Please fill in this form to change an existing "
                           "contact on a remote server"));
  request->text ("name", _("Name:"), get_name (), std::string ());
  request->text ("uri", _("Address:"), uri, std::string ());

  request->editable_set ("groups", _("Choose groups:"),
                         groups, all_groups);

  questions (request);
}


void
RL::Presentity::edit_presentity_form_submitted (bool submitted,
						Ekiga::Form &result)
{
  if (!submitted)
    return;

  const std::string new_name = result.text ("name");
  const std::string new_uri = result.text ("uri");
  const std::set<std::string> new_groups = result.editable_set ("groups");
  std::map<std::string, xmlNodePtr> future_group_nodes;
  xmlNsPtr ns = xmlSearchNsByHref (node->doc, node,
                                   BAD_CAST "http://www.ekiga.org");
  bool reload = false;

  robust_xmlNodeSetContent (node, &name_node, "name", new_name);

  if (uri != new_uri) {

    xmlSetProp (node, (const xmlChar*)"uri", (const xmlChar*)uri.c_str ());
    boost::shared_ptr<Ekiga::PresenceCore> presence_core(services.get<Ekiga::PresenceCore> ("presence-core"));
    presence_core->unfetch_presence (uri);
    reload = true;
  }

  for (std::map<std::string, xmlNodePtr>::const_iterator iter
         = group_nodes.begin ();
       iter != group_nodes.end () ;
       iter++) {

    if (new_groups.find (iter->first) == new_groups.end ()) {

      xmlUnlinkNode (iter->second);
      xmlFreeNode (iter->second);
    }
    else {
      future_group_nodes[iter->first] = iter->second;
    }
  }

  for (std::set<std::string>::const_iterator iter = new_groups.begin ();
       iter != new_groups.end ();
       iter++) {

    if (std::find (groups.begin (), groups.end (), *iter) == groups.end ())
      future_group_nodes[*iter] = xmlNewChild (node, ns,
					       BAD_CAST "group",
					       BAD_CAST robust_xmlEscape (node->doc, *iter).c_str ());
  }

  group_nodes = future_group_nodes;
  groups = new_groups;

  save (reload);
}

void
RL::Presentity::save (bool reload)
{
  xmlBufferPtr buffer = xmlBufferCreate ();
  int result = xmlNodeDump (buffer, node->doc, node, 0, 0);

  if (result >= 0) {

    boost::shared_ptr<XCAP::Core> xcap = services.get<XCAP::Core> ("xcap-core");
    xcap->write (path, "application/xcap-el+xml",
                 (const char*)xmlBufferContent (buffer),
                 boost::bind (&RL::Presentity::save_result, this, _1, reload));
  }

  xmlBufferFree (buffer);

}

void
RL::Presentity::remove ()
{
  xmlUnlinkNode (node);
  xmlFreeNode (node);
  boost::shared_ptr<Ekiga::PresenceCore> presence_core = services.get<Ekiga::PresenceCore> ("presence-core");

  presence_core->unfetch_presence (uri);

  boost::shared_ptr<XCAP::Core> xcap = services.get<XCAP::Core> ("xcap-core");
  xcap->erase (path,
               boost::bind (&RL::Presentity::erase_result, this, _1));
}

void
RL::Presentity::save_result (std::string error,
			     bool reload)
{
  if ( !error.empty ()) {

    // FIXME: do better
    std::cout << "XCAP error: " << error << std::endl;
    trigger_reload ();
  } else {

    if (reload)
      trigger_reload ();
    else
      updated ();
  }
}

void
RL::Presentity::erase_result (std::string error)
{
  if ( !error.empty ()) {

    // FIXME: do better
    std::cout << "XCAP error: " << error << std::endl;
  }

  trigger_reload ();
}