~cairo-dock-team/ubuntu/quantal/cairo-dock-plug-ins/3.0.2

« back to all changes in this revision

Viewing changes to Indicator-applet/indicator-applet.c

Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* based on indicator-me.c written by :
 
6
*  Ted Gould <ted@canonical.com>
 
7
*  Cody Russell <cody.russell@canonical.com>
 
8
* E-mail    : see the 'copyright' file.
 
9
*
 
10
* This program is free software; you can redistribute it and/or
 
11
* modify it under the terms of the GNU General Public License
 
12
* as published by the Free Software Foundation; either version 3
 
13
* of the License, or (at your option) any later version.
 
14
*
 
15
* This program is distributed in the hope that it will be useful,
 
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
* GNU General Public License for more details.
 
19
* You should have received a copy of the GNU General Public License
 
20
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
*/
 
22
 
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
 
 
26
#include "indicator-applet.h"
 
27
 
 
28
#define INDICATOR_APPLET_DBUS_VERSION  1
 
29
 
 
30
 
 
31
static void _cd_indicator_make_menu (CDAppletIndicator *pIndicator)
 
32
{
 
33
        if (pIndicator->pMenu == NULL)
 
34
        {
 
35
                pIndicator->pMenu = dbusmenu_gtkmenu_new (pIndicator->cBusName, pIndicator->cMenuObject);
 
36
                if (pIndicator->pMenu != NULL)
 
37
                {
 
38
                        DbusmenuGtkClient * client = dbusmenu_gtkmenu_get_client (pIndicator->pMenu);
 
39
                        
 
40
                        if (pIndicator->add_menu_handler)
 
41
                                pIndicator->add_menu_handler (client);
 
42
                }
 
43
        }
 
44
}
 
45
 
 
46
static gboolean _get_menu_once (CDAppletIndicator *pIndicator)
 
47
{
 
48
        _cd_indicator_make_menu (pIndicator);
 
49
        pIndicator->iSidGetMenuOnce = 0;
 
50
        return FALSE;
 
51
}
 
52
 
 
53
static void
 
54
connection_changed (IndicatorServiceManager * sm, gboolean connected, CDAppletIndicator *pIndicator)
 
55
{
 
56
        CairoDockModuleInstance *myApplet = pIndicator->pApplet;
 
57
        if (connected)
 
58
        {
 
59
                // connect to the service.
 
60
                if (pIndicator->pServiceProxy == NULL)
 
61
                {
 
62
                        GError * error = NULL;
 
63
                        DBusGConnection * sbus = cairo_dock_get_session_connection ();
 
64
                        pIndicator->pServiceProxy = dbus_g_proxy_new_for_name_owner(sbus,
 
65
                                pIndicator->cBusName,
 
66
                                pIndicator->cServiceObject,
 
67
                                pIndicator->cServiceInterface,
 
68
                                &error);
 
69
                        if (error != NULL)
 
70
                        {
 
71
                                cd_warning ("'%s' service not found on the bus : %s", pIndicator->cServiceObject, error->message);
 
72
                                g_error_free(error);
 
73
                        }
 
74
                        if (pIndicator->pServiceProxy == NULL)
 
75
                                return;
 
76
                        
 
77
                        if (pIndicator->on_connect)
 
78
                                pIndicator->on_connect (myApplet);
 
79
                }
 
80
                
 
81
                // query the service to display initial values.
 
82
                if (pIndicator->get_initial_values)
 
83
                        pIndicator->get_initial_values (myApplet);
 
84
                
 
85
                pIndicator->iSidGetMenuOnce = g_idle_add ((GSourceFunc)_get_menu_once, pIndicator);
 
86
                pIndicator->bConnected = TRUE;
 
87
        }
 
88
        else  // If we're disconnecting, go back to offline.
 
89
        {
 
90
                if (pIndicator->on_disconnect)
 
91
                        pIndicator->on_disconnect (myApplet);
 
92
                pIndicator->bConnected = FALSE;
 
93
        }
 
94
 
 
95
        return;
 
96
}
 
