27
28
namespace Inkscape {
30
static Inkscape::XML::Document *loadImpl( std::string const& prefsFilename, Glib::ustring & errMsg );
31
static void migrateDetails( Inkscape::XML::Document *from, Inkscape::XML::Document *to );
33
static Inkscape::XML::Document *migrateFromDoc = 0;
35
// TODO clean up. Function copied from file.cpp:
36
// what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
37
static void file_add_recent(gchar const *uri)
40
g_warning("file_add_recent: uri == NULL");
42
GtkRecentManager *recent = gtk_recent_manager_get_default();
43
gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
45
if (g_file_test(fn, G_FILE_TEST_EXISTS)) {
46
gchar *uriToAdd = g_filename_to_uri(fn, NULL, NULL);
48
gtk_recent_manager_add_item(recent, uriToAdd);
29
58
// private inner class definition
104
132
// NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose
106
134
// 1. Does the file exist?
107
if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_EXISTS)) {
135
if (!g_file_test(_prefs_filename.c_str(), G_FILE_TEST_EXISTS)) {
108
136
// No - we need to create one.
109
137
// Does the profile directory exist?
110
if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_EXISTS)) {
138
if (!g_file_test(_prefs_dir.c_str(), G_FILE_TEST_EXISTS)) {
111
139
// No - create the profile directory
112
if (g_mkdir(_prefs_dir.data(), 0755)) {
140
if (g_mkdir(_prefs_dir.c_str(), 0755)) {
113
141
// the creation failed
114
142
//_reportError(Glib::ustring::compose(_("Cannot create profile directory %1."),
115
143
// Glib::filename_to_utf8(_prefs_dir)), not_saved);
116
144
gchar *msg = g_strdup_printf(_("Cannot create profile directory %s."),
117
Glib::filename_to_utf8(_prefs_dir).data());
145
Glib::filename_to_utf8(_prefs_dir).c_str());
118
146
_reportError(msg, not_saved);
130
} else if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_IS_DIR)) {
158
} else if (!g_file_test(_prefs_dir.c_str(), G_FILE_TEST_IS_DIR)) {
131
159
// The profile dir is not actually a directory
132
160
//_reportError(Glib::ustring::compose(_("%1 is not a valid directory."),
133
161
// Glib::filename_to_utf8(_prefs_dir)), not_saved);
134
162
gchar *msg = g_strdup_printf(_("%s is not a valid directory."),
135
Glib::filename_to_utf8(_prefs_dir).data());
163
Glib::filename_to_utf8(_prefs_dir).c_str());
136
164
_reportError(msg, not_saved);
140
168
// The profile dir exists and is valid.
141
if (!g_file_set_contents(_prefs_filename.data(), preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL)) {
169
if (!g_file_set_contents(_prefs_filename.c_str(), preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL)) {
142
170
// The write failed.
143
171
//_reportError(Glib::ustring::compose(_("Failed to create the preferences file %1."),
144
172
// Glib::filename_to_utf8(_prefs_filename)), not_saved);
145
173
gchar *msg = g_strdup_printf(_("Failed to create the preferences file %s."),
146
Glib::filename_to_utf8(_prefs_filename).data());
174
Glib::filename_to_utf8(_prefs_filename).c_str());
147
175
_reportError(msg, not_saved);
180
if ( migrateFromDoc ) {
181
migrateDetails( migrateFromDoc, _prefs_doc );
152
184
// The prefs file was just created.
153
185
// We can return now and skip the rest of the load process.
154
186
_writable = true;
158
190
// Yes, the pref file exists.
191
Glib::ustring errMsg;
192
Inkscape::XML::Document *prefs_read = loadImpl( _prefs_filename, errMsg );
195
// Merge the loaded prefs with defaults.
196
_prefs_doc->root()->mergeFrom(prefs_read->root(), "id");
197
Inkscape::GC::release(prefs_read);
200
_reportError(errMsg, not_saved);
204
//_reportError(msg, not_saved);
205
static Inkscape::XML::Document *loadImpl( std::string const& prefsFilename, Glib::ustring & errMsg )
159
207
// 2. Is it a regular file?
160
if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_IS_REGULAR)) {
161
//_reportError(Glib::ustring::compose(_("The preferences file %1 is not a regular file."),
162
// Glib::filename_to_utf8(_prefs_filename)), not_saved);
208
if (!g_file_test(prefsFilename.c_str(), G_FILE_TEST_IS_REGULAR)) {
163
209
gchar *msg = g_strdup_printf(_("The preferences file %s is not a regular file."),
164
Glib::filename_to_utf8(_prefs_filename).data());
165
_reportError(msg, not_saved);
210
Glib::filename_to_utf8(prefsFilename).c_str());
170
216
// 3. Is the file readable?
171
217
gchar *prefs_xml = NULL; gsize len = 0;
172
if (!g_file_get_contents(_prefs_filename.data(), &prefs_xml, &len, NULL)) {
173
//_reportError(Glib::ustring::compose(_("The preferences file %1 could not be read."),
174
// Glib::filename_to_utf8(_prefs_filename)), not_saved);
218
if (!g_file_get_contents(prefsFilename.c_str(), &prefs_xml, &len, NULL)) {
175
219
gchar *msg = g_strdup_printf(_("The preferences file %s could not be read."),
176
Glib::filename_to_utf8(_prefs_filename).data());
177
_reportError(msg, not_saved);
220
Glib::filename_to_utf8(prefsFilename).c_str());
182
226
// 4. Is it valid XML?
183
227
Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL);
184
228
g_free(prefs_xml);
185
229
if (!prefs_read) {
186
//_reportError(Glib::ustring::compose(_("The preferences file %1 is not a valid XML document."),
187
// Glib::filename_to_utf8(_prefs_filename)), not_saved);
188
230
gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."),
189
Glib::filename_to_utf8(_prefs_filename).data());
190
_reportError(msg, not_saved);
231
Glib::filename_to_utf8(prefsFilename).c_str());
195
237
// 5. Basic sanity check: does the root element have a correct name?
196
238
if (strcmp(prefs_read->root()->name(), "inkscape")) {
197
//_reportError(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."),
198
// Glib::filename_to_utf8(_prefs_filename)), not_saved);
199
239
gchar *msg = g_strdup_printf(_("The file %s is not a valid Inkscape preferences file."),
200
Glib::filename_to_utf8(_prefs_filename).data());
201
_reportError(msg, not_saved);
240
Glib::filename_to_utf8(prefsFilename).c_str());
203
243
Inkscape::GC::release(prefs_read);
207
// Merge the loaded prefs with defaults.
208
_prefs_doc->root()->mergeFrom(prefs_read->root(), "id");
209
Inkscape::GC::release(prefs_read);
250
static void migrateDetails( Inkscape::XML::Document *from, Inkscape::XML::Document *to )
252
// TODO pull in additional prefs with more granularity
253
to->root()->mergeFrom(from->root(), "id");
298
void Preferences::migrate( std::string const& legacyDir, std::string const& prefdir )
310
if ( g_mkdir_with_parents(prefdir.c_str(), mode) == -1 ) {
314
gchar * oldPrefFile = g_build_filename(legacyDir.c_str(), PREFERENCES_FILE_NAME, NULL);
316
if (g_file_test(oldPrefFile, G_FILE_TEST_EXISTS)) {
317
Glib::ustring errMsg;
318
Inkscape::XML::Document *oldPrefs = loadImpl( oldPrefFile, errMsg );
320
Glib::ustring docId("documents");
321
Glib::ustring recentId("recent");
322
Inkscape::XML::Node *node = oldPrefs->root();
323
Inkscape::XML::Node *child = 0;
324
Inkscape::XML::Node *recentNode = 0;
325
if (node->attribute("version")) {
326
node->setAttribute("version", "1");
328
for (child = node->firstChild(); child; child = child->next()) {
329
if (docId == child->attribute("id")) {
330
for (child = child->firstChild(); child; child = child->next()) {
331
if (recentId == child->attribute("id")) {
333
for (child = child->firstChild(); child; child = child->next()) {
334
gchar const* uri = child->attribute("uri");
336
file_add_recent(uri);
347
while (recentNode->firstChild()) {
348
recentNode->removeChild(recentNode->firstChild());
351
migrateFromDoc = oldPrefs;
352
//Inkscape::GC::release(oldPrefs);
355
g_warning( "%s", errMsg.c_str() );
255
363
// Now for the meat.