some code improvements

This commit is contained in:
guinux 2015-01-28 16:40:05 +01:00
parent 66e66eab7e
commit 271c4a4204
4 changed files with 38 additions and 34 deletions

View File

@ -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;
}
}

View File

@ -469,7 +469,7 @@ namespace Pamac {
return pkgs;
}
public void populate_packages_list (Alpm.List<Alpm.Package?>? pkgs, Json.Array? aur_pkgs = null) {
public void populate_packages_list (Alpm.List<Alpm.Package?>? 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<Alpm.Package?> 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 ();

View File

@ -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) {

View File

@ -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 ("");