Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / pidgin / plugins / gevolution / gevo-util.c
blobb6c71bdec876667a29da241e245fe5e35f71c41a
1 /*
2 * Evolution integration plugin for Purple
4 * Copyright (C) 2003 Christian Hammond.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02111-1301, USA.
21 #include "internal.h"
22 #include "gtkblist.h"
23 #include "pidgin.h"
24 #include "gtkutils.h"
26 #include "gevolution.h"
28 void
29 gevo_add_buddy(PurpleAccount *account, const char *group_name,
30 const char *buddy_name, const char *alias)
32 PurpleIMConversation *im = NULL;
33 PurpleBuddy *buddy;
34 PurpleGroup *group;
36 im = purple_conversations_find_im_with_account(buddy_name, account);
38 group = purple_blist_find_group(group_name);
39 if (group == NULL)
41 group = purple_group_new(group_name);
42 purple_blist_add_group(group, NULL);
45 buddy = purple_blist_find_buddy_in_group(account, buddy_name, group);
46 if (buddy == NULL)
48 buddy = purple_buddy_new(account, buddy_name, alias);
49 purple_blist_add_buddy(buddy, NULL, group, NULL);
52 purple_account_add_buddy(account, buddy, NULL);
54 if (im != NULL)
56 purple_buddy_icon_update(purple_im_conversation_get_icon(im));
57 purple_conversation_update(PURPLE_CONVERSATION(im),
58 PURPLE_CONVERSATION_UPDATE_ADD);
62 GList *
63 gevo_get_groups(void)
65 static GList *list = NULL;
66 PurpleGroup *g;
67 PurpleBlistNode *gnode;
69 g_list_free(list);
70 list = NULL;
72 if (purple_blist_get_buddy_list()->root == NULL) {
73 list = g_list_append(list,
74 (gpointer)PURPLE_BLIST_DEFAULT_GROUP_NAME);
75 } else {
76 for (gnode = purple_blist_get_buddy_list()->root;
77 gnode != NULL;
78 gnode = gnode->next)
80 if (PURPLE_IS_GROUP(gnode))
82 g = PURPLE_GROUP(gnode);
83 list = g_list_append(list,
84 (gpointer)purple_group_get_name(g));
89 return list;
92 EContactField
93 gevo_protocol_get_field(PurpleAccount *account, PurpleBuddy *buddy)
95 EContactField protocol_field = 0;
96 const char *protocol_id;
98 g_return_val_if_fail(account != NULL, 0);
100 protocol_id = purple_account_get_protocol_id(account);
102 if (!strcmp(protocol_id, "prpl-aim"))
103 protocol_field = E_CONTACT_IM_AIM;
104 else if (!strcmp(protocol_id, "prpl-icq"))
105 protocol_field = E_CONTACT_IM_ICQ;
106 else if (!strcmp(protocol_id, "prpl-msn"))
107 protocol_field = E_CONTACT_IM_MSN;
108 else if (!strcmp(protocol_id, "prpl-yahoo"))
109 protocol_field = E_CONTACT_IM_YAHOO;
110 else if (!strcmp(protocol_id, "prpl-jabber"))
111 protocol_field = E_CONTACT_IM_JABBER;
112 else if (!strcmp(protocol_id, "prpl-novell"))
113 protocol_field = E_CONTACT_IM_GROUPWISE;
114 else if (!strcmp(protocol_id, "prpl-gg"))
115 protocol_field = E_CONTACT_IM_GADUGADU;
117 return protocol_field;
120 gboolean
121 gevo_protocol_is_supported(PurpleAccount *account, PurpleBuddy *buddy)
123 return (gevo_protocol_get_field(account, buddy) != 0);
126 gboolean
127 gevo_load_addressbook(const gchar* uid, EBook **book, GError **error)
129 gboolean result = FALSE;
130 ESourceRegistry *registry;
131 ESource *source;
133 g_return_val_if_fail(book != NULL, FALSE);
135 registry = e_source_registry_new_sync(NULL, error);
137 if (!registry)
138 return FALSE;
140 if (uid == NULL)
141 source = e_source_registry_ref_default_address_book(registry);
142 else
143 source = e_source_registry_ref_source(registry, uid);
145 g_object_unref(registry);
147 result = gevo_load_addressbook_from_source(source, book, error);
149 g_object_unref(source);
151 return result;
154 gboolean
155 gevo_load_addressbook_from_source(ESource *source, EBook **book, GError **error)
157 gboolean result = FALSE;
159 *book = e_book_new(source, error);
161 if (*book == NULL)
162 return FALSE;
164 *error = NULL;
166 result = e_book_open(*book, FALSE, error);
168 if (!result && *book != NULL)
170 g_object_unref(*book);
171 *book = NULL;
174 return result;
177 char *
178 gevo_get_email_for_buddy(PurpleBuddy *buddy)
180 EContact *contact;
181 char *mail = NULL;
183 contact = gevo_search_buddy_in_contacts(buddy, NULL);
185 if (contact != NULL)
187 mail = g_strdup(e_contact_get(contact, E_CONTACT_EMAIL_1));
188 g_object_unref(contact);
191 if (mail == NULL)
193 PurpleAccount *account = purple_buddy_get_account(buddy);
194 const char *protocol_id = purple_account_get_protocol_id(account);
196 if (!strcmp(protocol_id, "prpl-msn"))
198 mail = g_strdup(purple_normalize(account,
199 purple_buddy_get_name(buddy)));
201 else if (!strcmp(protocol_id, "prpl-yahoo"))
203 mail = g_strdup_printf("%s@yahoo.com",
204 purple_normalize(account,
205 purple_buddy_get_name(buddy)));
209 return mail;