Code

SPDocument->Document
[inkscape.git] / src / extension / input.cpp
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)
105     g_free(mimetype);
106     g_free(extension);
107     g_free(filetypename);
108     g_free(filetypetooltip);
109     g_free(output_extension);
110     return;
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)
124     if (extension == NULL)
125         return FALSE;
126     if (mimetype == NULL)
127         return FALSE;
129     return Extension::check();
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.
142     After opening the document the output_extension is set.  What this
143     accomplishes is that save can try to use an extension that supports
144     the same fileformat.  So something like opening and saveing an 
145     Adobe Illustrator file can be transparent (not recommended, but
146     transparent).  This is all done with undo being turned off.
147 */
148 Document *
149 Input::open (const gchar *uri)
151     if (!loaded()) {
152         set_state(Extension::STATE_LOADED);
153     }
154     if (!loaded()) {
155         return NULL;
156     }
157     timer->touch();
159     Document *const doc = imp->open(this, uri);
160     if (doc != NULL) {
161         Inkscape::XML::Node * repr = sp_document_repr_root(doc);
162         bool saved = sp_document_get_undo_sensitive(doc);
163         sp_document_set_undo_sensitive (doc, false);
164         repr->setAttribute("inkscape:output_extension", output_extension);
165         sp_document_set_undo_sensitive (doc, saved);
166     }
168     return doc;
171 /**
172     \return  IETF mime-type for the extension
173     \brief   Get the mime-type that describes this extension
174 */
175 gchar *
176 Input::get_mimetype(void)
178     return mimetype;
181 /**
182     \return  Filename extension for the extension
183     \brief   Get the filename extension for this extension
184 */
185 gchar *
186 Input::get_extension(void)
188     return extension;
191 /**
192     \return  The name of the filetype supported
193     \brief   Get the name of the filetype supported
194 */
195 gchar *
196 Input::get_filetypename(void)
198     if (filetypename != NULL)
199         return filetypename;
200     else
201         return get_name();
204 /**
205     \return  Tooltip giving more information on the filetype
206     \brief   Get the tooltip for more information on the filetype
207 */
208 gchar *
209 Input::get_filetypetooltip(void)
211     return filetypetooltip;
214 /**
215     \return  A dialog to get settings for this extension
216     \brief   Create a dialog for preference for this extension
218     Calls the implementation to get the preferences.
219 */
220 bool
221 Input::prefs (const gchar *uri)
223     if (!loaded()) {
224         set_state(Extension::STATE_LOADED);
225     }
226     if (!loaded()) {
227         return false;
228     }
230     Gtk::Widget * controls;
231     controls = imp->prefs_input(this, uri);
232     if (controls == NULL) {
233         // std::cout << "No preferences for Input" << std::endl;
234         return true;
235     }
237     PrefDialog * dialog = new PrefDialog(this->get_name(), this->get_help(), controls);
238     int response = dialog->run();
239     dialog->hide();
241     delete dialog;
243     if (response == Gtk::RESPONSE_OK) return true;
244     return false;
247 } }  /* namespace Inkscape, Extension */
249 /*
250   Local Variables:
251   mode:c++
252   c-file-style:"stroustrup"
253   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
254   indent-tabs-mode:nil
255   fill-column:99
256   End:
257 */
258 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :