X-Git-Url: http://git.roojs.org/?p=gitlive;a=blobdiff_plain;f=RooProject.vala;fp=RooProject.vala;h=23fcbfa2f479577f535df297ca61eff75f31013a;hp=0000000000000000000000000000000000000000;hb=7787e7058c952459a07f64edaa20d5e7778dc1fa;hpb=d06e3c696bfaee21c6e9201b4cebcd1ac5bcc32a diff --git a/RooProject.vala b/RooProject.vala new file mode 100644 index 00000000..23fcbfa2 --- /dev/null +++ b/RooProject.vala @@ -0,0 +1,159 @@ +/** +This a mostly a static class that manages an array of it'self... + +*/ + +static RooProject _RooProject; +public class RooProject : Object +{ + + public string id; // not really important that they are numbers.. + public string code; + public string name; + public string type; + + + // --- static from here on.... + + + + static Gee.ArrayList _projects; + static int loadcount = 0; + const string roourl = "https://roojs.com/admin.php/Roo"; + + + static RooProject singleton() + { + if (_RooProject == null) { + _RooProject = new RooProject(); + RooProject.loadcount = 0; + RooProject.loadProjects(); + + } + return _RooProject; + } + + + public static Gee.ArrayList projects() + { + RooProject.singleton(); + + return RooProject._projects; + } + + + public static RooProject? getProjectByRepo(GitRepo repo) + { + RooProject.singleton(); // init... + + // fixme -- needs to get from rep list.. + + var pid = ""; + foreach(var roo_repo in RooRepo.repos()) { + if (roo_repo.shortname == repo.name) { + pid = roo_repo.project_id; + break; + } + } + if (pid == "") { + GLib.debug("getProjectByRepo: repo has no project"); + return null; + } + // get project by id... + foreach(var roo_project in RooProject.projects()) { + if (roo_project.id == pid) { + GLib.debug("getProjectByRepo: project_id = %s", pid); + return roo_project; + } + } + GLib.debug("getProjectByRepo: can not find project"); + return null; + + } + + + public static RooProject addProject(Json.Object t) + { + RooProject.singleton(); // init... + + var add = new RooProject(); + add.id = t.get_string_member("id"); + add.name = t.get_string_member("name"); + add.type = t.get_string_member("type"); + add.code = t.get_string_member("code"); + RooProject._projects.add(add); + GLib.debug("ADD project %s : %s : %s", add.id, add.code, add.name); + return add; + } + + public static void reload() /// has to be called on singleton.. + { + RooProject.loadcount = 0; + RooProject.singleton(); + if (RooProject.loadcount == 0 ) { + RooProject.loadProjects(); + } + } + + static void loadProjects() // called from singleton... + { + RooProject._projects = new Gee.ArrayList(); + + RooProject.loadcount ++; + var table = new GLib.HashTable(str_hash, str_equal); + + table.insert("query[project_filter]","P,N,U"); + table.insert("limit","200"); + table.insert("sort","name"); + table.insert("dir","ASC"); + + var params = Soup.Form.encode_hash(table); + + var url = "%s/%s?%s" . printf(roourl, "core_project", params); + + GLib.debug("request %s", url); + + var session = new Soup.Session (); + session.timeout = 0; + var message = new Soup.Message ("GET", url); + + + RooTicket.setAuth(message); + + session.send_message (message); + + var data = (string) message.response_body.flatten().data; + //GLib.debug("got %s", data); + try { + var parser = new Json.Parser (); + parser.load_from_data (data, -1); + + var response = parser.get_root().get_object(); + var status = response.get_boolean_member("success"); + + if(!status){ + GLib.error(response.get_string_member("errorMsg")); + return; + } + var rd = response.get_array_member ("data"); + + // got a valid result... + + for(var i = 0; i < rd.get_length(); i++) { + RooProject.addProject(rd.get_object_element(i)); + } + + + } catch (Error e) { + GLib.error(e.message); + return; + } + + } + + + + + +} +