Revert "The Desktop handler is not production ready yet."
authorJan Schneider <jan@horde.org>
Thu, 4 Nov 2010 13:12:41 +0000 (14:12 +0100)
committerJan Schneider <jan@horde.org>
Thu, 4 Nov 2010 13:12:41 +0000 (14:12 +0100)
This reverts commit d6c70f7046855dbd3a082ba6f00503d7aed0733e.

framework/Alarm/lib/Horde/Alarm/Handler/Desktop.php [new file with mode: 0644]

diff --git a/framework/Alarm/lib/Horde/Alarm/Handler/Desktop.php b/framework/Alarm/lib/Horde/Alarm/Handler/Desktop.php
new file mode 100644 (file)
index 0000000..e5a867d
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+/**
+ * @package Horde_Alarm
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ */
+
+/**
+ * The Horde_Alarm_Handler_Mail class is a Horde_Alarm handler that notifies
+ * of active alarms by desktop notification through webkit browsers.
+ *
+ * @author  Jan Schneider <jan@horde.org>
+ * @package Horde_Alarm
+ */
+class Horde_Alarm_Handler_Desktop extends Horde_Alarm_Handler
+{
+    /**
+     * A notification callback.
+     *
+     * @var callback
+     */
+    protected $_jsNotify;
+
+    /**
+     * An icon URL.
+     *
+     * @var string
+     */
+    protected $_icon;
+
+    /**
+     * Constructor.
+     *
+     * @param array $params  Any parameters that the handler might need.
+     *                       Required parameter:
+     *                       - js_notify: A Horde_Notification_Handler
+     *                         instance.
+     *                       Optional parameter:
+     *                       - icon: URL of an icon to display.
+     */
+    public function __construct(array $params = null)
+    {
+        /*
+        if (!isset($params['js_notify'])) {
+            throw new InvalidArgumentException('Parameter \'js_notify\' missing.');
+        }
+        if (!is_callable($params['js_notify'])) {
+            throw new Horde_Alarm_Exception('Parameter \'js_notify\' is not a Horde_Notification_Handler object.');
+        }
+        $this->_jsNotify = $params['jsNotify'];
+        if (isset($params['icon'])) {
+            $this->_icon = $params['icon'];
+        }
+        */
+        $this->_jsNotify = isset($params['js_notify'])
+            ? $params['js_notify']
+            : array('Horde', 'addInlineScript');
+        $this->_icon = isset($params['icon']) ? $params['icon'] : (string)Horde_Themes::img('alerts/alarm.png');
+    }
+
+    /**
+     * Notifies about an alarm through javscript.
+     *
+     * @param array $alarm  An alarm hash.
+     */
+    public function notify(array $alarm)
+    {
+        $js = sprintf('if(window.webkitNotifications&&!window.webkitNotifications.checkPermission())(function(){var notify=window.webkitNotifications.createNotification(\'%s\',\'%s\',\'%s\');notify.show();(function(){notify.cancel()}).delay(5)})()',
+                      $this->_icon,
+                      addslashes($alarm['title']),
+                      isset($alarm['text']) ? addslashes($alarm['text']) : '');
+        call_user_func($this->_jsNotify($js));
+    }
+
+    /**
+     * Returns a human readable description of the handler.
+     *
+     * @return string
+     */
+    public function getDescription()
+    {
+        return _("Desktop notification (with certain browsers)");
+    }
+}