~ubuntu-branches/ubuntu/trusty/modem-manager-gui/trusty-backports

« back to all changes in this revision

Viewing changes to src/notifications.c

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-30 12:51:59 UTC
  • Revision ID: package-import@ubuntu.com-20130730125159-flzv882fhuzhmfmi
Tags: upstream-0.0.16
ImportĀ upstreamĀ versionĀ 0.0.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      notifications.c
 
3
 *      
 
4
 *      Copyright 2013 Alex <alex@linuxonly.ru>
 
5
 *      
 
6
 *      This program is free software: you can redistribute it and/or modify
 
7
 *      it under the terms of the GNU General Public License as published by
 
8
 *      the Free Software Foundation; either version 3 of the License, or
 
9
 *      (at your option) any later version.
 
10
 *      
 
11
 *      This program is distributed in the hope that it will be useful,
 
12
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *      GNU General Public License for more details.
 
15
 *      
 
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/>.
 
18
 */
 
19
 
 
20
#include <stdlib.h>
 
21
#include <stdio.h>
 
22
#include <glib.h>
 
23
#include <gmodule.h>
 
24
#include <gtk/gtk.h>
 
25
 
 
26
#include "notifications.h"
 
27
#include "resources.h"
 
28
 
 
29
 
 
30
mmgui_notifications_t mmgui_notifications_new(mmgui_libpaths_cache_t libcache)
 
31
{
 
32
        mmgui_notifications_t notifications;
 
33
        gboolean libopened;
 
34
        GtkSettings *gtksettings;
 
35
        gchar *gtksoundtheme;
 
36
                
 
37
        notifications = g_new0(struct _mmgui_notifications, 1);
 
38
        
 
39
        //libnotify
 
40
        notifications->notifymodule = NULL;
 
41
        
 
42
        //Open module
 
43
        notifications->notifymodule = g_module_open(mmgui_libpaths_cache_get_library_name(libcache, "libnotify"), G_MODULE_BIND_LAZY);
 
44
        
 
45
        if (notifications->notifymodule != NULL) {
 
46
                libopened = TRUE;
 
47
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_init", (gpointer *)&(notifications->notify_init));
 
48
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_notification_new", (gpointer *)&(notifications->notify_notification_new));
 
49
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_notification_set_timeout", (gpointer *)&(notifications->notify_notification_set_timeout));
 
50
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_notification_set_hint", (gpointer *)&(notifications->notify_notification_set_hint));
 
51
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_notification_set_image_from_pixbuf", (gpointer *)&(notifications->notify_notification_set_image_from_pixbuf));
 
52
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_notification_set_category", (gpointer *)&(notifications->notify_notification_set_category));
 
53
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_notification_set_urgency", (gpointer *)&(notifications->notify_notification_set_urgency));
 
54
                libopened = libopened && g_module_symbol(notifications->notifymodule, "notify_notification_show", (gpointer *)&(notifications->notify_notification_show));
 
55
                //If some functions not exported, close library
 
56
                if (!libopened) {
 
57
                        notifications->notify_init = NULL;
 
58
                        notifications->notify_notification_new = NULL;
 
59
                        notifications->notify_notification_set_timeout = NULL;
 
60
                        notifications->notify_notification_set_hint = NULL;
 
61
                        notifications->notify_notification_set_image_from_pixbuf = NULL;
 
62
                        notifications->notify_notification_set_category = NULL;
 
63
                        notifications->notify_notification_set_urgency = NULL;
 
64
                        notifications->notify_notification_show = NULL;
 
65
                        //Close module
 
66
                        g_module_close(notifications->notifymodule);
 
67
                        notifications->notifymodule = NULL;
 
68
                } else {
 
69
                        //Initialize libnotify
 
70
                        (notifications->notify_init)("Modem Manager GUI");
 
71
                }
 
72
        }
 
73
        
 
74
        //libcanberra
 
75
        notifications->canberramodule = NULL;
 
76
        notifications->cacontext = NULL;
 
77
        
 
78
        //Open module
 
79
        notifications->canberramodule = g_module_open(mmgui_libpaths_cache_get_library_name(libcache, "libcanberra"), G_MODULE_BIND_LAZY);
 
80
        
 
81
        if (notifications->canberramodule != NULL) {
 
82
                libopened = TRUE;
 
83
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_context_create", (gpointer *)&(notifications->ca_context_create));
 
84
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_context_destroy", (gpointer *)&(notifications->ca_context_destroy));
 
85
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_context_play", (gpointer *)&(notifications->ca_context_play));
 
86
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_context_change_props", (gpointer *)&(notifications->ca_context_change_props));
 
87
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_proplist_create", (gpointer *)&(notifications->ca_proplist_create));
 
88
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_proplist_destroy", (gpointer *)&(notifications->ca_proplist_destroy));
 
89
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_proplist_sets", (gpointer *)&(notifications->ca_proplist_sets));
 
90
                libopened = libopened && g_module_symbol(notifications->canberramodule, "ca_context_play_full", (gpointer *)&(notifications->ca_context_play_full));
 
91
                //If some functions not exported, close library
 
92
                if (!libopened) {
 
93
                        notifications->ca_context_create = NULL;
 
94
                        notifications->ca_context_destroy = NULL;
 
95
                        notifications->ca_context_play = NULL;
 
96
                        notifications->ca_context_change_props = NULL;
 
97
                        notifications->ca_proplist_create = NULL;
 
98
                        notifications->ca_proplist_destroy = NULL;
 
99
                        notifications->ca_proplist_sets = NULL;
 
100
                        notifications->ca_context_play_full = NULL;
 
101
                        //Close module
 
102
                        g_module_close(notifications->canberramodule);
 
103
                        notifications->canberramodule = NULL;
 
104
                        notifications->cacontext = NULL;
 
105
                } else {
 
106
                        //Initialize libnotify
 
107
                        (notifications->ca_context_create)(&(notifications->cacontext));
 
108
                        gtksettings = gtk_settings_get_default();
 
109
                        gtksoundtheme = NULL;
 
110
                        g_object_get(gtksettings, "gtk-sound-theme-name", &gtksoundtheme, NULL);
 
111
                        if (gtksoundtheme != NULL) {
 
112
                                (notifications->ca_context_change_props)(notifications->cacontext, "canberra.xdg-theme.name", gtksoundtheme, NULL);
 
113
                                g_free(gtksoundtheme);
 
114
                        }
 
115
                }
 
116
        }
 
117
        
 
118
        return notifications;
 
119
}
 
