[gitg/wip/albfan/commit-mesage-history] Interface to load older messages



commit f7e8bae65d7e2d85bc495cbe909054ae1f2b4416
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Thu Oct 17 16:03:53 2019 +0200

    Interface to load older messages

 gitg/commit/gitg-commit-dialog.vala             | 203 +++++++++++++++++++++++-
 gitg/commit/gitg-commit.vala                    |  15 ++
 gitg/gitg-window.vala                           |   8 +-
 gitg/meson.build                                |   1 +
 gitg/resources/ui/gitg-author-details-dialog.ui |   4 +-
 gitg/resources/ui/gitg-commit-dialog.ui         | 167 +++++++++++--------
 libgitg-ext/gitg-ext-activity.vala              |   7 +
 libgitg/gitg-diff-view.vala                     |   2 +-
 meson.build                                     |   1 +
 9 files changed, 333 insertions(+), 75 deletions(-)
---
diff --git a/gitg/commit/gitg-commit-dialog.vala b/gitg/commit/gitg-commit-dialog.vala
index 695ed9ce..b85a477c 100644
--- a/gitg/commit/gitg-commit-dialog.vala
+++ b/gitg/commit/gitg-commit-dialog.vala
@@ -65,6 +65,12 @@ class Dialog : Gtk.Dialog
        [GtkChild (name = "scrolled_window_stats")]
        private Gtk.ScrolledWindow d_scrolled_window_stats;
 
+       [GtkChild (name = "prev_commit_message_button")]
+       private Gtk.Button d_prev_commit_message_button;
+
+       [GtkChild (name = "next_commit_message_button")]
+       private Gtk.Button d_next_commit_message_button;
+
        private bool d_show_markup;
        private bool d_show_right_margin;
        private bool d_show_subject_margin;
@@ -81,6 +87,13 @@ class Dialog : Gtk.Dialog
        private Gspell.Checker? d_spell_checker;
        private Ggit.Diff d_diff;
        private bool d_infobar_shown;
+       private Json.Array d_message_array;
+       private int reverse_pos_messages = 0;
+       private string saved_commit_message;
+       private Gtk.TextTag d_subject_tag;
+       private Gtk.TextTag d_too_long_tag;
+
+
 
        public Ggit.Diff? diff
        {
@@ -401,6 +414,16 @@ class Dialog : Gtk.Dialog
 
        construct
        {
+               response.connect(() => {
+                       save_commit_message ();
+               });
+
+               d_message_array = load_commit_messages ();
+               if (d_message_array.get_length () == 0) {
+               reverse_pos_messages = 0;
+                   d_prev_commit_message_button.sensitive = false;
+               }
+
                d_font_manager = new Gitg.FontManager(d_source_view_message, false);
 
                var b = d_source_view_message.buffer;
@@ -492,9 +515,81 @@ class Dialog : Gtk.Dialog
                }
        }
 
