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

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

icon for torrent creation and some cleanup

File size: 8.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: (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}
173
174- (void) cancelCreateWindow: (id) sender
175{
176    [[self window] close];
177}
178
179- (void) windowWillClose: (NSNotification *) notification
180{
181    [self release];
182}
183
184- (void) cancelCreateProgress: (id) sender
185{
186
187}
188
189@end
190
191@implementation CreatorWindowController (Private)
192
193+ (NSString *) chooseFile
194{
195    NSOpenPanel * panel = [NSOpenPanel openPanel];
196   
197    [panel setPrompt: NSLocalizedString(@"Select", "Create torrent -> select file")];
198    [panel setAllowsMultipleSelection: NO];
199    [panel setCanChooseFiles: YES];
200    [panel setCanChooseDirectories: YES];
201    [panel setCanCreateDirectories: NO];
202
203    [panel setMessage: NSLocalizedString(@"Select a file or folder for the torrent file.", "Create torrent -> select file")];
204   
205    BOOL success = [panel runModal] == NSOKButton;
206    return success ? [[panel filenames] objectAtIndex: 0] : nil;
207}
208
209- (void) locationSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (void *) info
210{
211    if (code == NSOKButton)
212    {
213        [fLocation release];
214        fLocation = [[panel filename] retain];
215       
216        [fLocationIcon setImage: [[NSWorkspace sharedWorkspace] iconForFile: [fLocation stringByDeletingLastPathComponent]]];
217        [fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]];
218        [fLocationField setToolTip: fLocation];
219    }
220}
221
222- (void) checkProgress: (NSTimer *) timer
223{
224    if (fInfo->isDone)
225    {
226        [timer invalidate];
227        timer = nil;
228       
229        #warning check failed or not
230       
231        if ([[self window] attachedSheet])
232            [NSApp endSheet: fProgressWindow];
233        [fProgressWindow orderOut: nil];
234       
235        #warning add to T
236       
237        [[self window] close];
238    }
239    else
240    {
241        [fProgressIndicator setDoubleValue: (double)fInfo->pieceIndex / fInfo->pieceCount];
242        if (![[self window] attachedSheet])
243            [NSApp beginSheet: fProgressWindow modalForWindow: [self window] modalDelegate: self
244                    didEndSelector: nil contextInfo: nil];
245    }
246}
247
248@end
Note: See TracBrowser for help on using the repository browser.