~ubuntu-branches/ubuntu/maverick/gnome-power-manager/maverick-proposed

« back to all changes in this revision

Viewing changes to src/gpm-upower.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2010-08-13 09:22:28 UTC
  • mfrom: (2.1.54 upstream)
  • Revision ID: james.westby@ubuntu.com-20100813092228-t3as2pyseicp20dk
Tags: 2.31.6-0ubuntu1
* New upstream version:
  - Adjust dim timeout when necessary.
  - Backport support for new devices recognised in UPower.
  - Backport various translation fixes and translator comments from git master.
  - Do not assume the lid is open at boot.
  - Do not show gnome-power-statistics in the menus.
  - Do not show the prefs menu on LiveCD's and with GDM.
  - Don't treat percentage=0 as error condition.
  - Fix a crash when displaying the 'No GConf schema warning'.
* Drop 00git-initial-lid-status.patch, upstream now.
* 12-add-appindicators.patch: Port to new upstream version.
* Add 00git-devices-changed-signal.patch: Add a "devices-changed" signal.
  This was already in 2.31.1, but got dropped from the 2.32 branch. However,
  our appindicator patch needs it now.
* 02-notify-osd-support.patch: Unfuzz for new upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
/**
37
37
 * gpm_upower_get_device_icon_index:
38
 
 * @device: The UpDevice
 
38
 * @percent: The charge of the device
 
39
 *
 
40
 * The index value depends on the percentage charge:
 
41
 *      00-10  = 000
 
42
 *      10-30  = 020
 
43
 *      30-50  = 040
 
44
 *      50-70  = 060
 
45
 *      70-90  = 080
 
46
 *      90-100 = 100
39
47
 *
40
48
 * Return value: The character string for the filename suffix.
41
49
 **/
59
67
}
60
68
 
61
69
/**
62
 
 * gpm_upower_get_device_icon_suffix:
63
 
 * @device: The UpDevice
64
 
 *
65
 
 * Return value: The character string for the filename suffix.
66
 
 **/
67
 
static const gchar *
68
 
gpm_upower_get_device_icon_suffix (UpDevice *device)
69
 
{
70
 
        gdouble percentage;
71
 
        /* get device properties */
72
 
        g_object_get (device, "percentage", &percentage, NULL);
73
 
        if (percentage < 10)
74
 
                return "caution";
75
 
        else if (percentage < 30)
76
 
                return "low";
77
 
        else if (percentage < 60)
78
 
                return "good";
79
 
        return "full";
80
 
}
81
 
 
82
 
/**
83
70
 * gpm_upower_get_device_icon:
84
71
 *
85
 
 * Return value: The device icon, use g_object_unref() when done.
 
72
 * Need to free the return value
 
73
 *
86
74
 **/
87
 
GIcon *
88
 
gpm_upower_get_device_icon (UpDevice *device, gboolean use_symbolic)
 
75
gchar *
 
76
gpm_upower_get_device_icon (UpDevice *device)
89
77
{
90
 
        GString *filename;
91
 
        gchar **iconnames;
92
 
        const gchar *kind_str;
93
 
        const gchar *suffix_str;
 
78
        gchar *filename = NULL;
 
79
        const gchar *prefix = NULL;
94
80
        const gchar *index_str;
95
81
        UpDeviceKind kind;
96
82
        UpDeviceState state;
97
83
        gboolean is_present;
98
84
        gdouble percentage;
99
 
        GIcon *icon = NULL;
100
85
 
101
86
        g_return_val_if_fail (device != NULL, NULL);
102
87
 
109
94
                      NULL);
110
95
 
111
96
        /* get correct icon prefix */
112
 
        filename = g_string_new (NULL);
 
97
        prefix = up_device_kind_to_string (kind);
113
98
 
114
99
        /* get the icon from some simple rules */
