~ubuntu-branches/ubuntu/jaunty/transmission/jaunty-security

« back to all changes in this revision

Viewing changes to macosx/AddWindowController.m

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2008-11-28 15:33:48 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20081128153348-it70trfnxiroblmc
Tags: 1.40-0ubuntu1
* New upstream release (LP: #302672)
  - Tracker communication uses fewer resources
  - More accurate bandwidth limits
  - Reduce disk fragmentation by preallocating files (LP: #287726)
  - Stability, security and performance improvements to the RPC /
    Web UI server (closes LP: #290423)
  - Support compression when serving Web UI and RPC responses
  - Simplify the RPC whitelist
  - Fix bug that prevented handshakes with encrypted BitComet peers
  - Fix 1.3x bug that could re-download some data unnecessarily
    (LP: #295040)
  - Option to automatically update the blocklist weekly
  - Added off-hour bandwidth scheduling
  - Simplify file/priority selection in the details dialog
  - Fix a couple of crashes
  - New / updated translations
  - Don't inhibit hibernation by default (LP: #292929)
  - Use "close" animation when sending to notification area (LP: #130811)
  - Fix resize problems (LP: #269872)
  - Support "--version" option when launching from command line
    (LP: #292011)
  - Correctly parse announce URLs that have leading or trailing
    spaces (LP: #262411)
  - Display an error when "Open Torrent" fails (LP: #281463)
* Dropped 10_fix_crasher_from_upstream.dpatch: Fix is in this
  upstream release.
* debian/control: Don't just build-depend on libcurl-dev, which is
  a virtual package.

Show diffs side-by-side

added added

removed removed

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