~macslow/notify-osd/mouse-movement-monitor

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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
////////////////////////////////////////////////////////////////////////////////
//3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
//      10        20        30        40        50        60        70        80
//
// notify-osd
//
// notification.c - notification object storing attributes like title- and body-
//                  text, value, icon, id, sender-pid etc.
//
// Copyright 2009 Canonical Ltd.
//
// Authors:
//    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 3, as published
// by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranties of
// MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
// PURPOSE.  See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////

#include "notification.h"

G_DEFINE_TYPE (Notification, notification, G_TYPE_OBJECT);

enum
{
	PROP_DUMMY = 0,
	PROP_ID,
	PROP_TITLE,
	PROP_BODY,
	PROP_VALUE,
	PROP_ICON_THEMENAME,
	PROP_ICON_FILENAME,
	PROP_ICON_PIXBUF,
	PROP_ONSCREEN_TIME,
	PROP_SENDER_NAME,
	PROP_SENDER_PID,
	PROP_TIMESTAMP,
	PROP_URGENCY
};

#define GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), NOTIFICATION_TYPE, NotificationPrivate))

struct _NotificationPrivate {
	gint       id;                  // unique notification-id
	GString*   title;               // summary-text strdup'ed from setter
	GString*   body;                // body-text strdup'ed from setter
	gint       value;               // range: 0..100, special: -1, 101
	GString*   icon_themename;      // e.g. "notification-message-email"
	GString*   icon_filename;       // e.g. "/usr/share/icons/icon.png"
	GdkPixbuf* icon_pixbuf;         // from setter memcpy'ed pixbuf 
	gint       onscreen_time;       // time on-screen in ms
	GString*   sender_name;         // app-name, strdup'ed from setter
	gint       sender_pid;          // pid of sending application
	GTimeVal   timestamp;           // timestamp of  reception
	Urgency    urgency;             // urgency-level: low, normal, high
};

#define RETURN_GCHAR(n, string)\
	NotificationPrivate* priv;\
	g_return_val_if_fail (IS_NOTIFICATION (n), NULL);\
	priv = GET_PRIVATE (n);\
	if (!priv->string)\
		return NULL;\
	return GET_PRIVATE (n)->string->str;

#define SET_GCHAR(n, string)\
	NotificationPrivate* priv;\
	g_assert (IS_NOTIFICATION (n));\
	if (!string)\
		return;\
	priv = GET_PRIVATE (n);\
	if (priv->string)\
	{\
		g_string_free (priv->string, TRUE);\
		priv->string = NULL;\
	}\
	priv->string = g_string_new (string);

//-- private functions ---------------------------------------------------------

//-- internal functions --------------------------------------------------------

// this is what gets called if one does g_object_unref(bla)
static void
notification_dispose (GObject* gobject)
{
	Notification*        n;
	NotificationPrivate* priv;

	// sanity checks
	g_assert (gobject);
	n = NOTIFICATION (gobject);
	g_assert (n);
	g_assert (IS_NOTIFICATION (n));
	priv = GET_PRIVATE (n);
	g_assert (priv);

	// free any allocated resources
	if (priv->title)
	{
		g_string_free (priv->title, TRUE);
		priv->title = NULL;
	}

	if (priv->body)
	{
		g_string_free (priv->body, TRUE);
		priv->body = NULL;
	}

	if (priv->icon_themename)
	{
		g_string_free (priv->icon_themename, TRUE);
		priv->icon_themename = NULL;
	}

	if (priv->icon_filename)
	{
		g_string_free (priv->icon_filename, TRUE);
		priv->icon_filename = NULL;
	}

	if (priv->icon_pixbuf)
	{
		g_object_unref (priv->icon_pixbuf);
	}

	if (priv->sender_name)
	{
		g_string_free (priv->sender_name, TRUE);
		priv->sender_name = NULL;
	}

	// chain up to the parent class
	G_OBJECT_CLASS (notification_parent_class)->dispose (gobject);
}

static void
notification_finalize (GObject* gobject)
{
	// chain up to the parent class
	G_OBJECT_CLASS (notification_parent_class)->finalize (gobject);
}

static void
notification_init (Notification* n)
{
	// nothing to be done here for now
}