120
 
 
121
gboolean mmgui_notifications_show(mmgui_notifications_t notifications, gchar *caption, gchar *text, enum _mmgui_notifications_sound sound)
 
122
{
 
123
        gpointer notification;
 
124
        gint caresult;
 
125
        ca_proplist *caproplist;
 
126
                
 
127
        if (notifications == NULL) return FALSE;
 
128
        
 
129
        if ((caption != NULL) && (text != NULL) && (notifications->notifymodule != NULL)) {
 
130
                notification = (notifications->notify_notification_new)(caption, text, NULL);
 
131
                if (notification != NULL) {
 
132
                        (notifications->notify_notification_set_timeout)(notification, 3000);
 
133
                        (notifications->notify_notification_show)(notification, NULL);
 
134
                }
 
135
        }
 
136
        
 
137
        if ((sound != MMGUI_NOTIFICATIONS_SOUND_NONE) && (notifications->canberramodule != NULL) && (notifications->cacontext != NULL)) {
 
138
                (notifications->ca_proplist_create)(&caproplist);
 
139
                switch (sound) {
 
140
                        case MMGUI_NOTIFICATIONS_SOUND_MESSAGE:
 
141
                                (notifications->ca_proplist_sets)(caproplist, "media.filename", RESOURCE_SOUND_MESSAGE);
 
142
                                (notifications->ca_proplist_sets)(caproplist, "media.role", "event");
 
143
                                break;
 
144
                        case MMGUI_NOTIFICATIONS_SOUND_INFO:
 
145
                                (notifications->ca_proplist_sets)(caproplist, "event.id", "dialog-info");
 
146
                                (notifications->ca_proplist_sets)(caproplist, "media.role", "event");
 
147
                                break;
 
148
                        case MMGUI_NOTIFICATIONS_SOUND_NONE:
 
149
                                break;
 
150
                        default:
 
151
                                (notifications->ca_proplist_sets)(caproplist, "event.id", "dialog-error");
 
152
                                (notifications->ca_proplist_sets)(caproplist, "media.role", "event");
 
153
                                break;
 
154
                }
 
155
                caresult = (notifications->ca_context_play_full)(notifications->cacontext, 0, caproplist, NULL, NULL);
 
156
                (notifications->ca_proplist_destroy)(caproplist);
 
157
        }
 
158
        
 
159
        return TRUE;
 
160
}
 
161
 
 
162
void mmgui_notifications_close(mmgui_notifications_t notifications)
 
163
{
 
164
        if (notifications == NULL) return;
 
165
        
 
166
        if (notifications->notifymodule != NULL) {
 
167
                //First close context
 
168
                if (notifications->cacontext != NULL) {
 
169
                        (notifications->ca_context_destroy)(notifications->cacontext);
 
170
                }
 
171
                //Then unload module
 
172
                g_module_close(notifications->notifymodule);
 
173
                notifications->notifymodule = NULL;
 
174
        }
 
175
        
 
176
        if (notifications->canberramodule != NULL) {
 
177
                //Only module unload needed
 
178
                g_module_close(notifications->canberramodule);
 
179
                notifications->canberramodule = NULL;
 
180
        }
 
181
        
 
182
        g_free(notifications);
 
183
}