~jroose/cairo-dock-plug-ins/Messaging-Menu-alaric-devel

« back to all changes in this revision

Viewing changes to powermanager/src/powermanager-draw.c

  • Committer: jroose at gmail
  • Date: 2010-11-18 14:43:40 UTC
  • Revision ID: jroose@gmail.com-20101118144340-qvrs0rmanr5lr1mj
Messaging-Menu

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
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU 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, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include "string.h"
 
21
#include <glib/gi18n.h>
 
22
 
 
23
#include "powermanager-struct.h"
 
24
#include "powermanager-draw.h"
 
25
 
 
26
 
 
27
void update_icon(void)
 
28
{
 
29
        gboolean bNeedRedraw = FALSE;
 
30
        cd_message ("%s (time:%.2f -> %.2f ; charge:%.2f -> %.2f)", __func__, myData.previous_battery_time, myData.battery_time, myData.previous_battery_charge, myData.battery_charge);
 
31
        if(myData.battery_present)
 
32
        {
 
33
                // on reactualise le temps restant en info rapide.
 
34
                if (myData.previous_battery_time != myData.battery_time)
 
35
                {
 
36
                        if(myConfig.quickInfoType == POWER_MANAGER_TIME)
 
37
                        {
 
38
                                if (myData.battery_time != 0) {
 
39
                                        CD_APPLET_SET_HOURS_MINUTES_AS_QUICK_INFO (myData.battery_time);
 
40
                                }
 
41
                                else {
 
42
                                        CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("-:--");
 
43
                                }
 
44
                        }
 
45
                        else if(myConfig.quickInfoType == POWER_MANAGER_CHARGE)
 
46
                        {
 
47
                                CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%d%%", (int)myData.battery_charge);
 
48
                        }
 
49
                        else
 
50
                        {
 
51
                          CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
 
52
                        }
 
53
                        
 
54
                        bNeedRedraw = TRUE;
 
55
                        myData.previous_battery_time = myData.battery_time;
 
56
                }
 
57
                
 
58
                // on prend en compte la nouvelle charge.
 
59
                if (myData.previously_on_battery != myData.on_battery || myData.previous_battery_charge != myData.battery_charge)
 
60
                {
 
61
                        if (myData.previously_on_battery != myData.on_battery)
 
62
                        {
 
63
                                myData.previously_on_battery = myData.on_battery;
 
64
                                myData.alerted = FALSE;  //On a changé de statut, donc on réinitialise les alertes
 
65
                                myData.bCritical = FALSE;
 
66
                        }
 
67
                        
 
68
                        // on redessine l'icone.
 
69
                        if (myConfig.iDisplayType == CD_POWERMANAGER_GAUGE || myConfig.iDisplayType == CD_POWERMANAGER_GRAPH)
 
70
                        {
 
71
                                double fPercent = (double) myData.battery_charge / 100.;
 
72
                                CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (&fPercent);
 
73
                                bNeedRedraw = FALSE;
 
74
                        }
 
75
                        else if (myConfig.iDisplayType == CD_POWERMANAGER_ICONS)
 
76
                        {
 
77
                                cd_powermanager_draw_icon_with_effect (myData.on_battery);
 
78
                                bNeedRedraw = FALSE;
 
79
                        }
 
80
                        
 
81
                        // on declenche les alarmes.
 
82
                        if(myData.on_battery)
 
83
                        {
 
84
                                //Alert when battery charge is under a configured value in %
 
85
                                if (myData.battery_charge <= myConfig.lowBatteryValue && ! myData.alerted)
 
86
                                {
 
87
                                        cd_powermanager_alert(POWER_MANAGER_CHARGE_LOW);
 
88
                                        if (myConfig.cSoundPath[POWER_MANAGER_CHARGE_LOW] != NULL)
 
89
                                                cairo_dock_play_sound (myConfig.cSoundPath[POWER_MANAGER_CHARGE_LOW]);
 
90
                                }
 
91
                                //Alert when battery charge is under 4%
 
92
                                if (myData.battery_charge <= 4 && ! myData.bCritical)
 
93
                                {
 
94
                                        myData.bCritical = TRUE;
 
95
                                        cd_powermanager_alert (POWER_MANAGER_CHARGE_CRITICAL);
 
96
                                        if (myConfig.cSoundPath[POWER_MANAGER_CHARGE_CRITICAL] != NULL)
 
97
                                                cairo_dock_play_sound (myConfig.cSoundPath[POWER_MANAGER_CHARGE_CRITICAL]);
 
98
                                }
 
99
                                //Embleme sur notre icône
 
100
                                //CD_APPLET_DRAW_EMBLEM (CAIRO_DOCK_EMBLEM_BLANK, CAIRO_DOCK_EMBLEM_MIDDLE);
 
101
                        }
 
102
                        else
 
103
                        {
 
104
                                //Alert when battery is charged
 
105
                                if(myData.battery_charge == 100 && ! myData.alerted)
 
106
                                        cd_powermanager_alert (POWER_MANAGER_CHARGE_FULL);
 
107
                                        
 
108
                                //CD_APPLET_DRAW_EMBLEM (CAIRO_DOCK_EMBLEM_CHARGE, CAIRO_DOCK_EMBLEM_MIDDLE);
 
109
                                CD_APPLET_DRAW_EMBLEM_ON_MY_ICON (myData.pEmblem);
 
110
                        }
 
111
                        
 
112
                        myData.previously_on_battery = myData.on_battery;
 
113
                        myData.previous_battery_charge = myData.battery_charge;
 
114
                }
 
115
        }
 
116
        else if (myData.prev_battery_present)
 
117
        {
 
118
                CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("sector.svg");
 
119
                bNeedRedraw = TRUE;
 
120
                myData.prev_battery_present = FALSE;
 
121
        }
 
122
        
 
123
        if (bNeedRedraw)
 
124
                CD_APPLET_REDRAW_MY_ICON;
 
125
}
 