115
100
        if (kind == UP_DEVICE_KIND_LINE_POWER) {
116
 
                if (use_symbolic)
117
 
                        g_string_append (filename, "ac-adapter-symbolic;");
118
 
                g_string_append (filename, "ac-adapter;");
119
 
 
 
101
                filename = g_strdup ("gpm-ac-adapter");
120
102
        } else if (kind == UP_DEVICE_KIND_MONITOR) {
121
 
                if (use_symbolic)
122
 
                        g_string_append (filename, "gpm-monitor-symbolic;");
123
 
                g_string_append (filename, "gpm-monitor;");
124
 
 
125
 
        } else {
126
 
 
127
 
                kind_str = up_device_kind_to_string (kind);
128
 
                if (!is_present) {
129
 
                        if (use_symbolic)
130
 
                                g_string_append (filename, "battery-missing-symbolic;");
131
 
                        g_string_append_printf (filename, "gpm-%s-missing;", kind_str);
132
 
                        g_string_append_printf (filename, "gpm-%s-000;", kind_str);
133
 
                        g_string_append (filename, "battery-missing;");
134
 
 
 
103
                filename = g_strdup ("gpm-monitor");
 
104
        } else if (kind == UP_DEVICE_KIND_UPS) {
 
105
                if (!is_present) {
 
106
                        /* battery missing */
 
107
                        filename = g_strdup_printf ("gpm-%s-missing", prefix);
 
108
 
 
109
                } else if (state == UP_DEVICE_STATE_FULLY_CHARGED) {
 
110
                        filename = g_strdup_printf ("gpm-%s-100", prefix);
 
111
 
 
112
                } else if (state == UP_DEVICE_STATE_CHARGING) {
 
113
                        index_str = gpm_upower_get_device_icon_index (device);
 
114
                        filename = g_strdup_printf ("gpm-%s-%s-charging", prefix, index_str);
 
115
 
 
116
                } else if (state == UP_DEVICE_STATE_DISCHARGING) {
 
117
                        index_str = gpm_upower_get_device_icon_index (device);
 
118
                        filename = g_strdup_printf ("gpm-%s-%s", prefix, index_str);
 
119
                }
 
120
        } else if (kind == UP_DEVICE_KIND_BATTERY) {
 
121
                if (!is_present) {
 
122
                        /* battery missing */
 
123
                        filename = g_strdup_printf ("gpm-%s-missing", prefix);
 
124
 
 
125
                } else if (state == UP_DEVICE_STATE_EMPTY) {
 
126
                        filename = g_strdup_printf ("gpm-%s-empty", prefix);
 
127
 
 
128
                } else if (state == UP_DEVICE_STATE_FULLY_CHARGED) {
 
129
                        filename = g_strdup_printf ("gpm-%s-charged", prefix);
 
130
 
 
131
                } else if (state == UP_DEVICE_STATE_CHARGING) {
 
132
                        index_str = gpm_upower_get_device_icon_index (device);
 
133
                        filename = g_strdup_printf ("gpm-%s-%s-charging", prefix, index_str);
 
134
 
 
135
                } else if (state == UP_DEVICE_STATE_DISCHARGING) {
 
136
                        index_str = gpm_upower_get_device_icon_index (device);
 
137
                        filename = g_strdup_printf ("gpm-%s-%s", prefix, index_str);
 
138
 
 
139
                } else if (state == UP_DEVICE_STATE_PENDING_CHARGE) {
 
140
                        index_str = gpm_upower_get_device_icon_index (device);
 
141
                        /* FIXME: do new grey icons */
 
142
                        filename = g_strdup_printf ("gpm-%s-%s-charging", prefix, index_str);
 
143
 
 
144
                } else if (state == UP_DEVICE_STATE_PENDING_DISCHARGE) {
 
145
                        index_str = gpm_upower_get_device_icon_index (device);
 
146
                        filename = g_strdup_printf ("gpm-%s-%s", prefix, index_str);
135
147
                } else {
136
 
                        switch (state) {
137
 
                        case UP_DEVICE_STATE_EMPTY:
138
 
                                if (use_symbolic)
139
 
                                        g_string_append (filename, "battery-empty-symbolic;");
140
 
                                g_string_append_printf (filename, "gpm-%s-empty;", kind_str);
141
 
                                g_string_append_printf (filename, "gpm-%s-000;", kind_str);
142
 
                                g_string_append (filename, "battery-empty;");
143
 
                                break;
144
 
                        case UP_DEVICE_STATE_FULLY_CHARGED:
145
 
                                if (use_symbolic)
146
 
                                        g_string_append (filename, "battery-full-symbolic;");
147
 
                                g_string_append_printf (filename, "gpm-%s-full;", kind_str);
148
 
                                g_string_append_printf (filename, "gpm-%s-100;", kind_str);
149
 
                                g_string_append (filename, "battery-full;");
150
 
                                break;
151
 
                        case UP_DEVICE_STATE_CHARGING:
152
 
                        case UP_DEVICE_STATE_PENDING_CHARGE:
153
 
                                suffix_str = gpm_upower_get_device_icon_suffix (device);
154
 
                                index_str = gpm_upower_get_device_icon_index (device);
155
 
                                if (use_symbolic)
156
 
                                        g_string_append_printf (filename, "battery-%s-charging-symbolic;", suffix_str);
157
 
                                g_string_append_printf (filename, "gpm-%s-%s-charging;", kind_str, index_str);
158
 
                                g_string_append_printf (filename, "battery-%s-charging;", suffix_str);
159
 
                                break;
160
 
                        case UP_DEVICE_STATE_DISCHARGING:
161
 
                        case UP_DEVICE_STATE_PENDING_DISCHARGE:
162
 
                                suffix_str = gpm_upower_get_device_icon_suffix (device);
163
 
                                index_str = gpm_upower_get_device_icon_index (device);
164
 
                                if (use_symbolic)
165
 
                                        g_string_append_printf (filename, "battery-%s-symbolic;", suffix_str);
166
 
                                g_string_append_printf (filename, "gpm-%s-%s;", kind_str, index_str);
167
 
                                g_string_append_printf (filename, "battery-%s;", suffix_str);
168
 
                                break;
169
 
                        default:
170
 
                                if (use_symbolic)
171
 
                                        g_string_append (filename, "battery-missing-symbolic;");
172
 
                                g_string_append (filename, "gpm-battery-missing;");
173
 
                                g_string_append (filename, "battery-missing;");
174
 
                        }
 
148
                        filename = g_strdup ("gpm-battery-missing");
 
149
                }
 
150
 
 
151
        } else if (kind == UP_DEVICE_KIND_MOUSE ||
 
152
                   kind == UP_DEVICE_KIND_KEYBOARD ||
 
153
                   kind == UP_DEVICE_KIND_PHONE) {
 
154
                if (!is_present) {
 
155
                        /* battery missing */
 
156
                        filename = g_strdup_printf ("gpm-%s-000", prefix);
 
157
 
 
158
                } else if (state == UP_DEVICE_STATE_FULLY_CHARGED) {
 
159
                        filename = g_strdup_printf ("gpm-%s-100", prefix);
 
160
 
 
161
                } else if (state == UP_DEVICE_STATE_DISCHARGING) {
 
162
                        index_str = gpm_upower_get_device_icon_index (device);
 
163
                        filename = g_strdup_printf ("gpm-%s-%s", prefix, index_str);
175
164
                }
176
165
        }
177
166
 
178
167
        /* nothing matched */
179
 
        if (filename->len == 0) {
 
168
        if (filename == NULL) {
180
169
                egg_warning ("nothing matched, falling back to default icon");
181
 
                g_string_append (filename, "dialog-warning;");
 
170
                filename = g_strdup ("dialog-warning");
182
171
        }
183
172
 
184
 
        egg_debug ("got filename: %s", filename->str);
185
 
 
186
 
        iconnames = g_strsplit (filename->str, ";", -1);
187
 
        icon = g_themed_icon_new_from_names (iconnames, -1);
188
 
 
189
 
        g_strfreev (iconnames);
190
 
        g_string_free (filename, TRUE);
191
 
        return icon;
 
173
        egg_debug ("got filename: %s", filename);
 
174
        return filename;
192
175
}
193
176
 
194
177
/**
198
181
gpm_upower_get_device_summary (UpDevice *device)
199
182
{
200
183
        const gchar *kind_desc = NULL;
201
 
        GString *description;
 
184
        gchar *description = NULL;
202
185
        guint time_to_full_round;
203
186
        guint time_to_empty_round;
204
 
        gchar *time_to_full_str = NULL;
205
 
        gchar *time_to_empty_str = NULL;
 
187
        gchar *time_to_full_str;
 
188
        gchar *time_to_empty_str;
206
189
        UpDeviceKind kind;
207
190
        UpDeviceState state;
208
191
        gdouble percentage;
220
203
                      "time-to-empty", &time_to_empty,
221
204
                      NULL);
222
205
 
223
 
        description = g_string_new (NULL);
224
 
        kind_desc = gpm_device_kind_to_localised_string (kind, 1);
 
206
        if (!is_present)
 
207
                return NULL;
225
208
 
226
 
        /* not installed */
227
 
        if (!is_present) {
228
 
                /* TRANSLATORS: device not present */
229
 
                g_string_append_printf (description, _("%s not present"), kind_desc);
230
 
                goto out;
231
 
        }
 
209
        kind_desc = gpm_device_kind_to_localised_text (kind, 1);
232
210
 
233
211
        /* don't display all the extra stuff for keyboards and mice */
234
212
        if (kind == UP_DEVICE_KIND_MOUSE ||
235
213
            kind == UP_DEVICE_KIND_KEYBOARD ||
236
 
            kind == UP_DEVICE_KIND_PDA) {
237
 
                g_string_append (description, kind_desc);
238
 
                g_string_append_printf (description, " (%.0f%%)", percentage);
239
 
                goto out;
240
 
        }
 
214
            kind == UP_DEVICE_KIND_PDA)
 
