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

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

small update to news

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