From b9145a8161071bee25c6c1c026fbc2381bdfa0bb Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 21 Jun 2010 23:12:44 +0200 Subject: Show roster --- src/gui/CMakeLists.txt | 4 + src/gui/CellRendererContact.vala | 13 +++ src/gui/Contact.vala | 24 ++++++ src/gui/CoreConnector.vala | 53 ++++++++++--- src/gui/Ephraim.vala | 14 +++- src/gui/Roster.vala | 166 +++++++++++++++++++++++++++++++++++++++ src/gui/ephraim.glade | 86 ++------------------ 7 files changed, 264 insertions(+), 96 deletions(-) create mode 100644 src/gui/CellRendererContact.vala create mode 100644 src/gui/Contact.vala create mode 100644 src/gui/Roster.vala (limited to 'src/gui') diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 8839e16..d2a464c 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -2,9 +2,13 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ephraim_BINARY_DIR}) vala_precompile(VALA_C "Ephraim.vala" + "CellRendererContact.vala" + "Contact.vala" "CoreConnector.vala" + "Roster.vala" PACKAGES gtk+-2.0 + gee-1.0 erl_interface OPTIONS --thread diff --git a/src/gui/CellRendererContact.vala b/src/gui/CellRendererContact.vala new file mode 100644 index 0000000..f8aa6a1 --- /dev/null +++ b/src/gui/CellRendererContact.vala @@ -0,0 +1,13 @@ +public class CellRendererContact : Gtk.CellRendererText { + private Contact _contact; + + public Contact contact { + get { + return _contact; + } + set { + _contact = value; + text = contact.jid; + } + } +} diff --git a/src/gui/Contact.vala b/src/gui/Contact.vala new file mode 100644 index 0000000..2eb40ae --- /dev/null +++ b/src/gui/Contact.vala @@ -0,0 +1,24 @@ +public class Contact : Object { + public enum Show { + ONLINE, AWAY, CHAT, DND, XA, UNDEFINED + } + + public enum Subscription { + BOTH + } + + public Contact(string jid0) { + Object(jid: jid0); + } + + public class Resource : Object { + public int priority {get; construct;} + public Show show {get; construct;} + public string status {get; construct;} + } + + public string jid {get; construct;} + public Subscription subscription {get; set;} + public string name {get; set;} + public Gee.HashMap resources {get; private set;} +} diff --git a/src/gui/CoreConnector.vala b/src/gui/CoreConnector.vala index 9edbc7d..fdd74ee 100644 --- a/src/gui/CoreConnector.vala +++ b/src/gui/CoreConnector.vala @@ -2,6 +2,12 @@ public class CoreConnector { unowned Thread thread; bool running; + Erl.Node node; + Erl.Connection con; + Erl.Term self; + + Roster roster; + private class TermStore { public Erl.Term term; } @@ -10,8 +16,9 @@ public class CoreConnector { Erl.init(); } - public CoreConnector() { + public CoreConnector(Roster roster0) { running = false; + roster = roster0; } public bool start() { @@ -20,6 +27,13 @@ public class CoreConnector { running = true; + node = Erl.Node("ephraim-gtk", "magiccookie", 0); + con = node.connect("ephraim-core@avalon.local"); + + self = Erl.mk_self_pid(node); + + con.reg_send("ephraim", Erl.format("{register_ui,~w}", self)); + try { thread = Thread.create(receive, true); return true; @@ -34,16 +48,12 @@ public class CoreConnector { running = false; thread.join(); + + con.reg_send("ephraim", Erl.format("{unregister_ui,~w}", self)); } + private void* receive() { - Erl.Node node = Erl.Node("ephraim-gtk", "magiccookie", 0); - Erl.Connection con = node.connect("ephraim-core@avalon.local"); - - Erl.Term self = Erl.mk_self_pid(node); - - con.reg_send("ephraim", Erl.format("{register_ui,~w}", self)); - while(running) { TermStore response = new TermStore(); Erl.ReceiveType ret = con.receive(out response.term, 1000); @@ -64,14 +74,31 @@ public class CoreConnector { } } - con.reg_send("ephraim", Erl.format("{unregister_ui,~w}", self)); - return null; } + private Erl.Term? match(string pattern, Erl.Term term) { + Erl.Term pattern_term = Erl.format(pattern); + + if(pattern_term.match(term) != 0) + return pattern_term; + else + return null; + } + private void handleTerm(TermStore store) { - unowned Erl.Term term = store.term; - Erl.print_term(stdout, term); - stdout.printf("\n"); + unowned Erl.Term term = store.term; + Erl.Term match_term; + + if((match_term = match("{roster_update,JID,Resources}", term)) != null) { + Erl.Term JID_term = match_term.var_content("JID"); + string JID = ((string)JID_term.bin_ptr()).ndup(JID_term.bin_size()); + + roster.update_contact(new Contact(JID)); + } + else { + Erl.print_term(stdout, term); + stdout.printf("\n"); + } } } diff --git a/src/gui/Ephraim.vala b/src/gui/Ephraim.vala index a52c4df..cf87965 100644 --- a/src/gui/Ephraim.vala +++ b/src/gui/Ephraim.vala @@ -9,14 +9,24 @@ public class Ephraim { return 1; } - CoreConnector coreconn = new CoreConnector(); + Roster roster = new Roster(); + CoreConnector coreconn = new CoreConnector(roster); if(!coreconn.start()) return 1; unowned Gtk.Window window = builder.get_object("MainWindow") as Gtk.Window; window.hide.connect(Gtk.main_quit); - window.show(); + + unowned Gtk.MenuItem quitItem = builder.get_object("MenuItemQuit") as Gtk.MenuItem; + quitItem.activate.connect(() => window.visible = false); + + unowned Gtk.TreeView rosterView = builder.get_object("Roster") as Gtk.TreeView; + rosterView.set_model(roster.get_default_group()); + + rosterView.append_column(new Gtk.TreeViewColumn.with_attributes("Contact", new CellRendererContact(), "contact", 0, null)); + + window.visible = true; Gtk.main(); diff --git a/src/gui/Roster.vala b/src/gui/Roster.vala new file mode 100644 index 0000000..2ef4618 --- /dev/null +++ b/src/gui/Roster.vala @@ -0,0 +1,166 @@ +public class Roster : Object { + private int stamp; + private Gee.TreeMap entries = new Gee.TreeMap(); + private Gee.TreeMap groups = new Gee.TreeMap(); + + + public Roster() { + stamp = 0; + groups["default"] = new RosterGroup(this); + } + + public Gtk.TreeModel get_default_group() { + return groups["default"]; + } + + public void update_contact(Contact c) { + entries[c.jid] = c; + groups["default"].update_contact(c); + } + + private class RosterGroup : Object, Gtk.TreeModel { + private unowned Roster roster {get; set;} + private Gee.TreeMap entries = new Gee.TreeMap(); + + + public RosterGroup(Roster roster0) { + roster = roster0; + } + + public void update_contact(Contact c) { + bool added = !entries.has_key(c.jid); + + entries[c.jid] = c; + + Gtk.TreeIter iter = Gtk.TreeIter(); + iter.stamp = roster.stamp; + iter.user_data = this; + iter.user_data2 = c; + + Gtk.TreePath path = get_path(iter); + + if (added) + row_inserted(path, iter); + else + row_changed(path, iter); + } + + public Type get_column_type (int index) { + switch(index) { + case 0: + return typeof(Contact); + } + + return Type.INVALID; + } + + public Gtk.TreeModelFlags get_flags () { + return Gtk.TreeModelFlags.LIST_ONLY; + } + + public bool get_iter (out Gtk.TreeIter iter, Gtk.TreePath path) { + if(path.get_depth() != 1) + return false; + + int index = path.get_indices()[0]; + if(index < 0 || index >= entries.size) + return false; + + Gee.MapIterator it = entries.map_iterator(); + it.first(); + for(int i = 0; i < index; ++i) + it.next(); + + iter.stamp = roster.stamp; + iter.user_data = this; + iter.user_data2 = it.get_value(); + + return true; + } + + public int get_n_columns () { + return 1; + } + + public Gtk.TreePath get_path (Gtk.TreeIter iter) { + if(iter.stamp != roster.stamp || iter.user_data != this) + return (Gtk.TreePath)null; + + int index = 0; + + foreach(Contact c in entries.values) { + if(c == iter.user_data2) + break; + + ++index; + } + + return new Gtk.TreePath.from_indices(index, -1); + } + + public void get_value (Gtk.TreeIter iter, int column, out Value value) { + if (column != 0 || iter.stamp != roster.stamp || iter.user_data != this) + return; + + Contact c = iter.user_data2 as Contact; + value = Value(typeof(Contact)); + value.take_object(c); + } + + public bool iter_children (out Gtk.TreeIter iter, Gtk.TreeIter? parent) { + if (parent != null) { + iter.stamp = -1; + return false; + } + + return get_iter(out iter, new Gtk.TreePath.from_indices(0, -1)); + } + + public bool iter_has_child (Gtk.TreeIter iter) { + return false; + } + + public int iter_n_children (Gtk.TreeIter? iter) { + if (iter == null) + return entries.size; + else + return 0; + } + + public bool iter_next (ref Gtk.TreeIter iter) { + if(iter.stamp != roster.stamp || iter.user_data != this) + return false; + + bool next = false; + foreach(Contact c in entries.values) { + if(next) { + iter.user_data2 = c; + return true; + } + + if(c == iter.user_data2) + next = true; + } + + iter.stamp = -1; + return false; + } + + public bool iter_nth_child (out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n) { + if (parent != null) { + iter.stamp = -1; + return false; + } + + return get_iter(out iter, new Gtk.TreePath.from_indices(n, -1)); + } + + public bool iter_parent (out Gtk.TreeIter iter, Gtk.TreeIter child) { + iter.stamp = -1; + return false; + } + + public void ref_node (Gtk.TreeIter iter) {} + public void unref_node (Gtk.TreeIter iter) {} + } +} diff --git a/src/gui/ephraim.glade b/src/gui/ephraim.glade index b1b3455..629e2de 100644 --- a/src/gui/ephraim.glade +++ b/src/gui/ephraim.glade @@ -13,50 +13,18 @@ True - _Datei + _Aktionen True True - - - gtk-new - True - True - True - - - - - gtk-open - True - True - True - - - - - gtk-save - True - True - True - - - - - gtk-save-as - True - True - True - - True - + gtk-quit True True @@ -67,54 +35,10 @@ - - - True - _Bearbeiten - True - - - True - - - gtk-cut - True - True - True - - - - - gtk-copy - True - True - True - - - - - gtk-paste - True - True - True - - - - - gtk-delete - True - True - True - - - - - - True - _Ansicht + A_nsicht True @@ -148,11 +72,12 @@ True True + 160 True True - RosterStore + False False @@ -196,5 +121,4 @@ - -- cgit v1.2.3