[gitg] customizable datetime format



commit ce7b4b2d7826c78a4a8785eddfe8e68c5adc5a1b
Author: Tiago Neto <tiago lpneto gmail com>
Date:   Sun Oct 2 22:50:48 2022 +0200

    customizable datetime format

 data/org.gnome.gitg.gschema.xml.in            |  21 +++++
 gitg/preferences/gitg-preferences-commit.vala |  57 +++++++++++++
 gitg/resources/ui/gitg-preferences-commit.ui  | 111 ++++++++++++++++++++++++++
 libgitg/gitg-diff-view-commit-details.vala    |  67 +++++++++++++++-
 4 files changed, 252 insertions(+), 4 deletions(-)
---
diff --git a/data/org.gnome.gitg.gschema.xml.in b/data/org.gnome.gitg.gschema.xml.in
index d10ab842..b0d240a8 100644
--- a/data/org.gnome.gitg.gschema.xml.in
+++ b/data/org.gnome.gitg.gschema.xml.in
@@ -242,6 +242,27 @@
       <summary>Maximum number of days to store previous commit messages</summary>
       <description>Maximum number of days to store previous commit messages for commit message 
history.</description>
     </key>
+    <key name="datetime-selection" type="s">
+      <default>'predefined'</default>
+      <summary>Type of datetime format selected.</summary>
+      <description>
+        Type of datetime format selected.
+      </description>
+    </key>
+    <key name="predefined-datetime" type="s">
+      <default>'%Y-%m-%dT%R%z'</default>
+      <summary>Prefered Datatime format of the user.</summary>
+      <description>
+      The datetime format to be used in commit-diff.
+      </description>
+    </key>
+    <key name="custom-datetime" type="s">
+      <default>''</default>
+      <summary>Custom datetime format.</summary>
+      <description>
+      Custom datetime format.
+      </description>
+    </key>
   </schema>
   <schema gettext-domain="@GETTEXT_PACKAGE@" id="@APPLICATION_ID  preferences diff" 
path="@SCHEMA_PATH@/preferences/diff/">
     <key name="patience" type="b">
diff --git a/gitg/preferences/gitg-preferences-commit.vala b/gitg/preferences/gitg-preferences-commit.vala
index c5920aa4..9eb48851 100644
--- a/gitg/preferences/gitg-preferences-commit.vala
+++ b/gitg/preferences/gitg-preferences-commit.vala
@@ -59,6 +59,18 @@ public class PreferencesCommit : Gtk.Grid, GitgExt.Preferences
        [GtkChild (name = "spin_button_max_num_days_commit_messages")]
        private unowned Gtk.SpinButton d_spin_button_max_num_days_commit_messages;
 
+       [GtkChild (name = "radiobutton_predefined_datetime" )]
+       private unowned Gtk.RadioButton d_predefined_datetime;
+
+       [GtkChild (name = "radiobutton_custom_datetime" )]
+       private unowned Gtk.RadioButton d_custom_datetime;
+
+       [GtkChild (name = "combobox_predefined_datetime")]
+       private unowned Gtk.ComboBox d_predefined_datetime_combo;
+
+       [GtkChild (name = "custom_datetime")]
+       private unowned Gtk.Entry d_custom_datetime_entry;
+
        construct
        {
                var settings = new Settings(Gitg.Config.APPLICATION_ID + ".preferences.commit.message");
@@ -122,6 +134,32 @@ public class PreferencesCommit : Gtk.Grid, GitgExt.Preferences
                              "value",
                              SettingsBindFlags.GET | SettingsBindFlags.SET);
 
