forked from edenwaith/GUI-Tar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleController.m
More file actions
executable file
·358 lines (300 loc) · 13.1 KB
/
Copy pathConsoleController.m
File metadata and controls
executable file
·358 lines (300 loc) · 13.1 KB
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
//
// ConsoleController.m
// GUI Tar
//
// Created by Chad Armstrong on 1/17/09.
// Copyright 2009 Edenwaith. All rights reserved.
//
// Copyright 2003-2011 Chad Armstrong <support@edenwaith.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see <http://www.gnu.org/licenses/>.
#import "ConsoleController.h"
#import "WinController.h"
@implementation ConsoleController
- (id) init
{
self = [super initWithWindowNibName: @"Console"];
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self name: NSWindowWillCloseNotification object: consoleWindow];
[super dealloc];
}
- (void) windowDidLoad
{
}
// =========================================================================
// (void) awakeFromNib
// -------------------------------------------------------------------------
//
// -------------------------------------------------------------------------
// Created: 18 January 2009 0:42
// Version: 21 January 2011 21:10
// =========================================================================
- (void) awakeFromNib
{
consoleToolbar = [[[NSToolbar alloc] initWithIdentifier:@"consoleToolbar"] autorelease];
[consoleToolbar setDelegate:self];
[consoleToolbar setAllowsUserCustomization:YES];
[consoleToolbar setAutosavesConfiguration:YES];
[consoleToolbar setSizeMode:NSToolbarSizeModeDefault];
[consoleToolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
[consoleWindow setToolbar:consoleToolbar];
// [[self window] setShowsToolbarButton:NO];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(closeConsoleLogPanel:)
name:NSWindowWillCloseNotification
object:consoleWindow];
// Do not allow the text fields to be editable (they are selectable, however)
[textView setEditable: NO];
[searchTextView setEditable: NO];
// Load in console log data
if ([[NSFileManager defaultManager] fileExistsAtPath: [@"~/Library/Logs/GUITar.log" stringByExpandingTildeInPath]] == YES)
{
NSString *consoleTextString = [[NSString alloc] initWithContentsOfFile:[@"~/Library/Logs/GUITar.log" stringByExpandingTildeInPath]];
[textView setEditable:YES];
[textView insertText: consoleTextString];
[textView setEditable:NO];
// This should scroll to the end of the console, but doesn't seem to be perfect...
[textView scrollRangeToVisible: NSMakeRange([[textView string] length], 0)];
[consoleTextString release];
}
if ([[NSUserDefaults standardUserDefaults] objectForKey: @"ConsoleLogPosition"] != nil)
{
NSRect consolePosition = NSRectFromString ([[NSUserDefaults standardUserDefaults] objectForKey: @"ConsoleLogPosition"]);
[consoleWindow setFrame: consolePosition display: YES];
}
}
#pragma mark -
#pragma mark Toolbar Methods
// =========================================================================
// (NSToolbarItem *) toolbar: (NSToolbar *) toolbar ...
// -------------------------------------------------------------------------
// http://www.macdevcenter.com/pub/a/mac/2002/03/15/cocoa.html?page=3
// http://developer.apple.com/documentation/Cocoa/Conceptual/SearchFields/Articles/MenuTemplate.html
// http://developer.apple.com/documentation/Cocoa/Conceptual/SearchFields/Articles/MenuTemplate.html#//apple_ref/doc/uid/20002245-BABFIBIA
// http://developer.apple.com/samplecode/SearchField/listing3.html
// -------------------------------------------------------------------------
// Created: 18 January 2009 15:20
// Version: 5 March 2009 23:05
// =========================================================================
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
if ( [itemIdentifier isEqualToString:@"Clear"] )
{
[item setLabel:@"Clear"];
[item setToolTip: @"Clear Log"];
[item setPaletteLabel:[item label]];
[item setImage:[NSImage imageNamed:@"Clear"]];
[item setTarget:self];
[item setAction:@selector(clearConsole:)];
}
else if ([itemIdentifier isEqual: @"SearchField"])
{
[item setLabel:@"Search"];
[item setPaletteLabel:@"Search"];
[item setToolTip:@"Search Your Document"];
searchField = [[[NSSearchField alloc] init] autorelease];
[searchField setAction:@selector(searchText:)];
[[searchField cell] setSendsWholeSearchString: NO];
[item setView:searchField];
[item setMinSize:NSMakeSize(100,NSHeight([searchFieldView frame]))];
[item setMaxSize:NSMakeSize(200,NSHeight([searchFieldView frame]))];
NSMenu *cellMenu = [[[NSMenu alloc] initWithTitle: @"Search Menu"] autorelease];
NSMenuItem *item;
item = [[[NSMenuItem alloc] initWithTitle:@"Recent Searches"
action:NULL
keyEquivalent:@""] autorelease];
[item setTag:NSSearchFieldRecentsTitleMenuItemTag];
[cellMenu insertItem:item atIndex:0];
item = [[[NSMenuItem alloc] initWithTitle:@"Recents"
action:NULL
keyEquivalent:@""] autorelease];
[item setTag:NSSearchFieldRecentsMenuItemTag];
[cellMenu insertItem:item atIndex:1];
item = [NSMenuItem separatorItem];
[item setTag:NSSearchFieldRecentsTitleMenuItemTag];
[cellMenu insertItem:item atIndex:2];
item = [[[NSMenuItem alloc] initWithTitle: @"Clear" action: NULL keyEquivalent: @""] autorelease];
[item setTag: NSSearchFieldClearRecentsMenuItemTag];
[cellMenu insertItem: item atIndex: 3];
item = [[[NSMenuItem alloc] initWithTitle:@"No recent searches"
action:NULL
keyEquivalent:@""] autorelease];
[item setTag:NSSearchFieldNoRecentsMenuItemTag];
[cellMenu insertItem:item atIndex:4];
id searchCell = [searchField cell];
[searchCell setSearchMenuTemplate:cellMenu];
}
return [item autorelease];
}
// Created: 18 January 2009 15:20
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray arrayWithObjects: @"Clear",
@"SearchField",
NSToolbarSpaceItemIdentifier,
NSToolbarFlexibleSpaceItemIdentifier,
NSToolbarPrintItemIdentifier,
NSToolbarSeparatorItemIdentifier,
nil];
}
// Created: 18 January 2009 15:20
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray arrayWithObjects: @"Clear",
NSToolbarFlexibleSpaceItemIdentifier,
@"SearchField", nil];
}
#pragma mark -
// =========================================================================
// (BOOL) isVisible
// -------------------------------------------------------------------------
// Return if the console window is visible or not
// -------------------------------------------------------------------------
// Created: 18 January 2009
// Version: 18 January 2009
// =========================================================================
- (BOOL) isVisible
{
return ([consoleWindow isVisible]);
}
// =========================================================================
// (IBAction) clearConsole: (id) sender
// -------------------------------------------------------------------------
// Clear the console and save out blank data to the GUI Tar log file
// -------------------------------------------------------------------------
// Created: 18 January 2009 15:27
// Version: 26 January 2009 21:41
// =========================================================================
- (IBAction) clearConsole: (id) sender
{
NSRange theEnd = NSMakeRange(0, [[textView string] length]);
[textView replaceCharactersInRange: theEnd withString: @""];
theEnd.location = 0;
[textView scrollRangeToVisible: theEnd];
[[textView string] writeToFile:[@"~/Library/Logs/GUITar.log" stringByExpandingTildeInPath] atomically:YES];
}
// =========================================================================
// (void) openConsoleLogPanel
// -------------------------------------------------------------------------
// Open the console log window, position it, and set its size
// -------------------------------------------------------------------------
// Created: 14 February 2009 19:04
// Version: 14 February 2009 19:04
// =========================================================================
- (void) openConsoleLogPanel
{
[consoleWindow orderFront: nil];
}
// =========================================================================
// (void) closeConsoleLogPanel: (NSNotification *) aNotification
// -------------------------------------------------------------------------
// When the console log panel has closed, this notification sets the
// consoleLogMenu to be in an off state (no check mark next to it), and
// save the position and size since there is an oddity where the Autosave
// doesn't work with the Console Log
// -------------------------------------------------------------------------
// Created: 25 January 2009 14:33
// Version: 16 February 2009 20:13
// =========================================================================
- (void) closeConsoleLogPanel: (NSNotification *) aNotification
{
[[NSUserDefaults standardUserDefaults] setObject: NSStringFromRect( NSMakeRect([consoleWindow frame].origin.x, [consoleWindow frame].origin.y, [consoleWindow frame].size.width, [consoleWindow frame].size.height)) forKey:@"ConsoleLogPosition"];
[[NSUserDefaults standardUserDefaults] synchronize]; // Force the defaults to update
[[NSApp delegate] consoleLogClosed];
}
// =========================================================================
// (void) writeToConsole: (NSString *) string
// -------------------------------------------------------------------------
//
// -------------------------------------------------------------------------
// Created: 8 February 2009 19:47
// Version: 8 February 2009 19:47
// =========================================================================
- (void) writeToConsole: (NSString *) string
{
NSRange theEnd = NSMakeRange([[textView string] length], 0);
[textView replaceCharactersInRange: theEnd withString: string];
theEnd.location += [string length];
[textView scrollRangeToVisible: theEnd];
}
// =========================================================================
// - (void) saveLogToFile
// -------------------------------------------------------------------------
// Created: 10 February 2009 20:08
// Version: 10 February 2009 20:08
// =========================================================================
- (void) saveLogToFile
{
[ [textView string] writeToFile:[@"~/Library/Logs/GUITar.log" stringByExpandingTildeInPath] atomically:YES];
}
// =========================================================================
// - (IBAction) searchText: (id) sender
// -------------------------------------------------------------------------
// Search through the console log and display the results
// -------------------------------------------------------------------------
// Created: 15 February 2009 16:30
// Version: 15 February 2009 19:11
// =========================================================================
- (IBAction) searchText: (id) sender
{
NSString *searchString = [sender stringValue];
if ((searchString != nil) && (![searchString isEqualToString:@""]))
{
// Get original text and put into an NSString or NSArray
// Iterate through the log string/array. Add each "line" into a string
// Put the search result string into the text view
// Thought: Could NSPredicate also be used to search?
NSArray *originalLog = [[textView string] componentsSeparatedByString:@"\n"];
NSMutableString *searchResults = [[NSMutableString alloc] init];
[consoleTabView selectTabViewItemAtIndex: 1];
for (int i = 0; i < [originalLog count]; i++)
{
if ([[originalLog objectAtIndex: i] rangeOfString: searchString options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[searchResults appendString: [originalLog objectAtIndex: i]];
[searchResults appendString: @"\n"]; // append a new line character after each result
}
}
if ([searchResults isEqualToString: @""]) // No results found
{
[searchTextView setEditable:YES];
NSRange theEnd = NSMakeRange(0, [[searchTextView string] length]);
[searchTextView replaceCharactersInRange: theEnd withString: @""];
theEnd.location = 0;
[searchTextView scrollRangeToVisible: theEnd];
[searchTextView setEditable:NO];
}
else
{
[searchTextView setEditable:YES];
NSRange theEnd = NSMakeRange(0, [[searchTextView string] length]);
[searchTextView replaceCharactersInRange: theEnd withString: searchResults];
theEnd.location = 0;
[searchTextView scrollRangeToVisible: theEnd];
[searchTextView setEditable:NO];
}
[searchResults release];
}
else // Otherwise, Cancel button was pressed, display original log
{
[consoleTabView selectTabViewItemAtIndex: 0];
}
}
@end