[totem/wip/hadess/precise-stepping: 3/7] all: Convert totem_time_to_string() to using flags




commit 866cab1fea61a7ec8747c24a54dc8ee3e3e9d0d9
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Feb 17 21:29:39 2022 +0100

    all: Convert totem_time_to_string() to using flags
    
    See https://blog.ometer.com/2011/01/20/boolean-parameters-are-wrong/

 src/backend/bacon-time-label.c        | 20 +++++++-------------
 src/gst/totem-time-helpers.c          | 13 ++++++-------
 src/gst/totem-time-helpers.h          | 11 ++++++++---
 src/plugins/skipto/totem-time-entry.c |  4 ++--
 src/test-totem.c                      | 10 +++++-----
 src/totem-gallery-thumbnailer.c       |  4 ++--
 src/totem-grilo.c                     |  2 +-
 7 files changed, 31 insertions(+), 33 deletions(-)
---
diff --git a/src/backend/bacon-time-label.c b/src/backend/bacon-time-label.c
index e666c8036..a10e4da78 100644
--- a/src/backend/bacon-time-label.c
+++ b/src/backend/bacon-time-label.c
@@ -46,7 +46,7 @@ bacon_time_label_init (BaconTimeLabel *label)
        char *time_string;
        PangoAttrList *attrs;
 
-       time_string = totem_time_to_string (0, FALSE, FALSE);
+       time_string = totem_time_to_string (0, TOTEM_TIME_FLAG_NONE);
        gtk_label_set_text (GTK_LABEL (label), time_string);
        g_free (time_string);
 
@@ -99,36 +99,30 @@ bacon_time_label_class_init (BaconTimeLabelClass *klass)
 static void
 update_label_text (BaconTimeLabel *label)
 {
-       char *label_str;
-       gboolean force_hour = FALSE;
+       g_autofree char *label_str = NULL;
        gint64 _time, length;
+       TotemTimeFlag flags;
 
        _time = label->time;
        length = label->length;
 
+       flags = label->remaining ? TOTEM_TIME_FLAG_REMAINING : TOTEM_TIME_FLAG_NONE;
        if (length > 60 * 60 * 1000)
-               force_hour = TRUE;
+               flags |= TOTEM_TIME_FLAG_FORCE_HOUR;
 
        if (length <= 0 ||
            _time > length) {
                if (!label->remaining) {
-                       label_str = totem_time_to_string (_time, FALSE, force_hour);
+                       label_str = totem_time_to_string (_time, flags);
                } else {
                        /* translators: Unknown remaining time */
                        label_str = g_strdup (_("--:--"));
                }
        } else {
-               if (!label->remaining) {
-                       /* Elapsed */
-                       label_str = totem_time_to_string (_time, FALSE, force_hour);
-               } else {
-                       /* Remaining */
-                       label_str = totem_time_to_string (length - _time, TRUE, force_hour);
-               }
+               label_str = totem_time_to_string (label->remaining ? length - _time : _time, flags);
        }
 
        gtk_label_set_text (GTK_LABEL (label), label_str);
-       g_free (label_str);
 }
 
 void
diff --git a/src/gst/totem-time-helpers.c b/src/gst/totem-time-helpers.c
index eed2bb2e6..3af9dd0e0 100644
--- a/src/gst/totem-time-helpers.c
+++ b/src/gst/totem-time-helpers.c
@@ -34,9 +34,8 @@
 /* FIXME: Remove
  * See https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/26 */
 char *