+               settings.bind("predefined-datetime",
+                            d_predefined_datetime_combo,
+                            "active-id",
+                            SettingsBindFlags.GET | SettingsBindFlags.SET);
+
+               settings.bind("custom-datetime",
+                            d_custom_datetime_entry,
+                            "text",
+                            SettingsBindFlags.GET | SettingsBindFlags.SET);
+
+               settings.bind("datetime-selection",
+                             this,
+                             "datetime-selection",
+                             SettingsBindFlags.GET | SettingsBindFlags.SET);
+
+               d_predefined_datetime.notify["active"].connect(() => {
+                       if (d_predefined_datetime.active) {
+                               notify_property("datetime-selection");
+                       }
+               });
+
+               d_custom_datetime.notify["active"].connect(() => {
+                       if (d_custom_datetime.active) {
+                               notify_property("datetime-selection");
+                       }
+               });
        }
 
        public Gtk.Widget widget
@@ -141,6 +179,25 @@ public class PreferencesCommit : Gtk.Grid, GitgExt.Preferences
        {
                owned get { return C_("Preferences", "Commit"); }
        }
+
+       public string datetime_selection
+       {
+               get
+               {
+                       return d_custom_datetime.active ? "custom" : "predefined";
+               }
+
+               set
+               {
+                       if (value == "custom"){
+                               d_custom_datetime.active = true;
+                       }
+                       else
+                       {
+                               d_predefined_datetime.active = true;
+                       }
+               }
+       }
 }
 
 }
diff --git a/gitg/resources/ui/gitg-preferences-commit.ui b/gitg/resources/ui/gitg-preferences-commit.ui
index 67811801..d68dfc03 100644
--- a/gitg/resources/ui/gitg-preferences-commit.ui
+++ b/gitg/resources/ui/gitg-preferences-commit.ui
@@ -358,5 +358,116 @@
         <property name="height">1</property>
       </packing>
     </child>
+    <child>
+      <object class="GtkGrid" id="grid_datetime">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="hexpand">True</property>
+        <property name="row_spacing">6</property>
+        <child>
+          <object class="GtkLabel" id="datetime_preference_top_label">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="halign">start</property>
+            <property name="label" translatable="yes">Datetime format</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+            </attributes>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkGrid" id="grid_prefered_datetime_default">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <child>
+              <object class="GtkComboBoxText" id="combobox_predefined_datetime">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="active">0</property>
+                <property name="active_id">0</property>
+                <items>
+                  <item id="%Y-%m">%Y-%m</item>
+                  <item id="%Y-%m-%d">%Y-%m-%d</item>
+                  <item id="%Y-%m-%dT%R%z">%Y-%m-%dT%R%z</item>
+                  <item id="%Y-%m-%dT%T%z">%Y-%m-%dT%T%z</item>
+                  <item id="%Y">%Y</item>
+                  <item id="%Y-%d-%m">%Y-%d-%m</item>
+                  <item id="%Y-%d-%mT%R%z">%Y-%d-%mT%R%z</item>
+                  <item id="%Y-%d-%mT%T%z">%Y-%d-%mT%T%z</item>
+                </items>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="top_attach">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkRadioButton" id="radiobutton_predefined_datetime">
+                <property name="label" translatable="yes">Predefined:</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="active">True</property>
+                <property name="draw_indicator">True</property>
+                <property name="group">radiobutton_custom_datetime</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkGrid">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <child>
+              <object class="GtkRadioButton" id="radiobutton_custom_datetime">
+                <property name="label" translatable="yes">Custom:</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="draw_indicator">True</property>
+                <property name="group">radiobutton_predefined_datetime</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkEntry" id="custom_datetime">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="secondary_icon_stock">gtk-help</property>
+                <property name="secondary_icon_sensitive">False</property>
+                <property name="secondary_icon_tooltip_markup" translatable="yes">Check in vala 
documentation for datetime to write your own format.</property>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="top_attach">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">2</property>
+          </packing>
+        </child>
+      </object>
+      <packing>
+        <property name="left_attach">0</property>
+        <property name="top_attach">1</property>
+      </packing>
+    </child>
   </template>
 </interface>
diff --git a/libgitg/gitg-diff-view-commit-details.vala b/libgitg/gitg-diff-view-commit-details.vala
index 0b604650..6f5a56c2 100644
--- a/libgitg/gitg-diff-view-commit-details.vala
+++ b/libgitg/gitg-diff-view-commit-details.vala
@@ -53,6 +53,8 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
        [GtkChild( name = "label_expand_collapse_files" )]
        private unowned Gtk.Label d_label_expand_collapse_files;
 
