From c0795652f729fd65a9feaab348b62bbb27992c26 Mon Sep 17 00:00:00 2001 From: alec Date: Thu, 22 Sep 2011 17:12:24 +0000 Subject: - Support HTML5 notifications + notification test feature git-svn-id: https://svn.roundcube.net/trunk@5269 208e9e7b-5314-0410-a742-e7e81cd9613c --- plugins/newmail_notifier/localization/en_US.inc | 9 ++- plugins/newmail_notifier/localization/pl_PL.inc | 9 ++- plugins/newmail_notifier/mail.png | Bin 0 -> 1408 bytes plugins/newmail_notifier/newmail_notifier.js | 60 +++++++++++++++++- plugins/newmail_notifier/newmail_notifier.php | 78 +++++++++++++----------- 5 files changed, 117 insertions(+), 39 deletions(-) create mode 100644 plugins/newmail_notifier/mail.png (limited to 'plugins') diff --git a/plugins/newmail_notifier/localization/en_US.inc b/plugins/newmail_notifier/localization/en_US.inc index fbb5d9aae..3017c43dc 100644 --- a/plugins/newmail_notifier/localization/en_US.inc +++ b/plugins/newmail_notifier/localization/en_US.inc @@ -1,6 +1,13 @@ diff --git a/plugins/newmail_notifier/localization/pl_PL.inc b/plugins/newmail_notifier/localization/pl_PL.inc index 5fe37903b..d95e658af 100644 --- a/plugins/newmail_notifier/localization/pl_PL.inc +++ b/plugins/newmail_notifier/localization/pl_PL.inc @@ -1,6 +1,13 @@ diff --git a/plugins/newmail_notifier/mail.png b/plugins/newmail_notifier/mail.png new file mode 100644 index 000000000..79f3a5311 Binary files /dev/null and b/plugins/newmail_notifier/mail.png differ diff --git a/plugins/newmail_notifier/newmail_notifier.js b/plugins/newmail_notifier/newmail_notifier.js index 6afd66aee..16e3edd42 100644 --- a/plugins/newmail_notifier/newmail_notifier.js +++ b/plugins/newmail_notifier/newmail_notifier.js @@ -1,7 +1,7 @@ /** * New Mail Notifier plugin script * - * @version 0.2 + * @version 0.3 * @author Aleksander Machniak */ @@ -22,6 +22,8 @@ function newmail_notifier_run(prop) newmail_notifier_basic(); if (prop.sound) newmail_notifier_sound(); + if (prop.desktop) + newmail_notifier_desktop(rcmail.gettext('body', 'newmail_notifier')); } // Stops notification @@ -37,11 +39,13 @@ function newmail_notifier_stop(prop) // Basic notification: window.focus and favicon change function newmail_notifier_basic() { - window.focus(); + var w = rcmail.is_framed() ? window.parent : window; + + w.focus(); // we cannot simply change a href attribute, we must to replace the link element (at least in FF) var link = $(''), - oldlink = $('link[rel="shortcut icon"]'); + oldlink = $('link[rel="shortcut icon"]', w.document); rcmail.env.favicon_href = oldlink.attr('href'); link.replaceAll(oldlink); @@ -64,3 +68,53 @@ function newmail_notifier_sound() window.setTimeout("$('#sound').remove()", 5000); } } + +// Desktop notification (need Chrome or Firefox with a plugin) +function newmail_notifier_desktop(body) +{ + var dn = window.webkitNotifications; + + if (dn && !dn.checkPermission()) { + if (rcmail.newmail_popup) + rcmail.newmail_popup.cancel(); + var popup = window.webkitNotifications.createNotification('plugins/newmail_notifier/mail.png', + rcmail.gettext('title', 'newmail_notifier'), body); + popup.onclick = function() { + this.cancel(); + } + popup.show(); + setTimeout(function() { popup.cancel(); }, 10000); // close after 10 seconds + rcmail.newmail_popup = popup; + return true; + } + + return false; +} + +function newmail_notifier_test_desktop() +{ + var dn = window.webkitNotifications, + txt = rcmail.gettext('testbody', 'newmail_notifier'); + + if (dn) { + if (!dn.checkPermission()) + newmail_notifier_desktop(txt); + else + dn.requestPermission(function() { + if (!newmail_notifier_desktop(txt)) + rcmail.display_message(rcmail.gettext('desktopdisabled', 'newmail_notifier'), 'error'); + }); + } + else + rcmail.display_message(rcmail.gettext('desktopunsupported', 'newmail_notifier'), 'error'); +} + +function newmail_notifier_test_basic() +{ + newmail_notifier_basic(); +} + +function newmail_notifier_test_sound() +{ + newmail_notifier_sound(); +} diff --git a/plugins/newmail_notifier/newmail_notifier.php b/plugins/newmail_notifier/newmail_notifier.php index a72d728dc..1f1df9e75 100644 --- a/plugins/newmail_notifier/newmail_notifier.php +++ b/plugins/newmail_notifier/newmail_notifier.php @@ -6,8 +6,10 @@ * Supports two methods of notification: * 1. Basic - focus browser window and change favicon * 2. Sound - play wav file + * 3. Desktop - display desktop notification (using webkitNotifications feature, + * supported by Chrome and Firefox with 'HTML5 Notifications' plugin) * - * @version 0.2 + * @version 0.3 * @author Aleksander Machniak * * @@ -32,6 +34,7 @@ class newmail_notifier extends rcube_plugin public $task = 'mail|settings'; private $rc; + private $notified; /** * Plugin initialization @@ -49,6 +52,8 @@ class newmail_notifier extends rcube_plugin $this->add_hook('new_messages', array($this, 'notify')); // add script when not in ajax and not in frame if (is_a($this->rc->output, 'rcube_template') && empty($_REQUEST['_framed'])) { + $this->add_texts('localization/'); + $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.body'); $this->include_script('newmail_notifier.js'); } } @@ -69,27 +74,29 @@ class newmail_notifier extends rcube_plugin // Load localization and configuration $this->add_texts('localization/'); - // Check that configuration is not disabled - $dont_override = (array) $this->rc->config->get('dont_override', array()); - $basic_override = in_array('newmail_notifier_basic', $dont_override); - $sound_override = in_array('newmail_notifier_sound', $dont_override); - - if (!$basic_override) { - $field_id = '_newmail_notifier_basic'; - $input = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1)); - $args['blocks']['new_message']['options']['newmail_notifier_basic'] = array( - 'title' => html::label($field_id, Q($this->gettext('basic'))), - 'content' => $input->show($this->rc->config->get('newmail_notifier_basic')), - ); + if (!empty($_REQUEST['_framed'])) { + $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.testbody', + 'newmail_notifier.desktopunsupported', 'newmail_notifier.desktopenabled', 'newmail_notifier.desktopdisabled'); + $this->include_script('newmail_notifier.js'); } - if (!$sound_override) { - $field_id = '_newmail_notifier_sound'; - $input = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1)); - $args['blocks']['new_message']['options']['newmail_notifier_sound'] = array( - 'title' => html::label($field_id, Q($this->gettext('sound'))), - 'content' => $input->show($this->rc->config->get('newmail_notifier_sound')), - ); + // Check that configuration is not disabled + $dont_override = (array) $this->rc->config->get('dont_override', array()); + + foreach (array('basic', 'desktop', 'sound') as $type) { + $key = 'newmail_notifier_' . $type; + if (!in_array($key, $dont_override)) { + $field_id = '_' . $key; + $input = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1)); + $content = $input->show($this->rc->config->get($key)) + . ' ' . html::a(array('href' => '#', 'onclick' => 'newmail_notifier_test_'.$type.'()'), + $this->gettext('test')); + + $args['blocks']['new_message']['options'][$key] = array( + 'title' => html::label($field_id, Q($this->gettext($type))), + 'content' => $content + ); + } } return $args; @@ -108,17 +115,13 @@ class newmail_notifier extends rcube_plugin $this->load_config(); // Check that configuration is not disabled - $dont_override = (array) $this->rc->config->get('dont_override', array()); - $basic_override = in_array('newmail_notifier_basic', $dont_override); - $sound_override = in_array('newmail_notifier_sound', $dont_override); + $dont_override = (array) $this->rc->config->get('dont_override', array()); - if (!$basic_override) { - $key = 'newmail_notifier_basic'; - $args['prefs'][$key] = get_input_value('_'.$key, RCUBE_INPUT_POST) ? true : false; - } - if (!$sound_override) { - $key = 'newmail_notifier_sound'; - $args['prefs'][$key] = get_input_value('_'.$key, RCUBE_INPUT_POST) ? true : false; + foreach (array('basic', 'desktop', 'sound') as $type) { + $key = 'newmail_notifier_' . $type; + if (!in_array($key, $dont_override)) { + $args['prefs'][$key] = get_input_value('_'.$key, RCUBE_INPUT_POST) ? true : false; + } } return $args; @@ -129,15 +132,22 @@ class newmail_notifier extends rcube_plugin */ function notify($args) { + if ($this->notified) { + return $args; + } + + $this->notified = true; + // Load configuration $this->load_config(); - $basic = $this->rc->config->get('newmail_notifier_basic'); - $sound = $this->rc->config->get('newmail_notifier_sound'); + $basic = $this->rc->config->get('newmail_notifier_basic'); + $sound = $this->rc->config->get('newmail_notifier_sound'); + $desktop = $this->rc->config->get('newmail_notifier_desktop'); - if ($basic || $sound) { + if ($basic || $sound || $desktop) { $this->rc->output->command('plugin.newmail_notifier', - array('basic' => $basic, 'sound' => $sound)); + array('basic' => $basic, 'sound' => $sound, 'desktop' => $desktop)); } return $args; -- cgit v1.2.3