source: subversion/trunk/plugins/newmail_notifier/newmail_notifier.php @ 5113

Last change on this file since 5113 was 5113, checked in by alec, 21 months ago
File size: 4.7 KB
Line 
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 *
10 * @version 0.2
11 * @author Aleksander Machniak <alec@alec.pl>
12 *
13 *
14 * Copyright (C) 2011, Kolab Systems AG
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30class newmail_notifier extends rcube_plugin
31{
32    public $task = 'mail|settings';
33
34    private $rc;
35
36    /**
37     * Plugin initialization
38     */
39    function init()
40    {
41        $this->rc = rcmail::get_instance();
42
43        // Preferences hooks
44        if ($this->rc->task == 'settings') {
45            $this->add_hook('preferences_list', array($this, 'prefs_list'));
46            $this->add_hook('preferences_save', array($this, 'prefs_save'));
47        }
48        else { // if ($this->rc->task == 'mail') {
49            $this->add_hook('new_messages', array($this, 'notify'));
50            // add script when not in ajax and not in frame
51            if (is_a($this->rc->output, 'rcube_template') && empty($_REQUEST['_framed'])) {
52                $this->include_script('newmail_notifier.js');
53            }
54        }
55    }
56
57    /**
58     * Handler for user preferences form (preferences_list hook)
59     */
60    function prefs_list($args)
61    {
62        if ($args['section'] != 'mailbox') {
63            return $args;
64        }
65
66        // Load configuration
67        $this->load_config();
68
69        // Load localization and configuration
70        $this->add_texts('localization/');
71
72        // Check that configuration is not disabled
73        $dont_override  = (array) $this->rc->config->get('dont_override', array());
74        $basic_override = in_array('newmail_notifier_basic', $dont_override);
75        $sound_override = in_array('newmail_notifier_sound', $dont_override);
76
77        if (!$basic_override) {
78            $field_id = '_newmail_notifier_basic';
79            $input    = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
80            $args['blocks']['new_message']['options']['newmail_notifier_basic'] = array(
81                'title' => html::label($field_id, Q($this->gettext('basic'))),
82                'content' => $input->show($this->rc->config->get('newmail_notifier_basic')),
83            );
84        }
85
86        if (!$sound_override) {
87            $field_id = '_newmail_notifier_sound';
88            $input    = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
89            $args['blocks']['new_message']['options']['newmail_notifier_sound'] = array(
90                'title' => html::label($field_id, Q($this->gettext('sound'))),
91                'content' => $input->show($this->rc->config->get('newmail_notifier_sound')),
92            );
93        }
94
95        return $args;
96    }
97
98    /**
99     * Handler for user preferences save (preferences_save hook)
100     */
101    function prefs_save($args)
102    {
103        if ($args['section'] != 'mailbox') {
104            return $args;
105        }
106
107        // Load configuration
108        $this->load_config();
109
110        // Check that configuration is not disabled
111        $dont_override  = (array) $this->rc->config->get('dont_override', array());
112        $basic_override = in_array('newmail_notifier_basic', $dont_override);
113        $sound_override = in_array('newmail_notifier_sound', $dont_override);
114
115        if (!$basic_override) {
116            $key = 'newmail_notifier_basic';
117            $args['prefs'][$key] = get_input_value('_'.$key, RCUBE_INPUT_POST) ? true : false;
118        }
119        if (!$sound_override) {
120            $key = 'newmail_notifier_sound';
121            $args['prefs'][$key] = get_input_value('_'.$key, RCUBE_INPUT_POST) ? true : false;
122        }
123
124        return $args;
125    }
126
127    /**
128     * Handler for new message action (new_messages hook)
129     */
130    function notify($args)
131    {
132        // Load configuration
133        $this->load_config();
134
135        $basic = $this->rc->config->get('newmail_notifier_basic');
136        $sound = $this->rc->config->get('newmail_notifier_sound');
137
138        if ($basic || $sound) {
139            $this->rc->output->command('plugin.newmail_notifier',
140                array('basic' => $basic, 'sound' => $sound));
141        }
142
143        return $args;
144    }
145}
Note: See TracBrowser for help on using the repository browser.