1 /*
2 * Authors:
3 * Ted Gould <ted@gould.cx>
4 *
5 * Copyright (C) 2002-2005 Authors
6 *
7 * Released under GNU GPL, read the file 'COPYING' for more information
8 */
10 #ifdef HAVE_CONFIG_H
11 # include "config.h"
12 #endif
14 #include "implementation/implementation.h"
15 #include "timer.h"
16 #include "input.h"
17 #include "io/sys.h"
18 #include "prefdialog.h"
20 /* Inkscape::Extension::Input */
22 namespace Inkscape {
23 namespace Extension {
25 /**
26 \return None
27 \brief Builds a SPModuleInput object from a XML description
28 \param module The module to be initialized
29 \param repr The XML description in a Inkscape::XML::Node tree
31 Okay, so you want to build a SPModuleInput object.
33 This function first takes and does the build of the parent class,
34 which is SPModule. Then, it looks for the <input> section of the
35 XML description. Under there should be several fields which
36 describe the input module to excruciating detail. Those are parsed,
37 copied, and put into the structure that is passed in as module.
38 Overall, there are many levels of indentation, just to handle the
39 levels of indentation in the XML file.
40 */
41 Input::Input (Inkscape::XML::Node * in_repr, Implementation::Implementation * in_imp) : Extension(in_repr, in_imp)
42 {
43 mimetype = NULL;
44 extension = NULL;
45 filetypename = NULL;
46 filetypetooltip = NULL;
47 output_extension = NULL;
49 if (repr != NULL) {
50 Inkscape::XML::Node * child_repr;
52 child_repr = sp_repr_children(repr);
54 while (child_repr != NULL) {
55 if (!strcmp(child_repr->name(), INKSCAPE_EXTENSION_NS "input")) {
56 child_repr = sp_repr_children(child_repr);
57 while (child_repr != NULL) {
58 char const * chname = child_repr->name();
59 if (!strncmp(chname, INKSCAPE_EXTENSION_NS_NC, strlen(INKSCAPE_EXTENSION_NS_NC))) {
60 chname += strlen(INKSCAPE_EXTENSION_NS);
61 }
62 if (chname[0] == '_') /* Allow _ for translation of tags */
63 chname++;
64 if (!strcmp(chname, "extension")) {
65 g_free (extension);
66 extension = g_strdup(sp_repr_children(child_repr)->content());
67 }
68 if (!strcmp(chname, "mimetype")) {
69 g_free (mimetype);
70 mimetype = g_strdup(sp_repr_children(child_repr)->content());
71 }
72 if (!strcmp(chname, "filetypename")) {
73 g_free (filetypename);
74 filetypename = g_strdup(sp_repr_children(child_repr)->content());
75 }
76 if (!strcmp(chname, "filetypetooltip")) {
77 g_free (filetypetooltip);
78 filetypetooltip = g_strdup(sp_repr_children(child_repr)->content());
79 }
80 if (!strcmp(chname, "output_extension")) {
81 g_free (output_extension);
82 output_extension = g_strdup(sp_repr_children(child_repr)->content());
83 }
85 child_repr = sp_repr_next(child_repr);
86 }
88 break;
89 }
91 child_repr = sp_repr_next(child_repr);
92 }
94 }
96 return;
97 }
99 /**
100 \return None
101 \brief Destroys an Input extension
102 */
103 Input::~Input (void)
104 {
105 g_free(mimetype);
106 g_free(extension);
107 g_free(filetypename);
108 g_free(filetypetooltip);
109 g_free(output_extension);
110 return;
111 }
113 /**
114 \return Whether this extension checks out
115 \brief Validate this extension
117 This function checks to make sure that the input extension has
118 a filename extension and a MIME type. Then it calls the parent
119 class' check function which also checks out the implmentation.
120 */
121 bool
122 Input::check (void)
123 {
124 if (extension == NULL)
125 return FALSE;
126 if (mimetype == NULL)
127 return FALSE;
129 return Extension::check();
130 }
132 /**
133 \return A new document
134 \brief This function creates a document from a file
135 \param uri The filename to create the document from
137 This function acts as the first step in creating a new document
138 from a file. The first thing that this does is make sure that the
139 file actually exists. If it doesn't, a NULL is returned. If the
140 file exits, then it is opened using the implmentation of this extension.
141 */
142 SPDocument *
143 Input::open (const gchar *uri)
144 {
145 if (!loaded()) {
146 set_state(Extension::STATE_LOADED);
147 }
148 if (!loaded()) {
149 return NULL;
150 }
151 timer->touch();
153 SPDocument *const doc = imp->open(this, uri);
155 return doc;
156 }
158 /**
159 \return IETF mime-type for the extension
160 \brief Get the mime-type that describes this extension
161 */
162 gchar *
163 Input::get_mimetype(void)
164 {
165 return mimetype;
166 }
168 /**
169 \return Filename extension for the extension
170 \brief Get the filename extension for this extension
171 */
172 gchar *
173 Input::get_extension(void)
174 {
175 return extension;
176 }
178 /**
179 \return The name of the filetype supported
180 \brief Get the name of the filetype supported
181 */
182 gchar *
183 Input::get_filetypename(void)
184 {
185 if (filetypename != NULL)
186 return filetypename;
187 else
188 return get_name();
189 }
191 /**
192 \return Tooltip giving more information on the filetype
193 \brief Get the tooltip for more information on the filetype
194 */
195 gchar *
196 Input::get_filetypetooltip(void)
197 {
198 return filetypetooltip;
199 }
201 /**
202 \return A dialog to get settings for this extension
203 \brief Create a dialog for preference for this extension
205 Calls the implementation to get the preferences.
206 */
207 bool
208 Input::prefs (const gchar *uri)
209 {
210 if (!loaded()) {
211 set_state(Extension::STATE_LOADED);
212 }
213 if (!loaded()) {
214 return false;
215 }
217 Gtk::Widget * controls;
218 controls = imp->prefs_input(this, uri);
219 if (controls == NULL) {
220 // std::cout << "No preferences for Input" << std::endl;
221 return true;
222 }
224 PrefDialog * dialog = new PrefDialog(this->get_name(), this->get_help(), controls);
225 int response = dialog->run();
226 dialog->hide();
228 delete dialog;
230 if (response == Gtk::RESPONSE_OK) return true;
231 return false;
232 }
234 } } /* namespace Inkscape, Extension */
236 /*
237 Local Variables:
238 mode:c++
239 c-file-style:"stroustrup"
240 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
241 indent-tabs-mode:nil
242 fill-column:99
243 End:
244 */
245 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :