35 #include <glib/gi18n.h> 36 #include <glib/gprintf.h> 49 #define FILENAME_STRING "filename" 50 #define MAX_HISTORY_FILES 10 51 #define GNC_PREFS_GROUP_HISTORY "history" 52 #define GNC_PREF_HISTORY_MAXFILES "maxfiles" 53 #define HISTORY_STRING_FILE_N "file%d" 55 static void gnc_plugin_file_history_finalize (GObject *
object);
57 static void gnc_plugin_file_history_add_to_window (GncPlugin *plugin,
GncMainWindow *window, GQuark type);
58 static void gnc_plugin_file_history_remove_from_window (GncPlugin *plugin,
GncMainWindow *window, GQuark type);
62 static QofLogModule log_module = GNC_MOD_GUI;
65 static void gnc_plugin_file_history_cmd_open_file (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
68 #define PLUGIN_ACTIONS_NAME "gnc-plugin-file-history-actions" 70 #define PLUGIN_UI_FILENAME "gnc-plugin-file-history.ui" 72 #define GNOME1_HISTORY "History" 73 #define GNOME1_MAXFILES "MaxFiles" 80 static GActionEntry gnc_plugin_actions [] =
82 {
"RecentFile0Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
83 {
"RecentFile1Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
84 {
"RecentFile2Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
85 {
"RecentFile3Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
86 {
"RecentFile4Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
87 {
"RecentFile5Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
88 {
"RecentFile6Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
89 {
"RecentFile7Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
90 {
"RecentFile8Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
91 {
"RecentFile9Action", gnc_plugin_file_history_cmd_open_file, NULL, NULL, NULL },
94 static guint gnc_plugin_n_actions = G_N_ELEMENTS(gnc_plugin_actions);
97 static const gchar *gnc_plugin_load_ui_items [] =
105 GncPlugin gnc_plugin;
121 gnc_history_index_to_pref_name (guint index)
123 return g_strdup_printf(HISTORY_STRING_FILE_N, index);
136 gnc_history_pref_name_to_index (
const gchar *pref)
140 result = sscanf(pref, HISTORY_STRING_FILE_N, &index);
143 if ((index < 0) || (index >= gnc_plugin_n_actions))
157 gchar *filename, *from, *to;
162 if (!g_utf8_validate(newfile, -1, NULL))
168 last = MAX_HISTORY_FILES - 1;
169 for (i = 0; i < MAX_HISTORY_FILES; i++)
171 from = gnc_history_index_to_pref_name(i);
180 if (g_utf8_collate(newfile, filename) == 0)
192 to = gnc_history_index_to_pref_name(last);
193 for (i = last - 1; i >= 0; i--)
195 from = gnc_history_index_to_pref_name(i);
197 if (filename && *filename)
226 gchar *filename, *from, *to;
231 if (!g_utf8_validate(oldfile, -1, NULL))
234 for (i = 0, j = 0; i < MAX_HISTORY_FILES; i++)
236 from = gnc_history_index_to_pref_name(i);
241 if (g_utf8_collate(oldfile, filename) == 0)
249 to = gnc_history_index_to_pref_name(j);
269 gchar *filename, *from;
271 gboolean found = FALSE;
275 if (!g_utf8_validate(oldfile, -1, NULL))
278 for (i = 0; i < MAX_HISTORY_FILES; i++)
280 from = gnc_history_index_to_pref_name(i);
287 if (g_utf8_collate(oldfile, filename) == 0)
307 char *filename, *pref;
309 pref = gnc_history_index_to_pref_name(0);
331 gnc_history_generate_label (
int index,
const gchar *filename)
333 gchar *label, *result;
340 label = g_path_get_basename ( filepath );
350 splitlabel = g_strsplit ( label,
"_", 0);
352 label = g_strjoinv (
"__", splitlabel);
353 g_strfreev (splitlabel);
355 result = g_strdup_printf (
"_%d %s", (index + 1) % 10, label);
372 gnc_history_generate_tooltip (
int index,
const gchar *filename)
405 const gchar *filename)
407 GncMenuModelSearch *gsm = g_new0 (GncMenuModelSearch, 1);
410 gboolean add_item = FALSE;
413 ENTER(
"window %p, index %d, filename %s", window, index,
414 filename ? filename :
"(null)");
416 action_name = g_strdup_printf (
"RecentFile%dAction", index);
418 gsm->search_action_label = NULL;
419 gsm->search_action_name = action_name;
424 gsm->search_action_name =
"FilePlaceholder6";
428 LEAVE(
"Could not find 'menu_item' with action name '%s'", action_name);
430 g_free (action_name);
434 pos = gsm->index + index;
440 GNC_PREF_HISTORY_MAXFILES);
442 if (filename && (strlen(filename) > 0) && (index < limit))
445 gchar *label_name = gnc_history_generate_label (index, filename);
446 gchar *tooltip = gnc_history_generate_tooltip (index, filename);
450 item = g_menu_item_new (label_name, full_action_name);
452 g_menu_item_set_attribute (item, GNC_MENU_ATTRIBUTE_TOOLTIP,
"s", tooltip);
455 g_menu_remove (G_MENU(gsm->model), pos);
457 g_menu_insert_item (G_MENU(gsm->model), pos, item);
459 g_free (full_action_name);
462 g_object_unref (item);
465 g_free (action_name);
481 gchar *filename, *pref;
486 for (i = 0; i < MAX_HISTORY_FILES; i++)
488 pref = gnc_history_index_to_pref_name(i);
490 gnc_history_update_action(window, i, filename);
510 gnc_plugin_history_list_changed (gpointer prefs,
519 window = GNC_MAIN_WINDOW(user_data);
521 if (strcmp(pref, GNC_PREF_HISTORY_MAXFILES) == 0)
523 gnc_history_update_menus (window);
524 LEAVE(
"updated maxfiles");
527 index = gnc_history_pref_name_to_index(pref);
535 gnc_history_update_action (window, index, filename);
545 G_DEFINE_TYPE(GncPluginFileHistory, gnc_plugin_file_history, GNC_TYPE_PLUGIN)
549 gnc_plugin_file_history_class_init (GncPluginFileHistoryClass *klass)
551 GObjectClass *object_class = G_OBJECT_CLASS (klass);
552 GncPluginClass *plugin_class = GNC_PLUGIN_CLASS (klass);
554 object_class->finalize = gnc_plugin_file_history_finalize;
557 plugin_class->plugin_name = GNC_PLUGIN_FILE_HISTORY_NAME;
560 plugin_class->add_to_window = gnc_plugin_file_history_add_to_window;
561 plugin_class->remove_from_window = gnc_plugin_file_history_remove_from_window;
565 plugin_class->actions = gnc_plugin_actions;
566 plugin_class->n_actions = gnc_plugin_n_actions;
568 plugin_class->ui_updates = gnc_plugin_load_ui_items;
574 gnc_plugin_file_history_init (GncPluginFileHistory *plugin)
576 ENTER(
"plugin %p", plugin);
583 gnc_plugin_file_history_finalize (GObject *
object)
585 g_return_if_fail (GNC_IS_PLUGIN_FILE_HISTORY (
object));
587 ENTER(
"plugin %p",
object);
588 G_OBJECT_CLASS (gnc_plugin_file_history_parent_class)->finalize (
object);
599 GncPlugin *plugin_page = NULL;
602 plugin_page = GNC_PLUGIN (g_object_new (GNC_TYPE_PLUGIN_FILE_HISTORY, NULL));
603 LEAVE(
"plugin %p", plugin_page);
628 gnc_plugin_file_history_add_to_window (GncPlugin *plugin,
633 gnc_plugin_history_list_changed, window);
634 gnc_history_update_menus(window);
650 gnc_plugin_file_history_remove_from_window (GncPlugin *plugin,
655 gnc_plugin_history_list_changed, window);
675 gnc_plugin_file_history_cmd_open_file (GSimpleAction *simple,
681 gchar *filename, *pref, *index;
682 const gchar *action_name;
684 g_return_if_fail (G_IS_SIMPLE_ACTION(simple));
685 g_return_if_fail (data != NULL);
690 action_name = g_action_get_name (G_ACTION(simple));
692 index = g_utf8_substring (action_name, 10, 11);
694 pref = gnc_history_index_to_pref_name (atoi (index));
697 PINFO(
"File to open is '%s' on action '%s'", filename, action_name);
699 gnc_window_set_progressbar_window (GNC_WINDOW(data->window));
701 gnc_file_open_file (GTK_WINDOW (data->window),
703 gnc_window_set_progressbar_window (NULL);
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from the preferences backend.
gulong gnc_prefs_register_cb(const char *group, const gchar *pref_name, gpointer func, gpointer user_data)
Register a callback that gets triggered when the given preference changes.
void gnc_history_add_file(const char *newfile)
Add a file name to the front of the file "history list".
#define PINFO(format, args...)
Print an informational note.
GMenuModel * gnc_main_window_get_menu_model(GncMainWindow *window)
Return the GMenuModel for the main window menu bar.
gboolean gnc_menubar_model_find_item(GMenuModel *menu_model, GncMenuModelSearch *gsm)
Find a GtkMenu item from the action name.
Functions that are supported by all types of windows.
GncPlugin * gnc_plugin_file_history_new(void)
Create a new file history plugin.
GKeyFile helper routines.
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
void gnc_prefs_reset(const gchar *group, const gchar *pref_name)
Reset a preference to its default value in the preferences backend.
gboolean gnc_prefs_set_string(const gchar *group, const gchar *pref_name, const gchar *value)
Store a string into the preferences backend.
#define ENTER(format, args...)
Print a function entry debugging message.
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
Functions for adding content to a window.
char * gnc_history_get_last(void)
Retrieve the name of the file most recently accessed.
gchar * gnc_uri_normalize_uri(const gchar *uri, gboolean allow_password)
Composes a normalized uri starting from any uri (filename, db spec,...).
Functions providing the file history menu.
void gnc_history_remove_file(const char *oldfile)
Remove all occurrences of a file name from the history list.
All type declarations for the whole Gnucash engine.
gboolean gnc_main_window_finish_pending(GncMainWindow *window)
Tell a window to finish any outstanding activities.
Generic api to store and retrieve preferences.
gboolean gnc_uri_targets_local_fs(const gchar *uri)
Checks if the given uri is either a valid file uri or a local filesystem path.
gboolean gnc_history_test_for_file(const char *oldfile)
Test for a file name existing in the history list.
#define PLUGIN_ACTIONS_NAME
The label given to the main window for this plugin.
#define LEAVE(format, args...)
Print a function exit debugging message.
Utility functions for convert uri in separate components and back.
The instance data structure for a main window object.
The instance data structure for a file history plugin.
#define PLUGIN_UI_FILENAME
The name of the UI description file for this plugin.
void gnc_prefs_remove_cb_by_func(const gchar *group, const gchar *pref_name, gpointer func, gpointer user_data)
Remove a function that was registered for a callback when the given preference changed.