[gitg/wip/clone] dash: factor out create_repository_data and recent manager method



commit 4b28cc0e9630324982072bf337b41067ed5adebc
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Thu Mar 21 20:06:18 2013 +0100

    dash: factor out create_repository_data and recent manager method

 libgitg-gtk/gitg-gtk-dash-view.vala |  140 ++++++++++++++++++-----------------
 1 files changed, 71 insertions(+), 69 deletions(-)
---
diff --git a/libgitg-gtk/gitg-gtk-dash-view.vala b/libgitg-gtk/gitg-gtk-dash-view.vala
index a63f356..dda384a 100644
--- a/libgitg-gtk/gitg-gtk-dash-view.vala
+++ b/libgitg-gtk/gitg-gtk-dash-view.vala
@@ -29,7 +29,7 @@ namespace GitgGtk
                private Egg.ListBox d_listbox;
                private class RepositoryData
                {
-                       public Repository repository;
+                       public Repository? repository;
                        public DateTime time;
                        public Grid grid;
                        public Image image;
@@ -160,6 +160,68 @@ namespace GitgGtk
                        return data;
                }
 
+               private RepositoryData create_repository_data(string name, string branch_name, bool spin)
+               {
+                       var data = new RepositoryData();
+                       data.repository = null;
+                       data.time = new DateTime.now_local();
+                       data.grid = new Grid();
+                       data.grid.margin = 12;
+                       data.grid.column_spacing = 10;
+
+                       // FIXME: choose the folder image in relation to the next:
+                       // - the repository is local
+                       // - the repository has a remote
+                       // - the repository uses github...
+                       data.image = new Image.from_icon_name("folder", d_icon_size);
+                       data.grid.attach(data.image, 0, 0, 1, 2);
+
+                       data.repository_label = new Label(null);
+                       data.repository_label.set_markup("<b>%s</b>".printf(name));
+                       data.repository_label.ellipsize = Pango.EllipsizeMode.END;
+                       data.repository_label.halign = Align.START;
+                       data.repository_label.valign = Align.END;
+                       data.repository_label.hexpand = true;
+                       data.grid.attach(data.repository_label, 1, 0, 1, 1);
+
+                       data.branch_label = new Label("");
+                       data.branch_label.set_markup("<small>%s</small>".printf(branch_name));
+                       data.branch_label.ellipsize = Pango.EllipsizeMode.END;
+                       data.branch_label.valign = Align.START;
+                       data.branch_label.halign = Align.START;
+                       data.branch_label.get_style_context().add_class("dim-label");
+                       data.grid.attach(data.branch_label, 1, 1, 1, 1);
+
+                       data.arrow = new Arrow(ArrowType.RIGHT, ShadowType.NONE);
+                       data.grid.attach(data.arrow, 2, 0, 1, 2);
+
+                       data.grid.set_data<RepositoryData>("data", data);
+                       data.grid.show_all();
+                       d_listbox.add(data.grid);
+
+                       if (spin)
+                       {
+                               data.arrow.hide();
+                               data.spinner = new Spinner();
+                               data.grid.attach(data.arrow, 2, 0, 1, 2);
+                               data.spinner.show();
+                               data.spinner.start();
+                       }
+
+                       return data;
+               }
+
+               private void add_repository_to_recent_manager(string uri)
+               {
+                       var recent_manager = RecentManager.get_default();
+                       var item = RecentData();
+                       item.app_name = Environment.get_application_name();
+                       item.mime_type = "inode/directory";
+                       item.app_exec = string.join(" ", Environment.get_prgname(), "%f");
+                       item.groups = { "gitg", null };
+                       recent_manager.add_full(uri, item);
+               }
+
                public void add_repository(Gitg.Repository repository)
                {
                        RepositoryData? data = get_data_for_repository(repository);
@@ -168,73 +230,19 @@ namespace GitgGtk
 
                        if (data == null)
                        {
-                               data = new RepositoryData();
-                               data.repository = repository;
-                               data.time = new DateTime.now_local();
-                               data.grid = new Grid();
-                               data.grid.margin = 12;
-                               data.grid.column_spacing = 10;
-
-                               // FIXME: choose the folder image in relation to the next:
-                               // - the repository is local
-                               // - the repository has a remote
-                               // - the repository uses github...
-                               data.image = new Image.from_icon_name("folder", d_icon_size);
-                               data.grid.attach(data.image, 0, 0, 1, 2);
-
-                               data.repository_label = new Label(null);
-                               var label_text = (workdir != null) ? workdir.get_basename() : 
repo_file.get_basename();
-                               data.repository_label.set_markup("<b>%s</b>".printf(label_text));
-                               data.repository_label.ellipsize = Pango.EllipsizeMode.END;
-                               data.repository_label.halign = Align.START;
-                               data.repository_label.valign = Align.END;
-                               data.repository_label.hexpand = true;
-                               data.grid.attach(data.repository_label, 1, 0, 1, 1);
-
-                               data.branch_label = new Label("");
-                               data.branch_label.ellipsize = Pango.EllipsizeMode.END;
-                               data.branch_label.valign = Align.START;
-                               data.branch_label.halign = Align.START;
-                               data.branch_label.get_style_context().add_class("dim-label");
-                               data.grid.attach(data.branch_label, 1, 1, 1, 1);
-
+                               var name = (workdir != null) ? workdir.get_basename() : 
repo_file.get_basename();
                                Gitg.Ref? head = null;
+                               string branch_name = "";
+
                                try
                                {
                                        head = repository.get_head();
+                                       branch_name = head.parsed_name.shortname;
                                }
                                catch {}
 
-                               // show the active branch
-                               if (head != null)
-                               {
-                                       try
-                                       {
-                                               repository.branches_foreach(Ggit.BranchType.LOCAL, 
(branch_name, branch_type) => {
-                                                       try
-                                                       {
-                                                               Ref? reference = 
repository.lookup_reference("refs/heads/" + branch_name);
-
-                                                               if (reference != null && 
reference.get_target().equal(head.get_target()))
-                                                               {
-                                                                       
data.branch_label.set_markup("<small>%s</small>".printf(branch_name));
-                                                                       return 1;
-                                                               }
-                                                       }
-                                                       catch {}
-
-                                                       return 0;
-                                               });
-                                       }
-                                       catch {}
-                               }
-
-                               data.arrow = new Arrow(ArrowType.RIGHT, ShadowType.NONE);
-                               data.grid.attach(data.arrow, 2, 0, 1, 2);
-
-                               data.grid.set_data<RepositoryData>("data", data);
-                               data.grid.show_all();
-                               d_listbox.add(data.grid);
+                               data = create_repository_data(name, branch_name, false);
+                               data.repository = repository;
                        }
                        else
                        {
@@ -243,14 +251,8 @@ namespace GitgGtk
                                d_listbox.resort();
                        }
 
-                       // add repository to recent manager
-                       var recent_manager = RecentManager.get_default();
-                       var item = RecentData();
-                       item.app_name = Environment.get_application_name();
-                       item.mime_type = "inode/directory";
-                       item.app_exec = string.join(" ", Environment.get_prgname(), "%f");
-                       item.groups = { "gitg", null };
-                       recent_manager.add_full(workdir.get_uri(), item);
+                       var uri = (workdir != null) ? workdir.get_uri() : repo_file.get_uri();
+                       add_repository_to_recent_manager(uri);
                }
 
                public void clone_repository(string url, File location)


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