]> git.0d.be Git - empathy.git/blob - libempathy/empathy-session.c
1a46a9cd275b98dec7115b2c435f5adfdec04df1
[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 Collabora Ltd.
4  *
5  * This program 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 program 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 program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <glib.h>
26
27 #include <libtelepathy/tp-helpers.h>
28
29 #include <libmissioncontrol/mc-account-monitor.h>
30
31 #include "empathy-session.h"
32 #include "gossip-debug.h"
33
34 #define DEBUG_DOMAIN "Session"
35
36 static void session_start_mission_control (void);
37 static void session_error_cb              (MissionControl   *mc,
38                                            GError           *error,
39                                            gpointer          data);
40 static void session_account_enabled_cb    (McAccountMonitor *monitor,
41                                            gchar            *unique_name,
42                                            gpointer          user_data);
43 static void session_service_ended_cb      (MissionControl   *mc,
44                                            gpointer          user_data);
45
46 static MissionControl        *mission_control = NULL;
47 static EmpathyContactManager *contact_manager = NULL;
48
49 void
50 empathy_session_connect (void)
51 {
52         MissionControl   *mc;
53         McAccountMonitor *monitor;
54         static gboolean   started = FALSE;
55
56         if (started) {
57                 return;
58         }
59
60         mc = empathy_session_get_mission_control ();
61         monitor = mc_account_monitor_new ();
62
63         g_signal_connect (monitor, "account-enabled",
64                           G_CALLBACK (session_account_enabled_cb),
65                           NULL);
66         g_signal_connect (mc, "ServiceEnded",
67                           G_CALLBACK (session_service_ended_cb),
68                           NULL);
69
70         g_object_unref (monitor);
71         session_start_mission_control ();
72
73         started = TRUE;
74 }
75
76 void
77 empathy_session_finalize (void)
78 {
79         if (mission_control) {
80                 g_object_unref (mission_control);
81                 mission_control = NULL;
82         }
83
84         if (contact_manager) {
85                 g_object_unref (contact_manager);
86                 contact_manager = NULL;
87         }
88 }
89
90 MissionControl *
91 empathy_session_get_mission_control (void)
92 {
93         if (!mission_control) {
94                 mission_control = mission_control_new (tp_get_bus ());
95         }
96
97         return mission_control;
98 }
99
100 EmpathyContactManager *
101 empathy_session_get_contact_manager (void)
102 {
103         if (!contact_manager) {
104                 contact_manager = empathy_contact_manager_new ();
105         }
106
107         return contact_manager;
108 }
109
110 static void
111 session_start_mission_control (void)
112 {
113         MissionControl *mc;
114         McPresence      presence;
115
116         mc = empathy_session_get_mission_control ();
117         presence = mission_control_get_presence_actual (mc, NULL);
118
119         if (presence != MC_PRESENCE_UNSET &&
120             presence != MC_PRESENCE_OFFLINE) {
121                 /* MC is already running and online, nothing to do */
122                 return;
123         }
124
125         gossip_debug (DEBUG_DOMAIN, "Starting Mission Control...");
126
127         /* FIXME: Save/Restore status message */
128         mission_control_set_presence (mc, MC_PRESENCE_AVAILABLE,
129                                       NULL,
130                                       (McCallback) session_error_cb,
131                                       NULL);
132
133         mission_control_connect_all_with_default_presence (mc,
134                                                            (McCallback) session_error_cb,
135                                                            NULL);
136 }
137
138 static void
139 session_error_cb (MissionControl *mc,
140                   GError         *error,
141                   gpointer        data)
142 {
143         if (error) {
144                 gossip_debug (DEBUG_DOMAIN, "Error: %s", error->message);
145         }
146 }
147
148 static void
149 session_account_enabled_cb (McAccountMonitor *monitor,
150                             gchar            *unique_name,
151                             gpointer          user_data)
152 {
153         gossip_debug (DEBUG_DOMAIN, "Account enabled: %s", unique_name);
154         session_start_mission_control ();
155 }
156
157 static void
158 session_service_ended_cb (MissionControl *mc,
159                           gpointer        user_data)
160 {
161         gossip_debug (DEBUG_DOMAIN, "Mission Control stopped");
162 }
163