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"
55static void gnc_plugin_file_history_finalize (GObject *
object);
57static void gnc_plugin_file_history_add_to_window (GncPlugin *plugin, GncMainWindow *window, GQuark type);
58static void gnc_plugin_file_history_remove_from_window (GncPlugin *plugin, GncMainWindow *window, GQuark type);
62static QofLogModule log_module = GNC_MOD_GUI;
65static 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"
80static 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 },
94static guint gnc_plugin_n_actions = G_N_ELEMENTS(gnc_plugin_actions);
97static const gchar *gnc_plugin_load_ui_items [] =
105 GncPlugin gnc_plugin;
121gnc_history_index_to_pref_name (guint index)
123 return g_strdup_printf(HISTORY_STRING_FILE_N, index);
136gnc_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);
331gnc_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);
372gnc_history_generate_tooltip (
int index,
const gchar *filename)
403gnc_history_update_action (GncMainWindow *window,
405 const gchar *filename)
407 GncMenuModelSearch *gsm = g_new0 (GncMenuModelSearch, 1);
411 ENTER(
"window %p, index %d, filename %s", window, index,
412 filename ? filename :
"(null)");
414 action_name = g_strdup_printf (
"RecentFile%dAction", index);
416 gsm->search_action_label = NULL;
417 gsm->search_action_name = action_name;
421 g_menu_remove (G_MENU(gsm->model), gsm->index);
425 gsm->search_action_name =
"FilePlaceholder6";
429 LEAVE(
"Could not find 'menu_item' with action name '%s'", action_name);
431 g_free (action_name);
439 if (filename && *filename && index < limit)
442 gchar *label_name = gnc_history_generate_label (index, filename);
443 gchar *tooltip = gnc_history_generate_tooltip (index, filename);
447 item = g_menu_item_new (label_name, full_action_name);
448 g_menu_item_set_attribute (item, GNC_MENU_ATTRIBUTE_TOOLTIP,
"s", tooltip);
449 g_menu_insert_item (G_MENU(gsm->model), gsm->index, item);
451 g_free (full_action_name);
454 g_object_unref (item);
457 g_free (action_name);
471gnc_history_update_menus (GncMainWindow *window)
473 gchar *filename, *pref;
478 for (i = 0; i < MAX_HISTORY_FILES; i++)
480 pref = gnc_history_index_to_pref_name(i);
482 gnc_history_update_action(window, i, filename);
502gnc_plugin_history_list_changed (gpointer prefs,
506 GncMainWindow *window;
511 window = GNC_MAIN_WINDOW(user_data);
513 if (strcmp(pref, GNC_PREF_HISTORY_MAXFILES) == 0)
515 gnc_history_update_menus (window);
516 LEAVE(
"updated maxfiles");
519 index = gnc_history_pref_name_to_index(pref);
527 gnc_history_update_action (window, index, filename);
537G_DEFINE_TYPE(GncPluginFileHistory, gnc_plugin_file_history, GNC_TYPE_PLUGIN)
541gnc_plugin_file_history_class_init (GncPluginFileHistoryClass *klass)
543 GObjectClass *object_class = G_OBJECT_CLASS (klass);
544 GncPluginClass *plugin_class = GNC_PLUGIN_CLASS (klass);
546 object_class->finalize = gnc_plugin_file_history_finalize;
549 plugin_class->plugin_name = GNC_PLUGIN_FILE_HISTORY_NAME;
552 plugin_class->add_to_window = gnc_plugin_file_history_add_to_window;
553 plugin_class->remove_from_window = gnc_plugin_file_history_remove_from_window;
557 plugin_class->actions = gnc_plugin_actions;
558 plugin_class->n_actions = gnc_plugin_n_actions;
560 plugin_class->ui_updates = gnc_plugin_load_ui_items;
566gnc_plugin_file_history_init (GncPluginFileHistory *plugin)
568 ENTER(
"plugin %p", plugin);
575gnc_plugin_file_history_finalize (GObject *
object)
577 g_return_if_fail (GNC_IS_PLUGIN_FILE_HISTORY (
object));
579 ENTER(
"plugin %p",
object);
580 G_OBJECT_CLASS (gnc_plugin_file_history_parent_class)->finalize (
object);
591 GncPlugin *plugin_page = NULL;
594 plugin_page = GNC_PLUGIN (g_object_new (GNC_TYPE_PLUGIN_FILE_HISTORY, NULL));
595 LEAVE(
"plugin %p", plugin_page);
620gnc_plugin_file_history_add_to_window (GncPlugin *plugin,
621 GncMainWindow *window,
625 gnc_plugin_history_list_changed, window);
626 gnc_history_update_menus(window);
642gnc_plugin_file_history_remove_from_window (GncPlugin *plugin,
643 GncMainWindow *window,
647 gnc_plugin_history_list_changed, window);
667gnc_plugin_file_history_cmd_open_file (GSimpleAction *simple,
673 gchar *filename, *pref, *index;
674 const gchar *action_name;
676 g_return_if_fail (G_IS_SIMPLE_ACTION(simple));
677 g_return_if_fail (data != NULL);
682 action_name = g_action_get_name (G_ACTION(simple));
684 index = g_utf8_substring (action_name, 10, 11);
686 pref = gnc_history_index_to_pref_name (atoi (index));
689 PINFO(
"File to open is '%s' on action '%s'", filename, action_name);
691 gnc_window_set_progressbar_window (GNC_WINDOW(data->window));
693 gnc_file_open_file (GTK_WINDOW (data->window),
695 gnc_window_set_progressbar_window (NULL);
All type declarations for the whole Gnucash engine.
GKeyFile helper routines.
Functions for adding content to a window.
Functions providing the file history menu.
Generic api to store and retrieve preferences.
Utility functions for convert uri in separate components and back.
Functions that are supported by all types of windows.
gboolean gnc_main_window_finish_pending(GncMainWindow *window)
Tell a window to finish any outstanding activities.
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.
#define PINFO(format, args...)
Print an informational note.
#define LEAVE(format, args...)
Print a function exit debugging message.
#define ENTER(format, args...)
Print a function entry debugging message.
void gnc_history_add_file(const char *newfile)
Add a file name to the front of the file "history list".
#define PLUGIN_ACTIONS_NAME
The label given to the main window for this plugin.
void gnc_history_remove_file(const char *oldfile)
Remove all occurrences of a file name from the history list.
#define PLUGIN_UI_FILENAME
The name of the UI description file for this plugin.
gboolean gnc_history_test_for_file(const char *oldfile)
Test for a file name existing in the history list.
char * gnc_history_get_last(void)
Retrieve the name of the file most recently accessed.
GncPlugin * gnc_plugin_file_history_new(void)
Create a new file history 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.
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.
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
void gnc_prefs_reset(const gchar *group, const gchar *pref_name)
Reset a preference to its default value in the preferences backend.
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from 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.
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
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.
gchar * gnc_uri_normalize_uri(const gchar *uri, gboolean allow_password)
Composes a normalized uri starting from any uri (filename, db spec,...).
The instance data structure for a file history plugin.