+       private Settings d_settings;
+
        public bool expanded
        {
                get { return d_expander_files.expanded; }
@@ -131,14 +133,39 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
 
        public Gitg.Repository? repository {get; set; }
 
+       private string d_datetime_format;
+
+       private string datetime_format
+       {
+               get {
+                       return d_datetime_format;
+               }
+               set {
+                       d_datetime_format = value;
+                       update_datetime();
+               }
+       }
+
        private Gee.HashMap<Ggit.OId, Gtk.RadioButton> d_parents_map;
 
        private GLib.Regex regex_url = /\w+:(\/?\/?)[^\s]+/;
        private Ggit.Config config {get; set;}
        private GLib.Regex regex_custom_links = /gitg\.custom-link\.(.+)\.regex/;
 
+       private void on_change_datetime(Settings settings, string key) {
+               datetime_format = settings.get_string("datetime-selection") == "custom"
+                       ? settings.get_string("custom-datetime")
+                       : settings.get_string("predefined-datetime");
+       }
+
        construct
        {
+               d_settings = new Settings(Gitg.Config.APPLICATION_ID + ".preferences.commit.message");
+               d_settings.changed["datetime-selection"].connect(on_change_datetime);
+               d_settings.changed["custom-datetime"].connect(on_change_datetime);
+               d_settings.changed["predefined-datetime"].connect(on_change_datetime);
+               on_change_datetime(d_settings, "");
+
                d_expander_files.notify["expanded"].connect(() => {
                        if (d_expander_files.expanded)
                        {
@@ -151,11 +178,19 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
 
                        notify_property("expanded");
                });
-
        }
 
        protected override void dispose()
        {
+
+               if (d_settings != null)
+               {
+                       d_settings.changed["datetime-selection"].disconnect(on_change_datetime);
+                       d_settings.changed["custom-datetime"].disconnect(on_change_datetime);
+                       d_settings.changed["predefined-datetime"].disconnect(on_change_datetime);
+                       d_settings = null;
+               }
+
                if (d_avatar_cancel != null)
                {
                        d_avatar_cancel.cancel();
@@ -192,7 +227,6 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
                var author = commit.get_author();
 
                d_label_author.label = author_to_markup(author);
-               d_label_author_date.label = author.get_time().to_timezone(author.get_time_zone()).format("%x 
%X %z");
 
                var committer = commit.get_committer();
 
@@ -201,14 +235,14 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
                    committer.get_time().compare(author.get_time()) != 0)
                {
                        d_label_committer.label = _("Committed by %s").printf(author_to_markup(committer));
-                       d_label_committer_date.label = 
committer.get_time().to_timezone(committer.get_time_zone()).format("%x %X %z");
                }
                else
                {
                        d_label_committer.label = "";
-                       d_label_committer_date.label = "";
                }
 
+               update_datetime();
+
                var parents = commit.get_parents();
                var first_parent = parents.size == 0 ? null : parents.get(0);
 
@@ -257,6 +291,31 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
                update_avatar();
        }
 
+       private void update_datetime()
+       {
+               if (commit == null)
+               {
+                       return;
+               }
+
+               var author = commit.get_author();
+
+               d_label_author_date.label = 
author.get_time().to_timezone(author.get_time_zone()).format(datetime_format);
+
+               var committer = commit.get_committer();
+
+               if (committer.get_name() != author.get_name() ||
+                   committer.get_email() != author.get_email() ||
+                   committer.get_time().compare(author.get_time()) != 0)
+               {
+                       d_label_committer_date.label = 
committer.get_time().to_timezone(committer.get_time_zone()).format(datetime_format);
+               }
+               else
+               {
+                       d_label_committer_date.label = "";
+               }
+       }
+
        private string subject_to_markup(string subject_text)
        {
                return parse_links_on_subject(subject_text);


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