-       private Gtk.TextTag d_subject_tag;
-       private Gtk.TextTag d_too_long_tag;
+       private void save_commit_message ()
+       {
+               var message = pretty_message;
+               if (message == "") {
+                       return;
+               }
 
+        Json.Builder builder = new Json.Builder ();
+
+        builder.begin_object ();
+        builder.set_member_name ("version");
+        builder.add_string_value ("1.0");
+
+        builder.set_member_name ("description");
+        builder.add_string_value ("recent commit messages");
+
+        builder.set_member_name ("messages");
+        builder.begin_array ();
+        builder.begin_object ();
+        builder.set_member_name ("datetime");
+        builder.add_string_value (new DateTime.now_utc ().to_string ());
+        builder.set_member_name ("text");
+        builder.add_string_value (message);
+        builder.end_object ();
+               //TODO: Get max days from gsettings;
+               var gsettings_max_days = 10;
+               var max_datetime = new DateTime.now_utc ().add_days (-gsettings_max_days);
+               //TODO: Get max num of messages from gsettings
+               var max_num_messages = 10;
+               int message_counter = 1;
+        foreach (unowned Json.Node node in d_message_array.get_elements ()) {
+                  var object_node = node.get_object ();
+                  if (gsettings_max_days > 0) {
+                     var datetime_str = object_node.get_string_member ("datetime");
+                     var datetime = new DateTime.from_iso8601 (datetime_str, null);
+                     if (datetime.compare (max_datetime) < 0) {
+                         break;
+                     }
+                  }
+                  if (max_num_messages > 0) {
+                         if (message_counter == max_num_messages) {
+                            break;
+                         }
+                     message_counter++;
+                  }
+                  var node_message = object_node.get_string_member ("text");
+                  if (message == node_message) {
+                          continue;
+                  }
+           builder.add_value (node);
+        }
+        builder.end_array ();
+
+        builder.end_object ();
+
+        Json.Generator generator = new Json.Generator ();
+        generator.pretty = true;
+        Json.Node root = builder.get_root ();
+        generator.set_root (root);
+
+        string current_contents = generator.to_data (null);
+        try {
+                           var dirname = Environment.get_user_data_dir() + "/" + Gitg.Config.APPLICATION_ID;
+                var dir = File.new_for_path(dirname);
+                               if (!dir.query_exists ()) {
+                                   dir.make_directory_with_parents ();
+                               }
+                var file = File.new_for_path(dirname + "/" + "commit-messages.json");
+                file.replace_contents (current_contents.data, null, false,
+                                       GLib.FileCreateFlags.NONE, null, null);
+        }
+        catch (GLib.Error err) {
+            error ("%s\n", err.message);
+        }
+       }
        private void iterate_diff()
        {
                var n = diff.get_num_deltas();
@@ -590,11 +685,21 @@ class Dialog : Gtk.Dialog
        {
                var mmask = Gtk.accelerator_get_default_mod_mask();
 
-               if ((mmask & event.state) == Gdk.ModifierType.CONTROL_MASK &&
-                   (event.keyval == Gdk.Key.Return || event.keyval == Gdk.Key.KP_Enter))
+               if ((mmask & event.state) == Gdk.ModifierType.CONTROL_MASK)
                {
-                       d_button_ok.activate();
-                       return true;
+                   if ((event.keyval == Gdk.Key.Return || event.keyval == Gdk.Key.KP_Enter))
+                   {
+                       d_button_ok.activate();
+                       return true;
+                   }
+                       else if ((event.keyval == Gdk.Key.Left || event.keyval == Gdk.Key.KP_Left))
+                   {
+                               on_prev_commit_message_button_clicked ();
+                       }
+                       else if ((event.keyval == Gdk.Key.Right || event.keyval == Gdk.Key.KP_Right))
+                   {
+                               on_next_commit_message_button_clicked ();
+                       }
                }
 
                return false;
@@ -648,7 +753,7 @@ class Dialog : Gtk.Dialog
                                FileUtils.get_contents(path, out contents, out len);
 
                                default_message = Gitg.Convert.utf8(contents, (ssize_t)len).strip();
-                               d_source_view_message.buffer.set_text(default_message);
+                               message = default_message;
                        }
                }
                catch {}
@@ -779,6 +884,90 @@ class Dialog : Gtk.Dialog
 
                set_response_sensitive(Gtk.ResponseType.OK, false);
        }
