2
* This file is a part of the Cairo-Dock project
4
* Copyright : (C) see the 'copyright' file.
5
* E-mail : see the 'copyright' file.
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.
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/>.
21
#include <glib/gi18n.h>
23
#include "powermanager-struct.h"
24
#include "powermanager-draw.h"
27
void update_icon(void)
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)
33
// on reactualise le temps restant en info rapide.
34
if (myData.previous_battery_time != myData.battery_time)
36
if(myConfig.quickInfoType == POWER_MANAGER_TIME)
38
if (myData.battery_time != 0) {
39
CD_APPLET_SET_HOURS_MINUTES_AS_QUICK_INFO (myData.battery_time);
42
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("-:--");
45
else if(myConfig.quickInfoType == POWER_MANAGER_CHARGE)
47
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%d%%", (int)myData.battery_charge);
51
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
55
myData.previous_battery_time = myData.battery_time;
58
// on prend en compte la nouvelle charge.
59
if (myData.previously_on_battery != myData.on_battery || myData.previous_battery_charge != myData.battery_charge)
61
if (myData.previously_on_battery != myData.on_battery)
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;
68
// on redessine l'icone.
69
if (myConfig.iDisplayType == CD_POWERMANAGER_GAUGE || myConfig.iDisplayType == CD_POWERMANAGER_GRAPH)
71
double fPercent = (double) myData.battery_charge / 100.;
72
CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (&fPercent);
75
else if (myConfig.iDisplayType == CD_POWERMANAGER_ICONS)
77
cd_powermanager_draw_icon_with_effect (myData.on_battery);
81
// on declenche les alarmes.
84
//Alert when battery charge is under a configured value in %
85
if (myData.battery_charge <= myConfig.lowBatteryValue && ! myData.alerted)
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]);
91
//Alert when battery charge is under 4%
92
if (myData.battery_charge <= 4 && ! myData.bCritical)
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]);
99
//Embleme sur notre icône
100
//CD_APPLET_DRAW_EMBLEM (CAIRO_DOCK_EMBLEM_BLANK, CAIRO_DOCK_EMBLEM_MIDDLE);
104
//Alert when battery is charged
105
if(myData.battery_charge == 100 && ! myData.alerted)
106
cd_powermanager_alert (POWER_MANAGER_CHARGE_FULL);
108
//CD_APPLET_DRAW_EMBLEM (CAIRO_DOCK_EMBLEM_CHARGE, CAIRO_DOCK_EMBLEM_MIDDLE);
109
CD_APPLET_DRAW_EMBLEM_ON_MY_ICON (myData.pEmblem);
112
myData.previously_on_battery = myData.on_battery;
113
myData.previous_battery_charge = myData.battery_charge;
116
else if (myData.prev_battery_present)
118
CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("sector.svg");
120
myData.prev_battery_present = FALSE;
124
CD_APPLET_REDRAW_MY_ICON;
127
gchar *get_hours_minutes (int iTimeInSeconds)
131
m = iTimeInSeconds / 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"));
141
static void _cd_powermanager_dialog (const gchar *cInfo, int iDuration)
143
cairo_dock_remove_dialog_if_any (myIcon);
145
const gchar *cIconPath;
146
if (!myData.on_battery || !myData.battery_present)
147
cIconPath = MY_APPLET_SHARE_DATA_DIR"/sector.svg";
149
cIconPath = MY_APPLET_SHARE_DATA_DIR"/default-battery.svg";
151
cd_debug ("%s (%s)", cInfo, cIconPath);
152
cairo_dock_show_temporary_dialog_with_icon (cInfo, myIcon, myContainer, 1000*iDuration, cIconPath);
155
void cd_powermanager_bubble (void)
157
GString *sInfo = g_string_new ("");
158
if(myData.battery_present)
161
if (myData.battery_time > 0.)
162
hms = get_hours_minutes (myData.battery_time);
164
hms = g_strdup_printf ("%s", D_("Unknown"));
165
if(myData.on_battery)
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);
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));
177
g_string_printf (sInfo, "%s", D_("No battery found."));
180
_cd_powermanager_dialog (sInfo->str, 6);
181
g_string_free (sInfo, TRUE);
184
gboolean cd_powermanager_alert (MyAppletCharge alert)
186
cd_debug ("%s", __func__);
187
GString *sInfo = g_string_new ("");
190
if (myData.battery_time > 0.)
191
hms = get_hours_minutes (myData.battery_time);
193
hms = g_strdup (D_("Unknown"));
195
if ((alert == POWER_MANAGER_CHARGE_LOW && myConfig.lowBatteryWitness) || (alert == POWER_MANAGER_CHARGE_CRITICAL && myConfig.criticalBatteryWitness))
197
if (myConfig.iNotificationType != 1)
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);
202
if (myConfig.iNotificationType != 2)
204
CD_APPLET_DEMANDS_ATTENTION (myConfig.cNotificationAnimation, myConfig.iNotificationDuration);
206
if (myConfig.cSoundPath[alert] != NULL)
207
cairo_dock_play_sound (myConfig.cSoundPath[alert]);
209
else if (alert == POWER_MANAGER_CHARGE_FULL && myConfig.highBatteryWitness)
211
if (myConfig.iNotificationType != 1)
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);
216
if (myConfig.iNotificationType != 2)
218
CD_APPLET_DEMANDS_ATTENTION (myConfig.cNotificationAnimation, myConfig.iNotificationDuration);
220
if (myConfig.cSoundPath[alert] != NULL)
221
cairo_dock_play_sound (myConfig.cSoundPath[alert]);
225
g_string_free (sInfo, TRUE);
226
myData.alerted = TRUE;
231
void cd_powermanager_draw_icon_with_effect (gboolean bOnBattery)
233
if (bOnBattery && myData.pSurfaceBattery == NULL)
236
if (myConfig.cUserBatteryIconName == NULL)
237
cImagePath = g_strdup (MY_APPLET_SHARE_DATA_DIR"/default-battery.svg");
239
cImagePath = cairo_dock_generate_file_path (myConfig.cUserBatteryIconName);
241
myData.pSurfaceBattery = CD_APPLET_LOAD_SURFACE_FOR_MY_APPLET (cImagePath);
244
else if (! bOnBattery && myData.pSurfaceCharge == NULL)
247
if (myConfig.cUserChargeIconName == NULL)
248
cImagePath = g_strdup (MY_APPLET_SHARE_DATA_DIR"/default-charge.svg");
250
cImagePath = cairo_dock_generate_file_path (myConfig.cUserChargeIconName);
252
myData.pSurfaceCharge = CD_APPLET_LOAD_SURFACE_FOR_MY_APPLET (cImagePath);
256
cairo_surface_t *pSurface = (bOnBattery ? myData.pSurfaceBattery : myData.pSurfaceCharge);
258
switch (myConfig.iEffect)
260
case POWER_MANAGER_EFFECT_NONE :
261
CD_APPLET_SET_SURFACE_ON_MY_ICON (pSurface);
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);
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);
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);
286
void cd_powermanager_format_value (CairoDataRenderer *pRenderer, int iNumValue, gchar *cFormatBuffer, int iBufferLength, CairoDockModuleInstance *myApplet)
288
double fValue = cairo_data_renderer_get_normalized_current_value_with_latency (pRenderer, iNumValue);
289
if(myConfig.quickInfoType == POWER_MANAGER_TIME)
291
if (myData.battery_time != 0)
293
int time = myData.battery_time;
294
int hours = time / 3600;
295
int minutes = (time % 3600) / 60;
297
snprintf (cFormatBuffer, iBufferLength, "%dh%02d", hours, abs (minutes));
299
snprintf (cFormatBuffer, iBufferLength, "%dmn", minutes);
303
strncpy (cFormatBuffer, "-:--", iBufferLength);
306
else if(myConfig.quickInfoType == POWER_MANAGER_CHARGE)
308
snprintf (cFormatBuffer, iBufferLength, "%d%%", (int)myData.battery_charge);
311
cFormatBuffer[0] = '\0';