File: ServicesPrefs.m

package info (click to toggle)
terminal.app 0.9.4+cvs20051125-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 460 kB
  • ctags: 106
  • sloc: objc: 6,098; makefile: 42; sh: 13
file content (718 lines) | stat: -rw-r--r-- 16,050 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
copyright 2002 Alexander Malmberg <alexander@malmberg.org>

This file is a part of Terminal.app. Terminal.app is free software; you
can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; version 2
of the License. See COPYING or main.m for more information.
*/

#include <Foundation/NSString.h>
#include <Foundation/NSUserDefaults.h>
#include <AppKit/NSButton.h>
#include <AppKit/NSPopUpButton.h>
#include <AppKit/NSTextField.h>
#include <AppKit/NSTableView.h>
#include <AppKit/NSTableColumn.h>
#include <AppKit/NSScrollView.h>
#include <AppKit/NSClipView.h>
#include <AppKit/NSBox.h>
#include <GNUstepGUI/GSVbox.h>
#include <GNUstepGUI/GSHbox.h>
#include <AppKit/NSSavePanel.h>
#include <AppKit/NSOpenPanel.h>
#include "Label.h"

#include "ServicesPrefs.h"

#include "Services.h"


@implementation TerminalServicesPrefs

-(void) _update
{
	NSString *name,*new_name;
	NSMutableDictionary *d;
	int i;

	if (current<0)
		return;

	name=[service_list objectAtIndex: current];
	new_name=[tf_name stringValue];
	if (![new_name length])
		new_name=name;
	d=[services objectForKey: name];
	if (!d)
		d=[[NSMutableDictionary alloc] init];

	[d setObject: [tf_key stringValue]
		forKey: Key];
	[d setObject: [tf_cmdline stringValue]
		forKey: Commandline];
	[d setObject: [NSString stringWithFormat: @"%i",[pb_input indexOfSelectedItem]]
		forKey: Input];
	[d setObject: [NSString stringWithFormat: @"%i",[pb_output indexOfSelectedItem]]
		forKey: ReturnData];
	[d setObject: [NSString stringWithFormat: @"%i",[pb_type indexOfSelectedItem]]
		forKey: Type];

	i=0;
	if ([cb_string state]) i|=1;
	if ([cb_filenames state]) i|=2;
	[d setObject: [NSString stringWithFormat: @"%i",i]
		forKey: AcceptTypes];

	if (![name isEqual: new_name])
	{
		[services setObject: d
			forKey: new_name];
		[services removeObjectForKey: name];
		[service_list replaceObjectAtIndex: current
			withObject: new_name];
		[list reloadData];
	}
}


-(void) save
{
	NSUserDefaults *ud;

	if (!services) return;
	if (!top) return;

	[self _update];

	ud=[NSUserDefaults standardUserDefaults];

	if (![services isEqual: [TerminalServices terminalServicesDictionary]])
	{
		[ud setObject: [services copy]
			forKey: @"TerminalServices"];

		[TerminalServices updateServicesPlist];
	}
}

-(void) revert
{
	NSDictionary *d;

	[list deselectAll: self];
	DESTROY(services);
	DESTROY(service_list);

	services=[[NSMutableDictionary alloc] init];
	d=[TerminalServices terminalServicesDictionary];

	{
		NSEnumerator *e=[d keyEnumerator];
		NSString *key;
		while ((key=[e nextObject]))
		{
			[services setObject: [[d objectForKey: key] mutableCopy]
				forKey: key];
		}
	}

	service_list=[[[services allKeys]
		sortedArrayUsingSelector: @selector(compare:)]
		mutableCopy];

	[list reloadData];
	current=-1;
	[self tableViewSelectionDidChange: nil];
}


-(int) numberOfRowsInTableView: (NSTableView *)tv
{
	return [service_list count];
}

-(id) tableView: (NSTableView *)tv  objectValueForTableColumn: (NSTableColumn *)tc  row: (int)row
{
	return [service_list objectAtIndex: row];
}

