comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 2958:bb7a66efbeb1

OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Dec 2018 16:31:29 +0100
parents 859e880ac9a8
children e361df74639f
comparison
equal deleted inserted replaced
2957:ccf61f6e22ef 2958:bb7a66efbeb1
37 #include <json/writer.h> 37 #include <json/writer.h>
38 38
39 39
40 namespace OrthancPlugins 40 namespace OrthancPlugins
41 { 41 {
42 static OrthancPluginContext* globalContext_ = NULL;
43
44
45 void SetGlobalContext(OrthancPluginContext* context)
46 {
47 if (context == NULL)
48 {
49 ORTHANC_PLUGINS_THROW_EXCEPTION(NullPointer);
50 }
51 else if (globalContext_ == NULL)
52 {
53 globalContext_ = context;
54 }
55 else
56 {
57 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls);
58 }
59 }
60
61
62 bool HasGlobalContext()
63 {
64 return globalContext_ != NULL;
65 }
66
67
68 OrthancPluginContext* GetGlobalContext()
69 {
70 if (globalContext_ == NULL)
71 {
72 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls);
73 }
74 else
75 {
76 return globalContext_;
77 }
78 }
79
80
42 void MemoryBuffer::Check(OrthancPluginErrorCode code) 81 void MemoryBuffer::Check(OrthancPluginErrorCode code)
43 { 82 {
44 if (code != OrthancPluginErrorCode_Success) 83 if (code != OrthancPluginErrorCode_Success)
45 { 84 {
46 // Prevent using garbage information 85 // Prevent using garbage information
74 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); 113 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
75 } 114 }
76 } 115 }
77 116
78 117
79 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) : 118 MemoryBuffer::MemoryBuffer()
80 context_(context)
81 { 119 {
82 buffer_.data = NULL; 120 buffer_.data = NULL;
83 buffer_.size = 0; 121 buffer_.size = 0;
84 } 122 }
85 123
86 124
87 void MemoryBuffer::Clear() 125 void MemoryBuffer::Clear()
88 { 126 {
89 if (buffer_.data != NULL) 127 if (buffer_.data != NULL)
90 { 128 {
91 OrthancPluginFreeMemoryBuffer(context_, &buffer_); 129 OrthancPluginFreeMemoryBuffer(GetGlobalContext(), &buffer_);
92 buffer_.data = NULL; 130 buffer_.data = NULL;
93 buffer_.size = 0; 131 buffer_.size = 0;
94 } 132 }
95 } 133 }
96 134
142 const char* tmp = reinterpret_cast<const char*>(buffer_.data); 180 const char* tmp = reinterpret_cast<const char*>(buffer_.data);
143 181
144 Json::Reader reader; 182 Json::Reader reader;
145 if (!reader.parse(tmp, tmp + buffer_.size, target)) 183 if (!reader.parse(tmp, tmp + buffer_.size, target))
146 { 184 {
147 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON"); 185 LogError("Cannot convert some memory buffer to JSON");
148 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 186 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
149 } 187 }
150 } 188 }
151 189
152 190
155 { 193 {
156 Clear(); 194 Clear();
157 195
158 if (applyPlugins) 196 if (applyPlugins)
159 { 197 {
160 return CheckHttp(OrthancPluginRestApiGetAfterPlugins(context_, &buffer_, uri.c_str())); 198 return CheckHttp(OrthancPluginRestApiGetAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str()));
161 } 199 }
162 else 200 else
163 { 201 {
164 return CheckHttp(OrthancPluginRestApiGet(context_, &buffer_, uri.c_str())); 202 return CheckHttp(OrthancPluginRestApiGet(GetGlobalContext(), &buffer_, uri.c_str()));
165 } 203 }
166 } 204 }
167 205
168 206
169 bool MemoryBuffer::RestApiPost(const std::string& uri, 207 bool MemoryBuffer::RestApiPost(const std::string& uri,
173 { 211 {
174 Clear(); 212 Clear();
175 213
176 if (applyPlugins) 214 if (applyPlugins)
177 { 215 {
178 return CheckHttp(OrthancPluginRestApiPostAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize)); 216 return CheckHttp(OrthancPluginRestApiPostAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize));
179 } 217 }
180 else 218 else
181 { 219 {
182 return CheckHttp(OrthancPluginRestApiPost(context_, &buffer_, uri.c_str(), body, bodySize)); 220 return CheckHttp(OrthancPluginRestApiPost(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize));
183 } 221 }
184 } 222 }
185 223
186 224
187 bool MemoryBuffer::RestApiPut(const std::string& uri, 225 bool MemoryBuffer::RestApiPut(const std::string& uri,
191 { 229 {
192 Clear(); 230 Clear();
193 231
194 if (applyPlugins) 232 if (applyPlugins)
195 { 233 {
196 return CheckHttp(OrthancPluginRestApiPutAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize)); 234 return CheckHttp(OrthancPluginRestApiPutAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize));
197 } 235 }
198 else 236 else
199 { 237 {
200 return CheckHttp(OrthancPluginRestApiPut(context_, &buffer_, uri.c_str(), body, bodySize)); 238 return CheckHttp(OrthancPluginRestApiPut(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize));
201 } 239 }
202 } 240 }
203 241
204 242
205 bool MemoryBuffer::RestApiPost(const std::string& uri, 243 bool MemoryBuffer::RestApiPost(const std::string& uri,
226 Clear(); 264 Clear();
227 265
228 Json::FastWriter writer; 266 Json::FastWriter writer;
229 std::string s = writer.write(tags); 267 std::string s = writer.write(tags);
230 268
231 Check(OrthancPluginCreateDicom(context_, &buffer_, s.c_str(), NULL, flags)); 269 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), NULL, flags));
232 } 270 }
233 271
234 void MemoryBuffer::CreateDicom(const Json::Value& tags, 272 void MemoryBuffer::CreateDicom(const Json::Value& tags,
235 const OrthancImage& pixelData, 273 const OrthancImage& pixelData,
236 OrthancPluginCreateDicomFlags flags) 274 OrthancPluginCreateDicomFlags flags)
238 Clear(); 276 Clear();
239 277
240 Json::FastWriter writer; 278 Json::FastWriter writer;
241 std::string s = writer.write(tags); 279 std::string s = writer.write(tags);
242 280
243 Check(OrthancPluginCreateDicom(context_, &buffer_, s.c_str(), pixelData.GetObject(), flags)); 281 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), pixelData.GetObject(), flags));
244 } 282 }
245 283
246 284
247 void MemoryBuffer::ReadFile(const std::string& path) 285 void MemoryBuffer::ReadFile(const std::string& path)
248 { 286 {
249 Clear(); 287 Clear();
250 Check(OrthancPluginReadFile(context_, &buffer_, path.c_str())); 288 Check(OrthancPluginReadFile(GetGlobalContext(), &buffer_, path.c_str()));
251 } 289 }
252 290
253 291
254 void MemoryBuffer::GetDicomQuery(const OrthancPluginWorklistQuery* query) 292 void MemoryBuffer::GetDicomQuery(const OrthancPluginWorklistQuery* query)
255 { 293 {
256 Clear(); 294 Clear();
257 Check(OrthancPluginWorklistGetDicomQuery(context_, &buffer_, query)); 295 Check(OrthancPluginWorklistGetDicomQuery(GetGlobalContext(), &buffer_, query));
258 } 296 }
259 297
260 298
261 void OrthancString::Assign(char* str) 299 void OrthancString::Assign(char* str)
262 { 300 {
274 312
275 void OrthancString::Clear() 313 void OrthancString::Clear()
276 { 314 {
277 if (str_ != NULL) 315 if (str_ != NULL)
278 { 316 {
279 OrthancPluginFreeString(context_, str_); 317 OrthancPluginFreeString(GetGlobalContext(), str_);
280 str_ = NULL; 318 str_ = NULL;
281 } 319 }
282 } 320 }
283 321
284 322
297 335
298 void OrthancString::ToJson(Json::Value& target) const 336 void OrthancString::ToJson(Json::Value& target) const
299 { 337 {
300 if (str_ == NULL) 338 if (str_ == NULL)
301 { 339 {
302 OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON"); 340 LogError("Cannot convert an empty memory buffer to JSON");
303 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 341 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
304 } 342 }
305 343
306 Json::Reader reader; 344 Json::Reader reader;
307 if (!reader.parse(str_, target)) 345 if (!reader.parse(str_, target))
308 { 346 {
309 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON"); 347 LogError("Cannot convert some memory buffer to JSON");
310 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 348 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
311 } 349 }
312 } 350 }
313 351
314 352
315 void MemoryBuffer::DicomToJson(Json::Value& target, 353 void MemoryBuffer::DicomToJson(Json::Value& target,
316 OrthancPluginDicomToJsonFormat format, 354 OrthancPluginDicomToJsonFormat format,
317 OrthancPluginDicomToJsonFlags flags, 355 OrthancPluginDicomToJsonFlags flags,
318 uint32_t maxStringLength) 356 uint32_t maxStringLength)
319 { 357 {
320 OrthancString str(context_); 358 OrthancString str;
321 str.Assign(OrthancPluginDicomBufferToJson(context_, GetData(), GetSize(), format, flags, maxStringLength)); 359 str.Assign(OrthancPluginDicomBufferToJson
360 (GetGlobalContext(), GetData(), GetSize(), format, flags, maxStringLength));
322 str.ToJson(target); 361 str.ToJson(target);
323 } 362 }
324 363
325 364
326 bool MemoryBuffer::HttpGet(const std::string& url, 365 bool MemoryBuffer::HttpGet(const std::string& url,
327 const std::string& username, 366 const std::string& username,
328 const std::string& password) 367 const std::string& password)
329 { 368 {
330 Clear(); 369 Clear();
331 return CheckHttp(OrthancPluginHttpGet(context_, &buffer_, url.c_str(), 370 return CheckHttp(OrthancPluginHttpGet(GetGlobalContext(), &buffer_, url.c_str(),
332 username.empty() ? NULL : username.c_str(), 371 username.empty() ? NULL : username.c_str(),
333 password.empty() ? NULL : password.c_str())); 372 password.empty() ? NULL : password.c_str()));
334 } 373 }
335 374
336 375
338 const std::string& body, 377 const std::string& body,
339 const std::string& username, 378 const std::string& username,
340 const std::string& password) 379 const std::string& password)
341 { 380 {
342 Clear(); 381 Clear();
343 return CheckHttp(OrthancPluginHttpPost(context_, &buffer_, url.c_str(), 382 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(),
344 body.c_str(), body.size(), 383 body.c_str(), body.size(),
345 username.empty() ? NULL : username.c_str(), 384 username.empty() ? NULL : username.c_str(),
346 password.empty() ? NULL : password.c_str())); 385 password.empty() ? NULL : password.c_str()));
347 } 386 }
348 387
351 const std::string& body, 390 const std::string& body,
352 const std::string& username, 391 const std::string& username,
353 const std::string& password) 392 const std::string& password)
354 { 393 {
355 Clear(); 394 Clear();
356 return CheckHttp(OrthancPluginHttpPut(context_, &buffer_, url.c_str(), 395 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(),
357 body.empty() ? NULL : body.c_str(), 396 body.empty() ? NULL : body.c_str(),
358 body.size(), 397 body.size(),
359 username.empty() ? NULL : username.c_str(), 398 username.empty() ? NULL : username.c_str(),
360 password.empty() ? NULL : password.c_str())); 399 password.empty() ? NULL : password.c_str()));
361 } 400 }
362 401
363 402
364 void MemoryBuffer::GetDicomInstance(const std::string& instanceId) 403 void MemoryBuffer::GetDicomInstance(const std::string& instanceId)
365 { 404 {
366 Clear(); 405 Clear();
367 Check(OrthancPluginGetDicomForInstance(context_, &buffer_, instanceId.c_str())); 406 Check(OrthancPluginGetDicomForInstance(GetGlobalContext(), &buffer_, instanceId.c_str()));
368 } 407 }
369 408
370 409
371 bool HttpDelete(OrthancPluginContext* context_, 410 bool HttpDelete(const std::string& url,
372 const std::string& url,
373 const std::string& username, 411 const std::string& username,
374 const std::string& password) 412 const std::string& password)
375 { 413 {
376 OrthancPluginErrorCode error = OrthancPluginHttpDelete 414 OrthancPluginErrorCode error = OrthancPluginHttpDelete
377 (context_, url.c_str(), 415 (GetGlobalContext(), url.c_str(),
378 username.empty() ? NULL : username.c_str(), 416 username.empty() ? NULL : username.c_str(),
379 password.empty() ? NULL : password.c_str()); 417 password.empty() ? NULL : password.c_str());
380 418
381 if (error == OrthancPluginErrorCode_Success) 419 if (error == OrthancPluginErrorCode_Success)
382 { 420 {
392 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); 430 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
393 } 431 }
394 } 432 }
395 433
396 434
397 OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) : 435 void LogError(const std::string& message)
398 context_(context) 436 {
399 { 437 if (HasGlobalContext())
400 OrthancString str(context); 438 {
401 str.Assign(OrthancPluginGetConfiguration(context)); 439 OrthancPluginLogError(GetGlobalContext(), message.c_str());
440 }
441 }
442
443
444 void LogWarning(const std::string& message)
445 {
446 if (HasGlobalContext())
447 {
448 OrthancPluginLogWarning(GetGlobalContext(), message.c_str());
449 }
450 }
451
452
453 void LogInfo(const std::string& message)
454 {
455 if (HasGlobalContext())
456 {
457 OrthancPluginLogInfo(GetGlobalContext(), message.c_str());
458 }
459 }
460
461
462 OrthancConfiguration::OrthancConfiguration()
463 {
464 OrthancString str;
465 str.Assign(OrthancPluginGetConfiguration(GetGlobalContext()));
402 466
403 if (str.GetContent() == NULL) 467 if (str.GetContent() == NULL)
404 { 468 {
405 OrthancPluginLogError(context, "Cannot access the Orthanc configuration"); 469 LogError("Cannot access the Orthanc configuration");
406 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 470 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
407 } 471 }
408 472
409 str.ToJson(configuration_); 473 str.ToJson(configuration_);
410 474
411 if (configuration_.type() != Json::objectValue) 475 if (configuration_.type() != Json::objectValue)
412 { 476 {
413 OrthancPluginLogError(context, "Unable to read the Orthanc configuration"); 477 LogError("Unable to read the Orthanc configuration");
414 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 478 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
415 }
416 }
417
418
419 OrthancPluginContext* OrthancConfiguration::GetContext() const
420 {
421 if (context_ == NULL)
422 {
423 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
424 }
425 else
426 {
427 return context_;
428 } 479 }
429 } 480 }
430 481
431 482
432 std::string OrthancConfiguration::GetPath(const std::string& key) const 483 std::string OrthancConfiguration::GetPath(const std::string& key) const
454 void OrthancConfiguration::GetSection(OrthancConfiguration& target, 505 void OrthancConfiguration::GetSection(OrthancConfiguration& target,
455 const std::string& key) const 506 const std::string& key) const
456 { 507 {
457 assert(configuration_.type() == Json::objectValue); 508 assert(configuration_.type() == Json::objectValue);
458 509
459 target.context_ = context_;
460 target.path_ = GetPath(key); 510 target.path_ = GetPath(key);
461 511
462 if (!configuration_.isMember(key)) 512 if (!configuration_.isMember(key))
463 { 513 {
464 target.configuration_ = Json::objectValue; 514 target.configuration_ = Json::objectValue;
465 } 515 }
466 else 516 else
467 { 517 {
468 if (configuration_[key].type() != Json::objectValue) 518 if (configuration_[key].type() != Json::objectValue)
469 { 519 {
470 if (context_ != NULL) 520 LogError("The configuration section \"" + target.path_ +
471 { 521 "\" is not an associative array as expected");
472 std::string s = "The configuration section \"" + target.path_ + "\" is not an associative array as expected";
473 OrthancPluginLogError(context_, s.c_str());
474 }
475 522
476 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 523 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
477 } 524 }
478 525
479 target.configuration_ = configuration_[key]; 526 target.configuration_ = configuration_[key];
491 return false; 538 return false;
492 } 539 }
493 540
494 if (configuration_[key].type() != Json::stringValue) 541 if (configuration_[key].type() != Json::stringValue)
495 { 542 {
496 if (context_ != NULL) 543 LogError("The configuration option \"" + GetPath(key) +
497 { 544 "\" is not a string as expected");
498 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected";
499 OrthancPluginLogError(context_, s.c_str());
500 }
501 545
502 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 546 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
503 } 547 }
504 548
505 target = configuration_[key].asString(); 549 target = configuration_[key].asString();
526 case Json::uintValue: 570 case Json::uintValue:
527 target = configuration_[key].asUInt(); 571 target = configuration_[key].asUInt();
528 return true; 572 return true;
529 573
530 default: 574 default:
531 if (context_ != NULL) 575 LogError("The configuration option \"" + GetPath(key) +
532 { 576 "\" is not an integer as expected");
533 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
534 OrthancPluginLogError(context_, s.c_str());
535 }
536 577
537 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 578 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
538 } 579 }
539 } 580 }
540 581
548 return false; 589 return false;
549 } 590 }
550 591
551 if (tmp < 0) 592 if (tmp < 0)
552 { 593 {
553 if (context_ != NULL) 594 LogError("The configuration option \"" + GetPath(key) +
554 { 595 "\" is not a positive integer as expected");
555 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a positive integer as expected";
556 OrthancPluginLogError(context_, s.c_str());
557 }
558 596
559 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 597 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
560 } 598 }
561 else 599 else
562 { 600 {
576 return false; 614 return false;
577 } 615 }
578 616
579 if (configuration_[key].type() != Json::booleanValue) 617 if (configuration_[key].type() != Json::booleanValue)
580 { 618 {
581 if (context_ != NULL) 619 LogError("The configuration option \"" + GetPath(key) +
582 { 620 "\" is not a Boolean as expected");
583 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a Boolean as expected";
584 OrthancPluginLogError(context_, s.c_str());
585 }
586 621
587 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 622 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
588 } 623 }
589 624
590 target = configuration_[key].asBool(); 625 target = configuration_[key].asBool();
615 case Json::uintValue: 650 case Json::uintValue:
616 target = static_cast<float>(configuration_[key].asUInt()); 651 target = static_cast<float>(configuration_[key].asUInt());
617 return true; 652 return true;
618 653
619 default: 654 default:
620 if (context_ != NULL) 655 LogError("The configuration option \"" + GetPath(key) +
621 { 656 "\" is not an integer as expected");
622 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
623 OrthancPluginLogError(context_, s.c_str());
624 }
625 657
626 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 658 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
627 } 659 }
628 } 660 }
629 661
678 710
679 default: 711 default:
680 break; 712 break;
681 } 713 }
682 714
683 if (context_ != NULL) 715 LogError("The configuration option \"" + GetPath(key) +
684 { 716 "\" is not a list of strings as expected");
685 std::string s = ("The configuration option \"" + GetPath(key) +
686 "\" is not a list of strings as expected");
687 OrthancPluginLogError(context_, s.c_str());
688 }
689 717
690 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 718 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
691 } 719 }
692 720
693 721
803 return; 831 return;
804 } 832 }
805 833
806 if (configuration_[key].type() != Json::objectValue) 834 if (configuration_[key].type() != Json::objectValue)
807 { 835 {
808 if (context_ != NULL) 836 LogError("The configuration option \"" + GetPath(key) +
809 { 837 "\" is not a string as expected");
810 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected";
811 OrthancPluginLogError(context_, s.c_str());
812 }
813 838
814 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 839 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
815 } 840 }
816 841
817 Json::Value::Members members = configuration_[key].getMemberNames(); 842 Json::Value::Members members = configuration_[key].getMemberNames();
824 { 849 {
825 target[members[i]] = value.asString(); 850 target[members[i]] = value.asString();
826 } 851 }
827 else 852 else
828 { 853 {
829 if (context_ != NULL) 854 LogError("The configuration option \"" + GetPath(key) +
830 { 855 "\" is not a dictionary mapping strings to strings");
831 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a dictionary mapping strings to strings";
832 OrthancPluginLogError(context_, s.c_str());
833 }
834 856
835 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 857 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
836 } 858 }
837 } 859 }
838 } 860 }
840 862
841 void OrthancImage::Clear() 863 void OrthancImage::Clear()
842 { 864 {
843 if (image_ != NULL) 865 if (image_ != NULL)
844 { 866 {
845 OrthancPluginFreeImage(context_, image_); 867 OrthancPluginFreeImage(GetGlobalContext(), image_);
846 image_ = NULL; 868 image_ = NULL;
847 } 869 }
848 } 870 }
849 871
850 872
851 void OrthancImage::CheckImageAvailable() 873 void OrthancImage::CheckImageAvailable()
852 { 874 {
853 if (image_ == NULL) 875 if (image_ == NULL)
854 { 876 {
855 OrthancPluginLogError(context_, "Trying to access a NULL image"); 877 LogError("Trying to access a NULL image");
856 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 878 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
857 } 879 }
858 } 880 }
859 881
860 882
861 OrthancImage::OrthancImage(OrthancPluginContext* context) : 883 OrthancImage::OrthancImage() :
862 context_(context),
863 image_(NULL) 884 image_(NULL)
864 { 885 {
865 if (context == NULL) 886 }
866 { 887
867 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 888
868 } 889 OrthancImage::OrthancImage(OrthancPluginImage* image) :
869 }
870
871
872 OrthancImage::OrthancImage(OrthancPluginContext* context,
873 OrthancPluginImage* image) :
874 context_(context),
875 image_(image) 890 image_(image)
876 { 891 {
877 if (context == NULL) 892 }
878 { 893
879 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 894
880 } 895 OrthancImage::OrthancImage(OrthancPluginPixelFormat format,
881 }
882
883
884 OrthancImage::OrthancImage(OrthancPluginContext* context,
885 OrthancPluginPixelFormat format,
886 uint32_t width, 896 uint32_t width,
887 uint32_t height) : 897 uint32_t height)
888 context_(context) 898 {
889 { 899 image_ = OrthancPluginCreateImage(GetGlobalContext(), format, width, height);
890 if (context == NULL) 900
891 { 901 if (image_ == NULL)
892 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 902 {
893 } 903 LogError("Cannot create an image");
894 else 904 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
895 { 905 }
896 image_ = OrthancPluginCreateImage(context, format, width, height); 906 }
897 } 907
898 } 908
899 909 OrthancImage::OrthancImage(OrthancPluginPixelFormat format,
900 OrthancImage::OrthancImage(OrthancPluginContext* context,
901 OrthancPluginPixelFormat format,
902 uint32_t width, 910 uint32_t width,
903 uint32_t height, 911 uint32_t height,
904 uint32_t pitch, 912 uint32_t pitch,
905 void* buffer) : 913 void* buffer)
906 context_(context) 914 {
907 { 915 image_ = OrthancPluginCreateImageAccessor
908 if (context == NULL) 916 (GetGlobalContext(), format, width, height, pitch, buffer);
909 { 917
910 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 918 if (image_ == NULL)
911 } 919 {
912 else 920 LogError("Cannot create an image accessor");
913 { 921 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
914 image_ = OrthancPluginCreateImageAccessor(context, format, width, height, pitch, buffer);
915 } 922 }
916 } 923 }
917 924
918 void OrthancImage::UncompressPngImage(const void* data, 925 void OrthancImage::UncompressPngImage(const void* data,
919 size_t size) 926 size_t size)
920 { 927 {
921 Clear(); 928 Clear();
922 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Png); 929
930 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Png);
931
923 if (image_ == NULL) 932 if (image_ == NULL)
924 { 933 {
925 OrthancPluginLogError(context_, "Cannot uncompress a PNG image"); 934 LogError("Cannot uncompress a PNG image");
926 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 935 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
927 } 936 }
928 } 937 }
929 938
930 939
931 void OrthancImage::UncompressJpegImage(const void* data, 940 void OrthancImage::UncompressJpegImage(const void* data,
932 size_t size) 941 size_t size)
933 { 942 {
934 Clear(); 943 Clear();
935 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Jpeg); 944 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Jpeg);
936 if (image_ == NULL) 945 if (image_ == NULL)
937 { 946 {
938 OrthancPluginLogError(context_, "Cannot uncompress a JPEG image"); 947 LogError("Cannot uncompress a JPEG image");
939 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 948 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
940 } 949 }
941 } 950 }
942 951
943 952
944 void OrthancImage::DecodeDicomImage(const void* data, 953 void OrthancImage::DecodeDicomImage(const void* data,
945 size_t size, 954 size_t size,
946 unsigned int frame) 955 unsigned int frame)
947 { 956 {
948 Clear(); 957 Clear();
949 image_ = OrthancPluginDecodeDicomImage(context_, data, size, frame); 958 image_ = OrthancPluginDecodeDicomImage(GetGlobalContext(), data, size, frame);
950 if (image_ == NULL) 959 if (image_ == NULL)
951 { 960 {
952 OrthancPluginLogError(context_, "Cannot uncompress a DICOM image"); 961 LogError("Cannot uncompress a DICOM image");
953 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 962 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
954 } 963 }
955 } 964 }
956 965
957 966
958 OrthancPluginPixelFormat OrthancImage::GetPixelFormat() 967 OrthancPluginPixelFormat OrthancImage::GetPixelFormat()
959 { 968 {
960 CheckImageAvailable(); 969 CheckImageAvailable();
961 return OrthancPluginGetImagePixelFormat(context_, image_); 970 return OrthancPluginGetImagePixelFormat(GetGlobalContext(), image_);
962 } 971 }
963 972
964 973
965 unsigned int OrthancImage::GetWidth() 974 unsigned int OrthancImage::GetWidth()
966 { 975 {
967 CheckImageAvailable(); 976 CheckImageAvailable();
968 return OrthancPluginGetImageWidth(context_, image_); 977 return OrthancPluginGetImageWidth(GetGlobalContext(), image_);
969 } 978 }
970 979
971 980
972 unsigned int OrthancImage::GetHeight() 981 unsigned int OrthancImage::GetHeight()
973 { 982 {
974 CheckImageAvailable(); 983 CheckImageAvailable();
975 return OrthancPluginGetImageHeight(context_, image_); 984 return OrthancPluginGetImageHeight(GetGlobalContext(), image_);
976 } 985 }
977 986
978 987
979 unsigned int OrthancImage::GetPitch() 988 unsigned int OrthancImage::GetPitch()
980 { 989 {
981 CheckImageAvailable(); 990 CheckImageAvailable();
982 return OrthancPluginGetImagePitch(context_, image_); 991 return OrthancPluginGetImagePitch(GetGlobalContext(), image_);
983 } 992 }
984 993
985 994
986 const void* OrthancImage::GetBuffer() 995 const void* OrthancImage::GetBuffer()
987 { 996 {
988 CheckImageAvailable(); 997 CheckImageAvailable();
989 return OrthancPluginGetImageBuffer(context_, image_); 998 return OrthancPluginGetImageBuffer(GetGlobalContext(), image_);
990 } 999 }
991 1000
992 1001
993 void OrthancImage::CompressPngImage(MemoryBuffer& target) 1002 void OrthancImage::CompressPngImage(MemoryBuffer& target)
994 { 1003 {
995 CheckImageAvailable(); 1004 CheckImageAvailable();
996 1005
997 OrthancPluginMemoryBuffer tmp; 1006 OrthancPluginMemoryBuffer tmp;
998 OrthancPluginCompressPngImage(context_, &tmp, GetPixelFormat(), 1007 OrthancPluginCompressPngImage(GetGlobalContext(), &tmp, GetPixelFormat(),
999 GetWidth(), GetHeight(), GetPitch(), GetBuffer()); 1008 GetWidth(), GetHeight(), GetPitch(), GetBuffer());
1000 1009
1001 target.Assign(tmp); 1010 target.Assign(tmp);
1002 } 1011 }
1003 1012
1006 uint8_t quality) 1015 uint8_t quality)
1007 { 1016 {
1008 CheckImageAvailable(); 1017 CheckImageAvailable();
1009 1018
1010 OrthancPluginMemoryBuffer tmp; 1019 OrthancPluginMemoryBuffer tmp;
1011 OrthancPluginCompressJpegImage(context_, &tmp, GetPixelFormat(), 1020 OrthancPluginCompressJpegImage(GetGlobalContext(), &tmp, GetPixelFormat(),
1012 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality); 1021 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality);
1013 1022
1014 target.Assign(tmp); 1023 target.Assign(tmp);
1015 } 1024 }
1016 1025
1017 1026
1018 void OrthancImage::AnswerPngImage(OrthancPluginRestOutput* output) 1027 void OrthancImage::AnswerPngImage(OrthancPluginRestOutput* output)
1019 { 1028 {
1020 CheckImageAvailable(); 1029 CheckImageAvailable();
1021 OrthancPluginCompressAndAnswerPngImage(context_, output, GetPixelFormat(), 1030 OrthancPluginCompressAndAnswerPngImage(GetGlobalContext(), output, GetPixelFormat(),
1022 GetWidth(), GetHeight(), GetPitch(), GetBuffer()); 1031 GetWidth(), GetHeight(), GetPitch(), GetBuffer());
1023 } 1032 }
1024 1033
1025 1034
1026 void OrthancImage::AnswerJpegImage(OrthancPluginRestOutput* output, 1035 void OrthancImage::AnswerJpegImage(OrthancPluginRestOutput* output,
1027 uint8_t quality) 1036 uint8_t quality)
1028 { 1037 {
1029 CheckImageAvailable(); 1038 CheckImageAvailable();
1030 OrthancPluginCompressAndAnswerJpegImage(context_, output, GetPixelFormat(), 1039 OrthancPluginCompressAndAnswerJpegImage(GetGlobalContext(), output, GetPixelFormat(),
1031 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality); 1040 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality);
1032 } 1041 }
1033 1042
1034 1043
1035 1044
1036 #if HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 1045 #if HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1
1037 FindMatcher::FindMatcher(OrthancPluginContext* context, 1046 FindMatcher::FindMatcher(const OrthancPluginWorklistQuery* worklist) :
1038 const OrthancPluginWorklistQuery* worklist) :
1039 context_(context),
1040 matcher_(NULL), 1047 matcher_(NULL),
1041 worklist_(worklist) 1048 worklist_(worklist)
1042 { 1049 {
1043 if (worklist_ == NULL) 1050 if (worklist_ == NULL)
1044 { 1051 {
1045 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1052 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1046 } 1053 }
1047 } 1054 }
1048 1055
1049 1056
1050 void FindMatcher::SetupDicom(OrthancPluginContext* context, 1057 void FindMatcher::SetupDicom(const void* query,
1051 const void* query, 1058 uint32_t size)
1052 uint32_t size) 1059 {
1053 {
1054 context_ = context;
1055 worklist_ = NULL; 1060 worklist_ = NULL;
1056 1061
1057 matcher_ = OrthancPluginCreateFindMatcher(context_, query, size); 1062 matcher_ = OrthancPluginCreateFindMatcher(GetGlobalContext(), query, size);
1058 if (matcher_ == NULL) 1063 if (matcher_ == NULL)
1059 { 1064 {
1060 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 1065 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
1061 } 1066 }
1062 } 1067 }
1066 { 1071 {
1067 // The "worklist_" field 1072 // The "worklist_" field
1068 1073
1069 if (matcher_ != NULL) 1074 if (matcher_ != NULL)
1070 { 1075 {
1071 OrthancPluginFreeFindMatcher(context_, matcher_); 1076 OrthancPluginFreeFindMatcher(GetGlobalContext(), matcher_);
1072 } 1077 }
1073 } 1078 }
1074 1079
1075 1080
1076 1081
1079 { 1084 {
1080 int32_t result; 1085 int32_t result;
1081 1086
1082 if (matcher_ != NULL) 1087 if (matcher_ != NULL)
1083 { 1088 {
1084 result = OrthancPluginFindMatcherIsMatch(context_, matcher_, dicom, size); 1089 result = OrthancPluginFindMatcherIsMatch(GetGlobalContext(), matcher_, dicom, size);
1085 } 1090 }
1086 else if (worklist_ != NULL) 1091 else if (worklist_ != NULL)
1087 { 1092 {
1088 result = OrthancPluginWorklistIsMatch(context_, worklist_, dicom, size); 1093 result = OrthancPluginWorklistIsMatch(GetGlobalContext(), worklist_, dicom, size);
1089 } 1094 }
1090 else 1095 else
1091 { 1096 {
1092 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 1097 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
1093 } 1098 }
1108 1113
1109 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */ 1114 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */
1110 1115
1111 1116
1112 bool RestApiGet(Json::Value& result, 1117 bool RestApiGet(Json::Value& result,
1113 OrthancPluginContext* context,
1114 const std::string& uri, 1118 const std::string& uri,
1115 bool applyPlugins) 1119 bool applyPlugins)
1116 { 1120 {
1117 MemoryBuffer answer(context); 1121 MemoryBuffer answer;
1122
1118 if (!answer.RestApiGet(uri, applyPlugins)) 1123 if (!answer.RestApiGet(uri, applyPlugins))
1119 { 1124 {
1120 return false; 1125 return false;
1121 } 1126 }
1122 else 1127 else
1126 } 1131 }
1127 } 1132 }
1128 1133
1129 1134
1130 bool RestApiPost(Json::Value& result, 1135 bool RestApiPost(Json::Value& result,
1131 OrthancPluginContext* context,
1132 const std::string& uri, 1136 const std::string& uri,
1133 const char* body, 1137 const char* body,
1134 size_t bodySize, 1138 size_t bodySize,
1135 bool applyPlugins) 1139 bool applyPlugins)
1136 { 1140 {
1137 MemoryBuffer answer(context); 1141 MemoryBuffer answer;
1142
1138 if (!answer.RestApiPost(uri, body, bodySize, applyPlugins)) 1143 if (!answer.RestApiPost(uri, body, bodySize, applyPlugins))
1139 { 1144 {
1140 return false; 1145 return false;
1141 } 1146 }
1142 else 1147 else
1146 } 1151 }
1147 } 1152 }
1148 1153
1149 1154
1150 bool RestApiPost(Json::Value& result, 1155 bool RestApiPost(Json::Value& result,
1151 OrthancPluginContext* context,
1152 const std::string& uri, 1156 const std::string& uri,
1153 const Json::Value& body, 1157 const Json::Value& body,
1154 bool applyPlugins) 1158 bool applyPlugins)
1155 { 1159 {
1156 Json::FastWriter writer; 1160 Json::FastWriter writer;
1157 return RestApiPost(result, context, uri, writer.write(body), applyPlugins); 1161 return RestApiPost(result, uri, writer.write(body), applyPlugins);
1158 } 1162 }
1159 1163
1160 1164
1161 bool RestApiPut(Json::Value& result, 1165 bool RestApiPut(Json::Value& result,
1162 OrthancPluginContext* context,
1163 const std::string& uri, 1166 const std::string& uri,
1164 const char* body, 1167 const char* body,
1165 size_t bodySize, 1168 size_t bodySize,
1166 bool applyPlugins) 1169 bool applyPlugins)
1167 { 1170 {
1168 MemoryBuffer answer(context); 1171 MemoryBuffer answer;
1172
1169 if (!answer.RestApiPut(uri, body, bodySize, applyPlugins)) 1173 if (!answer.RestApiPut(uri, body, bodySize, applyPlugins))
1170 { 1174 {
1171 return false; 1175 return false;
1172 } 1176 }
1173 else 1177 else
1177 } 1181 }
1178 } 1182 }
1179 1183
1180 1184
1181 bool RestApiPut(Json::Value& result, 1185 bool RestApiPut(Json::Value& result,
1182 OrthancPluginContext* context,
1183 const std::string& uri, 1186 const std::string& uri,
1184 const Json::Value& body, 1187 const Json::Value& body,
1185 bool applyPlugins) 1188 bool applyPlugins)
1186 { 1189 {
1187 Json::FastWriter writer; 1190 Json::FastWriter writer;
1188 return RestApiPut(result, context, uri, writer.write(body), applyPlugins); 1191 return RestApiPut(result, uri, writer.write(body), applyPlugins);
1189 } 1192 }
1190 1193
1191 1194
1192 bool RestApiDelete(OrthancPluginContext* context, 1195 bool RestApiDelete(const std::string& uri,
1193 const std::string& uri,
1194 bool applyPlugins) 1196 bool applyPlugins)
1195 { 1197 {
1196 OrthancPluginErrorCode error; 1198 OrthancPluginErrorCode error;
1197 1199
1198 if (applyPlugins) 1200 if (applyPlugins)
1199 { 1201 {
1200 error = OrthancPluginRestApiDeleteAfterPlugins(context, uri.c_str()); 1202 error = OrthancPluginRestApiDeleteAfterPlugins(GetGlobalContext(), uri.c_str());
1201 } 1203 }
1202 else 1204 else
1203 { 1205 {
1204 error = OrthancPluginRestApiDelete(context, uri.c_str()); 1206 error = OrthancPluginRestApiDelete(GetGlobalContext(), uri.c_str());
1205 } 1207 }
1206 1208
1207 if (error == OrthancPluginErrorCode_Success) 1209 if (error == OrthancPluginErrorCode_Success)
1208 { 1210 {
1209 return true; 1211 return true;
1218 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); 1220 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
1219 } 1221 }
1220 } 1222 }
1221 1223
1222 1224
1223 void ReportMinimalOrthancVersion(OrthancPluginContext* context, 1225 void ReportMinimalOrthancVersion(unsigned int major,
1224 unsigned int major,
1225 unsigned int minor, 1226 unsigned int minor,
1226 unsigned int revision) 1227 unsigned int revision)
1227 { 1228 {
1228 std::string s = ("Your version of the Orthanc core (" + 1229 LogError("Your version of the Orthanc core (" +
1229 std::string(context->orthancVersion) + 1230 std::string(GetGlobalContext()->orthancVersion) +
1230 ") is too old to run this plugin (version " + 1231 ") is too old to run this plugin (version " +
1231 boost::lexical_cast<std::string>(major) + "." + 1232 boost::lexical_cast<std::string>(major) + "." +
1232 boost::lexical_cast<std::string>(minor) + "." + 1233 boost::lexical_cast<std::string>(minor) + "." +
1233 boost::lexical_cast<std::string>(revision) + 1234 boost::lexical_cast<std::string>(revision) +
1234 " is required)"); 1235 " is required)");
1235 1236 }
1236 OrthancPluginLogError(context, s.c_str()); 1237
1237 } 1238
1238 1239 bool CheckMinimalOrthancVersion(unsigned int major,
1239
1240 bool CheckMinimalOrthancVersion(OrthancPluginContext* context,
1241 unsigned int major,
1242 unsigned int minor, 1240 unsigned int minor,
1243 unsigned int revision) 1241 unsigned int revision)
1244 { 1242 {
1245 if (context == NULL) 1243 if (!HasGlobalContext())
1246 { 1244 {
1247 OrthancPluginLogError(context, "Bad Orthanc context in the plugin"); 1245 LogError("Bad Orthanc context in the plugin");
1248 return false; 1246 return false;
1249 } 1247 }
1250 1248
1251 if (!strcmp(context->orthancVersion, "mainline")) 1249 if (!strcmp(GetGlobalContext()->orthancVersion, "mainline"))
1252 { 1250 {
1253 // Assume compatibility with the mainline 1251 // Assume compatibility with the mainline
1254 return true; 1252 return true;
1255 } 1253 }
1256 1254
1260 #ifdef _MSC_VER 1258 #ifdef _MSC_VER
1261 sscanf_s 1259 sscanf_s
1262 #else 1260 #else
1263 sscanf 1261 sscanf
1264 #endif 1262 #endif
1265 (context->orthancVersion, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 || 1263 (GetGlobalContext()->orthancVersion, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 ||
1266 aa < 0 || 1264 aa < 0 ||
1267 bb < 0 || 1265 bb < 0 ||
1268 cc < 0) 1266 cc < 0)
1269 { 1267 {
1270 return false; 1268 return false;
1324 { 1322 {
1325 return index; 1323 return index;
1326 } 1324 }
1327 else 1325 else
1328 { 1326 {
1329 std::string s = "Inexistent peer: " + name; 1327 LogError("Inexistent peer: " + name);
1330 OrthancPluginLogError(context_, s.c_str());
1331 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource); 1328 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource);
1332 } 1329 }
1333 } 1330 }
1334 1331
1335 1332
1336 OrthancPeers::OrthancPeers(OrthancPluginContext* context) : 1333 OrthancPeers::OrthancPeers() :
1337 context_(context),
1338 peers_(NULL), 1334 peers_(NULL),
1339 timeout_(0) 1335 timeout_(0)
1340 { 1336 {
1341 if (context_ == NULL) 1337 peers_ = OrthancPluginGetPeers(GetGlobalContext());
1342 {
1343 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer);
1344 }
1345
1346 peers_ = OrthancPluginGetPeers(context_);
1347 1338
1348 if (peers_ == NULL) 1339 if (peers_ == NULL)
1349 { 1340 {
1350 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 1341 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1351 } 1342 }
1352 1343
1353 uint32_t count = OrthancPluginGetPeersCount(context_, peers_); 1344 uint32_t count = OrthancPluginGetPeersCount(GetGlobalContext(), peers_);
1354 1345
1355 for (uint32_t i = 0; i < count; i++) 1346 for (uint32_t i = 0; i < count; i++)
1356 { 1347 {
1357 const char* name = OrthancPluginGetPeerName(context_, peers_, i); 1348 const char* name = OrthancPluginGetPeerName(GetGlobalContext(), peers_, i);
1358 if (name == NULL) 1349 if (name == NULL)
1359 { 1350 {
1360 OrthancPluginFreePeers(context_, peers_); 1351 OrthancPluginFreePeers(GetGlobalContext(), peers_);
1361 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 1352 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1362 } 1353 }
1363 1354
1364 index_[name] = i; 1355 index_[name] = i;
1365 } 1356 }
1368 1359
1369 OrthancPeers::~OrthancPeers() 1360 OrthancPeers::~OrthancPeers()
1370 { 1361 {
1371 if (peers_ != NULL) 1362 if (peers_ != NULL)
1372 { 1363 {
1373 OrthancPluginFreePeers(context_, peers_); 1364 OrthancPluginFreePeers(GetGlobalContext(), peers_);
1374 } 1365 }
1375 } 1366 }
1376 1367
1377 1368
1378 bool OrthancPeers::LookupName(size_t& target, 1369 bool OrthancPeers::LookupName(size_t& target,
1398 { 1389 {
1399 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); 1390 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1400 } 1391 }
1401 else 1392 else
1402 { 1393 {
1403 const char* s = OrthancPluginGetPeerName(context_, peers_, static_cast<uint32_t>(index)); 1394 const char* s = OrthancPluginGetPeerName(GetGlobalContext(), peers_, static_cast<uint32_t>(index));
1404 if (s == NULL) 1395 if (s == NULL)
1405 { 1396 {
1406 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 1397 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1407 } 1398 }
1408 else 1399 else
1419 { 1410 {
1420 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); 1411 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1421 } 1412 }
1422 else 1413 else
1423 { 1414 {
1424 const char* s = OrthancPluginGetPeerUrl(context_, peers_, static_cast<uint32_t>(index)); 1415 const char* s = OrthancPluginGetPeerUrl(GetGlobalContext(), peers_, static_cast<uint32_t>(index));
1425 if (s == NULL) 1416 if (s == NULL)
1426 { 1417 {
1427 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 1418 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1428 } 1419 }
1429 else 1420 else
1448 { 1439 {
1449 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); 1440 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
1450 } 1441 }
1451 else 1442 else
1452 { 1443 {
1453 const char* s = OrthancPluginGetPeerUserProperty(context_, peers_, static_cast<uint32_t>(index), key.c_str()); 1444 const char* s = OrthancPluginGetPeerUserProperty(GetGlobalContext(), peers_, static_cast<uint32_t>(index), key.c_str());
1454 if (s == NULL) 1445 if (s == NULL)
1455 { 1446 {
1456 return false; 1447 return false;
1457 } 1448 }
1458 else 1449 else
1482 } 1473 }
1483 1474
1484 OrthancPluginMemoryBuffer answer; 1475 OrthancPluginMemoryBuffer answer;
1485 uint16_t status; 1476 uint16_t status;
1486 OrthancPluginErrorCode code = OrthancPluginCallPeerApi 1477 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1487 (context_, &answer, NULL, &status, peers_, 1478 (GetGlobalContext(), &answer, NULL, &status, peers_,
1488 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Get, uri.c_str(), 1479 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Get, uri.c_str(),
1489 0, NULL, NULL, NULL, 0, timeout_); 1480 0, NULL, NULL, NULL, 0, timeout_);
1490 1481
1491 if (code == OrthancPluginErrorCode_Success) 1482 if (code == OrthancPluginErrorCode_Success)
1492 { 1483 {
1512 1503
1513 bool OrthancPeers::DoGet(Json::Value& target, 1504 bool OrthancPeers::DoGet(Json::Value& target,
1514 size_t index, 1505 size_t index,
1515 const std::string& uri) const 1506 const std::string& uri) const
1516 { 1507 {
1517 MemoryBuffer buffer(context_); 1508 MemoryBuffer buffer;
1518 1509
1519 if (DoGet(buffer, index, uri)) 1510 if (DoGet(buffer, index, uri))
1520 { 1511 {
1521 buffer.ToJson(target); 1512 buffer.ToJson(target);
1522 return true; 1513 return true;
1530 1521
1531 bool OrthancPeers::DoGet(Json::Value& target, 1522 bool OrthancPeers::DoGet(Json::Value& target,
1532 const std::string& name, 1523 const std::string& name,
1533 const std::string& uri) const 1524 const std::string& uri) const
1534 { 1525 {
1535 MemoryBuffer buffer(context_); 1526 MemoryBuffer buffer;
1536 1527
1537 if (DoGet(buffer, name, uri)) 1528 if (DoGet(buffer, name, uri))
1538 { 1529 {
1539 buffer.ToJson(target); 1530 buffer.ToJson(target);
1540 return true; 1531 return true;
1560 bool OrthancPeers::DoPost(Json::Value& target, 1551 bool OrthancPeers::DoPost(Json::Value& target,
1561 size_t index, 1552 size_t index,
1562 const std::string& uri, 1553 const std::string& uri,
1563 const std::string& body) const 1554 const std::string& body) const
1564 { 1555 {
1565 MemoryBuffer buffer(context_); 1556 MemoryBuffer buffer;
1566 1557
1567 if (DoPost(buffer, index, uri, body)) 1558 if (DoPost(buffer, index, uri, body))
1568 { 1559 {
1569 buffer.ToJson(target); 1560 buffer.ToJson(target);
1570 return true; 1561 return true;
1579 bool OrthancPeers::DoPost(Json::Value& target, 1570 bool OrthancPeers::DoPost(Json::Value& target,
1580 const std::string& name, 1571 const std::string& name,
1581 const std::string& uri, 1572 const std::string& uri,
1582 const std::string& body) const 1573 const std::string& body) const
1583 { 1574 {
1584 MemoryBuffer buffer(context_); 1575 MemoryBuffer buffer;
1585 1576
1586 if (DoPost(buffer, name, uri, body)) 1577 if (DoPost(buffer, name, uri, body))
1587 { 1578 {
1588 buffer.ToJson(target); 1579 buffer.ToJson(target);
1589 return true; 1580 return true;
1606 } 1597 }
1607 1598
1608 OrthancPluginMemoryBuffer answer; 1599 OrthancPluginMemoryBuffer answer;
1609 uint16_t status; 1600 uint16_t status;
1610 OrthancPluginErrorCode code = OrthancPluginCallPeerApi 1601 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1611 (context_, &answer, NULL, &status, peers_, 1602 (GetGlobalContext(), &answer, NULL, &status, peers_,
1612 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(), 1603 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(),
1613 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_); 1604 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_);
1614 1605
1615 if (code == OrthancPluginErrorCode_Success) 1606 if (code == OrthancPluginErrorCode_Success)
1616 { 1607 {
1634 } 1625 }
1635 1626
1636 OrthancPluginMemoryBuffer answer; 1627 OrthancPluginMemoryBuffer answer;
1637 uint16_t status; 1628 uint16_t status;
1638 OrthancPluginErrorCode code = OrthancPluginCallPeerApi 1629 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1639 (context_, &answer, NULL, &status, peers_, 1630 (GetGlobalContext(), &answer, NULL, &status, peers_,
1640 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(), 1631 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(),
1641 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_); 1632 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_);
1642 1633
1643 if (code == OrthancPluginErrorCode_Success) 1634 if (code == OrthancPluginErrorCode_Success)
1644 { 1635 {
1645 OrthancPluginFreeMemoryBuffer(context_, &answer); 1636 OrthancPluginFreeMemoryBuffer(GetGlobalContext(), &answer);
1646 return (status == 200); 1637 return (status == 200);
1647 } 1638 }
1648 else 1639 else
1649 { 1640 {
1650 return false; 1641 return false;
1671 } 1662 }
1672 1663
1673 OrthancPluginMemoryBuffer answer; 1664 OrthancPluginMemoryBuffer answer;
1674 uint16_t status; 1665 uint16_t status;
1675 OrthancPluginErrorCode code = OrthancPluginCallPeerApi 1666 OrthancPluginErrorCode code = OrthancPluginCallPeerApi
1676 (context_, &answer, NULL, &status, peers_, 1667 (GetGlobalContext(), &answer, NULL, &status, peers_,
1677 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(), 1668 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(),
1678 0, NULL, NULL, NULL, 0, timeout_); 1669 0, NULL, NULL, NULL, 0, timeout_);
1679 1670
1680 if (code == OrthancPluginErrorCode_Success) 1671 if (code == OrthancPluginErrorCode_Success)
1681 { 1672 {
1682 OrthancPluginFreeMemoryBuffer(context_, &answer); 1673 OrthancPluginFreeMemoryBuffer(GetGlobalContext(), &answer);
1683 return (status == 200); 1674 return (status == 200);
1684 } 1675 }
1685 else 1676 else
1686 { 1677 {
1687 return false; 1678 return false;
1886 ClearContent(); 1877 ClearContent();
1887 ClearSerialized(); 1878 ClearSerialized();
1888 } 1879 }
1889 1880
1890 1881
1891 OrthancPluginJob* OrthancJob::Create(OrthancPluginContext* context, 1882 OrthancPluginJob* OrthancJob::Create(OrthancJob* job)
1892 OrthancJob* job)
1893 { 1883 {
1894 if (job == NULL) 1884 if (job == NULL)
1895 { 1885 {
1896 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer); 1886 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer);
1897 } 1887 }
1898 1888
1899 OrthancPluginJob* orthanc = OrthancPluginCreateJob( 1889 OrthancPluginJob* orthanc = OrthancPluginCreateJob(
1900 context, job, CallbackFinalize, job->jobType_.c_str(), 1890 GetGlobalContext(), job, CallbackFinalize, job->jobType_.c_str(),
1901 CallbackGetProgress, CallbackGetContent, CallbackGetSerialized, 1891 CallbackGetProgress, CallbackGetContent, CallbackGetSerialized,
1902 CallbackStep, CallbackStop, CallbackReset); 1892 CallbackStep, CallbackStop, CallbackReset);
1903 1893
1904 if (orthanc == NULL) 1894 if (orthanc == NULL)
1905 { 1895 {
1906 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 1896 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1907 } 1897 }
1910 return orthanc; 1900 return orthanc;
1911 } 1901 }
1912 } 1902 }
1913 1903
1914 1904
1915 std::string OrthancJob::Submit(OrthancPluginContext* context, 1905 std::string OrthancJob::Submit(OrthancJob* job,
1916 OrthancJob* job,
1917 int priority) 1906 int priority)
1918 { 1907 {
1919 OrthancPluginJob* orthanc = Create(context, job); 1908 OrthancPluginJob* orthanc = Create(job);
1920 1909
1921 char* id = OrthancPluginSubmitJob(context, orthanc, priority); 1910 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority);
1922 1911
1923 if (id == NULL) 1912 if (id == NULL)
1924 { 1913 {
1925 OrthancPluginLogError(context, "Plugin cannot submit job"); 1914 LogError("Plugin cannot submit job");
1926 OrthancPluginFreeJob(context, orthanc); 1915 OrthancPluginFreeJob(GetGlobalContext(), orthanc);
1927 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 1916 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
1928 } 1917 }
1929 else 1918 else
1930 { 1919 {
1931 std::string tmp(id); 1920 std::string tmp(id);
1932 tmp.assign(id); 1921 tmp.assign(id);
1933 OrthancPluginFreeString(context, id); 1922 OrthancPluginFreeString(GetGlobalContext(), id);
1934 1923
1935 return tmp; 1924 return tmp;
1936 } 1925 }
1937 } 1926 }
1938 #endif 1927 #endif