/** * gimp_config_deserialize_gfile: * @config: a #GObject that implements the #GimpConfigInterface. * @file: the #GFile to read configuration from. * @data: user data passed to the deserialize implementation. * @error: return location for a possible error * * Opens the file specified by @file, reads configuration data from it * and configures @config accordingly. Basically this function creates * a properly configured #GScanner for you and calls the deserialize * function of the @config's #GimpConfigInterface. * * Return value: %TRUE if deserialization succeeded, %FALSE otherwise. * * Since: 2.10 **/ gboolean gimp_config_deserialize_gfile (GimpConfig *config, GFile *file, gpointer data, GError **error) { GScanner *scanner; gboolean success; g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE); g_return_val_if_fail (G_IS_FILE (file), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); scanner = gimp_scanner_new_gfile (file, error); if (! scanner) return FALSE; g_object_freeze_notify (G_OBJECT (config)); success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config, scanner, 0, data); g_object_thaw_notify (G_OBJECT (config)); gimp_scanner_destroy (scanner); if (! success) g_assert (error == NULL || *error != NULL); return success; }
/** * gimp_scanner_new_file: * @filename: * @error: * * Return value: * * Since: 2.4 **/ GScanner * gimp_scanner_new_file (const gchar *filename, GError **error) { GScanner *scanner; GFile *file; g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); file = g_file_new_for_path (filename); scanner = gimp_scanner_new_gfile (file, error); g_object_unref (file); return scanner; }
void gimp_unitrc_load (Gimp *gimp) { GFile *file; GScanner *scanner; GTokenType token; GError *error = NULL; g_return_if_fail (GIMP_IS_GIMP (gimp)); file = gimp_directory_file ("unitrc", NULL); if (gimp->be_verbose) g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file)); scanner = gimp_scanner_new_gfile (file, &error); if (! scanner && error->code == GIMP_CONFIG_ERROR_OPEN_ENOENT) { g_clear_error (&error); g_object_unref (file); file = gimp_sysconf_directory_file ("unitrc", NULL); scanner = gimp_scanner_new_gfile (file, NULL); } if (! scanner) { g_clear_error (&error); g_object_unref (file); return; } g_scanner_scope_add_symbol (scanner, 0, "unit-info", GINT_TO_POINTER (UNIT_INFO)); g_scanner_scope_add_symbol (scanner, UNIT_INFO, "factor", GINT_TO_POINTER (UNIT_FACTOR)); g_scanner_scope_add_symbol (scanner, UNIT_INFO, "digits", GINT_TO_POINTER (UNIT_DIGITS)); g_scanner_scope_add_symbol (scanner, UNIT_INFO, "symbol", GINT_TO_POINTER (UNIT_SYMBOL)); g_scanner_scope_add_symbol (scanner, UNIT_INFO, "abbreviation", GINT_TO_POINTER (UNIT_ABBREV)); g_scanner_scope_add_symbol (scanner, UNIT_INFO, "singular", GINT_TO_POINTER (UNIT_SINGULAR)); g_scanner_scope_add_symbol (scanner, UNIT_INFO, "plural", GINT_TO_POINTER (UNIT_PLURAL)); token = G_TOKEN_LEFT_PAREN; while (g_scanner_peek_next_token (scanner) == token) { token = g_scanner_get_next_token (scanner); switch (token) { case G_TOKEN_LEFT_PAREN: token = G_TOKEN_SYMBOL; break; case G_TOKEN_SYMBOL: if (scanner->value.v_symbol == GINT_TO_POINTER (UNIT_INFO)) { g_scanner_set_scope (scanner, UNIT_INFO); token = gimp_unitrc_unit_info_deserialize (scanner, gimp); if (token == G_TOKEN_RIGHT_PAREN) g_scanner_set_scope (scanner, 0); } break; case G_TOKEN_RIGHT_PAREN: token = G_TOKEN_LEFT_PAREN; break; default: /* do nothing */ break; } } if (token != G_TOKEN_LEFT_PAREN) { g_scanner_get_next_token (scanner); g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, _("fatal parse error"), TRUE); gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message); g_clear_error (&error); gimp_config_file_backup_on_error (file, "unitrc", NULL); } gimp_scanner_destroy (scanner); g_object_unref (file); }
GSList * plug_in_rc_parse (Gimp *gimp, GFile *file, GError **error) { GScanner *scanner; GEnumClass *enum_class; GSList *plug_in_defs = NULL; gint protocol_version = GIMP_PROTOCOL_VERSION; gint file_version = PLUG_IN_RC_FILE_VERSION; GTokenType token; g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL); g_return_val_if_fail (G_IS_FILE (file), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); scanner = gimp_scanner_new_gfile (file, error); if (! scanner) return NULL; enum_class = g_type_class_ref (GIMP_TYPE_ICON_TYPE); g_scanner_scope_add_symbol (scanner, 0, "protocol-version", GINT_TO_POINTER (PROTOCOL_VERSION)); g_scanner_scope_add_symbol (scanner, 0, "file-version", GINT_TO_POINTER (FILE_VERSION)); g_scanner_scope_add_symbol (scanner, 0, "plug-in-def", GINT_TO_POINTER (PLUG_IN_DEF)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "proc-def", GINT_TO_POINTER (PROC_DEF)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "locale-def", GINT_TO_POINTER (LOCALE_DEF)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "help-def", GINT_TO_POINTER (HELP_DEF)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "has-init", GINT_TO_POINTER (HAS_INIT)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "proc-arg", GINT_TO_POINTER (PROC_ARG)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "menu-path", GINT_TO_POINTER (MENU_PATH)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "icon", GINT_TO_POINTER (ICON)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "load-proc", GINT_TO_POINTER (LOAD_PROC)); g_scanner_scope_add_symbol (scanner, PLUG_IN_DEF, "save-proc", GINT_TO_POINTER (SAVE_PROC)); g_scanner_scope_add_symbol (scanner, LOAD_PROC, "extension", GINT_TO_POINTER (EXTENSION)); g_scanner_scope_add_symbol (scanner, LOAD_PROC, "prefix", GINT_TO_POINTER (PREFIX)); g_scanner_scope_add_symbol (scanner, LOAD_PROC, "magic", GINT_TO_POINTER (MAGIC)); g_scanner_scope_add_symbol (scanner, LOAD_PROC, "mime-type", GINT_TO_POINTER (MIME_TYPE)); g_scanner_scope_add_symbol (scanner, LOAD_PROC, "handles-uri", GINT_TO_POINTER (HANDLES_URI)); g_scanner_scope_add_symbol (scanner, LOAD_PROC, "thumb-loader", GINT_TO_POINTER (THUMB_LOADER)); g_scanner_scope_add_symbol (scanner, SAVE_PROC, "extension", GINT_TO_POINTER (EXTENSION)); g_scanner_scope_add_symbol (scanner, SAVE_PROC, "prefix", GINT_TO_POINTER (PREFIX)); g_scanner_scope_add_symbol (scanner, SAVE_PROC, "mime-type", GINT_TO_POINTER (MIME_TYPE)); g_scanner_scope_add_symbol (scanner, SAVE_PROC, "handles-uri", GINT_TO_POINTER (HANDLES_URI)); token = G_TOKEN_LEFT_PAREN; while (protocol_version == GIMP_PROTOCOL_VERSION && file_version == PLUG_IN_RC_FILE_VERSION && g_scanner_peek_next_token (scanner) == token) { token = g_scanner_get_next_token (scanner); switch (token) { case G_TOKEN_LEFT_PAREN: token = G_TOKEN_SYMBOL; break; case G_TOKEN_SYMBOL: switch (GPOINTER_TO_INT (scanner->value.v_symbol)) { case PROTOCOL_VERSION: token = G_TOKEN_INT; if (gimp_scanner_parse_int (scanner, &protocol_version)) token = G_TOKEN_RIGHT_PAREN; break; case FILE_VERSION: token = G_TOKEN_INT; if (gimp_scanner_parse_int (scanner, &file_version)) token = G_TOKEN_RIGHT_PAREN; break; case PLUG_IN_DEF: g_scanner_set_scope (scanner, PLUG_IN_DEF); token = plug_in_def_deserialize (gimp, scanner, &plug_in_defs); g_scanner_set_scope (scanner, 0); break; default: break; } break; case G_TOKEN_RIGHT_PAREN: token = G_TOKEN_LEFT_PAREN; break; default: /* do nothing */ break; } } if (protocol_version != GIMP_PROTOCOL_VERSION || file_version != PLUG_IN_RC_FILE_VERSION || token != G_TOKEN_LEFT_PAREN) { if (protocol_version != GIMP_PROTOCOL_VERSION) { g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_VERSION, _("Skipping '%s': wrong GIMP protocol version."), gimp_file_get_utf8_name (file)); } else if (file_version != PLUG_IN_RC_FILE_VERSION) { g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_VERSION, _("Skipping '%s': wrong pluginrc file format version."), gimp_file_get_utf8_name (file)); } else { g_scanner_get_next_token (scanner); g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, _("fatal parse error"), TRUE); } g_slist_free_full (plug_in_defs, (GDestroyNotify) g_object_unref); plug_in_defs = NULL; } g_type_class_unref (enum_class); gimp_scanner_destroy (scanner); return g_slist_reverse (plug_in_defs); }
void color_history_restore (Gimp *gimp) { GFile *file; GScanner *scanner; GTokenType token; g_return_if_fail (GIMP_IS_GIMP (gimp)); file = gimp_personal_rc_gfile ("colorrc"); if (gimp->be_verbose) g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file)); scanner = gimp_scanner_new_gfile (file, NULL); g_object_unref (file); if (! scanner) return; g_scanner_scope_add_symbol (scanner, 0, "color-history", GINT_TO_POINTER (COLOR_HISTORY)); token = G_TOKEN_LEFT_PAREN; while (g_scanner_peek_next_token (scanner) == token) { token = g_scanner_get_next_token (scanner); switch (token) { case G_TOKEN_LEFT_PAREN: token = G_TOKEN_SYMBOL; break; case G_TOKEN_SYMBOL: if (scanner->value.v_symbol == GINT_TO_POINTER (COLOR_HISTORY)) { while (g_scanner_peek_next_token (scanner) == G_TOKEN_LEFT_PAREN) { GimpRGB color; if (! gimp_scanner_parse_color (scanner, &color)) goto error; color_history_add_from_rc (&color); } } token = G_TOKEN_RIGHT_PAREN; break; case G_TOKEN_RIGHT_PAREN: token = G_TOKEN_LEFT_PAREN; break; default: /* do nothing */ break; } } error: gimp_scanner_destroy (scanner); }
void gimp_action_history_init (Gimp *gimp) { GimpGuiConfig *config; GFile *file; GScanner *scanner; GTokenType token; gint count = 0; gint n_items = 0; g_return_if_fail (GIMP_IS_GIMP (gimp)); config = GIMP_GUI_CONFIG (gimp->config); if (history.items != NULL) { g_warning ("%s: must be run only once.", G_STRFUNC); return; } file = gimp_directory_file (GIMP_ACTION_HISTORY_FILENAME, NULL); if (gimp->be_verbose) g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file)); scanner = gimp_scanner_new_gfile (file, NULL); g_object_unref (file); if (! scanner) return; g_scanner_scope_add_symbol (scanner, 0, "history-item", GINT_TO_POINTER (HISTORY_ITEM)); token = G_TOKEN_LEFT_PAREN; while (g_scanner_peek_next_token (scanner) == token) { token = g_scanner_get_next_token (scanner); switch (token) { case G_TOKEN_LEFT_PAREN: token = G_TOKEN_SYMBOL; break; case G_TOKEN_SYMBOL: if (scanner->value.v_symbol == GINT_TO_POINTER (HISTORY_ITEM)) { gchar *action_name; token = G_TOKEN_STRING; if (g_scanner_peek_next_token (scanner) != token) break; if (! gimp_scanner_parse_string (scanner, &action_name)) break; token = G_TOKEN_INT; if (g_scanner_peek_next_token (scanner) != token || ! gimp_scanner_parse_int (scanner, &count)) { g_free (action_name); break; } history.items = g_list_insert_sorted (history.items, gimp_action_history_item_new (action_name, count), (GCompareFunc) gimp_action_history_init_compare_func); n_items++; g_free (action_name); } token = G_TOKEN_RIGHT_PAREN; break; case G_TOKEN_RIGHT_PAREN: token = G_TOKEN_LEFT_PAREN; if (n_items >= config->action_history_size) goto done; break; default: /* do nothing */ break; } } done: gimp_scanner_destroy (scanner); if (count > 1) { GList *actions; gint i; for (actions = history.items, i = 0; actions && i < config->action_history_size; actions = g_list_next (actions), i++) { GimpActionHistoryItem *action = actions->data; action->count -= count - 1; } } }
void session_init (Gimp *gimp) { GFile *file; GScanner *scanner; GTokenType token; GError *error = NULL; g_return_if_fail (GIMP_IS_GIMP (gimp)); file = session_file (gimp); scanner = gimp_scanner_new_gfile (file, &error); if (! scanner && error->code == GIMP_CONFIG_ERROR_OPEN_ENOENT) { g_clear_error (&error); g_object_unref (file); file = gimp_sysconf_directory_file ("sessionrc", NULL); scanner = gimp_scanner_new_gfile (file, NULL); } if (! scanner) { g_clear_error (&error); g_object_unref (file); return; } if (gimp->be_verbose) g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file)); g_scanner_scope_add_symbol (scanner, 0, "session-info", GINT_TO_POINTER (SESSION_INFO)); g_scanner_scope_add_symbol (scanner, 0, "hide-docks", GINT_TO_POINTER (HIDE_DOCKS)); g_scanner_scope_add_symbol (scanner, 0, "single-window-mode", GINT_TO_POINTER (SINGLE_WINDOW_MODE)); g_scanner_scope_add_symbol (scanner, 0, "show-tabs", GINT_TO_POINTER (SHOW_TABS)); g_scanner_scope_add_symbol (scanner, 0, "tabs-position", GINT_TO_POINTER (TABS_POSITION)); g_scanner_scope_add_symbol (scanner, 0, "last-tip-shown", GINT_TO_POINTER (LAST_TIP_SHOWN)); token = G_TOKEN_LEFT_PAREN; while (g_scanner_peek_next_token (scanner) == token) { token = g_scanner_get_next_token (scanner); switch (token) { case G_TOKEN_LEFT_PAREN: token = G_TOKEN_SYMBOL; break; case G_TOKEN_SYMBOL: if (scanner->value.v_symbol == GINT_TO_POINTER (SESSION_INFO)) { GimpDialogFactory *factory = NULL; GimpSessionInfo *info = NULL; gchar *factory_name = NULL; gchar *entry_name = NULL; GimpDialogFactoryEntry *entry = NULL; token = G_TOKEN_STRING; if (! gimp_scanner_parse_string (scanner, &factory_name)) break; /* In versions <= GIMP 2.6 there was a "toolbox", a * "dock", a "display" and a "toplevel" factory. These * are now merged to a single gimp_dialog_factory_get_singleton (). We * need the legacy name though, so keep it around. */ factory = gimp_dialog_factory_get_singleton (); info = gimp_session_info_new (); /* GIMP 2.6 has the entry name as part of the * session-info header, so try to get it */ gimp_scanner_parse_string (scanner, &entry_name); if (entry_name) { /* Previously, GimpDock was a toplevel. That is why * versions <= GIMP 2.6 has "dock" as the entry name. We * want "dock" to be interpreted as 'dock window' * however so have some special-casing for that. When * the entry name is "dock" the factory name is either * "dock" or "toolbox". */ if (strcmp (entry_name, "dock") == 0) { entry = gimp_dialog_factory_find_entry (factory, (strcmp (factory_name, "toolbox") == 0 ? "gimp-toolbox-window" : "gimp-dock-window")); } else { entry = gimp_dialog_factory_find_entry (factory, entry_name); } } /* We're done with these now */ g_free (factory_name); g_free (entry_name); /* We can get the factory entry either now (the GIMP <= * 2.6 way), or when we deserialize (the GIMP 2.8 way) */ if (entry) { gimp_session_info_set_factory_entry (info, entry); } /* Always try to deserialize */ if (gimp_config_deserialize (GIMP_CONFIG (info), scanner, 1, NULL)) { /* Make sure we got a factory entry either the 2.6 * or 2.8 way */ if (gimp_session_info_get_factory_entry (info)) { GIMP_LOG (DIALOG_FACTORY, "successfully parsed and added session info %p", info); gimp_dialog_factory_add_session_info (factory, info); } else { GIMP_LOG (DIALOG_FACTORY, "failed to parse session info %p, not adding", info); } g_object_unref (info); } else { g_object_unref (info); /* set token to left paren to we won't set another * error below, gimp_config_deserialize() already did */ token = G_TOKEN_LEFT_PAREN; goto error; } } else if (scanner->value.v_symbol == GINT_TO_POINTER (HIDE_DOCKS)) { gboolean hide_docks; token = G_TOKEN_IDENTIFIER; if (! gimp_scanner_parse_boolean (scanner, &hide_docks)) break; g_object_set (gimp->config, "hide-docks", hide_docks, NULL); } else if (scanner->value.v_symbol == GINT_TO_POINTER (SINGLE_WINDOW_MODE)) { gboolean single_window_mode; token = G_TOKEN_IDENTIFIER; if (! gimp_scanner_parse_boolean (scanner, &single_window_mode)) break; g_object_set (gimp->config, "single-window-mode", single_window_mode, NULL); } else if (scanner->value.v_symbol == GINT_TO_POINTER (SHOW_TABS)) { gboolean show_tabs; token = G_TOKEN_IDENTIFIER; if (! gimp_scanner_parse_boolean (scanner, &show_tabs)) break; g_object_set (gimp->config, "show-tabs", show_tabs, NULL); } else if (scanner->value.v_symbol == GINT_TO_POINTER (TABS_POSITION)) { gint tabs_position; token = G_TOKEN_INT; if (! gimp_scanner_parse_int (scanner, &tabs_position)) break; g_object_set (gimp->config, "tabs-position", tabs_position, NULL); } else if (scanner->value.v_symbol == GINT_TO_POINTER (LAST_TIP_SHOWN)) { gint last_tip_shown; token = G_TOKEN_INT; if (! gimp_scanner_parse_int (scanner, &last_tip_shown)) break; g_object_set (gimp->config, "last-tip-shown", last_tip_shown, NULL); } token = G_TOKEN_RIGHT_PAREN; break; case G_TOKEN_RIGHT_PAREN: token = G_TOKEN_LEFT_PAREN; break; default: /* do nothing */ break; } } error: if (token != G_TOKEN_LEFT_PAREN) { g_scanner_get_next_token (scanner); g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, _("fatal parse error"), TRUE); } if (error) { gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message); g_clear_error (&error); gimp_config_file_backup_on_error (file, "sessionrc", NULL); } gimp_scanner_destroy (scanner); g_object_unref (file); dialogs_load_recent_docks (gimp); }
void gimp_modules_load (Gimp *gimp) { GFile *file; GScanner *scanner; gchar *module_load_inhibit = NULL; g_return_if_fail (GIMP_IS_GIMP (gimp)); if (gimp->no_interface) return; /* FIXME, gimp->be_verbose is not yet initialized in init() */ gimp->module_db->verbose = gimp->be_verbose; file = gimp_directory_file ("modulerc", NULL); if (gimp->be_verbose) g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file)); scanner = gimp_scanner_new_gfile (file, NULL); g_object_unref (file); if (scanner) { GTokenType token; GError *error = NULL; #define MODULE_LOAD_INHIBIT 1 g_scanner_scope_add_symbol (scanner, 0, "module-load-inhibit", GINT_TO_POINTER (MODULE_LOAD_INHIBIT)); token = G_TOKEN_LEFT_PAREN; while (g_scanner_peek_next_token (scanner) == token) { token = g_scanner_get_next_token (scanner); switch (token) { case G_TOKEN_LEFT_PAREN: token = G_TOKEN_SYMBOL; break; case G_TOKEN_SYMBOL: if (scanner->value.v_symbol == GINT_TO_POINTER (MODULE_LOAD_INHIBIT)) { token = G_TOKEN_STRING; if (! gimp_scanner_parse_string_no_validate (scanner, &module_load_inhibit)) goto error; } token = G_TOKEN_RIGHT_PAREN; break; case G_TOKEN_RIGHT_PAREN: token = G_TOKEN_LEFT_PAREN; break; default: /* do nothing */ break; } } #undef MODULE_LOAD_INHIBIT if (token != G_TOKEN_LEFT_PAREN) { g_scanner_get_next_token (scanner); g_scanner_unexp_token (scanner, token, NULL, NULL, NULL, _("fatal parse error"), TRUE); } error: if (error) { gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message); g_clear_error (&error); } gimp_scanner_destroy (scanner); } if (module_load_inhibit) { gimp_module_db_set_load_inhibit (gimp->module_db, module_load_inhibit); g_free (module_load_inhibit); } gimp_module_db_load (gimp->module_db, gimp->config->module_path); }