215
                return g_strdup_printf ("%s (%.1f%%)", kind_desc, percentage);
241
216
 
242
217
        /* we care if we are on AC */
243
218
        if (kind == UP_DEVICE_KIND_PHONE) {
244
219
                if (state == UP_DEVICE_STATE_CHARGING || !state == UP_DEVICE_STATE_DISCHARGING) {
245
220
                        /* TRANSLATORS: a phone is charging */
246
 
                        g_string_append_printf (description, _("%s charging"), kind_desc);
247
 
                        g_string_append_printf (description, " (%.0f%%)", percentage);
248
 
                        goto out;
 
221
                        return g_strdup_printf (_("%s charging (%.1f%%)"), kind_desc, percentage);
249
222
                }
250
 
                g_string_append (description, kind_desc);
251
 
                g_string_append_printf (description, " (%.0f%%)", percentage);
252
 
                goto out;
 
223
                return g_strdup_printf ("%s (%.1f%%)", kind_desc, percentage);
253
224
        }
254
225
 
255
226
        /* precalculate so we don't get Unknown time remaining */
259
230
        /* we always display "Laptop battery 16 minutes remaining" as we need to clarify what device we are refering to */
260
231
        if (state == UP_DEVICE_STATE_FULLY_CHARGED) {
261
232
 
262
 
                /* TRANSLATORS: the device is charged */
263
 
                g_string_append_printf (description, _("%s is charged"), kind_desc);
264
 
 
265
233
                if (kind == UP_DEVICE_KIND_BATTERY && time_to_empty_round > GPM_UP_TEXT_MIN_TIME) {
266
234
                        time_to_empty_str = gpm_get_timestring (time_to_empty_round);
267
 
                        g_string_append (description, " - ");
268
 
                        /* TRANSLATORS: The laptop battery is charged, and we know a time */
269
 
                        g_string_append_printf (description, _("provides %s laptop runtime"), time_to_empty_str);
 
235
                        /* TRANSLATORS: The laptop battery is fully charged, and we know a time */
 
236
                        description = g_strdup_printf (_("Battery is fully charged.\nProvides %s laptop runtime"),
 
237
                                                        time_to_empty_str);
 
238
                        g_free (time_to_empty_str);
 
239
                } else {
 
240
                        /* TRANSLATORS: the device is fully charged */
 
241
                        description = g_strdup_printf (_("%s is fully charged"), kind_desc);
270
242
                }
271
 
                goto out;
272
 
        }
273
 
        if (state == UP_DEVICE_STATE_DISCHARGING) {
 
243
 
 
244
        } else if (state == UP_DEVICE_STATE_DISCHARGING) {
274
245
 
275
246
                if (time_to_empty_round > GPM_UP_TEXT_MIN_TIME) {
276
247
                        time_to_empty_str = gpm_get_timestring (time_to_empty_round);
277
248
                        /* TRANSLATORS: the device is discharging, and we have a time remaining */
278
 
                        g_string_append_printf (description, _("%s %s remaining"),
279
 
                                                kind_desc, time_to_empty_str);
280
 
                        g_string_append_printf (description, " (%.0f%%)", percentage);
 
249
                        description = g_strdup_printf (_("%s %s remaining (%.1f%%)"),
 
250
                                                        kind_desc, time_to_empty_str, percentage);
 
251
                        g_free (time_to_empty_str);
281
252
                } else {
282
253
                        /* TRANSLATORS: the device is discharging, but we only have a percentage */
283
 
                        g_string_append_printf (description, _("%s discharging"), kind_desc);
284
 
                        g_string_append_printf (description, " (%.0f%%)", percentage);
 
254
                        description = g_strdup_printf (_("%s discharging (%.1f%%)"),
 
255
                                                        kind_desc, percentage);
285
256
                }
286
 
                goto out;
287
 
        }