126
 
 
127
gchar *get_hours_minutes (int iTimeInSeconds)
 
128
{
 
129
        gchar *cTimeString;
 
130
        int h=0, m=0;
 
131
        m = iTimeInSeconds / 60;
 
132
        h = m / 60;
 
133
        m = m - (h * 60);
 
134
        if (h > 0) cTimeString = g_strdup_printf("%dh%02dm", h, m);
 
135
        else if (m > 0) cTimeString = g_strdup_printf("%dm", m);
 
136
        else cTimeString = g_strdup (D_("None"));
 
137
        
 
138
        return cTimeString;
 
139
}
 
140
 
 
141
static void _cd_powermanager_dialog (const gchar *cInfo, int iDuration)
 
142
{
 
143
        cairo_dock_remove_dialog_if_any (myIcon);
 
144
        
 
145
        const gchar *cIconPath;
 
146
        if (!myData.on_battery || !myData.battery_present)
 
147
                cIconPath = MY_APPLET_SHARE_DATA_DIR"/sector.svg";
 
148
        else
 
149
                cIconPath = MY_APPLET_SHARE_DATA_DIR"/default-battery.svg";
 
150
        
 
151
        cd_debug ("%s (%s)", cInfo, cIconPath);
 
152
        cairo_dock_show_temporary_dialog_with_icon (cInfo, myIcon, myContainer, 1000*iDuration, cIconPath);
 
153
}
 
154
 
 
155
void cd_powermanager_bubble (void)
 