-(void) tableViewSelectionDidChange: (NSNotification *)n
{
	int r=[list selectedRow];

	if (current>=0)
		[self _update];

	if (r>=0)
	{
		int i;
		NSString *name,*s;
		NSDictionary *d;

		name=[service_list objectAtIndex: r];
		d=[services objectForKey: name];

		[tf_name setEditable: YES];
		[tf_cmdline setEditable: YES];
		[tf_key setEditable: YES];
		[pb_input setEnabled: YES];
		[pb_output setEnabled: YES];
		[pb_type setEnabled: YES];
		[cb_string setEnabled: YES];
		[cb_filenames setEnabled: YES];

		[tf_name setStringValue: name];

		s=[d objectForKey: Key];
		[tf_key setStringValue: s?s:@""];

		s=[d objectForKey: Commandline];
		[tf_cmdline setStringValue: s?s:@""];

		i=[[d objectForKey: Type] intValue];
		if (i<0 || i>2) i=0;
		[pb_type selectItemAtIndex: i];

		i=[[d objectForKey: Input] intValue];
		if (i<0 || i>2) i=0;
		[pb_input selectItemAtIndex: i];

		i=[[d objectForKey: ReturnData] intValue];
		if (i<0 || i>1) i=0;
		[pb_output selectItemAtIndex: i];

		if ([d objectForKey: AcceptTypes])
		{
			i=[[d objectForKey: AcceptTypes] intValue];
			[cb_string setState: !!(i&1)];
			[cb_filenames setState: !!(i&2)];
		}
		else
		{
			[cb_string setState: 1];
			[cb_filenames setState: 0];
		}
	}
	else
	{
		[tf_name setEditable: NO];
		[tf_cmdline setEditable: NO];
		[tf_key setEditable: NO];
		[pb_input setEnabled: NO];
		[pb_output setEnabled: NO];
		[pb_type setEnabled: NO];
		[cb_string setEnabled: NO];
		[cb_filenames setEnabled: NO];

		[tf_name setStringValue: @""];
		[tf_key setStringValue: @""];
		[tf_cmdline setStringValue: @""];
	}

	current=r;
}


-(void) _removeService: (id)sender
{
	NSString *name;
	if (current<0)
		return;
	[list deselectAll: self];

	name=[service_list objectAtIndex: current];
	[services removeObjectForKey: name];
	[service_list removeObjectAtIndex: current];

	[list reloadData];
	current=-1;
	[self tableViewSelectionDidChange: nil];
}

-(void) _addService: (id)sender
{
	NSString *n=_(@"Unnamed service");
	[service_list addObject: n];
	[list reloadData];
	[list selectRow: [service_list count]-1 byExtendingSelection: NO];
}


