2009-10-28 19:30:26 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
2009-10-27 15:49:21 +00:00
|
|
|
const DBus = imports.dbus;
|
2009-10-28 19:30:26 +00:00
|
|
|
const Lang = imports.lang;
|
2009-10-27 15:49:21 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
|
2009-10-29 20:14:39 +00:00
|
|
|
const Main = imports.ui.main;
|
|
|
|
|
2009-10-28 19:30:26 +00:00
|
|
|
const AVATAR_SIZE = 24;
|
|
|
|
|
2009-10-27 15:49:21 +00:00
|
|
|
const TELEPATHY = "org.freedesktop.Telepathy.";
|
|
|
|
const CONN = TELEPATHY + "Connection";
|
|
|
|
const CHANNEL = TELEPATHY + "Channel";
|
|
|
|
const CHANNELTEXT = CHANNEL + ".Type.Text";
|
|
|
|
|
|
|
|
const ClientIface = {
|
|
|
|
name: TELEPATHY + "Client",
|
|
|
|
properties: [{ name: "Interfaces",
|
|
|
|
signature: "as",
|
|
|
|
access: "read" }]
|
|
|
|
};
|
|
|
|
|
|
|
|
const ClientObserverIface = {
|
|
|
|
name: TELEPATHY + "Client.Observer",
|
|
|
|
methods: [{ name: "ObserveChannels",
|
|
|
|
inSignature: "ooa(oa{sv})oaoa{sv}",
|
|
|
|
outSignature: "" }],
|
|
|
|
properties: [{ name: "ObserverChannelFilter",
|
|
|
|
signature: "aa{sv}",
|
|
|
|
access: "read" }]
|
|
|
|
};
|
|
|
|
|
|
|
|
const ConnectionIface = {
|
2009-10-28 19:30:26 +00:00
|
|
|
name: CONN,
|
|
|
|
signals: [
|
|
|
|
{ name: 'StatusChanged', inSignature: 'u' }
|
|
|
|
]
|
2009-10-27 15:49:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const ConnectionAvatarsIface = {
|
|
|
|
name: CONN + '.Interface.Avatars',
|
|
|
|
methods: [
|
|
|
|
{ name: 'RequestAvatar',
|
|
|
|
inSignature: 'u',
|
|
|
|
outSignature: 'ays'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
function Connection(path) {
|
|
|
|
this._init(path);
|
|
|
|
};
|
|
|
|
|
|
|
|
Connection.prototype = {
|
|
|
|
_init: function(path) {
|
|
|
|
DBus.session.proxifyObject(this, nameify(path), path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DBus.proxifyPrototype(Connection.prototype, ConnectionIface);
|
|
|
|
DBus.proxifyPrototype(Connection.prototype, ConnectionAvatarsIface);
|
|
|
|
|
|
|
|
const ChannelIface = {
|
|
|
|
name: CHANNEL,
|
|
|
|
signals: [
|
|
|
|
{ name: 'Closed', inSignature: '' }
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
function Channel(name, path) {
|
|
|
|
this._init(name, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
Channel.prototype = {
|
|
|
|
_init: function(name, path) {
|
|
|
|
DBus.session.proxifyObject(this, name, path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DBus.proxifyPrototype(Channel.prototype, ChannelIface);
|
|
|
|
|
|
|
|
const ChannelTextIface = {
|
|
|
|
name: CHANNELTEXT,
|
2009-10-28 19:30:26 +00:00
|
|
|
methods: [
|
|
|
|
{ name: 'ListPendingMessages',
|
|
|
|
inSignature: 'b',
|
|
|
|
outSignature: 'a(uuuuus)'
|
|
|
|
}
|
|
|
|
],
|
2009-10-27 15:49:21 +00:00
|
|
|
signals: [
|
|
|
|
{ name: 'Received', inSignature: 'uuuuus' }
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
function ChannelText(name, path) {
|
|
|
|
this._init(name, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
ChannelText.prototype = {
|
|
|
|
_init: function(name, path) {
|
|
|
|
DBus.session.proxifyObject(this, name, path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DBus.proxifyPrototype(ChannelText.prototype, ChannelTextIface);
|
|
|
|
|
|
|
|
let nameify = function(path) {
|
|
|
|
return path.substr(1).replace('/', '.', 'g');
|
|
|
|
};
|
|
|
|
|
|
|
|
let pathify = function(name) {
|
|
|
|
return '/' + name.replace('.', '/', 'g');
|
|
|
|
};
|
|
|
|
|
|
|
|
function Messaging() {
|
|
|
|
this._init();
|
|
|
|
};
|
|
|
|
|
|
|
|
Messaging.prototype = {
|
|
|
|
_init : function() {
|
|
|
|
let name = TELEPATHY + "Client.GnomeShell";
|
|
|
|
DBus.session.exportObject(pathify(name), this);
|
|
|
|
DBus.session.acquire_name(name, DBus.SINGLE_INSTANCE,
|
|
|
|
function(name){log("Acquired name " + name);},
|
|
|
|
function(name){log("Lost name " + name);});
|
2009-10-28 19:30:26 +00:00
|
|
|
|
|
|
|
this._conns = {};
|
2009-10-27 15:49:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get Interfaces() {
|
|
|
|
return [TELEPATHY + "Client.Observer"];
|
|
|
|
},
|
|
|
|
|
|
|
|
get ObserverChannelFilter() {
|
|
|
|
return [
|
|
|
|
{ 'org.freedesktop.Telepathy.Channel.ChannelType': CHANNELTEXT }
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
|
|
|
ObserveChannels: function(account, conn_path, channels,
|
|
|
|
dispatch_operation, requests_satisfied,
|
|
|
|
observer_info) {
|
2009-10-28 19:30:26 +00:00
|
|
|
let conn = this._conns[conn_path];
|
|
|
|
if (!conn) {
|
|
|
|
conn = new Connection(conn_path);
|
|
|
|
conn.connect('StatusChanged', Lang.bind(this, this._connectionStatusChanged));
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn_name = nameify(conn_path);
|
|
|
|
for (let i = 0; i < channels.length; i++) {
|
|
|
|
new Source(conn, conn_name, channels[i][0], channels[i][1]);
|
2009-10-27 15:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [true];
|
2009-10-28 19:30:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_connectionStatusChanged: function(connection, status) {
|
|
|
|
if (status == Connection_Status.Disconnected) {
|
|
|
|
delete this._conns[connection.getPath()];
|
|
|
|
}
|
2009-10-27 15:49:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DBus.conformExport(Messaging.prototype, ClientIface);
|
|
|
|
DBus.conformExport(Messaging.prototype, ClientObserverIface);
|
2009-10-28 19:30:26 +00:00
|
|
|
|
|
|
|
function Source(conn, conn_name, channel_path, channel_props) {
|
|
|
|
this._init(conn, conn_name, channel_path, channel_props);
|
|
|
|
}
|
|
|
|
|
|
|
|
Source.prototype = {
|
|
|
|
_init: function(conn, conn_name, channel_path, channel_props) {
|
|
|
|
this._targetId = channel_props[CHANNEL + '.TargetID'];
|
|
|
|
|
|
|
|
log('channel for ' + this._targetId);
|
|
|
|
|
2009-11-03 17:13:08 +00:00
|
|
|
this._pendingMessages = null;
|
2009-10-28 19:30:26 +00:00
|
|
|
|
2009-11-03 17:13:08 +00:00
|
|
|
// FIXME: RequestAvatar is deprecated in favor of
|
|
|
|
// RequestAvatars; but RequestAvatars provides no explicit
|
|
|
|
// indication of "no avatar available", so there's no way we
|
|
|
|
// can reliably wait for it to finish before displaying a
|
|
|
|
// message. So we use RequestAvatar() instead.
|
2009-10-28 19:30:26 +00:00
|
|
|
let targethandle = channel_props[CHANNEL + '.TargetHandle'];
|
|
|
|
conn.RequestAvatarRemote(targethandle, Lang.bind(this, this._gotAvatar));
|
|
|
|
|
|
|
|
this._channel = new Channel(conn_name, channel_path);
|
|
|
|
this._closedId = this._channel.connect('Closed', Lang.bind(this, this._channelClosed));
|
|
|
|
|
|
|
|
this._channelText = new ChannelText(conn_name, channel_path);
|
|
|
|
this._receivedId = this._channelText.connect('Received', Lang.bind(this, this._receivedMessage));
|
|
|
|
|
|
|
|
this._channelText.ListPendingMessagesRemote(false,
|
|
|
|
Lang.bind(this, function(msgs, excp) {
|
|
|
|
if (msgs) {
|
2009-11-03 17:13:08 +00:00
|
|
|
log('got pending messages for ' + this._targetId);
|
|
|
|
this._pendingMessages = msgs;
|
|
|
|
this._processPendingMessages();
|
|
|
|
}
|
|
|
|
}));
|
2009-10-28 19:30:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_gotAvatar: function(result, excp) {
|
|
|
|
if (result) {
|
|
|
|
let bytes = result[0];
|
|
|
|
this._avatar = Shell.TextureCache.get_default().load_from_data(bytes, bytes.length, AVATAR_SIZE, AVATAR_SIZE);
|
2009-11-03 17:13:08 +00:00
|
|
|
log('got avatar for ' + this._targetId);
|
2009-10-28 19:30:26 +00:00
|
|
|
} else {
|
|
|
|
// fallback avatar (FIXME)
|
|
|
|
this._avatar = Shell.TextureCache.get_default().load_icon_name("stock_person", AVATAR_SIZE);
|
2009-11-03 17:13:08 +00:00
|
|
|
log('using default avatar for ' + this._targetId);
|
2009-10-28 19:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._processPendingMessages();
|
|
|
|
},
|
|
|
|
|
|
|
|
_processPendingMessages: function() {
|
2009-11-03 17:13:08 +00:00
|
|
|
if (!this._avatar || !this._pendingMessages)
|
|
|
|
return;
|
2009-10-28 19:30:26 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < this._pendingMessages.length; i++)
|
|
|
|
this._receivedMessage.apply(this, [this._channel].concat(this._pendingMessages[i]));
|
2009-11-03 17:13:08 +00:00
|
|
|
this._pendingMessages = null;
|
2009-10-28 19:30:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_channelClosed: function() {
|
|
|
|
log('closed');
|
|
|
|
this._channel.disconnect(this._closedId);
|
|
|
|
this._channelText.disconnect(this._receivedId);
|
|
|
|
},
|
|
|
|
|
|
|
|
_receivedMessage: function(channel, id, timestamp, sender,
|
|
|
|
type, flags, text) {
|
|
|
|
log('Received: id ' + id + ', time ' + timestamp + ', sender ' + sender + ', type ' + type + ', flags ' + flags + ': ' + text);
|
2009-11-03 15:47:02 +00:00
|
|
|
Main.notificationPopup.show(this._avatar, text);
|
2009-10-28 19:30:26 +00:00
|
|
|
}
|
|
|
|
};
|