]> git.donarmstrong.com Git - roundcube.git/blob - plugins/newmail_notifier/newmail_notifier.php
Imported Upstream version 0.7
[roundcube.git] / plugins / newmail_notifier / newmail_notifier.php
1 <?php
2
3 /**
4  * New Mail Notifier plugin
5  *
6  * Supports two 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)
11  *
12  * @version 0.3
13  * @author Aleksander Machniak <alec@alec.pl>
14  *
15  *
16  * Copyright (C) 2011, Kolab Systems AG
17  *
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.
21  *
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.
26  *
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.
30  */
31
32 class newmail_notifier extends rcube_plugin
33 {
34     public $task = 'mail|settings';
35
36     private $rc;
37     private $notified;
38
39     /**
40      * Plugin initialization
41      */
42     function init()
43     {
44         $this->rc = rcmail::get_instance();
45
46         // Preferences hooks
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'));
50         }
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');
58             }
59         }
60     }
61
62     /**
63      * Handler for user preferences form (preferences_list hook)
64      */
65     function prefs_list($args)
66     {
67         if ($args['section'] != 'mailbox') {
68             return $args;
69         }
70
71         // Load configuration
72         $this->load_config();
73
74         // Load localization and configuration
75         $this->add_texts('localization/');
76
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');
81         }
82
83         // Check that configuration is not disabled
84         $dont_override = (array) $this->rc->config->get('dont_override', array());
85
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'));
94
95                 $args['blocks']['new_message']['options'][$key] = array(
96                     'title' => html::label($field_id, Q($this->gettext($type))),
97                     'content' => $content
98                 );
99             }
100         }
101
102         return $args;
103     }
104
105     /**
106      * Handler for user preferences save (preferences_save hook)
107      */
108     function prefs_save($args)
109     {
110         if ($args['section'] != 'mailbox') {
111             return $args;
112         }
113
114         // Load configuration
115         $this->load_config();
116
117         // Check that configuration is not disabled
118         $dont_override = (array) $this->rc->config->get('dont_override', array());
119
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;
124             }
125         }
126
127         return $args;
128     }
129
130     /**
131      * Handler for new message action (new_messages hook)
132      */
133     function notify($args)
134     {
135         if ($this->notified || !empty($_GET['_refresh'])) {
136             return $args;
137         }
138
139         $this->notified = true;
140
141         // Load configuration
142         $this->load_config();
143
144         $basic   = $this->rc->config->get('newmail_notifier_basic');
145         $sound   = $this->rc->config->get('newmail_notifier_sound');
146         $desktop = $this->rc->config->get('newmail_notifier_desktop');
147
148         if ($basic || $sound || $desktop) {
149             $this->rc->output->command('plugin.newmail_notifier',
150                 array('basic' => $basic, 'sound' => $sound, 'desktop' => $desktop));
151         }
152
153         return $args;
154     }
155 }