156
{
 
157
        GString *sInfo = g_string_new ("");
 
158
        if(myData.battery_present)
 
159
        {
 
160
                gchar *hms = NULL;
 
161
                if (myData.battery_time > 0.)
 
162
                        hms = get_hours_minutes (myData.battery_time);
 
163
                else
 
164
                        hms = g_strdup_printf ("%s", D_("Unknown"));
 
165
                if(myData.on_battery)
 
166
                {
 
167
                        g_string_printf (sInfo, "%s %.2f%% \n %s %s", D_("Laptop on Battery.\n Battery charged at:"), myData.battery_charge, D_("Estimated time with charge:"), hms);
 
168
                }
 
169
                else
 
170
                {
 
171
                        g_string_printf (sInfo, "%s %.2f%% \n %s %s", D_("Laptop on Charge.\n Battery charged at:"), myData.battery_charge, D_("Estimated charge time:"), (myData.battery_charge > 99.9 ? "0" : hms));
 
172
                }
 
173
                g_free (hms);
 
174
        }
 
175
        else
 
176
        {
 
177
                g_string_printf (sInfo, "%s", D_("No battery found."));
 
178
        }
 
179
        
 
180
        _cd_powermanager_dialog (sInfo->str, 6);
 
181
        g_string_free (sInfo, TRUE);
 
182
}
 
183
 
 
184
gboolean cd_powermanager_alert (MyAppletCharge alert)
 
185
{
 
186
        cd_debug ("%s", __func__);
 
187
        GString *sInfo = g_string_new ("");
 
188
        
 
189
        gchar *hms = NULL;
 
190
        if (myData.battery_time > 0.)
 
191
                hms = get_hours_minutes (myData.battery_time);
 
192
        else
 
193
                hms = g_strdup (D_("Unknown"));
 
194
                
 
195
        if ((alert == POWER_MANAGER_CHARGE_LOW && myConfig.lowBatteryWitness) || (alert == POWER_MANAGER_CHARGE_CRITICAL && myConfig.criticalBatteryWitness))
 
196
        {
 
197
                if (myConfig.iNotificationType != 1)
 
198
                {
 
199
                        g_string_printf (sInfo, "%s (%.2f%%) \n %s %s \n %s", D_("PowerManager.\nBattery charge seems to be low"), myData.battery_charge, D_("Estimated time with charge:"), hms, D_("Please put your laptop on charge."));
 
200
                        _cd_powermanager_dialog (sInfo->str, myConfig.iNotificationDuration);
 
201
                }
 
202
                if (myConfig.iNotificationType != 2)
 
203
                {
 
204
                        CD_APPLET_DEMANDS_ATTENTION (myConfig.cNotificationAnimation, myConfig.iNotificationDuration);
 
205
                }
 
206
                if (myConfig.cSoundPath[alert] != NULL)
 
207
                        cairo_dock_play_sound (myConfig.cSoundPath[alert]);
 
208
        }
 
209
        else if (alert == POWER_MANAGER_CHARGE_FULL && myConfig.highBatteryWitness)
 
210
        {
 
211
                if (myConfig.iNotificationType != 1)
 
212
                {
 
213
                        g_string_printf (sInfo, "%s (%.2f%%)", D_("PowerManager.\nYour battery is now charged"), myData.battery_charge);
 
214
                        _cd_powermanager_dialog (sInfo->str, myConfig.iNotificationDuration);
 
215
                }
 
216
                if (myConfig.iNotificationType != 2)
 
217
                {
 
218
                        CD_APPLET_DEMANDS_ATTENTION (myConfig.cNotificationAnimation, myConfig.iNotificationDuration);
 
219
                }
 
220
                if (myConfig.cSoundPath[alert] != NULL)
 
221
                        cairo_dock_play_sound (myConfig.cSoundPath[alert]);
 
222
        }
 
223
        
 
224
        g_free (hms);
 
225
        g_string_free (sInfo, TRUE);
 
226
        myData.alerted = TRUE;
 
227
        return FALSE;
 
228
}
 
229
 
 
230
 
 
231
void cd_powermanager_draw_icon_with_effect (gboolean bOnBattery)
 
