source: branches/file_selection/macosx/CreatorWindowController.m @ 2130

Last change on this file since 2130 was 2130, checked in by livings124, 15 years ago

instead of a sheet when creating the torrent file, turn the whole window into the progress bar

File size: 10.9 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2007 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 "StringAdditions.h"
27
28#define DEFAULT_SAVE_LOCATION @"~/Desktop/"
29
30@interface CreatorWindowController (Private)
31
32+ (NSString *) chooseFile;
33- (void) locationSheetClosed: (NSSavePanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
34- (void) checkProgress;
35- (void) failureSheetClosed: (NSAlert *) alert returnCode: (int) code contextInfo: (void *) info;
36
37@end
38
39@implementation CreatorWindowController
40
41+ (void) createTorrentFile: (tr_handle_t *) handle
42{
43    //get file/folder for torrent
44    NSString * path;
45    if (!(path = [CreatorWindowController chooseFile]))
46        return;
47   
48    CreatorWindowController * creator = [[self alloc] initWithWindowNibName: @"Creator" handle: handle path: path];
49    [creator showWindow: nil];
50}
51
52- (id) initWithWindowNibName: (NSString *) name handle: (tr_handle_t *) handle path: (NSString *) path
53{
54    if ((self = [super initWithWindowNibName: name]))
55    {
56        fStarted = NO;
57       
58        fPath = [path retain];
59        fInfo = tr_metaInfoBuilderCreate(handle, [fPath UTF8String]);
60        if (fInfo->fileCount == 0)
61        {
62            NSAlert * alert = [[[NSAlert alloc] init] autorelease];
63            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> no files -> button")];
64            [alert setMessageText: NSLocalizedString(@"This folder contains no files.",
65                                                    "Create torrent -> no files -> title")];
66            [alert setInformativeText: NSLocalizedString(@"There must be at least one file in a folder to create a torrent file.",
67                                                        "Create torrent -> no files -> warning")];
68            [alert setAlertStyle: NSWarningAlertStyle];
69           
70            [alert runModal];
71           
72            [self release];
73            return nil;
74        }
75    }
76    return self;
77}
78
79- (void) awakeFromNib
80{
81    NSString * name = [fPath lastPathComponent];
82   
83    [[self window] setTitle: name];
84   
85    [fNameField setStringValue: name];
86    [fNameField setToolTip: fPath];
87   
88    BOOL multifile = !fInfo->isSingleFile;
89   
90    NSImage * icon = [[[NSWorkspace sharedWorkspace] iconForFileType: multifile
91                        ? NSFileTypeForHFSTypeCode('fldr') : [fPath pathExtension]] copy];
92    [fIcon setImage: icon];
93    [icon release];
94   
95    NSString * statusString = [NSString stringForFileSize: fInfo->totalSize];
96    if (multifile)
97    {
98        NSString * fileString;
99        int count = fInfo->fileCount;
100        if (count != 1)
101            fileString = [NSString stringWithFormat: NSLocalizedString(@"%d Files, ", "Create torrent -> info"), count];
102        else
103            fileString = NSLocalizedString(@"1 File, ", "Create torrent -> info");
104        statusString = [fileString stringByAppendingString: statusString];
105    }
106    [fStatusField setStringValue: statusString];
107   
108    NSString * piecesCountString;
109    int piecesCount = fInfo->pieceCount;
110    if (piecesCount == 1)
111        piecesCountString = NSLocalizedString(@"1 piece", "Create torrent -> info");
112    else
113        piecesCountString = [NSString stringWithFormat: NSLocalizedString(@"%d pieces", "Create torrent -> info"),
114                                                                piecesCount];
115    [fPiecesField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@, %@ each", "Create torrent -> info"),
116                                        piecesCountString, [NSString stringForFileSize: fInfo->pieceSize]]];
117   
118    fLocation = [[[DEFAULT_SAVE_LOCATION stringByAppendingPathComponent: [name stringByAppendingPathExtension: @"torrent"]]
119                                            stringByExpandingTildeInPath] retain];
120    [fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]];
121    [fLocationField setToolTip: fLocation];
122}
123
124- (void) dealloc
125{
126    [fPath release];
127    if (fLocation)
128        [fLocation release];
129   
130    if (fInfo)
131        tr_metaInfoBuilderFree(fInfo);
132   
133    if (fTimer)
134        [fTimer invalidate];
135   
136    [super dealloc];
137}
138
139- (void) setLocation: (id) sender
140{
141    NSSavePanel * panel = [NSSavePanel savePanel];
142
143    [panel setPrompt: @"Select"];
144    [panel setRequiredFileType: @"torrent"];
145    [panel setCanSelectHiddenExtension: YES];
146
147    [panel beginSheetForDirectory: nil file: [fLocation lastPathComponent] modalForWindow: [self window] modalDelegate: self
148            didEndSelector: @selector(locationSheetClosed:returnCode:contextInfo:) contextInfo: nil];
149}
150
151- (void) create: (id) sender
152{
153    #warning check already exists
154   
155    //parse tracker string
156    NSString * trackerString = [fTrackerField stringValue];
157    if ([trackerString rangeOfString: @"://"].location != NSNotFound)
158    {
159        if (![trackerString hasPrefix: @"http://"])
160        {
161            NSAlert * alert = [[[NSAlert alloc] init] autorelease];
162            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> http warning -> button")];
163            [alert setMessageText: NSLocalizedString(@"Tracker address must begin with \"http://\".",
164                                                    "Create torrent -> http warning -> title")];
165            [alert setInformativeText: NSLocalizedString(@"Change the tracker address to create the torrent.",
166                                                        "Create torrent -> http warning -> warning")];
167            [alert setAlertStyle: NSWarningAlertStyle];
168           
169            [alert beginSheetModalForWindow: [self window] modalDelegate: self didEndSelector: nil contextInfo: nil];
170            return;
171        }
172    }
173    else
174        trackerString = [@"http://" stringByAppendingString: trackerString];
175   
176    tr_makeMetaInfo(fInfo, [fLocation UTF8String], [trackerString UTF8String], [[fCommentView string] UTF8String],
177                    [fPrivateCheck state] == NSOnState);
178    #warning delay at first?
179    fTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkProgress)
180                        userInfo: nil repeats: YES];
181}
182
183- (void) cancelCreateWindow: (id) sender
184{
185    [[self window] close];
186}
187
188- (void) windowWillClose: (NSNotification *) notification
189{
190    [self release];
191}
192
193- (void) cancelCreateProgress: (id) sender
194{
195    fInfo->abortFlag = 1;
196    [fTimer fire];
197}
198
199@end
200
201@implementation CreatorWindowController (Private)
202
203+ (NSString *) chooseFile
204{
205    NSOpenPanel * panel = [NSOpenPanel openPanel];
206   
207    [panel setPrompt: NSLocalizedString(@"Select", "Create torrent -> select file")];
208    [panel setAllowsMultipleSelection: NO];
209    [panel setCanChooseFiles: YES];
210    [panel setCanChooseDirectories: YES];
211    [panel setCanCreateDirectories: NO];
212
213    [panel setMessage: NSLocalizedString(@"Select a file or folder for the torrent file.", "Create torrent -> select file")];
214   
215    BOOL success = [panel runModal] == NSOKButton;
216    return success ? [[panel filenames] objectAtIndex: 0] : nil;
217}
218
219- (void) locationSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (void *) info
220{
221    if (code == NSOKButton)
222    {
223        [fLocation release];
224        fLocation = [[panel filename] retain];
225       
226        [fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]];
227        [fLocationField setToolTip: fLocation];
228    }
229}
230
231- (void) checkProgress
232{
233    if (fInfo->isDone)
234    {
235        [fTimer invalidate];
236        fTimer = nil;
237       
238        if (fInfo->failed)
239        {
240            if (!fInfo->abortFlag)
241            {
242                NSAlert * alert = [[[NSAlert alloc] init] autorelease];
243                [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> failed -> button")];
244                [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Creation of \"%@\" failed.",
245                                                "Create torrent -> failed -> title"), [fLocation lastPathComponent]]];
246                [alert setInformativeText: NSLocalizedString(@"There was an error parsing the data file. "
247                                            "The torrent file was not created.", "Create torrent -> failed -> warning")];
248                [alert setAlertStyle: NSWarningAlertStyle];
249               
250                [alert beginSheetModalForWindow: [self window] modalDelegate: self
251                        didEndSelector: @selector(failureSheetClosed:returnCode:contextInfo:) contextInfo: nil];
252                return;
253            }
254        }
255        else
256        {
257            #warning add to T
258        }
259       
260        [[self window] close];
261    }
262    else
263    {
264        [fProgressIndicator setDoubleValue: (double)fInfo->pieceIndex / fInfo->pieceCount];
265       
266        if (!fStarted)
267        {
268            NSWindow * window = [self window];
269           
270            NSRect windowRect = [window frame];
271            float difference = [fProgressView frame].size.height - [[window contentView] frame].size.height;
272            windowRect.origin.y -= difference;
273            windowRect.size.height += difference;
274           
275            //don't allow vertical resizing
276            float height = windowRect.size.height;
277            [window setMinSize: NSMakeSize([window minSize].width, height)];
278            [window setMaxSize: NSMakeSize([window maxSize].width, height)];
279           
280            [window setContentView: fProgressView];
281            [window setFrame: windowRect display: YES animate: YES];
282            [fProgressView setHidden: NO];
283           
284            fStarted = YES;
285        }
286    }
287}
288
289- (void) failureSheetClosed: (NSAlert *) alert returnCode: (int) code contextInfo: (void *) info
290{
291    [[alert window] orderOut: nil];
292    [[self window] close];
293}
294
295@end
Note: See TracBrowser for help on using the repository browser.