static void
notification_get_property (GObject*    gobject,
			   guint       prop,
			   GValue*     value,
			   GParamSpec* spec)
{
	Notification* n = NOTIFICATION (gobject);

	// sanity checks are done in public getters

	switch (prop)
	{
		case PROP_ID:
			g_value_set_int (value, notification_get_id (n));
		break;

		case PROP_TITLE:
			g_value_set_string (value, notification_get_title (n));
		break;

		case PROP_BODY:
			g_value_set_string (value, notification_get_body (n));
		break;

		case PROP_VALUE:
			g_value_set_int (value, notification_get_value (n));
		break;

		case PROP_ICON_THEMENAME:
			g_value_set_string (value,
					    notification_get_icon_themename (n));
		break;

		case PROP_ICON_FILENAME:
			g_value_set_string (value,
					    notification_get_icon_filename (n));
		break;

		case PROP_ICON_PIXBUF:
			g_value_set_pointer (value,
					     notification_get_icon_pixbuf (n));
		break;

		case PROP_ONSCREEN_TIME:
			g_value_set_int (value,
					 notification_get_onscreen_time (n));
		break;

		case PROP_SENDER_NAME:
			g_value_set_string (value,
					    notification_get_sender_name (n));
		break;

		case PROP_SENDER_PID:
			g_value_set_int (value,
					 notification_get_sender_pid (n));
		break;

		case PROP_TIMESTAMP:
			g_value_set_pointer (value,
					     notification_get_timestamp (n));
		break;

		case PROP_URGENCY:
			g_value_set_int (value,
					 notification_get_urgency (n));
		break;

		default :
			G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop, spec);
		break;
	}
}

static void
notification_set_property (GObject*      gobject,
			   guint         prop,
			   const GValue* value,
			   GParamSpec*   spec)
{
	Notification* n = NOTIFICATION (gobject);

	// sanity checks are done in the public setters

	switch (prop)
	{
		case PROP_ID:
			notification_set_id (n, g_value_get_int (value));
		break;

		case PROP_TITLE:
			notification_set_title (n, g_value_get_string (value));
		break;

		case PROP_BODY:
			notification_set_body (n, g_value_get_string (value));
		break;

		case PROP_VALUE:
			notification_set_value (n, g_value_get_int (value));
		break;

		case PROP_ICON_THEMENAME:
			notification_set_icon_themename (
				n,
				g_value_get_string (value));
		break;

		case PROP_ICON_FILENAME:
			notification_set_icon_filename (
				n,
				g_value_get_string (value));
		break;

		case PROP_ICON_PIXBUF:
			notification_set_icon_pixbuf (
				n,
				g_value_get_pointer (value));
		break;

		case PROP_ONSCREEN_TIME:
			notification_set_onscreen_time (
				n,
				g_value_get_int (value));
		break;

		case PROP_SENDER_NAME:
			notification_set_sender_name (
				n,
				g_value_get_string (value));
		break;

		case PROP_SENDER_PID:
			notification_set_sender_pid (
				n,
				g_value_get_int (value));
		break;

		case PROP_TIMESTAMP:
			notification_set_timestamp (
				n,
				g_value_get_pointer (value));
		break;

		case PROP_URGENCY:
			notification_set_urgency (
				n,
				g_value_get_int (value));
		break;

		default :
			G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop, spec);
		break;
	}
}

