]> git.0d.be Git - empathy.git/blob - libempathy/empathy-session.c
[darcs-to-svn @ initial import]
[empathy.git] / libempathy / empathy-session.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Xavier Claessens <xclaesse@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include <glib.h>
24
25 #include <libtelepathy/tp-helpers.h>
26
27 #include <libmissioncontrol/mc-account-monitor.h>
28
29 #include "empathy-session.h"
30 #include "gossip-debug.h"
31
32 #define DEBUG_DOMAIN "Session"
33
34 static void session_start_mission_control (void);
35 static void session_error_cb              (MissionControl   *mc,
36                                            GError           *error,
37                                            gpointer          data);
38 static void session_account_enabled_cb    (McAccountMonitor *monitor,
39                                            gchar            *unique_name,
40                                            gpointer          user_data);
41 static void session_service_ended_cb      (MissionControl   *mc,
42                                            gpointer          user_data);
43
44 static MissionControl        *mission_control = NULL;
45 static EmpathyContactManager *contact_manager = NULL;
46
47 void
48 empathy_session_connect (void)
49 {
50         MissionControl   *mc;
51         McAccountMonitor *monitor;
52         static gboolean   started = FALSE;
53
54         if (started) {
55                 return;
56         }
57
58         mc = empathy_session_get_mission_control ();
59         monitor = mc_account_monitor_new ();
60
61         g_signal_connect (monitor, "account-enabled",
62                           G_CALLBACK (session_account_enabled_cb),
63                           NULL);
64         g_signal_connect (mc, "ServiceEnded",
65                           G_CALLBACK (session_service_ended_cb),
66                           NULL);
67
68         g_object_unref (monitor);
69         session_start_mission_control ();
70
71         started = TRUE;
72 }
73
74 void
75 empathy_session_finalize (void)
76 {
77         if (mission_control) {
78                 g_object_unref (mission_control);
79                 mission_control = NULL;
80         }
81
82         if (contact_manager) {
83                 g_object_unref (contact_manager);
84                 contact_manager = NULL;
85         }
86 }
87
88 MissionControl *
89 empathy_session_get_mission_control (void)
90 {
91         if (!mission_control) {
92                 mission_control = mission_control_new (tp_get_bus ());
93         }
94
95         return mission_control;
96 }
97
98 EmpathyContactManager *
99 empathy_session_get_contact_manager (void)
100 {
101         if (!contact_manager) {
102                 contact_manager = empathy_contact_manager_new ();
103         }
104
105         return contact_manager;
106 }
107
108 static void
109 session_start_mission_control (void)
110 {
111         MissionControl *mc;
112         McPresence      presence;
113
114         mc = empathy_session_get_mission_control ();
115         presence = mission_control_get_presence_actual (mc, NULL);
116
117         if (presence != MC_PRESENCE_UNSET &&
118             presence != MC_PRESENCE_OFFLINE) {
119                 /* MC is already running and online, nothing to do */
120                 return;
121         }
122
123         gossip_debug (DEBUG_DOMAIN, "Starting Mission Control...");
124
125         /* FIXME: Save/Restore status message */
126         mission_control_set_presence (mc, MC_PRESENCE_AVAILABLE,
127                                       NULL,
128                                       (McCallback) session_error_cb,
129                                       NULL);
130
131         mission_control_connect_all_with_default_presence (mc,
132                                                            (McCallback) session_error_cb,
133                                                            NULL);
134 }
135
136 static void
137 session_error_cb (MissionControl *mc,
138                   GError         *error,
139                   gpointer        data)
140 {
141         if (error) {
142                 gossip_debug (DEBUG_DOMAIN, "Error: %s", error->message);
143         }
144 }
145
146 static void
147 session_account_enabled_cb (McAccountMonitor *monitor,
148                             gchar            *unique_name,
149                             gpointer          user_data)
150 {
151         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
152         session_start_mission_control ();
153 }
154
155 static void
156 session_service_ended_cb (MissionControl *mc,
157                           gpointer        user_data)
158 {
159         gossip_debug (DEBUG_DOMAIN, "Mission Control stopped");
160 }
161