[gitg/wip/adwait/add-remotes: 159/162] Allow to add new remotes




commit d2490c81a2ff21944944b2f8338d9a3e8ad264e5
Author: Adwait Rawat <adwait rawat gmail com>
Date:   Tue Jun 18 13:19:52 2019 +0900

    Allow to add new remotes

 gitg/gitg-add-remote-action-dialog.vala            |  61 +++++++++
 gitg/gitg-add-remote-action.vala                   | 133 ++++++++++++++++++++
 gitg/history/gitg-history.vala                     |  25 ++++
 gitg/meson.build                                   |   3 +
 gitg/resources/gitg-resources.xml.in               |   1 +
 gitg/resources/ui/gitg-add-remote-action-dialog.ui | 139 +++++++++++++++++++++
 6 files changed, 362 insertions(+)
---
diff --git a/gitg/gitg-add-remote-action-dialog.vala b/gitg/gitg-add-remote-action-dialog.vala
new file mode 100644
index 00000000..3b085105
--- /dev/null
+++ b/gitg/gitg-add-remote-action-dialog.vala
@@ -0,0 +1,61 @@
+namespace Gitg
+{
+
+[GtkTemplate (ui = "/org/gnome/gitg/ui/gitg-add-remote-action-dialog.ui")]
+class AddRemoteActionDialog : Gtk.Dialog
+{
+       [GtkChild]
+       private Gtk.Button d_button_create;
+
+       [GtkChild]
+       private Gtk.Entry d_entry_remote_name;
+
+       [GtkChild]
+       private Gtk.Entry d_entry_remote_url;
+
+       construct
+       {
+               d_entry_remote_name.changed.connect(() => {
+                       var is_name_valid = (d_entry_remote_name.text != "");
+
+                       d_entry_remote_url.changed.connect((e) => {
+                               var is_url_valid = (d_entry_remote_url.text != "");
+
+                               set_response_sensitive(Gtk.ResponseType.OK, is_name_valid && is_url_valid);
+                       });
+               });
+
+               set_default(d_button_create);
+               set_default_response(Gtk.ResponseType.OK);
+       }
+
+       public AddRemoteActionDialog(Gtk.Window? parent)
+       {
+               Object(use_header_bar : 1);
+
+               if (parent != null)
+               {
+                       set_transient_for(parent);
+               }
+       }
+
+       public string new_remote_name
+       {
+               owned get
+               {
+                       return d_entry_remote_name.text.strip();
+               }
+       }
+
+       public string new_remote_url
+       {
+               owned get
+               {
+                       return d_entry_remote_url.text.strip();
+               }
+       }
+}
+
+}
+
+// ex: ts=4 noet
diff --git a/gitg/gitg-add-remote-action.vala b/gitg/gitg-add-remote-action.vala
new file mode 100644
index 00000000..c6a8ad35
--- /dev/null
+++ b/gitg/gitg-add-remote-action.vala
@@ -0,0 +1,133 @@
+namespace Gitg
+{
+
+class AddRemoteAction : GitgExt.UIElement, GitgExt.Action, Object
+{
+       // Do this to pull in config.h before glib.h (for gettext...)
+       private const string version = Gitg.Config.VERSION;
+
+       public GitgExt.Application? application { owned get; construct set; }
+       Gitg.Remote? d_remote;
+
+       public AddRemoteAction(GitgExt.Application        application)
+       {
+               Object(application:      application);
+       }
+
+       public string id
+       {
+               owned get { return "/org/gnome/gitg/ref-actions/add-remote"; }
+       }
+
+       public string display_name
+       {
+               owned get { return _("Add Remote"); }
+       }
+
+       public string description
+       {
+               owned get { return _("Adds remote to the remotes list"); }
+       }
+
+       public async void fetch_remote( Gitg.Repository repo, string new_remote_name)
+       {
+               var notification = new RemoteNotification(d_remote);
+               application.notifications.add(notification);
+
+               notification.text = _("Fetching from %s").printf(d_remote.get_url());
+
+               var updates = new Gee.ArrayList<string>();
+
+               var tip_updated_id = d_remote.tip_updated.connect((d_remote, name, a, b) => {
+                       /* Translators: new refers to a new remote reference having been fetched, */
+                       updates.add(@"%s (%s)".printf(name, _("new")));
+               });
+
+               var fetched = true;
+
+               try
+               {
+                       yield d_remote.fetch(null, null);
+               }
+               catch (Error e)
+               {
+                       try {
+                               repo.remove_remote(new_remote_name);
+                               notification.error(_("Failed to fetch from %s: 
%s").printf(d_remote.get_url(), e.message));
+
+                               fetched = false;
+                       }
+                       catch {}
+                       application.show_infobar(_("Failed to fetch added remote"),
+                                                                       e.message,
+                                                                       Gtk.MessageType.ERROR);
+               }
+               finally
+               {
+                       ((Object)d_remote).disconnect(tip_updated_id);
+               }
+
+               if (fetched)
+               {
+                       notification.success(_("Fetched from %s: %s").printf(d_remote.get_url(), 
string.joinv(", ", updates.to_array())));
+                       ((Gtk.ApplicationWindow)application).activate_action("reload", null);
+               }
+               else
+               {
+                       add_remote();
+               }
+       }
+
+       public void add_remote()
+       {
+               var dlg = new AddRemoteActionDialog((Gtk.Window)application);
+               var remote_added = true;
+
+               dlg.response.connect((d, resp) => {
+                       if (resp == Gtk.ResponseType.OK)
+                       {
+                               Ggit.Remote? remote = null;
+                               d_remote = null;
+
+                               var repo = application.repository;
+                               var new_remote_name = dlg.new_remote_name;
+
+                               try
+                               {
+                                       remote = repo.create_remote(new_remote_name,
+                                                                                               
dlg.new_remote_url);
+                               }
+                               catch (Error e)
+                               {
+                                       remote_added = false;
+                                       add_remote();
+                                       application.show_infobar(_("Failed to add remote"),
+                                                                e.message,
+                                                                                        
Gtk.MessageType.ERROR);
+                               }
+
+                               d_remote = application.remote_lookup.lookup(new_remote_name);
+
+                               if (remote != null)
+                               {
+                                       fetch_remote.begin(repo, new_remote_name, (obj,res) => {
+                                               fetch_remote.end(res);
+                                       });
+                               }
+                       }
+
+                       dlg.destroy();
+               });
+
+               dlg.show();
+       }
+
+       public void activate()
+       {
+               add_remote();
+       }
+}
+
+}
+
+// ex:set ts=4 noet
diff --git a/gitg/history/gitg-history.vala b/gitg/history/gitg-history.vala
index 87289eb6..c423ca35 100644
--- a/gitg/history/gitg-history.vala
+++ b/gitg/history/gitg-history.vala
@@ -53,6 +53,7 @@ namespace GitgHistory
 
                private Paned d_main;
                private Gitg.PopupMenu d_refs_list_popup;
+               private Gitg.PopupMenu d_remote_header_popup;
                private Gitg.PopupMenu d_commit_list_popup;
 
                private string[] d_mainline;
@@ -611,6 +612,9 @@ namespace GitgHistory
                        d_refs_list_popup = new Gitg.PopupMenu(d_main.refs_list);
                        d_refs_list_popup.populate_menu.connect(on_refs_list_populate_menu);
 
+                       d_remote_header_popup = new Gitg.PopupMenu(d_main.refs_list);
+                       d_remote_header_popup.populate_menu.connect(on_remote_header_populate_menu);
+
                        d_refs_list_selection_id = 
d_main.refs_list.notify["selection"].connect(update_walker_idle);
                        d_refs_list_changed_id = d_main.refs_list.changed.connect(update_walker_idle);
 
@@ -852,6 +856,16 @@ namespace GitgHistory
                        return populate_menu_for_commit(commit);
                }
 
+               private Gtk.Menu? popup_menu_for_remote() {
+                       var action = new Gitg.AddRemoteAction(application);
+                       var menu = new Gtk.Menu();
+
+                       action.populate_menu(menu);
+                       menu.set_data("gitg-ext-actions", action);
+
+                       return menu;
+               }
+
                private Gtk.Menu? popup_menu_for_ref(Gitg.Ref reference)
                {
                        var actions = new Gee.LinkedList<GitgExt.RefAction?>();
@@ -1004,6 +1018,17 @@ namespace GitgHistory
                        return popup_menu_for_ref(references.first());
                }
 
+               private Gtk.Menu? on_remote_header_populate_menu(Gdk.EventButton? event)
+               {
+                       if (event != null)
+                       {
+                               var row = d_main.refs_list.get_row_at_y((int)event.y);
+                               d_main.refs_list.select_row(row);
+                       }
+
+                       return popup_menu_for_remote();
+               }
+
                private Ggit.OId? id_for_ref(Ggit.Ref r)
                {
                        Ggit.OId? id = null;
diff --git a/gitg/meson.build b/gitg/meson.build
index 85688b65..937a01a3 100644
--- a/gitg/meson.build
+++ b/gitg/meson.build
@@ -46,6 +46,8 @@ sources = gitg_sources + files(
   'gitg-ref-action-fetch.vala',
   'gitg-ref-action-push.vala',
   'gitg-ref-action-rename.vala',
+  'gitg-add-remote-action-dialog.vala',
+  'gitg-add-remote-action.vala',
   'gitg-remote-manager.vala',
   'gitg-remote-notification.vala',
   'gitg-simple-notification.vala',
@@ -106,6 +108,7 @@ resource_data = files(
   'resources/ui/gitg-preferences-history.ui',
   'resources/ui/gitg-preferences-interface.ui',
   'resources/ui/gitg-preferences.ui',
+  'resources/ui/gitg-add-remote-action-dialog.ui',
   'resources/ui/gitg-remote-notification.ui',
   'resources/ui/gitg-shortcuts.ui',
   'resources/ui/gitg-simple-notification.ui',
diff --git a/gitg/resources/gitg-resources.xml.in b/gitg/resources/gitg-resources.xml.in
index 1224cefe..5248b951 100644
--- a/gitg/resources/gitg-resources.xml.in
+++ b/gitg/resources/gitg-resources.xml.in
@@ -7,6 +7,7 @@
     <file compressed="true" preprocess="xml-stripblanks">ui/gitg-preferences-commit.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/gitg-preferences-interface.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/gitg-preferences.ui</file>
+    <file compressed="true" preprocess="xml-stripblanks">ui/gitg-add-remote-action-dialog.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/gitg-clone-dialog.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/gitg-author-details-dialog.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">ui/gitg-history-paned.ui</file>
diff --git a/gitg/resources/ui/gitg-add-remote-action-dialog.ui 
b/gitg/resources/ui/gitg-add-remote-action-dialog.ui
new file mode 100644
index 00000000..a73f2f16
--- /dev/null
+++ b/gitg/resources/ui/gitg-add-remote-action-dialog.ui
@@ -0,0 +1,139 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <template class="GitgAddRemoteActionDialog" parent="GtkDialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">12</property>
+    <property name="title" translatable="yes">Create Remote</property>
+    <property name="resizable">False</property>
+    <property name="modal">True</property>
+    <property name="type_hint">dialog</property>
+    <child internal-child="headerbar">
+      <object class="GtkHeaderBar" id="dialog-header_bar">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="show_close_button">False</property>
+        <child>
+          <object class="GtkButton" id="button_cancel">
+            <property name="label" translatable="yes">_Cancel</property>
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">True</property>
+            <property name="use_underline">True</property>
+            <property name="valign">center</property>
+            <style>
+              <class name="text-button"/>
+            </style>
+          </object>
+          <packing>
+            <property name="pack_type">start</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkButton" id="d_button_create">
+            <property name="label" translatable="yes">_Add</property>
+            <property name="visible">True</property>
+            <property name="sensitive">False</property>
+            <property name="can_focus">True</property>
+            <property name="can_default">True</property>
+            <property name="receives_default">True</property>
+            <property name="use_underline">True</property>
+            <style>
+              <class name="text-button"/>
+              <class name="suggested-action"/>
+            </style>
+          </object>
+          <packing>
+            <property name="pack_type">end</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="dialog-vbox1">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child>
+          <object class="GtkGrid" id="grid1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="hexpand">True</property>
+            <property name="row_spacing">6</property>
+            <property name="column_spacing">6</property>
+            <child>
+              <object class="GtkLabel" id="label1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">end</property>
+                <property name="label" translatable="yes">Remote _name:</property>
+                <property name="use_underline">True</property>
+                <property name="mnemonic_widget">d_entry_remote_name</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">0</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkEntry" id="d_entry_remote_name">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="hexpand">True</property>
+                <property name="activates_default">True</property>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="top_attach">0</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="label2">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">end</property>
+                <property name="label" translatable="yes">Remote _URL:</property>
+                <property name="use_underline">True</property>
+                <property name="mnemonic_widget">d_entry_remote_url</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">1</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkEntry" id="d_entry_remote_url">
+                <property name="width_request">350</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="hexpand">True</property>
+                <property name="activates_default">True</property>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="top_attach">1</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="cancel">button_cancel</action-widget>
+      <action-widget response="ok">d_button_create</action-widget>
+    </action-widgets>
+  </template>
+</interface>


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