forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowserController+Sources+Copy.m
318 lines (259 loc) · 15.2 KB
/
BrowserController+Sources+Copy.m
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
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "BrowserController+Sources+Copy.h"
#import "DicomImage.h"
#import "DicomFile.h"
#import "DicomDatabase.h"
#import "DataNodeIdentifier.h"
#import "DCMNetServiceDelegate.h"
#import "MutableArrayCategory.h"
#import "ThreadsManager.h"
#import "RemoteDicomDatabase.h"
#import "NSThread+N2.h"
#import "N2Debug.h"
#import "N2Stuff.h"
@implementation BrowserController (SourcesCopy)
-(void)copyImagesToLocalBrowserSourceThread:(NSArray*)io
{
@autoreleasepool
{
if( io.count < 4)
{
NSLog( @"******* copyImagesToLocalBrowserSourceThread : io.count < 4");
return;
}
@try
{
NSThread* thread = [NSThread currentThread];
DicomDatabase *srcDatabase = [io objectAtIndex:2];
NSArray* dicomImages = [srcDatabase.independentDatabase objectsWithIDs: [io objectAtIndex: 0]];
NSMutableArray* imagePaths = [NSMutableArray array];
for (DicomImage* image in dicomImages)
if (![imagePaths containsObject:image.completePath])
[imagePaths addObject:image.completePath];
thread.status = NSLocalizedString(@"Opening database...", nil);
DicomDatabase* dstDatabase = [[io objectAtIndex:3] independentDatabase];
thread.status = [NSString stringWithFormat:NSLocalizedString(@"Copying %@ %@...", nil), N2LocalizedDecimal( imagePaths.count), (imagePaths.count == 1 ? NSLocalizedString(@"file", nil) : NSLocalizedString(@"files", nil)) ];
NSMutableArray* dstPaths = [NSMutableArray array];
NSTimeInterval fiveSeconds = [NSDate timeIntervalSinceReferenceDate] + 5;
NSTimeInterval oneSecond = [NSDate timeIntervalSinceReferenceDate] + 1;
for (NSInteger i = 0; i < imagePaths.count; ++i)
{
thread.progress = 1.0*i/imagePaths.count;
if (thread.isCancelled)
break;
NSString* srcPath = [imagePaths objectAtIndex:i];
NSString* dstPath = [dstDatabase uniquePathForNewDataFileWithExtension: @"dcm"];
if( dstPath.length)
{
static NSString *oneCopyAtATime = @"oneCopyAtATime";
@synchronized( oneCopyAtATime)
{
if( srcDatabase.isReadOnly)
{
NSTask *t = [NSTask launchedTaskWithLaunchPath: @"/bin/cp" arguments: @[srcPath, dstPath]];
while( [t isRunning]){};
}
else if( [[NSFileManager defaultManager] copyItemAtPath: srcPath toPath: dstPath error: nil] == NO)
NSLog( @"**** copyItemAtPath failed: %@", dstPath);
if( [[NSFileManager defaultManager] fileExistsAtPath: dstPath])
{
if( [DicomFile isDICOMFile: dstPath] == NO)
[[NSFileManager defaultManager] moveItemAtPath: dstPath toPath: [[dstPath stringByDeletingPathExtension] stringByAppendingPathExtension: [srcPath pathExtension]] error: nil];
[dstPaths addObject:dstPath];
}
}
}
if( fiveSeconds < [NSDate timeIntervalSinceReferenceDate])
{
thread.status = [NSString stringWithFormat:NSLocalizedString(@"Indexing %@ %@...", nil), N2LocalizedDecimal( dstPaths.count), (dstPaths.count == 1 ? NSLocalizedString(@"file", nil) : NSLocalizedString(@"files", nil))];
[dstDatabase addFilesAtPaths: dstPaths postNotifications: YES dicomOnly: [[NSUserDefaults standardUserDefaults] boolForKey: @"onlyDICOM"] rereadExistingItems:NO generatedByOsiriX: NO importedFiles: YES returnArray: NO];
[dstPaths removeAllObjects];
fiveSeconds = [NSDate timeIntervalSinceReferenceDate] + 5;
}
if( oneSecond < [NSDate timeIntervalSinceReferenceDate])
{
thread.status = [NSString stringWithFormat:NSLocalizedString(@"Copying %@ %@...", nil), N2LocalizedDecimal( (long)imagePaths.count-i), ((long)imagePaths.count-i == 1 ? NSLocalizedString(@"file", nil) : NSLocalizedString(@"files", nil))];
oneSecond = [NSDate timeIntervalSinceReferenceDate] + 1;
}
}
thread.status = [NSString stringWithFormat:NSLocalizedString(@"Indexing %@ %@...", nil), N2LocalizedDecimal( dstPaths.count), (dstPaths.count == 1 ? NSLocalizedString(@"file", nil) : NSLocalizedString(@"files", nil))];
thread.progress = -1;
[dstDatabase addFilesAtPaths:dstPaths];
}
@catch (NSException *exception) {
N2LogException( exception);
}
}
}
-(void)copyImagesToRemoteBrowserSourceThread:(NSArray*)io
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSThread* thread = [NSThread currentThread];
DataNodeIdentifier* destination = [io objectAtIndex:1];
DicomDatabase *srcDatabase = [io objectAtIndex:2];
NSArray* dicomImages = [srcDatabase.independentDatabase objectsWithIDs: [io objectAtIndex: 0]];
NSMutableArray* imagePaths = [NSMutableArray array];
NSMutableArray* imagePathsObjs = [NSMutableArray array];
for (DicomImage* image in dicomImages)
if (![imagePaths containsObject:image.completePath]) {
[imagePaths addObject:image.completePath];
[imagePathsObjs addObject:image];
}
thread.status = NSLocalizedString(@"Opening database...", nil);
@try
{
RemoteDicomDatabase* dstDatabase = [RemoteDicomDatabase databaseForLocation:destination.location port:destination.port name:destination.description update:NO];
thread.status = [NSString stringWithFormat:NSLocalizedString(@"Sending %@ %@...", nil), N2LocalizedDecimal( imagePaths.count), (imagePaths.count == 1 ? NSLocalizedString(@"file", nil) : NSLocalizedString(@"files", nil)) ];
[dstDatabase uploadFilesAtPaths:imagePaths imageObjects:nil];
}
@catch (NSException* e)
{
thread.status = NSLocalizedString(@"Error: destination is unavailable", nil);
N2LogExceptionWithStackTrace(e);
[NSThread sleepForTimeInterval:1];
}
[pool release];
}
-(void)copyRemoteImagesToLocalBrowserSourceThread:(NSArray*)io
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSThread* thread = [NSThread currentThread];
DataNodeIdentifier* destination = [io objectAtIndex:1];
RemoteDicomDatabase* srcDatabase = [io objectAtIndex:2];
NSMutableArray* dicomImages = [[[srcDatabase.independentDatabase objectsWithIDs: [io objectAtIndex: 0]] mutableCopy] autorelease];
NSMutableArray* imagePaths = [[[dicomImages valueForKey:@"completePath"] mutableCopy] autorelease];
[imagePaths removeDuplicatedStringsInSyncWithThisArray:dicomImages];
thread.status = NSLocalizedString(@"Opening database...", nil);
DicomDatabase* idatabase = [[DicomDatabase databaseAtPath:destination.location name:destination.description] independentDatabase];
thread.status = [NSString stringWithFormat:NSLocalizedString(@"Fetching %@ %@...", nil), N2LocalizedDecimal( dicomImages.count), (dicomImages.count == 1 ? NSLocalizedString(@"file", nil) : NSLocalizedString(@"files", nil)) ];
NSMutableArray* dstPaths = [NSMutableArray array];
for (NSInteger i = 0; i < dicomImages.count; ++i)
{
@try
{
DicomImage* dicomImage = [dicomImages objectAtIndex:i];
NSString* srcPath = [srcDatabase cacheDataForImage:dicomImage maxFiles:0];
if (srcPath)
{
NSString* ext = [DicomFile isDICOMFile:srcPath]? @"dcm" : srcPath.pathExtension;
NSString* dstPath = [idatabase uniquePathForNewDataFileWithExtension:ext];
if( dstPath.length)
if ([[NSFileManager defaultManager] moveItemAtPath:srcPath toPath:dstPath error:NULL])
[dstPaths addObject:dstPath];
}
}
@catch (NSException *exception)
{
N2LogExceptionWithStackTrace( exception);
}
thread.progress = 1.0*i/dicomImages.count;
if (thread.isCancelled)
break;
}
thread.status = NSLocalizedString(@"Indexing files...", nil);
thread.progress = -1;
[idatabase addFilesAtPaths:dstPaths];
[pool release];
}
-(void)copyRemoteImagesToRemoteBrowserSourceThread:(NSArray*)io
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSThread* thread = [NSThread currentThread];
DataNodeIdentifier* destination = [io objectAtIndex:1];
RemoteDicomDatabase* srcDatabase = [io objectAtIndex:2];
NSMutableArray* dicomImages = [[[srcDatabase.independentDatabase objectsWithIDs: [io objectAtIndex: 0]] mutableCopy] autorelease];
NSMutableArray* imagePaths = [[[dicomImages valueForKey:@"completePath"] mutableCopy] autorelease];
[imagePaths removeDuplicatedStringsInSyncWithThisArray:dicomImages];
NSString* dstAddress = nil;
NSString* dstAET = nil;
NSInteger dstPort = 0;
NSInteger dstSyntax = 0;
if ([destination isKindOfClass:[RemoteDatabaseNodeIdentifier class]])
{
[RemoteDatabaseNodeIdentifier location:destination.location port:destination.port toAddress:&dstAddress port:NULL];
dstPort = [[destination.dictionary objectForKey:@"port"] integerValue];
dstAET = [destination.dictionary objectForKey:@"AETitle"];
if (!dstAET || !dstPort || !dstSyntax)
{
thread.status = NSLocalizedString(@"Fetching destination information...", nil);
NSDictionary* dstInfo = nil;
@try
{
RemoteDicomDatabase* dstDatabase = [RemoteDicomDatabase databaseForLocation:destination.location port:destination.port name:destination.description update:NO];
dstInfo = [dstDatabase fetchDicomDestinationInfo];
} @catch (NSException* e)
{
thread.status = NSLocalizedString(@"Error: destination is unavailable", nil);
N2LogExceptionWithStackTrace(e);
[NSThread sleepForTimeInterval:1];
}
if ([dstInfo objectForKey:@"AETitle"]) dstAET = [dstInfo objectForKey:@"AETitle"];
if ([dstInfo objectForKey:@"Port"]) dstPort = [[dstInfo objectForKey:@"Port"] integerValue];
if ([dstInfo objectForKey:@"TransferSyntax"]) dstSyntax = [[dstInfo objectForKey:@"TransferSyntax"] integerValue];
}
} else if ([destination isKindOfClass:[DicomNodeIdentifier class]])
{
[DicomNodeIdentifier location:destination.location port:destination.port toAddress:&dstAddress port:&dstPort aet:&dstAET];
dstSyntax = [[destination.dictionary objectForKey:@"TransferSyntax"] integerValue];
}
thread.status = [NSString stringWithFormat:NSLocalizedString(@"Sending SCU request...", nil), dicomImages.count];
[srcDatabase storeScuImages:dicomImages toDestinationAETitle:dstAET address:dstAddress port:dstPort transferSyntax:dstSyntax];
[pool release];
}
-(BOOL)initiateCopyImages:(NSArray*)dicomImages toSource:(DataNodeIdentifier*)destination
{
if (_database.isLocal)
{
if ([destination isKindOfClass:[LocalDatabaseNodeIdentifier class]]) { // local OsiriX to local OsiriX
DicomDatabase *dst = [DicomDatabase databaseAtPath:destination.location]; // Create the mainDatabase on the MAIN thread, if necessary !
NSThread* thread = [[[NSThread alloc] initWithTarget:self selector:@selector(copyImagesToLocalBrowserSourceThread:) object:[NSArray arrayWithObjects: [dicomImages valueForKey:@"objectID"], destination, _database, dst, NULL]] autorelease];
thread.name = NSLocalizedString(@"Copying images...", nil);
thread.supportsCancel = YES;
[[ThreadsManager defaultManager] addThreadAndStart:thread];
return YES;
} else if ([destination isKindOfClass:[RemoteDatabaseNodeIdentifier class]]) { // local OsiriX to remote OsiriX
NSThread* thread = [[[NSThread alloc] initWithTarget:self selector:@selector(copyImagesToRemoteBrowserSourceThread:) object:[NSArray arrayWithObjects: [dicomImages valueForKey:@"objectID"], destination, _database, NULL]] autorelease];
thread.supportsCancel = YES;
thread.name = NSLocalizedString(@"Sending images...", nil);
[[ThreadsManager defaultManager] addThreadAndStart:thread];
return YES;
} else if ([destination isKindOfClass:[DicomNodeIdentifier class]]) { // local OsiriX to remote DICOM
NSArray* r = [DCMNetServiceDelegate DICOMServersListSendOnly:YES QROnly:NO];
for (int i = 0; i < r.count; ++i)
if ([destination isEqualToDictionary:[r objectAtIndex:i]])
[[NSUserDefaults standardUserDefaults] setInteger:i forKey:@"lastSendServer"];
[self selectServer:dicomImages];
return YES;
// [_database storeScuImages:dicomImages toDestinationAETitle:(NSString*)aet address:(NSString*)address port:(NSInteger)port transferSyntax:(int)exsTransferSyntax];
}
}
else
{
if ([destination isKindOfClass:[LocalDatabaseNodeIdentifier class]])
{ // remote OsiriX to local OsiriX
[DicomDatabase databaseAtPath:destination.location]; // Create the mainDatabase on the MAIN thread, if necessary !
NSThread* thread = [[[NSThread alloc] initWithTarget:self selector:@selector(copyRemoteImagesToLocalBrowserSourceThread:) object:[NSArray arrayWithObjects: [dicomImages valueForKey:@"objectID"], destination, _database, NULL]] autorelease];
thread.name = NSLocalizedString(@"Copying images...", nil);
thread.supportsCancel = YES;
[[ThreadsManager defaultManager] addThreadAndStart:thread];
return YES;
} else if ([destination isKindOfClass:[RemoteDatabaseNodeIdentifier class]] || [destination isKindOfClass:[DicomNodeIdentifier class]]) { // remote OsiriX to remote OsiriX // remote OsiriX to remote DICOM
NSThread* thread = [[[NSThread alloc] initWithTarget:self selector:@selector(copyRemoteImagesToRemoteBrowserSourceThread:) object:[NSArray arrayWithObjects: [dicomImages valueForKey:@"objectID"], destination, _database, NULL]] autorelease];
thread.name = NSLocalizedString(@"Initiating image transfer...", nil);
thread.supportsCancel = YES;
[[ThreadsManager defaultManager] addThreadAndStart:thread];
return YES;
}
}
return NO;
}
@end