File: evolution-source.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 (279 lines) | stat: -rw-r--r-- 6,993 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

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


/*
 *                         evolution-source.cpp  -  description
 *                         ------------------------------------------
 *   begin                : written in 2007 by Julien Puydt
 *   copyright            : (c) 2007 by Julien Puydt
 *   description          : implementation of an evolution source
 *
 */

#include <iostream>

#include "evolution-source.h"

#if EDS_CHECK_VERSION(3,5,3)
#else
#define GCONF_PATH "/apps/evolution/addressbook/sources"
#endif

#if EDS_CHECK_VERSION(3,5,3)
static void
on_registry_source_added_c (ESourceRegistry */*registry*/,
                            ESource *source,
                            gpointer data)
{
  Evolution::Source *self = NULL;

  self = (Evolution::Source *)data;

  if (e_source_has_extension (source, E_SOURCE_EXTENSION_ADDRESS_BOOK))
    self->add_source (source);
}
#else
static void
on_source_list_group_added_c (ESourceList */*source_list*/,
			      ESourceGroup *group,
			      gpointer data)
{
  Evolution::Source *self = NULL;

  self = (Evolution::Source *)data;

  self->add_group (group);
}
#endif

#if EDS_CHECK_VERSION(3,5,3)
void
Evolution::Source::add_source (ESource *source)
{
  EBook *ebook = NULL;
  ebook = e_book_new (source, NULL);
  BookPtr book (new Evolution::Book (services, ebook));
  g_object_unref (ebook);
  add_book (book);
}
#else
void
Evolution::Source::add_group (ESourceGroup *group)
{
  GSList *sources = NULL;

  sources = e_source_group_peek_sources (group);

  for ( ; sources != NULL; sources = g_slist_next (sources)) {

    ESource *source = NULL;
    ESource *s = NULL;
    gchar *uri = NULL;
    EBook *ebook = NULL;

    source = E_SOURCE (sources->data);

    s = e_source_copy (source);

    uri = g_strdup_printf("%s/%s",
			  e_source_group_peek_base_uri (group),
			  e_source_peek_relative_uri (source));
    e_source_set_absolute_uri (s, uri);
    g_free (uri);

    ebook = e_book_new (s, NULL);
    g_object_unref (s);

    BookPtr book (new Evolution::Book (services, ebook));

    g_object_unref (ebook);

    add_book (book);
  }
}
#endif

#if EDS_CHECK_VERSION(3,5,3)
static void
on_registry_source_removed_c (ESourceRegistry */*registry*/,
                              ESource *source,
                              gpointer data)
{
  Evolution::Source *self = (Evolution::Source *)data;
  if (e_source_has_extension (source, E_SOURCE_EXTENSION_ADDRESS_BOOK))
    self->remove_source (source);
}
#else
static void
on_source_list_group_removed_c (ESourceList */*source_list*/,
				ESourceGroup *group,
				gpointer data)
{
  Evolution::Source *self = NULL;

  self = (Evolution::Source *)data;

  self->remove_group (group);
}
#endif

class remove_helper
{
public :

#if EDS_CHECK_VERSION(3,5,3)
  remove_helper (ESource* source_): source(source_)
#else
  remove_helper (ESourceGroup* group_): group(group_)
#endif
  { ready (); }

  inline void ready ()
  { found = false; }

  bool operator() (Ekiga::BookPtr book_)
  {
    Evolution::BookPtr book = boost::dynamic_pointer_cast<Evolution::Book> (book_);
    if (book) {

      EBook *book_ebook = book->get_ebook ();
      ESource *book_source = e_book_get_source (book_ebook);
#if EDS_CHECK_VERSION(3,5,3)
      if (e_source_equal (source, book_source)) {
#else
      ESourceGroup *book_group = e_source_peek_group (book_source);

      if (book_group == group) {
#endif
        book->removed ();
        found = true;
      }
    }

    return !found;
  }

  bool has_found () const
  { return found; }

private:
#if EDS_CHECK_VERSION(3,5,3)
  ESource* source;
#else
  ESourceGroup* group;
#endif
  bool found;
};

void
#if EDS_CHECK_VERSION(3,5,3)
Evolution::Source::remove_source (ESource *source)
{
  remove_helper helper (source);
#else
Evolution::Source::remove_group (ESourceGroup *group)
{
  remove_helper helper (group);
#endif

  do {

    helper.ready ();
    visit_books (boost::ref (helper));

  } while (helper.has_found ());
}

Evolution::Source::Source (Ekiga::ServiceCore &_services)
  : services(_services)
{
#if EDS_CHECK_VERSION(3,5,3)
  GList *list, *link;
  const gchar *extension_name;
  GError *error = NULL;

  /* XXX This blocks.  Should it be created asynchronously? */
  registry = e_source_registry_new_sync (NULL, &error);

  if (error != NULL) {
    g_warning ("%s", error->message);
    g_error_free (error);
    return;
  }

  extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
  list = e_source_registry_list_sources (registry, extension_name);

  for (link = list; link != NULL; link = g_list_next (link)) {
    ESource *source = E_SOURCE (link->data);
    add_source (source);
  }

  g_list_foreach (list, (GFunc) g_object_unref, NULL);
  g_list_free (list);

  g_signal_connect (registry, "source-added",
                    G_CALLBACK (on_registry_source_added_c), this);
  g_signal_connect (registry, "source-removed",
		    G_CALLBACK (on_registry_source_removed_c), this);
#else
  GSList *groups = NULL;
  ESourceGroup *group = NULL;

  source_list = e_source_list_new_for_gconf_default (GCONF_PATH);

  groups = e_source_list_peek_groups (source_list);

  for ( ; groups != NULL; groups = g_slist_next (groups)) {

    group = E_SOURCE_GROUP (groups->data);
    add_group (group);
  }

  g_signal_connect (source_list, "group-added",
		    G_CALLBACK (on_source_list_group_added_c), this);
  g_signal_connect (source_list, "group-removed",
		    G_CALLBACK (on_source_list_group_removed_c), this);
#endif
}

Evolution::Source::~Source ()
{
#if EDS_CHECK_VERSION(3,5,3)
  g_object_unref (registry);
#else
  g_object_unref (source_list);
#endif
}

bool
Evolution::Source::populate_menu (Ekiga::MenuBuilder &/*builder*/)
{
  /* FIXME: add back creating a new addressbook later */
  return false;
}