1/* XTermPref.m
2 *
3 * Copyright (C) 2003-2010 Free Software Foundation, Inc.
4 *
5 * Author: Enrico Sersale <enrico@imago.ro>
6 * Date: August 2001
7 *
8 * This file is part of the GNUstep GWorkspace application
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
23 */
24
25#import <Foundation/Foundation.h>
26#import <AppKit/AppKit.h>
27#import <GNUstepBase/GNUstep.h>
28
29#import "XTermPref.h"
30#import "GWorkspace.h"
31
32static NSString *nibName = @"XTermPref";
33
34@implementation XTermPref
35
36- (void)dealloc
37{
38  RELEASE (prefbox);
39  RELEASE (xterm);
40  RELEASE (xtermArgs);
41  [super dealloc];
42}
43
44- (id)init
45{
46  self = [super init];
47
48  if (self)
49    {
50      if ([NSBundle loadNibNamed: nibName owner: self] == NO)
51	{
52	  NSLog(@"failed to load %@!", nibName);
53	}
54      else
55	{
56	  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
57	  id entry;
58
59	  RETAIN (prefbox);
60	  RELEASE (win);
61
62	  useService = [defaults boolForKey: @"terminal_services"];
63
64	  if (useService)
65	    {
66	      [xtermField setSelectable: NO];
67	      [argsField setSelectable: NO];
68	      [setButt setEnabled: NO];
69	      [serviceCheck setState: NSOnState];
70	    } else
71	    {
72	      [serviceCheck setState: NSOffState];
73	    }
74
75	  entry = [defaults stringForKey: @"defxterm"];
76	  if (entry)
77	    {
78	      ASSIGN (xterm, entry);
79	      [xtermField setStringValue: xterm];
80	    }
81
82	  entry = [defaults stringForKey: @"defaultxtermargs"];
83	  if (entry)
84	    {
85	      ASSIGN (xtermArgs, entry);
86	      [argsField setStringValue: xtermArgs];
87	    }
88
89	  gw = [GWorkspace gworkspace];
90
91	  /* Internationalization */
92	  [serviceBox setTitle: NSLocalizedString(@"Terminal.app", @"")];
93	  [serviceCheck setTitle: NSLocalizedString(@"Use Terminal service", @"")];
94	  [xtermLabel setStringValue: NSLocalizedString(@"xterm", @"")];
95	  [argsLabel setStringValue: NSLocalizedString(@"arguments", @"")];
96	  [setButt setTitle: NSLocalizedString(@"Set", @"")];
97	  [fieldsBox setTitle: NSLocalizedString(@"Terminal", @"")];
98	}
99    }
100
101  return self;
102}
103
104- (NSView *)prefView
105{
106  return prefbox;
107}
108
109- (NSString *)prefName
110{
111  return NSLocalizedString(@"Terminal", @"");
112}
113
114- (IBAction)setUseService:(id)sender
115{
116  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
117
118  useService = ([sender state] == NSOnState);
119
120  if (useService)
121    {
122      [xtermField setSelectable: NO];
123      [argsField setSelectable: NO];
124      [setButt setEnabled: NO];
125    }
126  else
127    {
128      [xtermField setSelectable: YES];
129      [argsField setSelectable: YES];
130      [setButt setEnabled: YES];
131    }
132
133  [defaults setBool: useService forKey: @"terminal_services"];
134  [gw setUseTerminalService: useService];
135}
136
137- (IBAction)setXTerm:(id)sender
138{
139  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
140  NSString *xt = [xtermField stringValue];
141  NSString *xtargs = [argsField stringValue];
142  int lngt;
143
144  if ([xterm isEqual: xt] && [xtermArgs isEqual: xtargs]) {
145    return;
146  }
147
148  lngt = [xt length];
149
150  if (lngt) {
151    BOOL xtok = YES;
152    int i;
153
154    for (i = 0; i < lngt; i++) {
155      unichar c = [xt characterAtIndex: i];
156
157      if (c == ' ') {
158        xtok = NO;
159      }
160    }
161
162    if (xtok) {
163      lngt = [xtargs length];
164      xtok = (lngt == 0) ? YES : NO;
165
166      for (i = 0; i < lngt; i++) {
167        unichar c = [xtargs characterAtIndex: i];
168
169        if (c != ' ') {
170          xtok = YES;
171          break;
172        }
173      }
174    }
175
176    if (xtok) {
177      ASSIGN (xterm, xt);
178      ASSIGN (xtermArgs, xtargs);
179
180	    [defaults setObject: xterm forKey: @"defxterm"];
181	    [defaults setObject: xtermArgs forKey: @"defaultxtermargs"];
182	    [defaults synchronize];
183
184      [gw changeDefaultXTerm: xterm arguments: xtermArgs];
185    }
186  }
187}
188
189@end
190