No issues found
1 /*
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with the program; if not, see <http://www.gnu.org/licenses/>
15 *
16 *
17 * Authors:
18 * Not Zed <notzed@lostzed.mmc.com.au>
19 * Jeffrey Stedfast <fejj@ximian.com>
20 *
21 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32 #include <glib/gi18n.h>
33
34 #include "em-filter-editor-folder-element.h"
35 #include "mail/em-folder-selection-button.h"
36 #include "mail/em-utils.h"
37 #include "shell/e-shell.h"
38 #include "filter/e-filter-part.h"
39 #include "libevolution-utils/e-alert.h"
40
41 #define EM_FILTER_EDITOR_FOLDER_ELEMENT_GET_PRIVATE(obj) \
42 (G_TYPE_INSTANCE_GET_PRIVATE \
43 ((obj), EM_TYPE_FILTER_EDITOR_FOLDER_ELEMENT, EMFilterEditorFolderElementPrivate))
44
45 struct _EMFilterEditorFolderElementPrivate {
46 EMailSession *session;
47 };
48
49 enum {
50 PROP_0,
51 PROP_SESSION
52 };
53
54 G_DEFINE_TYPE (
55 EMFilterEditorFolderElement,
56 em_filter_editor_folder_element,
57 EM_TYPE_FILTER_FOLDER_ELEMENT)
58
59 static void
60 filter_editor_folder_element_set_session (EMFilterEditorFolderElement *element,
61 EMailSession *session)
62 {
63 if (session == NULL) {
64 EShell *shell;
65 EShellBackend *shell_backend;
66 EMailBackend *backend;
67
68 shell = e_shell_get_default ();
69 shell_backend = e_shell_get_backend_by_name (shell, "mail");
70
71 backend = E_MAIL_BACKEND (shell_backend);
72 session = e_mail_backend_get_session (backend);
73 }
74
75 g_return_if_fail (E_IS_MAIL_SESSION (session));
76 g_return_if_fail (element->priv->session == NULL);
77
78 element->priv->session = g_object_ref (session);
79 }
80
81 static void
82 filter_editor_folder_element_set_property (GObject *object,
83 guint property_id,
84 const GValue *value,
85 GParamSpec *pspec)
86 {
87 switch (property_id) {
88 case PROP_SESSION:
89 filter_editor_folder_element_set_session (
90 EM_FILTER_EDITOR_FOLDER_ELEMENT (object),
91 g_value_get_object (value));
92 return;
93 }
94
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
96 }
97
98 static void
99 filter_editor_folder_element_get_property (GObject *object,
100 guint property_id,
101 GValue *value,
102 GParamSpec *pspec)
103 {
104 switch (property_id) {
105 case PROP_SESSION:
106 g_value_set_object (
107 value,
108 em_filter_editor_folder_element_get_session (
109 EM_FILTER_EDITOR_FOLDER_ELEMENT (object)));
110 return;
111 }
112
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114 }
115
116 static void
117 filter_editor_folder_element_selected_cb (EMFolderSelectionButton *button,
118 EMFilterEditorFolderElement *ff)
119 {
120 GtkWidget *toplevel;
121 const gchar *uri;
122
123 uri = em_folder_selection_button_get_folder_uri (button);
124
125 em_filter_folder_element_set_uri ((EMFilterFolderElement *) ff, uri);
126
127 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
128 gtk_window_present (GTK_WINDOW (toplevel));
129 }
130
131 static void
132 filter_editor_folder_element_dispose (GObject *object)
133 {
134 EMFilterEditorFolderElementPrivate *priv;
135
136 priv = EM_FILTER_EDITOR_FOLDER_ELEMENT_GET_PRIVATE (object);
137 if (priv->session != NULL) {
138 g_object_unref (priv->session);
139 priv->session = NULL;
140 }
141
142 /* Chain up to parent's dispose() method. */
143 G_OBJECT_CLASS (em_filter_editor_folder_element_parent_class)->dispose (object);
144 }
145
146 static GtkWidget *
147 filter_editor_folder_element_get_widget (EFilterElement *fe)
148 {
149 EMFilterEditorFolderElement *ff = (EMFilterEditorFolderElement *) fe;
150 EMailSession *session;
151 GtkWidget *button;
152 const gchar *uri;
153
154 session = em_filter_editor_folder_element_get_session (ff);
155 uri = em_filter_folder_element_get_uri ((EMFilterFolderElement *) ff);
156
157 button = em_folder_selection_button_new (
158 session, _("Select Folder"), NULL);
159 em_folder_selection_button_set_folder_uri (
160 EM_FOLDER_SELECTION_BUTTON (button), uri);
161 gtk_widget_show (button);
162
163 g_signal_connect (
164 button, "selected",
165 G_CALLBACK (filter_editor_folder_element_selected_cb), ff);
166
167 return button;
168 }
169
170 static void
171 em_filter_editor_folder_element_class_init (EMFilterEditorFolderElementClass *class)
172 {
173 GObjectClass *object_class;
174 EFilterElementClass *filter_element_class;
175
176 g_type_class_add_private (class, sizeof (EMFilterEditorFolderElementPrivate));
177
178 object_class = G_OBJECT_CLASS (class);
179 object_class->set_property = filter_editor_folder_element_set_property;
180 object_class->get_property = filter_editor_folder_element_get_property;
181 object_class->dispose = filter_editor_folder_element_dispose;
182
183 filter_element_class = E_FILTER_ELEMENT_CLASS (class);
184 filter_element_class->get_widget = filter_editor_folder_element_get_widget;
185
186 g_object_class_install_property (
187 object_class,
188 PROP_SESSION,
189 g_param_spec_object (
190 "session",
191 NULL,
192 NULL,
193 E_TYPE_MAIL_SESSION,
194 G_PARAM_READWRITE |
195 G_PARAM_CONSTRUCT_ONLY));
196 }
197
198 static void
199 em_filter_editor_folder_element_init (EMFilterEditorFolderElement *element)
200 {
201 element->priv = EM_FILTER_EDITOR_FOLDER_ELEMENT_GET_PRIVATE (element);
202 }
203
204 EFilterElement *
205 em_filter_editor_folder_element_new (EMailSession *session)
206 {
207 g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
208
209 return g_object_new (
210 EM_TYPE_FILTER_EDITOR_FOLDER_ELEMENT,
211 "session", session, NULL);
212 }
213
214 EMailSession *
215 em_filter_editor_folder_element_get_session (EMFilterEditorFolderElement *element)
216 {
217 g_return_val_if_fail (EM_IS_FILTER_EDITOR_FOLDER_ELEMENT (element), NULL);
218
219 return element->priv->session;
220 }