Code

convert almost all libnrtype to Geom::
[inkscape.git] / src / ui / dialog / filedialog.cpp
1 /**
2  * Implementation of the file dialog interfaces defined in filedialog.h
3  *
4  * Authors:
5  *   Bob Jamison
6  *   Joel Holdsworth
7  *   Other dudes from The Inkscape Organization
8  *
9  * Copyright (C) 2004-2007 Bob Jamison
10  * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
11  * Copyright (C) 2007-2008 Joel Holdsworth
12  * Copyright (C) 2004-2008 The Inkscape Organization
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #include "filedialog.h"
18 #include "filedialogimpl-gtkmm.h"
19 #include "filedialogimpl-win32.h"
21 #include "gc-core.h"
22 #include <dialogs/dialog-events.h>
24 namespace Inkscape
25 {
26 namespace UI
27 {
28 namespace Dialog
29 {
31 /*#########################################################################
32 ### U T I L I T Y
33 #########################################################################*/
35 bool hasSuffix(const Glib::ustring &str, const Glib::ustring &ext)
36 {
37     int strLen = str.length();
38     int extLen = ext.length();
39     if (extLen > strLen)
40         return false;
41     int strpos = strLen-1;
42     for (int extpos = extLen-1 ; extpos>=0 ; extpos--, strpos--)
43         {
44         Glib::ustring::value_type ch = str[strpos];
45         if (ch != ext[extpos])
46             {
47             if ( ((ch & 0xff80) != 0) ||
48                  static_cast<Glib::ustring::value_type>( g_ascii_tolower( static_cast<gchar>(0x07f & ch) ) ) != ext[extpos] )
49                 {
50                 return false;
51                 }
52             }
53         }
54     return true;
55 }
57 bool isValidImageFile(const Glib::ustring &fileName)
58 {
59     std::vector<Gdk::PixbufFormat>formats = Gdk::Pixbuf::get_formats();
60     for (unsigned int i=0; i<formats.size(); i++)
61         {
62         Gdk::PixbufFormat format = formats[i];
63         std::vector<Glib::ustring>extensions = format.get_extensions();
64         for (unsigned int j=0; j<extensions.size(); j++)
65             {
66             Glib::ustring ext = extensions[j];
67             if (hasSuffix(fileName, ext))
68                 return true;
69             }
70         }
71     return false;
72 }
74 /*#########################################################################
75 ### F I L E    O P E N
76 #########################################################################*/
78 /**
79  * Public factory.  Called by file.cpp, among others.
80  */
81 FileOpenDialog *FileOpenDialog::create(Gtk::Window &parentWindow,
82                                                const Glib::ustring &path,
83                                        FileDialogType fileTypes,
84                                        const char *title)
85 {
86 #ifdef WIN32
87     FileOpenDialog *dialog = new FileOpenDialogImplWin32(parentWindow, path, fileTypes, title);
88 #else
89     FileOpenDialog *dialog = new FileOpenDialogImplGtk(parentWindow, path, fileTypes, title);
90 #endif
92         return dialog;
93 }
95 Glib::ustring FileOpenDialog::getFilename()
96 {
97     return myFilename;
98 }
100 //########################################################################
101 //# F I L E    S A V E
102 //########################################################################
104 /**
105  * Public factory method.  Used in file.cpp
106  */
107 FileSaveDialog *FileSaveDialog::create(Gtk::Window& parentWindow,
108                                                                            const Glib::ustring &path,
109                                        FileDialogType fileTypes,
110                                        const char *title,
111                                        const Glib::ustring &default_key,
112                                        const gchar *docTitle)
114 #ifdef WIN32
115     FileSaveDialog *dialog = new FileSaveDialogImplWin32(parentWindow, path, fileTypes, title, default_key, docTitle);
116 #else
117     FileSaveDialog *dialog = new FileSaveDialogImplGtk(parentWindow, path, fileTypes, title, default_key, docTitle);
118 #endif
119     return dialog;
122 Glib::ustring FileSaveDialog::getFilename()
124     return myFilename;
127 Glib::ustring FileSaveDialog::getDocTitle()
129         return myDocTitle;
132 //void FileSaveDialog::change_path(const Glib::ustring& path)
133 //{
134 //      myFilename = path;
135 //}
137 void FileSaveDialog::appendExtension(Glib::ustring& path, Inkscape::Extension::Output* outputExtension)
139         try {
140                 bool appendExtension = true;
141                 Glib::ustring utf8Name = Glib::filename_to_utf8( path );
142                 Glib::ustring::size_type pos = utf8Name.rfind('.');
143                 if ( pos != Glib::ustring::npos ) {
144                         Glib::ustring trail = utf8Name.substr( pos );
145                         Glib::ustring foldedTrail = trail.casefold();
146                         if ( (trail == ".")
147                                  | (foldedTrail != Glib::ustring( outputExtension->get_extension() ).casefold()
148                                         && ( knownExtensions.find(foldedTrail) != knownExtensions.end() ) ) ) {
149                                 utf8Name = utf8Name.erase( pos );
150                         } else {
151                                 appendExtension = false;
152                         }
153                 }
155                 if (appendExtension) {
156                         utf8Name = utf8Name + outputExtension->get_extension();
157                         myFilename = Glib::filename_from_utf8( utf8Name );
158                 }
159         } catch ( Glib::ConvertError& e ) {
160                 // ignore
161         }
164 //########################################################################
165 //# F I L E     E X P O R T
166 //########################################################################
168 /**
169  * Public factory method.  Used in file.cpp
170  */
171  FileExportDialog *FileExportDialog::create(Gtk::Window& parentWindow,
172                                                                                    const Glib::ustring &path,
173                                            FileDialogType fileTypes,
174                                            const char *title,
175                                            const Glib::ustring &default_key)
177     FileExportDialog *dialog = new FileExportDialogImpl(parentWindow, path, fileTypes, title, default_key);
178     return dialog;
182 } //namespace Dialog
183 } //namespace UI
184 } //namespace Inkscape
188 /*
189   Local Variables:
190   mode:c++
191   c-file-style:"stroustrup"
192   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
193   indent-tabs-mode:nil
194   fill-column:99
195   End:
196 */
197 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :