]> git.0d.be Git - empathy.git/blob - src/empathy-auto-salut-account-helper.c
Merge branch 'sasl'
[empathy.git] / src / empathy-auto-salut-account-helper.c
1 /*
2  * Copyright (C) 2007-2010 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <glib.h>
25 #include <glib/gi18n-lib.h>
26
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/util.h>
29
30 #if HAVE_EDS
31 #include <libebook/e-book.h>
32 #endif
33
34 #include <libempathy/empathy-account-settings.h>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
37 #include <libempathy/empathy-debug.h>
38
39 #include "empathy-auto-salut-account-helper.h"
40
41 /* Salut account creation. The TpAccountManager first argument
42  * must already be prepared when calling this function. */
43 gboolean
44 should_create_salut_account (TpAccountManager *manager)
45 {
46   gboolean salut_created = FALSE;
47   GList *accounts, *l;
48
49   accounts = tp_account_manager_get_valid_accounts (manager);
50
51   for (l = accounts; l != NULL;  l = g_list_next (l))
52     {
53       TpAccount *account = TP_ACCOUNT (l->data);
54
55       if (!tp_strdiff (tp_account_get_protocol (account), "local-xmpp"))
56         {
57           salut_created = TRUE;
58           break;
59         }
60     }
61
62   g_list_free (accounts);
63
64   return !salut_created;
65 }
66
67 EmpathyAccountSettings *
68 create_salut_account_settings (void)
69 {
70   EmpathyAccountSettings  *settings;
71 #if HAVE_EDS
72   EBook *book;
73   EContact *contact;
74   gchar *nickname = NULL;
75   gchar *first_name = NULL;
76   gchar *last_name = NULL;
77   gchar *email = NULL;
78   gchar *jid = NULL;
79   GError *error = NULL;
80 #endif
81
82   settings = empathy_account_settings_new ("salut", "local-xmpp", NULL,
83       _("People nearby"));
84
85 #if HAVE_EDS
86   /* Get self EContact from EDS */
87   if (!e_book_get_self (&contact, &book, &error))
88     {
89       DEBUG ("Failed to get self econtact: %s", error->message);
90       g_error_free (error);
91       return settings;
92     }
93
94   nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
95   first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
96   last_name = e_contact_get (contact, E_CONTACT_FAMILY_NAME);
97   email = e_contact_get (contact, E_CONTACT_EMAIL_1);
98   jid = e_contact_get (contact, E_CONTACT_IM_JABBER_HOME_1);
99
100   if (!tp_strdiff (nickname, "nickname"))
101     {
102       g_free (nickname);
103       nickname = NULL;
104     }
105
106   DEBUG ("Salut account created:\nnickname=%s\nfirst-name=%s\n"
107      "last-name=%s\nemail=%s\njid=%s\n",
108      nickname, first_name, last_name, email, jid);
109
110   empathy_account_settings_set_string (settings,
111       "nickname", nickname ? nickname : "");
112   empathy_account_settings_set_string (settings,
113       "first-name", first_name ? first_name : "");
114   empathy_account_settings_set_string (settings,
115       "last-name", last_name ? last_name : "");
116   empathy_account_settings_set_string (settings, "email", email ? email : "");
117   empathy_account_settings_set_string (settings, "jid", jid ? jid : "");
118
119   g_free (nickname);
120   g_free (first_name);
121   g_free (last_name);
122   g_free (email);
123   g_free (jid);
124   g_object_unref (contact);
125   g_object_unref (book);
126 #endif
127
128   return settings;
129 }