Aleksander Machniak
2015-03-08 0878c846bc2c1030ed01c8db34e20796c31ccd2d
commit | author | age
48e9c1 1 /* Enigma Plugin */
T 2
0878c8 3 window.rcmail && rcmail.addEventListener('init', function(evt) {
AM 4     if (rcmail.env.task == 'settings') {
5         rcmail.register_command('plugin.enigma', function() { rcmail.goto_url('plugin.enigma') }, true);
6         rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import() }, true);
7 //        rcmail.register_command('plugin.enigma-key-export', function() { rcmail.enigma_key_export() }, true);
8         rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_key_delete(); });
48e9c1 9
0878c8 10         if (rcmail.gui_objects.keyslist) {
AM 11             rcmail.keys_list = new rcube_list_widget(rcmail.gui_objects.keyslist,
12                 {multiselect:false, draggable:false, keyboard:false});
13             rcmail.keys_list
14                 .addEventListener('select', function(o) { rcmail.enigma_keylist_select(o); })
15                 .addEventListener('keypress', function(o) { rcmail.enigma_keylist_keypress(o); })
16                 .init()
17                 .focus();
48e9c1 18
0878c8 19             rcmail.enigma_list();
48e9c1 20
0878c8 21             rcmail.register_command('firstpage', function(props) { return rcmail.enigma_list_page('first'); });
AM 22             rcmail.register_command('previouspage', function(props) { return rcmail.enigma_list_page('previous'); });
23             rcmail.register_command('nextpage', function(props) { return rcmail.enigma_list_page('next'); });
24             rcmail.register_command('lastpage', function(props) { return rcmail.enigma_list_page('last'); });
25         }
48e9c1 26
0878c8 27         if (rcmail.env.action == 'plugin.enigmakeys') {
AM 28             rcmail.register_command('search', function(props) {return rcmail.enigma_search(props); }, true);
29             rcmail.register_command('reset-search', function(props) {return rcmail.enigma_search_reset(props); }, true);
30             rcmail.register_command('plugin.enigma-import', function() { rcmail.enigma_import(); }, true);
31 //            rcmail.register_command('plugin.enigma-export', function() { rcmail.enigma_export(); }, true);
32         }
33     }
34     else if (rcmail.env.task == 'mail') {
35         if (rcmail.env.action == 'compose') {
36             $('input,label', $('#enigmamenu')).mouseup(function(e) {
37                 // don't close the menu on mouse click inside
38                 e.stopPropagation();
39             });
40         }
41         else if (rcmail.env.action == 'show' || rcmail.env.action == 'preview') {
42             if (rcmail.env.enigma_password_request) {
43                 rcmail.enigma_password_request(rcmail.env.enigma_password_request);
48e9c1 44             }
T 45         }
0878c8 46     }
AM 47 });
48
48e9c1 49
T 50 /*********************************************************/
51 /*********    Enigma Settings/Keys/Certs UI      *********/
52 /*********************************************************/
53
54 // Display key(s) import form
55 rcube_webmail.prototype.enigma_key_import = function()
56 {
0878c8 57     this.enigma_loadframe('&_action=plugin.enigmakeys&_a=import');
48e9c1 58 };
T 59
0878c8 60 // Delete key(s)
AM 61 rcube_webmail.prototype.enigma_key_delete = function()
62 {
63     var keys = this.keys_list.get_selection();
64
65     if (!keys.length || !confirm(this.get_label('enigma.keyremoveconfirm')))
66         return;
67
68     var lock = this.display_message(this.get_label('enigma.keyremoving'), 'loading'),
69         post = {_a: 'delete', _keys: keys};
70
71     // send request to server
72     this.http_post('plugin.enigmakeys', post, lock);
73 };
74
75 // Submit key(s) import form
48e9c1 76 rcube_webmail.prototype.enigma_import = function()
T 77 {
78     var form, file;
0878c8 79
48e9c1 80     if (form = this.gui_objects.importform) {
T 81         file = document.getElementById('rcmimportfile');
82         if (file && !file.value) {
83             alert(this.get_label('selectimportfile'));
84             return;
85         }
0878c8 86
AM 87         var lock = this.set_busy(true, 'importwait');
88
89         form.action = this.add_url(form.action, '_unlock', lock);
48e9c1 90         form.submit();
0878c8 91
48e9c1 92         this.lock_form(form, true);
T 93    }
94 };
95
96 // list row selection handler
0878c8 97 rcube_webmail.prototype.enigma_keylist_select = function(list)
48e9c1 98 {
T 99     var id;
100     if (id = list.get_single_selection())
0878c8 101         this.enigma_loadframe('&_action=plugin.enigmakeys&_a=info&_id=' + id);
AM 102
103     this.enable_command('plugin.enigma-key-delete', list.selection.length > 0);
104 };
105
106 rcube_webmail.prototype.enigma_keylist_keypress = function(list)
107 {
108     if (list.modkey == CONTROL_KEY)
109         return;
110
111     if (list.key_pressed == list.DELETE_KEY || list.key_pressed == list.BACKSPACE_KEY)
112         this.command('plugin.enigma-key-delete');
113     else if (list.key_pressed == 33)
114         this.command('previouspage');
115     else if (list.key_pressed == 34)
116         this.command('nextpage');
48e9c1 117 };
T 118
119 // load key frame
0878c8 120 rcube_webmail.prototype.enigma_loadframe = function(url)
48e9c1 121 {
T 122     var frm, win;
0878c8 123
48e9c1 124     if (this.env.contentframe && window.frames && (frm = window.frames[this.env.contentframe])) {
0878c8 125         if (!url && (win = window.frames[this.env.contentframe])) {
AM 126             if (win.location && win.location.href.indexOf(this.env.blankpage) < 0)
48e9c1 127                 win.location.href = this.env.blankpage;
T 128             return;
129         }
0878c8 130
48e9c1 131         this.set_busy(true);
0878c8 132         frm.location.href = this.env.comm_path + '&_framed=1' + url;
48e9c1 133     }
T 134 };
135
136 // Search keys/certs
137 rcube_webmail.prototype.enigma_search = function(props)
138 {
139     if (!props && this.gui_objects.qsearchbox)
140         props = this.gui_objects.qsearchbox.value;
141
142     if (props || this.env.search_request) {
0878c8 143         var params = {'_a': 'search', '_q': urlencode(props)},
48e9c1 144           lock = this.set_busy(true, 'searching');
T 145 //        if (this.gui_objects.search_filter)
146   //          addurl += '&_filter=' + this.gui_objects.search_filter.value;
0878c8 147         this.env.current_page = 1;
48e9c1 148         this.enigma_loadframe();
T 149         this.enigma_clear_list();
0878c8 150         this.http_post('plugin.enigmakeys', params, lock);
48e9c1 151     }
T 152
153     return false;
154 }
155
156 // Reset search filter and the list
157 rcube_webmail.prototype.enigma_search_reset = function(props)
158 {
159     var s = this.env.search_request;
160     this.reset_qsearch();
161
162     if (s) {
163         this.enigma_loadframe();
164         this.enigma_clear_list();
165
166         // refresh the list
167         this.enigma_list();
168     }
169
170     return false;
171 }
172
173 // Keys/certs listing
174 rcube_webmail.prototype.enigma_list = function(page)
175 {
0878c8 176     var params = {'_a': 'list'},
48e9c1 177       lock = this.set_busy(true, 'loading');
T 178
179     this.env.current_page = page ? page : 1;
180
181     if (this.env.search_request)
182         params._q = this.env.search_request;
183     if (page)
184         params._p = page;
185
186     this.enigma_clear_list();
0878c8 187     this.http_post('plugin.enigmakeys', params, lock);
48e9c1 188 }
T 189
190 // Change list page
191 rcube_webmail.prototype.enigma_list_page = function(page)
192 {
193     if (page == 'next')
194         page = this.env.current_page + 1;
195     else if (page == 'last')
196         page = this.env.pagecount;
197     else if (page == 'prev' && this.env.current_page > 1)
198         page = this.env.current_page - 1;
199     else if (page == 'first' && this.env.current_page > 1)
200         page = 1;
201
202     this.enigma_list(page);
203 }
204
205 // Remove list rows
206 rcube_webmail.prototype.enigma_clear_list = function()
207 {
208     this.enigma_loadframe();
209     if (this.keys_list)
210         this.keys_list.clear(true);
211 }
212
213 // Adds a row to the list
214 rcube_webmail.prototype.enigma_add_list_row = function(r)
215 {
216     if (!this.gui_objects.keyslist || !this.keys_list)
217         return false;
218
219     var list = this.keys_list,
220         tbody = this.gui_objects.keyslist.tBodies[0],
221         rowcount = tbody.rows.length,
222         even = rowcount%2,
223         css_class = 'message'
224             + (even ? ' even' : ' odd'),
225         // for performance use DOM instead of jQuery here
226         row = document.createElement('tr'),
227         col = document.createElement('td');
228
229     row.id = 'rcmrow' + r.id;
230     row.className = css_class;
231
232     col.innerHTML = r.name;
233     row.appendChild(col);
234     list.insert_row(row);
235 }
236
237 /*********************************************************/
238 /*********        Enigma Message methods         *********/
239 /*********************************************************/
240
241 // Import attached keys/certs file
242 rcube_webmail.prototype.enigma_import_attachment = function(mime_id)
243 {
0878c8 244     var lock = this.set_busy(true, 'loading'),
AM 245         post = {_uid: this.env.uid, _mbox: this.env.mailbox, _part: mime_id};
246
247     this.http_post('plugin.enigmaimport', post, lock);
48e9c1 248
T 249     return false;
0878c8 250 }
48e9c1 251
0878c8 252 rcube_webmail.prototype.enigma_password_request = function(data)
AM 253 {
254     if (!data || !data.keyid) {
255         return;
256     }
257
258     var ref = this,
259         msg = this.get_label('enigma.enterkeypass'),
260         myprompt = $('<div class="prompt">'),
261         myprompt_content = $('<div class="message">')
262             .appendTo(myprompt),
263         myprompt_input = $('<input>').attr({type: 'password', size: 30})
264             .keypress(function(e) {
265                 if (e.which == 13)
266                     (ref.is_framed() ? window.parent.$ : $)('.ui-dialog-buttonpane button.mainaction:visible').click();
267             })
268             .appendTo(myprompt);
269
270     data.key = data.keyid;
271     data.keyid = data.keyid.substr(0, 8);
272
273     $.each(['keyid', 'user'], function() {
274         msg = msg.replace('$' + this, data[this]);
275     });
276
277     myprompt_content.text(msg);
278
279     this.show_popup_dialog(myprompt, this.get_label('enigma.enterkeypasstitle'),
280         [{
281             text: this.get_label('save'),
282             'class': 'mainaction',
283             click: function(e) {
284                 e.stopPropagation();
285
286                 var jq = ref.is_framed() ? window.parent.$ : $,
287                     pass = myprompt_input.val();
288
289                 if (!pass) {
290                     myprompt_input.focus();
291                     return;
292                 }
293
294                 ref.enigma_password_submit(data.key, pass);
295                 jq(this).remove();
296             }
297         },
298         {
299             text: this.get_label('cancel'),
300             click: function(e) {
301                 var jq = ref.is_framed() ? window.parent.$ : $;
302                 e.stopPropagation();
303                 jq(this).remove();
304             }
305         }], {width: 400});
306
307     if (this.is_framed() && parent.rcmail.message_list) {
308         // this fixes bug when pressing Enter on "Save" button in the dialog
309         parent.rcmail.message_list.blur();
310     }
311 }
312
313 rcube_webmail.prototype.enigma_password_submit = function(keyid, password)
314 {
315     var form = $('<form>').attr({method: 'post', action: location.href, style: 'display:none'})
316         .append($('<input>').attr({type: 'hidden', name: '_keyid', value: keyid}))
317         .append($('<input>').attr({type: 'hidden', name: '_passwd', value: password}))
318         .append($('<input>').attr({type: 'hidden', name: '_token', value: this.env.request_token}))
319         .appendTo(document.body);
320
321     form.submit();
322 }