Author: smaisonneuve Date: 2015-05-22 13:48:48 +0000 (Fri, 22 May 2015) New Revision: 1433 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1433 Log: [EsLint] Refactoring UserActivityService and fixing Timeline component Modified: wit/js/components/Timeline.js wit/js/services/UserActivityService.js Modified: wit/js/components/Timeline.js =================================================================== --- wit/js/components/Timeline.js 2015-05-22 13:43:31 UTC (rev 1432) +++ wit/js/components/Timeline.js 2015-05-22 13:48:48 UTC (rev 1433) @@ -22,7 +22,7 @@ } if (startDate && endDate) { - var setState = this.setState; + var setState = this.setState.bind(this); user.getSessions(startDate, endDate, true) .then(function(sessions) { Modified: wit/js/services/UserActivityService.js =================================================================== --- wit/js/services/UserActivityService.js 2015-05-22 13:43:31 UTC (rev 1432) +++ wit/js/services/UserActivityService.js 2015-05-22 13:48:48 UTC (rev 1433) @@ -1,3 +1,5 @@ +"use strict"; + var x11 = require('x11'); var xprop = require('xprop'); var moment = require('moment'); @@ -11,9 +13,195 @@ var activityChangeTimer; var disableActivityDetection = false; -const _NET_ACTIVE_WINDOW = 334; +/////////////// +// Utilities // +/////////////// + +var onActiveWindowChange = function(xClient, ev) { + if (ev.name === "PropertyNotify") { + + xClient.GetAtomName(ev.atom, function(err, name) { + + // if the property change refers to the currently active window, retrieve new active window name + + if (name === "_NET_ACTIVE_WINDOW") { + + registerWindowEvents(xClient, ev); + addWindowSession(xClient, ev); + + } else if (name === "_NET_WM_NAME") { + + updateWindowSession(ev); + } + }); + } +}; + +var getActiveWindowId = function(xClient, ev) { + return new Promise(function(resolve, reject) { + + if (!ev.wid) { + + reject(); + + } else { + + xClient.GetProperty(0, ev.wid, ev.atom, xClient.atoms.WINDOW, 0, 4, function(err, prop) { + if (err || !prop.data.length) { + console.log("Couldn't retrieve active window property"); + reject(); + + } else { + var wid = prop.data.readUInt32LE(0); + + if (wid) { + resolve(wid); + + } else { + console.log("Couldn't retrieve active window property"); + reject(); + } + } + }); + } + + }); +}; + +var getWindowName = function(windowId) { + return new Promise(function(resolve, reject) { + + if (!windowId) { + + reject(); + + } else { + + xprop({prop:'_NET_WM_NAME', id:windowId}, function(err, properties) { + + if (err) { + console.log("Couldn't retrieve window name for id :", windowId); + reject(); + + } else { + resolve(properties.length && properties[0].value); + } + }); + } + }); +}; + +var registerWindowEvents = function(xClient, ev) { + getActiveWindowId(xClient, ev) + .then(function(wid) { + xClient.ChangeWindowAttributes(wid, { + eventMask: x11.eventMask.PropertyChange + }); + }); +}; + +var setupActivityDetection = function(session) { + if (disableActivityDetection || !session || !timer.getTime()) { + + clearTimeout(activityChangeTimer); // stop activity detection + + } else { + + var now = moment(); + if (now.diff(session.startDate, "m") >= config.getActivityTime()) { + + // The user has stayed more than "config.getActivityTime()" minutes on a window + // and now has just switched to another window. + // If it stays at list as long as he did on the previous one, let's notify it to him + + activityChangeTimer = setTimeout( + eventEmitter.emit.bind(eventEmitter, "onActivityChange", now.toDate()), + moment.duration(config.getActivityTime(), "m").as("ms") + ); + activityChangeTimer.crtActivityName = session.name; + activityChangeTimer.changeDetectedDate = now.toDate(); + + } else if (session.name === activityChangeTimer.crtActivityName) { + + // The user is back to the window from which we initiated the activity timer, + // it seems to indicate that he is still working on the same activity. Let's clear the timer. + + clearTimeout(activityChangeTimer); + } + } +}; + /** + * Current active window is still the same + * but it's title has changed. + * Trace it by creating a new session with the same shape attributes than the previous one + */ +var updateWindowSession = function(ev) { + return getWindowName(ev.wid) + .then(function(windowName) { + + // Let's create a new session but with same color attributes than the last one, + // since the active window has not really changed, only some of its attributes + + if (crtSession.name !== windowName) { + createNewSession(windowName, crtSession.color); + } + + }); +}; + +var createNewSession = function(windowName, color, startDate) { + var now = moment(); + + // Stop current session + + if (crtSession) { + crtSession.endDate = now.valueOf(); + db.insertSession(crtSession); + } + + // Check if a session corresponding to the given name already exists, + // so that we can retrieve some properties + + var prev = db.getSessionByName(windowName); + + // Create new session + + crtSession = { + name: windowName, + color: color || prev && prev.color || "#" + (Math.floor(Math.random() * (999 - 100 + 1)) + 100), + startDate: startDate || now.valueOf() + }; +}; + +var createFakeSession = function(startDate, endDate) { + var d1 = moment(startDate), d2 = moment(endDate); + + return { + name: "", + color: "transparent", + startDate: d1, + endDate: d2 + }; +}; + +/** + * Create a new window session + */ +var addWindowSession = function(xClient, ev) { + var crt = crtSession; + + return getActiveWindowId(xClient, ev) + .then(getWindowName) + .then(createNewSession) + .then(setupActivityDetection.bind(null, crt)); +}; + +/////////////// +// Exports // +/////////////// +/** * Retrieve window activity sessions for the peridio [startDAte; endDate] * If withEmptySession parameter equals true, then if there is a gap between 2 sessions in the results, * it will be filled with a fake session @@ -32,7 +220,7 @@ // Append the current active session to the sessions list - return crtSession ? sessions.concat([crtSession]) : sessions + return crtSession ? sessions.concat([crtSession]) : sessions; }) .then(function (sessions) { var result = JSON.parse(JSON.stringify(sessions)); @@ -89,16 +277,20 @@ disableActivityDetection = false; }; + +/////////////////////////////// +// Init activity detection // +/////////////////////////////// + /* * Listen to active window change and window title change - * Some part of the code has derived from : + * Some part of the code has been derived from : * http://stackoverflow.com/questions/19840459/linux-get-notification-on-focuse... - * */ x11.createClient(function(err, display) { - var X = display.client; + var xClient = display.client; - X.ChangeWindowAttributes(display.screen[0].root, { + xClient.ChangeWindowAttributes(display.screen[0].root, { eventMask: x11.eventMask.PropertyChange }); @@ -106,7 +298,7 @@ // addWindowSession(X, display.screen[0].root); // Listen to client display events to identify when the currently active window has change - X.on('event', onActiveWindowChange.bind(null, X)); + xClient.on("event", onActiveWindowChange.bind(null, xClient)); }); /** @@ -114,180 +306,8 @@ * If timer change occured while activity change detection is on going, * stop activity change detection process (means the user has handled it) */ -timer.subscribeTimeChanged(function () { - !timer.getTime() && clearTimeout(activityChangeTimer); +timer.subscribeTimeChanged(function() { + if (!timer.getTime()) { + clearTimeout(activityChangeTimer); + } }); - - -/////////////// -// Utilities // -/////////////// - -var onActiveWindowChange = function (X, ev) { - if(ev.name == 'PropertyNotify') { - - X.GetAtomName(ev.atom, function(err, name) { - - // if the property change refers to the currently active window, retrieve new active window name - - if (name == '_NET_ACTIVE_WINDOW') { - - registerWindowEvents(X, ev); - addWindowSession(X, ev); - - } else if (name == '_NET_WM_NAME') { - - updateWindowSession(X, ev); - } - }); - } -}; - -var registerWindowEvents = function (X, ev) { - getActiveWindowId(X, ev) - .then(function (wid) { - X.ChangeWindowAttributes(wid, { - eventMask:x11.eventMask.PropertyChange - }); - }); -}; - -/** - * Create a new window session - */ -var addWindowSession = function (X, ev) { - var crt = crtSession; - - return getActiveWindowId(X, ev) - .then(getWindowName) - .then(createNewSession) - .then(setupActivityDetection.bind(null, crt)); -}; - -/** - * Current active window is still the same - * but it's title has changed. - * Trace it by creating a new session with the same shape attributes than the previous one - */ -var updateWindowSession = function (X, ev) { - return getWindowName(ev.wid) - .then(function (windowName) { - - // Let's create a new seesion but with same color attributes than the last one, - // since the active window has not really changed, only some of its attibutes - - if (currentSession.name != windowName) { - createNewSession(windowName, last.color); - } - - }); -}; - -var getActiveWindowId = function (X, ev) { - return new Promise(function (resolve, reject) { - - if (!ev.wid) return reject(); - - X.GetProperty(0, ev.wid, ev.atom, X.atoms.WINDOW, 0, 4, function(err, prop) { - if (err || !prop.data.length) { - console.log("Couldn't retrieve active window property"); - reject(); - - } else { - var wid = prop.data.readUInt32LE(0); - - if (wid) { - resolve(wid); - - } else { - console.log("Couldn't retrieve active window property"); - reject(); - } - } - }); - }); -}; - -var getWindowName = function (windowId) { - return new Promise(function (resolve, reject) { - - if (!windowId) return reject(); - - xprop({prop:'_NET_WM_NAME', id:windowId}, function(err, properties) { - - if (err) { - console.log("Couldn't retrieve window name for id :", windowId); - reject(); - - } else { - resolve(properties.length && properties[0].value); - } - }); - }); -}; - -var createNewSession = function (windowName, color, startDate) { - var now = moment(); - - // Stop current session - - if (crtSession) { - crtSession.endDate = now.valueOf(); - db.insertSession(crtSession); - } - - // Check if a session corresponding to the given name already exists, - // so that we can retrieve some properties - - var prev = db.getSessionByName(windowName); - - // Create new session - - crtSession = { - name: windowName, - color: color || prev && prev.color || "#" + (Math.floor(Math.random() * (999 - 100 + 1)) + 100), - startDate: startDate || now.valueOf() - }; -}; - -var createFakeSession = function (startDate, endDate) { - var d1 = moment(startDate), d2 = moment(endDate); - - return { - name: "", - color: "transparent", - startDate: d1, - endDate: d2 - }; -}; - -var setupActivityDetection = function (session) { - if (disableActivityDetection || !session || !timer.getTime()) { - - clearTimeout(activityChangeTimer); // stop activity detection - - } else { - - now = moment(); - if (now.diff(session.startDate, "m") >= config.getActivityTime()) { - - // The user has stayed more than "config.getActivityTime()" minutes on a window - // and now has just switched to another window. - // If it stays at list as long as he did on the previous one, let's notify it to him - - activityChangeTimer = setTimeout( - eventEmitter.emit.bind(eventEmitter, 'onActivityChange', now.toDate()), - moment.duration(config.getActivityTime(), "m").as("ms") - ); - activityChangeTimer.crtActivityName = session.name; - activityChangeTimer.changeDetectedDate = now.toDate(); - - } else if (session.name == activityChangeTimer.crtActivityName) { - - // The user is back to the window from which we initiated the activity timer, - // it seems to indicate that he is still working on the same activity. Let's clear the timer. - - clearTimeout(activityChangeTimer); - } - } -};
participants (1)
-
smaisonneuveďź users.nuiton.org