94b0f6ab4ddd4c14ea11e760b7c529c89a606371
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 * doc = NULL;
158 #ifdef WITH_GNOME_VFS
159 doc = imp->open(this, uri);
160 #else
161 if (Inkscape::IO::file_test(uri, G_FILE_TEST_EXISTS)) {
162 doc = imp->open(this, uri);
163 }
164 #endif
166 if (doc != NULL) {
167 Inkscape::XML::Node * repr = sp_document_repr_root(doc);
168 bool saved = sp_document_get_undo_sensitive(doc);
169 sp_document_set_undo_sensitive (doc, false);
170 repr->setAttribute("inkscape:output_extension", output_extension);
171 sp_document_set_undo_sensitive (doc, saved);
172 }
174 return doc;
175 }
177 /**
178 \return IETF mime-type for the extension
179 \brief Get the mime-type that describes this extension
180 */
181 gchar *
182 Input::get_mimetype(void)
183 {
184 return mimetype;
185 }
187 /**
188 \return Filename extension for the extension
189 \brief Get the filename extension for this extension
190 */
191 gchar *
192 Input::get_extension(void)
193 {
194 return extension;
195 }
197 /**
198 \return The name of the filetype supported
199 \brief Get the name of the filetype supported
200 */
201 gchar *
202 Input::get_filetypename(void)
203 {
204 if (filetypename != NULL)
205 return filetypename;
206 else
207 return get_name();
208 }
210 /**
211 \return Tooltip giving more information on the filetype
212 \brief Get the tooltip for more information on the filetype
213 */
214 gchar *
215 Input::get_filetypetooltip(void)
216 {
217 return filetypetooltip;
218 }
220 /**
221 \return A dialog to get settings for this extension
222 \brief Create a dialog for preference for this extension
224 Calls the implementation to get the preferences.
225 */
226 bool
227 Input::prefs (const gchar *uri)
228 {
229 if (!loaded())
230 set_state(Extension::STATE_LOADED);
231 if (!loaded()) return false;
233 Gtk::Widget * controls;
234 controls = imp->prefs_input(this, uri);
235 if (controls == NULL) {
236 // std::cout << "No preferences for Input" << std::endl;
237 return true;
238 }
240 PrefDialog * dialog = new PrefDialog(this->get_name(), this->get_help(), controls);
241 int response = dialog->run();
242 dialog->hide();
244 delete dialog;
246 if (response == Gtk::RESPONSE_OK) return true;
247 return false;
248 }
250 } } /* namespace Inkscape, Extension */
252 /*
253 Local Variables:
254 mode:c++
255 c-file-style:"stroustrup"
256 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
257 indent-tabs-mode:nil
258 fill-column:99
259 End:
260 */
261 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :