1/*
2 * Copyright (c) 2010, Wei Mingzhi <whistler@openoffice.org>.
3 * All Rights Reserved.
4 *
5 * Based on: Cdrom for Psemu Pro like Emulators
6 * By: linuzappz <linuzappz@hotmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses>.
20 */
21
22#import "PluginConfigController.h"
23#include "dfnet.h"
24
25#define kIPADDRKEY @"IP Address"
26#define kIPPORT @"IP Port"
27#define kPLAYERNUM @"Player Number"
28
29#define APP_ID @"net.codeplex.pcsxr.DFNet"
30#define PrefsKey APP_ID @" Settings"
31#define NSLocalizedStringInBundle(key, bundle, comment) \
32	[bundle localizedStringForKey:(key) value:@"" table:nil]
33
34static PluginConfigController *windowController = nil;
35
36static inline void RunOnMainThreadSync(dispatch_block_t block)
37{
38	if ([NSThread isMainThread]) {
39		block();
40	} else {
41		dispatch_sync(dispatch_get_main_queue(), block);
42	}
43}
44
45void AboutDlgProc()
46{
47	// Get parent application instance
48	NSBundle *bundle = [NSBundle bundleWithIdentifier:APP_ID];
49
50	// Get Credits.rtf
51	NSString *path = [bundle pathForResource:@"Credits" ofType:@"rtf"];
52	NSAttributedString *credits;
53	if (!path) {
54		path = [bundle pathForResource:@"Credits" ofType:@"rtfd"];
55	}
56	if (path) {
57		credits = [[NSAttributedString alloc] initWithPath:path documentAttributes:NULL];
58	} else {
59		credits = [[NSAttributedString alloc] initWithString:@""];
60	}
61
62	// Get Application Icon
63	NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:[bundle bundlePath]];
64	[icon setSize:NSMakeSize(64, 64)];
65
66	NSDictionary *infoPaneDict =
67	@{@"ApplicationName": [bundle objectForInfoDictionaryKey:@"CFBundleName"],
68	  @"ApplicationIcon": icon,
69	  @"ApplicationVersion": [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"],
70	  @"Version": [bundle objectForInfoDictionaryKey:@"CFBundleVersion"],
71	  @"Copyright": [bundle objectForInfoDictionaryKey:@"NSHumanReadableCopyright"],
72	  @"Credits": credits};
73	dispatch_async(dispatch_get_main_queue(), ^{
74		[NSApp orderFrontStandardAboutPanelWithOptions:infoPaneDict];
75	});
76}
77
78void ConfDlgProc()
79{
80	RunOnMainThreadSync(^{
81		NSWindow *window;
82
83		if (windowController == nil) {
84			windowController = [[PluginConfigController alloc] initWithWindowNibName:@"DFNet"];
85		}
86		window = [windowController window];
87
88		[windowController loadValues];
89
90		[window center];
91		[window makeKeyAndOrderFront:nil];
92	});
93}
94
95void ReadConfig()
96{
97	NSDictionary *keyValues;
98	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
99
100	[defaults registerDefaults:@{PrefsKey: @{kIPADDRKEY: @"127.0.0.1",
101								 kIPPORT: @33306,
102								 kPLAYERNUM: @1}}];
103
104	keyValues = [defaults dictionaryForKey:PrefsKey];
105
106	conf.PortNum = [keyValues[kIPPORT] unsignedShortValue];
107	conf.PlayerNum = [keyValues[kPLAYERNUM] intValue];
108	strlcpy(conf.ipAddress, [keyValues[kIPADDRKEY] cStringUsingEncoding:NSASCIIStringEncoding], sizeof(conf.ipAddress));
109}
110
111@implementation PluginConfigController
112@synthesize ipAddress;
113@synthesize portNum;
114@synthesize playerNum;
115
116- (IBAction)cancel:(id)sender
117{
118	[self close];
119}
120
121- (IBAction)ok:(id)sender
122{
123	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
124	NSBundle *curBundle = [NSBundle bundleForClass:[PluginConfigController class]];
125
126	NSString *theAddress = [ipAddress  stringValue];
127	NSInteger asciiLen = [theAddress lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
128	if (asciiLen > (sizeof(conf.ipAddress) - 1)) {
129		NSBeginAlertSheet(NSLocalizedStringInBundle(@"Address Too Long", curBundle, nil), nil, nil, nil, [self window], nil, NULL, NULL, NULL, @"%@", NSLocalizedStringInBundle(@"The address is too long.\n\nTry to use only the IP address and not a host name.", curBundle, nil));
130		return;
131	} else if (asciiLen == 0) {
132		NSBeginAlertSheet(NSLocalizedStringInBundle(@"Blank Address", curBundle, nil), nil, nil, nil, [self window], nil, NULL, NULL, NULL, @"%@", NSLocalizedStringInBundle(@"The address specified is either blank, or can't be converted to ASCII.\n\nTry connecting directly using the IP address using latin numerals.", curBundle, nil));
133		return;
134	}
135
136
137	NSMutableDictionary *writeDic = [NSMutableDictionary dictionaryWithDictionary:[defaults dictionaryForKey:PrefsKey]];
138	writeDic[kIPPORT] = @((unsigned short)[portNum intValue]);
139	writeDic[kPLAYERNUM] = @([playerNum intValue]);
140	writeDic[kIPADDRKEY] = theAddress;
141
142	// write to defaults
143	[defaults setObject:writeDic forKey:PrefsKey];
144	[defaults synchronize];
145
146	// and set global values accordingly
147	ReadConfig();
148
149	[self close];
150}
151
152- (void)loadValues
153{
154	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
155	ReadConfig();
156	NSDictionary *keyValues = [defaults dictionaryForKey:PrefsKey];
157
158	[ipAddress setStringValue:keyValues[kIPADDRKEY]];
159	[portNum setIntValue:[keyValues[kIPPORT] unsignedShortValue]];
160	[playerNum setIntValue:[keyValues[kPLAYERNUM] intValue]];
161}
162
163@end
164
165#import "OSXPlugLocalization.h"
166PLUGLOCIMP([PluginConfigController class])
167