evolution-3.6.4/modules/offline-alert/evolution-offline-alert.c

No issues found

  1 /*
  2  * evolution-offline-alert.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #ifdef HAVE_CONFIG_H
 20 #include <config.h>
 21 #endif
 22 
 23 #include <libebackend/libebackend.h>
 24 
 25 #include <shell/e-shell-view.h>
 26 #include <shell/e-shell-window-actions.h>
 27 #include <libevolution-utils/e-alert-sink.h>
 28 
 29 /* Standard GObject macros */
 30 #define E_TYPE_OFFLINE_ALERT \
 31 	(e_offline_alert_get_type ())
 32 #define E_OFFLINE_ALERT(obj) \
 33 	(G_TYPE_CHECK_INSTANCE_CAST \
 34 	((obj), E_TYPE_OFFLINE_ALERT, EOfflineAlert))
 35 
 36 typedef struct _EOfflineAlert EOfflineAlert;
 37 typedef struct _EOfflineAlertClass EOfflineAlertClass;
 38 
 39 struct _EOfflineAlert {
 40 	EExtension parent;
 41 	gpointer alert;  /* weak pointer */
 42 };
 43 
 44 struct _EOfflineAlertClass {
 45 	EExtensionClass parent_class;
 46 };
 47 
 48 /* Module Entry Points */
 49 void e_module_load (GTypeModule *type_module);
 50 void e_module_unload (GTypeModule *type_module);
 51 
 52 /* Forward Declarations */
 53 GType e_offline_alert_get_type (void);
 54 
 55 G_DEFINE_DYNAMIC_TYPE (EOfflineAlert, e_offline_alert, E_TYPE_EXTENSION)
 56 
 57 static EShell *
 58 offline_alert_get_shell (EOfflineAlert *extension)
 59 {
 60 	EExtensible *extensible;
 61 
 62 	extensible = e_extension_get_extensible (E_EXTENSION (extension));
 63 
 64 	return E_SHELL (extensible);
 65 }
 66 
 67 static void
 68 offline_alert_online_cb (EShell *shell,
 69                          GParamSpec *pspec,
 70                          EOfflineAlert *extension)
 71 {
 72 	if (!e_shell_get_online (shell))
 73 		return;
 74 
 75 	if (extension->alert != NULL)
 76 		e_alert_response (extension->alert, GTK_RESPONSE_OK);
 77 }
 78 
 79 static void
 80 offline_alert_network_available_cb (EShell *shell,
 81                                     GParamSpec *pspec,
 82                                     EOfflineAlert *extension)
 83 {
 84 	if (e_shell_get_network_available (shell)) {
 85 		if (extension->alert != NULL)
 86 			e_alert_response (extension->alert, GTK_RESPONSE_OK);
 87 		return;
 88 	}
 89 
 90 	if (!e_shell_get_online (shell))
 91 		return;
 92 
 93 	g_return_if_fail (extension->alert == NULL);
 94 
 95 	extension->alert = e_alert_new ("offline-alert:no-network", NULL);
 96 
 97 	g_object_add_weak_pointer (
 98 		G_OBJECT (extension->alert), &extension->alert);
 99 
100 	e_shell_submit_alert (shell, extension->alert);
101 
102 	g_object_unref (extension->alert);
103 }
104 
105 static void
106 offline_alert_window_added_cb (GtkApplication *application,
107                                GtkWindow *window,
108                                EOfflineAlert *extension)
109 {
110 	EShell *shell = E_SHELL (application);
111 	GtkAction *action;
112 
113 	if (!E_IS_SHELL_WINDOW (window))
114 		return;
115 
116 	/* Connect these signals after we have the first EShellWindow
117 	 * to avoid false-positive signals during EShell initialization. */
118 
119 	g_signal_connect (
120 		shell, "notify::online",
121 		G_CALLBACK (offline_alert_online_cb), extension);
122 
123 	g_signal_connect (
124 		shell, "notify::network-available",
125 		G_CALLBACK (offline_alert_network_available_cb), extension);
126 
127 	g_signal_handlers_disconnect_by_func (
128 		shell, offline_alert_window_added_cb, extension);
129 
130 	if (e_shell_get_online (shell))
131 		return;
132 
133 	g_return_if_fail (extension->alert == NULL);
134 
135 	/* This alert only shows at startup, not when the user
136 	 * chooses to work offline.  That's why we only wanted
137 	 * the first EShellWindow. */
138 
139 	action = E_SHELL_WINDOW_ACTION_WORK_ONLINE (window);
140 
141 	extension->alert = e_alert_new (e_shell_get_network_available (shell) ? "offline-alert:offline" : "offline-alert:no-network", NULL);
142 	e_alert_add_action (extension->alert, action, GTK_RESPONSE_NONE);
143 
144 	g_object_add_weak_pointer (
145 		G_OBJECT (extension->alert), &extension->alert);
146 
147 	e_shell_submit_alert (shell, extension->alert);
148 
149 	g_object_unref (extension->alert);
150 }
151 
152 static void
153 offline_alert_dispose (GObject *object)
154 {
155 	EOfflineAlert *extension;
156 
157 	extension = E_OFFLINE_ALERT (object);
158 
159 	if (extension->alert != NULL) {
160 		g_object_remove_weak_pointer (
161 			G_OBJECT (extension->alert), &extension->alert);
162 		extension->alert = NULL;
163 	}
164 
165 	/* Chain up to parent's dispose() method. */
166 	G_OBJECT_CLASS (e_offline_alert_parent_class)->dispose (object);
167 }
168 
169 static void
170 offline_alert_constructed (GObject *object)
171 {
172 	EShell *shell;
173 	EOfflineAlert *extension;
174 
175 	extension = E_OFFLINE_ALERT (object);
176 	shell = offline_alert_get_shell (extension);
177 
178 	/* Watch for the first EShellWindow. */
179 	g_signal_connect (
180 		shell, "window-added",
181 		G_CALLBACK (offline_alert_window_added_cb), extension);
182 
183 	/* Chain up to parent's constructed() method. */
184 	G_OBJECT_CLASS (e_offline_alert_parent_class)->constructed (object);
185 }
186 
187 static void
188 e_offline_alert_class_init (EOfflineAlertClass *class)
189 {
190 	GObjectClass *object_class;
191 	EExtensionClass *extension_class;
192 
193 	object_class = G_OBJECT_CLASS (class);
194 	object_class->dispose = offline_alert_dispose;
195 	object_class->constructed = offline_alert_constructed;
196 
197 	extension_class = E_EXTENSION_CLASS (class);
198 	extension_class->extensible_type = E_TYPE_SHELL;
199 }
200 
201 static void
202 e_offline_alert_class_finalize (EOfflineAlertClass *class)
203 {
204 }
205 
206 static void
207 e_offline_alert_init (EOfflineAlert *extension)
208 {
209 }
210 
211 G_MODULE_EXPORT void
212 e_module_load (GTypeModule *type_module)
213 {
214 	e_offline_alert_register_type (type_module);
215 }
216 
217 G_MODULE_EXPORT void
218 e_module_unload (GTypeModule *type_module)
219 {
220 }