From e927e735600271d7d81f3c660972c3d465bb8f4a Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Mon, 28 Nov 2011 17:42:35 +0000 Subject: [PATCH] * Add ability to use browser notifications+popups, when available from the browser --- .../inc/class.notifications_ajax.inc.php | 32 +++++++++++--- .../inc/class.notifications_popup.inc.php | 15 +++++-- notifications/js/notificationajaxpopup.js | 42 +++++++++++++++++-- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/notifications/inc/class.notifications_ajax.inc.php b/notifications/inc/class.notifications_ajax.inc.php index a032d389b6..e8f1b2ea7f 100644 --- a/notifications/inc/class.notifications_ajax.inc.php +++ b/notifications/inc/class.notifications_ajax.inc.php @@ -13,6 +13,10 @@ * Ajax methods for notifications */ class notifications_ajax { + public $public_functions = array( + 'get_notification' => true + ); + /** * Appname */ @@ -84,7 +88,10 @@ class notifications_ajax { * */ public function __construct() { - $this->response = new xajaxResponse(); + if(class_exists('xajaxResponse')) + { + $this->response = new xajaxResponse(); + } $this->recipient = (object)$GLOBALS['egw']->accounts->read(); $this->config = (object)config::read(self::_appname); @@ -110,7 +117,7 @@ class notifications_ajax { * * @return xajax response */ - public function get_notifications() { + public function get_notifications($browserNotify = false) { if ($GLOBALS['egw_info']['user']['apps']['felamimail']) $this->check_mailbox(); // update currentusers @@ -120,7 +127,7 @@ class notifications_ajax { $this->response->jquery('#currentusers', 'text', array((string)$GLOBALS['egw']->session->session_count())); } - $this->get_egwpopup(); + $this->get_egwpopup($browserNotify); return $this->response->getXML(); } @@ -137,6 +144,7 @@ class notifications_ajax { { $this->db->delete(self::_notification_table,array( 'notify_id' => $notify_id, + 'account_id' => $this->recipient->account_id, ),__LINE__,__FILE__,self::_appname); } } @@ -246,7 +254,7 @@ class notifications_ajax { * * @return boolean true or false */ - private function get_egwpopup() { + private function get_egwpopup($browserNotify = false) { $message = ''; $rs = $this->db->select(self::_notification_table, '*', array( 'account_id' => $this->recipient->account_id, @@ -254,7 +262,21 @@ class notifications_ajax { __LINE__,__FILE__,false,'',self::_appname); if ($rs->NumRows() > 0) { foreach ($rs as $notification) { - $this->response->addScriptCall('append_notification_message',$notification['notify_id'],$notification['notify_message']); + $message = null; + if($browserNotify && $this->preferences[self::_appname]['egwpopup_verbosity'] != 'low') + { + $message = $notification['notify_message']; + + // Check for a link - doesn't work in notification + if(strpos($message, lang('Linked entries:'))) + { + $message = substr_replace($message, '', strpos($message, lang('Linked entries:'))); + } + $message = preg_replace('#]*>#is','',$message); + + $message = 'data:text/html;charset=' . translation::charset() .';base64,'.base64_encode($message); + } + $this->response->addScriptCall('append_notification_message',$notification['notify_id'],$notification['notify_message'],$message); } switch($this->preferences[self::_appname]['egwpopup_verbosity']) { diff --git a/notifications/inc/class.notifications_popup.inc.php b/notifications/inc/class.notifications_popup.inc.php index ca166d6089..22217321e9 100644 --- a/notifications/inc/class.notifications_popup.inc.php +++ b/notifications/inc/class.notifications_popup.inc.php @@ -148,7 +148,8 @@ class notifications_popup implements notifications_iface { } else { $image = ''; } - if($link->popup) { + if($link->popup && !$GLOBALS['egw_info']['user']['preferences']['notifications']['external_mailclient']) + { $dimensions = explode('x', $link->popup); $rendered_links[] = html::div($image.$link->text,'onclick="'.$this->jspopup($url, '_blank', $dimensions[0], $dimensions[1]).'"','link'); } else { @@ -172,8 +173,16 @@ class notifications_popup implements notifications_iface { */ private function jspopup($link,$target='_blank',$width=750,$height=410) { - return 'egw_openWindowCentered2('.($link == 'this.href' ? $link : "'".$link."'").','. - ($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')"; + if($GLOBALS['egw_info']['user']['preferences']['notifications']['external_mailclient']) + { + return 'window.open('.($link == 'this.href' ? $link : "'".$link."'").','. + ($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')"; + } + else + { + return 'egw_openWindowCentered2('.($link == 'this.href' ? $link : "'".$link."'").','. + ($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')"; + } } /** diff --git a/notifications/js/notificationajaxpopup.js b/notifications/js/notificationajaxpopup.js index 853ebb90f3..0300f9ac6d 100644 --- a/notifications/js/notificationajaxpopup.js +++ b/notifications/js/notificationajaxpopup.js @@ -9,6 +9,7 @@ */ var notifymessages = {}; +var EGW_BROWSER_NOTIFY_ALLOWED = 0; function egwpopup_init(_i) { window.setTimeout("egwpopup_refresh(" + _i + ");", 1000); @@ -18,10 +19,18 @@ function egwpopup_setTimeout(_i) { window.setTimeout("egwpopup_refresh(" + _i + ");", _i*1000); } function egwpopup_refresh(_i) { - xajax_doXMLHTTP("notifications.notifications_ajax.get_notifications"); + var request = xajax_doXMLHTTP("notifications.notifications_ajax.get_notifications", check_browser_notify()); + request.request.error = function(_xmlhttp,_err){if(console) {console.log(request);console.log(_err)}}; egwpopup_setTimeout(_i); } +/** + * Check to see if browser supports / allows desktop notifications + */ +function check_browser_notify() { + return window.webkitNotifications && window.webkitNotifications.checkPermission() == EGW_BROWSER_NOTIFY_ALLOWED; +} + function egwpopup_display() { var egwpopup; var egwpopup_message; @@ -48,7 +57,16 @@ function egwpopup_display() { } else { egwpopup_ok_button.value = "OK"; } - + if(window.webkitNotifications && window.webkitNotifications.checkPermission() != EGW_BROWSER_NOTIFY_ALLOWED && + jQuery('#desktop_perms').length == 0) + { + var desktop_button = jQuery('') + .click(function() { + window.webkitNotifications.requestPermission(); + jQuery(this).hide(); + }); + desktop_button.appendTo(jQuery(egwpopup_ok_button).parent()); + } } function notificationbell_switch(mode) { @@ -98,6 +116,24 @@ function egwpopup_button_close() { notificationbell_switch("inactive"); } -function append_notification_message(_id, _message) { +function append_notification_message(_id, _message, _browser_notify) { + + if(!check_browser_notify() || typeof notifymessages[_id] != 'undefined') + { + notifymessages[_id] = _message; + return; + } + // Prevent the same thing popping up multiple times notifymessages[_id] = _message; + + // Notification API + var notice = webkitNotifications.createHTMLNotification(_browser_notify); + notice.ondisplay = function() { + // Confirm when user gets to see it - no close needed + // Wait a bit to let it load first, or it might not be there when requested. + window.setTimeout( function() { + xajax_doXMLHTTP("notifications.notifications_ajax.confirm_message", _id); + }, 2000); + }; + notice.show(); }