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 #include <shell/e-shell.h>
31 #include "mail/em-utils.h"
32 #include "em-vfolder-editor-context.h"
33 #include "em-vfolder-editor-rule.h"
34 #include "filter/e-filter-option.h"
35 #include "filter/e-filter-int.h"
36
37 #include "em-filter-editor-folder-element.h"
38
39 #define EM_VFOLDER_EDITOR_CONTEXT_GET_PRIVATE(obj) \
40 (G_TYPE_INSTANCE_GET_PRIVATE \
41 ((obj), EM_TYPE_VFOLDER_EDITOR_CONTEXT, EMVFolderEditorContextPrivate))
42
43 struct _EMVFolderEditorContextPrivate {
44 EMailSession *session;
45 };
46
47 enum {
48 PROP_0,
49 PROP_SESSION
50 };
51
52 G_DEFINE_TYPE (
53 EMVFolderEditorContext,
54 em_vfolder_editor_context,
55 EM_TYPE_VFOLDER_CONTEXT)
56
57 static void
58 vfolder_editor_context_set_session (EMVFolderEditorContext *context,
59 EMailSession *session)
60 {
61 if (session == NULL) {
62 EShell *shell;
63 EShellBackend *shell_backend;
64 EMailBackend *backend;
65
66 shell = e_shell_get_default ();
67 shell_backend = e_shell_get_backend_by_name (shell, "mail");
68
69 backend = E_MAIL_BACKEND (shell_backend);
70 session = e_mail_backend_get_session (backend);
71 }
72
73 g_return_if_fail (E_IS_MAIL_SESSION (session));
74 g_return_if_fail (context->priv->session == NULL);
75
76 context->priv->session = g_object_ref (session);
77 }
78
79 static void
80 vfolder_editor_context_set_property (GObject *object,
81 guint property_id,
82 const GValue *value,
83 GParamSpec *pspec)
84 {
85 switch (property_id) {
86 case PROP_SESSION:
87 vfolder_editor_context_set_session (
88 EM_VFOLDER_EDITOR_CONTEXT (object),
89 g_value_get_object (value));
90 return;
91 }
92
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94 }
95
96 static void
97 vfolder_editor_context_get_property (GObject *object,
98 guint property_id,
99 GValue *value,
100 GParamSpec *pspec)
101 {
102 switch (property_id) {
103 case PROP_SESSION:
104 g_value_set_object (
105 value,
106 em_vfolder_editor_context_get_session (
107 EM_VFOLDER_EDITOR_CONTEXT (object)));
108 return;
109 }
110
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
112 }
113
114 static void
115 vfolder_editor_context_dispose (GObject *object)
116 {
117 EMVFolderEditorContextPrivate *priv;
118
119 priv = EM_VFOLDER_EDITOR_CONTEXT_GET_PRIVATE (object);
120 if (priv->session != NULL) {
121 g_object_unref (priv->session);
122 priv->session = NULL;
123 }
124
125 /* Chain up to parent's dispose() method. */
126 G_OBJECT_CLASS (em_vfolder_editor_context_parent_class)->dispose (object);
127 }
128
129 static EFilterElement *
130 vfolder_editor_context_new_element (ERuleContext *context,
131 const gchar *type)
132 {
133 EMVFolderEditorContextPrivate *priv;
134
135 priv = EM_VFOLDER_EDITOR_CONTEXT_GET_PRIVATE (context);
136
137 if (strcmp (type, "system-flag") == 0)
138 return e_filter_option_new ();
139
140 if (strcmp (type, "score") == 0)
141 return e_filter_int_new_type ("score", -3, 3);
142
143 if (strcmp (type, "folder") == 0)
144 return em_filter_editor_folder_element_new (priv->session);
145
146 /* XXX Legacy type name. Same as "folder" now. */
147 if (strcmp (type, "folder-curi") == 0)
148 return em_filter_editor_folder_element_new (priv->session);
149
150 return E_RULE_CONTEXT_CLASS (em_vfolder_editor_context_parent_class)->
151 new_element (context, type);
152 }
153
154 static void
155 em_vfolder_editor_context_class_init (EMVFolderEditorContextClass *class)
156 {
157 GObjectClass *object_class;
158 ERuleContextClass *rule_context_class;
159
160 g_type_class_add_private (class, sizeof (EMVFolderEditorContextPrivate));
161
162 object_class = G_OBJECT_CLASS (class);
163 object_class->set_property = vfolder_editor_context_set_property;
164 object_class->get_property = vfolder_editor_context_get_property;
165 object_class->dispose = vfolder_editor_context_dispose;
166
167 rule_context_class = E_RULE_CONTEXT_CLASS (class);
168 rule_context_class->new_element = vfolder_editor_context_new_element;
169
170 g_object_class_install_property (
171 object_class,
172 PROP_SESSION,
173 g_param_spec_object (
174 "session",
175 NULL,
176 NULL,
177 E_TYPE_MAIL_SESSION,
178 G_PARAM_READWRITE |
179 G_PARAM_CONSTRUCT_ONLY));
180 }
181
182 static void
183 em_vfolder_editor_context_init (EMVFolderEditorContext *context)
184 {
185 context->priv = EM_VFOLDER_EDITOR_CONTEXT_GET_PRIVATE (context);
186
187 e_rule_context_add_part_set (
188 E_RULE_CONTEXT (context), "partset", E_TYPE_FILTER_PART,
189 (ERuleContextPartFunc) e_rule_context_add_part,
190 (ERuleContextNextPartFunc) e_rule_context_next_part);
191
192 e_rule_context_add_rule_set (
193 E_RULE_CONTEXT (context), "ruleset", EM_TYPE_VFOLDER_EDITOR_RULE,
194 (ERuleContextRuleFunc) e_rule_context_add_rule,
195 (ERuleContextNextRuleFunc) e_rule_context_next_rule);
196
197 E_RULE_CONTEXT (context)->flags =
198 E_RULE_CONTEXT_THREADING | E_RULE_CONTEXT_GROUPING;
199 }
200
201 EMVFolderEditorContext *
202 em_vfolder_editor_context_new (EMailSession *session)
203 {
204 g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
205
206 return g_object_new (
207 EM_TYPE_VFOLDER_EDITOR_CONTEXT, "session", session, NULL);
208 }
209
210 EMailSession *
211 em_vfolder_editor_context_get_session (EMVFolderEditorContext *context)
212 {
213 g_return_val_if_fail (EM_IS_VFOLDER_EDITOR_CONTEXT (context), NULL);
214
215 return context->priv->session;
216 }