/*global window, rJS */ /*jslint nomen: true, indent: 2, maxerr: 3*/ (function (window, rJS) { "use strict"; var gadget_klass = rJS(window), rusha = new Rusha(); gadget_klass .ready(function (g) { g.props = {}; return g.getDeclaredGadget('jio_gadget') .push(function (jio_gadget) { g.props.jio_gadget = jio_gadget; }); }) .ready(function (g) { return g.getDeclaredGadget('login_gadget') .push(function (login_gadget) { g.props.login_gadget = login_gadget; }); }) .declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("setSetting", "setSetting") .declareMethod("startSync", function (options) { var gadget = this, monitor_cred_dict = {}, sync_lock = false; function formatDate(d){ function addZero(n){ return n < 10 ? '0' + n : '' + n; } return d.getFullYear() + "-" + addZero(d.getMonth()+1) + "-" + addZero(d.getDate()) + " " + addZero(d.getHours()) + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getMinutes()); } function syncStorage(gadget, storage_dict, query) { gadget.props.jio_gadget.createJio(storage_dict, true, query); return gadget.props.jio_gadget.repair() .push(undefined, function (error) { gadget.props.error_list.push({ storage_dict: storage_dict, query: query }); console.log(error); }); } function getMonitoringStorageList (feed_url_list) { var base_url, base_url_hash, key, i, j, feed_config, dav_config, monitor_storage_list = [], storage_type_list = [], dav_storage = { type: "query", sub_storage: { type: "drivetojiomapping", sub_storage: { type: "dav" } } }, feed_storage = { type: "query", sub_storage: { type: "feed", feed_type: 'rss' } }; storage_type_list = [ {path: 'jio_public/', query: {}}, {path: 'jio_private/', query: {}, private_access: true}, /*{path: 'jio_private/data/', query: { //query: '_id: "%.data" AND _id: "%.status"' // Skip history }}*/ ]; for (i = 0; i < feed_url_list.length; i += 1) { feed_config = JSON.parse(JSON.stringify(feed_storage)); feed_config.sub_storage.url = feed_url_list[i].htmlurl; monitor_storage_list.push({ storage: feed_config, query: {} }); base_url = feed_url_list[i].url.replace('jio_public/', ''); // Hard coded!! base_url_hash = rusha.digestFromString(base_url + 'jio_private/'); // hard coded!! for (j = 0; j < storage_type_list.length; j += 1) { dav_config = JSON.parse(JSON.stringify(dav_storage)); dav_config.sub_storage.sub_storage.url = base_url + storage_type_list[j].path; if (storage_type_list[j].private_access) { if (monitor_cred_dict.hasOwnProperty(base_url_hash)) { dav_config.sub_storage.sub_storage.basic_login = monitor_cred_dict[base_url_hash].hash; } else { continue; } } monitor_storage_list.push({ storage: dav_config, query: storage_type_list[j].query }); } } return monitor_storage_list; } function syncAllStorage() { var monitor_storage_list = [], last_sync_time; if (sync_lock === true) { return []; } if (options.now) { sync_lock = true; } return new RSVP.Queue() .push(function () { $(".notifyjs-wrapper").remove(); return $.notify( "Synchronizing Data...", { position:"bottom right", autoHide: false, className: "info" } ); }) .push(function () { return gadget.props.login_gadget.getUrlDict(); }) .push(function(url_dict) { monitor_cred_dict = url_dict; return gadget.props.jio_gadget.getUrlFeedDescription(options.query); }) .push(function (url_list) { var i, promise_list = []; monitor_storage_list = getMonitoringStorageList(url_list); gadget.props.error_list = []; for (i = 0; i < monitor_storage_list.length; i += 1) { promise_list.push(syncStorage( gadget, monitor_storage_list[i].storage, monitor_storage_list[i].query)); } console.log("Sync monitoring data..."); return RSVP.all(promise_list); }) .push(function () { // Check if there was errors and retry them if possible var error_size = gadget.props.error_list.length, error_list = gadget.props.error_list.slice(0), promise_list = [], i; if (error_size > 0) { // Reset list of errors gadget.props.error_list = []; $(".notifyjs-wrapper").remove(); promise_list.push($.notify( "Retry Previous Failure(s)...", { position:"bottom right", autoHide: false, className: "info" } )); for (i = 0; i < error_size; i += 1) { promise_list.push(syncStorage( gadget, error_list[i].storage_dict, error_list[i].query )); } } return RSVP.all(promise_list); }) .push(function () { last_sync_time = new Date().getTime(); sync_lock = false; return gadget.setSetting('latest_sync_time', last_sync_time); }) .push(function () { var time = 3000, classname = "info", error_amount = gadget.props.error_list.length, message = "Synchronisation finished."; if ( error_amount > 0) { classname = "warning"; time = 5000; message = "Synchronisation finished with " + error_amount + "error(s). \n" + "You can retry with manual sync."; } $(".notifyjs-wrapper").remove(); return $.notify( message, { position:"bottom right", autoHide: true, className: classname, autoHideDelay: time } ); }) .push(function () { return $.notify( "Last Sync: " + formatDate(new Date(last_sync_time)), { position:"bottom right", autoHide: true, className: "success", autoHideDelay: 30000 } ); }); } function syncDataTimer() { if (gadget.props.timer) { clearTimeout(gadget.props.timer); } gadget.props.timer = setTimeout(function(){ return new RSVP.Queue() .push(function () { return syncAllStorage(); }) .push(function () { return gadget.getSetting('sync_data_interval'); }) .push(function (timer_interval) { if (timer_interval === undefined) { timer_interval = 300000; } gadget.props.timer_interval = timer_interval; return syncDataTimer(); }); }, gadget.props.timer_interval); } if (options === undefined) { options = {}; } if (options.query === undefined) { options.query = { include_docs: true }; } if (options.now) { return syncAllStorage(); } return new RSVP.Queue() .push(function () { return gadget.getSetting('sync_data_interval'); }) .push(function (timer_interval) { if (timer_interval === undefined) { timer_interval = 300000; } gadget.props.timer_interval = timer_interval; return gadget.getSetting('latest_sync_time'); }) .push(function (latest_sync_time) { var current_time = new Date().getTime(), time_diff; if (latest_sync_time !== undefined) { time_diff = current_time - latest_sync_time; if ((time_diff - 10000) >= gadget.props.timer_interval) { // sync in after 10 second gadget.props.timer_interval = 10000; } else { gadget.props.timer_interval = gadget.props.timer_interval - time_diff; } } return syncDataTimer(); }); }); }(window, rJS));