2015-01-19 01:43:25 +00:00
|
|
|
/*global $, Whisper, Backbone, textsecure, extension*/
|
|
|
|
/* vim: ts=4:sw=4:expandtab:
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-01-22 04:27:42 +00:00
|
|
|
// This script should only be included in background.html
|
|
|
|
// Whisper.windowMap is defined in background.js
|
2015-01-19 01:43:25 +00:00
|
|
|
(function () {
|
2015-02-11 10:47:50 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
|
|
|
var windowMap = new Whisper.Bimap('windowId', 'modelId');
|
|
|
|
var conversations = new Whisper.ConversationCollection();
|
|
|
|
|
2015-05-26 20:28:43 +00:00
|
|
|
window.inbox = new Whisper.ConversationCollection([], {
|
|
|
|
comparator: function(model) {
|
|
|
|
return -model.get('active_at');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
inbox.on('change:active_at', inbox.sort);
|
|
|
|
inbox.on('change:unreadCount', function(model, count) {
|
|
|
|
var prev = model.previous('unreadCount') || 0;
|
|
|
|
if (count < prev) { // decreased
|
|
|
|
var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
|
|
|
|
setUnreadCount(newUnreadCount);
|
|
|
|
storage.put("unreadCount", newUnreadCount);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-15 21:08:58 +00:00
|
|
|
window.updateInbox = function() {
|
2015-05-26 20:28:43 +00:00
|
|
|
conversations.fetchActive().then(function() {
|
|
|
|
inbox.reset(conversations.filter(function(model) {
|
|
|
|
return model.get('active_at');
|
|
|
|
}));
|
|
|
|
});
|
2015-07-15 21:08:58 +00:00
|
|
|
};
|
2015-05-26 20:28:43 +00:00
|
|
|
|
|
|
|
updateInbox();
|
|
|
|
setUnreadCount(storage.get("unreadCount", 0));
|
|
|
|
|
|
|
|
function setUnreadCount(count) {
|
|
|
|
if (count > 0) {
|
|
|
|
extension.navigator.setBadgeText(count);
|
|
|
|
} else {
|
|
|
|
extension.navigator.setBadgeText("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 10:47:50 +00:00
|
|
|
window.getConversationForWindow = function(windowId) {
|
|
|
|
return conversations.get(windowMap.modelIdFrom(windowId));
|
|
|
|
};
|
|
|
|
|
2015-02-19 07:56:13 +00:00
|
|
|
window.updateConversation = function(conversationId) {
|
2015-02-19 08:20:22 +00:00
|
|
|
var conversation = conversations.get(conversationId);
|
2015-02-19 07:56:13 +00:00
|
|
|
if (conversation) {
|
|
|
|
conversation.fetch();
|
|
|
|
conversation.fetchMessages();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-11 10:47:50 +00:00
|
|
|
function closeConversation (windowId) {
|
|
|
|
windowMap.remove('windowId', windowId);
|
2015-02-19 08:20:22 +00:00
|
|
|
}
|
2015-02-11 10:47:50 +00:00
|
|
|
|
2015-03-18 23:26:55 +00:00
|
|
|
window.getConversation = function(attrs) {
|
|
|
|
var conversation = window.inbox.get(attrs.id) || attrs;
|
2015-03-23 19:13:10 +00:00
|
|
|
conversation = conversations.add(conversation);
|
2015-03-17 22:06:21 +00:00
|
|
|
return conversation;
|
2015-03-18 23:26:55 +00:00
|
|
|
};
|
2015-03-17 22:06:21 +00:00
|
|
|
|
|
|
|
window.notifyConversation = function(message) {
|
2015-05-24 23:19:33 +00:00
|
|
|
var conversationId = message.get('conversationId');
|
|
|
|
var windowId = windowMap.windowIdFrom(conversationId);
|
|
|
|
if (windowId) {
|
|
|
|
// already open
|
|
|
|
updateConversation(conversationId);
|
|
|
|
extension.windows.drawAttention(windowId);
|
|
|
|
} else if (Whisper.Notifications.isEnabled()) {
|
2015-03-18 23:26:55 +00:00
|
|
|
var conversation = getConversation({id: message.get('conversationId')});
|
2015-03-19 23:17:26 +00:00
|
|
|
var sender = getConversation({id: message.get('source')});
|
2015-03-17 22:06:21 +00:00
|
|
|
conversation.fetch().then(function() {
|
2015-03-19 23:17:26 +00:00
|
|
|
sender.fetch().then(function() {
|
|
|
|
var notification = new Notification(sender.getTitle(), {
|
|
|
|
body: message.getDescription(),
|
2015-07-06 19:58:24 +00:00
|
|
|
icon: sender.getAvatar().url,
|
2015-03-19 23:17:26 +00:00
|
|
|
tag: conversation.id
|
|
|
|
});
|
|
|
|
notification.onclick = function() {
|
|
|
|
openConversation(conversation.id);
|
|
|
|
};
|
2015-03-17 22:06:21 +00:00
|
|
|
});
|
|
|
|
});
|
2015-03-18 23:26:55 +00:00
|
|
|
conversation.fetchMessages();
|
2015-03-17 22:06:21 +00:00
|
|
|
} else {
|
2015-05-20 22:50:03 +00:00
|
|
|
openConversation(conversationId);
|
|
|
|
extension.windows.drawAttention(windowId);
|
2015-03-17 22:06:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.openConversation = function openConversation (modelId) {
|
2015-03-18 23:26:55 +00:00
|
|
|
var conversation = getConversation({id: modelId});
|
2015-02-25 00:02:33 +00:00
|
|
|
conversation.fetch().then(function() {
|
|
|
|
conversation.fetchContacts();
|
|
|
|
});
|
2015-02-12 07:23:37 +00:00
|
|
|
conversation.fetchMessages();
|
2015-02-11 10:47:50 +00:00
|
|
|
|
|
|
|
var windowId = windowMap.windowIdFrom(modelId);
|
|
|
|
|
|
|
|
// prevent multiple copies of the same conversation from being opened
|
|
|
|
if (!windowId) {
|
|
|
|
// open the panel
|
|
|
|
extension.windows.open({
|
2015-05-12 21:01:45 +00:00
|
|
|
id: modelId,
|
2015-02-11 10:47:50 +00:00
|
|
|
url: 'conversation.html',
|
|
|
|
type: 'panel',
|
2015-05-21 21:35:44 +00:00
|
|
|
frame: 'none',
|
2015-02-11 10:47:50 +00:00
|
|
|
focused: true,
|
2015-03-04 21:59:40 +00:00
|
|
|
width: 300,
|
2015-07-29 18:23:56 +00:00
|
|
|
height: 440,
|
|
|
|
minWidth: 230,
|
|
|
|
minHeight: 73
|
2015-02-11 10:47:50 +00:00
|
|
|
}, function (windowInfo) {
|
2015-07-01 20:28:06 +00:00
|
|
|
windowId = windowInfo.id;
|
|
|
|
windowMap.add({ windowId: windowId, modelId: modelId });
|
|
|
|
|
|
|
|
windowInfo.onClosed.addListener(function () {
|
|
|
|
onWindowClosed(windowId);
|
|
|
|
});
|
2015-02-11 10:47:50 +00:00
|
|
|
|
|
|
|
// close the panel if background.html is refreshed
|
2015-05-13 18:23:59 +00:00
|
|
|
extension.windows.beforeUnload(function() {
|
2015-02-11 10:47:50 +00:00
|
|
|
// TODO: reattach after reload instead of closing.
|
2015-07-01 20:28:06 +00:00
|
|
|
extension.windows.remove(windowId);
|
2015-02-11 10:47:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// focus the panel
|
2015-05-12 21:01:45 +00:00
|
|
|
extension.windows.focus(windowId, function (error) {
|
|
|
|
if (error) {
|
2015-02-11 10:47:50 +00:00
|
|
|
closeConversation(windowId); // panel isn't actually open...
|
|
|
|
openConversation(modelId); // ...and so we try again.
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inbox window controller */
|
|
|
|
var inboxOpened = false;
|
2015-05-23 00:10:01 +00:00
|
|
|
var inboxWindowId = 'inbox';
|
2015-05-12 21:01:45 +00:00
|
|
|
window.openInbox = function() {
|
2015-02-11 10:47:50 +00:00
|
|
|
if (inboxOpened === false) {
|
|
|
|
inboxOpened = true;
|
|
|
|
extension.windows.open({
|
2015-05-12 21:01:45 +00:00
|
|
|
id: 'inbox',
|
2015-02-11 10:47:50 +00:00
|
|
|
url: 'index.html',
|
|
|
|
type: 'panel',
|
2015-05-21 20:05:52 +00:00
|
|
|
frame: 'none',
|
2015-02-11 10:47:50 +00:00
|
|
|
focused: true,
|
2015-07-29 18:23:56 +00:00
|
|
|
width: 260,
|
|
|
|
height: 440,
|
|
|
|
minWidth: 230,
|
|
|
|
minHeight: 150
|
2015-02-11 10:47:50 +00:00
|
|
|
}, function (windowInfo) {
|
|
|
|
inboxWindowId = windowInfo.id;
|
2015-03-12 18:23:41 +00:00
|
|
|
|
2015-07-01 20:28:06 +00:00
|
|
|
windowInfo.onClosed.addListener(function () {
|
|
|
|
onWindowClosed(inboxWindowId);
|
|
|
|
});
|
|
|
|
|
2015-03-12 18:23:41 +00:00
|
|
|
// close the panel if background.html is refreshed
|
2015-05-13 18:23:59 +00:00
|
|
|
extension.windows.beforeUnload(function() {
|
2015-03-12 18:23:41 +00:00
|
|
|
// TODO: reattach after reload instead of closing.
|
2015-07-01 20:28:06 +00:00
|
|
|
extension.windows.remove(inboxWindowId);
|
2015-03-12 18:23:41 +00:00
|
|
|
});
|
2015-02-11 10:47:50 +00:00
|
|
|
});
|
|
|
|
} else if (inboxOpened === true) {
|
2015-05-12 21:01:45 +00:00
|
|
|
extension.windows.focus(inboxWindowId, function (error) {
|
|
|
|
if (error) {
|
|
|
|
inboxOpened = false;
|
|
|
|
openInbox();
|
|
|
|
}
|
|
|
|
});
|
2015-01-22 04:27:42 +00:00
|
|
|
}
|
2015-05-12 21:01:45 +00:00
|
|
|
};
|
2015-05-15 20:54:29 +00:00
|
|
|
|
|
|
|
extension.onLaunched(function() {
|
2015-05-20 21:24:44 +00:00
|
|
|
storage.onready(function() {
|
|
|
|
if (textsecure.registration.isDone()) {
|
|
|
|
openInbox();
|
|
|
|
} else {
|
|
|
|
extension.install();
|
|
|
|
}
|
|
|
|
});
|
2015-05-15 20:54:29 +00:00
|
|
|
});
|
2015-01-19 01:43:25 +00:00
|
|
|
|
2015-02-11 10:47:50 +00:00
|
|
|
// make sure windows are cleaned up on close
|
2015-07-01 20:28:06 +00:00
|
|
|
var onWindowClosed = function (windowId) {
|
2015-02-11 10:47:50 +00:00
|
|
|
if (windowMap.windowId[windowId]) {
|
|
|
|
closeConversation(windowId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (windowId === inboxWindowId) {
|
|
|
|
inboxWindowId = 0;
|
|
|
|
inboxOpened = false;
|
|
|
|
}
|
2015-07-01 20:28:06 +00:00
|
|
|
};
|
2015-01-22 04:27:42 +00:00
|
|
|
})();
|