1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 17:17:31 +00:00
transmission/macosx/PrefsController.m

390 lines
12 KiB
Mathematica
Raw Normal View History

2006-01-12 17:43:21 +00:00
/******************************************************************************
* Copyright (c) 2005 Eric Petit
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import "PrefsController.h"
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
#define DEFAULT_UPLOAD @"20"
#define MIN_PORT 1
#define MAX_PORT 65535
#define DOWNLOAD_FOLDER 0
2006-01-12 18:58:57 +00:00
#define DOWNLOAD_TORRENT 2
#define DOWNLOAD_ASK 3
2006-01-12 18:57:23 +00:00
#define TOOLBAR_GENERAL @"General"
#define TOOLBAR_NETWORK @"Network"
2006-01-12 17:43:21 +00:00
@interface PrefsController (Private)
2006-01-12 18:57:23 +00:00
- (void) showGeneralPref: (id) sender;
- (void) showNetworkPref: (id) sender;
- (void) setPrefView: (NSView *) view;
2006-01-12 17:43:21 +00:00
- (void) folderSheetClosed: (NSOpenPanel *) s returnCode: (int) code
contextInfo: (void *) info;
- (void) updatePopUp;
@end
@implementation PrefsController
2006-01-12 18:57:23 +00:00
+ (void) initialize
2006-01-12 17:43:21 +00:00
{
NSDictionary * appDefaults;
2006-01-12 18:40:47 +00:00
NSString * desktop, * port;
2006-01-12 17:43:21 +00:00
/* Register defaults settings:
- Simple bar
- Always download to Desktop
2006-01-12 18:40:47 +00:00
- Port TR_DEFAULT_PORT
2006-01-12 18:57:23 +00:00
- Upload limit DEFAULT_UPLOAD
2006-01-12 18:58:57 +00:00
- Limit upload
2006-01-12 18:57:23 +00:00
- Ask before quitting
- Ask before removing */
2006-01-12 18:40:47 +00:00
desktop = [NSHomeDirectory() stringByAppendingString: @"/Desktop"];
2006-01-12 18:57:23 +00:00
port = [NSString stringWithFormat: @"%d", TR_DEFAULT_PORT];
2006-01-12 17:43:21 +00:00
appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
2006-01-12 18:57:23 +00:00
@"NO", @"UseAdvancedBar",
@"Constant", @"DownloadChoice",
desktop, @"DownloadFolder",
port, @"BindPort",
DEFAULT_UPLOAD, @"UploadLimit",
@"YES", @"CheckUpload",
@"YES", @"CheckQuit",
@"YES", @"CheckRemove",
2006-01-12 17:43:21 +00:00
NULL];
2006-01-12 18:57:23 +00:00
[[NSUserDefaults standardUserDefaults] registerDefaults: appDefaults];
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void)dealloc
{
[fDownloadFolder release];
[super dealloc];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
- (void) setPrefsWindow: (tr_handle_t *) handle
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
fToolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
[fToolbar setDelegate: self];
[fToolbar setAllowsUserCustomization: NO];
[fPrefsWindow setToolbar: fToolbar];
[fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
[fToolbar setSizeMode: NSToolbarSizeModeRegular];
[fPrefsWindow setShowsToolbarButton: NO];
[fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
[self setPrefView: fGeneralView];
fDefaults = [NSUserDefaults standardUserDefaults];
//set download folder
NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
fDownloadFolder = [fDefaults stringForKey: @"DownloadFolder"];
[fDownloadFolder retain];
if( [downloadChoice isEqualToString: @"Constant"] )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:58:57 +00:00
[fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
else if( [downloadChoice isEqualToString: @"Torrent"] )
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:58:57 +00:00
[fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
else
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:58:57 +00:00
[fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
[self updatePopUp];
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
//set bind port
int bindPort = [fDefaults integerForKey: @"BindPort"];
[fPortField setIntValue: bindPort];
fHandle = handle;
tr_setBindPort( fHandle, bindPort );
//checks for old version upload speed of -1
if ([fDefaults integerForKey: @"UploadLimit"] < 0)
{
[fDefaults setObject: DEFAULT_UPLOAD forKey: @"UploadLimit"];
[fDefaults setObject: @"NO" forKey: @"CheckUpload"];
}
//set upload limit
BOOL checkUpload = [[fDefaults stringForKey: @"CheckUpload"] isEqualToString:@"YES"];
int uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
[fUploadCheck setState: checkUpload ? NSOnState : NSOffState];
[fUploadField setIntValue: uploadLimit];
[fUploadField setEnabled: checkUpload];
if (!checkUpload || uploadLimit == 0)
uploadLimit = -1;
tr_setUploadLimit( fHandle, uploadLimit );
//set remove and quit prompts
[fQuitCheck setState:([[fDefaults stringForKey: @"CheckQuit"]
isEqualToString:@"YES"] ? NSOnState : NSOffState)];
[fRemoveCheck setState:([[fDefaults stringForKey: @"CheckRemove"]
isEqualToString:@"YES"] ? NSOnState : NSOffState)];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
- (NSToolbarItem *) toolbar: (NSToolbar *) t itemForItemIdentifier:
(NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
NSToolbarItem * item;
item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
if ([ident isEqualToString: TOOLBAR_GENERAL])
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
[item setLabel: TOOLBAR_GENERAL];
[item setImage: [NSImage imageNamed: @"Preferences.png"]];
[item setTarget: self];
[item setAction: @selector( showGeneralPref: )];
}
else if ([ident isEqualToString: TOOLBAR_NETWORK])
{
[item setLabel: TOOLBAR_NETWORK];
[item setImage: [NSImage imageNamed: @"Network.png"]];
[item setTarget: self];
[item setAction: @selector( showNetworkPref: )];
2006-01-12 17:43:21 +00:00
}
else
{
2006-01-12 18:57:23 +00:00
[item release];
return nil;
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
return item;
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
return [self toolbarDefaultItemIdentifiers: nil];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
return [self toolbarAllowedItemIdentifiers: nil];
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
{
return [NSArray arrayWithObjects:
TOOLBAR_GENERAL,
TOOLBAR_NETWORK,
nil];
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void) setPort: (id) sender
{
int bindPort = [fPortField intValue];
//if value entered is not an int or is not in range do not change
if (![[fPortField stringValue] isEqualToString:
[NSString stringWithFormat: @"%d", bindPort]]
|| bindPort < MIN_PORT
|| bindPort > MAX_PORT)
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
NSBeep();
bindPort = [fDefaults integerForKey: @"BindPort"];
2006-01-12 17:43:21 +00:00
[fPortField setIntValue: bindPort];
}
2006-01-12 18:57:23 +00:00
else
{
tr_setBindPort( fHandle, bindPort );
[fDefaults setObject: [NSString stringWithFormat: @"%d", bindPort]
forKey: @"BindPort"];
}
}
- (void) setLimitUploadCheck: (id) sender
{
BOOL checkUpload = [fUploadCheck state] == NSOnState;
[fDefaults setObject: checkUpload ? @"YES" : @"NO"
forKey: @"CheckUpload"];
[self setUploadLimit: sender];
[fUploadField setEnabled: checkUpload];
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void) setUploadLimit: (id) sender
{
int uploadLimit = [fUploadField intValue];
//if value entered is not an int or is less than 0 do not change
if (![[fUploadField stringValue] isEqualToString:
[NSString stringWithFormat: @"%d", uploadLimit]]
|| uploadLimit < 0)
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
NSBeep();
uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
[fUploadField setIntValue: uploadLimit];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
else
{
[fDefaults setObject: [NSString stringWithFormat: @"%d", uploadLimit]
forKey: @"UploadLimit"];
}
if ([fUploadCheck state] == NSOffState || uploadLimit == 0)
uploadLimit = -1;
tr_setUploadLimit( fHandle, uploadLimit );
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void) setQuitMessage: (id) sender
{
[fDefaults setObject: ([fQuitCheck state] == NSOnState ? @"YES" : @"NO")
forKey: @"CheckQuit"];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
- (void) setRemoveMessage: (id) sender
{
[fDefaults setObject: ([fRemoveCheck state] == NSOnState ? @"YES" : @"NO")
forKey: @"CheckRemove"];
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void) setDownloadLocation: (id) sender
{
//Download folder
2006-01-12 18:58:57 +00:00
switch( [fFolderPopUp indexOfSelectedItem] )
2006-01-12 18:57:23 +00:00
{
case DOWNLOAD_FOLDER:
[fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
break;
case DOWNLOAD_TORRENT:
[fDefaults setObject: @"Torrent" forKey: @"DownloadChoice"];
break;
case DOWNLOAD_ASK:
[fDefaults setObject: @"Ask" forKey: @"DownloadChoice"];
break;
}
}
2006-01-12 17:43:21 +00:00
- (void) folderSheetShow: (id) sender
{
2006-01-12 18:57:23 +00:00
NSOpenPanel * panel = [NSOpenPanel openPanel];
2006-01-12 17:43:21 +00:00
[panel setPrompt: @"Select"];
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel beginSheetForDirectory: NULL file: NULL types: NULL
modalForWindow: fPrefsWindow modalDelegate: self didEndSelector:
@selector( folderSheetClosed:returnCode:contextInfo: )
contextInfo: NULL];
}
2006-01-12 18:57:23 +00:00
@end // @implementation PrefsController
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
@implementation PrefsController (Private)
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void) showGeneralPref: (id) sender
{
[self setPrefView: fGeneralView];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
- (void) showNetworkPref: (id) sender
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
[self setPrefView: fNetworkView];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
- (void) setPrefView: (NSView *) view
2006-01-12 17:43:21 +00:00
{
2006-01-12 18:57:23 +00:00
NSRect windowRect = [fPrefsWindow frame];
int difference = [view frame].size.height - [[fPrefsWindow contentView] frame].size.height;
windowRect.origin.y -= difference;
windowRect.size.height += difference;
[fPrefsWindow setTitle: [fToolbar selectedItemIdentifier]];
[fPrefsWindow setContentView: fBlankView];
[fPrefsWindow setFrame:windowRect display: YES animate: YES];
[fPrefsWindow setContentView: view];
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void) folderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code
contextInfo: (void *) info
{
2006-01-12 18:58:57 +00:00
if (code == NSOKButton)
{
[fDownloadFolder release];
fDownloadFolder = [[openPanel filenames] objectAtIndex: 0];
[fDownloadFolder retain];
[fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
[fDefaults setObject: fDownloadFolder forKey: @"DownloadFolder"];
[fDefaults setObject: @"Constant" forKey: @"DownloadChoice"];
[self updatePopUp];
}
else
{
//reset if cancelled
NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
if( [downloadChoice isEqualToString: @"Constant"] )
{
[fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
}
else if( [downloadChoice isEqualToString: @"Torrent"] )
{
[fFolderPopUp selectItemAtIndex: DOWNLOAD_TORRENT];
}
else
{
[fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
}
}
2006-01-12 17:43:21 +00:00
}
- (void) updatePopUp
{
NSMenuItem * menuItem;
NSImage * image32, * image16;
2006-01-12 18:57:23 +00:00
// Get the icon for the folder
2006-01-12 17:43:21 +00:00
image32 = [[NSWorkspace sharedWorkspace] iconForFile:
fDownloadFolder];
image16 = [[NSImage alloc] initWithSize: NSMakeSize(16,16)];
2006-01-12 18:57:23 +00:00
// 32x32 -> 16x16 scaling
2006-01-12 17:43:21 +00:00
[image16 lockFocus];
[[NSGraphicsContext currentContext]
setImageInterpolation: NSImageInterpolationHigh];
[image32 drawInRect: NSMakeRect(0,0,16,16)
fromRect: NSMakeRect(0,0,32,32) operation: NSCompositeCopy
fraction: 1.0];
[image16 unlockFocus];
2006-01-12 18:57:23 +00:00
// Update the menu item
2006-01-12 17:43:21 +00:00
menuItem = (NSMenuItem *) [fFolderPopUp itemAtIndex: 0];
[menuItem setTitle: [fDownloadFolder lastPathComponent]];
[menuItem setImage: image16];
[image16 release];
}
@end /* @implementation PrefsController (Private) */