<commit_before>/**
 * TODO http://msdn.microsoft.com/en-us/library/exchange/aa580274(v=exchg.150).aspx
 */
 
function Exchange() {

    var defaultConfig = {
        serverEWS: '',
        serverOWA: '',
        updateInterval: 30,
        username: '',
        password: '',
        volume: 0.3
    };

    var exchange = this;
    var cjs = new CJS({});
    var timerId = 0;

    exchange.countSet = false;
    exchange.listener = false;
    exchange.data = '';
    exchange.errors = [];
    exchange.options = {};
    exchange.unread = 0;
    exchange.unreadItems = [];
    exchange.lastNotify = null;

    exchange.load = function () {
        Object.keys(defaultConfig).forEach(function(optionKey){
            exchange.options[optionKey] = localStorage.getItem(optionKey);
        });
        return exchange;
    };

    exchange.save = function ( options ) {
        $.extend(localStorage, options);
        exchange.load();
        return exchange;
    };

    exchange.isValid = function () {
        exchange.load();
        var result = Object.keys(defaultConfig).every(function(optionKey)
        {
            return exchange.options[optionKey] !== null;
        });

        return result;
    };

    exchange.xmlAction = function (action, callback) {
        $.ajax({
            type    : "GET",
            url     : chrome.extension.getURL('xml/' + action + '.xml'),
            dataType: "text",
            success : function (data) {
                exchange.data = data;
                callback();
            },
            error   : function () {
                chrome.browserAction.setBadgeText({text: 'error'});
            }
        })
    };

    exchange.addListener = function () {
        exchange.listener = true;
        chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
            if (changeInfo.url) {
                exchange.getUnread();
            }
        });
    };

    exchange.update = function (callback) {
        $.ajax({
            type    : "POST",
            url     : exchange.options.serverEWS + '/Exchange.asmx',
            dataType: "xml",
            headers : {"Content-Type": "text/xml"},
            data    : exchange.data,
            password: <PASSWORD>,
            username: exchange.options.username,
            success : callback,
            error   : function () {
                chrome.browserAction.setBadgeText({text: 'error'});
            }
        })

    };

    exchange.goToInbox = function () {

        if (exchange.isValid()) {
            chrome.tabs.getAllInWindow(undefined, function (tabs) {
                    for (var i = 0, tab; tab = tabs[i]; i++) {
                        if (tab.url && (tab.url.indexOf(exchange.options.serverOWA) > -1)) {
                            chrome.tabs.update(tab.id, {selected: true, url: tab.url});
                            return;
                        }
                    }
                    exchange.owa(exchange.options.serverOWA, exchange.options.username, exchange.options.password);
            });
        } else {
            chrome.tabs.create({url: chrome.extension.getURL('owa_options.html')});
        }
    };

    exchange.disable = function (text) {
        cjs.enableIcon(false);
        chrome.browserAction.setBadgeText({text: text});
    };

    exchange.enable = function (text) {
        cjs.enableIcon(true);
        chrome.browserAction.setBadgeText({text: text});
    };

    exchange.getUnreadCount = function (unread) {
        unread = parseInt(unread);
        return isNaN(unread) ? -1 : unread;
    };

    exchange.playSound = function(volume){
        if(volume){
            cjs.volume(volume);
        }
        cjs.playSound();
    };

    exchange.notification = function(url, data, onclick)
    {
        if (!window.webkitNotifications || !window.webkitNotifications.createHTMLNotification)
        {
            return;
        }
        var urlData = Object.keys(data || {}).map(function(key)
        {
            return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
        }).join('&');
        exchange.lastNotify && exchange.lastNotify.close();
        exchange.lastNotify = webkitNotifications.createHTMLNotification(chrome.extension.getURL(url) + '?' + urlData);
        exchange.lastNotify.onclick = onclick || Function.empty;
        exchange.lastNotify.show();

        return exchange.lastNotify;
    };

    exchange.updateIcon = function (unread) {
        var oldUnread = exchange.getUnreadCount(exchange.unread);
        exchange.unread = exchange.getUnreadCount(unread);
        if (exchange.unread < 0) {
            exchange.disable('error');
        } else {
            if (exchange.unread == 0) {
                exchange.enable('');
            } else {
                if (exchange.unread > oldUnread) {
                    exchange.playSound(exchange.options.volume);
                    cjs.animate();
                    exchange.enable(exchange.unread.toString());
                    exchange.notification(
                        'notify.html',
                        {
                            title: ('You have {0} unread mails').fmt(unread),
                            message: 'Click to view'
                        },
                        function() {
                            exchange.goToInbox();
                            this.close();
                        }
                    );

                } else {
                    exchange.enable(exchange.unread.toString());<commit_msg>Fixing Mac OSX Chrome notifications for version 30.0.1599.101. Discovered that in 30+ createHTMLNotification returns undefined; so updated code per example found at: http://jsfiddle.net/dandv/wT26x/1/ … should the old still be supported? Not sure.<commit_after>/**
 * TODO http://msdn.microsoft.com/en-us/library/exchange/aa580274(v=exchg.150).aspx
 */
 
function Exchange() {

    var defaultConfig = {
        serverEWS: '',
        serverOWA: '',
        updateInterval: 30,
        username: '',
        password: '',
        volume: 0.3
    };

    var exchange = this;
    var cjs = new CJS({});
    var timerId = 0;

    exchange.countSet = false;
    exchange.listener = false;
    exchange.data = '';
    exchange.errors = [];
    exchange.options = {};
    exchange.unread = 0;
    exchange.unreadItems = [];
    exchange.lastNotify = null;

    exchange.load = function () {
        Object.keys(defaultConfig).forEach(function(optionKey){
            exchange.options[optionKey] = localStorage.getItem(optionKey);
        });
        return exchange;
    };

    exchange.save = function ( options ) {
        $.extend(localStorage, options);
        exchange.load();
        return exchange;
    };

    exchange.isValid = function () {
        exchange.load();
        var result = Object.keys(defaultConfig).every(function(optionKey)
        {
            return exchange.options[optionKey] !== null;
        });

        return result;
    };

    exchange.xmlAction = function (action, callback) {
        $.ajax({
            type    : "GET",
            url     : chrome.extension.getURL('xml/' + action + '.xml'),
            dataType: "text",
            success : function (data) {
                exchange.data = data;
                callback();
            },
            error   : function () {
                chrome.browserAction.setBadgeText({text: 'error'});
            }
        })
    };

    exchange.addListener = function () {
        exchange.listener = true;
        chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
            if (changeInfo.url) {
                exchange.getUnread();
            }
        });
    };

    exchange.update = function (callback) {
        $.ajax({
            type    : "POST",
            url     : exchange.options.serverEWS + '/Exchange.asmx',
            dataType: "xml",
            headers : {"Content-Type": "text/xml"},
            data    : exchange.data,
            password: <PASSWORD>,
            username: exchange.options.username,
            success : callback,
            error   : function () {
                chrome.browserAction.setBadgeText({text: 'error'});
            }
        })

    };

    exchange.goToInbox = function () {

        if (exchange.isValid()) {
            chrome.tabs.getAllInWindow(undefined, function (tabs) {
                    for (var i = 0, tab; tab = tabs[i]; i++) {
                        if (tab.url && (tab.url.indexOf(exchange.options.serverOWA) > -1)) {
                            chrome.tabs.update(tab.id, {selected: true, url: tab.url});
                            return;
                        }
                    }
                    exchange.owa(exchange.options.serverOWA, exchange.options.username, exchange.options.password);
            });
        } else {
            chrome.tabs.create({url: chrome.extension.getURL('owa_options.html')});
        }
    };

    exchange.disable = function (text) {
        cjs.enableIcon(false);
        chrome.browserAction.setBadgeText({text: text});
    };

    exchange.enable = function (text) {
        cjs.enableIcon(true);
        chrome.browserAction.setBadgeText({text: text});
    };

    exchange.getUnreadCount = function (unread) {
        unread = parseInt(unread);
        return isNaN(unread) ? -1 : unread;
    };

    exchange.playSound = function(volume){
        if(volume){
            cjs.volume(volume);
        }
        cjs.playSound();
    };

    exchange.notification = function(url, data, onclick)
    {
        //createHTMLNotification returns undefined in Chrome 30
        //http://jsfiddle.net/dandv/wT26x/1/ (working example)
        var havePermission = window.webkitNotifications.checkPermission();
        if (havePermission == 0) {
            var urlData = Object.keys(data || {}).map(function(key)
            {
                return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
            }).join('&');
            exchange.lastNotify && exchange.lastNotify.close();
            exchange.lastNotify = webkitNotifications.createNotification('http://www.rizwanashraf.com/wp-content/uploads/2013/07/outlook-web-app.png', data.title, data.message);
            exchange.lastNotify.onclick = onclick || Function.empty;
            exchange.lastNotify.show();

            return exchange.lastNotify;
        } else {
            window.webkitNotifications.requestPermission();
        }
    };

    exchange.updateIcon = function (unread) {
        var oldUnread = exchange.getUnreadCount(exchange.unread);
        exchange.unread = exchange.getUnreadCount(unread);
        if (exchange.unread < 0) {
            exchange.disable('error');
        } else {
            if (exchange.unread == 0) {
                exchange.enable('');
            } else {
                if (exchange.unread > oldUnread) {
                    exchange.playSound(exchange.options.volume);
                    cjs.animate();
                    exchange.enable(exchange.unread.toString());
                    exchange.notification(
                        'notify.html',
                        {
                            title: ('You have {0} unread mails').fmt(unread),
                            message: 'Click to view'
                        },
                        function() {
                            exchange.goToInbox();
                            this.close();
                        }
                    );

                } else {
                    exchange.enable(exchange.unread.toString());
{'fraction_non_alphanumeric': 0.12619437031935957, 'fraction_numerical': 0.005939571317896186, 'mean_word_length': 1.5985238201744576, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2052305', 'n_tokens_mistral': 3180, 'n_tokens_neox': 2999, 'n_words': 538}