0.91/chrome/content/notifyWindow.js

Back to the directory /*
    --------------------------------------------------------
    Notify
    --------------------------------------------------------
    
    Copyright (C) 2006, Gemme.pl
    http://notify.torino.pl/en/
    All rights reserved.
    
    See attached file: LICENCE.TXT
    
    --------------------------------------------------------
*/

var NotifySitesWindow = {
    
    openOptions: function()
    {
        window.openDialog("chrome://notify/content/preferences.xul", "", "chrome,titlebar,toolbar,centerscreen,modal");
    },
    
    init: function()
    {
        var now = new Date();
        document.getElementById('calendarSiteDay').value = now.getDate();
        document.getElementById('calendarSiteMonth').value = now.getMonth() + 1;
        document.getElementById('calendarSiteYear').value = now.getFullYear();
        
        this.loadObservedSites();
        this.loadCalendarSites();
    },
    
    loadObservedSites: function()
    {
        var lbObserved = document.getElementById('observedSites');
        
        // clear
        var rowCount = lbObserved.getRowCount();
        for( var i = 0; i < rowCount; i++ )
        {
            lbObserved.removeItemAt(0);
        }
        
        // add sites
        for (var el in gNotify.ObservedSites)
        {
            var URL = gNotify.ObservedSites[el].URL;
            lbObserved.appendItem(URL, el); // label, value
        }
    },
    
    loadCalendarSites: function()
    {
        var lbCalendar = document.getElementById('calendarSites');
        
        // clear
        var rowCount = lbCalendar.getRowCount();
        for( var i = 0; i < rowCount; i++ )
        {
            lbCalendar.removeItemAt(0);
        }
        
        // add sites
        for (var el in gNotify.CalendarSites)
        {
            var URL = gNotify.CalendarSites[el].URL;
            var d = new Date(gNotify.CalendarSites[el].When);
            var When = d.toLocaleDateString();
            
            var item = document.createElement('listitem');
            item.nodeValue = el;
            var cell1 = document.createElement('listcell');
                cell1.setAttribute('label', URL);
            var cell2 = document.createElement('listcell');
                cell2.setAttribute('label', When);
            // hidden id
            var cell3 = document.createElement('listcell');
                cell3.style.display = "none";
                cell3.setAttribute('label', el);
            item.appendChild(cell1);
            item.appendChild(cell2);
            item.appendChild(cell3);
            lbCalendar.appendChild(item);
            
            /*
            var item = lbCalendar.appendItem(URL, el);
            item.appendChild( document.createElement('listcell') ).setAttribute('label', When); // second column
            */
        }
    },
    
    addObservedSite: function()
    {
        var URL = document.getElementById('observedSite').value;
        /* unnecessary, and doesn't work
        var result =
            gNotifyPrompt.prompt(
                null,
                gNotify.StrBundle.GetStringFromName("notify"),
                gNotify.StrBundle.GetStringFromName("Observe"),
                URL,
                null, false);
        */
        
        if (//result == false ||
            URL == "" ||
            URL == "http://")
        {
            return;
        }
        
        if (gNotify.Prefs.sites.addObservedSite(URL))
        {
            this.loadObservedSites();
        }
        else
        {
            window.alert(gNotify.StrBundle.GetStringFromName("SiteAlreadyObserved"));
        }
        
        // document.getElementById('observedSite').select();
        // TODO: selects with http:// - need to automatically add it, or .. ?
        document.getElementById('observedSite').focus();
    },
    
    addCalendarSite: function()
    {
        var URL = document.getElementById('calendarSite').value;
        
        var Day = parseInt(document.getElementById('calendarSiteDay').value);
        var Month = parseInt(document.getElementById('calendarSiteMonth').value);
        var Year = parseInt(document.getElementById('calendarSiteYear').value);
        
        if (isNaN(Day) || isNaN(Month) || isNaN(Year))
        {
            window.alert(gNotify.StrBundle.GetStringFromName("WrongDate"));
            return;
        }
        
        if (URL == "" || URL == "http://")
        {
            return;
        }
        
        gNotify.Prefs.sites.addCalendarSite(URL, Year, Month, Day);
        
        this.loadCalendarSites();
        
        // document.getElementById('observedSite').select();
        // TODO: selects with http:// - need to automatically add it, or .. ?
        document.getElementById('calendarSite').focus();
    },
    
    deleteObservedSite: function()
    {
        var lbObserved = document.getElementById('observedSites');
        var message;
        
        switch (lbObserved.selectedCount)
        {
            case 0:
                return;
                break;
            case 1:
                message = gNotify.StrBundle.GetStringFromName("ConfirmSiteDelete1");
                break;
            default:
                message = gNotify.StrBundle.GetStringFromName("ConfirmSiteDelete");
                break;
        }
        
        if (!window.confirm(message)) {
            return;
        }
        
        // delete :)
        var count = lbObserved.selectedCount;
        while (count--)
        {
            var item = lbObserved.selectedItems[0];
            gNotify.Prefs.sites.deleteObservedSite(item.value, (count == 0));
            lbObserved.removeItemAt(lbObserved.getIndexOfItem(item));
        }
        
        this.loadObservedSites();
    },
    
    deleteCalendarSite: function()
    {
        var lbCalendar = document.getElementById('calendarSites');
        var message;
        
        switch (lbCalendar.selectedCount)
        {
            case 0:
                return;
                break;
            case 1:
                message = gNotify.StrBundle.GetStringFromName("ConfirmSiteDelete1");
                break;
            default:
                message = gNotify.StrBundle.GetStringFromName("ConfirmSiteDelete");
                break;
        }
        
        if (!window.confirm(message)) {
            return;
        }
        
        // delete :)
        var count = lbCalendar.selectedCount;
        while (count--)
        {
            var item = lbCalendar.selectedItems[0];
            gNotify.Prefs.sites.deleteCalendarSite(
                item.getElementsByTagName("listcell")[/* hidden col */ 2].getAttribute("label"), (count == 0)
                );
            lbCalendar.removeItemAt(lbCalendar.getIndexOfItem(item));
        }
        
        this.loadCalendarSites();
    }
    
}; // var NotifySitesWindow

gNotify =
    Components.classes['@torino.pl/notify;1']
        .getService(Components.interfaces.nsINotify)
        .wrappedJSObject;
© 2006-2007 Zespół Gemme - Programy na zamówienie