-(void) _importServices: (id)sender
{
	NSOpenPanel *op;
	NSDictionary *d;
	NSEnumerator *e;
	NSString *name;
	int result;

	[self save];

	op=[NSOpenPanel openPanel];
	[op setAccessoryView: nil];
	[op setTitle: _(@"Import services")];
	[op setAllowsMultipleSelection: NO];
	[op setCanChooseDirectories: NO];
	[op setCanChooseFiles: YES];

	result=[op runModal];

	if (result!=NSOKButton)
		return;

	d=[NSDictionary dictionaryWithContentsOfFile: [op filename]];
	d=[d objectForKey: @"TerminalServices"];
	if (!d)
	{
		NSString *s=[NSString stringWithContentsOfFile: [op filename]];
		NSArray *lines=[s componentsSeparatedByString: @"\n"];
		NSArray *parts;
		int c=[lines count];
		int i,j,k;
		NSMutableDictionary *md=[[[NSMutableDictionary alloc] init] autorelease];
		NSMutableDictionary *service;

		for (i=0;i<c;i+=3)
		{
			if (i+3>=c)
				break;
		
			parts=[[lines objectAtIndex: i] componentsSeparatedByString: @" "];
			if ([parts count]!=11)
				break;

			service=[[NSMutableDictionary alloc] init];

			[service setObject: [lines objectAtIndex: i+2]
				forKey: Commandline];

			j=[[parts objectAtIndex: 0] intValue];
			if (j!=32)
			{
				unichar ch=j;
				[service setObject: [NSString stringWithCharacters: &ch length: 1]
					forKey: Key];
			}

			j=[[parts objectAtIndex: 2] intValue];
			k=0;
			if (j&3) /* TODO? why 1 and 2? */
				k|=ACCEPT_STRING;
			if (j&8)
				k|=ACCEPT_FILENAMES;
			[service setObject: [NSString stringWithFormat: @"%i",k]
				forKey: AcceptTypes];

			if (j==1)
			{
				k=0;
			}
			else
			{
				j=[[parts objectAtIndex: 3] intValue];
				if (j==2)
					k=INPUT_CMDLINE;
				else if (j==4)
					k=INPUT_STDIN;
				else
					k=INPUT_NO;
			}
			[service setObject: [NSString stringWithFormat: @"%i",k]
				forKey: Input];

			j=[[parts objectAtIndex: 8] intValue];
			if (j==3)
				k=1;
			else
				k=0;
			[service setObject: [NSString stringWithFormat: @"%i",k]
				forKey: ReturnData];

			j=[[parts objectAtIndex: 5] intValue];
			if (j==2)
				k=TYPE_WINDOW_IDLE;
			else if (j==4)
				k=TYPE_WINDOW_NEW;
			else
				k=TYPE_BACKGROUND;
			[service setObject: [NSString stringWithFormat: @"%i",k]
				forKey: Type];

			[md setObject: service
				forKey: [lines objectAtIndex: i+1]];
		}

		if (i<c && (i!=c-1 || [[lines objectAtIndex: i] length]!=0))
		{
			NSRunAlertPanel(_(@"Error importing services"),
				_(@"The file '%@' doesn't contain valid terminal services."),
				nil,nil,nil,
				[op filename]);
			return;
		}
		d=md;
	}

	e=[d keyEnumerator];
	while ((name=[e nextObject]))
	{
		NSString *new_name;
		NSDictionary *service,*s2;
		int i;

		service=[d objectForKey: name];
		new_name=name;
		i=2;
		while (1)
		{
			s2=[services objectForKey: new_name];
			if (!s2 || [s2 isEqual: service])
				break;

			new_name=[NSString stringWithFormat: @"%@ (%i)",
				name,i];
			i++;
		}

		if (!s2)
		{
			[services setObject: service
				forKey: new_name];
			[service_list addObject: new_name];
		}
	}

	[self save]; /* update external service list */
	[self revert]; /* reload views */
	[list reloadData];
}


