1
/******************************************************************************
2
* $Id: AddWindowController.m 7013 2008-11-01 22:08:02Z livings124 $
4
* Copyright (c) 2008 Transmission authors and contributors
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
*****************************************************************************/
25
#import "AddWindowController.h"
26
#import "FileOutlineController.h"
27
#import "Controller.h"
28
#import "GroupsController.h"
29
#import "NSStringAdditions.h"
30
#import "NSMenuAdditions.h"
31
#import "NSApplicationAdditions.h"
32
#import "ExpandedPathToIconTransformer.h"
34
#define UPDATE_SECONDS 1.0
36
@interface AddWindowController (Private)
40
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo;
42
- (void) setGroupsMenu;
43
- (void) changeGroupValue: (id) sender;
45
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
49
@implementation AddWindowController
51
- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path controller: (Controller *) controller
52
deleteTorrent: (torrentFileState) deleteTorrent
54
if ((self = [super initWithWindowNibName: @"AddWindow"]))
58
fDestination = [[path stringByExpandingTildeInPath] retain];
60
fController = controller;
62
fDeleteTorrent = deleteTorrent == TORRENT_FILE_DELETE || (deleteTorrent == TORRENT_FILE_DEFAULT
63
&& [[NSUserDefaults standardUserDefaults] boolForKey: @"DeleteOriginalTorrent"]);
64
fDeleteEnable = deleteTorrent == TORRENT_FILE_DEFAULT;
73
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateStatusField:)
74
name: @"TorrentFileCheckChange" object: fTorrent];
76
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
77
name: @"UpdateGroups" object: nil];
79
[fFileController setTorrent: fTorrent];
81
NSString * name = [fTorrent name];
82
[[self window] setTitle: name];
83
[fNameField setStringValue: name];
85
NSImage * icon = [[fTorrent icon] copy];
86
[icon setFlipped: NO];
87
[fIconView setImage: icon];
90
[self updateStatusField: nil];
93
[fGroupPopUp selectItemWithTag: -1];
95
[fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
97
[fDeleteCheck setState: fDeleteTorrent ? NSOnState : NSOffState];
98
[fDeleteCheck setEnabled: fDeleteEnable];
102
[fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
103
[fLocationField setToolTip: fDestination];
105
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
106
[fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
107
[iconTransformer release];
111
[fLocationField setStringValue: @""];
112
[fLocationImageView setImage: nil];
115
fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: fFileController
116
selector: @selector(reloadData) userInfo: nil repeats: YES];
119
- (void) windowDidLoad
121
//if there is no destination, prompt for one right away
123
[self setDestination: nil];
128
[[NSNotificationCenter defaultCenter] removeObserver: self];
132
[fDestination release];
137
- (Torrent *) torrent
142
- (void) setDestination: (id) sender
144
NSOpenPanel * panel = [NSOpenPanel openPanel];
146
[panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
147
[panel setAllowsMultipleSelection: NO];
148
[panel setCanChooseFiles: NO];
149
[panel setCanChooseDirectories: YES];
150
[panel setCanCreateDirectories: YES];
152
[panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
153
"Add -> select destination folder"), [fTorrent name]]];
155
[panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [self window] modalDelegate: self
156
didEndSelector: @selector(folderChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
159
- (void) add: (id) sender
161
if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
162
&& [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
164
NSAlert * alert = [[NSAlert alloc] init];
165
[alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
166
"Add torrent -> same name -> title")];
167
[alert setInformativeText: NSLocalizedString(@"If you are attempting to use already existing data,"
168
" the root data directory should be inside the destination directory.", "Add torrent -> same name -> message")];
169
[alert setAlertStyle: NSWarningAlertStyle];
170
[alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
171
[alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
173
if ([NSApp isOnLeopardOrBetter])
174
[alert setShowsSuppressionButton: YES];
176
[alert addButtonWithTitle: NSLocalizedString(@"Don't Alert Again", "Add torrent -> same name -> button")];
178
[alert beginSheetModalForWindow: [self window] modalDelegate: self
179
didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
185
- (void) cancelAdd: (id) sender
187
[[self window] performClose: sender];
190
//only called on cancel
191
- (BOOL) windowShouldClose: (id) window
196
[fFileController setTorrent: nil]; //avoid a crash when window tries to update
198
[fController askOpenConfirmed: self add: NO];
202
- (void) verifyLocalData: (id) sender
204
[fTorrent resetCache];
205
[fFileController reloadData];
208
- (void) updateStatusField: (NSNotification *) notification
210
NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
211
if ([fTorrent isFolder])
213
NSString * fileString;
214
NSInteger count = [fTorrent fileCount];
216
fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Add torrent -> info"), count];
218
fileString = NSLocalizedString(@"1 file", "Add torrent -> info");
220
NSString * selectedString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Add torrent -> info"),
221
[NSString stringForFileSize: [fTorrent totalSizeSelected]]];
223
statusString = [NSString stringWithFormat: @"%@, %@ (%@)", fileString, statusString, selectedString];
226
[fStatusField setStringValue: statusString];
229
- (void) updateGroupMenu: (NSNotification *) notification
231
[self setGroupsMenu];
232
if (![fGroupPopUp selectItemWithTag: fGroupValue])
235
[fGroupPopUp selectItemWithTag: fGroupValue];
239
- (void) showGroupsWindow: (id) sender
241
[fGroupPopUp selectItemWithTag: fGroupValue];
242
[fController showGroups: sender];
247
@implementation AddWindowController (Private)
254
[fTorrent setWaitToStart: [fStartCheck state] == NSOnState];
255
[fTorrent setGroupValue: [[fGroupPopUp selectedItem] tag]];
257
if ([fDeleteCheck state] == NSOnState)
258
[fTorrent trashTorrent];
260
[fFileController setTorrent: nil]; //avoid a crash when window tries to update
263
[fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
266
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo
268
if (code == NSOKButton)
270
[fDestination release];
271
fDestination = [[[openPanel filenames] objectAtIndex: 0] retain];
273
[fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
274
[fLocationField setToolTip: fDestination];
276
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
277
[fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
278
[iconTransformer release];
280
[fTorrent changeDownloadFolder: fDestination];
285
[self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
289
- (void) setGroupsMenu
291
NSMenu * menu = [fGroupPopUp menu];
293
for (NSInteger i = [menu numberOfItems]-1 - 2; i >= 0; i--)
294
[menu removeItemAtIndex: i];
296
NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
297
[menu appendItemsFromMenu: groupMenu atIndexes: [NSIndexSet indexSetWithIndexesInRange:
298
NSMakeRange(0, [groupMenu numberOfItems])] atBottom: NO];
301
- (void) changeGroupValue: (id) sender
303
fGroupValue = [sender tag];
306
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
308
if (([NSApp isOnLeopardOrBetter] ? [[alert suppressionButton] state] == NSOnState : returnCode == NSAlertThirdButtonReturn))
309
[[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
313
if (returnCode == NSAlertSecondButtonReturn)
314
[self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];