When we receive a distributed notification, any objects added to the autorelease...
[adiumx.git] / Source / ESDebugWindowController.m
blob73c632673be0858259c502282107746d69bfd238
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "ESDebugController.h"
18 #import "ESDebugWindowController.h"
19 #import <Adium/AIPreferenceControllerProtocol.h>
20 #import <AIUtilities/AIAutoScrollView.h>
22 #define KEY_DEBUG_WINDOW_FRAME  @"Debug Window Frame"
23 #define DEBUG_WINDOW_NIB                @"DebugWindow"
25 @implementation ESDebugWindowController
26 #ifdef DEBUG_BUILD
28 static ESDebugWindowController *sharedDebugWindowInstance = nil;
30 //Return the shared contact info window
31 + (id)showDebugWindow
33     //Create the window
34     if (!sharedDebugWindowInstance) {
35         sharedDebugWindowInstance = [[self alloc] initWithWindowNibName:DEBUG_WINDOW_NIB];
36     }
37         
38         //Configure and show window
39         [sharedDebugWindowInstance showWindow:nil];
40         
41         return sharedDebugWindowInstance;
44 + (BOOL)debugWindowIsOpen
46         return sharedDebugWindowInstance != nil;
49 - (void)performFilter
51         NSEnumerator *enumerator = [fullDebugLogArray objectEnumerator];
52         NSString         *aDebugString;
53         
54         [mutableDebugString setString:@""];
55         while ((aDebugString = [enumerator nextObject])) {
56                 if (!filter || 
57                         [aDebugString rangeOfString:filter options:NSCaseInsensitiveSearch].location != NSNotFound) {
58                         [mutableDebugString appendString:aDebugString];                 
59                 }
60         }
61         
62         
63         [[textView_debug textStorage] addAttribute:NSParagraphStyleAttributeName
64                                                                                  value:debugParagraphStyle
65                                                                                  range:NSMakeRange(0, [mutableDebugString length])];
67 //      [fullDebugLogArray removeAllObjects];
68         
69 //      [[adium debugController] clearDebugLogArray];
70         
71         [scrollView_debug scrollToBottom];      
74 - (void)addedDebugMessage:(NSString *)aDebugString
76         [fullDebugLogArray addObject:aDebugString];
78         if (!filter || 
79                 [aDebugString rangeOfString:filter options:NSCaseInsensitiveSearch].location != NSNotFound) {
80                 unsigned int aDebugStringLength = [aDebugString length];
81                 
82                 [mutableDebugString appendString:aDebugString];
83                 
84                 [[textView_debug textStorage] addAttribute:NSParagraphStyleAttributeName
85                                                                                          value:debugParagraphStyle
86                                                                                          range:NSMakeRange([mutableDebugString length] - aDebugStringLength, aDebugStringLength)];
87         }
89 + (void)addedDebugMessage:(NSString *)aDebugString
91         if (sharedDebugWindowInstance) [sharedDebugWindowInstance addedDebugMessage:aDebugString];
94 - (NSString *)adiumFrameAutosaveName
96         return KEY_DEBUG_WINDOW_FRAME;
99 //Setup the window before it is displayed
100 - (void)windowDidLoad
101 {    
102         NSEnumerator    *enumerator;
103         NSString                *aDebugString;
105         [super windowDidLoad];
107         //We store the reference to the mutableString of the textStore for efficiency
108         mutableDebugString = [[[textView_debug textStorage] mutableString] retain];
109         fullDebugLogArray = [[NSMutableArray alloc] init];
111         debugParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
112         [debugParagraphStyle setHeadIndent:12];
113         [debugParagraphStyle setFirstLineHeadIndent:2];
114         
115         [scrollView_debug setAutoScrollToBottom:YES];
117         //Load the logs which were added before the window was loaded
118         enumerator = [[[adium debugController] debugLogArray] objectEnumerator];
119         while ((aDebugString = [enumerator nextObject])) {
120                 [mutableDebugString appendString:aDebugString];
121                 if ((![aDebugString hasSuffix:@"\n"]) && (![aDebugString hasSuffix:@"\r"])) {
122                         [mutableDebugString appendString:@"\n"];
123                 }
124                 [fullDebugLogArray addObject:aDebugString];
125         }
128         [[self window] setTitle:AILocalizedString(@"Adium Debug Log","Debug window title")];
129         [checkBox_logWriting setLocalizedString:AILocalizedString(@"Log to ~/Library/Logs/Adium Debug", "Logging checkbox in the Adium Debug Window")];
131         //On the next run loop, scroll to the bottom
132         [scrollView_debug performSelector:@selector(scrollToBottom)
133                                                    withObject:nil
134                                                    afterDelay:0.001];
135         
136         [checkBox_logWriting setState:[[[adium preferenceController] preferenceForKey:KEY_DEBUG_WRITE_LOG
137                                                                                                                                                         group:GROUP_DEBUG] boolValue]];
140 //Close the debug window
141 + (void)closeDebugWindow
143     if (sharedDebugWindowInstance) {
144         [sharedDebugWindowInstance closeWindow:nil];
145     }
148 //called as the window closes
149 - (void)windowWillClose:(id)sender
151         [super windowWillClose:sender];
152         
153         //Close down
154         [mutableDebugString release]; mutableDebugString = nil;
155         [fullDebugLogArray release]; fullDebugLogArray = nil;
156         [debugParagraphStyle release]; debugParagraphStyle = nil;
157     [self autorelease]; sharedDebugWindowInstance = nil;
160 - (IBAction)toggleLogWriting:(id)sender
162         [[adium preferenceController] setPreference:[NSNumber numberWithBool:[sender state]]
163                                                                                  forKey:KEY_DEBUG_WRITE_LOG
164                                                                                   group:GROUP_DEBUG];
167 - (IBAction)clearLog:(id)sender
169         [mutableDebugString setString:@""];
170         [fullDebugLogArray removeAllObjects];
172         [[adium debugController] clearDebugLogArray];
173         
174         [scrollView_debug scrollToTop];
177 - (void)setFilter:(NSString *)inFilter
179         if (inFilter != filter) {
180                 [filter release];
181                 filter = [inFilter copy];
183                 [self performFilter];
184         }
187 #endif
189 @end