+
+       private Json.Array load_commit_messages ()
+       {
+        var file = File.new_for_path(Environment.get_user_data_dir() + "/" +
+                                                                        Gitg.Config.APPLICATION_ID + "/" + 
"commit-messages.json");
+
+        uint8[] file_contents;
+
+        Json.Array message_array;
+        try {
+            file.load_contents (null, out file_contents, null);
+            Json.Parser parser = new Json.Parser ();
+            parser.load_from_data ((string) file_contents);
+            Json.Node node = parser.get_root ();
+            var root_object_node = node.get_object ();
+            message_array = root_object_node.get_member ("messages").get_array ();
+        }
+        catch (GLib.Error err) {
+            warning ("%s\n", err.message);
+                       message_array = new Json.Array ();
+        }
+               return message_array;
+
+       }
+
+       [GtkCallback]
+       private void on_next_commit_message_button_clicked ()
+       {
+               if (reverse_pos_messages > 0) {
+                 d_prev_commit_message_button.sensitive = true;
+                 reverse_pos_messages--;
+                 if (reverse_pos_messages == 0) {
+                         message = saved_commit_message;
+                     d_next_commit_message_button.sensitive = false;
+                         return;
+                 }
+               } else {
+                       d_next_commit_message_button.sensitive = false;
+                       d_prev_commit_message_button.sensitive = true;
+                       return;
+               }
+
+               load_selected_commit_message ();
+       }
+
+       [GtkCallback]
+       private void on_prev_commit_message_button_clicked ()
+       {
+               if (reverse_pos_messages < d_message_array.get_length ()) {
+                 if (reverse_pos_messages == 0) {
+                         saved_commit_message = message;
+                 }
+                 d_next_commit_message_button.sensitive = true;
+                 reverse_pos_messages++;
+               } else {
+                       d_prev_commit_message_button.sensitive = false;
+                       if (reverse_pos_messages <= 0) {
+                               reverse_pos_messages = 0;
+                           d_next_commit_message_button.sensitive = true;
+                       }
+                       return;
+               }
+
+               if (d_message_array.get_length() == reverse_pos_messages) {
+                       d_prev_commit_message_button.sensitive = false;
+               }
+
+               load_selected_commit_message ();
+       }
+
+       private void load_selected_commit_message ()
+       {
+               int counter = 0;
+        foreach (unowned Json.Node node_item in d_message_array.get_elements ()) {
+                       counter++;
+                       if (counter < reverse_pos_messages) {
+                               continue;
+                       }
+            var object_node = node_item.get_object ();
+                       var commit_message = object_node.get_string_member("text");
+                       message = commit_message;
+                       break;
+        }
+       }
 }
 
 }
diff --git a/gitg/commit/gitg-commit.vala b/gitg/commit/gitg-commit.vala
index e7ff51c8..269ff4f9 100644
--- a/gitg/commit/gitg-commit.vala
+++ b/gitg/commit/gitg-commit.vala
@@ -141,6 +141,20 @@ namespace GitgCommit
                        return action == "commit";
                }
 
+           public bool on_key_pressed (Gdk.EventKey event) {
+                   var mmask = Gtk.accelerator_get_default_mod_mask();
+
+                   if ((mmask & event.state) == Gdk.ModifierType.CONTROL_MASK)
+                   {
+                       if ((event.keyval == Gdk.Key.Return || event.keyval == Gdk.Key.KP_Enter))
+                       {
+                               on_commit_clicked ();
+                               return true;
+                               }
+                       }
+                       return false;
+               }
+
                private delegate void StageUnstageCallback(Sidebar.Item item);
                private delegate void StageUnstageSubmoduleCommitCallback(Gitg.Commit commit);
 
@@ -1972,6 +1986,7 @@ namespace GitgCommit
                                                  d_main.diff_view,
                                                  "repository",
                                                  BindingFlags.SYNC_CREATE);
+
                }
        }
 }
diff --git a/gitg/gitg-window.vala b/gitg/gitg-window.vala
index 8d995454..d9dede7e 100644
--- a/gitg/gitg-window.vala
+++ b/gitg/gitg-window.vala
@@ -268,9 +268,14 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
 
        [GtkCallback]
        public bool on_key_pressed (Gtk.Widget widget, Gdk.EventKey event) {
+
+               print ("window key press '%s'\n", Gtk.accelerator_get_label(event.keyval,
+                                                                                                             
                                             (event.state & Gtk.accelerator_get_default_mod_mask())));
                bool ret = d_search_bar.handle_event(event);
                if (ret) {
                        d_search_bar.search_mode_enabled = true;
+               } else {
+                       ret = d_activities.current.on_key_pressed(event);
                }
                return ret;
        }
