transmission/macosx/PrefsController.m

546 lines
16 KiB
Mathematica
Raw Normal View History

2006-01-12 17:43:21 +00:00
/******************************************************************************
2006-03-23 12:39:39 +00:00
* Copyright (c) 2005-2006 Transmission authors and contributors
2006-01-12 17:43:21 +00:00
*
* 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-03-23 12:39:39 +00:00
#import "StringAdditions.h"
#import "Utils.h"
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
#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
2006-01-29 22:01:08 +00:00
#define UPDATE_DAILY 0
#define UPDATE_WEEKLY 1
#define UPDATE_NEVER 2
2006-01-12 18:57:23 +00:00
#define TOOLBAR_GENERAL @"General"
#define TOOLBAR_TRANSFERS @"Transfers"
2006-01-12 18:57:23 +00:00
#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) showTransfersPref: (id) sender;
2006-01-12 18:57:23 +00:00
- (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
{
[[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource: @"Defaults"
ofType: @"plist"]]];
2006-01-12 18:57:23 +00:00
}
2006-01-12 17:43:21 +00:00
- (void) dealloc
2006-01-12 18:57:23 +00:00
{
[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];
[fToolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
[self showGeneralPref: nil];
2006-01-12 18:57:23 +00:00
fDefaults = [NSUserDefaults standardUserDefaults];
fHandle = handle;
2006-01-12 18:57:23 +00:00
//set download folder
NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
fDownloadFolder = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
2006-01-12 18:57:23 +00:00
[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 setInteger: 20 forKey: @"UploadLimit"];
[fDefaults setBool: NO forKey: @"CheckUpload"];
2006-01-12 18:57:23 +00:00
}
//set upload limit
BOOL checkUpload = [fDefaults boolForKey: @"CheckUpload"];
2006-01-12 18:57:23 +00:00
int uploadLimit = [fDefaults integerForKey: @"UploadLimit"];
[fUploadCheck setState: checkUpload ? NSOnState : NSOffState];
[fUploadField setIntValue: uploadLimit];
[fUploadField setEnabled: checkUpload];
[fUploadLimitItem setTitle:
[NSString stringWithFormat: @"Limit (%d KB/s)", uploadLimit]];
if (checkUpload)
[fUploadLimitItem setState: NSOnState];
else
[fUploadNoLimitItem setState: NSOnState];
tr_setUploadLimit( fHandle, checkUpload ? uploadLimit : -1 );
//set download limit
BOOL checkDownload = [fDefaults boolForKey: @"CheckDownload"];
int downloadLimit = [fDefaults integerForKey: @"DownloadLimit"];
[fDownloadCheck setState: checkDownload ? NSOnState : NSOffState];
[fDownloadField setIntValue: downloadLimit];
[fDownloadField setEnabled: checkDownload];
[fDownloadLimitItem setTitle:
[NSString stringWithFormat: @"Limit (%d KB/s)", downloadLimit]];
if (checkDownload)
[fDownloadLimitItem setState: NSOnState];
else
[fDownloadNoLimitItem setState: NSOnState];
tr_setDownloadLimit( fHandle, checkDownload ? downloadLimit : -1 );
2006-01-12 18:57:23 +00:00
//set ratio limit
BOOL ratioCheck = [fDefaults boolForKey: @"RatioCheck"];
float ratioLimit = [fDefaults floatForKey: @"RatioLimit"];
[fRatioCheck setState: ratioCheck ? NSOnState : NSOffState];
[fRatioField setEnabled: ratioCheck];
[fRatioField setFloatValue: ratioLimit];
[fRatioSetItem setTitle: [NSString stringWithFormat: @"Stop at Ratio (%.1f)", ratioLimit]];
if (ratioCheck)
[fRatioSetItem setState: NSOnState];
else
[fRatioNotSetItem setState: NSOnState];
2006-01-12 18:57:23 +00:00
//set remove and quit prompts
[fQuitCheck setState: [fDefaults boolForKey: @"CheckQuit"] ?
NSOnState : NSOffState];
[fRemoveCheck setState: [fDefaults boolForKey: @"CheckRemove"] ?
NSOnState : NSOffState];
2006-01-29 19:47:54 +00:00
//set dock badging
[fBadgeDownloadRateCheck setState: [fDefaults boolForKey: @"BadgeDownloadRate"]];
[fBadgeUploadRateCheck setState: [fDefaults boolForKey: @"BadgeUploadRate"]];
//set auto start
[fAutoStartCheck setState: [fDefaults boolForKey: @"AutoStartDownload"]];
2006-01-29 22:01:08 +00:00
/* Check for update */
NSString * versionCheck = [fDefaults stringForKey: @"VersionCheck"];
if( [versionCheck isEqualToString: @"Daily"] )
[fUpdatePopUp selectItemAtIndex: UPDATE_DAILY];
else if( [versionCheck isEqualToString: @"Weekly"] )
[fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
else if( [versionCheck isEqualToString: @"Never"] )
[fUpdatePopUp selectItemAtIndex: UPDATE_NEVER];
else
{
[fDefaults setObject: @"Weekly" forKey: @"VersionCheck"];
[fUpdatePopUp selectItemAtIndex: UPDATE_WEEKLY];
}
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_TRANSFERS])
{
[item setLabel: TOOLBAR_TRANSFERS];
[item setImage: [NSImage imageNamed: @"Transfers.png"]];
[item setTarget: self];
[item setAction: @selector( showTransfersPref: )];
}
2006-01-12 18:57:23 +00:00
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
}
- (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
}
- (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
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
2006-01-12 18:57:23 +00:00
{
return [NSArray arrayWithObjects:
TOOLBAR_GENERAL, TOOLBAR_TRANSFERS,
TOOLBAR_NETWORK, nil];
2006-01-12 18:57:23 +00:00
}
2006-01-12 17:43:21 +00:00
2006-01-12 18:57:23 +00:00
- (void) setPort: (id) sender
{
int bindPort = [fPortField intValue];
tr_setBindPort( fHandle, bindPort );
[fDefaults setInteger: bindPort forKey: @"BindPort"];
2006-01-12 18:57:23 +00:00
}
- (void) setLimit: (id) sender
{
NSString * key;
NSMenuItem * menuItem;
if (sender == fUploadField)
{
key = @"UploadLimit";
menuItem = fUploadLimitItem;
}
else
{
key = @"DownloadLimit";
menuItem = fDownloadLimitItem;
}
int limit = [sender intValue];
[fDefaults setInteger: limit forKey: key];
[menuItem setTitle: [NSString stringWithFormat: @"Limit (%d KB/s)", limit]];
if( sender == fUploadField )
tr_setUploadLimit( fHandle,
( [fUploadCheck state] == NSOffState ) ? -1 : limit );
else
tr_setDownloadLimit( fHandle,
( [fDownloadCheck state] == NSOffState ) ? -1 : limit );
}
- (void) setLimitCheck: (id) sender
2006-01-12 18:57:23 +00:00
{
NSString * key;
NSTextField * field;
NSMenuItem * limitItem, * noLimitItem;
if( sender == fUploadCheck )
{
key = @"CheckUpload";
field = fUploadField;
limitItem = fUploadLimitItem;
noLimitItem = fUploadNoLimitItem;
}
else
{
key = @"CheckDownload";
field = fDownloadField;
limitItem = fDownloadLimitItem;
noLimitItem = fDownloadNoLimitItem;
}
2006-01-12 18:57:23 +00:00
BOOL check = [sender state] == NSOnState;
[limitItem setState: check ? NSOnState : NSOffState];
[noLimitItem setState: !check ? NSOnState : NSOffState];
[fDefaults setBool: check forKey: key];
[field setIntValue: [field intValue]]; //set to valid value
[self setLimit: field];
[field setEnabled: check];
2006-01-12 18:57:23 +00:00
}
2006-01-12 17:43:21 +00:00
- (void) setLimitMenu: (id) sender
2006-01-12 18:57:23 +00:00
{
NSButton * check = (sender == fUploadLimitItem || sender == fUploadNoLimitItem)
? fUploadCheck : fDownloadCheck;
int state = (sender == fUploadLimitItem || sender == fDownloadLimitItem)
? NSOnState : NSOffState;
[check setState: state];
[self setLimitCheck: check];
}
- (void) setQuickSpeed: (id) sender
{
NSString * title = [sender title];
int limit = [[title substringToIndex: [title length] - [@" KB/s" length]] intValue];
if ([sender menu] == fUploadMenu)
2006-01-12 17:43:21 +00:00
{
[fUploadField setIntValue: limit];
[self setLimitMenu: fUploadLimitItem];
2006-01-12 17:43:21 +00:00
}
2006-01-12 18:57:23 +00:00
else
{
[fDownloadField setIntValue: limit];
[self setLimitMenu: fDownloadLimitItem];
2006-01-12 18:57:23 +00:00
}
}
2006-01-12 17:43:21 +00:00
- (void) setRatio: (id) sender
{
float ratio = [sender floatValue];
[fDefaults setFloat: ratio forKey: @"RatioLimit"];
[fRatioSetItem setTitle: [NSString stringWithFormat: @"Stop at Ratio (%.1f)", ratio]];
}
- (void) setRatioCheck: (id) sender
{
BOOL check = [sender state] == NSOnState;
[fDefaults setBool: check forKey: @"RatioCheck"];
[fRatioField setFloatValue: [fRatioField floatValue]]; //set to valid value
[self setRatio: fRatioField];
[fRatioField setEnabled: check];
[fRatioSetItem setState: check ? NSOnState : NSOffState];
[fRatioNotSetItem setState: !check ? NSOnState : NSOffState];
}
- (void) setRatioMenu: (id) sender
2006-01-12 18:57:23 +00:00
{
int state = sender == fRatioSetItem ? NSOnState : NSOffState;
[fRatioCheck setState: state];
[self setRatioCheck: fRatioCheck];
2006-01-12 17:43:21 +00:00
}
- (void) setQuickRatio: (id) sender
2006-01-12 18:57:23 +00:00
{
float limit = [[sender title] floatValue];
[fRatioField setFloatValue: limit];
[self setRatioMenu: fRatioSetItem];
}
- (void) setShowMessage: (id) sender
{
if (sender == fQuitCheck)
[fDefaults setBool: [sender state] forKey: @"CheckQuit"];
else if (sender == fRemoveCheck)
[fDefaults setBool: [fRemoveCheck state] forKey: @"CheckRemove"];
else;
2006-01-12 18:57:23 +00:00
}
2006-01-12 17:43:21 +00:00
2006-01-29 19:47:54 +00:00
- (void) setBadge: (id) sender
{
if (sender == fBadgeDownloadRateCheck)
[fDefaults setBool: [sender state] forKey: @"BadgeDownloadRate"];
2006-01-29 19:47:54 +00:00
else if (sender == fBadgeUploadRateCheck)
[fDefaults setBool: [sender state] forKey: @"BadgeUploadRate"];
2006-01-29 19:47:54 +00:00
else;
}
2006-01-29 22:01:08 +00:00
- (void) setUpdate: (id) sender
{
switch( [fUpdatePopUp indexOfSelectedItem] )
{
case UPDATE_DAILY:
[fDefaults setObject: @"Daily" forKey: @"VersionCheck"];
break;
case UPDATE_WEEKLY:
[fDefaults setObject: @"Weekly" forKey: @"VersionCheck"];
break;
case UPDATE_NEVER:
[fDefaults setObject: @"Never" forKey: @"VersionCheck"];
break;
}
}
- (void) setAutoStart: (id) sender
{
[fDefaults setBool: [sender state] forKey: @"AutoStartDownload"];
}
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
}
- (void) showTransfersPref: (id) sender
{
[self setPrefView: fTransfersView];
}
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]];
2006-01-12 18:57:23 +00:00
[fPrefsWindow setContentView: fBlankView];
[fPrefsWindow setFrame: windowRect display: YES animate: YES];
2006-01-12 18:57:23 +00:00
[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) */