/Bindings/php/Net_Growl/Growl/Application.php

https://github.com/mutru/growl · PHP · 173 lines · 45 code · 9 blank · 119 comment · 4 complexity · 70474a0004679bdd3abc506bd2cbc320 MD5 · raw file

  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | Copyright (c) 2006, Bertrand Mansion |
  4. // | All rights reserved. |
  5. // | |
  6. // | Redistribution and use in source and binary forms, with or without |
  7. // | modification, are permitted provided that the following conditions |
  8. // | are met: |
  9. // | |
  10. // | o Redistributions of source code must retain the above copyright |
  11. // | notice, this list of conditions and the following disclaimer. |
  12. // | o Redistributions in binary form must reproduce the above copyright |
  13. // | notice, this list of conditions and the following disclaimer in the |
  14. // | documentation and/or other materials provided with the distribution.|
  15. // | o The names of the authors may not be used to endorse or promote |
  16. // | products derived from this software without specific prior written |
  17. // | permission. |
  18. // | |
  19. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  20. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  21. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  22. // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
  23. // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  24. // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
  25. // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  26. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  27. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
  28. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  29. // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
  30. // | |
  31. // +-----------------------------------------------------------------------+
  32. // | Author: Bertrand Mansion <golgote@mamasam.com> |
  33. // +-----------------------------------------------------------------------+
  34. //
  35. // $Id $
  36. /**
  37. * Application object for {@link Net_Growl}
  38. *
  39. * This object represents an application containing the notifications
  40. * to be registered by {@link http://growl.info Growl}. Feel free to use
  41. * your own application object as long as it implements the few public
  42. * getter methods:
  43. * - {@link Net_Growl_Application::getGrowlNotifications()}
  44. * - {@link Net_Growl_Application::getGrowlName()}
  45. * - {@link Net_Growl_Application::getGrowlPassword()}
  46. *
  47. * @author Bertrand Mansion <golgote@mamasam.com>
  48. * @copyright 2006
  49. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  50. * @package Net_Growl
  51. * @link http://growl.info Growl Homepage
  52. */
  53. class Net_Growl_Application
  54. {
  55. /**
  56. * Name of application to be registered by Growl
  57. * @var string
  58. * @access private
  59. */
  60. var $_growlAppName;
  61. /**
  62. * Password for notifications
  63. * @var string
  64. * @access private
  65. */
  66. var $_growlAppPassword = '';
  67. /**
  68. * Array of notifications
  69. * @var array
  70. * @access private
  71. */
  72. var $_growlNotifications = array();
  73. /**
  74. * Constructor
  75. * Constructs a new application to be registered by Growl
  76. *
  77. * @param string Application name
  78. * @param array Array of notifications
  79. * @param string Password to be used to notify Growl
  80. * @access public
  81. * @see Net_Growl_Application::addGrowlNotifications()
  82. */
  83. function Net_Growl_Application($appName, $notifications, $password = '')
  84. {
  85. $this->_growlAppName = $appName;
  86. $this->_growlAppPassword = (empty($password)) ? '' : $password;
  87. if (!empty($notifications) && is_array($notifications)) {
  88. $this->addGrowlNotifications($notifications);
  89. }
  90. }
  91. /**
  92. * Adds notifications supported by this application
  93. *
  94. * Expected array format is:
  95. * <pre>
  96. * array('notification name' => array('option name' => 'option value'))
  97. * </pre>
  98. * At the moment, only option name 'enabled' is supported. Example:
  99. * <code>
  100. * $notifications = array('Test Notification' => array('enabled' => true));
  101. * </code>
  102. *
  103. * @access public
  104. * @param array Array of notifications to support
  105. */
  106. function addGrowlNotifications($notifications)
  107. {
  108. $default = $this->_getGrowlNotificationDefaultOptions();
  109. foreach ($notifications as $name => $options) {
  110. if (is_int($name)) {
  111. $name = $options;
  112. $options = $default;
  113. } elseif (!empty($options) && is_array($options)) {
  114. $options = array_merge($default, $options);
  115. }
  116. $this->_growlNotifications[$name] = $options;
  117. }
  118. }
  119. function _getGrowlNotificationDefaultOptions()
  120. {
  121. return array('enabled' => true);
  122. }
  123. /**
  124. * Returns the notifications accepted by Growl for this application
  125. *
  126. * Expected array format is:
  127. * <pre>
  128. * array('notification name' => array('option name' => 'option value'))
  129. * </pre>
  130. * At the moment, only option name 'enabled' is supported. Example:
  131. * <code>
  132. * $notifications = array('Test Notification' => array('enabled' => true));
  133. * return $notifications;
  134. * </code>
  135. *
  136. * @access public
  137. * @return array notifications
  138. */
  139. function &getGrowlNotifications()
  140. {
  141. return $this->_growlNotifications;
  142. }
  143. /**
  144. * Returns the application name for registration in Growl
  145. *
  146. * @access public
  147. * @return string application name
  148. */
  149. function getGrowlName()
  150. {
  151. return $this->_growlAppName;
  152. }
  153. /**
  154. * Returns the password to be used by Growl to accept notification packets
  155. *
  156. * @access public
  157. * @return string password
  158. */
  159. function getGrowlPassword()
  160. {
  161. return $this->_growlAppPassword;
  162. }
  163. }
  164. ?>