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(), "input")) {
56 child_repr = sp_repr_children(child_repr);
57 while (child_repr != NULL) {
58 char const * chname = child_repr->name();
59 if (chname[0] == '_') /* Allow _ for translation of tags */
60 chname++;
61 if (!strcmp(chname, "extension")) {
62 g_free (extension);
63 extension = g_strdup(sp_repr_children(child_repr)->content());
64 }
65 if (!strcmp(chname, "mimetype")) {
66 g_free (mimetype);
67 mimetype = g_strdup(sp_repr_children(child_repr)->content());
68 }
69 if (!strcmp(chname, "filetypename")) {
70 g_free (filetypename);
71 filetypename = g_strdup(sp_repr_children(child_repr)->content());
72 }
73 if (!strcmp(chname, "filetypetooltip")) {
74 g_free (filetypetooltip);
75 filetypetooltip = g_strdup(sp_repr_children(child_repr)->content());
76 }
77 if (!strcmp(chname, "output_extension")) {
78 g_free (output_extension);
79 output_extension = g_strdup(sp_repr_children(child_repr)->content());
80 }
82 child_repr = sp_repr_next(child_repr);
83 }
85 break;
86 }
88 child_repr = sp_repr_next(child_repr);
89 }
91 }
93 return;
94 }
96 /**
97 \return None
98 \brief Destroys an Input extension
99 */
100 Input::~Input (void)
101 {
102 g_free(mimetype);
103 g_free(extension);
104 g_free(filetypename);
105 g_free(filetypetooltip);
106 g_free(output_extension);
107 return;
108 }
110 /**
111 \return Whether this extension checks out
112 \brief Validate this extension
114 This function checks to make sure that the input extension has
115 a filename extension and a MIME type. Then it calls the parent
116 class' check function which also checks out the implmentation.
117 */
118 bool
119 Input::check (void)
120 {
121 if (extension == NULL)
122 return FALSE;
123 if (mimetype == NULL)
124 return FALSE;
126 return Extension::check();
127 }
129 /**
130 \return A new document
131 \brief This function creates a document from a file
132 \param uri The filename to create the document from
134 This function acts as the first step in creating a new document
135 from a file. The first thing that this does is make sure that the
136 file actually exists. If it doesn't, a NULL is returned. If the
137 file exits, then it is opened using the implmentation of this extension.
139 After opening the document the output_extension is set. What this
140 accomplishes is that save can try to use an extension that supports
141 the same fileformat. So something like opening and saveing an
142 Adobe Illustrator file can be transparent (not recommended, but
143 transparent). This is all done with undo being turned off.
144 */
145 SPDocument *
146 Input::open (const gchar *uri)
147 {
148 if (!loaded()) {
149 set_state(Extension::STATE_LOADED);
150 }
151 if (!loaded()) {
152 return NULL;
153 }
154 timer->touch();
156 SPDocument *const doc = imp->open(this, uri);
157 if (doc != NULL) {
158 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
159 bool saved = sp_document_get_undo_sensitive(doc);
160 sp_document_set_undo_sensitive (doc, false);
161 repr->setAttribute("inkscape:output_extension", output_extension);
162 sp_document_set_undo_sensitive (doc, saved);
163 }
165 return doc;
166 }
168 /**
169 \return IETF mime-type for the extension
170 \brief Get the mime-type that describes this extension
171 */
172 gchar *
173 Input::get_mimetype(void)
174 {
175 return mimetype;
176 }
178 /**
179 \return Filename extension for the extension
180 \brief Get the filename extension for this extension
181 */
182 gchar *
183 Input::get_extension(void)
184 {
185 return extension;
186 }
188 /**
189 \return The name of the filetype supported
190 \brief Get the name of the filetype supported
191 */
192 gchar *
193 Input::get_filetypename(void)
194 {
195 if (filetypename != NULL)
196 return filetypename;
197 else
198 return get_name();
199 }
201 /**
202 \return Tooltip giving more information on the filetype
203 \brief Get the tooltip for more information on the filetype
204 */
205 gchar *
206 Input::get_filetypetooltip(void)
207 {
208 return filetypetooltip;
209 }
211 /**
212 \return A dialog to get settings for this extension
213 \brief Create a dialog for preference for this extension
215 Calls the implementation to get the preferences.
216 */
217 bool
218 Input::prefs (const gchar *uri)
219 {
220 if (!loaded())
221 set_state(Extension::STATE_LOADED);
222 if (!loaded()) return false;
224 Gtk::Widget * controls;
225 controls = imp->prefs_input(this, uri);
226 if (controls == NULL) {
227 // std::cout << "No preferences for Input" << std::endl;
228 return true;
229 }
231 PrefDialog * dialog = new PrefDialog(this->get_name(), this->get_help(), controls);
232 int response = dialog->run();
233 dialog->hide();
235 delete dialog;
237 if (response == Gtk::RESPONSE_OK) return true;
238 return false;
239 }
241 } } /* namespace Inkscape, Extension */
243 /*
244 Local Variables:
245 mode:c++
246 c-file-style:"stroustrup"
247 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
248 indent-tabs-mode:nil
249 fill-column:99
250 End:
251 */
252 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :