1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/*
* e-activity.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#include "e-activity.h"
#include <stdarg.h>
#include <glib/gi18n.h>
#include "e-util/e-util.h"
#define E_ACTIVITY_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_ACTIVITY, EActivityPrivate))
struct _EActivityPrivate {
gchar *icon_name;
gchar *primary_text;
gchar *secondary_text;
gdouble percent;
guint allow_cancel : 1;
guint cancelled : 1;
guint clickable : 1;
guint completed : 1;
};
enum {
PROP_0,
PROP_ALLOW_CANCEL,
PROP_CLICKABLE,
PROP_ICON_NAME,
PROP_PERCENT,
PROP_PRIMARY_TEXT,
PROP_SECONDARY_TEXT
};
enum {
CANCELLED,
CLICKED,
COMPLETED,
DESCRIBE,
LAST_SIGNAL
};
static gpointer parent_class;
static gulong signals[LAST_SIGNAL];
static gboolean
activity_describe_accumulator (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer accu_data)
{
const gchar *string;
string = g_value_get_string (handler_return);
g_value_set_string (return_accu, string);
return (string == NULL);
}
static void
activity_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_ALLOW_CANCEL:
e_activity_set_allow_cancel (
E_ACTIVITY (object),
g_value_get_boolean (value));
return;
case PROP_CLICKABLE:
e_activity_set_clickable (
E_ACTIVITY (object),
g_value_get_boolean (value));
return;
case PROP_ICON_NAME:
e_activity_set_icon_name (
E_ACTIVITY (object),
g_value_get_string (value));
return;
case PROP_PERCENT:
e_activity_set_percent (
E_ACTIVITY (object),
g_value_get_double (value));
return;
case PROP_PRIMARY_TEXT:
e_activity_set_primary_text (
E_ACTIVITY (object),
g_value_get_string (value));
return;
case PROP_SECONDARY_TEXT:
e_activity_set_secondary_text (
E_ACTIVITY (object),
g_value_get_string (value));
return;
}
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
activity_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_ALLOW_CANCEL:
g_value_set_boolean (
value, e_activity_get_allow_cancel (
E_ACTIVITY (object)));
return;
case PROP_CLICKABLE:
g_value_set_boolean (
value, e_activity_get_clickable (
E_ACTIVITY (object)));
return;
case PROP_ICON_NAME:
g_value_set_string (
value, e_activity_get_icon_name (
E_ACTIVITY (object)));
return;
case PROP_PERCENT:
g_value_set_double (
value, e_activity_get_percent (
E_ACTIVITY (object)));
return;
case PROP_PRIMARY_TEXT:
g_value_set_string (
value, e_activity_get_primary_text (
E_ACTIVITY (object)));
return;
case PROP_SECONDARY_TEXT:
g_value_set_string (
value, e_activity_get_secondary_text (
E_ACTIVITY (object)));
return;
}
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
activity_finalize (GObject *object)
{
EActivityPrivate *priv;
priv = E_ACTIVITY_GET_PRIVATE (object);
g_free (priv->icon_name);
g_free (priv->primary_text);
g_free (priv->secondary_text);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
activity_cancelled (EActivity *activity)
{
activity->priv->cancelled = TRUE;
}
static void
activity_completed (EActivity *activity)
{
activity->priv->completed = TRUE;
}
static void
activity_clicked (EActivity *activity)
{
/* Allow subclasses to safely chain up. */
}
static gchar *
activity_describe (EActivity *activity)
{
GString *string;
const gchar *text;
gboolean cancelled;
gboolean completed;
gdouble percent;
string = g_string_sized_new (256);
text = e_activity_get_primary_text (activity);
cancelled = e_activity_is_cancelled (activity);
completed = e_activity_is_completed (activity);
percent = e_activity_get_percent (activity);
if (cancelled) {
/* Translators: This is a cancelled activity. */
g_string_printf (string, _("%s (cancelled)"), text);
} else if (completed) {
/* Translators: This is a completed activity. */
g_string_printf (string, _("%s (completed)"), text);
} else if (percent < 0.0) {
/* Translators: This is an activity whose percent
* complete is unknown. */
g_string_printf (string, _("%s..."), text);
} else {
/* Translators: This is an activity whose percent
* complete is known. */
g_string_printf (
string, _("%s (%d%% complete)"), text,
(gint) (percent * 100.0 + 0.5));
}
return g_string_free (string, FALSE);
}
static void
activity_class_init (EActivityClass *class)
{
GObjectClass *object_class;
parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (EActivityPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->set_property = activity_set_property;
object_class->get_property = activity_get_property;
object_class->finalize = activity_finalize;
class->cancelled = activity_cancelled;
class->completed = activity_completed;
class->clicked = activity_clicked;
class->describe = activity_describe;
g_object_class_install_property (
object_class,
PROP_ALLOW_CANCEL,
g_param_spec_boolean (
"allow-cancel",
NULL,
NULL,
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (
object_class,
PROP_CLICKABLE,
g_param_spec_boolean (
"clickable",
NULL,
NULL,
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (
object_class,
PROP_ICON_NAME,
g_param_spec_string (
"icon-name",
NULL,
NULL,
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (
object_class,
PROP_PERCENT,
g_param_spec_double (
"percent",
NULL,
NULL,
-G_MAXDOUBLE,
G_MAXDOUBLE,
-1.0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (
object_class,
PROP_PRIMARY_TEXT,
g_param_spec_string (
"primary-text",
NULL,
NULL,
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (
object_class,
PROP_SECONDARY_TEXT,
g_param_spec_string (
"secondary-text",
NULL,
NULL,
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
signals[CANCELLED] = g_signal_new (
"cancelled",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (EActivityClass, cancelled),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[CLICKED] = g_signal_new (
"clicked",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (EActivityClass, clicked),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[COMPLETED] = g_signal_new (
"completed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (EActivityClass, completed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[DESCRIBE] = g_signal_new (
"describe",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (EActivityClass, describe),
activity_describe_accumulator, NULL,
e_marshal_STRING__VOID,
G_TYPE_STRING, 0);
}
static void
activity_init (EActivity *activity)
{
activity->priv = E_ACTIVITY_GET_PRIVATE (activity);
}
GType
e_activity_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static const GTypeInfo type_info = {
sizeof (EActivityClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) activity_class_init,
(GClassFinalizeFunc) NULL,
NULL, /* class_data */
sizeof (EActivity),
0, /* n_preallocs */
(GInstanceInitFunc) activity_init,
NULL /* value_table */
};
type = g_type_register_static (
G_TYPE_OBJECT, "EActivity", &type_info, 0);
}
return type;
}
EActivity *
e_activity_new (const gchar *primary_text)
{
return g_object_new (
E_TYPE_ACTIVITY,
"primary-text", primary_text, NULL);
}
EActivity *
e_activity_newv (const gchar *format, ...)
{
EActivity *activity;
gchar *primary_text;
va_list args;
va_start (args, format);
primary_text = g_strdup_vprintf (format, args);
activity = e_activity_new (primary_text);
g_free (primary_text);
va_end (args);
return activity;
}
void
e_activity_cancel (EActivity *activity)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
if (!activity->priv->allow_cancel)
return;
if (activity->priv->cancelled)
return;
if (activity->priv->completed)
return;
g_signal_emit (activity, signals[CANCELLED], 0);
}
void
e_activity_complete (EActivity *activity)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
if (activity->priv->cancelled)
return;
if (activity->priv->completed)
return;
g_signal_emit (activity, signals[COMPLETED], 0);
}
void
e_activity_clicked (EActivity *activity)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
g_signal_emit (activity, signals[CLICKED], 0);
}
gchar *
e_activity_describe (EActivity *activity)
{
EActivityClass *class;
g_return_val_if_fail (E_IS_ACTIVITY (activity), NULL);
class = E_ACTIVITY_GET_CLASS (activity);
g_return_val_if_fail (class->describe != NULL, NULL);
return class->describe (activity);
}
gboolean
e_activity_is_cancelled (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), FALSE);
return activity->priv->cancelled;
}
gboolean
e_activity_is_completed (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), FALSE);
return activity->priv->completed;
}
gboolean
e_activity_get_allow_cancel (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), FALSE);
return activity->priv->allow_cancel;
}
void
e_activity_set_allow_cancel (EActivity *activity,
gboolean allow_cancel)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
activity->priv->allow_cancel = allow_cancel;
g_object_notify (G_OBJECT (activity), "allow-cancel");
}
gboolean
e_activity_get_clickable (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), FALSE);
return activity->priv->clickable;
}
void
e_activity_set_clickable (EActivity *activity,
gboolean clickable)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
activity->priv->clickable = clickable;
g_object_notify (G_OBJECT (activity), "clickable");
}
const gchar *
e_activity_get_icon_name (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), NULL);
return activity->priv->icon_name;
}
void
e_activity_set_icon_name (EActivity *activity,
const gchar *icon_name)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
g_free (activity->priv->icon_name);
activity->priv->icon_name = g_strdup (icon_name);
g_object_notify (G_OBJECT (activity), "icon-name");
}
gdouble
e_activity_get_percent (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), -1.0);
return activity->priv->percent;
}
void
e_activity_set_percent (EActivity *activity,
gdouble percent)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
activity->priv->percent = percent;
g_object_notify (G_OBJECT (activity), "percent");
}
const gchar *
e_activity_get_primary_text (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), NULL);
return activity->priv->primary_text;
}
void
e_activity_set_primary_text (EActivity *activity,
const gchar *primary_text)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
g_free (activity->priv->primary_text);
activity->priv->primary_text = g_strdup (primary_text);
g_object_notify (G_OBJECT (activity), "primary-text");
}
const gchar *
e_activity_get_secondary_text (EActivity *activity)
{
g_return_val_if_fail (E_IS_ACTIVITY (activity), NULL);
return activity->priv->secondary_text;
}
void
e_activity_set_secondary_text (EActivity *activity,
const gchar *secondary_text)
{
g_return_if_fail (E_IS_ACTIVITY (activity));
g_free (activity->priv->secondary_text);
activity->priv->secondary_text = g_strdup (secondary_text);
g_object_notify (G_OBJECT (activity), "secondary-text");
}
|