~ubuntu-branches/ubuntu/oneiric/cairo-dock-plug-ins/oneiric-updates

« back to all changes in this revision

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

  • Committer: Kees Cook
  • Date: 2011-08-11 23:17:39 UTC
  • mfrom: (20.1.1 cairo-dock-plug-ins)
  • Revision ID: kees@outflux.net-20110811231739-cteedan51tmdg77v
Tags: 2.4.0~0beta2-0ubuntu1
releasing version 2.4.0~0beta2-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "powermanager-struct.h"
24
24
#include "powermanager-draw.h"
25
25
 
 
26
/* 3 states:
 
27
no battery -> empty gauge (0%), on-sector emblem, no alert
 
28
on battery -> gauge + %, no emblem, alert
 
29
on sector -> gauge + %, on-sector emblem, alert
 
30
*/
26
31
 
27
 
void update_icon(void)
 
32
void update_icon (void)
28
33
{
29
34
        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 prend en compte la nouvelle charge.
34
 
                if (myData.previously_on_battery != myData.on_battery || myData.previous_battery_charge != myData.battery_charge)
35
 
                {
36
 
                        if (myData.previously_on_battery != myData.on_battery)
37
 
                        {
38
 
                                myData.previously_on_battery = myData.on_battery;
39
 
                                myData.alerted = FALSE;  //On a changé de statut, donc on réinitialise les alertes
40
 
                                myData.bCritical = FALSE;
41
 
                        }
42
 
                        
43
 
                        // on redessine l'icone.
44
 
                        if (myConfig.iDisplayType == CD_POWERMANAGER_GAUGE || myConfig.iDisplayType == CD_POWERMANAGER_GRAPH)
45
 
                        {
46
 
                                double fPercent = (double) myData.battery_charge / 100.;
47
 
                                CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (&fPercent);
48
 
                                bNeedRedraw = FALSE;
49
 
                        }
50
 
                        else if (myConfig.iDisplayType == CD_POWERMANAGER_ICONS)
51
 
                        {
52
 
                                cd_powermanager_draw_icon_with_effect (myData.on_battery);
53
 
                                bNeedRedraw = FALSE;
54
 
                        }
55
 
                        
56
 
                        // on declenche les alarmes.
57
 
                        if(myData.on_battery)
58
 
                        {
59
 
                                //Alert when battery charge is under a configured value in %
60
 
                                if (myData.battery_charge <= myConfig.lowBatteryValue && ! myData.alerted)
61
 
                                {
62
 
                                        cd_powermanager_alert(POWER_MANAGER_CHARGE_LOW);
63
 
                                        if (myConfig.cSoundPath[POWER_MANAGER_CHARGE_LOW] != NULL)
64
 
                                                cairo_dock_play_sound (myConfig.cSoundPath[POWER_MANAGER_CHARGE_LOW]);
65
 
                                }
66
 
                                //Alert when battery charge is under 4%
67
 
                                if (myData.battery_charge <= 4 && ! myData.bCritical)
68
 
                                {
69
 
                                        myData.bCritical = TRUE;
70
 
                                        cd_powermanager_alert (POWER_MANAGER_CHARGE_CRITICAL);
71
 
                                        if (myConfig.cSoundPath[POWER_MANAGER_CHARGE_CRITICAL] != NULL)
72
 
                                                cairo_dock_play_sound (myConfig.cSoundPath[POWER_MANAGER_CHARGE_CRITICAL]);
73
 
                                }
74
 
                                //Embleme sur notre icône
75
 
                                //CD_APPLET_DRAW_EMBLEM (CAIRO_DOCK_EMBLEM_BLANK, CAIRO_DOCK_EMBLEM_MIDDLE);
76
 
                        }
77
 
                        else
78
 
                        {
79
 
                                //Alert when battery is charged
80
 
                                if(myData.battery_charge == 100 && ! myData.alerted)
81
 
                                        cd_powermanager_alert (POWER_MANAGER_CHARGE_FULL);
82
 
                                        
83
 
                                //CD_APPLET_DRAW_EMBLEM (CAIRO_DOCK_EMBLEM_CHARGE, CAIRO_DOCK_EMBLEM_MIDDLE);
84
 
                                CD_APPLET_DRAW_EMBLEM_ON_MY_ICON (myData.pEmblem);
85
 
                        }
86
 
                        
87
 
                        myData.previously_on_battery = myData.on_battery;
88
 
                        myData.previous_battery_charge = myData.battery_charge;
89
 
                }
90
 
                
91
 
                // on reactualise le temps restant en info rapide.
92
 
                if (myData.previous_battery_time != myData.battery_time)
93
 
                {
94
 
                        /*if(myConfig.quickInfoType == POWER_MANAGER_TIME)
95
 
                        {
96
 
                                if (myData.battery_time != 0) {
97
 
                                        CD_APPLET_SET_HOURS_MINUTES_AS_QUICK_INFO (myData.battery_time);
98
 
                                }
99
 
                                else {
100
 
                                        CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("-:--");
101
 
                                }
102
 
                        }
103
 
                        else if(myConfig.quickInfoType == POWER_MANAGER_CHARGE)
104
 
                        {
105
 
                                CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%d%%", (int)myData.battery_charge);
106
 
                        }
107
 
                        else
108
 
                        {
109
 
                          CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
110
 
                        }
111
 
                        
112
 
                        bNeedRedraw = TRUE;*/
113
 
                        
114
 
                        if (myConfig.defaultTitle == NULL || *myConfig.defaultTitle == '\0')
 
35
        cd_message ("%s (on battery: %d -> %d; time:%.1f -> %.1f ; charge:%.1f -> %.1f)", __func__, myData.bPrevOnBattery, myData.bOnBattery, (double)myData.iPrevTime, (double)myData.iTime, (double)myData.iPrevPercentage, (double)myData.iPercentage);
 
36
        
 
37
        if (myData.cBatteryStateFilePath == NULL && myData.pUPowerClient == NULL)
 
38
        {
 
39
                CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("sector.svg");
 
40
                CD_APPLET_REDRAW_MY_ICON;
 
41
                return;
 
42
        }
 
43
        // on prend en compte la nouvelle charge.
 
44
        if (myData.bPrevOnBattery != myData.bOnBattery || myData.iPrevPercentage != myData.iPercentage || myData.iTime != myData.iPrevTime)
 
45
        {
 
46
                if (myData.bPrevOnBattery != myData.bOnBattery)
 
47
                {
 
48
                        myData.bPrevOnBattery = myData.bOnBattery;
 
49
                        myData.bAlerted = FALSE;  // On a change de statut, donc on reinitialise les alertes
 
50
                        myData.bCritical = FALSE;
 
51
                }
 
52
                
 
53
                // on redessine l'icone.
 
54
                if (myConfig.iDisplayType == CD_POWERMANAGER_GAUGE || myConfig.iDisplayType == CD_POWERMANAGER_GRAPH)
 
55
                {
 
56
                        double fPercent = (double) myData.iPercentage / 100.;
 
57
                        CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (&fPercent);
 
58
                        bNeedRedraw = FALSE;
 
59
                }
 
60
                else if (myConfig.iDisplayType == CD_POWERMANAGER_ICONS)
 
61
                {
 
62
                        cd_powermanager_draw_icon_with_effect (myData.bOnBattery);
 
63
                        bNeedRedraw = FALSE;
 
64
                }
 
65
                
 
66
                // on declenche les alarmes.
 
67
                if (myData.bOnBattery)
 
68
                {
 
69
                        // Alert when battery charge is under a configured value in %
 
70
                        if (myData.iPercentage <= myConfig.lowBatteryValue && ! myData.bAlerted)
 
71
                        {
 
72
                                cd_powermanager_alert(POWER_MANAGER_CHARGE_LOW);
 
73
                                if (myConfig.cSoundPath[POWER_MANAGER_CHARGE_LOW] != NULL)
 
74
                                        cairo_dock_play_sound (myConfig.cSoundPath[POWER_MANAGER_CHARGE_LOW]);
 
75
                        }
 
76
                        // Alert when battery charge is under 4%
 
77
                        if (myData.iPercentage <= 4 && ! myData.bCritical)
 
78
                        {
 
79
                                myData.bCritical = TRUE;
 
80
                                cd_powermanager_alert (POWER_MANAGER_CHARGE_CRITICAL);
 
81
                                if (myConfig.cSoundPath[POWER_MANAGER_CHARGE_CRITICAL] != NULL)
 
82
                                        cairo_dock_play_sound (myConfig.cSoundPath[POWER_MANAGER_CHARGE_CRITICAL]);
 
83
                        }
 
84
                        // emblem is implicitely erased.
 
85
                }
 
86
                else
 
87
                {
 
88
                        // Alert when battery is charged
 
89
                        if(myData.iPercentage == 100 && ! myData.bAlerted)
 
90
                                cd_powermanager_alert (POWER_MANAGER_CHARGE_FULL);
 
91
                                
 
92
                        CD_APPLET_DRAW_EMBLEM_ON_MY_ICON (myData.pEmblem);
 
93
                }
 
94
                
 
95
                if (myConfig.defaultTitle == NULL || *myConfig.defaultTitle == '\0')
 
96
                {
 
97
                        if (! myData.bOnBattery && myData.iPercentage > 99.9)
 
98
                        {
 
99
                                CD_APPLET_SET_NAME_FOR_MY_ICON_PRINTF ("%s (%d%%)",
 
100
                                        D_("Battery charged"),
 
101
                                        (int)myData.iPercentage);
 
102
                        }
 
103
                        else
115
104
                        {
116
105
                                gchar cFormatBuffer[21];
117
106
                                int iBufferLength = 20;
118
 
                                if (myData.battery_time != 0)
 
107
                                if (myData.iTime != 0)
119
108
                                {
120
 
                                        int time = myData.battery_time;
 
109
                                        int time = myData.iTime;
121
110
                                        int hours = time / 3600;
122
111
                                        int minutes = (time % 3600) / 60;
123
112
                                        if (hours != 0)
129
118
                                {
130
119
                                        strncpy (cFormatBuffer, "-:--", iBufferLength);
131
120
                                }
132
 
                                CD_APPLET_SET_NAME_FOR_MY_ICON_PRINTF ("%s: %d%% - %s: %s",
133
 
                                        D_("Charge"),
134
 
                                        (int)myData.battery_charge,
135
 
                                        D_("Time"),
136
 
                                        cFormatBuffer);
 
121
                                CD_APPLET_SET_NAME_FOR_MY_ICON_PRINTF ("%s: %s (%d%%)",
 
122
                                        myData.bOnBattery ? D_("Time before empty") : D_("Time before full"),
 
123
                                        cFormatBuffer,
 
124
                                        (int)myData.iPercentage);
137
125
                        }
138
 
                        myData.previous_battery_time = myData.battery_time;
139
126
                }
140
 
        }
141
 
        else if (myData.prev_battery_present)
142
 
        {
143
 
                CD_APPLET_SET_LOCAL_IMAGE_ON_MY_ICON ("sector.svg");
144
 
                bNeedRedraw = TRUE;
145
 
                myData.prev_battery_present = FALSE;
 
127
                
 
128
                myData.bPrevOnBattery = myData.bOnBattery;
 
129
                myData.iPrevPercentage = myData.iPercentage;
 
130
                myData.iPrevTime = myData.iTime;
146
131
        }
147
132
        
148
133
        if (bNeedRedraw)
152
137
gchar *get_hours_minutes (int iTimeInSeconds)
153
138
{
154
139
        gchar *cTimeString;
155
 
        int h=0, m=0;
156
 
        m = iTimeInSeconds / 60;
157
 
        h = m / 60;
158
 
        m = m - (h * 60);
159
 
        if (h > 0) cTimeString = g_strdup_printf("%dh%02dm", h, m);
160
 
        else if (m > 0) cTimeString = g_strdup_printf("%dm", m);
161
 
        else cTimeString = g_strdup (D_("None"));
 
140
        int h = iTimeInSeconds / 3600;
 
141
        int m = (iTimeInSeconds % 3600) / 60;
 
142
        if (h > 0)              cTimeString = g_strdup_printf ("%dh%02dm", h, m);
 
143
        else if (m > 0) cTimeString = g_strdup_printf ("%dm", m);
 
144
        else                    cTimeString = g_strdup (D_("None"));
162
145
        
163
146
        return cTimeString;
164
147
}
167
150
{
168
151
        cairo_dock_remove_dialog_if_any (myIcon);
169
152
        
170
 
        const gchar *cIconPath;
171
 
        if (!myData.on_battery || !myData.battery_present)
 
153
        /**const gchar *cIconPath;
 
154
        if (!myData.bOnBattery)
172
155
                cIconPath = MY_APPLET_SHARE_DATA_DIR"/sector.svg";
173
156
        else
174
157
                cIconPath = MY_APPLET_SHARE_DATA_DIR"/default-battery.svg";
175
158
        
176
 
        cd_debug ("%s (%s)", cInfo, cIconPath);
177
 
        cairo_dock_show_temporary_dialog_with_icon (cInfo, myIcon, myContainer, 1000*iDuration, cIconPath);
 
159
        cd_debug ("%s (%s)", cInfo, cIconPath);*/
 
160
        cairo_dock_show_temporary_dialog_with_icon (cInfo, myIcon, myContainer, 1000*iDuration, "same icon");
178
161
}
179
162
 
180
163
void cd_powermanager_bubble (void)
181
164
{
182
165
        GString *sInfo = g_string_new ("");
183
 
        if(myData.battery_present)
 
166
        if (myData.cBatteryStateFilePath != NULL || myData.pUPowerClient != NULL)
184
167
        {
 
168
                // time and charge.
185
169
                gchar *hms = NULL;
186
 
                if (myData.battery_time > 0.)
187
 
                        hms = get_hours_minutes (myData.battery_time);
 
170
                if (myData.iTime > 0.)
 
171
                        hms = get_hours_minutes (myData.iTime);
188
172
                else
189
173
                        hms = g_strdup_printf ("%s", D_("Unknown"));
190
 
                if(myData.on_battery)
 
174
                if (myData.bOnBattery)
191
175
                {
192
 
                        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);
 
176
                        g_string_printf (sInfo, "%s\n"
 
177
                                "%s %d%%\n"
 
178
                                "%s %s",
 
179
                                D_("Laptop on Battery."),
 
180
                                D_("Battery charged at:"), (int)myData.iPercentage,
 
181
                                D_("Estimated time before empty:"), hms);
193
182
                }
194
183
                else
195
184
                {
196
 
                        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));
 
185
                        g_string_printf (sInfo, "%s\n"
 
186
                                "%s %d%%\n"
 
187
                                "%s %s",
 
188
                                D_("Laptop on Charge."),
 
189
                                D_("Battery charged at:"), (int)myData.iPercentage,
 
190
                                D_("Estimated time before full:"), (myData.iPercentage > 99.9 ? "0" : hms));
197
191
                }
198
192
                g_free (hms);
 
193
                
 
194
                // static info
 
195
                if (myData.cVendor != NULL || myData.cModel != NULL)
 
196
                {
 
197
                        g_string_append_printf (sInfo, "\n%s: %s %s", D_("Model"), myData.cVendor ? myData.cVendor : "", myData.cModel ? myData.cModel : "");
 
198
                }
 
199
                /*if (0&&myData.cTechnology != NULL) // if (0 && (...)) ??? :)
 
200
                {
 
201
                        g_string_append_printf (sInfo, "\n%s: %s", D_("Technology"), myData.cTechnology);
 
202
                }*/
 
203
                if (myData.fMaxAvailableCapacity != 0)
 
204
                {
 
205
                        g_string_append_printf (sInfo, "\n%s: %d%%", D_("Maximum capacity"), (int)myData.fMaxAvailableCapacity);
 
206
                }
199
207
        }
200
208
        else
201
209
        {
202
 
                g_string_printf (sInfo, "%s", D_("No battery found."));
 
210
                g_string_assign (sInfo, D_("No battery found."));
203
211
        }
204
212
        
205
 
        _cd_powermanager_dialog (sInfo->str, 6);
 
213
        _cd_powermanager_dialog (sInfo->str, 7);
206
214
        g_string_free (sInfo, TRUE);
207
215
}
208
216
 
