After opening a file, display a statusbar message with the last modification date and time.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@23185 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2013-09-20 21:09:46 +00:00
parent 517a621d9a
commit a46dba1201

View File

@ -72,6 +72,10 @@
#ifdef MAC_INTEGRATION
#include <gtkmacintegration/gtkosxapplication.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/types.h>
# include <sys/stat.h> // for stat(2)
#endif
/** Names of signals generated by the main window. */
enum
@ -156,6 +160,8 @@ static void gnc_main_window_cmd_help_about (GtkAction *action, GncMainWindow *wi
static void do_popup_menu(GncPluginPage *page, GdkEventButton *event);
static gboolean gnc_main_window_popup_menu_cb (GtkWidget *widget, GncPluginPage *page);
static GtkWidget *gnc_main_window_get_statusbar (GncWindow *window_in);
static void statusbar_notification_lastmodified(void);
#ifdef MAC_INTEGRATION
static void gnc_quartz_shutdown(GtkosxApplication *theApp, gpointer data);
@ -890,6 +896,8 @@ gnc_main_window_restore_all_windows(const GKeyFile *keyfile)
gnc_main_window_restore_window(window, &data);
}
gnc_unset_busy_cursor (NULL);
statusbar_notification_lastmodified();
}
void
@ -1500,6 +1508,126 @@ gnc_main_window_attach_to_book (QofSession *session)
#endif
}
static guint gnc_statusbar_notification_messageid = 0;
//#define STATUSBAR_NOTIFICATION_AUTOREMOVAL
#ifdef STATUSBAR_NOTIFICATION_AUTOREMOVAL
/* Removes the statusbar notification again that has been pushed to the
* statusbar by generate_statusbar_lastmodified_message. */
static gboolean statusbar_notification_off(gpointer user_data_unused)
{
GtkWidget *widget = gnc_ui_get_toplevel();
//g_warning("statusbar_notification_off\n");
if (gnc_statusbar_notification_messageid == 0)
return FALSE;
if (widget && GNC_IS_MAIN_WINDOW(widget))
{
GncMainWindow *mainwindow = GNC_MAIN_WINDOW(widget);
GtkWidget *statusbar = gnc_main_window_get_statusbar(GNC_WINDOW(mainwindow));
gtk_statusbar_remove(GTK_STATUSBAR(statusbar), 0, gnc_statusbar_notification_messageid);
gnc_statusbar_notification_messageid = 0;
} else {
g_warning("oops, no GncMainWindow obtained\n");
}
return FALSE; // should not be called again
}
#endif // STATUSBAR_NOTIFICATION_AUTOREMOVAL
/* Creates a statusbar message stating the last modification time of the opened
* data file. */
static gchar *generate_statusbar_lastmodified_message()
{
gchar *message = NULL;
const gchar *book_id = NULL;
if (gnc_current_session_exist())
{
book_id = qof_session_get_url (gnc_get_current_session ());
}
if (!book_id)
return NULL;
else
{
if ( gnc_uri_is_file_uri ( book_id ) )
{
#ifdef HAVE_SYS_STAT_H
/* The filename is a true file. */
gchar *filepath = gnc_uri_get_path ( book_id );
gchar *filename = g_path_get_basename ( filepath );
{
// Access the mtime information through stat(2)
struct stat statbuf;
int r = stat(filepath, &statbuf);
if (r == 0)
{
// File mtime could be accessed ok
gint64 mtime = statbuf.st_mtime;
GDateTime *gdt = gnc_g_date_time_new_from_unix_local (mtime);
/* Translators: This is the date and time that is shown in
the status bar after opening a file, the date and time of
last modification. */
gchar *time_string = g_date_time_format (gdt, _("%a %b %e %Y %H:%M:%S"));
g_date_time_unref (gdt);
//g_warning("got time %ld, str=%s\n", mtime, time_string);
/* Translators: This message appears in the status bar after opening the file. */
message = g_strdup_printf(_("File %s opened. Last modified: %s"),
filename, time_string);
g_free(time_string);
}
else
{
g_warning("Unable to read mtime for file %s\n", filepath);
// message is still NULL
}
}
g_free(filename);
g_free(filepath);
#else
return NULL;
#endif
}
// If the URI is not a file but a database, we can maybe also show
// something useful, but I have no idea how to obtain this information.
}
return message;
}
static void
statusbar_notification_lastmodified()
{
// First look up the first GncMainWindow to set the statusbar there
GList *iter;
GtkWidget *widget = NULL;
for (iter = active_windows; iter && !(widget && GNC_IS_MAIN_WINDOW(widget));
iter = g_list_next(iter))
{
widget = iter->data;
}
if (widget && GNC_IS_MAIN_WINDOW(widget))
{
// Ok, we found a mainwindow where we can set a statusbar message
GncMainWindow *mainwindow = GNC_MAIN_WINDOW(widget);
GtkWidget *statusbar = gnc_main_window_get_statusbar(GNC_WINDOW(mainwindow));
gchar *msg = generate_statusbar_lastmodified_message();
if (msg)
{
gnc_statusbar_notification_messageid = gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, msg);
}
g_free(msg);
#ifdef STATUSBAR_NOTIFICATION_AUTOREMOVAL
// Also register a timeout callback to remove that statusbar
// notification again after 10 seconds
g_timeout_add(10 * 1000, statusbar_notification_off, NULL); // maybe not needed anyway?
#endif
} else {
g_warning("uh oh, no GNC_IS_MAIN_WINDOW\n");
}
}
/** This data structure is used to describe the requested state of a
* GtkRadioAction, and us used to pass data among several