changeset 1249:40725595aaf0

Plugins can get/set global properties to save their configuration
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 15 Dec 2014 10:20:33 +0100
parents db753e57934f
children 2ffe07abd9d8
files NEWS OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h Plugins/Engine/OrthancPlugins.cpp Plugins/OrthancCPlugin/OrthancCPlugin.h Plugins/Samples/Basic/Plugin.c
diffstat 6 files changed, 148 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Tue Dec 09 12:17:59 2014 +0100
+++ b/NEWS	Mon Dec 15 10:20:33 2014 +0100
@@ -3,6 +3,7 @@
 
 * Introspection of plugins
 * Plugins can extend Orthanc Explorer with custom JavaScript
+* Plugins can get/set global properties to save their configuration
 * Instances without PatientID are now allowed
 * Support of Tudor DICOM in Query/Retrieve
 * Fix issue 25 (AET with underscore not allowed)
--- a/OrthancServer/ServerIndex.cpp	Tue Dec 09 12:17:59 2014 +0100
+++ b/OrthancServer/ServerIndex.cpp	Mon Dec 15 10:20:33 2014 +0100
@@ -1959,6 +1959,14 @@
   }
 
 
+  void ServerIndex::SetGlobalProperty(GlobalProperty property,
+                                      const std::string& value)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    db_.SetGlobalProperty(property, value);
+  }
+
+
   std::string ServerIndex::GetGlobalProperty(GlobalProperty property,
                                              const std::string& defaultValue)
   {
--- a/OrthancServer/ServerIndex.h	Tue Dec 09 12:17:59 2014 +0100
+++ b/OrthancServer/ServerIndex.h	Mon Dec 15 10:20:33 2014 +0100
@@ -251,6 +251,9 @@
     void DeleteAttachment(const std::string& publicId,
                           FileContentType type);
 
+    void SetGlobalProperty(GlobalProperty property,
+                           const std::string& value);
+
     std::string GetGlobalProperty(GlobalProperty property,
                                   const std::string& defaultValue);
   };
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Dec 09 12:17:59 2014 +0100
+++ b/Plugins/Engine/OrthancPlugins.cpp	Mon Dec 15 10:20:33 2014 +0100
@@ -1026,11 +1026,35 @@
         return true;
       }
 
-      case _OrthancPluginService_SetProperty:
+      case _OrthancPluginService_SetPluginProperty:
+      {
+        const _OrthancPluginSetPluginProperty& p = 
+          *reinterpret_cast<const _OrthancPluginSetPluginProperty*>(parameters);
+        pimpl_->properties_[std::make_pair(p.plugin, p.property)] = p.value;
+        return true;
+      }
+
+      case _OrthancPluginService_SetGlobalProperty:
       {
-        const _OrthancPluginSetProperty& p = 
-          *reinterpret_cast<const _OrthancPluginSetProperty*>(parameters);
-        pimpl_->properties_[std::make_pair(p.plugin, p.property)] = p.value;
+        const _OrthancPluginGlobalProperty& p = 
+          *reinterpret_cast<const _OrthancPluginGlobalProperty*>(parameters);
+        if (p.property < 1024)
+        {
+          return false;
+        }
+        else
+        {
+          pimpl_->context_.GetIndex().SetGlobalProperty(static_cast<GlobalProperty>(p.property), p.value);
+          return true;
+        }
+      }
+
+      case _OrthancPluginService_GetGlobalProperty:
+      {
+        const _OrthancPluginGlobalProperty& p = 
+          *reinterpret_cast<const _OrthancPluginGlobalProperty*>(parameters);
+        std::string result = pimpl_->context_.GetIndex().GetGlobalProperty(static_cast<GlobalProperty>(p.property), p.value);
+        *(p.result) = CopyString(result);
         return true;
       }
 
--- a/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Dec 09 12:17:59 2014 +0100
+++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h	Mon Dec 15 10:20:33 2014 +0100
@@ -89,7 +89,7 @@
 
 #define ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER     0
 #define ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER     8
-#define ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER  5
+#define ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER  6
 
 
 
@@ -248,7 +248,9 @@
     _OrthancPluginService_GetOrthancPath = 4,
     _OrthancPluginService_GetOrthancDirectory = 5,
     _OrthancPluginService_GetConfigurationPath = 6,
-    _OrthancPluginService_SetProperty = 7,
+    _OrthancPluginService_SetPluginProperty = 7,
+    _OrthancPluginService_GetGlobalProperty = 8,
+    _OrthancPluginService_SetGlobalProperty = 9,
 
     /* Registration of callbacks */
     _OrthancPluginService_RegisterRestCallback = 1000,
@@ -1733,7 +1735,7 @@
     const char* plugin;
     _OrthancPluginProperty property;
     const char* value;