static void
notification_class_init (NotificationClass* klass)
{
	GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
	GParamSpec*   property_id;
	GParamSpec*   property_title;
	GParamSpec*   property_body;
	GParamSpec*   property_value;
	GParamSpec*   property_icon_themename;
	GParamSpec*   property_icon_filename;
	GParamSpec*   property_icon_pixbuf;
	GParamSpec*   property_onscreen_time;
	GParamSpec*   property_sender_name;
	GParamSpec*   property_sender_pid;
	GParamSpec*   property_timestamp;
	GParamSpec*   property_urgency;

	g_type_class_add_private (klass, sizeof (NotificationPrivate));

	gobject_class->dispose      = notification_dispose;
	gobject_class->finalize     = notification_finalize;
	gobject_class->get_property = notification_get_property;
	gobject_class->set_property = notification_set_property;

	property_id = g_param_spec_int ("id",
					"id",
					"unique notification id",
					-1,
					G_MAXINT,
					-1,
					G_PARAM_CONSTRUCT |
					G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_ID,
					 property_id);

	property_title = g_param_spec_string ("title",
					      "title",
					      "title-text of a notification",
					      NULL,
					      G_PARAM_CONSTRUCT |
					      G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_TITLE,
					 property_title);

	property_body = g_param_spec_string ("body",
					     "body",
					     "body-text of a notification",
					     NULL,
					     G_PARAM_CONSTRUCT |
					     G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_BODY,
					 property_body);

	property_value = g_param_spec_int ("value",
					   "value",
					   "value between -1..101 to render",
					   -2,
					   101,
					   -2,
					   G_PARAM_CONSTRUCT |
					   G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_VALUE,
					 property_value);

	property_icon_themename = g_param_spec_string (
					"icon-themename",
					"icon-themename",
					"theme-name of icon to use",
					NULL,
					G_PARAM_CONSTRUCT |
					G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_ICON_THEMENAME,
					 property_icon_themename);

	property_icon_filename = g_param_spec_string (
					"icon-filename",
					"icon-filename",
					"file-name of icon to use",
					NULL,
					G_PARAM_CONSTRUCT |
					G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_ICON_FILENAME,
					 property_icon_filename);

	property_icon_pixbuf = g_param_spec_pointer ("icon-pixbuf",
						     "icon-pixbuf",
						     "pixbuf of icon to use",
						     G_PARAM_CONSTRUCT |
						     G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_ICON_PIXBUF,
					 property_icon_pixbuf);

	property_onscreen_time = g_param_spec_int ("onscreen-time",
						   "onscreen-time",
						   "time on screen sofar",
						   0,
						   G_MAXINT,
						   0,
						   G_PARAM_CONSTRUCT |
						   G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_ONSCREEN_TIME,
					 property_onscreen_time);

	property_sender_name = g_param_spec_string (
					"sender-name",
					"sender-name",
					"name of sending application",
					NULL,
					G_PARAM_CONSTRUCT |
					G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_SENDER_NAME,
					 property_sender_name);

	property_sender_pid = g_param_spec_int (
					"sender-pid",
					"sender-pid",
					"process ID of sending application",
					0,
					G_MAXSHORT,
					0,
					G_PARAM_CONSTRUCT |
					G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_SENDER_PID,
					 property_sender_pid);

	property_timestamp = g_param_spec_pointer ("timestamp",
						   "timestamp",
						   "timestamp of reception",
					 	   G_PARAM_CONSTRUCT |
						   G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_TIMESTAMP,
					 property_timestamp);

	property_urgency = g_param_spec_int ("urgency",
					     "urgency",
					     "urgency-level of notification",
					     URGENCY_LOW,
					     URGENCY_NONE,
					     URGENCY_NONE,
					     G_PARAM_CONSTRUCT |
					     G_PARAM_READWRITE);
	g_object_class_install_property (gobject_class,
					 PROP_URGENCY,
					 property_urgency);
}

//-- public functions ----------------------------------------------------------

Notification*
notification_new ()
{
	const GTimeVal timestamp = {0};

	return g_object_new (NOTIFICATION_TYPE,
	                     "timestamp", &timestamp,
	                     NULL);
}

gint
notification_get_id (Notification* n)
{
	g_return_val_if_fail (IS_NOTIFICATION (n), -1);

	return GET_PRIVATE (n)->id;
}

// -1 is meant to "unset" the id, 0 is the smallest possible PID, but very
// unlikely to ever be passed around
void
notification_set_id (Notification* n,
		     gint          id)
{
	g_assert (IS_NOTIFICATION (n));

	// IDs can't be negative, except for -1 to indicate it's "unset"
	if (id < -1)
		return;

	GET_PRIVATE (n)->id = id;
}

gchar*
notification_get_title (Notification* n)
{
	RETURN_GCHAR (n, title)
}

void
notification_set_title (Notification* n,
			const gchar*  title)
{
	SET_GCHAR (n, title)
}

gchar*
notification_get_body (Notification* n)
{
	RETURN_GCHAR (n, body)
}

void
notification_set_body (Notification* n,
		       const gchar*  body)
{
	SET_GCHAR (n, body)
}

// the allowed range for stored values is -1..101, thus a return-value of -2
// indicates an error on behalf of the caller
gint
notification_get_value (Notification* n)
{
	g_return_val_if_fail (IS_NOTIFICATION (n), -2);

	return GET_PRIVATE (n)->value;
}

// valid range is -1..101, -2 is used to indicate it's "unset"
void
notification_set_value (Notification* n,
			gint          value)
{
	NotificationPrivate* priv;

	g_assert (IS_NOTIFICATION (n));

	priv = GET_PRIVATE (n);

	// this is used to indicate the "unset" state
	if (value == -2)
	{
		priv->value = value;
		return;
	}

	// don't store any values outside of allowed range -1..101
	if (value <= NOTIFICATION_VALUE_MIN_ALLOWED)
	{
		priv->value = NOTIFICATION_VALUE_MIN_ALLOWED;
		return;
	}

	if (value >= NOTIFICATION_VALUE_MAX_ALLOWED)
	{
		priv->value = NOTIFICATION_VALUE_MAX_ALLOWED;
		return;
	}

	priv->value = value;
}

gchar*
notification_get_icon_themename (Notification* n)
{
	RETURN_GCHAR (n, icon_themename)
}

void
notification_set_icon_themename (Notification* n,
				 const gchar*  icon_themename)
{
	SET_GCHAR (n, icon_themename)
}

gchar*
notification_get_icon_filename (Notification* n)
{
	RETURN_GCHAR (n, icon_filename)
}