-(void) _exportServices: (id)sender
{
	NSSavePanel *sp;
	NSTableView *tv;
	int result;

	[self save];

	sp=[NSSavePanel savePanel];
	[sp setTitle: _(@"Export services")];

	{
		NSScrollView *sv;
		NSTableColumn *c_name;

		sv=[[NSScrollView alloc] initWithFrame: NSMakeRect(0,0,260,100)];
		[sv setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
		[sv setHasVerticalScroller: YES];
		[sv setHasHorizontalScroller: NO];
		[sv setBorderType: NSBezelBorder];

		c_name=[[NSTableColumn alloc] initWithIdentifier: @"Name"];
		[c_name setEditable: NO];
		[c_name setResizable: YES];
		[c_name setWidth: 260];

		tv=[[NSTableView alloc] initWithFrame: [[sv contentView] frame]];
		[tv setAllowsMultipleSelection: YES];
		[tv setAllowsColumnSelection: NO];
		[tv setAllowsEmptySelection: NO];
		[tv addTableColumn: c_name];
		DESTROY(c_name);
		[tv setAutoresizesAllColumnsToFit: YES];
		[tv setDataSource: self];
		[tv setDelegate: self];
		[tv setHeaderView: nil];
		[tv setCornerView: nil];
		[tv reloadData];
		[tv selectAll: self];

		[sv setDocumentView: tv];
		[sp setAccessoryView: sv];
		DESTROY(sv);
	}

	result=[sp runModal];

	if (result==NSOKButton)
	{
		NSEnumerator *e;
		NSNumber *n;
		NSMutableDictionary *d;
		NSDictionary *d2;
		NSString *name;

		d=[[NSMutableDictionary alloc] init];

		for (e=[tv selectedRowEnumerator];
		     (n=[e nextObject]);)
		{
			name=[service_list objectAtIndex: [n intValue]];
			[d setObject: [services objectForKey: name]
				forKey: name];
		}
		d2=[NSDictionary dictionaryWithObject: d
			forKey: @"TerminalServices"];
		[d2 writeToFile: [sp filename] atomically: NO];
		DESTROY(d);
	}

	DESTROY(tv);
	[sp setAccessoryView: nil];
}


-(NSString *) name
{
	return _(@"Terminal services");
}

-(void) setupButton: (NSButton *)b
{
	[b setTitle: _(@"Terminal\nservices")];
	[b sizeToFit];
}

-(void) willHide
{
}

-(NSView *) willShow
{
	if (!top)
	{
		GSHbox *hb;
	
		top=[[GSVbox alloc] init];
		[top setDefaultMinYMargin: 4];
		
		{
			GSHbox *hb;
			NSButton *b;

			hb=[[GSHbox alloc] init];
			[hb setDefaultMinXMargin: 4];
			[hb setAutoresizingMask: NSViewMinXMargin];

			b=[[NSButton alloc] init];
			[b setTitle: _(@"Add")];
			[b setTarget: self];
			[b setAction: @selector(_addService:)];
			[b sizeToFit];
			[hb addView: b  enablingXResizing: NO];
			DESTROY(b);

			b=[[NSButton alloc] init];
			[b setTitle: _(@"Remove")];
			[b setTarget: self];
			[b setAction: @selector(_removeService:)];
			[b sizeToFit];
			[hb addView: b  enablingXResizing: NO];
			DESTROY(b);

			b=[[NSButton alloc] init];
			[b setTitle: _(@"Import...")];
			[b setTarget: self];
			[b setAction: @selector(_importServices:)];
			[b sizeToFit];
			[hb addView: b  enablingXResizing: NO];
			DESTROY(b);

			b=[[NSButton alloc] init];
			[b setTitle: _(@"Export...")];
			[b setTarget: self];
			[b setAction: @selector(_exportServices:)];
			[b sizeToFit];
			[hb addView: b  enablingXResizing: NO];
			DESTROY(b);

			[top addView: hb enablingYResizing: NO];
			DESTROY(hb);
		}

		hb=[[GSHbox alloc] init];
		[hb setDefaultMinXMargin: 4];
		[hb setAutoresizingMask: NSViewWidthSizable];

		{
			NSPopUpButton *b;
			GSVbox *vb;

			vb=[[GSVbox alloc] init];
			[vb setDefaultMinYMargin: 4];
			[vb setAutoresizingMask: NSViewMaxXMargin];

			b=pb_output=[[NSPopUpButton alloc] init];
			[b setAutoresizingMask: NSViewWidthSizable];
			[b setAutoenablesItems: NO];
			[b addItemWithTitle: _(@"Ignore output")];
			[b addItemWithTitle: _(@"Return output")];
			[b sizeToFit];
			[vb addView: b enablingYResizing: NO];
			[b release];

			b=pb_input=[[NSPopUpButton alloc] init];
			[b setAutoresizingMask: NSViewWidthSizable];
			[b setAutoenablesItems: NO];
			[b addItemWithTitle: _(@"No input")];
			[b addItemWithTitle: _(@"Input in stdin")];
			[b addItemWithTitle: _(@"Input on command line")];
			[b sizeToFit];
			[vb addView: b enablingYResizing: NO];
			[b release];

			b=pb_type=[[NSPopUpButton alloc] init];
			[b setAutoresizingMask: NSViewWidthSizable];
			[b setAutoenablesItems: NO];
			[b addItemWithTitle: _(@"Run in background")];
			[b addItemWithTitle: _(@"Run in new window")];
			[b addItemWithTitle: _(@"Run in idle window")];
			[b sizeToFit];
			[vb addView: b enablingYResizing: NO];
			[b release];

			[hb addView: vb enablingXResizing: YES];
			DESTROY(vb);
		}

		{
			NSButton *b;
			NSBox *box;
			GSVbox *vb;

			box=[[NSBox alloc] init];
			[box setTitle: _(@"Accept types")];
			[box setAutoresizingMask: NSViewMinXMargin|NSViewMinYMargin];

			vb=[[GSVbox alloc] init];
			[vb setDefaultMinYMargin: 4];

			b=cb_filenames=[[NSButton alloc] init];
			[b setAutoresizingMask: NSViewWidthSizable];
			[b setButtonType: NSSwitchButton];
			[b setTitle: _(@"Filenames")];
			[b sizeToFit];
			[vb addView: b enablingYResizing: NO];
			[b release];

			b=cb_string=[[NSButton alloc] init];
			[b setAutoresizingMask: NSViewWidthSizable];
			[b setButtonType: NSSwitchButton];
			[b setTitle: _(@"Plain text")];
			[b sizeToFit];
			[vb addView: b enablingYResizing: NO];
			[b release];

			[box setContentView: vb];
			[box sizeToFit];
			DESTROY(vb);
			[hb addView: box enablingXResizing: YES];
			DESTROY(box);
		}

		[top addView: hb enablingYResizing: NO];
		DESTROY(hb);

		{
			GSTable *t;
			NSTextField *f;


			t=[[GSTable alloc] initWithNumberOfRows: 3 numberOfColumns: 2];
			[t setAutoresizingMask: NSViewWidthSizable];
			[t setXResizingEnabled: NO forColumn: 0];
			[t setXResizingEnabled: YES forColumn: 1];

			f=[NSTextField newLabel: _(@"Name:")];
			[f setAutoresizingMask: NSViewMinXMargin];
			[f setAutoresizingMask: 0];
			[t putView: f atRow: 2 column: 0 withXMargins: 2 yMargins: 2];
			DESTROY(f);

			tf_name=f=[[NSTextField alloc] init];
			[f setAutoresizingMask: NSViewWidthSizable];
			[f sizeToFit];
			[t putView: f atRow: 2 column: 1];
			DESTROY(f);


			f=[NSTextField newLabel: _(@"Key:")];
			[f setAutoresizingMask: NSViewMinXMargin];
			[f setAutoresizingMask: 0];
			[t putView: f atRow: 1 column: 0 withXMargins: 2 yMargins: 2];
			DESTROY(f);

			tf_key=f=[[NSTextField alloc] init];
			[f setAutoresizingMask: NSViewWidthSizable];
			[f sizeToFit];
			[t putView: f atRow: 1 column: 1];
			DESTROY(f);


			f=[NSTextField newLabel: _(@"Command line:")];
			[f setAutoresizingMask: NSViewMinXMargin];
			[f setAutoresizingMask: 0];
			[t putView: f atRow: 0 column: 0 withXMargins: 2 yMargins: 2];
			DESTROY(f);

			tf_cmdline=f=[[NSTextField alloc] init];
			[f setAutoresizingMask: NSViewWidthSizable];
			[f sizeToFit];
			[t putView: f atRow: 0 column: 1];
			DESTROY(f);


			[top addView: t enablingYResizing: NO];
			DESTROY(t);
		}

		{
			NSScrollView *sv;
			NSTableColumn *c_name;

			sv=[[NSScrollView alloc] init];
			[sv setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
			[sv setHasVerticalScroller: YES];
			[sv setHasHorizontalScroller: NO];
			[sv setBorderType: NSBezelBorder];

			c_name=[[NSTableColumn alloc] initWithIdentifier: @"Name"];
			[c_name setEditable: NO];
			[c_name setResizable: YES];

			list=[[NSTableView alloc] initWithFrame: [[sv contentView] frame]];
			[list setAllowsMultipleSelection: NO];
			[list setAllowsColumnSelection: NO];
			[list setAllowsEmptySelection: NO];
			[list addTableColumn: c_name];
			DESTROY(c_name);
			[list setAutoresizesAllColumnsToFit: YES];
			[list setDataSource: self];
			[list setDelegate: self];
			[list setHeaderView: nil];
			[list setCornerView: nil];

			[sv setDocumentView: list];
			[top addView: sv enablingYResizing: YES];
			[list release];
			DESTROY(sv);
		}

		current=-1;
		[self revert];
	}
	return top;
}

-(void) dealloc
{
	DESTROY(top);
	DESTROY(services);
	DESTROY(service_list);
	[super dealloc];
}

@end