288
 
        if (state == UP_DEVICE_STATE_CHARGING) {
 
257
 
 
258
        } else if (state == UP_DEVICE_STATE_CHARGING) {
289
259
 
290
260
                if (time_to_full_round > GPM_UP_TEXT_MIN_TIME &&
291
261
                    time_to_empty_round > GPM_UP_TEXT_MIN_TIME) {
294
264
                        time_to_full_str = gpm_get_timestring (time_to_full_round);
295
265
                        time_to_empty_str = gpm_get_timestring (time_to_empty_round);
296
266
 
297
 
                        /* TRANSLATORS: device is charging, and we have a time to full and a percentage */
298
 
                        g_string_append_printf (description, _("%s %s until charged"),
299
 
                                                kind_desc, time_to_full_str);
300
 
                        g_string_append_printf (description, " (%.0f%%)", percentage);
301
 
 
302
 
                        g_string_append (description, " - ");
303
267
                        /* TRANSLATORS: the device is charging, and we have a time to full and empty */
304
 
                        g_string_append_printf (description, _("provides %s battery runtime"),
305
 
                                                time_to_empty_str);
 
268
                        description = g_strdup_printf (_("%s %s until charged (%.1f%%)\nProvides %s battery runtime"),
 
269
                                                        kind_desc, time_to_full_str, percentage, time_to_empty_str);
 
270
                        g_free (time_to_full_str);
 
271
                        g_free (time_to_empty_str);
 
272
 
306
273
                } else if (time_to_full_round > GPM_UP_TEXT_MIN_TIME) {
307
274
 
308
275
                        /* display only charge time */
309
276
                        time_to_full_str = gpm_get_timestring (time_to_full_round);
310
277
 
311
278
                        /* TRANSLATORS: device is charging, and we have a time to full and a percentage */
312
 
                        g_string_append_printf (description, _("%s %s until charged"),
313
 
                                                kind_desc, time_to_full_str);
314
 
                        g_string_append_printf (description, " (%.0f%%)", percentage);
 
279
                        description = g_strdup_printf (_("%s %s until charged (%.1f%%)"),
 
280
                                                kind_desc, time_to_full_str, percentage);
 
281
                        g_free (time_to_full_str);
315
282
                } else {
316
283
 
317
284
                        /* TRANSLATORS: device is charging, but we only have a percentage */
318
 
                        g_string_append_printf (description, _("%s charging"), kind_desc);
319
 
                        g_string_append_printf (description, " (%.0f%%)", percentage);
 
285
                        description = g_strdup_printf (_("%s charging (%.1f%%)"),
 
286
                                                kind_desc, percentage);
320
287
                }
321
 
                goto out;
322
 
        }
