annotate Sources/StorageArea.cpp @ 79:068551520123

New Python function: "orthanc.RegisterStorageArea()"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 12 Aug 2021 17:52:17 +0200
parents
children ad31ac9a6fef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
79
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Python plugin for Orthanc
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2020-2021 Osimis S.A., Belgium
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 *
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * modify it under the terms of the GNU Affero General Public License
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * as published by the Free Software Foundation, either version 3 of
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * the License, or (at your option) any later version.
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 *
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful, but
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * Affero General Public License for more details.
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 *
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU Affero General Public License
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 **/
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 #include "StorageArea.h"
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "PythonString.h"
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 static PyObject* createCallback_ = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 static PyObject* readCallback_ = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 static PyObject* removeCallback_ = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 static OrthancPluginErrorCode RunCallback(PythonLock& lock,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 PyObject* callback,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 PythonObject& args,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 const std::string& name)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 PythonObject result(lock, PyObject_CallObject(callback, args.GetPyObject()));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 std::string traceback;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 if (lock.HasErrorOccurred(traceback))
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 OrthancPlugins::LogError("Error in the Python " + name + " callback, traceback:\n" + traceback);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 return OrthancPluginErrorCode_Plugin;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 else
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 return OrthancPluginErrorCode_Success;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 static OrthancPluginErrorCode StorageCreate(const char *uuid,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 const void *content,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 int64_t size,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 OrthancPluginContentType type)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 try
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 if (createCallback_ == NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InternalError);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 PythonLock lock;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 PythonObject args(lock, PyTuple_New(3));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 PythonString str(lock, uuid);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 PyTuple_SetItem(args.GetPyObject(), 0, str.Release());
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 PyTuple_SetItem(args.GetPyObject(), 1, PyLong_FromLong(type));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 PyTuple_SetItem(args.GetPyObject(), 2, PyBytes_FromStringAndSize(reinterpret_cast<const char*>(content), size));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 return RunCallback(lock, createCallback_, args, "StorageCreate");
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 catch (OrthancPlugins::PluginException& e)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 return e.GetErrorCode();
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 static OrthancPluginErrorCode StorageRead(void **content,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 int64_t *size,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 const char *uuid,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 OrthancPluginContentType type)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 try
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 if (readCallback_ == NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InternalError);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 PythonLock lock;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 PythonObject args(lock, PyTuple_New(2));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 PythonString str(lock, uuid);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 PyTuple_SetItem(args.GetPyObject(), 0, str.Release());
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 PyTuple_SetItem(args.GetPyObject(), 1, PyLong_FromLong(type));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 PythonObject result(lock, PyObject_CallObject(readCallback_, args.GetPyObject()));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 std::string traceback;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 if (lock.HasErrorOccurred(traceback))
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 OrthancPlugins::LogError("Error in the Python StorageRead callback, traceback:\n" + traceback);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 return OrthancPluginErrorCode_Plugin;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 else if (!PyBytes_Check(result.GetPyObject()))
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 OrthancPlugins::LogError("The Python StorageRead callback has not returned a byte array as expected");
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 return OrthancPluginErrorCode_Plugin;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 else
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 char* pythonBuffer = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 Py_ssize_t pythonSize = 0;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 if (PyBytes_AsStringAndSize(result.GetPyObject(), &pythonBuffer, &pythonSize) == 1)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 OrthancPlugins::LogError("Cannot access the byte buffer returned by the Python StorageRead callback");
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 return OrthancPluginErrorCode_Plugin;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 else
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if (pythonSize == 0)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 *content = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 *size = 0;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 else
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 *content = malloc(pythonSize);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 *size = pythonSize;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 if (*content == NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 return OrthancPluginErrorCode_NotEnoughMemory;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 memcpy(*content, pythonBuffer, pythonSize);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 return OrthancPluginErrorCode_Success;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 catch (OrthancPlugins::PluginException& e)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 return e.GetErrorCode();
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 static OrthancPluginErrorCode StorageRemove(const char *uuid,
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 OrthancPluginContentType type)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 try
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 if (removeCallback_ == NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InternalError);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 PythonLock lock;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 PythonObject args(lock, PyTuple_New(2));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 PythonString str(lock, uuid);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 PyTuple_SetItem(args.GetPyObject(), 0, str.Release());
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 PyTuple_SetItem(args.GetPyObject(), 1, PyLong_FromLong(type));
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 return RunCallback(lock, removeCallback_, args, "StorageRemove");
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 catch (OrthancPlugins::PluginException& e)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 return e.GetErrorCode();
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 PyObject* RegisterStorageArea(PyObject* module, PyObject* args)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 // The GIL is locked at this point (no need to create "PythonLock")
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 PyObject* a = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 PyObject* b = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 PyObject* c = NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 if (!PyArg_ParseTuple(args, "OOO", &a, &b, &c) ||
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 a == NULL ||
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 b == NULL ||
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 c == NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 PyErr_SetString(PyExc_ValueError, "Expected three callback functions to register a custom storage area");
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 return NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 else if (createCallback_ != NULL ||
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 readCallback_ != NULL ||
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 removeCallback_ != NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 PyErr_SetString(PyExc_RuntimeError, "Cannot register twice a custom storage area");
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 return NULL;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 else
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 OrthancPlugins::LogInfo("Registering a custom storage area in Python");
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 OrthancPluginRegisterStorageArea(OrthancPlugins::GetGlobalContext(),
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 StorageCreate, StorageRead, StorageRemove);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 createCallback_ = a;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 Py_XINCREF(createCallback_);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 readCallback_ = b;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 Py_XINCREF(readCallback_);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 removeCallback_ = c;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 Py_XINCREF(removeCallback_);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 Py_INCREF(Py_None);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 return Py_None;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 void FinalizeStorageArea()
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 PythonLock lock;
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 if (createCallback_ != NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 Py_XDECREF(createCallback_);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 if (readCallback_ != NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 Py_XDECREF(readCallback_);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 if (removeCallback_ != NULL)
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 Py_XDECREF(removeCallback_);
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 }
068551520123 New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 }