-  } _OrthancPluginSetProperty;
+  } _OrthancPluginSetPluginProperty;
 
 
   /**
@@ -1751,12 +1753,12 @@
     OrthancPluginContext*  context,
     const char*            uri)
   {
-    _OrthancPluginSetProperty params;
+    _OrthancPluginSetPluginProperty params;
     params.plugin = OrthancPluginGetName();
     params.property = _OrthancPluginProperty_RootUri;
     params.value = uri;
 
-    context->InvokeService(context, _OrthancPluginService_SetProperty, &params);
+    context->InvokeService(context, _OrthancPluginService_SetPluginProperty, &params);
   }
 
 
@@ -1773,12 +1775,12 @@
     OrthancPluginContext*  context,
     const char*            description)
   {
-    _OrthancPluginSetProperty params;
+    _OrthancPluginSetPluginProperty params;
     params.plugin = OrthancPluginGetName();
     params.property = _OrthancPluginProperty_Description;
     params.value = description;
 
-    context->InvokeService(context, _OrthancPluginService_SetProperty, &params);
+    context->InvokeService(context, _OrthancPluginService_SetPluginProperty, &params);
   }
 
 
@@ -1795,14 +1797,98 @@
     OrthancPluginContext*  context,
     const char*            javascript)
   {
-    _OrthancPluginSetProperty params;
+    _OrthancPluginSetPluginProperty params;
     params.plugin = OrthancPluginGetName();
     params.property = _OrthancPluginProperty_OrthancExplorer;
     params.value = javascript;
 
-    context->InvokeService(context, _OrthancPluginService_SetProperty, &params);
+    context->InvokeService(context, _OrthancPluginService_SetPluginProperty, &params);
   }
 
+
+  typedef struct
+  {
+    char**       result;
+    int32_t      property;
+    const char*  value;
+  } _OrthancPluginGlobalProperty;
+
+
+  /**
+   * @brief Get the value of a global property.
+   *
+   * Get the value of a global property that is stored in the Orthanc database. Global
+   * properties whose index is below 1024 are reserved by Orthanc.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param property The global property of interest.
+   * @param defaultValue The value to return, if the global property is unset.
+   * @return The value of the global property, or NULL in the case of an error. This
+   * string must be freed by OrthancPluginFreeString().
+   **/
+  ORTHANC_PLUGIN_INLINE char* OrthancPluginGetGlobalProperty(
+    OrthancPluginContext*  context,
+    int32_t                property,
+    const char*            defaultValue)
+  {
+    char* result;
+
+    _OrthancPluginGlobalProperty params;
+    params.result = &result;
+    params.property = property;
+    params.value = defaultValue;
+
+    if (context->InvokeService(context, _OrthancPluginService_GetGlobalProperty, &params))
+    {
+      /* Error */
+      return NULL;
+    }
+    else
+    {
+      return result;
+    }
+  }
+
+
+  /**
+   * @brief Set the value of a global property.
+   *
+   * Set the value of a global property into the Orthanc
+   * database. Setting a global property can be used by plugins to
+   * save their internal parameters. Plugins are only allowed to set
+   * properties whose index are above or equal to 1024 (properties
+   * below 1024 are read-only and reserved by Orthanc).
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param property The global property of interest.
+   * @param value The value to be set in the global property.
+   * @return 0 if success, -1 in case of error.
+   **/
+  ORTHANC_PLUGIN_INLINE int32_t OrthancPluginSetGlobalProperty(
+    OrthancPluginContext*  context,
+    int32_t                property,
+    const char*            value)
+  {
+    _OrthancPluginGlobalProperty params;
+    params.result = NULL;
+    params.property = property;
+    params.value = value;
+
+    if (context->InvokeService(context, _OrthancPluginService_SetGlobalProperty, &params))
+    {
+      /* Error */
+      return -1;
+    }
+    else
+    {
+      return 0;
+    }
+  }
+
+
+
+
+
 #ifdef  __cplusplus
 }
 #endif
--- a/Plugins/Samples/Basic/Plugin.c	Tue Dec 09 12:17:59 2014 +0100
+++ b/Plugins/Samples/Basic/Plugin.c	Mon Dec 15 10:20:33 2014 +0100
@@ -280,6 +280,7 @@
 {
   OrthancPluginMemoryBuffer tmp;
   char info[1024], *s;
+  int counter;
 
   context = c;
   OrthancPluginLogWarning(context, "Sample plugin is initializing");
@@ -340,7 +341,18 @@
   /* Play with PUT by defining a new target modality. */
   sprintf(info, "[ \"STORESCP\", \"localhost\", 2000 ]");
   OrthancPluginRestApiPut(context, &tmp, "/modalities/demo", info, strlen(info));
- 
+
+  /* Play with global properties: A global counter is incremented 
+     each time the plugin starts. */
+  s = OrthancPluginGetGlobalProperty(context, 1024, "0");
+  sscanf(s, "%d", &counter);
+  sprintf(info, "Number of times this plugin was started: %d", counter);
+  OrthancPluginLogWarning(context, info);
+  counter++;
+  sprintf(info, "%d", counter);
+  OrthancPluginSetGlobalProperty(context, 1024, info);
+  OrthancPluginFreeString(context, s);
+
   return 0;
 }