323
 
        if (state == UP_DEVICE_STATE_PENDING_DISCHARGE) {
324
 
 
325
 
                /* TRANSLATORS: this is only shown for laptops with multiple batteries */
326
 
                g_string_append_printf (description, _("%s waiting to discharge"), kind_desc);
327
 
                g_string_append_printf (description, " (%.0f%%)", percentage);
328
 
                goto out;
329
 
        }
330
 
        if (state == UP_DEVICE_STATE_PENDING_CHARGE) {
331
 
 
332
 
                /* TRANSLATORS: this is only shown for laptops with multiple batteries */
333
 
                g_string_append_printf (description, _("%s waiting to charge"), kind_desc);
334
 
                g_string_append_printf (description, " (%.0f%%)", percentage);
335
 
                goto out;
336
 
        }
337
 
        if (state == UP_DEVICE_STATE_EMPTY) {
338
 
 
339
 
                /* TRANSLATORS: when the device has no charge left */
340
 
                g_string_append_printf (description, _("%s empty"), kind_desc);
341
 
                goto out;
342
 
        }
343
 
 
344
 
        /* fallback */
345
 
        egg_warning ("in an undefined state we are not charging or "
346
 
                     "discharging and the batteries are also not charged");
347
 
        g_string_append (description, kind_desc);
348
 
        g_string_append_printf (description, " (%.0f%%)", percentage);
349
 
out:
350
 
        g_free (time_to_full_str);
351
 
        g_free (time_to_empty_str);
352
 
        return g_string_free (description, FALSE);
 
288
 
 
289
        } else if (state == UP_DEVICE_STATE_PENDING_DISCHARGE) {
 
290
 
 
291
                /* TRANSLATORS: this is only shown for laptops with multiple batteries */
 
292
                description = g_strdup_printf (_("%s waiting to discharge (%.1f%%)"),
 
293
                                                kind_desc, percentage);
 
294
 
 
295
        } else if (state == UP_DEVICE_STATE_PENDING_CHARGE) {
 
296
 
 
297
                /* TRANSLATORS: this is only shown for laptops with multiple batteries */
 
298
                description = g_strdup_printf (_("%s waiting to charge (%.1f%%)"), kind_desc, percentage);
 
299
 
 
300
        } else {
 
301
                egg_warning ("in an undefined state we are not charging or "
 
302
                             "discharging and the batteries are also not charged");
 
303
                description = g_strdup_printf ("%s (%.1f%%)", kind_desc, percentage);
 
304
        }
 
305
        return description;
353
306
}
354
307
 
355
308
/**
399
352
                      NULL);
400
353
 
401
354
        details = g_string_new ("");
402
 
        text = gpm_device_kind_to_localised_string (kind, 1);
 
355
        text = gpm_device_kind_to_localised_text (kind, 1);
403
356
        /* TRANSLATORS: the type of data, e.g. Laptop battery */
404
357
        g_string_append_printf (details, "<b>%s</b> %s\n", _("Product:"), text);
405
358
 
513
466
}
514
467
 
515
468
/**
516
 
 * gpm_device_kind_to_localised_string:
 
469
 * gpm_device_kind_to_localised_text:
517
470
 **/
518
471
const gchar *
519
 
gpm_device_kind_to_localised_string (UpDeviceKind kind, guint number)
 
472
gpm_device_kind_to_localised_text (UpDeviceKind kind, guint number)
520
473
{
521
474
        const gchar *text = NULL;
522
475
        switch (kind) {
582
535
        const gchar *icon = NULL;
583
536
        switch (kind) {
584
537
        case UP_DEVICE_KIND_LINE_POWER:
585
 
                icon = "ac-adapter";
 
538
                icon = "gpm-ac-adapter";
586
539
                break;
587
540
        case UP_DEVICE_KIND_BATTERY:
588
541
                icon = "battery";