@@ -901,7 +906,8 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
 
                var builtins = new GitgExt.Activity[] {
                        new GitgHistory.Activity(this),
-                       new GitgCommit.Activity(this)
+                       new GitgCommit.Activity(this) //TODO: Si está activo este lanzar el commit con 
Ctrl+Enter
+                               //para ello asociar los atajos a las actividades
                };
 
                var extset = new Peas.ExtensionSet(engine,
diff --git a/gitg/meson.build b/gitg/meson.build
index af0609d2..055b3cdc 100644
--- a/gitg/meson.build
+++ b/gitg/meson.build
@@ -62,6 +62,7 @@ deps = [
   libgitg_ext_dep,
   libpeas_dep,
   libdazzle_dep,
+  json_glib_dependency,
 ]
 
 cflags = warn_flags + [
diff --git a/gitg/resources/ui/gitg-author-details-dialog.ui b/gitg/resources/ui/gitg-author-details-dialog.ui
index 009aa3bf..8519baa1 100644
--- a/gitg/resources/ui/gitg-author-details-dialog.ui
+++ b/gitg/resources/ui/gitg-author-details-dialog.ui
@@ -134,8 +134,8 @@
       </object>
     </child>
     <action-widgets>
-      <action-widget response="-6">button_cancel</action-widget>
-      <action-widget response="-5">button_save</action-widget>
+      <action-widget response="cancel">button_cancel</action-widget>
+      <action-widget response="ok">button_save</action-widget>
     </action-widgets>
   </template>
 </interface>
diff --git a/gitg/resources/ui/gitg-commit-dialog.ui b/gitg/resources/ui/gitg-commit-dialog.ui
index 3c88b78d..a2014a60 100644
--- a/gitg/resources/ui/gitg-commit-dialog.ui
+++ b/gitg/resources/ui/gitg-commit-dialog.ui
@@ -104,67 +104,6 @@
                 <property name="height">1</property>
               </packing>
             </child>
-            <child>
-              <object class="GtkCheckButton" id="check_button_sign_off">
-                <property name="label" translatable="yes">Add _signed-off-by signature</property>
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="receives_default">False</property>
-                <property name="use_underline">True</property>
-                <property name="halign">start</property>
-                <property name="draw_indicator">True</property>
-              </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">6</property>
-                <property name="width">2</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkCheckButton" id="check_button_amend">
-                <property name="label" translatable="yes">_Amend previous commit</property>
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="receives_default">False</property>
-                <property name="use_underline">True</property>
-                <property name="halign">start</property>
-                <property name="draw_indicator">True</property>
-              </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">5</property>
-                <property name="width">2</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkScrolledWindow" id="scrolled_window_message">
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="hexpand">True</property>
-                <property name="vexpand">True</property>
-                <property name="shadow_type">in</property>
-                <child>
-                  <object class="GtkSourceView" id="source_view_message">
-                    <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="left_margin">2</property>
-                    <property name="right_margin">2</property>
-                    <property name="auto_indent">True</property>
-                    <property name="show_right_margin">True</property>
-                    <property name="right_margin_position">72</property>
-                    <property name="smart_home_end">after</property>
-                  </object>
-                </child>
-              </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">3</property>
-                <property name="width">2</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
             <child>
               <object class="GtkRevealer" id="infobar_revealer">
                 <property name="visible">True</property>
@@ -214,7 +153,34 @@
               <packing>
                 <property name="left_attach">0</property>
                 <property name="top_attach">2</property>
-                <property name="width">2</property>
+                <property name="width">3</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkScrolledWindow" id="scrolled_window_message">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="hexpand">True</property>
+                <property name="vexpand">True</property>
+                <property name="shadow_type">in</property>
+                <child>
+                  <object class="GtkSourceView" id="source_view_message">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="left_margin">2</property>
+                    <property name="right_margin">2</property>
+                    <property name="auto_indent">True</property>
+                    <property name="show_right_margin">True</property>
+                    <property name="right_margin_position">72</property>
+                    <property name="smart_home_end">after</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">3</property>
+                <property name="width">3</property>
                 <property name="height">1</property>
               </packing>
             </child>
@@ -242,10 +208,83 @@
               <packing>
                 <property name="left_attach">0</property>
                 <property name="top_attach">4</property>
+                <property name="width">3</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkCheckButton" id="check_button_amend">
+                <property name="label" translatable="yes">_Amend previous commit</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="use_underline">True</property>
+                <property name="halign">start</property>
+                <property name="draw_indicator">True</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">5</property>
                 <property name="width">2</property>
                 <property name="height">1</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkCheckButton" id="check_button_sign_off">
+                <property name="label" translatable="yes">Add _signed-off-by signature</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="use_underline">True</property>
+                <property name="halign">start</property>
+                <property name="draw_indicator">True</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">6</property>
+                <property name="width">2</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkBox">
+                <property name="visible">True</property>
+                <style>
+                  <class name="linked"/>
+                </style>
+                <child>
+                  <object class="GtkButton" id="prev_commit_message_button">
+                    <property name="visible">True</property>
+                    <signal name="clicked" handler="on_prev_commit_message_button_clicked"/>
+                    <child>
+                      <object class="GtkImage">
+                        <property name="icon-name">pan-start-symbolic</property>
+                        <property name="visible">True</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child>
+                  <object class="GtkButton" id="next_commit_message_button">
+                    <property name="visible">True</property>
+                    <property name="sensitive">False</property>
+                    <signal name="clicked" handler="on_next_commit_message_button_clicked"/>
+                    <child>
+                      <object class="GtkImage">
+                        <property name="icon-name">pan-end-symbolic</property>
+                        <property name="visible">True</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="left_attach">2</property>
+                <property name="top_attach">6</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="expand">False</property>
@@ -256,8 +295,8 @@
       </object>
     </child>
     <action-widgets>
-      <action-widget response="-6">cancel-button</action-widget>
-      <action-widget response="-5">ok-button</action-widget>
+      <action-widget response="cancel">cancel-button</action-widget>
+      <action-widget response="ok">ok-button</action-widget>
     </action-widgets>
   </template>
 </interface>
diff --git a/libgitg-ext/gitg-ext-activity.vala b/libgitg-ext/gitg-ext-activity.vala
index 23528f05..c28432b2 100644
--- a/libgitg-ext/gitg-ext-activity.vala
+++ b/libgitg-ext/gitg-ext-activity.vala
@@ -41,6 +41,13 @@ public interface Activity : Object, UIElement
        {
                return false;
        }
+
+       /**
+        * Key press event
+        */
+       public virtual bool on_key_pressed (Gdk.EventKey event) {
+               return false;
+       }
 }
 
 }
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index aa89b243..2acaa7cb 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -756,7 +756,7 @@ public class Gitg.DiffView : Gtk.Grid
                        if (current_file != null)
                        {
                                current_file.show();
-                               current_file.renderer.notify["has-selection"].connect(on_selection_changed);  
  
+                               current_file.renderer.notify["has-selection"].connect(on_selection_changed);
 
                                files.add(current_file);
 
diff --git a/meson.build b/meson.build
index 88cdb836..37bfa85a 100644
--- a/meson.build
+++ b/meson.build
@@ -139,6 +139,7 @@ libsecret_dep = dependency('libsecret-1')
 libsoup_dep = dependency('libsoup-2.4')
 libxml_dep = dependency('libxml-2.0', version: '>= 2.9.0')
 libdazzle_dep = dependency('libdazzle-1.0')
+json_glib_dependency = dependency('json-glib-1.0')
 
 config_dep = valac.find_library('config', dirs: vapi_dir)
 gitg_platform_support_dep = valac.find_library('gitg-platform-support', dirs: vapi_dir)


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