4 * New Mail Notifier plugin
6 * Supports three methods of notification:
7 * 1. Basic - focus browser window and change favicon
8 * 2. Sound - play wav file
9 * 3. Desktop - display desktop notification (using webkitNotifications feature,
10 * supported by Chrome and Firefox with 'HTML5 Notifications' plugin)
13 * @author Aleksander Machniak <alec@alec.pl>
16 * Copyright (C) 2011, Kolab Systems AG
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2
20 * as published by the Free Software Foundation.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 class newmail_notifier extends rcube_plugin
34 public $task = 'mail|settings';
40 * Plugin initialization
44 $this->rc = rcmail::get_instance();
47 if ($this->rc->task == 'settings') {
48 $this->add_hook('preferences_list', array($this, 'prefs_list'));
49 $this->add_hook('preferences_save', array($this, 'prefs_save'));
51 else { // if ($this->rc->task == 'mail') {
52 $this->add_hook('new_messages', array($this, 'notify'));
53 // add script when not in ajax and not in frame
54 if (is_a($this->rc->output, 'rcube_template') && empty($_REQUEST['_framed'])) {
55 $this->add_texts('localization/');
56 $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.body');
57 $this->include_script('newmail_notifier.js');
63 * Handler for user preferences form (preferences_list hook)
65 function prefs_list($args)
67 if ($args['section'] != 'mailbox') {
74 // Load localization and configuration
75 $this->add_texts('localization/');
77 if (!empty($_REQUEST['_framed'])) {
78 $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.testbody',
79 'newmail_notifier.desktopunsupported', 'newmail_notifier.desktopenabled', 'newmail_notifier.desktopdisabled');
80 $this->include_script('newmail_notifier.js');
83 // Check that configuration is not disabled
84 $dont_override = (array) $this->rc->config->get('dont_override', array());
86 foreach (array('basic', 'desktop', 'sound') as $type) {
87 $key = 'newmail_notifier_' . $type;
88 if (!in_array($key, $dont_override)) {
89 $field_id = '_' . $key;
90 $input = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
91 $content = $input->show($this->rc->config->get($key))
92 . ' ' . html::a(array('href' => '#', 'onclick' => 'newmail_notifier_test_'.$type.'()'),
93 $this->gettext('test'));
95 $args['blocks']['new_message']['options'][$key] = array(
96 'title' => html::label($field_id, Q($this->gettext($type))),
106 * Handler for user preferences save (preferences_save hook)
108 function prefs_save($args)
110 if ($args['section'] != 'mailbox') {
114 // Load configuration
115 $this->load_config();
117 // Check that configuration is not disabled
118 $dont_override = (array) $this->rc->config->get('dont_override', array());
120 foreach (array('basic', 'desktop', 'sound') as $type) {
121 $key = 'newmail_notifier_' . $type;
122 if (!in_array($key, $dont_override)) {
123 $args['prefs'][$key] = get_input_value('_'.$key, RCUBE_INPUT_POST) ? true : false;
131 * Handler for new message action (new_messages hook)
133 function notify($args)
135 // Already notified or non-automatic check
136 if ($this->notified || !empty($_GET['_refresh'])) {
140 // Get folders to skip checking for
141 if (empty($this->exceptions)) {
142 $this->delimiter = $this->rc->imap->get_hierarchy_delimiter();
143 foreach (array('drafts_mbox', 'sent_mbox') as $folder) {
144 $folder = $this->rc->config->get($folder);
145 if (strlen($folder) && $folder != 'INBOX') {
146 $this->exceptions[] = $folder;
151 $mbox = $args['mailbox'];
153 // Skip exception (sent/drafts) folders (and their subfolders)
154 foreach ($this->exceptions as $folder) {
155 if (strpos($mbox.$this->delimiter, $folder.$this->delimiter) === 0) {
160 $this->notified = true;
162 // Load configuration
163 $this->load_config();
165 $basic = $this->rc->config->get('newmail_notifier_basic');
166 $sound = $this->rc->config->get('newmail_notifier_sound');
167 $desktop = $this->rc->config->get('newmail_notifier_desktop');
169 if ($basic || $sound || $desktop) {
170 $this->rc->output->command('plugin.newmail_notifier',
171 array('basic' => $basic, 'sound' => $sound, 'desktop' => $desktop));