You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

newmail_notifier.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * New Mail Notifier plugin script
  3. *
  4. * @author Aleksander Machniak <alec@alec.pl>
  5. *
  6. * @licstart The following is the entire license notice for the
  7. * JavaScript code in this file.
  8. *
  9. * Copyright (c) 2013-2016, The Roundcube Dev Team
  10. *
  11. * The JavaScript code in this page is free software: you can redistribute it
  12. * and/or modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation, either version 3 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * @licend The above is the entire license notice
  17. * for the JavaScript code in this file.
  18. */
  19. if (window.rcmail && rcmail.env.task == 'mail') {
  20. rcmail.addEventListener('plugin.newmail_notifier', newmail_notifier_run)
  21. .addEventListener('actionbefore', newmail_notifier_stop)
  22. .addEventListener('init', function() {
  23. // bind to messages list select event, so favicon will be reverted on message preview too
  24. if (rcmail.message_list)
  25. rcmail.message_list.addEventListener('select', newmail_notifier_stop);
  26. });
  27. }
  28. // Executes notification methods
  29. function newmail_notifier_run(prop)
  30. {
  31. if (prop.basic)
  32. newmail_notifier_basic();
  33. if (prop.sound)
  34. newmail_notifier_sound();
  35. if (prop.desktop)
  36. newmail_notifier_desktop(rcmail.get_label('body', 'newmail_notifier'));
  37. }
  38. // Stops notification
  39. function newmail_notifier_stop(prop)
  40. {
  41. // revert original favicon
  42. if (rcmail.env.favicon_href && rcmail.env.favicon_changed && (!prop || prop.action != 'check-recent')) {
  43. $('<link rel="shortcut icon" href="'+rcmail.env.favicon_href+'"/>').replaceAll('link[rel="shortcut icon"]');
  44. rcmail.env.favicon_changed = 0;
  45. }
  46. // Remove IE icon overlay if we're pinned to Taskbar
  47. try {
  48. if(window.external.msIsSiteMode()) {
  49. window.external.msSiteModeClearIconOverlay();
  50. }
  51. } catch(e) {}
  52. }
  53. // Basic notification: window.focus and favicon change
  54. function newmail_notifier_basic()
  55. {
  56. var w = rcmail.is_framed() ? window.parent : window,
  57. path = rcmail.assets_path('plugins/newmail_notifier');
  58. w.focus();
  59. // we cannot simply change a href attribute, we must to replace the link element (at least in FF)
  60. var link = $('<link rel="shortcut icon">').attr('href', path + '/favicon.ico'),
  61. oldlink = $('link[rel="shortcut icon"]', w.document);
  62. if (!rcmail.env.favicon_href)
  63. rcmail.env.favicon_href = oldlink.attr('href');
  64. rcmail.env.favicon_changed = 1;
  65. link.replaceAll(oldlink);
  66. // Add IE icon overlay if we're pinned to Taskbar
  67. try {
  68. if (window.external.msIsSiteMode()) {
  69. window.external.msSiteModeSetIconOverlay(path + '/overlay.ico', rcmail.get_label('title', 'newmail_notifier'));
  70. }
  71. } catch(e) {}
  72. }
  73. // Sound notification
  74. function newmail_notifier_sound()
  75. {
  76. var elem, src = rcmail.assets_path('plugins/newmail_notifier/sound'),
  77. plugin = navigator.mimeTypes ? navigator.mimeTypes['audio/mp3'] : {};
  78. // Internet Explorer does not support wav files,
  79. // support in other browsers depends on enabled plugins,
  80. // so we use wav as a fallback
  81. src += bw.ie || (plugin && plugin.enabledPlugin) ? '.mp3' : '.wav';
  82. // HTML5
  83. try {
  84. elem = $('<audio>').attr('src', src);
  85. elem.get(0).play();
  86. }
  87. // old method
  88. catch (e) {
  89. elem = $('<embed id="sound" src="' + src + '" hidden=true autostart=true loop=false />');
  90. elem.appendTo($('body'));
  91. window.setTimeout("$('#sound').remove()", 5000);
  92. }
  93. }
  94. // Desktop notification
  95. // - Require window.Notification API support (Chrome 22+ or Firefox 22+)
  96. function newmail_notifier_desktop(body, disabled_callback)
  97. {
  98. var timeout = rcmail.env.newmail_notifier_timeout || 10,
  99. icon = rcmail.assets_path('plugins/newmail_notifier/mail.png'),
  100. success_callback = function() {
  101. var popup = new window.Notification(rcmail.get_label('title', 'newmail_notifier'), {
  102. dir: "auto",
  103. lang: "",
  104. body: body,
  105. tag: "newmail_notifier",
  106. icon: icon
  107. });
  108. popup.onclick = function() { this.close(); };
  109. setTimeout(function() { popup.close(); }, timeout * 1000);
  110. };
  111. try {
  112. window.Notification.requestPermission(function(perm) {
  113. if (perm == 'granted')
  114. success_callback();
  115. else if (perm == 'denied' && disabled_callback)
  116. disabled_callback();
  117. });
  118. return true;
  119. }
  120. catch (e) {
  121. return false;
  122. }
  123. }
  124. function newmail_notifier_test_desktop()
  125. {
  126. var status = newmail_notifier_desktop(rcmail.get_label('testbody', 'newmail_notifier'), function() {
  127. rcmail.display_message(rcmail.get_label('desktopdisabled', 'newmail_notifier'), 'error');
  128. });
  129. if (!status) {
  130. rcmail.display_message(rcmail.get_label('desktopunsupported', 'newmail_notifier'), 'error');
  131. }
  132. }
  133. function newmail_notifier_test_basic()
  134. {
  135. newmail_notifier_basic();
  136. }
  137. function newmail_notifier_test_sound()
  138. {
  139. newmail_notifier_sound();
  140. }