1 /** @file
2 * @brief 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,
113 const Inkscape::Extension::FileSaveMethod save_method)
114 {
115 #ifdef WIN32
116 FileSaveDialog *dialog = new FileSaveDialogImplWin32(parentWindow, path, fileTypes, title, default_key, docTitle, save_method);
117 #else
118 FileSaveDialog *dialog = new FileSaveDialogImplGtk(parentWindow, path, fileTypes, title, default_key, docTitle, save_method);
119 #endif
120 return dialog;
121 }
123 Glib::ustring FileSaveDialog::getFilename()
124 {
125 return myFilename;
126 }
128 Glib::ustring FileSaveDialog::getDocTitle()
129 {
130 return myDocTitle;
131 }
133 //void FileSaveDialog::change_path(const Glib::ustring& path)
134 //{
135 // myFilename = path;
136 //}
138 void FileSaveDialog::appendExtension(Glib::ustring& path, Inkscape::Extension::Output* outputExtension)
139 {
140 try {
141 bool appendExtension = true;
142 Glib::ustring utf8Name = Glib::filename_to_utf8( path );
143 Glib::ustring::size_type pos = utf8Name.rfind('.');
144 if ( pos != Glib::ustring::npos ) {
145 Glib::ustring trail = utf8Name.substr( pos );
146 Glib::ustring foldedTrail = trail.casefold();
147 if ( (trail == ".")
148 | (foldedTrail != Glib::ustring( outputExtension->get_extension() ).casefold()
149 && ( knownExtensions.find(foldedTrail) != knownExtensions.end() ) ) ) {
150 utf8Name = utf8Name.erase( pos );
151 } else {
152 appendExtension = false;
153 }
154 }
156 if (appendExtension) {
157 utf8Name = utf8Name + outputExtension->get_extension();
158 myFilename = Glib::filename_from_utf8( utf8Name );
159 }
160 } catch ( Glib::ConvertError& e ) {
161 // ignore
162 }
163 }
165 //########################################################################
166 //# F I L E E X P O R T
167 //########################################################################
169 #ifdef NEW_EXPORT_DIALOG
171 /**
172 * Public factory method. Used in file.cpp
173 */
174 FileExportDialog *FileExportDialog::create(Gtk::Window& parentWindow,
175 const Glib::ustring &path,
176 FileDialogType fileTypes,
177 const char *title,
178 const Glib::ustring &default_key)
179 {
180 FileExportDialog *dialog = new FileExportDialogImpl(parentWindow, path, fileTypes, title, default_key);
181 return dialog;
182 }
184 #endif // NEW_EXPORT_DIALOG
187 } //namespace Dialog
188 } //namespace UI
189 } //namespace Inkscape
193 /*
194 Local Variables:
195 mode:c++
196 c-file-style:"stroustrup"
197 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
198 indent-tabs-mode:nil
199 fill-column:99
200 End:
201 */
202 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :