From 271c4a420490cb819048df6ee054d56dbbcf2120 Mon Sep 17 00:00:00 2001 From: guinux Date: Wed, 28 Jan 2015 16:40:05 +0100 Subject: [PATCH] some code improvements --- src/aur.vala | 35 ++++++++++++++++++++++------------- src/manager_window.vala | 6 +++--- src/packages_model.vala | 27 +++++++++++---------------- src/transaction.vala | 4 ++-- 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/aur.vala b/src/aur.vala index c3ee850..285efe6 100644 --- a/src/aur.vala +++ b/src/aur.vala @@ -33,15 +33,16 @@ namespace AUR { var session = new Soup.Session (); var message = new Soup.Message ("GET", uri); var parser = new Json.Parser (); - unowned Json.Object root_object; session.send_message (message); try { parser.load_from_data ((string) message.response_body.flatten ().data, -1); - root_object = parser.get_root ().get_object (); - prev_inter = root_object.get_array_member ("results"); } catch (Error e) { print (e.message); } + unowned Json.Node? root = parser.get_root (); + if (root != null) { + prev_inter = root.get_object ().get_array_member ("results"); + } int length = needles.length; if (length == 1) return prev_inter; @@ -55,11 +56,13 @@ namespace AUR { session.send_message (message); try { parser.load_from_data ((string) message.response_body.flatten ().data, -1); - root_object = parser.get_root ().get_object (); - found = root_object.get_array_member ("results"); } catch (Error e) { print (e.message); } + root = parser.get_root (); + if (root != null) { + found = root.get_object ().get_array_member ("results"); + } foreach (var prev_inter_node in prev_inter.get_elements ()) { foreach (var found_node in found.get_elements ()) { if (strcmp (prev_inter_node.get_object ().get_string_member ("Name"), @@ -68,27 +71,31 @@ namespace AUR { } } } - if (i != (length -1)) + if (i != (length -1)) { prev_inter = inter; + } i += 1; } return inter; } - public Json.Object? info (string pkgname) { - unowned Json.Object? pkg_info = null; + public Json.Object info (string pkgname) { + var pkg_info = new Json.Object (); string uri = rpc_url + rpc_info + Uri.escape_string (pkgname); var session = new Soup.Session (); var message = new Soup.Message ("GET", uri); session.send_message (message); + var parser = new Json.Parser (); try { - var parser = new Json.Parser (); parser.load_from_data ((string) message.response_body.flatten ().data, -1); - pkg_info = parser.get_root ().get_object ().get_object_member ("results"); } catch (Error e) { stderr.printf ("Failed to get infos about %s from AUR\n", pkgname); print (e.message); } + unowned Json.Node? root = parser.get_root (); + if (root != null) { + pkg_info = root.get_object ().get_object_member ("results"); + } return pkg_info; } @@ -104,14 +111,16 @@ namespace AUR { var session = new Soup.Session (); var message = new Soup.Message ("GET", builder.str); session.send_message (message); + var parser = new Json.Parser (); try { - var parser = new Json.Parser (); parser.load_from_data ((string) message.response_body.flatten ().data, -1); - unowned Json.Object root_object = parser.get_root ().get_object (); - results = root_object.get_array_member ("results"); } catch (Error e) { print (e.message); } + unowned Json.Node? root = parser.get_root (); + if (root != null) { + results = root.get_object ().get_array_member ("results"); + } return results; } } diff --git a/src/manager_window.vala b/src/manager_window.vala index 61e91bb..147afce 100644 --- a/src/manager_window.vala +++ b/src/manager_window.vala @@ -469,7 +469,7 @@ namespace Pamac { return pkgs; } - public void populate_packages_list (Alpm.List? pkgs, Json.Array? aur_pkgs = null) { + public void populate_packages_list (Alpm.List? pkgs, Json.Array? aur_pkgs = new Json.Array ()) { packages_treeview.freeze_child_notify (); packages_treeview.set_model (null); @@ -924,7 +924,7 @@ namespace Pamac { Json.Array aur_pkgs; Alpm.List pkgs = search_pkgs.end (res, out aur_pkgs); populate_packages_list (pkgs, aur_pkgs); - }); + }); } } @@ -1058,7 +1058,7 @@ namespace Pamac { text.append ("\n"); } } catch (GLib.Error e) { - GLib.stderr.printf("%s\n", e.message); + GLib.stderr.printf ("%s\n", e.message); } history_dialog.textview.buffer.set_text (text.str, (int) text.len); history_dialog.run (); diff --git a/src/packages_model.vala b/src/packages_model.vala index 2205035..0a79733 100644 --- a/src/packages_model.vala +++ b/src/packages_model.vala @@ -29,23 +29,18 @@ namespace Pamac { foreach (unowned Alpm.Package alpm_pkg in alpm_pkgs) { all_pkgs.append (new Pamac.Package (alpm_pkg, null)); } - if (aur_pkgs != null) { - unowned Json.Object pkg_info; - string name; - bool found; - foreach (var node in aur_pkgs.get_elements ()) { - pkg_info = node.get_object (); - name = pkg_info.get_string_member ("Name"); - // add only the packages which are not already in the list - found = false; - foreach (Pamac.Package pkg in all_pkgs) { - if (pkg.name == name) { - found = true; - break; - } + bool found; + foreach (var node in aur_pkgs.get_elements ()) { + // add only the packages which are not already in the list + found = false; + foreach (Pamac.Package pkg in all_pkgs) { + if (pkg.name == node.get_object ().get_string_member ("Name")) { + found = true; + break; } - if (found == false) - all_pkgs.append (new Pamac.Package (null, pkg_info)); + } + if (found == false) { + all_pkgs.append (new Pamac.Package (null, node.get_object ())); } } if (all_pkgs.length () == 0) { diff --git a/src/transaction.vala b/src/transaction.vala index ff1a26a..39ac4cc 100644 --- a/src/transaction.vala +++ b/src/transaction.vala @@ -537,7 +537,7 @@ namespace Pamac { public void build_aur_packages () { print ("building packages\n"); string action = dgettext (null,"Building packages") + "..."; - spawn_in_term ({"echo", "-n", action}); + spawn_in_term ({"echo", action}); progress_dialog.action_label.set_text (action); progress_dialog.progressbar.set_fraction (0); progress_dialog.progressbar.set_text (""); @@ -1131,7 +1131,7 @@ namespace Pamac { void on_emit_generate_mirrorlist_start () { string action = dgettext (null, "Generating mirrorlist") + "..."; - spawn_in_term ({"echo", "-n", action}); + spawn_in_term ({"echo", action}); progress_dialog.action_label.set_text (action); progress_dialog.progressbar.set_fraction (0); progress_dialog.progressbar.set_text ("");