1 | /****************************************************************************** |
---|
2 | * $Id: CreatorWindowController.m 12943 2011-10-06 02:16:19Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007-2011 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "CreatorWindowController.h" |
---|
26 | #import "NSStringAdditions.h" |
---|
27 | |
---|
28 | #import "transmission.h" // required by utils.h |
---|
29 | #import "utils.h" // tr_urlIsValidTracker() |
---|
30 | |
---|
31 | #define TRACKER_ADD_TAG 0 |
---|
32 | #define TRACKER_REMOVE_TAG 1 |
---|
33 | |
---|
34 | @interface CreatorWindowController (Private) |
---|
35 | |
---|
36 | + (NSString *) chooseFile; |
---|
37 | - (void) locationSheetClosed: (NSSavePanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) info; |
---|
38 | |
---|
39 | - (void) createBlankAddressAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo; |
---|
40 | - (void) createReal; |
---|
41 | - (void) checkProgress; |
---|
42 | - (void) failureSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info; |
---|
43 | |
---|
44 | @end |
---|
45 | |
---|
46 | @implementation CreatorWindowController |
---|
47 | |
---|
48 | + (void) createTorrentFile: (tr_session *) handle |
---|
49 | { |
---|
50 | //get file/folder for torrent |
---|
51 | NSString * path; |
---|
52 | if (!(path = [CreatorWindowController chooseFile])) |
---|
53 | return; |
---|
54 | |
---|
55 | CreatorWindowController * creator = [[self alloc] initWithHandle: handle path: path]; |
---|
56 | [creator showWindow: nil]; |
---|
57 | } |
---|
58 | |
---|
59 | + (void) createTorrentFile: (tr_session *) handle forFile: (NSString *) file |
---|
60 | { |
---|
61 | CreatorWindowController * creator = [[self alloc] initWithHandle: handle path: file]; |
---|
62 | [creator showWindow: nil]; |
---|
63 | } |
---|
64 | |
---|
65 | - (id) initWithHandle: (tr_session *) handle path: (NSString *) path |
---|
66 | { |
---|
67 | if ((self = [super initWithWindowNibName: @"Creator"])) |
---|
68 | { |
---|
69 | fStarted = NO; |
---|
70 | |
---|
71 | fPath = [path retain]; |
---|
72 | fInfo = tr_metaInfoBuilderCreate([fPath UTF8String]); |
---|
73 | |
---|
74 | if (fInfo->fileCount == 0) |
---|
75 | { |
---|
76 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
77 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> no files -> button")]; |
---|
78 | [alert setMessageText: NSLocalizedString(@"This folder contains no files.", |
---|
79 | "Create torrent -> no files -> title")]; |
---|
80 | [alert setInformativeText: NSLocalizedString(@"There must be at least one file in a folder to create a torrent file.", |
---|
81 | "Create torrent -> no files -> warning")]; |
---|
82 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
83 | |
---|
84 | [alert runModal]; |
---|
85 | [alert release]; |
---|
86 | |
---|
87 | [self release]; |
---|
88 | return nil; |
---|
89 | } |
---|
90 | if (fInfo->totalSize == 0) |
---|
91 | { |
---|
92 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
93 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> zero size -> button")]; |
---|
94 | [alert setMessageText: NSLocalizedString(@"The total file size is zero bytes.", |
---|
95 | "Create torrent -> zero size -> title")]; |
---|
96 | [alert setInformativeText: NSLocalizedString(@"A torrent file cannot be created for files with no size.", |
---|
97 | "Create torrent -> zero size -> warning")]; |
---|
98 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
99 | |
---|
100 | [alert runModal]; |
---|
101 | [alert release]; |
---|
102 | |
---|
103 | [self release]; |
---|
104 | return nil; |
---|
105 | } |
---|
106 | |
---|
107 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
108 | |
---|
109 | //get list of trackers |
---|
110 | if (!(fTrackers = [[fDefaults arrayForKey: @"CreatorTrackers"] mutableCopy])) |
---|
111 | { |
---|
112 | fTrackers = [[NSMutableArray alloc] initWithCapacity: 1]; |
---|
113 | |
---|
114 | //check for single tracker from versions before 1.3 |
---|
115 | NSString * tracker; |
---|
116 | if ((tracker = [fDefaults stringForKey: @"CreatorTracker"])) |
---|
117 | { |
---|
118 | [fDefaults removeObjectForKey: @"CreatorTracker"]; |
---|
119 | if (![tracker isEqualToString: @""]) |
---|
120 | { |
---|
121 | [fTrackers addObject: tracker]; |
---|
122 | [fDefaults setObject: fTrackers forKey: @"CreatorTrackers"]; |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | //remove potentially invalid addresses |
---|
128 | for (NSInteger i = [fTrackers count]-1; i >= 0; i--) |
---|
129 | { |
---|
130 | if (!tr_urlIsValidTracker([[fTrackers objectAtIndex: i] UTF8String])) |
---|
131 | [fTrackers removeObjectAtIndex: i]; |
---|
132 | } |
---|
133 | } |
---|
134 | return self; |
---|
135 | } |
---|
136 | |
---|
137 | - (void) awakeFromNib |
---|
138 | { |
---|
139 | NSString * name = [fPath lastPathComponent]; |
---|
140 | |
---|
141 | [[self window] setTitle: name]; |
---|
142 | |
---|
143 | [fNameField setStringValue: name]; |
---|
144 | [fNameField setToolTip: fPath]; |
---|
145 | |
---|
146 | const BOOL multifile = !fInfo->isSingleFile; |
---|
147 | |
---|
148 | NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFileType: multifile |
---|
149 | ? NSFileTypeForHFSTypeCode('fldr') : [fPath pathExtension]]; |
---|
150 | [icon setSize: [fIconView frame].size]; |
---|
151 | [fIconView setImage: icon]; |
---|
152 | |
---|
153 | NSString * statusString = [NSString stringForFileSize: fInfo->totalSize]; |
---|
154 | if (multifile) |
---|
155 | { |
---|
156 | NSString * fileString; |
---|
157 | NSInteger count = fInfo->fileCount; |
---|
158 | if (count != 1) |
---|
159 | fileString = [NSString stringWithFormat: NSLocalizedString(@"%@ files", "Create torrent -> info"), |
---|
160 | [NSString formattedUInteger: count]]; |
---|
161 | else |
---|
162 | fileString = NSLocalizedString(@"1 file", "Create torrent -> info"); |
---|
163 | statusString = [NSString stringWithFormat: @"%@, %@", fileString, statusString]; |
---|
164 | } |
---|
165 | [fStatusField setStringValue: statusString]; |
---|
166 | |
---|
167 | if (fInfo->pieceCount == 1) |
---|
168 | [fPiecesField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"1 piece, %@", "Create torrent -> info"), |
---|
169 | [NSString stringForFileSize: fInfo->pieceSize]]]; |
---|
170 | else |
---|
171 | [fPiecesField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d pieces, %@ each", "Create torrent -> info"), |
---|
172 | fInfo->pieceCount, [NSString stringForFileSize: fInfo->pieceSize]]]; |
---|
173 | |
---|
174 | fLocation = [[[[fDefaults stringForKey: @"CreatorLocation"] stringByExpandingTildeInPath] stringByAppendingPathComponent: |
---|
175 | [name stringByAppendingPathExtension: @"torrent"]] retain]; |
---|
176 | [fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]]; |
---|
177 | [fLocationField setToolTip: fLocation]; |
---|
178 | |
---|
179 | //set previously saved values |
---|
180 | if ([fDefaults objectForKey: @"CreatorPrivate"]) |
---|
181 | [fPrivateCheck setState: [fDefaults boolForKey: @"CreatorPrivate"] ? NSOnState : NSOffState]; |
---|
182 | |
---|
183 | [fOpenCheck setState: [fDefaults boolForKey: @"CreatorOpen"] ? NSOnState : NSOffState]; |
---|
184 | } |
---|
185 | |
---|
186 | - (void) dealloc |
---|
187 | { |
---|
188 | [fPath release]; |
---|
189 | [fLocation release]; |
---|
190 | |
---|
191 | [fTrackers release]; |
---|
192 | |
---|
193 | if (fInfo) |
---|
194 | tr_metaInfoBuilderFree(fInfo); |
---|
195 | |
---|
196 | [fTimer invalidate]; |
---|
197 | |
---|
198 | [super dealloc]; |
---|
199 | } |
---|
200 | |
---|
201 | - (void) setLocation: (id) sender |
---|
202 | { |
---|
203 | NSSavePanel * panel = [NSSavePanel savePanel]; |
---|
204 | |
---|
205 | [panel setPrompt: NSLocalizedString(@"Select", "Create torrent -> location sheet -> button")]; |
---|
206 | [panel setMessage: NSLocalizedString(@"Select the name and location for the torrent file.", |
---|
207 | "Create torrent -> location sheet -> message")]; |
---|
208 | |
---|
209 | [panel setAllowedFileTypes: [NSArray arrayWithObjects: @"org.bittorrent.torrent", @"torrent", nil]]; |
---|
210 | [panel setCanSelectHiddenExtension: YES]; |
---|
211 | |
---|
212 | [panel beginSheetForDirectory: [fLocation stringByDeletingLastPathComponent] file: [fLocation lastPathComponent] |
---|
213 | modalForWindow: [self window] modalDelegate: self didEndSelector: @selector(locationSheetClosed:returnCode:contextInfo:) |
---|
214 | contextInfo: nil]; |
---|
215 | } |
---|
216 | |
---|
217 | - (void) create: (id) sender |
---|
218 | { |
---|
219 | //make sure the trackers are no longer being verified |
---|
220 | if ([fTrackerTable editedRow] != -1) |
---|
221 | [[self window] endEditingFor: fTrackerTable]; |
---|
222 | |
---|
223 | const BOOL isPrivate = [fPrivateCheck state] == NSOnState; |
---|
224 | if ([fTrackers count] == 0 |
---|
225 | && [fDefaults boolForKey: isPrivate ? @"WarningCreatorPrivateBlankAddress" : @"WarningCreatorBlankAddress"]) |
---|
226 | { |
---|
227 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
228 | [alert setMessageText: NSLocalizedString(@"There are no tracker addresses.", "Create torrent -> blank address -> title")]; |
---|
229 | |
---|
230 | NSString * infoString = isPrivate |
---|
231 | ? NSLocalizedString(@"A transfer marked as private with no tracker addresses will be unable to connect to peers." |
---|
232 | " The torrent file will only be useful if you plan to upload the file to a tracker website" |
---|
233 | " that will add the addresses for you.", "Create torrent -> blank address -> message") |
---|
234 | : NSLocalizedString(@"The transfer will not contact trackers for peers, and will have to rely solely on" |
---|
235 | " non-tracker peer discovery methods such as PEX and DHT to download and seed.", |
---|
236 | "Create torrent -> blank address -> message"); |
---|
237 | |
---|
238 | [alert setInformativeText: infoString]; |
---|
239 | [alert addButtonWithTitle: NSLocalizedString(@"Create", "Create torrent -> blank address -> button")]; |
---|
240 | [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Create torrent -> blank address -> button")]; |
---|
241 | [alert setShowsSuppressionButton: YES]; |
---|
242 | |
---|
243 | [alert beginSheetModalForWindow: [self window] modalDelegate: self |
---|
244 | didEndSelector: @selector(createBlankAddressAlertDidEnd:returnCode:contextInfo:) contextInfo: nil]; |
---|
245 | } |
---|
246 | else |
---|
247 | [self createReal]; |
---|
248 | } |
---|
249 | |
---|
250 | - (void) cancelCreateWindow: (id) sender |
---|
251 | { |
---|
252 | [[self window] close]; |
---|
253 | } |
---|
254 | |
---|
255 | - (void) windowWillClose: (NSNotification *) notification |
---|
256 | { |
---|
257 | [self release]; |
---|
258 | } |
---|
259 | |
---|
260 | - (void) cancelCreateProgress: (id) sender |
---|
261 | { |
---|
262 | fInfo->abortFlag = 1; |
---|
263 | [fTimer fire]; |
---|
264 | } |
---|
265 | |
---|
266 | - (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView |
---|
267 | { |
---|
268 | return [fTrackers count]; |
---|
269 | } |
---|
270 | |
---|
271 | - (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row |
---|
272 | { |
---|
273 | return [fTrackers objectAtIndex: row]; |
---|
274 | } |
---|
275 | |
---|
276 | - (void) addRemoveTracker: (id) sender |
---|
277 | { |
---|
278 | //don't allow add/remove when currently adding - it leads to weird results |
---|
279 | if ([fTrackerTable editedRow] != -1) |
---|
280 | return; |
---|
281 | |
---|
282 | if ([[sender cell] tagForSegment: [sender selectedSegment]] == TRACKER_REMOVE_TAG) |
---|
283 | { |
---|
284 | [fTrackers removeObjectsAtIndexes: [fTrackerTable selectedRowIndexes]]; |
---|
285 | |
---|
286 | [fTrackerTable deselectAll: self]; |
---|
287 | [fTrackerTable reloadData]; |
---|
288 | } |
---|
289 | else |
---|
290 | { |
---|
291 | [fTrackers addObject: @""]; |
---|
292 | [fTrackerTable reloadData]; |
---|
293 | |
---|
294 | const NSInteger row = [fTrackers count] - 1; |
---|
295 | [fTrackerTable selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO]; |
---|
296 | [fTrackerTable editColumn: 0 row: row withEvent: nil select: YES]; |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | - (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn |
---|
301 | row: (NSInteger) row |
---|
302 | { |
---|
303 | NSString * tracker = (NSString *)object; |
---|
304 | |
---|
305 | tracker = [tracker stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
---|
306 | |
---|
307 | if ([tracker rangeOfString: @"://"].location == NSNotFound) |
---|
308 | tracker = [@"http://" stringByAppendingString: tracker]; |
---|
309 | |
---|
310 | if (!tr_urlIsValidTracker([tracker UTF8String])) |
---|
311 | { |
---|
312 | NSBeep(); |
---|
313 | [fTrackers removeObjectAtIndex: row]; |
---|
314 | } |
---|
315 | else |
---|
316 | [fTrackers replaceObjectAtIndex: row withObject: tracker]; |
---|
317 | |
---|
318 | [fTrackerTable deselectAll: self]; |
---|
319 | [fTrackerTable reloadData]; |
---|
320 | } |
---|
321 | |
---|
322 | - (void) tableViewSelectionDidChange: (NSNotification *) notification |
---|
323 | { |
---|
324 | [fTrackerAddRemoveControl setEnabled: [fTrackerTable numberOfSelectedRows] > 0 forSegment: TRACKER_REMOVE_TAG]; |
---|
325 | } |
---|
326 | |
---|
327 | - (void) copy: (id) sender |
---|
328 | { |
---|
329 | NSArray * addresses = [fTrackers objectsAtIndexes: [fTrackerTable selectedRowIndexes]]; |
---|
330 | NSString * text = [addresses componentsJoinedByString: @"\n"]; |
---|
331 | |
---|
332 | NSPasteboard * pb = [NSPasteboard generalPasteboard]; |
---|
333 | [pb clearContents]; |
---|
334 | [pb writeObjects: [NSArray arrayWithObject: text]]; |
---|
335 | } |
---|
336 | |
---|
337 | - (BOOL) validateMenuItem: (NSMenuItem *) menuItem |
---|
338 | { |
---|
339 | const SEL action = [menuItem action]; |
---|
340 | |
---|
341 | if (action == @selector(copy:)) |
---|
342 | return [[self window] firstResponder] == fTrackerTable && [fTrackerTable numberOfSelectedRows] > 0; |
---|
343 | |
---|
344 | if (action == @selector(paste:)) |
---|
345 | return [[self window] firstResponder] == fTrackerTable |
---|
346 | && [[NSPasteboard generalPasteboard] canReadObjectForClasses: [NSArray arrayWithObject: [NSString class]] options: nil]; |
---|
347 | |
---|
348 | return YES; |
---|
349 | } |
---|
350 | |
---|
351 | - (void) paste: (id) sender |
---|
352 | { |
---|
353 | NSMutableArray * tempTrackers = [NSMutableArray array]; |
---|
354 | |
---|
355 | NSArray * items = [[NSPasteboard generalPasteboard] readObjectsForClasses: [NSArray arrayWithObject: [NSString class]] options: nil]; |
---|
356 | NSAssert(items != nil, @"no string items to paste; should not be able to call this method"); |
---|
357 | |
---|
358 | for (NSString * pbItem in items) |
---|
359 | { |
---|
360 | for (NSString * tracker in [pbItem componentsSeparatedByString: @"\n"]) |
---|
361 | [tempTrackers addObject: tracker]; |
---|
362 | } |
---|
363 | |
---|
364 | BOOL added = NO; |
---|
365 | |
---|
366 | for (NSString * tracker in tempTrackers) |
---|
367 | { |
---|
368 | tracker = [tracker stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
---|
369 | |
---|
370 | if ([tracker rangeOfString: @"://"].location == NSNotFound) |
---|
371 | tracker = [@"http://" stringByAppendingString: tracker]; |
---|
372 | |
---|
373 | if (tr_urlIsValidTracker([tracker UTF8String])) |
---|
374 | { |
---|
375 | [fTrackers addObject: tracker]; |
---|
376 | added = YES; |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | if (added) |
---|
381 | { |
---|
382 | [fTrackerTable deselectAll: self]; |
---|
383 | [fTrackerTable reloadData]; |
---|
384 | } |
---|
385 | else |
---|
386 | NSBeep(); |
---|
387 | } |
---|
388 | |
---|
389 | @end |
---|
390 | |
---|
391 | @implementation CreatorWindowController (Private) |
---|
392 | |
---|
393 | + (NSString *) chooseFile |
---|
394 | { |
---|
395 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
396 | |
---|
397 | [panel setTitle: NSLocalizedString(@"Create Torrent File", "Create torrent -> select file")]; |
---|
398 | [panel setPrompt: NSLocalizedString(@"Select", "Create torrent -> select file")]; |
---|
399 | [panel setAllowsMultipleSelection: NO]; |
---|
400 | [panel setCanChooseFiles: YES]; |
---|
401 | [panel setCanChooseDirectories: YES]; |
---|
402 | [panel setCanCreateDirectories: NO]; |
---|
403 | |
---|
404 | [panel setMessage: NSLocalizedString(@"Select a file or folder for the torrent file.", "Create torrent -> select file")]; |
---|
405 | |
---|
406 | BOOL success = [panel runModal] == NSOKButton; |
---|
407 | return success ? [[[panel URLs] objectAtIndex: 0] path] : nil; |
---|
408 | } |
---|
409 | |
---|
410 | - (void) locationSheetClosed: (NSSavePanel *) panel returnCode: (NSInteger) code contextInfo: (void *) info |
---|
411 | { |
---|
412 | if (code == NSOKButton) |
---|
413 | { |
---|
414 | [fLocation release]; |
---|
415 | fLocation = [[[panel URL] path] retain]; |
---|
416 | |
---|
417 | [fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]]; |
---|
418 | [fLocationField setToolTip: fLocation]; |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | - (void) createBlankAddressAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo |
---|
423 | { |
---|
424 | if ([[alert suppressionButton] state] == NSOnState) |
---|
425 | { |
---|
426 | [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningCreatorBlankAddress"]; //set regardless of private/public |
---|
427 | if ([fPrivateCheck state] == NSOnState) |
---|
428 | [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningCreatorPrivateBlankAddress"]; |
---|
429 | } |
---|
430 | |
---|
431 | [alert release]; |
---|
432 | |
---|
433 | if (returnCode == NSAlertFirstButtonReturn) |
---|
434 | [self performSelectorOnMainThread: @selector(createReal) withObject: nil waitUntilDone: NO]; |
---|
435 | } |
---|
436 | |
---|
437 | - (void) createReal |
---|
438 | { |
---|
439 | //check if the location currently exists |
---|
440 | if (![[NSFileManager defaultManager] fileExistsAtPath: [fLocation stringByDeletingLastPathComponent]]) |
---|
441 | { |
---|
442 | NSAlert * alert = [[[NSAlert alloc] init] autorelease]; |
---|
443 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> directory doesn't exist warning -> button")]; |
---|
444 | [alert setMessageText: NSLocalizedString(@"The chosen torrent file location does not exist.", |
---|
445 | "Create torrent -> directory doesn't exist warning -> title")]; |
---|
446 | [alert setInformativeText: [NSString stringWithFormat: |
---|
447 | NSLocalizedString(@"The directory \"%@\" does not currently exist. " |
---|
448 | "Create this directory or choose a different one to create the torrent file.", |
---|
449 | "Create torrent -> directory doesn't exist warning -> warning"), |
---|
450 | [fLocation stringByDeletingLastPathComponent]]]; |
---|
451 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
452 | |
---|
453 | [alert beginSheetModalForWindow: [self window] modalDelegate: self didEndSelector: nil contextInfo: nil]; |
---|
454 | return; |
---|
455 | } |
---|
456 | |
---|
457 | //check if a file with the same name and location already exists |
---|
458 | if ([[NSFileManager defaultManager] fileExistsAtPath: fLocation]) |
---|
459 | { |
---|
460 | NSArray * pathComponents = [fLocation pathComponents]; |
---|
461 | NSInteger count = [pathComponents count]; |
---|
462 | |
---|
463 | NSAlert * alert = [[[NSAlert alloc] init] autorelease]; |
---|
464 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> file already exists warning -> button")]; |
---|
465 | [alert setMessageText: NSLocalizedString(@"A torrent file with this name and directory cannot be created.", |
---|
466 | "Create torrent -> file already exists warning -> title")]; |
---|
467 | [alert setInformativeText: [NSString stringWithFormat: |
---|
468 | NSLocalizedString(@"A file with the name \"%@\" already exists in the directory \"%@\". " |
---|
469 | "Choose a new name or directory to create the torrent file.", |
---|
470 | "Create torrent -> file already exists warning -> warning"), |
---|
471 | [pathComponents objectAtIndex: count-1], [pathComponents objectAtIndex: count-2]]]; |
---|
472 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
473 | |
---|
474 | [alert beginSheetModalForWindow: [self window] modalDelegate: self didEndSelector: nil contextInfo: nil]; |
---|
475 | return; |
---|
476 | } |
---|
477 | |
---|
478 | //parse non-empty tracker strings |
---|
479 | tr_tracker_info * trackerInfo = tr_new0(tr_tracker_info, [fTrackers count]); |
---|
480 | |
---|
481 | for (NSUInteger i = 0; i < [fTrackers count]; i++) |
---|
482 | { |
---|
483 | trackerInfo[i].announce = (char *)[[fTrackers objectAtIndex: i] UTF8String]; |
---|
484 | trackerInfo[i].tier = i; |
---|
485 | } |
---|
486 | |
---|
487 | //store values |
---|
488 | [fDefaults setObject: fTrackers forKey: @"CreatorTrackers"]; |
---|
489 | [fDefaults setBool: [fPrivateCheck state] == NSOnState forKey: @"CreatorPrivate"]; |
---|
490 | [fDefaults setBool: [fOpenCheck state] == NSOnState forKey: @"CreatorOpen"]; |
---|
491 | [fDefaults setObject: [fLocation stringByDeletingLastPathComponent] forKey: @"CreatorLocation"]; |
---|
492 | |
---|
493 | [[NSNotificationCenter defaultCenter] postNotificationName: @"BeginCreateTorrentFile" object: fLocation userInfo: nil]; |
---|
494 | tr_makeMetaInfo(fInfo, [fLocation UTF8String], trackerInfo, [fTrackers count], [[fCommentView string] UTF8String], |
---|
495 | [fPrivateCheck state] == NSOnState); |
---|
496 | tr_free(trackerInfo); |
---|
497 | |
---|
498 | fTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkProgress) |
---|
499 | userInfo: nil repeats: YES]; |
---|
500 | } |
---|
501 | |
---|
502 | - (void) checkProgress |
---|
503 | { |
---|
504 | if (fInfo->isDone) |
---|
505 | { |
---|
506 | [fTimer invalidate]; |
---|
507 | fTimer = nil; |
---|
508 | |
---|
509 | NSAlert * alert; |
---|
510 | switch (fInfo->result) |
---|
511 | { |
---|
512 | case TR_MAKEMETA_OK: |
---|
513 | if ([fDefaults boolForKey: @"CreatorOpen"]) |
---|
514 | { |
---|
515 | NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: fLocation, @"File", |
---|
516 | [fPath stringByDeletingLastPathComponent], @"Path", nil]; |
---|
517 | [[NSNotificationCenter defaultCenter] postNotificationName: @"OpenCreatedTorrentFile" object: self userInfo: dict]; |
---|
518 | } |
---|
519 | |
---|
520 | [[self window] close]; |
---|
521 | break; |
---|
522 | |
---|
523 | case TR_MAKEMETA_CANCELLED: |
---|
524 | [[self window] close]; |
---|
525 | break; |
---|
526 | |
---|
527 | default: |
---|
528 | alert = [[[NSAlert alloc] init] autorelease]; |
---|
529 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> failed -> button")]; |
---|
530 | [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Creation of \"%@\" failed.", |
---|
531 | "Create torrent -> failed -> title"), [fLocation lastPathComponent]]]; |
---|
532 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
533 | |
---|
534 | if (fInfo->result == TR_MAKEMETA_IO_READ) |
---|
535 | [alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"Could not read \"%s\": %s.", |
---|
536 | "Create torrent -> failed -> warning"), fInfo->errfile, strerror(fInfo->my_errno)]]; |
---|
537 | else if (fInfo->result == TR_MAKEMETA_IO_WRITE) |
---|
538 | [alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"Could not write \"%s\": %s.", |
---|
539 | "Create torrent -> failed -> warning"), fInfo->errfile, strerror(fInfo->my_errno)]]; |
---|
540 | else //invalid url should have been caught before creating |
---|
541 | [alert setInformativeText: [NSString stringWithFormat: @"%@ (%d)", |
---|
542 | NSLocalizedString(@"An unknown error has occurred.", "Create torrent -> failed -> warning"), fInfo->result]]; |
---|
543 | |
---|
544 | [alert beginSheetModalForWindow: [self window] modalDelegate: self |
---|
545 | didEndSelector: @selector(failureSheetClosed:returnCode:contextInfo:) contextInfo: nil]; |
---|
546 | } |
---|
547 | } |
---|
548 | else |
---|
549 | { |
---|
550 | [fProgressIndicator setDoubleValue: (double)fInfo->pieceIndex / fInfo->pieceCount]; |
---|
551 | |
---|
552 | if (!fStarted) |
---|
553 | { |
---|
554 | fStarted = YES; |
---|
555 | |
---|
556 | [fProgressView setHidden: YES]; |
---|
557 | |
---|
558 | NSWindow * window = [self window]; |
---|
559 | [window setFrameAutosaveName: @""]; |
---|
560 | |
---|
561 | NSRect windowRect = [window frame]; |
---|
562 | CGFloat difference = [fProgressView frame].size.height - [[window contentView] frame].size.height; |
---|
563 | windowRect.origin.y -= difference; |
---|
564 | windowRect.size.height += difference; |
---|
565 | |
---|
566 | //don't allow vertical resizing |
---|
567 | CGFloat height = windowRect.size.height; |
---|
568 | [window setMinSize: NSMakeSize([window minSize].width, height)]; |
---|
569 | [window setMaxSize: NSMakeSize([window maxSize].width, height)]; |
---|
570 | |
---|
571 | [window setContentView: fProgressView]; |
---|
572 | [window setFrame: windowRect display: YES animate: YES]; |
---|
573 | [fProgressView setHidden: NO]; |
---|
574 | |
---|
575 | [[window standardWindowButton: NSWindowCloseButton] setEnabled: NO]; |
---|
576 | } |
---|
577 | } |
---|
578 | } |
---|
579 | |
---|
580 | - (void) failureSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info |
---|
581 | { |
---|
582 | [[alert window] orderOut: nil]; |
---|
583 | [[self window] close]; |
---|
584 | } |
---|
585 | |
---|
586 | @end |
---|