-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.js
361 lines (301 loc) · 10.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Title Session Sync
* Programmer Gabriel Ivanica
* Email gabriel.ivanica@gmail.com
* Description
*/
// TODO track new oppened tabs and update view if is neccesary
// TODO track tab oppening/closing/changing(url) and restrict UI update when oppened
'use strict';
// *****************************************************************************
// SDK Modules
const { Cc, Ci, Cr } = require('chrome'); // Cc, Ci, Cm, Cr, Cu, components
const { data } = require('sdk/self');
const { Hotkey } = require('sdk/hotkeys');
const { storage: simpleStorage } = require('sdk/simple-storage');
const tabs = require('sdk/tabs');
const windowUtils = require('sdk/window/utils');
const { browserWindows } = require("sdk/windows");
const { modelFor } = require("sdk/model/core");
const { viewFor } = require("sdk/view/core");
const { ActionButton } = require('sdk/ui/button/action');
// *****************************************************************************
// Custom Modules
const { SessionSyncUI } = require('session-sync-ui');
const { Bookmarks, HistoryService } = require('./utils/bookmarks');
const { GlobalEvents } = require('./utils/global-events');
// *****************************************************************************
// *****************************************************************************
// Main Addon Object
var query = HistoryService.getNewQuery();
var queryOptions = HistoryService.getNewQueryOptions();
function SessionInfo(session) {
this.id = session.itemId;
this.title = session.title;
this.position = session.bookmarkIndex;
this.date = session.dateAdded / 1000;
this.modified = session.lastModified / 1000;
}
const SessionSync = {
// Bookmarks storage
sessions : [],
AquaUIs : null,
storageFolderID : undefined,
// Query options and properties
SsFolderQuery : null,
////////////////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////////////////
// Set the query container
setSessionsQuery : function setSessionsQuery(folderID) {
this.SsFolderQuery = HistoryService.getNewQuery();
this.SsFolderQuery.setFolders([folderID], 1);
},
// Get saved sessions
retrieveSessions : function retrieveSessions() {
this.sessions = [];
var childNode;
var result = HistoryService.executeQuery(this.SsFolderQuery, queryOptions);
var container = result.root;
container.containerOpen = true;
// lock container
var child_count = container.childCount;
for (var i = 0; i < child_count; i++) {
childNode = container.getChild(i);
if (childNode.type === childNode.RESULT_TYPE_FOLDER) {
this.sessions.push(new SessionInfo(childNode));
}
}
// unlock container
container.containerOpen = false;
},
// *************************************************************************
// Validate Storage Folder
getStorageFolderID : function () {
var valid = false;
var index = this.findStorageFolder() | 0;
if (index === 0) {
index = Bookmarks.createFolder(Bookmarks.MENU_FOLDER, 'SessionSync');
Bookmarks.setItemDescription(index, 'session-sync');
}
return index;
},
isValidFolderAnnotation: function isValidFolderAnnotation(folderID) {
var annotation = Bookmarks.getItemDescription(folderID);
if (annotation === 'session-sync')
return true;
return false;
},
// Returns FolderID where sessions are stored
findStorageFolder : function findStorageFolder() {
var childNode;
var query = HistoryService.getNewQuery();
query.setFolders([Bookmarks.MENU_FOLDER], 1);
var result = HistoryService.executeQuery(query, queryOptions);
var container = result.root;
container.containerOpen = true;
// lock container
var child_count = container.childCount;
for (var i = 0; i < child_count; i++) {
childNode = container.getChild(i);
if (childNode.type === childNode.RESULT_TYPE_FOLDER) {
if (this.isValidFolderAnnotation(childNode.itemId)) {
container.containerOpen = false;
return childNode.itemId;
}
}
}
// unlock container
container.containerOpen = false;
return null;
},
toggleUI : function toggleUI(pinned) {
SessionSync.AquaUIs.get(browserWindows.activeWindow).toggle(pinned);
},
updateSessions : function updateSessions() {
this.retrieveSessions();
for (let UI of this.AquaUIs.values()) {
UI.setSessions(this.sessions);
}
},
removeSessionFolder : function removeSessionFolder(folderID) {
var sessions = this.sessions;
for (var i in sessions) {
if (sessions[i].id === folderID) {
sessions.splice(i, 1);
break;
}
}
for (let UI of this.AquaUIs.values()) {
UI.setSessions(sessions);
}
},
// *************************************************************************
// TODO sort by createdDate and modify that date when update
// *************************************************************************
// Attach/detach SessionSync XUL Panel on windows
init_XUL_Attachments : function () {
var updateAllSessions = SessionSync.updateSessions.bind(SessionSync);
GlobalEvents.on('lock-observer', Observer.lock);
GlobalEvents.on('unlock-observer', Observer.unlock);
GlobalEvents.on('update-sessions', updateAllSessions);
var trackWindow = function trackWindow(window) {
// Creates the UI for the newly created window
var UI = new SessionSyncUI(viewFor(window));
UI.setStorageFolderID(SessionSync.storageFolderID);
UI.setSessions(SessionSync.sessions);
SessionSync.AquaUIs.set(window, UI);
};
var untrackWindow = function untrackWindow(window) {
// Destroy UI
if (!SessionSync.destroyed) {
var UI = SessionSync.AquaUIs.get(window);
if (UI) UI.destroy();
SessionSync.AquaUIs.delete(window);
}
};
browserWindows.on('open', trackWindow);
browserWindows.on('close', untrackWindow);
browserWindows.on('activate', function (window) {
SessionSync.activeWindow = window;
});
for each (var window in browserWindows) {
trackWindow(window);
}
SessionSync.activeWindow = browserWindows.activeWindow;
},
init : function init() {
this.SSyncActionButton = ActionButton({
id: "syncbtn",
label: "Session Sync",
icon: {
"16": data.url("images/icon16.png"),
"32": data.url("images/icon32.png")
},
onClick: function(state) {
SessionSync.toggleUI(state);
}
});
this.KeyBind = Hotkey({
combo: 'accel-shift-s',
onPress: SessionSync.toggleUI.bind(SessionSync, false)
});
this.AquaUIs = new Map();
this.sessions = [];
// Register the Observer with the Bookmarks service
Bookmarks.addObserver(Observer, false);
this.storageFolderID = this.getStorageFolderID();
this.setSessionsQuery(this.storageFolderID);
this.retrieveSessions();
this.init_XUL_Attachments();
},
unload : function unload() {
// Remove the Bookrmarks Observer
Bookmarks.removeObserver(Observer);
GlobalEvents.off('lock-observer', Observer.lock);
GlobalEvents.off('unlock-observer', Observer.unlock);
this.SSyncActionButton.destroy();
this.KeyBind.destroy();
for (let UI of this.AquaUIs.values()) {
UI.destroy();
}
this.destroyed = true;
this.AquaUIs = null;
this.sessions = null;
this.storageFolderID = 0;
}
};
// *****************************************************************************
// Bookmarks Observer
var Observer = (function (){
var isLocked = false;
var update = false;
var changed_property = ['title', Bookmarks.BOOKMARK_DESCRIPTION];
var lock = function lock() {
isLocked = true;
};
var unlock = function unlock() {
isLocked = false;
};
var onBeginUpdateBatch = function onBeginUpdateBatch() {
};
var onEndUpdateBatch = function onEndUpdateBatch() {
if (isLocked) return;
if (update === true) {
update = false;
SessionSync.updateSessions();
}
};
var onItemAdded = function(id, folder, index) {
// console.log('Item Added', 'id', id, 'folder', folder, 'index', index);
if (isLocked) return;
if (folder === SessionSync.storageFolderID) {
update = true;
}
};
var onItemRemoved = function(id, folder, index) {
// console.log('Item Removed', 'id', id, 'folder', folder, 'index', index);
if (isLocked) return;
if (folder === SessionSync.storageFolderID) {
SessionSync.removeSessionFolder(id);
}
};
var onItemChanged = function(id, property, isAnnotationProperty, value) {
// console.log('Item Changed', 'id', id, 'property', property, 'value', value);
if (isLocked) return;
if (changed_property.indexOf(property) !== -1) {
var sessions = SessionSync.sessions;
for (var i in sessions) {
if (sessions[i].id === id) {
SessionSync.updateSessions();
break;
}
}
}
};
// onItemVisited: function(id, visitID, time) {
// The visit id can be used with the History service to access other properties of the visit.
// The time is the time at which the visit occurred, in microseconds.
// },
var onItemMoved = function(id, oldParent, oldIndex, newParent, newIndex) {
// console.log('Item Moved', 'id', id, 'oldParent', oldParent, 'oldIndex', oldIndex,
// 'newParent', newParent, 'newIndex', newIndex);
if (isLocked) return;
if (oldParent === SessionSync.storageFolderID ||
newParent === SessionSync.storageFolderID) {
update = true;
}
};
return {
lock : lock,
unlock : unlock,
onBeginUpdateBatch : onBeginUpdateBatch,
onEndUpdateBatch : onEndUpdateBatch,
onItemAdded : onItemAdded,
onItemRemoved : onItemRemoved,
onItemChanged : onItemChanged,
onItemMoved : onItemMoved,
QueryInterface: function(iid) {
if (iid.equals(Ci.nsINavBookmarkObserver) || iid.equals(Ci.nsISupports)) {
return this;
}
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
})();
// *****************************************************************************
// Load Addon
exports.main = function (options, callbacks) {
// console.log(options.loadReason);
var reasons = ['install', 'enable', 'upgrade'];
// Init Addon
SessionSync.init();
};
// *****************************************************************************
// Unload Addon
exports.onUnload = function (reason) {
SessionSync.unload();
};