Commits

Thijs Alkemade committed 3ff7575

Give users the ability to edit a regexp if it throws an error.

Comments (0)

Files changed (6)

 #define KEY_SF_FILTERS @"SF Filters"
 #define PREF_GROUP_SPAMFILTER @"SpamFilter Plugin"
 
+#define KEY_SF_PHRASE @"String"
+#define KEY_SF_CASE_SENSITIVE @"Case Sensitive"
+#define KEY_SF_REGEX @"Regular Expression"
+
 @class AISFPreferences;
 
 @interface AISFPlugin : NSObject <AIPlugin> {
 											   object:nil];
 	
 	AILogWithSignature(@"Adium spamfilter plugin loaded.");
+	
+	// the dynamic nib loading for preference panes can cause problems when editing outside the preferences.
+	// so use this trick to load it anyways.
+	[preferences view];
 }
 
 - (void)uninstallPlugin
 																group:PREF_GROUP_SPAMFILTER];
 	
 	for (NSDictionary *message in blacklist) {
-		if ([[message valueForKey:@"Regular Expression"] boolValue]) {
+		if ([[message valueForKey:KEY_SF_REGEX] boolValue]) {
 			
 			NSPredicate *regex;
 			
-			if ([[message valueForKey:@"Case sensitive"] boolValue]) {
-				regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", [message valueForKey:@"String"]];
+			if ([[message valueForKey:KEY_SF_CASE_SENSITIVE] boolValue]) {
+				regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", [message valueForKey:KEY_SF_PHRASE]];
 			} else {
-				regex = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", [message valueForKey:@"String"]];
+				regex = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", [message valueForKey:KEY_SF_PHRASE]];
 			}
 			
 			@try {
 					break;
 				}
 			}
-			@catch (NSException * e) {
+			@catch (NSException *e) {
 				AILog(@"Regex %@ seems to have failed: %@", message, e);
 				// show the error after a delay, so the incoming message doesn't have to wait
-				[self performSelector:@selector(error:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:message, @"Message", e, @"Exception", nil] afterDelay:0];
+				[self performSelector:@selector(error:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:message, @"Message", e, @"Exception", nil] afterDelay:0.1];
 			}
 			
-		} else if ([contentObject.message.string rangeOfString:[message valueForKey:@"String"]
-												options:([[message valueForKey:@"Case sensitive"] boolValue] ? 0 : NSCaseInsensitiveSearch)].location != NSNotFound) {
+		} else if ([contentObject.message.string rangeOfString:[message valueForKey:KEY_SF_PHRASE]
+												options:([[message valueForKey:KEY_SF_CASE_SENSITIVE] boolValue] ? 0 : NSCaseInsensitiveSearch)].location != NSNotFound) {
 			hidden = YES;
 			AILogWithSignature(@"Hiding %@ as it matches %@", contentObject, message);
 			break;
 				 
 - (void)error:(NSDictionary *)context
 {
-	NSRunAlertPanel([NSString stringWithFormat:@"Evaluation of regular expression \"%@\" failed.", [[context valueForKey:@"Message"] valueForKey:@"String"]],
-					[NSString stringWithFormat:@"Adium SpamFilter plugin encountered an error when evaluating this regular expression:\n\n%@", [context valueForKey:@"Exception"]],
-					@"OK",
-					nil,
-					nil);
+	NSInteger result = NSRunAlertPanel([NSString stringWithFormat:@"Evaluation of regular expression \"%@\" failed.", [[context valueForKey:@"Message"] valueForKey:KEY_SF_PHRASE]],
+									   [NSString stringWithFormat:@"Adium SpamFilter plugin encountered an error when evaluating this regular expression:\n\n%@", [context valueForKey:@"Exception"]],
+									   @"OK",
+									   @"Edit expression",
+									   nil);
+	
+	if (result == NSAlertAlternateReturn) {
+		[preferences editObject:[context valueForKey:@"Message"]];
+	}
 }
 
 - (NSString *)pluginAuthor

AISFPreferences.h

 	IBOutlet NSTextField		*label_explanation;
 	
 	NSMutableArray				*blacklist;
+	NSMutableDictionary			*currentlyEditing;
 }
 - (IBAction)add:(id)sender;
 - (IBAction)remove:(id)sender;
 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;
 
+- (void)editObject:(NSDictionary *)inObject;
+
 @end

AISFPreferences.m

 
 - (IBAction)add:(id)sender
 {
+	[currentlyEditing release]; currentlyEditing = nil;
+	
 	[addField setStringValue:@""];
 	[addField becomeFirstResponder];
 	
 {
 	AILogWithSignature(@"Adding %@ to blacklist", [addField stringValue]);
 	
-	NSMutableDictionary *newWord = [NSMutableDictionary dictionaryWithObjectsAndKeys:[addField stringValue], @"String",
-									[NSNumber numberWithBool:([phraseIsCaseSensitive state] == NSOnState)], @"Case sensitive",
-									[NSNumber numberWithBool:([phraseIsRegularExpression state] == NSOnState)], @"Regular Expression", nil];
-	
-	[blacklist addObject:newWord];
+	if (!currentlyEditing) {
+		NSMutableDictionary *newWord = [NSMutableDictionary dictionaryWithObjectsAndKeys:[addField stringValue], KEY_SF_PHRASE,
+										[NSNumber numberWithBool:([phraseIsCaseSensitive state] == NSOnState)], KEY_SF_CASE_SENSITIVE,
+										[NSNumber numberWithBool:([phraseIsRegularExpression state] == NSOnState)], KEY_SF_REGEX, nil];
+		
+		
+		[blacklist addObject:newWord];
+	} else {
+		[currentlyEditing setValue:[addField stringValue] forKey:KEY_SF_PHRASE];
+		[currentlyEditing setValue:[NSNumber numberWithBool:([phraseIsCaseSensitive state] == NSOnState)] forKey:KEY_SF_CASE_SENSITIVE];
+		[currentlyEditing setValue:[NSNumber numberWithBool:([phraseIsRegularExpression state] == NSOnState)] forKey:KEY_SF_REGEX];
+	}
+
 	
 	[self saveTerms];
 	
 
 - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
 {
-	if ([[tableColumn identifier] isEqualToString:@"Case sensitive"] || [[tableColumn identifier] isEqualToString:@"Regular Expression"]) {
+	if ([[tableColumn identifier] isEqualToString:KEY_SF_CASE_SENSITIVE] || [[tableColumn identifier] isEqualToString:KEY_SF_REGEX]) {
 		[cell setTitle:@""];
 	}
 }
 
+- (void)editObject:(NSDictionary *)inObject
+{
+	currentlyEditing = [inObject retain];
+	
+	[addField setStringValue:[inObject valueForKey:KEY_SF_PHRASE]];
+	[phraseIsCaseSensitive setState:[[inObject valueForKey:KEY_SF_CASE_SENSITIVE] integerValue]];
+	[phraseIsRegularExpression setState:[[inObject valueForKey:KEY_SF_REGEX] integerValue]];
+	
+	[addSheet makeKeyAndOrderFront:self];
+	[addSheet setLevel:NSModalPanelWindowLevel];
+}
+
 @end

English.lproj/SFPreferences.xib

 		</object>
 		<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
 			<bool key="EncodedWithXMLCoder">YES</bool>
-			<integer value="23"/>
-			<integer value="43"/>
+			<integer value="53"/>
+			<integer value="22"/>
 		</object>
 		<object class="NSArray" key="IBDocument.PluginDependencies">
 			<bool key="EncodedWithXMLCoder">YES</bool>
 										<object class="NSMutableArray" key="NSTableColumns">
 											<bool key="EncodedWithXMLCoder">YES</bool>
 											<object class="NSTableColumn" id="161040152">
-												<string key="NSIdentifier">Case sensitive</string>
+												<string key="NSIdentifier">Case Sensitive</string>
 												<double key="NSWidth">84</double>
 												<double key="NSMinWidth">40</double>
 												<double key="NSMaxWidth">1000</double>
 								<int key="NSvFlags">-2147483392</int>
 								<string key="NSFrame">{{1, 185}, {438, 15}}</string>
 								<reference key="NSSuperview" ref="789821158"/>
-								<bool key="NSEnabled">YES</bool>
 								<int key="NSsFlags">1</int>
 								<reference key="NSTarget" ref="789821158"/>
 								<string key="NSAction">_doScroller:</string>
 						<string key="NSFrame">{{5, 34}, {326, 154}}</string>
 						<reference key="NSSuperview" ref="821482609"/>
 						<reference key="NSNextKeyView" ref="455047774"/>
-						<int key="NSsFlags">690</int>
+						<int key="NSsFlags">562</int>
 						<reference key="NSVScroller" ref="308268912"/>
 						<reference key="NSHScroller" ref="337151649"/>
 						<reference key="NSContentView" ref="455047774"/>
 				<int key="NSWindowStyleMask">31</int>
 				<int key="NSWindowBacking">2</int>
 				<string key="NSWindowRect">{{140, 334}, {399, 124}}</string>
-				<int key="NSWTFlags">-1535639552</int>
-				<string key="NSWindowTitle">Window</string>
+				<int key="NSWTFlags">-461897728</int>
+				<string key="NSWindowTitle">Edit Phrase</string>
 				<string key="NSWindowClass">NSPanel</string>
 				<nil key="NSViewClass"/>
 				<string key="NSWindowContentMaxSize">{1.79769e+308, 1.79769e+308}</string>
 							<object class="NSTextFieldCell" key="NSCell" id="91945047">
 								<int key="NSCellFlags">68288064</int>
 								<int key="NSCellFlags2">272630784</int>
-								<string key="NSContents">Add word:</string>
+								<string key="NSContents">Phrase:</string>
 								<reference key="NSSupport" ref="941022885"/>
 								<reference key="NSControlView" ref="462415365"/>
 								<reference key="NSBackgroundColor" ref="483544159"/>

spamfilter.xcodeproj/project.pbxproj

 				ALWAYS_SEARCH_USER_PATHS = NO;
 				COPY_PHASE_STRIP = YES;
 				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+				DEPLOYMENT_LOCATION = NO;
+				DSTROOT = "/tmp/$(PROJECT_NAME).dst";
 				FRAMEWORK_SEARCH_PATHS = (
 					"$(inherited)",
 					"\"$(SRCROOT)/../adium/build/Release\"",
 				);
 				GCC_MODEL_TUNING = G5;
 				INFOPLIST_FILE = Info.plist;
-				INSTALL_PATH = "\"$(HOME)/Library/Application Support/Adium 2.0/PlugIns/\"";
+				INSTALL_PATH = "\"$(HOME)/Library/Application\\ Support/Adium\\ 2.0/PlugIns/\"";
 				OTHER_LDFLAGS = (
 					"-undefined",
 					dynamic_lookup,
 				);
 				PRODUCT_NAME = spamfilter;
 				SDKROOT = macosx10.5;
+				SKIP_INSTALL = NO;
 				WRAPPER_EXTENSION = AdiumPlugin;
 			};
 			name = Release;