]> source.dussan.org Git - nextcloud-server.git/commitdiff
Allow to register settings/types via info.xml
authorJoas Schilling <coding@schilljs.com>
Tue, 25 Oct 2016 16:00:25 +0000 (18:00 +0200)
committerJoas Schilling <coding@schilljs.com>
Wed, 16 Nov 2016 08:25:44 +0000 (09:25 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Activity/LegacySetting.php [new file with mode: 0644]
lib/private/Activity/Manager.php
lib/private/App/InfoParser.php
lib/private/legacy/app.php
lib/public/Activity/IManager.php
lib/public/Activity/ISetting.php [new file with mode: 0644]

diff --git a/lib/private/Activity/LegacySetting.php b/lib/private/Activity/LegacySetting.php
new file mode 100644 (file)
index 0000000..eb65e68
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Activity;
+
+use OCP\Activity\ISetting;
+
+class LegacySetting implements ISetting {
+
+       /** @var string */
+       protected $identifier;
+       /** @var string */
+       protected $name;
+       /** @var bool */
+       protected $canChangeStream;
+       /** @var bool */
+       protected $isDefaultEnabledStream;
+       /** @var bool */
+       protected $canChangeMail;
+       /** @var bool */
+       protected $isDefaultEnabledMail;
+
+       /**
+        * LegacySetting constructor.
+        *
+        * @param string $identifier
+        * @param string $name
+        * @param bool $canChangeStream
+        * @param bool $isDefaultEnabledStream
+        * @param bool $canChangeMail
+        * @param bool $isDefaultEnabledMail
+        */
+       public function __construct($identifier,
+                                                               $name,
+                                                               $canChangeStream,
+                                                               $isDefaultEnabledStream,
+                                                               $canChangeMail,
+                                                               $isDefaultEnabledMail) {
+               $this->identifier = $identifier;
+               $this->name = $name;
+               $this->canChangeStream = $canChangeStream;
+               $this->isDefaultEnabledStream = $isDefaultEnabledStream;
+               $this->canChangeMail = $canChangeMail;
+               $this->isDefaultEnabledMail = $isDefaultEnabledMail;
+       }
+
+       /**
+        * @return string Lowercase a-z and underscore only identifier
+        * @since 9.2.0
+        */
+       public function getIdentifier() {
+               return $this->identifier;
+       }
+
+       /**
+        * @return string A translated string
+        * @since 9.2.0
+        */
+       public function getName() {
+               return $this->name;
+       }
+
+       /**
+        * @return int whether the filter should be rather on the top or bottom of
+        * the admin section. The filters are arranged in ascending order of the
+        * priority values. It is required to return a value between 0 and 100.
+        * @since 9.2.0
+        */
+       public function getPriority() {
+               return 70;
+       }
+
+       /**
+        * @return bool True when the option can be changed for the stream
+        * @since 9.2.0
+        */
+       public function canChangeStream() {
+               return $this->canChangeStream;
+       }
+
+       /**
+        * @return bool True when the option can be changed for the stream
+        * @since 9.2.0
+        */
+       public function isDefaultEnabledStream() {
+               return $this->isDefaultEnabledStream;
+       }
+
+       /**
+        * @return bool True when the option can be changed for the mail
+        * @since 9.2.0
+        */
+       public function canChangeMail() {
+               return $this->canChangeMail;
+       }
+
+       /**
+        * @return bool True when the option can be changed for the stream
+        * @since 9.2.0
+        */
+       public function isDefaultEnabledMail() {
+               return $this->isDefaultEnabledMail;
+       }
+}
+
index a18dace88ee27c311a61857be04a14d178afc6d1..60a4a935898d9cc0633353ad46062d375f356389 100644 (file)
@@ -29,6 +29,7 @@ use OCP\Activity\IEvent;
 use OCP\Activity\IExtension;
 use OCP\Activity\IFilter;
 use OCP\Activity\IManager;
+use OCP\Activity\ISetting;
 use OCP\IConfig;
 use OCP\IRequest;
 use OCP\IUser;
@@ -246,7 +247,7 @@ class Manager implements IManager {
                $this->extensions = [];
        }
 
-       /** @var IFilter[] */
+       /** @var string[] */
        protected $filterClasses;
 
        /** @var IFilter[] */
@@ -316,49 +317,82 @@ class Manager implements IManager {
                throw new \InvalidArgumentException('Requested filter does not exist');
        }
 
+       /** @var string[] */
+       protected $settingsClasses;
+
+       /** @var ISetting[] */
+       protected $settings;
+
+       /** @var bool */
+       protected $loadedLegacyTypes = false;
+
        /**
-        * Will return additional notification types as specified by other apps
-        *
-        * @param string $languageCode
-        * @return array
+        * @param string $setting Class must implement OCA\Activity\ISetting
+        * @return void
         */
-       public function getNotificationTypes($languageCode) {
-               $filesNotificationTypes = [];
-               $sharingNotificationTypes = [];
+       public function registerSetting($setting) {
+               $this->settingsClasses[$setting] = false;
+       }
 
-               $notificationTypes = array();
-               foreach ($this->getExtensions() as $c) {
-                       $result = $c->getNotificationTypes($languageCode);
-                       if (is_array($result)) {
-                               if (class_exists('\OCA\Files\Activity', false) && $c instanceof \OCA\Files\Activity) {
-                                       $filesNotificationTypes = $result;
-                                       continue;
-                               }
-                               if (class_exists('\OCA\Files_Sharing\Activity', false) && $c instanceof \OCA\Files_Sharing\Activity) {
-                                       $sharingNotificationTypes = $result;
-                                       continue;
+       /**
+        * @return ISetting[]
+        * @throws \InvalidArgumentException
+        */
+       public function getSettings() {
+               if (!$this->loadedLegacyTypes) {
+                       $l = \OC::$server->getL10N('core');
+                       $legacyTypes = $this->getNotificationTypes($l->getLanguageCode());
+                       $streamTypes = $this->getDefaultTypes(IExtension::METHOD_STREAM);
+                       $mailTypes = $this->getDefaultTypes(IExtension::METHOD_MAIL);
+                       foreach ($legacyTypes as $type => $data) {
+                               if (is_string($data)) {
+                                       $desc = $data;
+                                       $canChangeStream = true;
+                                       $canChangeMail = true;
+                               } else {
+                                       $desc = $data['desc'];
+                                       $canChangeStream = in_array(IExtension::METHOD_STREAM, $data['methods']);
+                                       $canChangeMail = in_array(IExtension::METHOD_MAIL, $data['methods']);
                                }
 
-                               $notificationTypes = array_merge($notificationTypes, $result);
+                               $this->settings[$type] = new LegacySetting(
+                                       $type, $desc,
+                                       $canChangeStream, in_array($type, $streamTypes),
+                                       $canChangeMail, in_array($type, $mailTypes)
+                               );
                        }
+                       $this->loadedLegacyTypes = true;
                }
 
-               return array_merge($filesNotificationTypes, $sharingNotificationTypes, $notificationTypes);
+               foreach ($this->settingsClasses as $class => $false) {
+                       /** @var ISetting $setting */
+                       $setting = \OC::$server->query($class);
+
+                       if (!$setting instanceof ISetting) {
+                               throw new \InvalidArgumentException('Invalid activity filter registered');
+                       }
+
+                       $this->settings[$setting->getIdentifier()] = $setting;
+
+                       unset($this->settingsClasses[$class]);
+               }
+               return $this->settings;
        }
 
        /**
-        * @param string $method
-        * @return array
+        * @param string $id
+        * @return ISetting
+        * @throws \InvalidArgumentException when the setting was not found
+        * @since 9.2.0
         */
-       public function getDefaultTypes($method) {
-               $defaultTypes = array();
-               foreach ($this->getExtensions() as $c) {
-                       $types = $c->getDefaultTypes($method);
-                       if (is_array($types)) {
-                               $defaultTypes = array_merge($types, $defaultTypes);
-                       }
+       public function getSettingById($id) {
+               $settings = $this->getSettings();
+
+               if (isset($settings[$id])) {
+                       return $settings[$id];
                }
-               return $defaultTypes;
+
+               throw new \InvalidArgumentException('Requested setting does not exist');
        }
 
        /**
@@ -606,4 +640,44 @@ class Manager implements IManager {
 
                return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
        }
+
+       /**
+        * Will return additional notification types as specified by other apps
+        *
+        * @param string $languageCode
+        * @return array
+        * @deprecated 9.2.0 - Use getSettings() instead
+        */
+       public function getNotificationTypes($languageCode) {
+               $notificationTypes = $sharingNotificationTypes = [];
+               foreach ($this->getExtensions() as $c) {
+                       $result = $c->getNotificationTypes($languageCode);
+                       if (is_array($result)) {
+                               if (class_exists('\OCA\Files_Sharing\Activity', false) && $c instanceof \OCA\Files_Sharing\Activity) {
+                                       $sharingNotificationTypes = $result;
+                                       continue;
+                               }
+
+                               $notificationTypes = array_merge($notificationTypes, $result);
+                       }
+               }
+
+               return array_merge($sharingNotificationTypes, $notificationTypes);
+       }
+
+       /**
+        * @param string $method
+        * @return array
+        * @deprecated 9.2.0 - Use getSettings()->isDefaulEnabled<method>() instead
+        */
+       public function getDefaultTypes($method) {
+               $defaultTypes = array();
+               foreach ($this->getExtensions() as $c) {
+                       $types = $c->getDefaultTypes($method);
+                       if (is_array($types)) {
+                               $defaultTypes = array_merge($types, $defaultTypes);
+                       }
+               }
+               return $defaultTypes;
+       }
 }
index 2a6909c484c19440a68080f923334da6657a9f68..6fc1ef47b09485fcdbd3a82511f9f277fe889935 100644 (file)
@@ -116,6 +116,9 @@ class InfoParser {
                if (!array_key_exists('filters', $array['activity'])) {
                        $array['activity']['filters'] = [];
                }
+               if (!array_key_exists('settings', $array['activity'])) {
+                       $array['activity']['settings'] = [];
+               }
 
                if (array_key_exists('types', $array)) {
                        if (is_array($array['types'])) {
@@ -153,6 +156,9 @@ class InfoParser {
                if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
                        $array['activity']['filters'] = $array['activity']['filters']['filter'];
                }
+               if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
+                       $array['activity']['settings'] = $array['activity']['settings']['setting'];
+               }
 
                if(!is_null($this->cache)) {
                        $this->cache->set($fileCacheKey, json_encode($array));
index 746dcd4f7ccc842965382d177b7dbfe92581a6e4..156985d0c5a92ffdf4ecea4ac78113fdce174b45 100644 (file)
@@ -169,6 +169,11 @@ class OC_App {
                                \OC::$server->getActivityManager()->registerFilter($filter);
                        }
                }
+               if (!empty($info['activity']['settings'])) {
+                       foreach ($info['activity']['settings'] as $setting) {
+                               \OC::$server->getActivityManager()->registerSetting($setting);
+                       }
+               }
        }
 
        /**
index 387a1d8514415b4675aeee8220505a8f17e9009d..ded83bcc922b80b813291672690c9e6d4f26b9b9 100644 (file)
@@ -131,6 +131,27 @@ interface IManager {
         */
        public function getFilterById($id);
 
+       /**
+        * @param string $setting Class must implement OCA\Activity\ISetting
+        * @return void
+        * @since 9.2.0
+        */
+       public function registerSetting($setting);
+
+       /**
+        * @return ISetting[]
+        * @since 9.2.0
+        */
+       public function getSettings();
+
+       /**
+        * @param string $id
+        * @return ISetting
+        * @throws \InvalidArgumentException when the setting was not found
+        * @since 9.2.0
+        */
+       public function getSettingById($id);
+
        /**
         * Will return additional notification types as specified by other apps
         *
@@ -141,6 +162,7 @@ interface IManager {
         *                                      'methods' => [\OCP\Activity\IExtension::METHOD_*],
         *                              ]
         * @since 8.0.0 - 8.2.0: Added support to allow limiting notifications to certain methods
+        * @deprecated 9.2.0 - Use getSettings() instead
         */
        public function getNotificationTypes($languageCode);
 
@@ -148,6 +170,7 @@ interface IManager {
         * @param string $method
         * @return array
         * @since 8.0.0
+        * @deprecated 9.2.0 - Use getSettings()->isDefaulEnabled<method>() instead
         */
        public function getDefaultTypes($method);
 
diff --git a/lib/public/Activity/ISetting.php b/lib/public/Activity/ISetting.php
new file mode 100644 (file)
index 0000000..b90616e
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Activity;
+
+/**
+ * Interface ISetting
+ *
+ * @package OCP\Activity
+ * @since 9.2.0
+ */
+interface ISetting {
+
+       /**
+        * @return string Lowercase a-z and underscore only identifier
+        * @since 9.2.0
+        */
+       public function getIdentifier();
+
+       /**
+        * @return string A translated string
+        * @since 9.2.0
+        */
+       public function getName();
+
+       /**
+        * @return int whether the filter should be rather on the top or bottom of
+        * the admin section. The filters are arranged in ascending order of the
+        * priority values. It is required to return a value between 0 and 100.
+        * @since 9.2.0
+        */
+       public function getPriority();
+
+       /**
+        * @return bool True when the option can be changed for the stream
+        * @since 9.2.0
+        */
+       public function canChangeStream();
+
+       /**
+        * @return bool True when the option can be changed for the stream
+        * @since 9.2.0
+        */
+       public function isDefaultEnabledStream();
+
+       /**
+        * @return bool True when the option can be changed for the mail
+        * @since 9.2.0
+        */
+       public function canChangeMail();
+
+       /**
+        * @return bool True when the option can be changed for the stream
+        * @since 9.2.0
+        */
+       public function isDefaultEnabledMail();
+}
+