/
pmrwebservice.cpp
823 lines (595 loc) · 30.7 KB
/
pmrwebservice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// PMR Web service
//==============================================================================
#include "corecliutils.h"
#include "pmrsupport.h"
#include "pmrwebservice.h"
#include "pmrwebservicemanager.h"
#include "pmrwebserviceresponse.h"
#include "pmrworkspacemanager.h"
//==============================================================================
#include <QtConcurrent/QtConcurrent>
//==============================================================================
namespace OpenCOR {
namespace PMRSupport {
//==============================================================================
PmrWebService::PmrWebService(const QString &pPmrUrl, QObject *pParent) :
QObject(pParent),
mPmrUrl(pPmrUrl)
{
// Create a PMR Web service manager so that we can retrieve various things
// from PMR
mPmrWebServiceManager = new PmrWebServiceManager(pPmrUrl, this);
// Forward any signal we receive from our PMR Web service manager
connect(mPmrWebServiceManager, &PmrWebServiceManager::busy,
this, &PmrWebService::busy);
connect(mPmrWebServiceManager, &PmrWebServiceManager::authenticated,
this, &PmrWebService::authenticated);
connect(mPmrWebServiceManager, &PmrWebServiceManager::error,
this, &PmrWebService::error);
connect(mPmrWebServiceManager, &PmrWebServiceManager::authenticationCancelled,
this, &PmrWebService::authenticationCancelled);
}
//==============================================================================
PmrWebService::PmrWebService(QObject *pParent) :
PmrWebService(QString(), pParent)
{
}
//==============================================================================
bool PmrWebService::isAuthenticated() const
{
// Return whether we are authenticated
return mPmrWebServiceManager->isAuthenticated();
}
//==============================================================================
void PmrWebService::authenticate(bool pAuthenticate)
{
// Un/authenticate ourselves
mPmrWebServiceManager->authenticate(pAuthenticate);
}
//==============================================================================
void PmrWebService::requestExposures() const
{
// Request the list of exposures from PMR
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(mPmrUrl+"/exposure", false);
if (pmrResponse != nullptr) {
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::exposuresResponse);
}
}
//==============================================================================
void PmrWebService::exposuresResponse(const QJsonDocument &pJsonDocument)
{
// Retrieve the list of exposures from the given PMR response
static const QString Bookmark = "bookmark";
PmrExposures exposures;
QVariantMap collectionMap = pJsonDocument.object().toVariantMap()["collection"].toMap();
for (const auto &links : collectionMap["links"].toList()) {
QVariantMap linksMap = links.toMap();
if (linksMap["rel"].toString() == Bookmark) {
QString exposureUrl = linksMap["href"].toString().trimmed();
QString exposureName = linksMap["prompt"].toString().simplified();
if (!exposureUrl.isEmpty() && !exposureName.isEmpty()) {
auto exposure = new PmrExposure(exposureUrl, exposureName, this);
mUrlExposures.insert(exposureUrl, exposure);
exposures << exposure;
}
}
}
std::sort(exposures.begin(), exposures.end(), PmrExposure::compare);
// Let people know about the list of exposures
emit PmrWebService::exposures(exposures);
}
//==============================================================================
static const char *PathProperty = "Path";
static const char *ExposureProperty = "Exposure";
static const char *NextActionProperty = "NextAction";
static const char *WorkspaceProperty = "Workspace";
//==============================================================================
PmrWorkspace * PmrWebService::workspace(const QString &pUrl) const
{
// Retrieve and return the workspace for the given URL
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(pUrl, true);
if (pmrResponse != nullptr) {
PmrWorkspace *workspace = nullptr;
pmrResponse->setProperty(WorkspaceProperty, QVariant::fromValue(reinterpret_cast<void *>(&workspace)));
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::workspaceResponse);
// Don't return until the PMR response has been processed
QEventLoop waitLoop;
connect(pmrResponse, &PmrWebServiceResponse::finished,
&waitLoop, &QEventLoop::quit);
waitLoop.exec();
return workspace;
}
return nullptr;
}
//==============================================================================
void PmrWebService::workspaceResponse(const QJsonDocument &pJsonDocument)
{
// Retrieve the workspace from the given PMR response
PmrWorkspace **workspacePointer = reinterpret_cast<PmrWorkspace **>(sender()->property(WorkspaceProperty).value<void *>());
QVariantMap collectionMap = pJsonDocument.object().toVariantMap()["collection"].toMap();
QVariantList itemsList = collectionMap["items"].toList();
if (!itemsList.isEmpty()) {
// Retrieve the details of the workspace we are dealing with
static const QString Storage = "storage";
static const QString Description = "description";
static const QString Owner = "owner";
static const QString Title = "title";
static const QString Id = "id";
static const QString Git = "git";
QString workspaceUrl = itemsList.first().toMap()["href"].toString().trimmed();
QString storage;
QString workspaceDescription;
QString workspaceOwner;
QString workspaceName;
QString workspaceId;
for (const auto &data : itemsList.first().toMap()["data"].toList()) {
QVariantMap dataMap = data.toMap();
QString name = dataMap["name"].toString();
if (name == Storage) {
storage = dataMap["value"].toString();
} else if (name == Description) {
workspaceDescription = dataMap["value"].toString();
} else if (name == Owner) {
workspaceOwner = dataMap["value"].toString();
} else if (name == Title) {
workspaceName = dataMap["value"].toString();
} else if (name == Id) {
workspaceId = dataMap["value"].toString();
}
}
if (workspaceName.isEmpty()) {
workspaceName = workspaceId;
}
if (workspaceName.isEmpty()) {
workspaceName = tr("** Unknown name **");
}
if (!workspaceUrl.isEmpty() && (storage == Git)) {
*workspacePointer = new PmrWorkspace(false, workspaceName,
workspaceUrl,
workspaceDescription,
workspaceOwner, this);
}
}
}
//==============================================================================
void PmrWebService::requestNewWorkspace(const QString &pName,
const QString &pDescription,
const QString &pPath) const
{
// Create a new workspace
static const QString CreateWorkspaceJson = R"({ "template": { "data": [)"
R"( { "name": "form.widgets.title", "value": "%1" },)"
R"( { "name": "form.widgets.description", "value": "%2" },)"
R"( { "name": "form.widgets.storage", "value": "git" },)"
R"( { "name": "form.buttons.add", "value": "Add" })"
"] } }";
QJsonDocument createWorkspaceJson = QJsonDocument::fromJson(QString(CreateWorkspaceJson).arg(pName,
pDescription).toUtf8());
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(mPmrUrl+"/workspace/+/addWorkspace",
true, true, createWorkspaceJson);
if (pmrResponse != nullptr) {
pmrResponse->setProperty(PathProperty, pPath);
connect(pmrResponse, &PmrWebServiceResponse::found,
this, &PmrWebService::newWorkspaceResponse);
}
}
//==============================================================================
void PmrWebService::newWorkspaceResponse(const QString &pUrl)
{
// Let people know that the workspace, which URL is given, has been created
emit workspaceCreated(pUrl);
// Now that the workspace has been created, we can request its information
// Note: a non-empty path will also clone the workspace...
requestWorkspaceInformation(pUrl, sender()->property(PathProperty).toString());
}
//==============================================================================
void PmrWebService::requestWorkspaces() const
{
// Retrieve all the workspaces
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(mPmrUrl+"/my-workspaces", true);
if (pmrResponse != nullptr) {
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::workspacesResponse);
}
}
//==============================================================================
void PmrWebService::workspacesResponse(const QJsonDocument &pJsonDocument)
{
// Retrieve the list of workspaces from the given PMR response
static const QString Bookmark = "bookmark";
PmrWorkspaces workspaces;
QVariantMap collectionMap = pJsonDocument.object().toVariantMap()["collection"].toMap();
for (const auto &links : collectionMap["links"].toList()) {
QVariantMap linksMap = links.toMap();
if (linksMap["rel"].toString() == Bookmark) {
QString workspaceUrl = linksMap["href"].toString().trimmed();
QString workspaceName = linksMap["prompt"].toString().simplified();
if (!workspaceUrl.isEmpty() && !workspaceName.isEmpty()) {
workspaces << new PmrWorkspace(true, workspaceName,
workspaceUrl, this);
}
}
}
std::sort(workspaces.begin(), workspaces.end(), PmrWorkspace::compare);
// Let people know about the list of workspaces
emit PmrWebService::workspaces(workspaces);
}
//==============================================================================
void PmrWebService::requestWorkspaceInformation(const QString &pUrl,
const QString &pPath,
PmrExposure *pExposure)
{
// Retrieve some information about the workspace, which URL is given, and
// clone it (if the given path is non-empty or the given exposure is
// non-null)
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(pUrl, true);
if (pmrResponse != nullptr) {
pmrResponse->setProperty(ExposureProperty, QVariant::fromValue(reinterpret_cast<void *>(pExposure)));
pmrResponse->setProperty(PathProperty, pPath);
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::workspaceInformationResponse);
}
}
//==============================================================================
void PmrWebService::workspaceInformationResponse(const QJsonDocument &pJsonDocument)
{
// Retrieve the workspace information from the given PMR response
QVariantMap collectionMap = pJsonDocument.object().toVariantMap()["collection"].toMap();
QVariantList itemsList = collectionMap["items"].toList();
if (!itemsList.isEmpty()) {
// Retrieve the details of the workspace we are dealing with
static const QString Storage = "storage";
static const QString Description = "description";
static const QString Owner = "owner";
static const QString Title = "title";
static const QString Git = "git";
QString workspaceUrl = itemsList.first().toMap()["href"].toString().trimmed();
QString storage;
QString workspaceDescription;
QString workspaceOwner;
QString workspaceName;
for (const auto &data : itemsList.first().toMap()["data"].toList()) {
QVariantMap dataMap = data.toMap();
if (dataMap["name"].toString() == Storage) {
storage = dataMap["value"].toString();
} else if (dataMap["name"].toString() == Description) {
workspaceDescription = dataMap["value"].toString();
} else if (dataMap["name"].toString() == Owner) {
workspaceOwner = dataMap["value"].toString();
} else if (dataMap["name"].toString() == Title) {
workspaceName = dataMap["value"].toString();
}
}
// Retrieve the exposure
PmrExposure *exposure = reinterpret_cast<PmrExposure *>(sender()->property(ExposureProperty).value<void *>());
if (!workspaceUrl.isEmpty()) {
// Make sure that our workspace is a Git repository
if (storage == Git) {
QString dirName;
if (exposure != nullptr) {
// Check that we aren't already managing a clone of the
// workspace
PmrWorkspace *existingWorkspace = PmrWorkspaceManager::instance()->workspace(workspaceUrl);
if (existingWorkspace == nullptr) {
// Retrieve the name of an empty directory
dirName = getEmptyDirectory();
} else {
emit warning(tr("The workspace for %1 is already cloned in %2.").arg(workspaceUrl,
existingWorkspace->path()));
}
} else {
// Cloning after creating a new (owned) workspace
dirName = sender()->property(PathProperty).toString();
}
// Clone the workspace, if we have an empty directory
if (!dirName.isEmpty()) {
auto workspace = new PmrWorkspace(exposure == nullptr,
workspaceName,
workspaceUrl,
workspaceDescription,
workspaceOwner, this);
requestWorkspaceClone(workspace, dirName);
// Let our exposure, if any, know that we have cloned it
if (exposure != nullptr) {
exposure->setWorkspace(workspace);
}
}
} else if (exposure != nullptr) {
emitInformation(tr("The workspace for %1 is not a Git repository.").arg(exposure->toHtml()));
}
} else if (exposure != nullptr) {
emitInformation(tr("No workspace information could be found for %1.").arg(exposure->toHtml()));
}
}
}
//==============================================================================
void PmrWebService::requestWorkspaceClone(PmrWorkspace *pWorkspace,
const QString &pPath)
{
// Let people know that we are (going to be) busy
emit busy(true);
// Get credentials for the given workspace
requestWorkspaceCredentials(pWorkspace);
// Clone the given workspace to the given path
connect(pWorkspace, &PmrWorkspace::warning,
this, &PmrWebService::workspaceErrored);
connect(pWorkspace, &PmrWorkspace::workspaceCloned,
this, &PmrWebService::workspaceCloneFinished);
QtConcurrent::run(pWorkspace, &PmrWorkspace::clone, pPath);
}
//==============================================================================
void PmrWebService::workspaceErrored()
{
// Let people know that we are not busy anymore
emit busy(false);
}
//==============================================================================
void PmrWebService::workspaceCloneFinished(PmrWorkspace *pWorkspace)
{
// Let people know that we are not busy anymore and that a workspace has
// been cloned
emit busy(false);
emit workspaceCloned(pWorkspace);
}
//==============================================================================
void PmrWebService::requestWorkspaceSynchronize(PmrWorkspace *pWorkspace,
bool pPush)
{
// Let people know that we are (going to be) busy
emit busy(true);
// Get credentials for the given workspace
requestWorkspaceCredentials(pWorkspace);
// Synchronise the given workspace
connect(pWorkspace, &PmrWorkspace::workspaceSynchronized,
this, &PmrWebService::workspaceSynchronizeFinished);
QtConcurrent::run(pWorkspace, &PmrWorkspace::synchronize, pPush);
}
//==============================================================================
void PmrWebService::workspaceSynchronizeFinished(PmrWorkspace *pWorkspace)
{
// Let people know that we are not busy anymore and that the given workspace
// has been synchronised
emit busy(false);
emit workspaceSynchronized(pWorkspace);
}
//==============================================================================
void PmrWebService::update(const QString &pPmrUrl)
{
// Keep track of the new PMR URL and then update our PMR Web service manager
mPmrUrl = pPmrUrl;
mPmrWebServiceManager->update(pPmrUrl);
}
//==============================================================================
QString PmrWebService::siteName() const
{
// Determine and return the name of our PMR site
return tr("%1 Instance").arg(Core::formatMessage(QUrl(mPmrUrl).host().split('.').first(), false));
}
//==============================================================================
void PmrWebService::requestWorkspaceCredentials(PmrWorkspace *pWorkspace)
{
// Request credentials for the given workspace
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(pWorkspace->url()+"/request_temporary_password",
true, true);
if (pmrResponse != nullptr) {
pmrResponse->setProperty(WorkspaceProperty, QVariant::fromValue(reinterpret_cast<void *>(pWorkspace)));
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::workspaceCredentialsResponse);
// Don't return until the PMR response has been processed
QEventLoop waitLoop;
connect(pmrResponse, &PmrWebServiceResponse::finished,
&waitLoop, &QEventLoop::quit);
waitLoop.exec();
}
}
//==============================================================================
void PmrWebService::workspaceCredentialsResponse(const QJsonDocument &pJsonDocument)
{
// Retrieve the credentials from the given PMR response
QVariantMap jsonResponse = pJsonDocument.object().toVariantMap();
PmrWorkspace *workspace = reinterpret_cast<PmrWorkspace *>(sender()->property(WorkspaceProperty).value<void *>());
if ((workspace != nullptr) && (jsonResponse["target"].toString() == workspace->url())) {
workspace->setCredentials(jsonResponse["user"].toString(), jsonResponse["key"].toString());
}
}
//==============================================================================
void PmrWebService::requestExposureFileInformation(const QString &pUrl,
PmrExposure *pExposure)
{
// Retrieve some information about the exposure file, which URL is given
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(pUrl, false);
if (pmrResponse != nullptr) {
pmrResponse->setProperty(ExposureProperty, QVariant::fromValue(reinterpret_cast<void *>(pExposure)));
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::exposureFileInformationResponse);
}
}
//==============================================================================
void PmrWebService::exposureFileInformationResponse(const QJsonDocument &pJsonDocument)
{
// Retrieve some information about an exposure file
PmrExposure *exposure = reinterpret_cast<PmrExposure *>(sender()->property(ExposureProperty).value<void *>());
if (exposure != nullptr) {
bool hasExposureFileInformation = false;
QVariantMap collectionMap = pJsonDocument.object().toVariantMap()["collection"].toMap();
QVariantList itemsList = collectionMap["items"].toList();
if (!itemsList.isEmpty()) {
QVariantList linksList = itemsList.first().toMap()["links"].toList();
if (!linksList.isEmpty()) {
// Retrieve the exposure file name, from the exposure file
// information, and keep track of it
QString exposureFile = linksList.first().toMap()["href"].toString().trimmed();
if (!exposureFile.isEmpty()) {
hasExposureFileInformation = true;
exposure->addExposureFile(exposureFile);
mFileExposuresLeftCount.insert(exposure, mFileExposuresLeftCount.value(exposure)-1);
// Check whether the exposure file has a link called
// "Launch with OpenCOR" (e.g. for SED-ML files)
static const QString LaunchWithOpencor = "Launch with OpenCOR";
static const QString Section = "section";
for (const auto &links : collectionMap["links"].toList()) {
QVariantMap linksMap = links.toMap();
QString prompt = linksMap["prompt"].toString();
QString rel = linksMap["rel"].toString();
if ((prompt == LaunchWithOpencor) && (rel == Section)) {
// Our exposure file has a link called
// "Launch with OpenCOR", so check whether its href
// value is already listed in our list of exposure
// files and, if not, add it to it
exposureFile = linksMap["href"].toString().trimmed().remove("opencor://openFile/");
if (!exposureFile.isEmpty()) {
exposure->addExposureFile(exposureFile);
}
}
}
// Let people know that exposure files are available, should
// we have no exposure file URLs left to handle
if (mFileExposuresLeftCount.value(exposure) == 0) {
emit exposureFiles(exposure->url(), exposure->exposureFiles());
}
}
}
}
if (!hasExposureFileInformation) {
emitInformation(tr("No exposure file information could be found for %1.").arg(exposure->toHtml()));
}
}
}
//==============================================================================
void PmrWebService::emitInformation(const QString &pMessage)
{
// Let people know about the given information message
emit information( pMessage+"<br/><br/>"
+tr(R"(<strong>Note:</strong> you might want to email <a href="mailto: help@physiomeproject.org">help@physiomeproject.org</a> and ask why this is the case.)"));
}
//==============================================================================
void PmrWebService::forbidden(const QString &pUrl)
{
// Let people know that access to the given URL is forbidden
emitInformation(tr(R"(Access to <a href="%1">%1</a> is forbidden (you might need to reauthenticate).)").arg(pUrl));
}
//==============================================================================
void PmrWebService::requestExposureFiles(const QString &pUrl)
{
// Request some information about the exposure, which URL is given, and then
// request the coorresponding exposure files
PmrExposure *exposure = mUrlExposures.value(pUrl);
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(exposure->url(), false);
if (pmrResponse != nullptr) {
pmrResponse->setProperty(ExposureProperty, QVariant::fromValue(reinterpret_cast<void *>(exposure)));
pmrResponse->setProperty(NextActionProperty, int(Action::RequestExposureFiles));
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::exposureInformationResponse);
}
}
//==============================================================================
void PmrWebService::exposureInformationResponse(const QJsonDocument &pJsonDocument)
{
// Retrieve some information about an exposure from the given PMR response,
// and then clone it (if requested) and retrieve some information for all
// the corresponding exposure files
PmrExposure *exposure = reinterpret_cast<PmrExposure *>(sender()->property(ExposureProperty).value<void *>());
if (exposure != nullptr) {
// Retrieve the URLs that will help us to retrieve some information
// about the exposure's workspace and its exposure files
static const QString Via = "via";
static const QString Bookmark = "bookmark";
QVariantMap collectionMap = pJsonDocument.object().toVariantMap()["collection"].toMap();
QString workspaceUrl;
QStringList exposureFileUrls;
for (const auto &links : collectionMap["links"].toList()) {
QVariantMap linksMap = links.toMap();
QString rel = linksMap["rel"].toString();
if (rel == Via) {
workspaceUrl = linksMap["href"].toString().trimmed();
} else if (rel == Bookmark) {
QString exposureFileUrl = linksMap["href"].toString().trimmed();
if (!exposureFileUrl.isEmpty()) {
exposureFileUrls << exposureFileUrl;
}
}
}
mFileExposuresLeftCount.insert(exposure, exposureFileUrls.count());
// Make sure that we at least have a workspace
if (workspaceUrl.isEmpty()) {
emitInformation(tr("No workspace could be found for %1.").arg(exposure->toHtml()));
} else {
Action action = Action(sender()->property(NextActionProperty).toInt());
if (action == Action::CloneExposureWorkspace) {
// Retrieve some information about the workspace and then clone
// it
requestWorkspaceInformation(workspaceUrl, QString(), exposure);
} else if ( exposureFileUrls.isEmpty()
&& (action == Action::RequestExposureFiles)) {
emitInformation(tr("No exposure files could be found for %1.").arg(exposure->toHtml()));
}
}
// Retrieve some information about the exposure files, but only if it
// hasn't already been done
if (exposure->exposureFiles().isEmpty()) {
for (const auto &exposureFileUrl : exposureFileUrls) {
requestExposureFileInformation(exposureFileUrl, exposure);
}
}
}
}
//==============================================================================
void PmrWebService::requestExposureWorkspaceClone(const QString &pUrl)
{
// Ask for the workspace associated with the given exposure to be cloned,
// but only if we don't already know about that workspace
PmrExposure *exposure = mUrlExposures.value(pUrl);
if (exposure->workspace() != nullptr) {
QString url = exposure->workspace()->url();
QString dirName = exposure->workspace()->path();
// Check that we aren't already managing a clone of the workspace
if (!dirName.isEmpty()) {
emit warning(tr("The workspace for %1 is already cloned in %2.").arg(url,
dirName));
} else {
PmrWorkspace *existingWorkspace = PmrWorkspaceManager::instance()->workspace(url);
if (existingWorkspace == nullptr) {
// Retrieve the name of an empty directory
dirName = getEmptyDirectory();
if (!dirName.isEmpty()) {
requestWorkspaceClone(exposure->workspace(), dirName);
}
} else {
emit warning(tr("The workspace for %1 is already cloned in %2.").arg(url,
existingWorkspace->path()));
}
}
} else {
// To clone the workspace associated with the given exposure, we first
// need to retrieve some information about the exposure itself
PmrWebServiceResponse *pmrResponse = mPmrWebServiceManager->request(pUrl, false);
if (pmrResponse != nullptr) {
pmrResponse->setProperty(ExposureProperty, QVariant::fromValue(reinterpret_cast<void *>(exposure)));
pmrResponse->setProperty(NextActionProperty, int(Action::CloneExposureWorkspace));
connect(pmrResponse, &PmrWebServiceResponse::response,
this, &PmrWebService::exposureInformationResponse);
}
}
}
//==============================================================================
} // namespace PMRSupport
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================