From 486ad3625ca52b318f5514f782b90f1eda0b87e7 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Thu, 24 Feb 2011 15:02:00 -0500 Subject: have spinners work in unison -- only save when neither is focused --- src/datetime-prefs.c | 124 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 92 insertions(+), 32 deletions(-) diff --git a/src/datetime-prefs.c b/src/datetime-prefs.c index fbadd27..d053c7e 100644 --- a/src/datetime-prefs.c +++ b/src/datetime-prefs.c @@ -46,6 +46,11 @@ GDBusProxy * proxy = NULL; GtkWidget * auto_radio = NULL; GtkWidget * tz_entry = NULL; CcTimezoneMap * tzmap = NULL; +GtkWidget * time_spin = NULL; +GtkWidget * date_spin = NULL; +GDateTime * manual_time = NULL; +guint save_time_id = 0; +gboolean user_edited_time = FALSE; /* Turns the boolean property into a string gsettings */ static GVariant * @@ -277,13 +282,56 @@ void proxy_ready (GObject *object, GAsyncResult *res, gpointer user_data) NULL, tz_query_answered, NULL); } +static gboolean +are_spinners_focused (void) +{ + // save_time_id means that we were in focus and haven't finished our save + // yet, so act like we are still focused. + return save_time_id || gtk_widget_has_focus (time_spin) || gtk_widget_has_focus (date_spin); +} + +static gboolean +save_time (gpointer user_data) +{ + if (user_edited_time) { + g_dbus_proxy_call (proxy, "SetTime", g_variant_new ("(x)", g_date_time_to_unix (manual_time)), + G_DBUS_CALL_FLAGS_NONE, -1, NULL, dbus_set_answered, "time"); + } + user_edited_time = FALSE; + save_time_id = 0; + return FALSE; +} + +static gboolean +spin_focus_in (void) +{ + if (save_time_id > 0) { + g_source_remove (save_time_id); + save_time_id = 0; + } + return FALSE; +} + +static gboolean +spin_focus_out (void) +{ + /* We want to only save when both spinners are unfocused. But it's difficult + to tell who is about to get focus during a focus-out. So we set an idle + callback to save the time if we don't focus in to another spinner by that + time. */ + if (save_time_id == 0) { + save_time_id = g_idle_add ((GSourceFunc)save_time, NULL); + } + return FALSE; +} + static int input_time_text (GtkWidget * spinner, gdouble *value, gpointer user_data) { gboolean is_time = (gboolean)GPOINTER_TO_INT (g_object_get_data (G_OBJECT (spinner), "is-time")); const gchar * text = gtk_entry_get_text (GTK_ENTRY (spinner)); - GDateTime * now = g_date_time_new_now_local (); + GDateTime * now = manual_time; gint year, month, day, hour, minute, second; year = g_date_time_get_year (now); month = g_date_time_get_month (now); @@ -291,7 +339,6 @@ input_time_text (GtkWidget * spinner, gdouble *value, gpointer user_data) hour = g_date_time_get_hour (now); minute = g_date_time_get_minute (now); second = g_date_time_get_second (now); - g_date_time_unref (now); /* Parse this string as if it were in the output format */ gint scanned = 0; @@ -354,10 +401,9 @@ input_time_text (GtkWidget * spinner, gdouble *value, gpointer user_data) return TRUE; } - GDateTime * datetime = g_date_time_new_local (year, month, day, hour, minute, second); - - g_dbus_proxy_call (proxy, "SetTime", g_variant_new ("(x)", g_date_time_to_unix (datetime)), - G_DBUS_CALL_FLAGS_NONE, -1, NULL, dbus_set_answered, "time"); + g_date_time_unref (manual_time); + manual_time = g_date_time_new_local (year, month, day, hour, minute, second); + user_edited_time = TRUE; return TRUE; } @@ -365,12 +411,6 @@ input_time_text (GtkWidget * spinner, gdouble *value, gpointer user_data) static gboolean format_time_text (GtkWidget * spinner, gpointer user_data) { - if (gtk_widget_has_focus (spinner)) { - /* Don't do anything if we have focus, user is likely editing us */ - return TRUE; - } - - GDateTime * datetime = (GDateTime *)g_object_get_data (G_OBJECT (spinner), "datetime"); gboolean is_time = (gboolean)GPOINTER_TO_INT (g_object_get_data (G_OBJECT (spinner), "is-time")); const gchar * format; @@ -385,39 +425,60 @@ format_time_text (GtkWidget * spinner, gpointer user_data) format = "%Y-%m-%d"; } - gchar * formatted = g_date_time_format (datetime, format); + gchar * formatted = g_date_time_format (manual_time, format); gtk_entry_set_text (GTK_ENTRY (spinner), formatted); return TRUE; } static gboolean -update_spinner (GtkWidget * spinner) +update_spinners (void) { /* Add datetime object to spinner, which will hold the real time value, rather - then using the value of the spinner itself. */ - GDateTime * datetime = g_date_time_new_now_local (); - g_object_set_data_full (G_OBJECT (spinner), "datetime", datetime, (GDestroyNotify)g_date_time_unref); - - format_time_text (spinner, NULL); - + then using the value of the spinner itself. And don't update while user is + editing. */ + if (!are_spinners_focused ()) { + if (manual_time != NULL) { + g_date_time_unref (manual_time); + } + manual_time = g_date_time_new_now_local (); + format_time_text (time_spin, NULL); + format_time_text (date_spin, NULL); + } return TRUE; } static void -setup_time_spinner (GtkWidget * spinner, GtkWidget * other, gboolean is_time) +setup_time_spinners (GtkWidget * time, GtkWidget * date) { - /* Set up spinner to have reasonable behavior */ - gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinner), FALSE); - g_signal_connect (spinner, "input", G_CALLBACK (input_time_text), other); - g_signal_connect (spinner, "output", G_CALLBACK (format_time_text), other); - g_object_set_data (G_OBJECT (spinner), "is-time", GINT_TO_POINTER (is_time)); + gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (time), FALSE); + gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (date), FALSE); - /* 2 seconds is what the indicator itself uses */ - guint time_id = g_timeout_add_seconds (2, (GSourceFunc)update_spinner, spinner); - g_signal_connect_swapped (spinner, "destroy", G_CALLBACK (g_source_remove), GINT_TO_POINTER (time_id)); + gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (time), TRUE); + gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (date), TRUE); + + g_signal_connect (time, "input", G_CALLBACK (input_time_text), date); + g_signal_connect (date, "input", G_CALLBACK (input_time_text), time); + + g_signal_connect (time, "output", G_CALLBACK (format_time_text), date); + g_signal_connect (date, "output", G_CALLBACK (format_time_text), time); + + g_signal_connect (time, "focus-in-event", G_CALLBACK (spin_focus_in), date); + g_signal_connect (date, "focus-in-event", G_CALLBACK (spin_focus_in), time); - update_spinner (spinner); + g_signal_connect (time, "focus-out-event", G_CALLBACK (spin_focus_out), date); + g_signal_connect (date, "focus-out-event", G_CALLBACK (spin_focus_out), time); + + g_object_set_data (G_OBJECT (time), "is-time", GINT_TO_POINTER (TRUE)); + g_object_set_data (G_OBJECT (date), "is-time", GINT_TO_POINTER (FALSE)); + + time_spin = time; + date_spin = date; + + /* 2 seconds is what the indicator itself uses */ + guint time_id = g_timeout_add_seconds (2, (GSourceFunc)update_spinners, NULL); + g_signal_connect_swapped (time_spin, "destroy", G_CALLBACK (g_source_remove), GINT_TO_POINTER (time_id)); + update_spinners (); } static void @@ -554,8 +615,7 @@ create_dialog (void) gtk_widget_set_sensitive (WIG ("showEventsCheck"), (evo_path != NULL)); g_free (evo_path); - setup_time_spinner (WIG ("timeSpinner"), WIG ("dateSpinner"), TRUE); - setup_time_spinner (WIG ("dateSpinner"), WIG ("timeSpinner"), FALSE); + setup_time_spinners (WIG ("timeSpinner"), WIG ("dateSpinner")); GtkWidget * dlg = WIG ("timeDateDialog"); auto_radio = WIG ("automaticTimeRadio"); -- cgit v1.2.3 From 2de6f443b603965a7fc786df9ef58fcc63841daf Mon Sep 17 00:00:00 2001 From: Ken VanDine Date: Mon, 28 Feb 2011 23:05:10 -0500 Subject: Simplify calendar query, we don't need to compute times for the range. The query system has a built in way to specify a relative range. --- src/datetime-service.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/datetime-service.c b/src/datetime-service.c index 6303a98..078ede0 100644 --- a/src/datetime-service.c +++ b/src/datetime-service.c @@ -462,8 +462,7 @@ update_appointment_menu_items (gpointer user_data) { if (calendar == NULL) return FALSE; if (!g_settings_get_boolean(conf, SETTINGS_SHOW_EVENTS_S)) return FALSE; - time_t t1, t2; - gchar *query, *is, *ie, *ad; + gchar *query, *ad; GList *objects = NULL, *l; GList *allobjects = NULL; GSList *g; @@ -472,13 +471,6 @@ update_appointment_menu_items (gpointer user_data) { gint width, height; ESourceList * sources = NULL; - time(&t1); - time(&t2); - t2 += (time_t) (7 * 24 * 60 * 60); /* 7 days ahead of now */ - - is = isodate_from_time_t(t1); - ie = isodate_from_time_t(t2); - gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height); /* Remove all of the previous appointments */ @@ -497,7 +489,7 @@ update_appointment_menu_items (gpointer user_data) { // TODO Remove all highlights from the calendar widget // FIXME can we put a limit on the number of results? Or if not complete, or is event/todo? Or sort it by date? - query = g_strdup_printf("(occur-in-time-range? (make-time\"%s\") (make-time\"%s\"))", is, ie); + query = g_strdup_printf("(occur-in-time-range? (time-now) (time-add-day (time-now) 7))"); if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_EVENT, &gerror)) { g_debug("Failed to get ecal sources\n"); -- cgit v1.2.3 From 6ee562ac5028df8f28e3559656117d7093b0f73c Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Tue, 1 Mar 2011 16:31:28 -0500 Subject: let date spin buttons work --- data/datetime-dialog.ui | 14 ++++++++++++ src/datetime-prefs.c | 57 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/data/datetime-dialog.ui b/data/datetime-dialog.ui index 35cb172..4277d72 100644 --- a/data/datetime-dialog.ui +++ b/data/datetime-dialog.ui @@ -2,6 +2,11 @@ + + 1.8446744073709552e+19 + 86400 + 864000 + False Locations @@ -102,6 +107,11 @@ + + 1.8446744073709552e+19 + 60 + 3600 + False 5 @@ -286,6 +296,8 @@ 11 1 True + timeAdjustment + True False @@ -328,6 +340,8 @@ 11 1 True + dateAdjustment + True False diff --git a/src/datetime-prefs.c b/src/datetime-prefs.c index c71217a..b6fd1c9 100644 --- a/src/datetime-prefs.c +++ b/src/datetime-prefs.c @@ -48,9 +48,9 @@ GtkWidget * tz_entry = NULL; CcTimezoneMap * tzmap = NULL; GtkWidget * time_spin = NULL; GtkWidget * date_spin = NULL; -GDateTime * manual_time = NULL; guint save_time_id = 0; gboolean user_edited_time = FALSE; +gboolean changing_time = FALSE; /* Turns the boolean property into a string gsettings */ static GVariant * @@ -255,7 +255,8 @@ static gboolean save_time (gpointer user_data) { if (user_edited_time) { - g_dbus_proxy_call (proxy, "SetTime", g_variant_new ("(x)", g_date_time_to_unix (manual_time)), + gdouble current_value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (date_spin)); + g_dbus_proxy_call (proxy, "SetTime", g_variant_new ("(x)", (guint64)current_value), G_DBUS_CALL_FLAGS_NONE, -1, NULL, dbus_set_answered, "time"); } user_edited_time = FALSE; @@ -287,12 +288,15 @@ spin_focus_out (void) } static int -input_time_text (GtkWidget * spinner, gdouble *value, gpointer user_data) +input_time_text (GtkWidget * spinner, gdouble * value, gpointer user_data) { gboolean is_time = (gboolean)GPOINTER_TO_INT (g_object_get_data (G_OBJECT (spinner), "is-time")); const gchar * text = gtk_entry_get_text (GTK_ENTRY (spinner)); - GDateTime * now = manual_time; + gdouble current_value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinner)); + *value = current_value; + + GDateTime * now = g_date_time_new_from_unix_utc (current_value); gint year, month, day, hour, minute, second; year = g_date_time_get_year (now); month = g_date_time_get_month (now); @@ -362,9 +366,13 @@ input_time_text (GtkWidget * spinner, gdouble *value, gpointer user_data) return TRUE; } - g_date_time_unref (manual_time); - manual_time = g_date_time_new_local (year, month, day, hour, minute, second); + gboolean prev_changing = changing_time; + changing_time = TRUE; + GDateTime * new_time = g_date_time_new_local (year, month, day, hour, minute, second); + *value = g_date_time_to_unix (new_time); user_edited_time = TRUE; + g_date_time_unref (new_time); + changing_time = prev_changing; return TRUE; } @@ -386,12 +394,25 @@ format_time_text (GtkWidget * spinner, gpointer user_data) format = "%Y-%m-%d"; } - gchar * formatted = g_date_time_format (manual_time, format); + GDateTime * datetime = g_date_time_new_from_unix_utc (gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinner))); + gchar * formatted = g_date_time_format (datetime, format); gtk_entry_set_text (GTK_ENTRY (spinner), formatted); + g_date_time_unref (datetime); return TRUE; } +static void +spin_copy_value (GtkSpinButton * spinner, GtkSpinButton * other) +{ + if (gtk_spin_button_get_value (spinner) != gtk_spin_button_get_value (other)) { + gtk_spin_button_set_value (other, gtk_spin_button_get_value (spinner)); + } + if (!changing_time) { /* Means user pressed spin buttons */ + user_edited_time = TRUE; + } +} + static gboolean update_spinners (void) { @@ -399,12 +420,13 @@ update_spinners (void) then using the value of the spinner itself. And don't update while user is editing. */ if (!are_spinners_focused ()) { - if (manual_time != NULL) { - g_date_time_unref (manual_time); - } - manual_time = g_date_time_new_now_local (); - format_time_text (time_spin, NULL); - format_time_text (date_spin, NULL); + gboolean prev_changing = changing_time; + changing_time = TRUE; + GDateTime * now = g_date_time_new_now_local (); + gtk_spin_button_set_value (GTK_SPIN_BUTTON (time_spin), (gdouble)g_date_time_to_unix (now)); + /* will be copied to other spin button */ + g_date_time_unref (now); + changing_time = prev_changing; } return TRUE; } @@ -412,12 +434,6 @@ update_spinners (void) static void setup_time_spinners (GtkWidget * time, GtkWidget * date) { - gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (time), FALSE); - gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (date), FALSE); - - gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (time), TRUE); - gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (date), TRUE); - g_signal_connect (time, "input", G_CALLBACK (input_time_text), date); g_signal_connect (date, "input", G_CALLBACK (input_time_text), time); @@ -430,6 +446,9 @@ setup_time_spinners (GtkWidget * time, GtkWidget * date) g_signal_connect (time, "focus-out-event", G_CALLBACK (spin_focus_out), date); g_signal_connect (date, "focus-out-event", G_CALLBACK (spin_focus_out), time); + g_signal_connect (time, "value-changed", G_CALLBACK (spin_copy_value), date); + g_signal_connect (date, "value-changed", G_CALLBACK (spin_copy_value), time); + g_object_set_data (G_OBJECT (time), "is-time", GINT_TO_POINTER (TRUE)); g_object_set_data (G_OBJECT (date), "is-time", GINT_TO_POINTER (FALSE)); -- cgit v1.2.3 From 55736aa91ce77af6127ec115860cb2379158d48b Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Thu, 3 Mar 2011 09:44:42 -0500 Subject: add back missing unref --- src/datetime-prefs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/datetime-prefs.c b/src/datetime-prefs.c index b6fd1c9..901c290 100644 --- a/src/datetime-prefs.c +++ b/src/datetime-prefs.c @@ -304,6 +304,7 @@ input_time_text (GtkWidget * spinner, gdouble * value, gpointer user_data) hour = g_date_time_get_hour (now); minute = g_date_time_get_minute (now); second = g_date_time_get_second (now); + g_date_time_unref (now); /* Parse this string as if it were in the output format */ gint scanned = 0; -- cgit v1.2.3