void
notification_set_icon_filename (Notification* n,
				const gchar*  icon_filename)
{
	SET_GCHAR (n, icon_filename)
}

GdkPixbuf*
notification_get_icon_pixbuf (Notification* n)
{
	NotificationPrivate* priv;

	g_return_val_if_fail (IS_NOTIFICATION (n), NULL);

	priv = GET_PRIVATE (n);

	if (!priv->icon_pixbuf)
		return NULL;

	return GET_PRIVATE (n)->icon_pixbuf;
}

void
notification_set_icon_pixbuf (Notification*    n,
			      const GdkPixbuf* icon_pixbuf)
{
	NotificationPrivate* priv;

	// sanity checks
	g_assert (IS_NOTIFICATION (n));
	if (!icon_pixbuf)
		return;

	priv = GET_PRIVATE (n);

	// free any previous stored pixbuf
	if (priv->icon_pixbuf)
	{
		g_object_unref (priv->icon_pixbuf);
		priv->icon_pixbuf = NULL;
	}

	// create a new/private copy of the supplied pixbuf
	priv->icon_pixbuf = gdk_pixbuf_copy (icon_pixbuf);
}

// a return-value of -1 indicates an error on behalf of the caller, a
// return-value of 0 would indicate that a notification has not been displayed
// yet
gint
notification_get_onscreen_time (Notification* n)
{
	g_return_val_if_fail (IS_NOTIFICATION (n), -1);

	return GET_PRIVATE (n)->onscreen_time;
}

// only onscreen_time values <= 0 are considered valid
void
notification_set_onscreen_time (Notification* n,
				gint          onscreen_time)
{
	NotificationPrivate* priv;

	g_assert (IS_NOTIFICATION (n));

	priv = GET_PRIVATE (n);

	// avoid storing negative values
	if (onscreen_time < 0)
		return;

	// onscreen-time can only increase not decrease
	if (priv->onscreen_time > onscreen_time)
		return;

	// you made it upto here, congratulations... let's store the new value
	priv->onscreen_time = onscreen_time;
}

gchar*
notification_get_sender_name (Notification* n)
{
	RETURN_GCHAR (n, sender_name)
}

void
notification_set_sender_name (Notification* n,
			      const gchar*  sender_name)
{
	SET_GCHAR (n, sender_name)
}

// a return-value of 0 indicates an error on behalf of the caller, PIDs are
// never negative
gint
notification_get_sender_pid (Notification* n)
{
	g_return_val_if_fail (IS_NOTIFICATION (n), 0);

	return GET_PRIVATE (n)->sender_pid;
}

void
notification_set_sender_pid (Notification* n,
			     const gint    sender_pid)
{
	g_assert (IS_NOTIFICATION (n));

	// it's hardly possible we'll get a notification from init, but anyway
	if (sender_pid <= 0)
		return;

	GET_PRIVATE (n)->sender_pid = sender_pid;
}

GTimeVal*
notification_get_timestamp (Notification* n)
{
	NotificationPrivate* priv;

	g_return_val_if_fail (IS_NOTIFICATION (n), NULL);

	priv = GET_PRIVATE (n);

	return &priv->timestamp;
}

void
notification_set_timestamp (Notification*   n,
			    const GTimeVal* timestamp)
{
	NotificationPrivate* priv;

	// sanity checks
	g_assert (IS_NOTIFICATION (n));
	if (!timestamp)
		return;

	priv = GET_PRIVATE (n);

	// don't store older timestamp over newer one
	if (priv->timestamp.tv_sec > timestamp->tv_sec)
		return;

	if (priv->timestamp.tv_sec == timestamp->tv_sec)
		if (priv->timestamp.tv_usec > timestamp->tv_usec)
			return;

	// new timestamp certainly more current that stored one
	priv->timestamp.tv_sec  = timestamp->tv_sec;
	priv->timestamp.tv_usec = timestamp->tv_usec;
}

gint
notification_get_urgency (Notification* n)
{
	g_return_val_if_fail (IS_NOTIFICATION (n), URGENCY_NONE);

	return GET_PRIVATE (n)->urgency;
}

void
notification_set_urgency (Notification* n,
			  Urgency       urgency)
{
	NotificationPrivate* priv;

	g_assert (IS_NOTIFICATION (n));

	priv = GET_PRIVATE (n);

	// URGENCY_NONE is meant to indicate the "unset" state
	if (urgency == URGENCY_NONE)
	{
		priv->urgency = URGENCY_NONE;
		return;
	}

	// don't store any values outside of allowed range LOW..HIGH
	if (urgency != URGENCY_LOW    &&
	    urgency != URGENCY_NORMAL &&
	    urgency != URGENCY_HIGH)
		return;

	GET_PRIVATE (n)->urgency = urgency;
}