97
 
 
98
CDAppletIndicator *cd_indicator_new (CairoDockModuleInstance *pApplet, const gchar *cBusName, const gchar *cServiceObject, const gchar *cServiceInterface, const gchar *cMenuObject)
 
99
{
 
100
        CDAppletIndicator *pIndicator = g_new0 (CDAppletIndicator, 1);
 
101
        pIndicator->pApplet = pApplet;
 
102
        pIndicator->cBusName = cBusName;
 
103
        pIndicator->cServiceObject = cServiceObject;
 
104
        pIndicator->cServiceInterface = cServiceInterface;
 
105
        pIndicator->cMenuObject = cMenuObject;
 
106
        
 
107
        pIndicator->service = indicator_service_manager_new_version (cBusName, INDICATOR_APPLET_DBUS_VERSION);
 
108
        g_signal_connect (G_OBJECT(pIndicator->service), INDICATOR_SERVICE_MANAGER_SIGNAL_CONNECTION_CHANGE, G_CALLBACK(connection_changed), pIndicator);  // on sera appele une fois la connexion etablie.
 
109
        
 
110
        return pIndicator;
 
111
}
 
112
 
 
113
 
 
114
void cd_indicator_destroy (CDAppletIndicator *pIndicator)
 
115
{
 
116
        if (!pIndicator)
 
117
                return;
 
118
        if (pIndicator->iSidGetMenuOnce != 0)
 
119
                g_source_remove (pIndicator->iSidGetMenuOnce);
 
120
        if (pIndicator->service)
 
121
                g_object_unref (pIndicator->service);
 
122
        if (pIndicator->pServiceProxy)
 
123
                g_object_unref (pIndicator->pServiceProxy);
 
124
        g_print ("destroy menu...\n");
 
125
        if (pIndicator->pMenu)
 
126
                g_object_ref_sink (pIndicator->pMenu);
 
127
        g_print ("done.\n");
 
128
        g_free (pIndicator);
 
129
}
 
130
 
 
131
 
 
132
void cd_indicator_set_icon (CDAppletIndicator *pIndicator, const gchar *cStatusIcon)
 
133
{
 
134
        CairoDockModuleInstance *myApplet = pIndicator->pApplet;
 
135
        if (cStatusIcon != pIndicator->cStatusIcon)
 
136
        {
 
137
                g_free (pIndicator->cStatusIcon);
 
138
                pIndicator->cStatusIcon = g_strdup (cStatusIcon);
 
139
        }
 
140
        if (cStatusIcon == NULL)
 
141
                return;
 
142
        
 
143
        gchar *cIconPath = cairo_dock_search_icon_s_path (cStatusIcon);
 
144
        gchar *cIconPathFallback = NULL;
 
145
        if (cIconPath == NULL)  // l'icone ne sera pas trouvee, on met une icone par defaut.
 
146
                cIconPathFallback = g_strdup_printf ("%s/%s.svg", myApplet->pModule->pVisitCard->cShareDataDir, cStatusIcon);
 
147
        
 
148
        g_print ("set %s\n", cIconPathFallback ? cIconPathFallback : cStatusIcon);
 
149
        CD_APPLET_SET_IMAGE_ON_MY_ICON (cIconPathFallback ? cIconPathFallback : cStatusIcon);
 
150
        
 
151
        g_free (cIconPath);
 
152
        g_free (cIconPathFallback);
 
153
}
 
154
 
 
155
 
 
156
void cd_indicator_reload_icon (CDAppletIndicator *pIndicator)
 
157
{
 
158
        cd_indicator_set_icon (pIndicator, pIndicator->cStatusIcon);
 
159
}
 
160
 
 
161
 
 
162
gboolean cd_indicator_show_menu (CDAppletIndicator *pIndicator)
 
163
{
 
164
        _cd_indicator_make_menu (pIndicator);
 
165
        if (pIndicator->pMenu != NULL)
 
166
        {
 
167
                cairo_dock_popup_menu_on_container (GTK_WIDGET (pIndicator->pMenu), myContainer);
 
168
                return TRUE;
 
169
        }
 
170
        return FALSE;
 
171
}