212
220
        GString *sInfo = g_string_new ("");
213
221
        
214
222
        gchar *hms = NULL;
215
 
        if (myData.battery_time > 0.)
216
 
                hms = get_hours_minutes (myData.battery_time);
 
223
        if (myData.iTime > 0.)
 
224
                hms = get_hours_minutes (myData.iTime);
217
225
        else
218
226
                hms = g_strdup (D_("Unknown"));
219
227
                
221
229
        {
222
230
                if (myConfig.iNotificationType != 1)
223
231
                {
224
 
                        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."));
 
232
                        g_string_printf (sInfo, "%s (%d%%) \n %s %s \n %s", D_("PowerManager.\nBattery charge seems to be low"), (int)myData.iPercentage, D_("Estimated time with charge:"), hms, D_("Please put your laptop on charge."));
225
233
                        _cd_powermanager_dialog (sInfo->str, myConfig.iNotificationDuration);
226
234
                }
227
235
                if (myConfig.iNotificationType != 2)
235
243
        {
236
244
                if (myConfig.iNotificationType != 1)
237
245
                {
238
 
                        g_string_printf (sInfo, "%s (%.2f%%)", D_("PowerManager.\nYour battery is now charged"), myData.battery_charge);
 
246
                        g_string_printf (sInfo, "%s (%d%%)", D_("PowerManager.\nYour battery is now charged"), (int)myData.iPercentage);
239
247
                        _cd_powermanager_dialog (sInfo->str, myConfig.iNotificationDuration);
240
248
                }
241
249
                if (myConfig.iNotificationType != 2)
248
256
        
249
257
        g_free (hms);
250
258
        g_string_free (sInfo, TRUE);
251
 
        myData.alerted = TRUE;
 
259
        myData.bAlerted = TRUE;
252
260
        return FALSE;
253
261
}
254
262
 
287
295
                break;
288
296
                case POWER_MANAGER_EFFECT_ZOOM :
289
297
                        cairo_save (myDrawContext);
290
 
                        double fScale = .3 + .7 * myData.battery_charge / 100.;
 
298
                        double fScale = .3 + .7 * myData.iPercentage / 100.;
291
299
                        CD_APPLET_SET_SURFACE_ON_MY_ICON_WITH_ZOOM (pSurface, fScale);
292
300
                        cairo_restore (myDrawContext);
293
301
                break;
294
302
                case POWER_MANAGER_EFFECT_TRANSPARENCY :
295
303
                        cairo_save (myDrawContext);
296
 
                        double fAlpha = .3 + .7 * myData.battery_charge / 100.;
 
304
                        double fAlpha = .3 + .7 * myData.iPercentage / 100.;
297
305
                        CD_APPLET_SET_SURFACE_ON_MY_ICON_WITH_ALPHA (pSurface, fAlpha);
298
306
                        cairo_restore (myDrawContext);
299
307
                break;
300
308
                case POWER_MANAGER_EFFECT_BAR :
301
309
                        cairo_save (myDrawContext);
302
 
                        CD_APPLET_SET_SURFACE_ON_MY_ICON_WITH_BAR (pSurface, myData.battery_charge * .01);
 
310
                        CD_APPLET_SET_SURFACE_ON_MY_ICON_WITH_BAR (pSurface, myData.iPercentage * .01);
303
311
                        cairo_restore (myDrawContext);
304
312
                break;
305
313
                default :
313
321
        double fValue = cairo_data_renderer_get_normalized_current_value_with_latency (pRenderer, iNumValue);
314
322
        if(myConfig.quickInfoType == POWER_MANAGER_TIME)
315
323
        {
316
 
                if (myData.battery_time != 0)
 
324
                if (myData.iTime != 0)
317
325
                {
318
 
                        int time = myData.battery_time;
 
326
                        int time = myData.iTime;
319
327
                        int hours = time / 3600;
320
328
                        int minutes = (time % 3600) / 60;
 
329
                        g_print ("time: %d -> %d;%d\n", time, hours, minutes);
321
330
                        if (hours != 0)
322
331
                                snprintf (cFormatBuffer, iBufferLength, "%dh%02d", hours, abs (minutes));
323
332
                        else
330
339
        }
331
340
        else if(myConfig.quickInfoType == POWER_MANAGER_CHARGE)
332
341
        {
333
 
                snprintf (cFormatBuffer, iBufferLength, "%d%%", (int)myData.battery_charge);
 
342
                snprintf (cFormatBuffer, iBufferLength, "%d%%", (int)myData.iPercentage);
334
343
        }
335
344
        else
336
345
                cFormatBuffer[0] = '\0';