-totem_time_to_string (gint64   msecs,
-                     gboolean remaining,
-                     gboolean force_hour)
+totem_time_to_string (gint64        msecs,
+                     TotemTimeFlag flags)
 {
        int sec, min, hour, _time;
        double time_f;
@@ -47,7 +46,7 @@ totem_time_to_string (gint64   msecs,
         * we want to make sure that:
         * current time + time remaining = total run time */
        time_f = (double) msecs / 1000;
-       if (remaining)
+       if (flags & TOTEM_TIME_FLAG_REMAINING)
                time_f = ceil (time_f);
        else
                time_f = round (time_f);
@@ -59,8 +58,8 @@ totem_time_to_string (gint64   msecs,
        _time = _time - (min * 60);
        hour = _time / (60*60);
 
-       if (hour > 0 || force_hour) {
-               if (!remaining) {
+       if (hour > 0 || flags & TOTEM_TIME_FLAG_FORCE_HOUR) {
+               if (!(flags & TOTEM_TIME_FLAG_REMAINING)) {
                        /* hour:minutes:seconds */
                        /* Translators: This is a time format, like "-9:05:02" for 9
                         * hours, 5 minutes, and 2 seconds. You may change ":" to
@@ -79,7 +78,7 @@ totem_time_to_string (gint64   msecs,
                }
        }
 
-       if (remaining) {
+       if (flags & TOTEM_TIME_FLAG_REMAINING) {
                /* -minutes:seconds */
                /* Translators: This is a time format, like "-5:02" for 5
                 * minutes and 2 seconds playback remaining. You may change
diff --git a/src/gst/totem-time-helpers.h b/src/gst/totem-time-helpers.h
index a5269b787..8ed34bc9f 100644
--- a/src/gst/totem-time-helpers.h
+++ b/src/gst/totem-time-helpers.h
@@ -30,8 +30,13 @@
 
 #include <glib.h>
 
-char *totem_time_to_string (gint64   msecs,
-                           gboolean remaining,
-                           gboolean force_hour);
+typedef enum {
+       TOTEM_TIME_FLAG_NONE            = 0,
+       TOTEM_TIME_FLAG_REMAINING       = 1 << 0,
+       TOTEM_TIME_FLAG_FORCE_HOUR      = 1 << 2,
+} TotemTimeFlag;
+
+char *totem_time_to_string (gint64        msecs,
+                           TotemTimeFlag flags);
 
 #endif /* _TOTEM_TIME_HELPERS_H_ */
diff --git a/src/plugins/skipto/totem-time-entry.c b/src/plugins/skipto/totem-time-entry.c
index eb03f17e3..501d1784a 100644
--- a/src/plugins/skipto/totem-time-entry.c
+++ b/src/plugins/skipto/totem-time-entry.c
@@ -120,7 +120,7 @@ output_cb (GtkSpinButton *self, gpointer user_data)
 {
        gchar *text;
 
-       text = totem_time_to_string ((gint64) gtk_spin_button_get_value (self) * 1000, FALSE, FALSE);
+       text = totem_time_to_string ((gint64) gtk_spin_button_get_value (self) * 1000, TOTEM_TIME_FLAG_NONE);
        gtk_entry_set_text (GTK_ENTRY (self), text);
        g_free (text);
 
@@ -168,7 +168,7 @@ changed_cb (GtkAdjustment *adjustment, TotemTimeEntry *self)
        /* Set the width of the entry according to the length of the longest string it'll now accept */
        upper = (guint) gtk_adjustment_get_upper (adjustment); /* in seconds */
 
-       time_string = totem_time_to_string (((gint64) upper) * 1000, FALSE, FALSE);
+       time_string = totem_time_to_string (((gint64) upper) * 1000, TOTEM_TIME_FLAG_NONE);
        width = strlen (time_string);
        g_free (time_string);
 
diff --git a/src/test-totem.c b/src/test-totem.c
index de4520a22..e055b25ff 100644
--- a/src/test-totem.c
+++ b/src/test-totem.c
@@ -146,23 +146,23 @@ test_time_label (void)
                    50 * 60 * 1000, 45 * 60 * 1000,
                    "50:00", "--:--");
 
-       str = totem_time_to_string (0, FALSE, FALSE);
+       str = totem_time_to_string (0, TOTEM_TIME_FLAG_NONE);
        g_assert_cmpstr (str, ==, "0:00");
        g_free (str);
 
-       str = totem_time_to_string (500, FALSE, FALSE);
+       str = totem_time_to_string (500, TOTEM_TIME_FLAG_NONE);
        g_assert_cmpstr (str, ==, "0:01");
        g_free (str);
 
-       str = totem_time_to_string (500, TRUE, FALSE);
+       str = totem_time_to_string (500, TOTEM_TIME_FLAG_REMAINING);
        g_assert_cmpstr (str, ==, "-0:01");
        g_free (str);
 
-       str = totem_time_to_string (1250, FALSE, FALSE);
+       str = totem_time_to_string (1250, TOTEM_TIME_FLAG_NONE);
        g_assert_cmpstr (str, ==, "0:01");
        g_free (str);
 
-       str = totem_time_to_string (1250, TRUE, FALSE);
+       str = totem_time_to_string (1250, TOTEM_TIME_FLAG_REMAINING);
        g_assert_cmpstr (str, ==, "-0:02");
        g_free (str);
 }
diff --git a/src/totem-gallery-thumbnailer.c b/src/totem-gallery-thumbnailer.c
index 1d9b51965..d066d1301 100644
--- a/src/totem-gallery-thumbnailer.c
+++ b/src/totem-gallery-thumbnailer.c
@@ -620,7 +620,7 @@ create_gallery (ThumbApp *app)
        g_object_unref (pixbuf);
 
        /* Build the header information */
-       duration_text = totem_time_to_string (stream_length, FALSE, FALSE);
+       duration_text = totem_time_to_string (stream_length, TOTEM_TIME_FLAG_NONE);
        file = g_file_new_for_commandline_arg (app->input);
        filename = g_file_get_basename (file);
        g_object_unref (file);
@@ -669,7 +669,7 @@ create_gallery (ThumbApp *app)
                gchar *timestamp_text;
                gint layout_width, layout_height;
 
-               timestamp_text = totem_time_to_string (pos, FALSE, FALSE);
+               timestamp_text = totem_time_to_string (pos, TOTEM_TIME_FLAG_NONE);
 
                pango_layout_set_text (layout, timestamp_text, -1);
                pango_layout_get_pixel_size (layout, &layout_width, &layout_height);
diff --git a/src/totem-grilo.c b/src/totem-grilo.c
index 47e9c7041..f07cf8d7a 100644
--- a/src/totem-grilo.c
+++ b/src/totem-grilo.c
@@ -294,7 +294,7 @@ get_secondary_text (GrlMedia *media)
                return g_strdup (artist);
        duration = grl_media_get_duration (media);
        if (duration > 0)
-               return totem_time_to_string (duration * 1000, FALSE, FALSE);
+               return totem_time_to_string (duration * 1000, TOTEM_TIME_FLAG_NONE);
        return NULL;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]