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



commit 251570a8e51d99bd8e1ec88667249ea7477bb3eb
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     | 121 ++++++++++++++++++++++++
 gitg/meson.build                        |   1 +
 gitg/resources/ui/gitg-commit-dialog.ui | 163 ++++++++++++++++++++------------
 meson.build                             |   1 +
 5 files changed, 238 insertions(+), 62 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..e2c71939 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;
@@ -779,6 +785,121 @@ class Dialog : Gtk.Dialog
 
                set_response_sensitive(Gtk.ResponseType.OK, false);
        }
+
+       //TODO: Save new messages on exit (checking existing ones)
+       //TODO: Load messages on dialog load
+       //TODO: 0 means empty (or saved typed text
+       int reverse_pos_messages = 0;
+       string saved_commit_message;
+
+       [GtkCallback]
+       private void on_next_commit_message_button_clicked ()
+       {
+               print("path next\n");
+               if (reverse_pos_messages > 0) {
+                 d_prev_commit_message_button.sensitive = true;
+                 reverse_pos_messages--;
+                 if (reverse_pos_messages == 0) {
+                         d_source_view_message.buffer.text = 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;
+               }
+
+        var file = File.new_for_path(Environment.get_user_data_dir() + "/" +
+                                                                        Gitg.Config.APPLICATION_ID + "/" + 
"commit-messages.json");
+
+        uint8[] file_contents;
+
+        try {
+            file.load_contents (null, out file_contents, null);
+        }
+        catch (GLib.Error err) {
+            warning ("%s\n", err.message);
+                       return;
+        }
+        Json.Parser parser = new Json.Parser ();
+               try {
+          parser.load_from_data ((string) file_contents);
+               } catch {
+                       return;
+               }
+
+        Json.Node node = parser.get_root ();
+        var root_object_node = node.get_object ();
+        Json.Array array = root_object_node.get_member ("messages").get_array ();
+               int counter = 0;
+        foreach (unowned Json.Node node_item in array.get_elements ()) {
+                       counter++;
+                       if (counter < reverse_pos_messages) {
+                               continue;
+                       }
+            var object_node = node_item.get_object ();
+                       var message = object_node.get_string_member("text");
+                       d_source_view_message.buffer.text = message;
+                       break;
+        }
+       }
+
+       [GtkCallback]
+       private void on_prev_commit_message_button_clicked ()
+       {
+               //TODO: Count max stored messages
+               print("path previous\n");
+               if (reverse_pos_messages < 2) {
+                 if (reverse_pos_messages == 0) {
+                         saved_commit_message = d_source_view_message.buffer.text;
+                 }
+                 d_next_commit_message_button.sensitive = true;
+                 reverse_pos_messages++;
+               } else {
+                       d_prev_commit_message_button.sensitive = false;
+                       d_next_commit_message_button.sensitive = true;
+                       return;
+               }
+
+        var file = File.new_for_path(Environment.get_user_data_dir() + "/" +
+                                                                        Gitg.Config.APPLICATION_ID + "/" + 
"commit-messages.json");
+               print("path: %s\n", file.get_path());
+
+        uint8[] file_contents;
+
+        try {
+            file.load_contents (null, out file_contents, null);
+        }
+        catch (GLib.Error err) {
+            warning ("%s\n", err.message);
+                       return;
+        }
+        Json.Parser parser = new Json.Parser ();
+               try {
+          parser.load_from_data ((string) file_contents);
+               } catch {
+                       return;
+               }
+
+        Json.Node node = parser.get_root ();
+        var root_object_node = node.get_object ();
+        Json.Array array = root_object_node.get_member ("messages").get_array ();
+               if (array.get_length() == reverse_pos_messages) {
+                       d_prev_commit_message_button.sensitive = false;
+               }
+               int counter = 0;
+        foreach (unowned Json.Node node_item in array.get_elements ()) {
+                       counter++;
+                       if (counter < reverse_pos_messages) {
+                               continue;
+                       }
+            var object_node = node_item.get_object ();
+                       var message = object_node.get_string_member("text");
+                       d_source_view_message.buffer.text = 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-commit-dialog.ui b/gitg/resources/ui/gitg-commit-dialog.ui
index 3c88b78d..335247e3 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>
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]