Ext.namespace("Zarafa.plugins.desktopnotifications");Zarafa.plugins.desktopnotifications.ABOUT='<p>Copyright (C) 2013  Saket Patel &lt;silentsakky@gmail.com&gt;</p><p>This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.</p><p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.</p><p>You should have received a copy of the GNU Affero General Public License along with this program. If not, see <a href="http://www.gnu.org/licenses/" target="_blank">http://www.gnu.org/licenses/</a>.</p>';
Ext.namespace("Zarafa.plugins.desktopnotifications.js");
Zarafa.plugins.desktopnotifications.js.DesktopNotification=function(){var a=window.webkitNotifications||window.Notification,b=["granted","default","denied"];return{supports:function(){if(a)return true;return false},hasPermission:function(){if(this.supports()){var c="default";if(Ext.isFunction(a.checkPermission))c=b[a.checkPermission()];else if(Ext.isFunction(a.permissionLevel))c=a.permissionLevel();else if(a.permission)c=a.permission;if(c==="granted")return true;return false}else console.log("Browser doesn't support notifications")},
authorize:function(c){if(this.supports()){var e=Ext.isFunction(c)?function(f){var d="granted";if(f)d=f;c.apply(this,[d])}:Ext.emptyFn;Ext.isFunction(a.requestPermission)&&a.requestPermission(e)}else console.log("Browser doesn't support notifications")},notify:function(c,e,f){if(this.supports())if(this.hasPermission()){var d;if(Ext.isFunction(a.createNotification)){d=a.createNotification(e.icon,c,e.body);d.show()}else d=new a(c,{icon:e.icon,body:e.body,tag:e.tag});if(container.getSettingsModel().get("zarafa/v1/plugins/desktopnotifications/autohide_enable")){var h=
container.getSettingsModel().get("zarafa/v1/plugins/desktopnotifications/autohide_time")*1E3;d.addEventListener("show",function(){setTimeout(function(){d.close()},h)})}if(f)for(var g in f)d["on"+g]=f[g];this.audioTag=Ext.getBody().createChild({tag:"audio",type:"audio/ogg",src:"plugins/desktopnotifications/resources/audio.ogg",autoplay:true});this.audioTag.on("ended",function(){Ext.destroy(this.audioTag);delete this.audioTag},this)}else console.log("Permission is denied to show desktop notifications");
else console.log("Browser doesn't support notifications")}}}();Ext.namespace("Zarafa.plugins.desktopnotifications");
Zarafa.plugins.desktopnotifications.DesktopNotificationsPlugin=Ext.extend(Zarafa.core.Plugin,{initPlugin:function(){Zarafa.plugins.desktopnotifications.DesktopNotificationsPlugin.superclass.initPlugin.apply(this,arguments);this.registerInsertionPoint("context.settings.categories",this.createSettingsCategory,this)},createSettingsCategory:function(){return{xtype:"zarafa.settingsdesktopnotificationscategory",plugin:this}}});
Zarafa.onReady(function(){container.registerPlugin(new Zarafa.core.PluginMetaData({name:"desktopnotifications",displayName:_("Desktop Notifications Plugin"),about:Zarafa.plugins.desktopnotifications.ABOUT,pluginConstructor:Zarafa.plugins.desktopnotifications.DesktopNotificationsPlugin}))});Ext.namespace("Zarafa.plugins.desktopnotifications.js");
Zarafa.plugins.desktopnotifications.js.DesktopNotifier=Ext.extend(Zarafa.core.ui.notifier.NotifyPlugin,{constructor:function(a){a=a||{};Zarafa.plugins.desktopnotifications.js.DesktopNotifier.superclass.constructor.call(this,a)},notify:function(a,b,c){Zarafa.plugins.desktopnotifications.js.DesktopNotification.notify(b,{tag:a,body:c,icon:"plugins/desktopnotifications/resources/popup.ico"},{click:function(){window.focus()}});Zarafa.plugins.desktopnotifications.js.DesktopNotifier.superclass.notify.apply(this,
arguments)}});Zarafa.onReady(function(){container.getNotifier().registerPlugin("desktopnotifier",new Zarafa.plugins.desktopnotifications.js.DesktopNotifier)});Ext.namespace("Zarafa.plugins.desktopnotifications.js.settings");
Zarafa.plugins.desktopnotifications.js.settings.SettingsNotificationsCategory=Ext.extend(Zarafa.settings.ui.SettingsCategory,{constructor:function(a){a=a||{};Ext.applyIf(a,{title:_("Desktop Notifications"),categoryIndex:9996,xtype:"zarafa.settingsdesktopnotificationscategory",iconCls:"icon_desktopnotifications_settings",items:[{xtype:"zarafa.settingsdesktopnotificationswidget",settingsContext:a.settingsContext}]});Zarafa.plugins.desktopnotifications.js.settings.SettingsNotificationsCategory.superclass.constructor.call(this,
a)}});Ext.reg("zarafa.settingsdesktopnotificationscategory",Zarafa.plugins.desktopnotifications.js.settings.SettingsNotificationsCategory);Ext.namespace("Zarafa.plugins.desktopnotifications.js.settings");
Zarafa.plugins.desktopnotifications.js.settings.SettingsNotificationsWidget=Ext.extend(Zarafa.settings.ui.SettingsWidget,{plugin:undefined,model:undefined,constructor:function(a){a=a||{};Ext.applyIf(a,{title:_("Desktop Notifications Settings Plugin"),xtype:"zarafa.settingsdesktopnotificationswidget",layout:{type:"fit"},items:this.createPanelItems()});Zarafa.plugins.desktopnotifications.js.settings.SettingsNotificationsWidget.superclass.constructor.call(this,a)},createPanelItems:function(){return[{xtype:"form",
border:false,autoHeight:true,items:[{xtype:"displayfield",hideLabel:true,value:_("For enabling desktop notifications we need permissions from the browser. (Only Chrome and Firefox are supported) ")},{xtype:"button",width:200,text:_("Request Permissions"),handler:this.requestPermission,scope:this,hideLabel:true,ref:"../requestPermissionBtn",style:"margin-bottom: 6px;"},{xtype:"checkbox",boxLabel:_("Enable desktop notifications for new mail"),name:"zarafa/v1/main/notifier/info/newmail/value",handler:this.onChangeCheckbox,
scope:this,hideLabel:true,ref:"../newMailNotificationsCheck"},{xtype:"checkbox",boxLabel:_("Enable desktop notifications for reminders"),name:"zarafa/v1/main/notifier/info/reminder/value",handler:this.onChangeCheckbox,scope:this,hideLabel:true,ref:"../reminderNotificationsCheck"},{xtype:"zarafa.compositefield",plugins:["zarafa.splitfieldlabeler"],fieldLabel:_("{A} Autohide desktop notification after second(s) {B}"),labelWidth:300,combineErrors:false,items:[{xtype:"checkbox",labelSplitter:"{A}",name:"zarafa/v1/plugins/desktopnotifications/autohide_enable",
ref:"../../autoHideBox",boxLabel:"",hideLabel:true,checked:true,style:"margin-top: 4px",listeners:{change:this.onFieldChange,scope:this}},{xtype:"zarafa.spinnerfield",labelSplitter:"{B}",vtype:"naturalInteger",name:"zarafa/v1/plugins/desktopnotifications/autohide_time",ref:"../../autoHideTimeSpinner",incrementValue:1,defaultValue:1,minValue:1,width:55,allowBlank:false,allowDecimals:false,allowNegative:false,listeners:{change:this.onFieldChange,scope:this},plugins:["zarafa.numberspinner"]}]}]}]},requestPermission:function(){Zarafa.plugins.desktopnotifications.js.DesktopNotification.authorize(function(a){a===
"granted"&&this.update(this.model)}.createDelegate(this))},onChangeCheckbox:function(a,b){if(this.model){var c="popup";if(b)c="desktopnotifier";this.model.get(a.name)!==c&&this.model.set(a.name,c);this.update(this.model)}},update:function(a){this.model=a;var b=Zarafa.plugins.desktopnotifications.js.DesktopNotification.hasPermission();this.requestPermissionBtn.setDisabled(b);this.newMailNotificationsCheck.setDisabled(!b);this.reminderNotificationsCheck.setDisabled(!b);this.newMailNotificationsCheck.setValue(this.model.get(this.newMailNotificationsCheck.name)===
"desktopnotifier");this.reminderNotificationsCheck.setValue(this.model.get(this.reminderNotificationsCheck.name)==="desktopnotifier");this.autoHideBox.setValue(a.get(this.autoHideBox.name));a=this.model.get(this.autoHideTimeSpinner.name);a===0||!Ext.isDefined(a)?this.autoHideTimeSpinner.setValue(5):this.autoHideTimeSpinner.setValue(a)},updateSettings:function(a){var b=this.autoHideTimeSpinner.getValue();if(b===0||!Ext.isDefined(b))b=1;a.beginEdit();a.set(this.autoHideTimeSpinner.name,b);a.endEdit()},
onFieldChange:function(a,b){this.model&&this.model.get(a.name)!==b&&this.model.set(a.name,b)}});Ext.reg("zarafa.settingsdesktopnotificationswidget",Zarafa.plugins.desktopnotifications.js.settings.SettingsNotificationsWidget);
