summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMorten Mjelva <morten.mjelva@gmail.com>2010-05-10 17:57:18 +0200
committerMorten Mjelva <morten.mjelva@gmail.com>2010-05-28 17:58:38 +0200
commitf25b43e59c9e3b554a007375535e1363bd648b33 (patch)
tree95a89c1e00f90c177e7b20200d0e0cbcd76b6670
parentf7ba66a44ab3bdf160afe83ffb11b9d064c91626 (diff)
Added SimpleObserver example
-rw-r--r--examples/js/text-observer.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/examples/js/text-observer.js b/examples/js/text-observer.js
new file mode 100644
index 00000000..d67cc405
--- /dev/null
+++ b/examples/js/text-observer.js
@@ -0,0 +1,79 @@
+const Tp = imports.gi.TelepathyGLib;
+const Mainloop = imports.mainloop;
+
+var chan_cache = [];
+
+function channel_invalidated_cb(chan, domain, code, msg)
+{
+ print("Channel invalidated");
+}
+
+function channel_ready_cb(chan, res)
+{
+ if (!chan.prepare_finish(res))
+ return;
+
+ let conn = chan.borrow_connection();
+ let [handle, handleType] = chan.get_handle();
+ let id = chan.get_identifier();
+
+ print("Chat started with " + id + " (" + handle + ")");
+
+ chan.connect('invalidated', channel_invalidated_cb);
+
+ /*
+ conn.get_contacts_by_handle(
+ 1, [handle],
+ 0, [],
+ function() { print("get by handle works!"); },
+ null);
+
+ conn.get_contacts_by_id(
+ 1, [id],
+ 0, [],
+ function() { print("get by id works"); },
+ null);
+ */
+}
+
+function observe_channels(observer, account, connection, channels,
+ dispatch_op, requests, context, user_data)
+{
+ for (let i in channels) {
+ let chan = channels[i];
+ let index = chan_cache.indexOf(chan);
+
+ if (index == -1) {
+ print("Channel not in cache");
+ chan_cache.push(chan);
+
+ chan.prepare_async(null, channel_ready_cb, null);
+ } else {
+ print("Channel cached at index: " + index);
+ }
+ }
+
+ context.accept();
+}
+
+function main()
+{
+ let dbus = Tp.DBusDaemon.dup();
+
+ let observer = Tp.SimpleObserver.new(dbus, true, "TextObserver", true,
+ observe_channels);
+
+ observer.add_observer_filter({
+ "org.freedesktop.Telepathy.Channel.ChannelType": Tp.IFACE_CHANNEL_TYPE_TEXT,
+ });
+
+ try {
+ observer.register();
+ } catch (e) {
+ print("Error: " + e);
+ }
+
+ Mainloop.run('');
+}
+
+main();