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

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

cancel while torrent is being created now works

File size: 9.0 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: (NSTimer *) timer;
35
36@end
37
38@implementation CreatorWindowController
39
40+ (void) createTorrentFile: (tr_handle_t *) handle
41{
42    //get file/folder for torrent
43    NSString * path;
44    if (!(path = [CreatorWindowController chooseFile]))
45        return;
46   
47    CreatorWindowController * creator = [[self alloc] initWithWindowNibName: @"Creator" handle: handle path: path];
48    [creator showWindow: nil];
49}
50
51- (id) initWithWindowNibName: (NSString *) name handle: (tr_handle_t *) handle path: (NSString *) path
52{
53    if ((self = [super initWithWindowNibName: name]))
54    {
55        fPath = [path retain];
56        fInfo = tr_metaInfoBuilderCreate(handle, [fPath UTF8String]);
57        if (fInfo->fileCount == 0)
58        {
59            NSAlert * alert = [[[NSAlert alloc] init] autorelease];
60            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> no files -> button")];
61            [alert setMessageText: NSLocalizedString(@"This folder contains no files.",
62                                                    "Create torrent -> no files -> title")];
63            [alert setInformativeText: NSLocalizedString(@"There must be at least one file in a folder to create a torrent file.",
64                                                        "Create torrent -> no files -> warning")];
65            [alert setAlertStyle: NSWarningAlertStyle];
66           
67            [alert runModal];
68           
69            [self release];
70            return nil;
71        }
72    }
73    return self;
74}
75
76- (void) awakeFromNib
77{
78    NSString * name = [fPath lastPathComponent];
79   
80    [[self window] setTitle: name];
81   
82    [fNameField setStringValue: name];
83    [fNameField setToolTip: fPath];
84   
85    BOOL multifile = !fInfo->isSingleFile;
86   
87    NSImage * icon = [[[NSWorkspace sharedWorkspace] iconForFileType: multifile
88                        ? NSFileTypeForHFSTypeCode('fldr') : [fPath pathExtension]] copy];
89    [fIcon setImage: icon];
90    [icon release];
91   
92    NSString * statusString = [NSString stringForFileSize: fInfo->totalSize];
93    if (multifile)
94    {
95        NSString * fileString;
96        int count = fInfo->fileCount;
97        if (count != 1)
98            fileString = [NSString stringWithFormat: NSLocalizedString(@"%d Files, ", "Create torrent -> info"), count];
99        else
100            fileString = NSLocalizedString(@"1 File, ", "Create torrent -> info");
101        statusString = [fileString stringByAppendingString: statusString];
102    }
103    [fStatusField setStringValue: statusString];
104   
105    [fPiecesField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d pieces, %@ each", "Create torrent -> info"),
106                                    fInfo->pieceCount, [NSString stringForFileSize: fInfo->pieceSize]]];
107   
108    fLocation = [[[DEFAULT_SAVE_LOCATION stringByAppendingPathComponent: [name stringByAppendingPathExtension: @"torrent"]]
109                                            stringByExpandingTildeInPath] retain];
110    [fLocationIcon setImage: [[NSWorkspace sharedWorkspace] iconForFile: [fLocation stringByDeletingLastPathComponent]]];
111    [fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]];
112    [fLocationField setToolTip: fLocation];
113}
114
115- (void) dealloc
116{
117    [fPath release];
118    if (fLocation)
119        [fLocation release];
120   
121    if (fInfo)
122        tr_metaInfoBuilderFree(fInfo);
123   
124    if (fTimer)
125        [fTimer invalidate];
126   
127    [super dealloc];
128}
129
130- (void) setLocation: (id) sender
131{
132    NSSavePanel * panel = [NSSavePanel savePanel];
133
134    [panel setPrompt: @"Select"];
135    [panel setRequiredFileType: @"torrent"];
136    [panel setCanSelectHiddenExtension: YES];
137
138    [panel beginSheetForDirectory: nil file: [fLocation lastPathComponent] modalForWindow: [self window] modalDelegate: self
139            didEndSelector: @selector(locationSheetClosed:returnCode:contextInfo:) contextInfo: nil];
140}
141
142- (void) create: (id) sender
143{
144    #warning check already exists
145   
146    //parse tracker string
147    NSString * trackerString = [fTrackerField stringValue];
148    if ([trackerString rangeOfString: @"://"].location != NSNotFound)
149    {
150        if (![trackerString hasPrefix: @"http://"])
151        {
152            NSAlert * alert = [[[NSAlert alloc] init] autorelease];
153            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> http warning -> button")];
154            [alert setMessageText: NSLocalizedString(@"Tracker address must begin with \"http://\".",
155                                                    "Create torrent -> http warning -> title")];
156            [alert setInformativeText: NSLocalizedString(@"Change the tracker address to create the torrent.",
157                                                        "Create torrent -> http warning -> warning")];
158            [alert setAlertStyle: NSWarningAlertStyle];
159           
160            [alert beginSheetModalForWindow: [self window] modalDelegate: self didEndSelector: nil contextInfo: nil];
161            return;
162        }
163    }
164    else
165        trackerString = [@"http://" stringByAppendingString: trackerString];
166   
167    tr_makeMetaInfo(fInfo, [fLocation UTF8String], [trackerString UTF8String], [[fCommentView string] UTF8String],
168                    [fPrivateCheck state] == NSOnState);
169   
170    fTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkProgress:)
171                        userInfo: nil repeats: YES];
172    [fTimer fire];
173}
174
175- (void) cancelCreateWindow: (id) sender
176{
177    [[self window] close];
178}
179
180- (void) windowWillClose: (NSNotification *) notification
181{
182    [self release];
183}
184
185- (void) cancelCreateProgress: (id) sender
186{
187    fInfo->abortFlag = 1;
188    [fTimer fire];
189}
190
191@end
192
193@implementation CreatorWindowController (Private)
194
195+ (NSString *) chooseFile
196{
197    NSOpenPanel * panel = [NSOpenPanel openPanel];
198   
199    [panel setPrompt: NSLocalizedString(@"Select", "Create torrent -> select file")];
200    [panel setAllowsMultipleSelection: NO];
201    [panel setCanChooseFiles: YES];
202    [panel setCanChooseDirectories: YES];
203    [panel setCanCreateDirectories: NO];
204
205    [panel setMessage: NSLocalizedString(@"Select a file or folder for the torrent file.", "Create torrent -> select file")];
206   
207    BOOL success = [panel runModal] == NSOKButton;
208    return success ? [[panel filenames] objectAtIndex: 0] : nil;
209}
210
211- (void) locationSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (void *) info
212{
213    if (code == NSOKButton)
214    {
215        [fLocation release];
216        fLocation = [[panel filename] retain];
217       
218        [fLocationIcon setImage: [[NSWorkspace sharedWorkspace] iconForFile: [fLocation stringByDeletingLastPathComponent]]];
219        [fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]];
220        [fLocationField setToolTip: fLocation];
221    }
222}
223
224- (void) checkProgress: (NSTimer *) timer
225{
226    if (fInfo->isDone)
227    {
228        [timer invalidate];
229        timer = nil;
230       
231        #warning check failed or not
232       
233        if ([[self window] attachedSheet])
234            [NSApp endSheet: fProgressWindow];
235        [fProgressWindow orderOut: nil];
236       
237        #warning add to T
238       
239        [[self window] close];
240    }
241    else
242    {
243        [fProgressIndicator setDoubleValue: (double)fInfo->pieceIndex / fInfo->pieceCount];
244        if (![[self window] attachedSheet])
245            [NSApp beginSheet: fProgressWindow modalForWindow: [self window] modalDelegate: self
246                    didEndSelector: nil contextInfo: nil];
247    }
248}
249
250@end
Note: See TracBrowser for help on using the repository browser.