source: trunk/macosx/AddWindowController.m @ 7364

Last change on this file since 7364 was 7364, checked in by livings124, 12 years ago

merge the group location selection code

  • Property svn:keywords set to Date Rev Author Id
File size: 11.1 KB
Line 
1/******************************************************************************
2 * $Id: AddWindowController.m 7364 2008-12-13 22:49:46Z livings124 $
3 *
4 * Copyright (c) 2008 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 "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"
33
34#define UPDATE_SECONDS 1.0
35
36@interface AddWindowController (Private)
37
38- (void) confirmAdd;
39
40- (void) setDestinationPath: (NSString *) destination;
41
42- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo;
43
44- (void) setGroupsMenu;
45- (void) changeGroupValue: (id) sender;
46
47- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
48
49@end
50
51@implementation AddWindowController
52
53- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path lockDestination: (BOOL) lockDestination
54    controller: (Controller *) controller deleteTorrent: (torrentFileState) deleteTorrent
55{
56    if ((self = [super initWithWindowNibName: @"AddWindow"]))
57    {
58        fTorrent = torrent;
59        if (path)
60            fDestination = [[path stringByExpandingTildeInPath] retain];
61        fLockDestination = lockDestination;
62       
63        fController = controller;
64       
65        fDeleteTorrent = deleteTorrent == TORRENT_FILE_DELETE || (deleteTorrent == TORRENT_FILE_DEFAULT
66                            && [[NSUserDefaults standardUserDefaults] boolForKey: @"DeleteOriginalTorrent"]);
67        fDeleteEnable = deleteTorrent == TORRENT_FILE_DEFAULT;
68       
69        fGroupValue = [torrent groupValue];
70    }
71    return self;
72}
73
74- (void) awakeFromNib
75{
76    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateStatusField:)
77        name: @"TorrentFileCheckChange" object: fTorrent];
78   
79    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
80        name: @"UpdateGroups" object: nil];
81   
82    [fFileController setTorrent: fTorrent];
83   
84    NSString * name = [fTorrent name];
85    [[self window] setTitle: name];
86    [fNameField setStringValue: name];
87   
88    NSImage * icon = [[fTorrent icon] copy];
89    [icon setFlipped: NO];
90    [fIconView setImage: icon];
91    [icon release];
92   
93    [self updateStatusField: nil];
94   
95    [self setGroupsMenu];
96    [fGroupPopUp selectItemWithTag: fGroupValue];
97   
98    [fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
99   
100    [fDeleteCheck setState: fDeleteTorrent ? NSOnState : NSOffState];
101    [fDeleteCheck setEnabled: fDeleteEnable];
102   
103    if (fDestination)
104        [self setDestinationPath: fDestination];
105    else
106    {
107        [fLocationField setStringValue: @""];
108        [fLocationImageView setImage: nil];
109    }
110   
111    fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: fFileController
112                selector: @selector(reloadData) userInfo: nil repeats: YES];
113}
114
115- (void) windowDidLoad
116{
117    //if there is no destination, prompt for one right away
118    if (!fDestination)
119        [self setDestination: nil];
120}
121
122- (void) dealloc
123{
124    [[NSNotificationCenter defaultCenter] removeObserver: self];
125   
126    [fTimer invalidate];
127   
128    [fDestination release];
129   
130    [super dealloc];
131}
132
133- (Torrent *) torrent
134{
135    return fTorrent;
136}
137
138- (void) setDestination: (id) sender
139{
140    NSOpenPanel * panel = [NSOpenPanel openPanel];
141
142    [panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
143    [panel setAllowsMultipleSelection: NO];
144    [panel setCanChooseFiles: NO];
145    [panel setCanChooseDirectories: YES];
146    [panel setCanCreateDirectories: YES];
147   
148    [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
149                        "Add -> select destination folder"), [fTorrent name]]];
150   
151    [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [self window] modalDelegate: self
152            didEndSelector: @selector(folderChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
153}
154
155- (void) add: (id) sender
156{
157    if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
158        && [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
159    {
160        NSAlert * alert = [[NSAlert alloc] init];
161        [alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
162                                "Add torrent -> same name -> title")];
163        [alert setInformativeText: NSLocalizedString(@"If you are attempting to use already existing data,"
164            " the root data directory should be inside the destination directory.", "Add torrent -> same name -> message")];
165        [alert setAlertStyle: NSWarningAlertStyle];
166        [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
167        [alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
168       
169        if ([NSApp isOnLeopardOrBetter])
170            [alert setShowsSuppressionButton: YES];
171        else
172            [alert addButtonWithTitle: NSLocalizedString(@"Don't Alert Again", "Add torrent -> same name -> button")];
173       
174        [alert beginSheetModalForWindow: [self window] modalDelegate: self
175            didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
176    }
177    else
178        [self confirmAdd];
179}
180
181- (void) cancelAdd: (id) sender
182{
183    [[self window] performClose: sender];
184}
185
186//only called on cancel
187- (BOOL) windowShouldClose: (id) window
188{
189    [fTimer invalidate];
190    fTimer = nil;
191   
192    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
193   
194    [fController askOpenConfirmed: self add: NO];
195    return YES;
196}
197
198- (void) verifyLocalData: (id) sender
199{
200    [fTorrent resetCache];
201    [fFileController reloadData];
202}
203
204- (void) updateStatusField: (NSNotification *) notification
205{
206    NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
207    if ([fTorrent isFolder])
208    {
209        NSString * fileString;
210        NSInteger count = [fTorrent fileCount];
211        if (count != 1)
212            fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Add torrent -> info"), count];
213        else
214            fileString = NSLocalizedString(@"1 file", "Add torrent -> info");
215       
216        NSString * selectedString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Add torrent -> info"),
217                                        [NSString stringForFileSize: [fTorrent totalSizeSelected]]];
218       
219        statusString = [NSString stringWithFormat: @"%@, %@ (%@)", fileString, statusString, selectedString];
220    }
221   
222    [fStatusField setStringValue: statusString];
223}
224
225- (void) updateGroupMenu: (NSNotification *) notification
226{
227    [self setGroupsMenu];
228    if (![fGroupPopUp selectItemWithTag: fGroupValue])
229    {
230        fGroupValue = -1;
231        [fGroupPopUp selectItemWithTag: fGroupValue];
232    }
233}
234
235@end
236
237@implementation AddWindowController (Private)
238
239- (void) confirmAdd
240{
241    [fTimer invalidate];
242    fTimer = nil;
243   
244    [fTorrent setWaitToStart: [fStartCheck state] == NSOnState];
245    [fTorrent setGroupValue: fGroupValue];
246   
247    if ([fDeleteCheck state] == NSOnState)
248        [fTorrent trashTorrent];
249   
250    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
251   
252    [self close];
253    [fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
254}
255
256- (void) setDestinationPath: (NSString *) destination
257{
258    destination = [destination stringByExpandingTildeInPath];
259    if (!fDestination || ![fDestination isEqualToString: destination])
260    {
261        [fDestination release];
262        fDestination = [destination retain];
263       
264        [fTorrent changeDownloadFolder: fDestination];
265    }
266   
267    [fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
268    [fLocationField setToolTip: fDestination];
269   
270    ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
271    [fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
272    [iconTransformer release];
273}
274
275- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo
276{
277    if (code == NSOKButton)
278    {
279        fLockDestination = NO;
280        [self setDestinationPath: [[openPanel filenames] objectAtIndex: 0]];
281    }
282    else
283    {
284        if (!fDestination)
285            [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
286    }
287}
288
289- (void) setGroupsMenu
290{
291    NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
292    [fGroupPopUp setMenu: groupMenu];
293}
294
295- (void) changeGroupValue: (id) sender
296{
297    NSInteger previousGroup = fGroupValue;
298    fGroupValue = [sender tag];
299   
300    if (!fLockDestination)
301    {
302        if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
303            [self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
304        else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
305            [self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
306        else;
307    }
308}
309
310- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
311{
312    if (([NSApp isOnLeopardOrBetter] ? [[alert suppressionButton] state] == NSOnState : returnCode == NSAlertThirdButtonReturn))
313        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
314   
315    [alert release];
316   
317    if (returnCode == NSAlertSecondButtonReturn)
318        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
319}
320
321@end
Note: See TracBrowser for help on using the repository browser.