232
{
 
233
        if (bOnBattery && myData.pSurfaceBattery == NULL)
 
234
        {
 
235
                gchar *cImagePath;
 
236
                if (myConfig.cUserBatteryIconName == NULL)
 
237
                        cImagePath = g_strdup (MY_APPLET_SHARE_DATA_DIR"/default-battery.svg");
 
238
                else
 
239
                        cImagePath = cairo_dock_generate_file_path (myConfig.cUserBatteryIconName);
 
240
                
 
241
                myData.pSurfaceBattery = CD_APPLET_LOAD_SURFACE_FOR_MY_APPLET (cImagePath);
 
242
                g_free (cImagePath);
 
243
        }
 
244
        else if (! bOnBattery && myData.pSurfaceCharge == NULL)
 
245
        {
 
246
                gchar *cImagePath;
 
247
                if (myConfig.cUserChargeIconName == NULL)
 
248
                        cImagePath = g_strdup (MY_APPLET_SHARE_DATA_DIR"/default-charge.svg");
 
249
                else
 
250
                        cImagePath = cairo_dock_generate_file_path (myConfig.cUserChargeIconName);
 
251
                
 
252
                myData.pSurfaceCharge = CD_APPLET_LOAD_SURFACE_FOR_MY_APPLET (cImagePath);
 
253
                g_free (cImagePath);
 
254
        }
 
255
        
 
256
        cairo_surface_t *pSurface = (bOnBattery ? myData.pSurfaceBattery : myData.pSurfaceCharge);
 
257
        
 
258
        switch (myConfig.iEffect)
 
259
        {
 
260
                case POWER_MANAGER_EFFECT_NONE :
 
261
                        CD_APPLET_SET_SURFACE_ON_MY_ICON (pSurface);
 
262
                break;
 
263
                case POWER_MANAGER_EFFECT_ZOOM :
 
264
                        cairo_save (myDrawContext);
 
265
                        double fScale = .3 + .7 * myData.battery_charge / 100.;
 
266
                        CD_APPLET_SET_SURFACE_ON_MY_ICON_WITH_ZOOM (pSurface, fScale);
 
267
                        cairo_restore (myDrawContext);
 
268
                break;
 
269
                case POWER_MANAGER_EFFECT_TRANSPARENCY :
 
270
                        cairo_save (myDrawContext);
 
271
                        double fAlpha = .3 + .7 * myData.battery_charge / 100.;
 
272
                        CD_APPLET_SET_SURFACE_ON_MY_ICON_WITH_ALPHA (pSurface, fAlpha);
 
273
                        cairo_restore (myDrawContext);
 
274
                break;
 
275
                case POWER_MANAGER_EFFECT_BAR :
 
276
                        cairo_save (myDrawContext);
 
277
                        CD_APPLET_SET_SURFACE_ON_MY_ICON_WITH_BAR (pSurface, myData.battery_charge * .01);
 
278
                        cairo_restore (myDrawContext);
 
279
                break;
 
280
                default :
 
281
                break;
 
282
        }
 
283
}
 
284
 
 
285
 
 
286
void cd_powermanager_format_value (CairoDataRenderer *pRenderer, int iNumValue, gchar *cFormatBuffer, int iBufferLength, CairoDockModuleInstance *myApplet)
 
287
{
 
288
        double fValue = cairo_data_renderer_get_normalized_current_value_with_latency (pRenderer, iNumValue);
 
289
        if(myConfig.quickInfoType == POWER_MANAGER_TIME)
 
290
        {
 
291
                if (myData.battery_time != 0)
 
292
                {
 
293
                        int time = myData.battery_time;
 
294
                        int hours = time / 3600;
 
295
                        int minutes = (time % 3600) / 60;
 
296
                        if (hours != 0)
 
297
                                snprintf (cFormatBuffer, iBufferLength, "%dh%02d", hours, abs (minutes));
 
298
                        else
 
299
                                snprintf (cFormatBuffer, iBufferLength, "%dmn", minutes);
 
300
                }
 
301
                else
 
302
                {
 
303
                        strncpy (cFormatBuffer, "-:--", iBufferLength);
 
304
                }
 
305
        }
 
306
        else if(myConfig.quickInfoType == POWER_MANAGER_CHARGE)
 
307
        {
 
308
                snprintf (cFormatBuffer, iBufferLength, "%d%%", (int)myData.battery_charge);
 
309
        }
 
310
        else
 
311
                cFormatBuffer[0] = '\0';
 
312
}