From 3eec1a95c0546cd51df0536d8b7b5b1552bb43bc Mon Sep 17 00:00:00 2001 From: Michael Gorbach Date: Fri, 22 Jun 2012 22:38:33 -0400 Subject: [PATCH] If set to ignore commits from the current user, get all user emails from the AddressBook and ignore all of them, in addition to the email we get from git itself. --- Classes/Commit.h | 2 +- Classes/Commit.m | 4 +- Classes/GitifierAppDelegate.h | 5 ++- Classes/GitifierAppDelegate.m | 60 +++++++++++++++++++++++--- Classes/NotificationsPanelController.h | 4 -- Classes/NotificationsPanelController.m | 22 +++++++--- Gitifier.xcodeproj/project.pbxproj | 6 +++ NotificationsPreferencesPanel.xib | 28 ++++++------ 8 files changed, 98 insertions(+), 33 deletions(-) diff --git a/Classes/Commit.h b/Classes/Commit.h index ce664f9..e39f833 100644 --- a/Classes/Commit.h +++ b/Classes/Commit.h @@ -26,7 +26,7 @@ @property (retain) Repository *repository; + (Commit *) commitFromDictionary: (NSDictionary *) dictionary; -+ (NSArray *) chooseRelevantCommits: (NSArray *) commits forUser: (NSString *) userEmail; ++ (NSArray *) chooseRelevantCommits: (NSArray *) commits forUserEmails: (NSArray *) userEmails; - (BOOL) isMergeCommit; - (NSDictionary *) toDictionary; diff --git a/Classes/Commit.m b/Classes/Commit.m index b491658..70e593b 100644 --- a/Classes/Commit.m +++ b/Classes/Commit.m @@ -21,14 +21,14 @@ + (NSArray *) propertyList { return PSArray(@"authorName", @"authorEmail", @"subject", @"gitHash", @"date", @"repository"); } -+ (NSArray *) chooseRelevantCommits: (NSArray *) commits forUser: (NSString *) userEmail { ++ (NSArray *) chooseRelevantCommits: (NSArray *) commits forUserEmails: (NSArray *) userEmails { BOOL ignoreMerges = [GitifierDefaults boolForKey: IgnoreMergesKey]; BOOL ignoreOwnCommits = [GitifierDefaults boolForKey: IgnoreOwnCommitsKey]; NSMutableArray *relevantCommits = [NSMutableArray arrayWithCapacity: commits.count]; for (Commit *commit in commits) { if (ignoreMerges && [commit isMergeCommit]) continue; - if (ignoreOwnCommits && [commit.authorEmail isEqualToString: userEmail]) continue; + if (ignoreOwnCommits && [userEmails containsObject:commit.authorEmail]) continue; [relevantCommits addObject: commit]; } diff --git a/Classes/GitifierAppDelegate.h b/Classes/GitifierAppDelegate.h index ca4de53..dd8e37c 100644 --- a/Classes/GitifierAppDelegate.h +++ b/Classes/GitifierAppDelegate.h @@ -11,7 +11,7 @@ @class StatusBarController; @interface GitifierAppDelegate : NSObject { - NSString *userEmail; + NSString *userEmailFromGit; NSMutableArray *repositoryList; Monitor *monitor; StatusBarController *statusBarController; @@ -19,13 +19,14 @@ RepositoryListController *repositoryListController; } -@property (readonly) NSString *userEmail; @property (assign) NSMutableArray *repositoryList; @property IBOutlet Monitor *monitor; @property IBOutlet StatusBarController *statusBarController; @property IBOutlet PreferencesWindowController *preferencesWindowController; @property IBOutlet RepositoryListController *repositoryListController; +@property (nonatomic, readonly, retain) NSArray *userEmailAddresses; + // public - (IBAction) showPreferences: (id) sender; - (IBAction) quit: (id) sender; diff --git a/Classes/GitifierAppDelegate.m b/Classes/GitifierAppDelegate.m index 9600cd7..a49b1b1 100644 --- a/Classes/GitifierAppDelegate.m +++ b/Classes/GitifierAppDelegate.m @@ -17,13 +17,15 @@ #import "RepositoryListController.h" #import "StatusBarController.h" +#import + static NSString *SUEnableAutomaticChecksKey = @"SUEnableAutomaticChecks"; static NSString *SUSendProfileInfoKey = @"SUSendProfileInfo"; static CGFloat IntervalBetweenGrowls = 0.05; @implementation GitifierAppDelegate -@synthesize monitor, userEmail, preferencesWindowController, statusBarController, repositoryListController, +@synthesize monitor, preferencesWindowController, statusBarController, repositoryListController, repositoryList; // --- initialization and termination --- @@ -138,7 +140,7 @@ - (IBAction) quit: (id) sender { // --- user email management --- - (void) updateUserEmail { - if (!userEmail && [Git gitExecutable]) { + if (!userEmailFromGit && [Git gitExecutable]) { Git *git = [[Git alloc] initWithDelegate: self]; [git runCommand: @"config" withArguments: PSArray(@"user.email") inPath: NSHomeDirectory()]; } @@ -211,8 +213,8 @@ - (void) rejectGitPath { - (void) commandCompleted: (NSString *) command output: (NSString *) output { if ([command isEqual: @"config"]) { if (output && output.length > 0) { - userEmail = [output psTrimmedString]; - PSNotifyWithData(UserEmailChangedNotification, PSHash(@"email", userEmail)); + userEmailFromGit = [output psTrimmedString]; + PSNotifyWithData(UserEmailChangedNotification, PSHash(@"emails", self.userEmailAddresses)); } } else if ([command isEqual: @"version"]) { if (!output || ![output isMatchedByRegex: @"^git version \\d"]) { @@ -227,13 +229,61 @@ - (void) commandFailed: (NSString *) command output: (NSString *) output { } } +- (NSArray *)userEmailsFromAddressBook +{ + ABAddressBook *addressBook = [ABAddressBook sharedAddressBook]; + ABPerson *mePerson = [addressBook me]; + NSMutableArray *userEmailsToReturn = nil; + if (mePerson != nil) { + ABMultiValue *emailMultiValue = [mePerson valueForProperty:kABEmailProperty]; + for (NSString *identifier in emailMultiValue) + { + NSString *emailAddress = [[emailMultiValue valueForIdentifier:identifier] psTrimmedString]; + if ([emailAddress length] > 0) { + if (userEmailsToReturn == nil) { + userEmailsToReturn = [NSMutableArray array]; + } + [userEmailsToReturn addObject:emailAddress]; + } + } + } + return userEmailsToReturn; +} + +- (NSArray *)userEmailAddresses +{ + NSMutableArray *emailsToReturn = nil; + NSString *gitEmail = userEmailFromGit; + if (gitEmail != nil) { + if (emailsToReturn == nil) { + emailsToReturn = [NSMutableArray array]; + } + [emailsToReturn addObject:gitEmail]; + } + + NSArray *addressBookEmails = [self userEmailsFromAddressBook]; + if ([addressBookEmails count] > 0) + { + for (NSString *emailFromAddressBook in addressBookEmails) { + if (emailsToReturn == nil) { + emailsToReturn = [NSMutableArray array]; + } + if ([emailsToReturn containsObject:emailFromAddressBook] == NO) { + [emailsToReturn addObject:emailFromAddressBook]; + } + } + } + return emailsToReturn; +} + + // --- repository callbacks --- - (void) commitsReceived: (NSArray *) commits inRepository: (Repository *) repository { BOOL hasNotificationLimit = [GitifierDefaults boolForKey: NotificationLimitEnabledKey]; NSInteger notificationLimit = [GitifierDefaults integerForKey: NotificationLimitValueKey]; - NSArray *relevantCommits = [Commit chooseRelevantCommits: commits forUser: userEmail]; + NSArray *relevantCommits = [Commit chooseRelevantCommits: commits forUserEmails: self.userEmailAddresses]; NSArray *displayedCommits, *remainingCommits; if (hasNotificationLimit && relevantCommits.count > notificationLimit) { diff --git a/Classes/NotificationsPanelController.h b/Classes/NotificationsPanelController.h index 47cac80..183b208 100644 --- a/Classes/NotificationsPanelController.h +++ b/Classes/NotificationsPanelController.h @@ -16,11 +16,7 @@ @property IBOutlet NSButton *ignoreOwnEmailsField; @property IBOutlet NSView *growlInfoPanel; -// public - (IBAction) getGrowlButtonPressed: (id) sender; - (void) updateGrowlInfoPanel; -// private -- (void) updateUserEmailText: (NSString *) email; - @end diff --git a/Classes/NotificationsPanelController.m b/Classes/NotificationsPanelController.m index 6a2b747..7116899 100644 --- a/Classes/NotificationsPanelController.m +++ b/Classes/NotificationsPanelController.m @@ -13,6 +13,10 @@ static NSString *IgnoreMyCommitsText = @"Ignore my own commits"; static NSString *GrowlAppStoreURL = @"macappstore://itunes.apple.com/us/app/growl/id467939042"; +@interface NotificationsPanelController () +- (void)updateUserEmailText:(NSArray *)emailAddresses; +@end + @implementation NotificationsPanelController @synthesize growlInfoPanel, ignoreOwnEmailsField; @@ -21,7 +25,7 @@ - (id) init { } - (void) awakeFromNib { - [self updateUserEmailText: [[NSApp delegate] userEmail]]; + [self updateUserEmailText:[[NSApp delegate] userEmailAddresses]]; PSObserve(nil, UserEmailChangedNotification, userEmailChanged:); } @@ -38,13 +42,13 @@ - (NSString *) toolbarItemLabel { } - (void) userEmailChanged: (NSNotification *) notification { - NSString *email = [notification.userInfo objectForKey: @"email"]; - [self updateUserEmailText: email]; + NSArray *userEmailAddresses = [notification.userInfo objectForKey:@"emails"]; + [self updateUserEmailText:userEmailAddresses]; } -- (void) updateUserEmailText: (NSString *) email { - if (email) { - NSString *title = PSFormat(@"%@ (%@)", IgnoreMyCommitsText, email); +- (void) updateUserEmailText:(NSArray *)emailAddresses { + if ([emailAddresses count] > 0) { + NSString *title = PSFormat(@"%@ (%@)", IgnoreMyCommitsText, [emailAddresses componentsJoinedByString:@","]); NSInteger labelLength = IgnoreMyCommitsText.length; NSRange labelRange = NSMakeRange(0, labelLength); NSRange emailRange = NSMakeRange(labelLength, title.length - labelLength); @@ -57,6 +61,12 @@ - (void) updateUserEmailText: (NSString *) email { [text addAttribute: NSFontAttributeName value: standardFont range: labelRange]; [text addAttribute: NSFontAttributeName value: smallerFont range: emailRange]; [text addAttribute: NSForegroundColorAttributeName value: gray range: emailRange]; + + NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease]; + [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail]; + + [text addAttribute: NSParagraphStyleAttributeName value:paragraphStyle range: NSMakeRange(0, [text length])]; + ignoreOwnEmailsField.attributedTitle = text; } else { ignoreOwnEmailsField.title = IgnoreMyCommitsText; diff --git a/Gitifier.xcodeproj/project.pbxproj b/Gitifier.xcodeproj/project.pbxproj index 2e48940..091a345 100644 --- a/Gitifier.xcodeproj/project.pbxproj +++ b/Gitifier.xcodeproj/project.pbxproj @@ -76,6 +76,7 @@ 8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; }; 8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; }; 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; }; + D46B06921595581A00E42A1F /* AddressBook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D46B06911595581A00E42A1F /* AddressBook.framework */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -222,6 +223,7 @@ 58E81DC3128B2B7000062F15 /* LinkLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LinkLabel.m; sourceTree = ""; }; 8D1107310486CEB800E47090 /* Gitifier-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Gitifier-Info.plist"; sourceTree = ""; }; 8D1107320486CEB800E47090 /* Gitifier.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Gitifier.app; sourceTree = BUILT_PRODUCTS_DIR; }; + D46B06911595581A00E42A1F /* AddressBook.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AddressBook.framework; path = System/Library/Frameworks/AddressBook.framework; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -237,6 +239,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + D46B06921595581A00E42A1F /* AddressBook.framework in Frameworks */, 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */, 58DEC0591242B6EB00DE7C7E /* libicucore.dylib in Frameworks */, 58DEC0C61242C16600DE7C7E /* Growl.framework in Frameworks */, @@ -251,6 +254,7 @@ 1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */ = { isa = PBXGroup; children = ( + D46B06911595581A00E42A1F /* AddressBook.framework */, 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */, ); name = "Linked Frameworks"; @@ -286,8 +290,10 @@ 29B97323FDCFA39411CA2CEA /* Frameworks */, 19C28FACFE9D520D11CA2CBB /* Products */, ); + indentWidth = 2; name = Gitifier; sourceTree = ""; + tabWidth = 4; }; 29B97315FDCFA39411CA2CEA /* Other Sources */ = { isa = PBXGroup; diff --git a/NotificationsPreferencesPanel.xib b/NotificationsPreferencesPanel.xib index 6c0d971..b21ccff 100644 --- a/NotificationsPreferencesPanel.xib +++ b/NotificationsPreferencesPanel.xib @@ -2,13 +2,13 @@ 1070 - 11C74 - 1938 - 1138.23 - 567.00 + 11E53 + 2182 + 1138.47 + 569.00 com.apple.InterfaceBuilder.CocoaPlugin - 1938 + 2182 YES @@ -59,6 +59,7 @@ {{0, 18}, {485, 5}} + {0, 0} 67239424 @@ -265,7 +266,7 @@ nilSymbol positiveInfinitySymbol - + YES @@ -358,6 +359,7 @@ {{163, 18}, {196, 18}} + YES -2080244224 @@ -450,8 +452,8 @@ YES - -2080244224 - 0 + -2080244160 + 2304 Ignore my own commits @@ -493,7 +495,7 @@ {525, 327} - + NSView @@ -983,7 +985,7 @@ 65.IBPluginDependency 85.IBPluginDependency - + YES com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -1061,7 +1063,7 @@ growlInfoPanel ignoreOwnEmailsField - + YES NSView NSButton @@ -1074,7 +1076,7 @@ growlInfoPanel ignoreOwnEmailsField - + YES growlInfoPanel @@ -1108,7 +1110,7 @@ NSSwitch growl_icon - + YES {15, 15} {64, 64}