2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
17 * Jeffrey Stedfast <fejj@ximian.com>
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
26 #include <sys/types.h>
29 #include <glib/gi18n.h>
30 #include <glib/gstdio.h>
32 #include <libedataserver/e-sexp.h>
34 #include "e-util/e-alert.h"
36 #include "e-filter-file.h"
37 #include "e-filter-part.h"
42 E_TYPE_FILTER_ELEMENT
)
45 filter_file_filename_changed (GtkFileChooser
*file_chooser
,
46 EFilterElement
*element
)
48 EFilterFile
*file
= E_FILTER_FILE (element
);
51 path
= gtk_file_chooser_get_filename (file_chooser
);
54 file
->path
= g_strdup (path
);
58 filter_file_finalize (GObject
*object
)
60 EFilterFile
*file
= E_FILTER_FILE (object
);
65 /* Chain up to parent's finalize() method. */
66 G_OBJECT_CLASS (e_filter_file_parent_class
)->finalize (object
);
70 filter_file_validate (EFilterElement
*element
,
73 EFilterFile
*file
= E_FILTER_FILE (element
);
75 g_warn_if_fail (alert
== NULL
|| *alert
== NULL
);
79 *alert
= e_alert_new ("filter:no-file", NULL
);
83 /* FIXME: do more to validate command-lines? */
85 if (g_strcmp0 (file
->type
, "file") == 0) {
86 if (!g_file_test (file
->path
, G_FILE_TEST_IS_REGULAR
)) {
88 *alert
= e_alert_new ( "filter:bad-file",
92 } else if (g_strcmp0 (file
->type
, "command") == 0) {
93 /* Only requirements so far is that the
94 * command can't be an empty string. */
95 return (file
->path
[0] != '\0');
102 filter_file_eq (EFilterElement
*element_a
,
103 EFilterElement
*element_b
)
105 EFilterFile
*file_a
= E_FILTER_FILE (element_a
);
106 EFilterFile
*file_b
= E_FILTER_FILE (element_b
);
108 /* Chain up to parent's eq() method. */
109 if (!E_FILTER_ELEMENT_CLASS (e_filter_file_parent_class
)->
110 eq (element_a
, element_b
))
113 if (g_strcmp0 (file_a
->path
, file_b
->path
) != 0)
116 if (g_strcmp0 (file_a
->type
, file_b
->type
) != 0)
123 filter_file_xml_encode (EFilterElement
*element
)
125 EFilterFile
*file
= E_FILTER_FILE (element
);
126 xmlNodePtr cur
, value
;
129 type
= file
->type
? file
->type
: "file";
131 value
= xmlNewNode (NULL
, (xmlChar
*)"value");
132 xmlSetProp (value
, (xmlChar
*) "name", (xmlChar
*) element
->name
);
133 xmlSetProp (value
, (xmlChar
*) "type", (xmlChar
*) type
);
135 cur
= xmlNewChild (value
, NULL
, (xmlChar
*)type
, NULL
);
136 xmlNodeSetContent (cur
, (xmlChar
*)file
->path
);
142 filter_file_xml_decode (EFilterElement
*element
,
145 EFilterFile
*file
= E_FILTER_FILE (element
);
146 gchar
*name
, *str
, *type
;
149 name
= (gchar
*)xmlGetProp (node
, (xmlChar
*) "name");
150 type
= (gchar
*)xmlGetProp (node
, (xmlChar
*) "type");
152 xmlFree (element
->name
);
153 element
->name
= name
;
155 xmlFree (file
->type
);
161 child
= node
->children
;
162 while (child
!= NULL
) {
163 if (!strcmp ((gchar
*)child
->name
, type
)) {
164 str
= (gchar
*)xmlNodeGetContent (child
);
165 file
->path
= g_strdup (str
? str
: "");
169 } else if (child
->type
== XML_ELEMENT_NODE
) {
171 "Unknown node type '%s' encountered "
172 "decoding a %s\n", child
->name
, type
);
182 filter_file_get_widget (EFilterElement
*element
)
184 EFilterFile
*file
= E_FILTER_FILE (element
);
187 widget
= gtk_file_chooser_button_new (
188 _("Choose a File"), GTK_FILE_CHOOSER_ACTION_OPEN
);
189 gtk_file_chooser_set_filename (
190 GTK_FILE_CHOOSER (widget
), file
->path
);
192 widget
, "selection-changed",
193 G_CALLBACK (filter_file_filename_changed
), element
);
199 filter_file_format_sexp (EFilterElement
*element
,
202 EFilterFile
*file
= E_FILTER_FILE (element
);
204 e_sexp_encode_string (out
, file
->path
);
208 e_filter_file_class_init (EFilterFileClass
*class)
210 GObjectClass
*object_class
;
211 EFilterElementClass
*filter_element_class
;
213 object_class
= G_OBJECT_CLASS (class);
214 object_class
->finalize
= filter_file_finalize
;
216 filter_element_class
= E_FILTER_ELEMENT_CLASS (class);
217 filter_element_class
->validate
= filter_file_validate
;
218 filter_element_class
->eq
= filter_file_eq
;
219 filter_element_class
->xml_encode
= filter_file_xml_encode
;
220 filter_element_class
->xml_decode
= filter_file_xml_decode
;
221 filter_element_class
->get_widget
= filter_file_get_widget
;
222 filter_element_class
->format_sexp
= filter_file_format_sexp
;
226 e_filter_file_init (EFilterFile
*filter
)
233 * Create a new EFilterFile object.
235 * Return value: A new #EFilterFile object.
238 e_filter_file_new (void)
240 return g_object_new (E_TYPE_FILTER_FILE
, NULL
);
244 e_filter_file_new_type_name (const gchar
*type
)
248 file
= e_filter_file_new ();
249 file
->type
= (gchar
*) xmlStrdup ((xmlChar
*)type
);
255 e_filter_file_set_path (EFilterFile
*file
,
258 g_return_if_fail (E_IS_FILTER_FILE (file
));
261 file
->path
= g_strdup (path
);