2010-02-02 15:21:47 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
const DBus = imports.dbus;
|
2010-04-14 14:24:14 +00:00
|
|
|
const GLib = imports.gi.GLib;
|
2010-02-02 15:21:47 +00:00
|
|
|
const Lang = imports.lang;
|
2010-11-25 14:29:41 +00:00
|
|
|
const Mainloop = imports.mainloop;
|
2010-04-16 21:24:34 +00:00
|
|
|
const Signals = imports.signals;
|
2010-02-02 15:21:47 +00:00
|
|
|
const St = imports.gi.St;
|
2010-04-16 21:24:34 +00:00
|
|
|
const Gettext = imports.gettext.domain('gnome-shell');
|
|
|
|
const _ = Gettext.gettext;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const MessageTray = imports.ui.messageTray;
|
|
|
|
const Telepathy = imports.misc.telepathy;
|
|
|
|
|
2010-05-11 16:26:00 +00:00
|
|
|
let contactManager;
|
2010-04-08 14:55:22 +00:00
|
|
|
let channelDispatcher;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2010-02-22 19:23:36 +00:00
|
|
|
// See Notification.appendMessage
|
2010-11-25 14:29:41 +00:00
|
|
|
const SCROLLBACK_IMMEDIATE_TIME = 60; // 1 minute
|
2010-02-22 19:23:36 +00:00
|
|
|
const SCROLLBACK_RECENT_TIME = 15 * 60; // 15 minutes
|
|
|
|
const SCROLLBACK_RECENT_LENGTH = 20;
|
|
|
|
const SCROLLBACK_IDLE_LENGTH = 5;
|
|
|
|
|
2010-04-16 19:18:32 +00:00
|
|
|
// A 'Qualified_Property_Value_Map' that represents a single-user
|
|
|
|
// text-based chat.
|
|
|
|
let singleUserTextChannel = {};
|
|
|
|
singleUserTextChannel[Telepathy.CHANNEL_NAME + '.ChannelType'] = Telepathy.CHANNEL_TEXT_NAME;
|
|
|
|
singleUserTextChannel[Telepathy.CHANNEL_NAME + '.TargetHandleType'] = Telepathy.HandleType.CONTACT;
|
|
|
|
|
|
|
|
// Some protocols only support 'multi-user' chats, and single-user
|
|
|
|
// chats are just treated as multi-user chats with only one other
|
|
|
|
// participant. Telepathy uses HandleType.NONE for all chats in these
|
|
|
|
// protocols; there's no good way for us to tell if the channel is
|
|
|
|
// single- or multi-user.
|
|
|
|
let oneOrMoreUserTextChannel = {};
|
|
|
|
oneOrMoreUserTextChannel[Telepathy.CHANNEL_NAME + '.ChannelType'] = Telepathy.CHANNEL_TEXT_NAME;
|
|
|
|
oneOrMoreUserTextChannel[Telepathy.CHANNEL_NAME + '.TargetHandleType'] = Telepathy.HandleType.NONE;
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
// The (non-chat) channel indicating the users whose presence
|
|
|
|
// information we subscribe to
|
|
|
|
let subscribedContactsChannel = {};
|
|
|
|
subscribedContactsChannel[Telepathy.CHANNEL_NAME + '.ChannelType'] = Telepathy.CHANNEL_CONTACT_LIST_NAME;
|
|
|
|
subscribedContactsChannel[Telepathy.CHANNEL_NAME + '.TargetHandleType'] = Telepathy.HandleType.LIST;
|
|
|
|
subscribedContactsChannel[Telepathy.CHANNEL_NAME + '.TargetID'] = 'subscribe';
|
|
|
|
|
2010-04-16 19:18:32 +00:00
|
|
|
|
2010-05-13 19:46:04 +00:00
|
|
|
// This is GNOME Shell's implementation of the Telepathy 'Client'
|
|
|
|
// interface. Specifically, the shell is a Telepathy 'Observer', which
|
2010-04-08 14:55:22 +00:00
|
|
|
// lets us see messages even if they belong to another app (eg,
|
|
|
|
// Empathy).
|
2010-02-02 15:21:47 +00:00
|
|
|
|
|
|
|
function Client() {
|
|
|
|
this._init();
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype = {
|
|
|
|
_init : function() {
|
|
|
|
let name = Telepathy.CLIENT_NAME + '.GnomeShell';
|
|
|
|
DBus.session.exportObject(Telepathy.nameToPath(name), this);
|
|
|
|
DBus.session.acquire_name(name, DBus.SINGLE_INSTANCE,
|
|
|
|
function (name) { /* FIXME: acquired */ },
|
|
|
|
function (name) { /* FIXME: lost */ });
|
|
|
|
|
2010-05-11 17:00:55 +00:00
|
|
|
this._accounts = {};
|
2010-04-16 21:24:34 +00:00
|
|
|
this._sources = {};
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2010-05-11 16:26:00 +00:00
|
|
|
contactManager = new ContactManager();
|
2010-04-16 21:24:34 +00:00
|
|
|
contactManager.connect('presence-changed', Lang.bind(this, this._presenceChanged));
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
channelDispatcher = new Telepathy.ChannelDispatcher(DBus.session,
|
|
|
|
Telepathy.CHANNEL_DISPATCHER_NAME,
|
|
|
|
Telepathy.nameToPath(Telepathy.CHANNEL_DISPATCHER_NAME));
|
|
|
|
|
2010-02-02 15:21:47 +00:00
|
|
|
// Acquire existing connections. (Needed to make things work
|
|
|
|
// through a restart.)
|
|
|
|
let accountManager = new Telepathy.AccountManager(DBus.session,
|
|
|
|
Telepathy.ACCOUNT_MANAGER_NAME,
|
|
|
|
Telepathy.nameToPath(Telepathy.ACCOUNT_MANAGER_NAME));
|
2010-05-11 17:00:55 +00:00
|
|
|
accountManager.GetRemote('ValidAccounts', Lang.bind(this,
|
|
|
|
function (accounts, err) {
|
|
|
|
if (!accounts)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (let i = 0; i < accounts.length; i++)
|
|
|
|
this._gotAccount(accounts[i]);
|
|
|
|
}));
|
|
|
|
accountManager.connect('AccountValidityChanged', Lang.bind(this, this._accountValidityChanged));
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
2010-05-11 17:00:55 +00:00
|
|
|
_accountValidityChanged: function(accountManager, accountPath, valid) {
|
|
|
|
if (!valid) {
|
|
|
|
delete this._accounts[accountPath];
|
|
|
|
// We don't need to clean up connections, sources, etc; they'll
|
|
|
|
// get Closed and cleaned up independently.
|
|
|
|
} else
|
|
|
|
this._gotAccount(accountPath);
|
|
|
|
},
|
|
|
|
|
|
|
|
_gotAccount: function(accountPath) {
|
|
|
|
let account = new Telepathy.Account(DBus.session,
|
|
|
|
Telepathy.ACCOUNT_MANAGER_NAME,
|
|
|
|
accountPath);
|
|
|
|
this._accounts[accountPath] = account;
|
|
|
|
account.GetRemote('Connection', Lang.bind(this,
|
|
|
|
function (connPath, err) {
|
|
|
|
if (!connPath || connPath == '/')
|
|
|
|
return;
|
|
|
|
|
|
|
|
let connReq = new Telepathy.ConnectionRequests(DBus.session,
|
|
|
|
Telepathy.pathToName(connPath),
|
|
|
|
connPath);
|
|
|
|
connReq.GetRemote('Channels', Lang.bind(this,
|
|
|
|
function(channels, err) {
|
|
|
|
if (!channels)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._addChannels(accountPath, connPath, channels);
|
|
|
|
}));
|
2010-04-16 21:24:34 +00:00
|
|
|
|
|
|
|
contactManager.addConnection(connPath);
|
2010-05-11 17:00:55 +00:00
|
|
|
}));
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get Interfaces() {
|
2010-04-08 14:55:22 +00:00
|
|
|
return [ Telepathy.CLIENT_OBSERVER_NAME ];
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
get ObserverChannelFilter() {
|
2010-04-16 19:18:32 +00:00
|
|
|
return [ singleUserTextChannel, oneOrMoreUserTextChannel ];
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
ObserveChannels: function(accountPath, connPath, channels,
|
2010-02-02 15:21:47 +00:00
|
|
|
dispatchOperation, requestsSatisfied,
|
|
|
|
observerInfo) {
|
2010-04-08 14:55:22 +00:00
|
|
|
this._addChannels(accountPath, connPath, channels);
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
_addChannels: function(accountPath, connPath, channelDetailsList) {
|
2010-02-02 15:21:47 +00:00
|
|
|
for (let i = 0; i < channelDetailsList.length; i++) {
|
|
|
|
let [channelPath, props] = channelDetailsList[i];
|
|
|
|
|
2010-04-07 14:50:44 +00:00
|
|
|
// If this is being called from the startup code then it
|
|
|
|
// won't have passed through our filters, so we need to
|
|
|
|
// check the channel/targetHandle type ourselves.
|
|
|
|
|
2010-02-02 15:21:47 +00:00
|
|
|
let channelType = props[Telepathy.CHANNEL_NAME + '.ChannelType'];
|
|
|
|
if (channelType != Telepathy.CHANNEL_TEXT_NAME)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
let targetHandleType = props[Telepathy.CHANNEL_NAME + '.TargetHandleType'];
|
2010-04-07 14:50:44 +00:00
|
|
|
if (targetHandleType != Telepathy.HandleType.CONTACT &&
|
|
|
|
targetHandleType != Telepathy.HandleType.NONE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
let targetHandle = props[Telepathy.CHANNEL_NAME + '.TargetHandle'];
|
2010-02-02 15:21:47 +00:00
|
|
|
let targetId = props[Telepathy.CHANNEL_NAME + '.TargetID'];
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
if (this._sources[connPath + ':' + targetHandle])
|
|
|
|
continue;
|
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
let source = new Source(accountPath, connPath, channelPath,
|
2010-02-02 15:21:47 +00:00
|
|
|
targetHandle, targetHandleType, targetId);
|
2010-04-16 21:24:34 +00:00
|
|
|
this._sources[connPath + ':' + targetHandle] = source;
|
2010-02-02 15:21:47 +00:00
|
|
|
source.connect('destroy', Lang.bind(this,
|
|
|
|
function() {
|
2010-04-16 21:24:34 +00:00
|
|
|
delete this._sources[connPath + ':' + targetHandle];
|
2010-02-02 15:21:47 +00:00
|
|
|
}));
|
|
|
|
}
|
2010-04-16 21:24:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_presenceChanged: function(contactManager, connPath, handle,
|
|
|
|
type, message) {
|
|
|
|
let source = this._sources[connPath + ':' + handle];
|
|
|
|
if (!source)
|
|
|
|
return;
|
|
|
|
|
|
|
|
source.setPresence(type, message);
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
DBus.conformExport(Client.prototype, Telepathy.ClientIface);
|
|
|
|
DBus.conformExport(Client.prototype, Telepathy.ClientObserverIface);
|
|
|
|
|
|
|
|
|
2010-05-11 16:26:00 +00:00
|
|
|
function ContactManager() {
|
2010-02-02 15:21:47 +00:00
|
|
|
this._init();
|
|
|
|
};
|
|
|
|
|
2010-05-11 16:26:00 +00:00
|
|
|
ContactManager.prototype = {
|
2010-02-02 15:21:47 +00:00
|
|
|
_init: function() {
|
|
|
|
this._connections = {};
|
2010-04-14 14:24:14 +00:00
|
|
|
// Note that if we changed this to '/telepathy/avatars' then
|
|
|
|
// we would share cache files with empathy. But since this is
|
|
|
|
// not documented/guaranteed, it seems a little sketchy
|
|
|
|
this._cacheDir = GLib.get_user_cache_dir() + '/gnome-shell/avatars';
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
addConnection: function(connPath) {
|
|
|
|
let info = this._connections[connPath];
|
|
|
|
if (info)
|
|
|
|
return info;
|
|
|
|
|
|
|
|
info = {};
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2010-04-14 14:24:14 +00:00
|
|
|
// Figure out the cache subdirectory for this connection by
|
|
|
|
// parsing the connection manager name (eg, 'gabble') and
|
|
|
|
// protocol name (eg, 'jabber') from the Connection's path.
|
|
|
|
// Telepathy requires the D-Bus path for a connection to have
|
|
|
|
// a specific form, and explicitly says that clients are
|
|
|
|
// allowed to parse it.
|
2010-04-16 21:24:34 +00:00
|
|
|
let match = connPath.match(/\/org\/freedesktop\/Telepathy\/Connection\/([^\/]*\/[^\/]*)\/.*/);
|
2010-04-14 14:24:14 +00:00
|
|
|
if (!match)
|
2010-04-16 21:24:34 +00:00
|
|
|
throw new Error('Could not parse connection path ' + connPath);
|
2010-04-14 14:24:14 +00:00
|
|
|
|
|
|
|
info.cacheDir = this._cacheDir + '/' + match[1];
|
2010-09-30 16:41:20 +00:00
|
|
|
GLib.mkdir_with_parents(info.cacheDir, 0x1c0); // 0x1c0 = octal 0700
|
2010-04-14 14:24:14 +00:00
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
// info.names[handle] is @handle's real name
|
2010-05-11 16:26:00 +00:00
|
|
|
// info.tokens[handle] is the token for @handle's avatar
|
2010-04-16 21:24:34 +00:00
|
|
|
info.names = {};
|
2010-05-11 16:26:00 +00:00
|
|
|
info.tokens = {};
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2010-04-14 14:24:14 +00:00
|
|
|
// info.icons[handle] is an array of the icon actors currently
|
2010-02-02 15:21:47 +00:00
|
|
|
// being displayed for @handle. These will be updated
|
|
|
|
// automatically if @handle's avatar changes.
|
|
|
|
info.icons = {};
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
let connName = Telepathy.pathToName(connPath);
|
|
|
|
|
|
|
|
info.connectionAvatars = new Telepathy.ConnectionAvatars(DBus.session, connName, connPath);
|
2010-02-02 15:21:47 +00:00
|
|
|
info.updatedId = info.connectionAvatars.connect(
|
|
|
|
'AvatarUpdated', Lang.bind(this, this._avatarUpdated));
|
|
|
|
info.retrievedId = info.connectionAvatars.connect(
|
|
|
|
'AvatarRetrieved', Lang.bind(this, this._avatarRetrieved));
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
info.connectionContacts = new Telepathy.ConnectionContacts(DBus.session, connName, connPath);
|
|
|
|
|
|
|
|
info.connectionPresence = new Telepathy.ConnectionSimplePresence(DBus.session, connName, connPath);
|
|
|
|
info.presenceChangedId = info.connectionPresence.connect(
|
|
|
|
'PresencesChanged', Lang.bind(this, this._presencesChanged));
|
|
|
|
|
|
|
|
let conn = new Telepathy.Connection(DBus.session, connName, connPath);
|
2010-02-02 15:21:47 +00:00
|
|
|
info.statusChangedId = conn.connect('StatusChanged', Lang.bind(this,
|
|
|
|
function (status, reason) {
|
|
|
|
if (status == Telepathy.ConnectionStatus.DISCONNECTED)
|
|
|
|
this._removeConnection(conn);
|
|
|
|
}));
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
let connReq = new Telepathy.ConnectionRequests(DBus.session,
|
|
|
|
connName, connPath);
|
|
|
|
connReq.EnsureChannelRemote(subscribedContactsChannel, Lang.bind(this,
|
|
|
|
function (result, err) {
|
|
|
|
if (!result)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let [mine, channelPath, props] = result;
|
|
|
|
this._gotContactsChannel(connPath, channelPath, props);
|
|
|
|
}));
|
|
|
|
|
|
|
|
this._connections[connPath] = info;
|
2010-02-02 15:21:47 +00:00
|
|
|
return info;
|
|
|
|
},
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
_gotContactsChannel: function(connPath, channelPath, props) {
|
|
|
|
let info = this._connections[connPath];
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
|
|
|
info.contactsGroup = new Telepathy.ChannelGroup(DBus.session,
|
|
|
|
Telepathy.pathToName(connPath),
|
|
|
|
channelPath);
|
|
|
|
info.contactsListChangedId =
|
|
|
|
info.contactsGroup.connect('MembersChanged', Lang.bind(this, this._contactsListChanged, info));
|
|
|
|
|
|
|
|
info.contactsGroup.GetRemote('Members', Lang.bind(this,
|
|
|
|
function(contacts, err) {
|
|
|
|
if (!contacts)
|
|
|
|
return;
|
|
|
|
|
|
|
|
info.connectionContacts.GetContactAttributesRemote(
|
|
|
|
contacts, [Telepathy.CONNECTION_ALIASING_NAME], false,
|
|
|
|
Lang.bind(this, this._gotContactAttributes, info));
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_contactsListChanged: function(group, message, added, removed,
|
|
|
|
local_pending, remote_pending,
|
|
|
|
actor, reason, info) {
|
|
|
|
for (let i = 0; i < removed.length; i++)
|
|
|
|
delete info.names[removed[i]];
|
|
|
|
|
|
|
|
info.connectionContacts.GetContactAttributesRemote(
|
|
|
|
added, [Telepathy.CONNECTION_ALIASING_NAME], false,
|
|
|
|
Lang.bind(this, this._gotContactAttributes, info));
|
|
|
|
},
|
|
|
|
|
|
|
|
_gotContactAttributes: function(attrs, err, info) {
|
|
|
|
if (!attrs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (let handle in attrs)
|
|
|
|
info.names[handle] = attrs[handle][Telepathy.CONNECTION_ALIASING_NAME + '/alias'];
|
|
|
|
},
|
|
|
|
|
|
|
|
_presencesChanged: function(conn, presences, err) {
|
|
|
|
if (!presences)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let info = this._connections[conn.getPath()];
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (let handle in presences) {
|
|
|
|
let [type, status, message] = presences[handle];
|
|
|
|
this.emit('presence-changed', conn.getPath(), handle, type, message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-02-02 15:21:47 +00:00
|
|
|
_removeConnection: function(conn) {
|
|
|
|
let info = this._connections[conn.getPath()];
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
|
|
|
conn.disconnect(info.statusChangedId);
|
|
|
|
info.connectionAvatars.disconnect(info.updatedId);
|
|
|
|
info.connectionAvatars.disconnect(info.retrievedId);
|
2010-04-16 21:24:34 +00:00
|
|
|
info.connectionPresence.disconnect(info.presenceChangedId);
|
|
|
|
info.contactsGroup.disconnect(info.contactsListChangedId);
|
2010-02-02 15:21:47 +00:00
|
|
|
|
|
|
|
delete this._connections[conn.getPath()];
|
|
|
|
},
|
|
|
|
|
2010-04-14 14:24:14 +00:00
|
|
|
_getFileForToken: function(info, token) {
|
|
|
|
return info.cacheDir + '/' + Telepathy.escapeAsIdentifier(token);
|
|
|
|
},
|
|
|
|
|
|
|
|
_setIcon: function(iconBox, info, handle) {
|
|
|
|
let textureCache = St.TextureCache.get_default();
|
2010-05-11 16:26:00 +00:00
|
|
|
let token = info.tokens[handle];
|
2010-04-14 14:24:14 +00:00
|
|
|
let file;
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
file = this._getFileForToken(info, token);
|
|
|
|
if (!GLib.file_test(file, GLib.FileTest.EXISTS))
|
|
|
|
file = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
let uri = GLib.filename_to_uri(file, null);
|
|
|
|
iconBox.child = textureCache.load_uri_async(uri, iconBox._size, iconBox._size);
|
|
|
|
} else {
|
2010-11-16 19:08:52 +00:00
|
|
|
iconBox.child = new St.Icon({ icon_name: 'stock_person',
|
|
|
|
icon_type: St.IconType.FULLCOLOR,
|
|
|
|
icon_size: iconBox._size });
|
2010-04-14 14:24:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateIcons: function(info, handle) {
|
|
|
|
if (!info.icons[handle])
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (let i = 0; i < info.icons[handle].length; i++) {
|
|
|
|
let iconBox = info.icons[handle][i];
|
|
|
|
this._setIcon(iconBox, info, handle);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-02-02 15:21:47 +00:00
|
|
|
_avatarUpdated: function(conn, handle, token) {
|
|
|
|
let info = this._connections[conn.getPath()];
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
2010-05-11 16:26:00 +00:00
|
|
|
if (info.tokens[handle] == token)
|
2010-02-02 15:21:47 +00:00
|
|
|
return;
|
|
|
|
|
2010-05-11 16:26:00 +00:00
|
|
|
info.tokens[handle] = token;
|
2010-04-14 14:24:14 +00:00
|
|
|
if (token != '') {
|
|
|
|
let file = this._getFileForToken(info, token);
|
|
|
|
if (!GLib.file_test(file, GLib.FileTest.EXISTS)) {
|
|
|
|
info.connectionAvatars.RequestAvatarsRemote([handle]);
|
|
|
|
return;
|
|
|
|
}
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-14 14:24:14 +00:00
|
|
|
this._updateIcons(info, handle);
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_avatarRetrieved: function(conn, handle, token, avatarData, mimeType) {
|
|
|
|
let info = this._connections[conn.getPath()];
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
2010-04-14 14:24:14 +00:00
|
|
|
let file = this._getFileForToken(info, token);
|
|
|
|
let success = false;
|
|
|
|
try {
|
|
|
|
success = GLib.file_set_contents(file, avatarData, avatarData.length);
|
|
|
|
} catch (e) {
|
|
|
|
logError(e, 'Error caching avatar data');
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
2010-04-14 14:24:14 +00:00
|
|
|
|
|
|
|
if (success)
|
|
|
|
this._updateIcons(info, handle);
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
createAvatar: function(conn, handle, size) {
|
|
|
|
let iconBox = new St.Bin({ style_class: 'avatar-box' });
|
2010-04-14 14:24:14 +00:00
|
|
|
iconBox._size = size;
|
2010-02-02 15:21:47 +00:00
|
|
|
|
|
|
|
let info = this._connections[conn.getPath()];
|
|
|
|
if (!info)
|
2010-05-19 20:52:34 +00:00
|
|
|
info = this.addConnection(conn.getPath());
|
2010-02-02 15:21:47 +00:00
|
|
|
|
|
|
|
if (!info.icons[handle])
|
|
|
|
info.icons[handle] = [];
|
|
|
|
info.icons[handle].push(iconBox);
|
|
|
|
|
|
|
|
iconBox.connect('destroy', Lang.bind(this,
|
|
|
|
function() {
|
|
|
|
let i = info.icons[handle].indexOf(iconBox);
|
|
|
|
if (i != -1)
|
|
|
|
info.icons[handle].splice(i, 1);
|
|
|
|
}));
|
|
|
|
|
2010-04-14 14:24:14 +00:00
|
|
|
// If we already have the icon cached and know its token, this
|
|
|
|
// will fill it in. Otherwise it will fill in the default
|
|
|
|
// icon.
|
|
|
|
this._setIcon(iconBox, info, handle);
|
|
|
|
|
|
|
|
// Asynchronously load the real avatar if we don't have it yet.
|
2010-05-11 16:26:00 +00:00
|
|
|
if (info.tokens[handle] == null) {
|
2010-04-14 14:24:14 +00:00
|
|
|
info.connectionAvatars.GetKnownAvatarTokensRemote([handle], Lang.bind(this,
|
|
|
|
function (tokens, err) {
|
|
|
|
let token = tokens && tokens[handle] ? tokens[handle] : '';
|
|
|
|
this._avatarUpdated(conn, handle, token);
|
|
|
|
}));
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return iconBox;
|
|
|
|
}
|
|
|
|
};
|
2010-04-16 21:24:34 +00:00
|
|
|
Signals.addSignalMethods(ContactManager.prototype);
|
2010-02-02 15:21:47 +00:00
|
|
|
|
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
function Source(accountPath, connPath, channelPath, targetHandle, targetHandleType, targetId) {
|
|
|
|
this._init(accountPath, connPath, channelPath, targetHandle, targetHandleType, targetId);
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Source.prototype = {
|
|
|
|
__proto__: MessageTray.Source.prototype,
|
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
_init: function(accountPath, connPath, channelPath, targetHandle, targetHandleType, targetId) {
|
2010-08-09 17:18:15 +00:00
|
|
|
MessageTray.Source.prototype._init.call(this, targetId);
|
2010-02-02 15:21:47 +00:00
|
|
|
|
2010-04-08 14:55:22 +00:00
|
|
|
this._accountPath = accountPath;
|
|
|
|
|
2010-02-02 15:21:47 +00:00
|
|
|
let connName = Telepathy.pathToName(connPath);
|
|
|
|
this._conn = new Telepathy.Connection(DBus.session, connName, connPath);
|
|
|
|
this._channel = new Telepathy.Channel(DBus.session, connName, channelPath);
|
|
|
|
this._closedId = this._channel.connect('Closed', Lang.bind(this, this._channelClosed));
|
|
|
|
|
|
|
|
this._targetHandle = targetHandle;
|
2010-04-08 14:55:22 +00:00
|
|
|
this._targetHandleType = targetHandleType;
|
2010-02-02 15:21:47 +00:00
|
|
|
this._targetId = targetId;
|
|
|
|
|
|
|
|
if (targetHandleType == Telepathy.HandleType.CONTACT) {
|
|
|
|
let aliasing = new Telepathy.ConnectionAliasing(DBus.session, connName, connPath);
|
|
|
|
aliasing.RequestAliasesRemote([this._targetHandle], Lang.bind(this,
|
|
|
|
function (aliases, err) {
|
|
|
|
if (aliases && aliases.length)
|
2010-06-23 19:20:39 +00:00
|
|
|
this.title = aliases[0];
|
2010-02-02 15:21:47 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
// Since we only create sources when receiving a message, this
|
|
|
|
// is a plausible default
|
|
|
|
this._presence = Telepathy.ConnectionPresenceType.AVAILABLE;
|
|
|
|
|
2010-02-02 15:21:47 +00:00
|
|
|
this._channelText = new Telepathy.ChannelText(DBus.session, connName, channelPath);
|
2010-02-22 19:23:36 +00:00
|
|
|
this._receivedId = this._channelText.connect('Received', Lang.bind(this, this._messageReceived));
|
2010-02-02 15:21:47 +00:00
|
|
|
|
|
|
|
this._channelText.ListPendingMessagesRemote(false, Lang.bind(this, this._gotPendingMessages));
|
2010-08-05 17:09:27 +00:00
|
|
|
|
|
|
|
this._setSummaryIcon(this.createNotificationIcon());
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
2010-08-05 17:09:27 +00:00
|
|
|
createNotificationIcon: function() {
|
|
|
|
return contactManager.createAvatar(this._conn, this._targetHandle,
|
|
|
|
this.ICON_SIZE);
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
MessageTray: untangle click notifications
Previously, when you clicked on a notification, it would call
this.source.clicked(), which would emit a 'clicked' signal on the
source, and then various other stuff would happen from there. This
used to make a little bit of sense, when clicking on a notification
was supposed to do the same thing as clicking on its source, but makes
less sense now, when clicking on the source itself *doesn't* call
source.clicked()...
Change it so that when you click on a notification, the notification
emits 'clicked' itself, and the source notices that and calls its
notificationClicked() method, and the various source subclasses do
what they need to do with that, and Source no longer has a clicked
method/signal.
https://bugzilla.gnome.org/show_bug.cgi?id=631042
2010-09-30 20:41:38 +00:00
|
|
|
_notificationClicked: function(notification) {
|
2010-04-08 14:55:22 +00:00
|
|
|
channelDispatcher.EnsureChannelRemote(this._accountPath,
|
|
|
|
{ 'org.freedesktop.Telepathy.Channel.ChannelType': Telepathy.CHANNEL_TEXT_NAME,
|
|
|
|
'org.freedesktop.Telepathy.Channel.TargetHandle': this._targetHandle,
|
|
|
|
'org.freedesktop.Telepathy.Channel.TargetHandleType': this._targetHandleType },
|
|
|
|
global.get_current_time(),
|
|
|
|
'',
|
|
|
|
Lang.bind(this, this._gotChannelRequest));
|
|
|
|
},
|
|
|
|
|
|
|
|
_gotChannelRequest: function (chanReqPath, ex) {
|
|
|
|
if (ex) {
|
|
|
|
log ('EnsureChannelRemote failed? ' + ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let chanReq = new Telepathy.ChannelRequest(DBus.session, Telepathy.CHANNEL_DISPATCHER_NAME, chanReqPath);
|
|
|
|
chanReq.ProceedRemote();
|
|
|
|
},
|
|
|
|
|
2010-02-02 15:21:47 +00:00
|
|
|
_gotPendingMessages: function(msgs, err) {
|
|
|
|
if (!msgs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (let i = 0; i < msgs.length; i++)
|
2010-02-22 19:23:36 +00:00
|
|
|
this._messageReceived.apply(this, [this._channel].concat(msgs[i]));
|
2010-02-02 15:21:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_channelClosed: function() {
|
|
|
|
this._channel.disconnect(this._closedId);
|
|
|
|
this._channelText.disconnect(this._receivedId);
|
|
|
|
this.destroy();
|
|
|
|
},
|
|
|
|
|
2010-04-16 21:24:34 +00:00
|
|
|
_ensureNotification: function() {
|
2010-02-02 15:21:47 +00:00
|
|
|
if (!Main.messageTray.contains(this))
|
|
|
|
Main.messageTray.add(this);
|
|
|
|
|
2010-02-22 19:23:36 +00:00
|
|
|
if (!this._notification)
|
2010-08-11 13:43:40 +00:00
|
|
|
this._notification = new Notification(this);
|
2010-04-16 21:24:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_messageReceived: function(channel, id, timestamp, sender,
|
|
|
|
type, flags, text) {
|
|
|
|
this._ensureNotification();
|
2010-11-25 14:29:41 +00:00
|
|
|
this._notification.appendMessage(text, timestamp);
|
2010-02-22 19:23:36 +00:00
|
|
|
this.notify(this._notification);
|
|
|
|
},
|
|
|
|
|
|
|
|
respond: function(text) {
|
|
|
|
this._channelText.SendRemote(Telepathy.ChannelTextMessageType.NORMAL, text);
|
2010-04-16 21:24:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setPresence: function(presence, message) {
|
|
|
|
let msg, notify;
|
|
|
|
|
|
|
|
if (presence == Telepathy.ConnectionPresenceType.AVAILABLE) {
|
2010-07-03 22:28:32 +00:00
|
|
|
msg = _("%s is online.").format(this.title);
|
2010-04-16 21:24:34 +00:00
|
|
|
notify = (this._presence == Telepathy.ConnectionPresenceType.OFFLINE);
|
|
|
|
} else if (presence == Telepathy.ConnectionPresenceType.OFFLINE ||
|
|
|
|
presence == Telepathy.ConnectionPresenceType.EXTENDED_AWAY) {
|
|
|
|
presence = Telepathy.ConnectionPresenceType.OFFLINE;
|
2010-07-03 22:28:32 +00:00
|
|
|
msg = _("%s is offline.").format(this.title);
|
2010-04-16 21:24:34 +00:00
|
|
|
notify = (this._presence != Telepathy.ConnectionPresenceType.OFFLINE);
|
|
|
|
} else if (presence == Telepathy.ConnectionPresenceType.AWAY) {
|
2010-07-03 22:28:32 +00:00
|
|
|
msg = _("%s is away.").format(this.title);
|
2010-04-16 21:24:34 +00:00
|
|
|
notify = false;
|
|
|
|
} else if (presence == Telepathy.ConnectionPresenceType.BUSY) {
|
2010-07-03 22:28:32 +00:00
|
|
|
msg = _("%s is busy.").format(this.title);
|
2010-04-16 21:24:34 +00:00
|
|
|
notify = false;
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._presence = presence;
|
|
|
|
|
|
|
|
if (message)
|
|
|
|
msg += ' <i>(' + GLib.markup_escape_text(message, -1) + ')</i>';
|
|
|
|
|
|
|
|
this._ensureNotification();
|
2010-11-25 14:29:41 +00:00
|
|
|
this._notification.appendPresence(msg, notify);
|
2010-04-16 21:24:34 +00:00
|
|
|
if (notify)
|
|
|
|
this.notify(this._notification);
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-11 13:43:40 +00:00
|
|
|
function Notification(source) {
|
|
|
|
this._init(source);
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Notification.prototype = {
|
|
|
|
__proto__: MessageTray.Notification.prototype,
|
|
|
|
|
2010-08-11 13:43:40 +00:00
|
|
|
_init: function(source) {
|
2010-08-30 20:03:08 +00:00
|
|
|
MessageTray.Notification.prototype._init.call(this, source, source.title, null, { customContent: true });
|
2010-02-22 19:23:36 +00:00
|
|
|
|
|
|
|
this._responseEntry = new St.Entry({ style_class: 'chat-response' });
|
|
|
|
this._responseEntry.clutter_text.connect('activate', Lang.bind(this, this._onEntryActivated));
|
|
|
|
this.setActionArea(this._responseEntry);
|
|
|
|
|
|
|
|
this._history = [];
|
2010-11-25 14:29:41 +00:00
|
|
|
this._timestampTimeoutId = 0;
|
2010-02-22 19:23:36 +00:00
|
|
|
},
|
|
|
|
|
2010-11-25 14:29:41 +00:00
|
|
|
appendMessage: function(text, timestamp) {
|
|
|
|
this.update(this.source.title, text, { customContent: true });
|
|
|
|
this._append(text, 'chat-received', timestamp);
|
2010-02-22 19:23:36 +00:00
|
|
|
},
|
|
|
|
|
2010-11-25 14:29:41 +00:00
|
|
|
_append: function(text, style, timestamp) {
|
|
|
|
let currentTime = (Date.now() / 1000);
|
|
|
|
if (!timestamp)
|
|
|
|
timestamp = currentTime;
|
|
|
|
let lastMessageTime = -1;
|
|
|
|
if (this._history.length > 0)
|
|
|
|
lastMessageTime = this._history[0].time;
|
|
|
|
|
|
|
|
// Reset the old message timeout
|
|
|
|
if (this._timestampTimeoutId)
|
|
|
|
Mainloop.source_remove(this._timestampTimeoutId);
|
|
|
|
|
2010-02-22 19:23:36 +00:00
|
|
|
let body = this.addBody(text);
|
|
|
|
body.add_style_class_name(style);
|
|
|
|
this.scrollTo(St.Side.BOTTOM);
|
|
|
|
|
2010-11-25 14:29:41 +00:00
|
|
|
this._history.unshift({ actor: body, time: timestamp, realMessage: true });
|
|
|
|
|
|
|
|
if (timestamp < currentTime - SCROLLBACK_IMMEDIATE_TIME)
|
|
|
|
this._appendTimestamp();
|
|
|
|
else
|
|
|
|
// Schedule a new timestamp in SCROLLBACK_IMMEDIATE_TIME
|
|
|
|
// from the timestamp of the message.
|
|
|
|
this._timestampTimeoutId = Mainloop.timeout_add_seconds(
|
|
|
|
SCROLLBACK_IMMEDIATE_TIME - (currentTime - timestamp),
|
|
|
|
Lang.bind(this, this._appendTimestamp));
|
2010-02-22 19:23:36 +00:00
|
|
|
|
|
|
|
if (this._history.length > 1) {
|
|
|
|
// Keep the scrollback from growing too long. If the most
|
|
|
|
// recent message (before the one we just added) is within
|
|
|
|
// SCROLLBACK_RECENT_TIME, we will keep
|
|
|
|
// SCROLLBACK_RECENT_LENGTH previous messages. Otherwise
|
|
|
|
// we'll keep SCROLLBACK_IDLE_LENGTH messages.
|
|
|
|
|
2010-11-25 14:29:41 +00:00
|
|
|
let maxLength = (lastMessageTime < currentTime - SCROLLBACK_RECENT_TIME) ?
|
2010-02-22 19:23:36 +00:00
|
|
|
SCROLLBACK_IDLE_LENGTH : SCROLLBACK_RECENT_LENGTH;
|
2010-11-25 14:29:41 +00:00
|
|
|
let filteredHistory = this._history.filter(function(item) { return item.realMessage });
|
|
|
|
if (filteredHistory.length > maxLength) {
|
|
|
|
let lastMessageToKeep = filteredHistory[maxLength];
|
|
|
|
let expired = this._history.splice(this._history.indexOf(lastMessageToKeep));
|
2010-02-22 19:23:36 +00:00
|
|
|
for (let i = 0; i < expired.length; i++)
|
|
|
|
expired[i].actor.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-11-25 14:29:41 +00:00
|
|
|
_appendTimestamp: function() {
|
|
|
|
let lastMessageTime = this._history[0].time;
|
|
|
|
let lastMessageDate = new Date(lastMessageTime * 1000);
|
|
|
|
|
|
|
|
/* Translators: this is a time format string followed by a date.
|
|
|
|
If applicable, replace %X with a strftime format valid for your
|
|
|
|
locale, without seconds. */
|
|
|
|
let timeLabel = this.addBody(lastMessageDate.toLocaleFormat(_("Sent at %X on %A")), false, { expand: true, x_fill: false, x_align: St.Align.END });
|
|
|
|
timeLabel.add_style_class_name('chat-meta-message');
|
|
|
|
this._history.unshift({ actor: timeLabel, time: lastMessageTime, realMessage: false });
|
|
|
|
|
|
|
|
this._timestampTimeoutId = 0;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
appendPresence: function(text, asTitle) {
|
|
|
|
if (asTitle)
|
|
|
|
this.update(text, null, { customContent: true });
|
|
|
|
else
|
|
|
|
this.update(this.source.title, null, { customContent: true });
|
|
|
|
let label = this.addBody(text);
|
|
|
|
label.add_style_class_name('chat-meta-message');
|
|
|
|
this._history.unshift({ actor: label, time: (Date.now() / 1000), realMessage: false});
|
|
|
|
},
|
|
|
|
|
2010-07-21 05:19:25 +00:00
|
|
|
grabFocus: function(lockTray) {
|
2010-07-21 04:42:37 +00:00
|
|
|
// Need to call the base class function first so that
|
|
|
|
// it saves where the key focus was before.
|
2010-07-21 05:19:25 +00:00
|
|
|
MessageTray.Notification.prototype.grabFocus.call(this, lockTray);
|
2010-07-21 04:42:37 +00:00
|
|
|
global.stage.set_key_focus(this._responseEntry.clutter_text);
|
2010-02-22 19:23:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onEntryActivated: function() {
|
|
|
|
let text = this._responseEntry.get_text();
|
2010-07-21 04:42:37 +00:00
|
|
|
if (text == '')
|
2010-02-22 19:23:36 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
this._responseEntry.set_text('');
|
|
|
|
this._append(text, 'chat-sent');
|
|
|
|
this.source.respond(text);
|
2010-02-02 15:21:47 +00:00
|
|
|
}
|
|
|
|
};
|