-
Notifications
You must be signed in to change notification settings - Fork 3
/
threading_as_default.php
97 lines (91 loc) · 3.13 KB
/
threading_as_default.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* In here there be hacks. We do three things:
* - If someone sets list view, save this in our settings.
* - For new folders, select "Threads" as default
* - Every time we are rendering the list of mailboxes, get the user
* preferences and see if the user has overridden the view mode for any
* folder. Use tihs to rebuild the message_threading setting.
*
* TODO/Notes:
* - Does not respect setting "list" view mode from drop-down in the mailbox.
* - Does not clean the settings over the long run. (i.e. may accumulate a few
* bytes of settings data if run over a long time with many, frequently
* changing mailboxes)
* - Might be run the preferences update more frequently than needed.
* - Not properly tested. Yet.
*
* @version 0.9
* @author Morten Minde Neergaard
*/
function unrecurse($tree) {
$paths = array();
foreach ($tree as $key => $value) {
$paths[] = $value['id'];
$paths = array_merge($paths, unrecurse($value['folders']));
}
return $paths;
}
class threading_as_default extends rcube_plugin
{
public $task = 'mail|settings';
function init()
{
$this->add_hook('render_mailboxlist', array($this, 'default_override'));
$this->add_hook('folder_update', array($this, 'folder_save_override'));
$this->add_hook('folder_form', array($this, 'folder_form_override'));
}
function folder_form_override($args) {
$rcmail = rcmail::get_instance();
$prefs = $rcmail->config->get('thread_override', array());
if (array_key_exists('info', $args['form']['props']['fieldsets'])) {
$path = $args['form']['props']['fieldsets']['location']['content']['path']['value'];
$path = preg_replace('/.*value="([^"]*)".*/', "$1", $path);
if ($path == '') {
$folder = '';
} else {
$folder = $path . $rcmail->imap->get_hierarchy_delimiter();
}
$path = $args['form']['props']['fieldsets']['location']['content']['name']['value'];
$path = preg_replace('/.*value="([^"]*)".*/', "$1", $path);
$folder .= $path;
} else {
$folder = '';
}
if (! array_key_exists($folder, $prefs)) {
$args['form']['props']['fieldsets']['settings']['content']['viewmode']['value'] = <<<EOF
<select name="_viewmode" id="_listmode">
<option value="0">List</option>
<option value="1" selected="selected">Threads</option>
</select>
EOF;
}
return $args;
}
function folder_save_override($args) {
$rcmail = rcmail::get_instance();
$prefs = $rcmail->config->get('thread_override', array());
if (! $args['record']['settings']['view_mode']) {
$prefs[$args['record']['name']] = 0;
$rcmail->user->save_prefs(array('thread_override' => $prefs));
}
return $args;
}
function default_override($args) {
$rcmail = rcmail::get_instance();
$prefs = $rcmail->config->get('thread_override', array());
$a_thread = $rcmail->config->get('message_threading', array());
$list = unrecurse($args['list']);
$changed = false;
foreach ($list as $folder) {
if ((! array_key_exists($folder, $prefs)) && (! array_key_exists($folder, $a_thread))) {
$changed = true;
$a_thread[$folder] = true;
}
}
if ($changed) {
$rcmail->user->save_prefs(array('message_threading' => $a_thread));
}
return $args;
}
}