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



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

    Interface to load older messages

 commit-messages.json                            |  14 ++
 gitg/commit/gitg-commit-dialog.vala             | 167 +++++++++++++++++++++++-
 gitg/meson.build                                |   1 +
 gitg/resources/ui/gitg-author-details-dialog.ui |   4 +-
 gitg/resources/ui/gitg-commit-dialog.ui         | 167 +++++++++++++++---------
 meson.build                                     |   1 +
 6 files changed, 285 insertions(+), 69 deletions(-)
---
diff --git a/commit-messages.json b/commit-messages.json
new file mode 100644
index 00000000..f2abf6d9
--- /dev/null
+++ b/commit-messages.json
@@ -0,0 +1,14 @@
+{
+   "version": "1.0",
+   "description": "recent commit messages",
+   "messages" : [
+      {
+         "datetime" : "2019-10-18 06:23:05",
+         "text" : "Say Hello to history feature\n\nThis is helpful to use gitg in long sessions"
+      },
+      {
+         "datetime" : "2019-10-18 06:24:15",
+         "text" : "Another saved message\n\nRetrieve your last messages.\n\nSee Important info collected 
before."
+      }
+   ]
+}
diff --git a/gitg/commit/gitg-commit-dialog.vala b/gitg/commit/gitg-commit-dialog.vala
index 695ed9ce..e0472868 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,63 @@ 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 ();
+        foreach (unowned Json.Node node in d_message_array.get_elements ()) {
+                  var node_message = node.get_object ().get_string_member ("text");
+                  if (message == node_message) {
+                          //TODO: Mover el mensaje al primero de la lista
+                          return;
+                  }
+           builder.add_value (node);
+        }
+        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 ();
+        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);
+
+               print("eco");
+        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();
@@ -648,7 +725,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 +856,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/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/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]