Enable popups to use desktop notifications, if browser supports it

This commit is contained in:
Nathan Gray 2011-11-23 23:48:58 +00:00
parent 2effc93ea4
commit d7cfdfa3d0
3 changed files with 76 additions and 9 deletions

View File

@ -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,9 +262,23 @@ class notifications_ajax {
__LINE__,__FILE__,false,'',self::_appname);
if ($rs->NumRows() > 0) {
foreach ($rs as $notification) {
if($browserNotify)
{
// Check for a link - doesn't work in notification
if(strpos($notification['notify_message'], lang('Linked entries:')))
{
$notification['notify_message'] = substr_replace($notification['notify_message'], '', strpos($notification['notify_message'], lang('Linked entries:')));
}
$notification['notify_message'] = preg_replace('#</?a[^>]*>#is','',$notification['notify_message']);
$notification['notify_message'] = 'data:text/html;base64,'.base64_encode($notification['notify_message']);
}
$this->response->addScriptCall('append_notification_message',$notification['notify_id'],$notification['notify_message']);
}
// Skip in-page popup
if($browserNotify) return true;
switch($this->preferences[self::_appname]['egwpopup_verbosity']) {
case 'low':
$this->response->addScript('notificationbell_switch("active");');

View File

@ -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')";
}
}
/**

View File

@ -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('<button id="desktop_perms">' + (egw?egw.lang('Desktop notifications') : 'Desktop notifications') + '</button>')
.click(function() {
window.webkitNotifications.requestPermission();
jQuery(this).hide();
});
desktop_button.appendTo(jQuery(egwpopup_ok_button).parent());
}
}
function notificationbell_switch(mode) {
@ -99,5 +117,23 @@ function egwpopup_button_close() {
}
function append_notification_message(_id, _message) {
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(_message);
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();
}