Skip to content

Commit

Permalink
actions are categorized and sorted, hopefully I didn't do that too wr…
Browse files Browse the repository at this point in the history
…ong...
  • Loading branch information
dustinrue committed Mar 23, 2012
1 parent e1e4fbc commit 8af7926
Show file tree
Hide file tree
Showing 38 changed files with 290 additions and 18 deletions.
6 changes: 6 additions & 0 deletions ControlPlane.xcodeproj/project.pbxproj
Expand Up @@ -81,6 +81,7 @@
8DFA46170CC33E46003EAC16 /* QuitApplicationAction.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DFA46150CC33E46003EAC16 /* QuitApplicationAction.m */; };
8DFD0DA60CEBBC8A00E79049 /* UnmountAction.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DFD0DA40CEBBC8A00E79049 /* UnmountAction.m */; };
DA16392613D4B14A00E3F421 /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = DA16392513D4B14A00E3F421 /* dsa_pub.pem */; };
DA41954D151C17F000FF7BA0 /* ActionSubmenuItem.m in Sources */ = {isa = PBXBuildFile; fileRef = DA41954C151C17F000FF7BA0 /* ActionSubmenuItem.m */; };
DA4411A514EE03C9005E7B3B /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA4411A414EE03C9005E7B3B /* MapKit.framework */; };
DA461C0C14C33E3C0019594A /* NSString+ShellScriptHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = DA461C0B14C33E3B0019594A /* NSString+ShellScriptHelper.m */; };
DA49AF6C14C3E0AB00D03D6A /* TogglePrinterSharingAction.m in Sources */ = {isa = PBXBuildFile; fileRef = DA49AF6B14C3E0AB00D03D6A /* TogglePrinterSharingAction.m */; };
Expand Down Expand Up @@ -362,6 +363,8 @@
DA146F7E143F3C4000906DBF /* da-DK */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = "da-DK"; path = "da-DK.lproj/CoreLocationRule.xib"; sourceTree = "<group>"; };
DA146F7F143F3C4000906DBF /* de */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = de; path = de.lproj/CoreLocationRule.xib; sourceTree = "<group>"; };
DA16392513D4B14A00E3F421 /* dsa_pub.pem */ = {isa = PBXFileReference; lastKnownFileType = text; name = dsa_pub.pem; path = Resources/dsa_pub.pem; sourceTree = "<group>"; };
DA41954B151C17EF00FF7BA0 /* ActionSubmenuItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ActionSubmenuItem.h; path = Source/ActionSubmenuItem.h; sourceTree = "<group>"; };
DA41954C151C17F000FF7BA0 /* ActionSubmenuItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ActionSubmenuItem.m; path = Source/ActionSubmenuItem.m; sourceTree = "<group>"; };
DA4411A414EE03C9005E7B3B /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = Frameworks/MapKit.framework; sourceTree = "<group>"; };
DA461C0A14C33E3B0019594A /* NSString+ShellScriptHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSString+ShellScriptHelper.h"; path = "Source/NSString+ShellScriptHelper.h"; sourceTree = "<group>"; };
DA461C0B14C33E3B0019594A /* NSString+ShellScriptHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSString+ShellScriptHelper.m"; path = "Source/NSString+ShellScriptHelper.m"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -764,6 +767,8 @@
8DF5A4160BC2353D00CBEB4B /* Action.m */,
DDAEFFF91414714F00FDADE8 /* Action+HelperTool.h */,
DDAEFFFA1414714F00FDADE8 /* Action+HelperTool.m */,
DA41954B151C17EF00FF7BA0 /* ActionSubmenuItem.h */,
DA41954C151C17F000FF7BA0 /* ActionSubmenuItem.m */,
8D71CAEC0C17BA7900E56526 /* ToggleableAction.h */,
8D71CAED0C17BA7900E56526 /* ToggleableAction.m */,
);
Expand Down Expand Up @@ -1234,6 +1239,7 @@
DAC45F2014CDEC170007F5EF /* TimeMachineDestinationAction.m in Sources */,
DA72BFF615019D8C003072C6 /* CPBonjourResolver.m in Sources */,
DA571B71151587B80074CE22 /* ToggleFileSharingAction.m in Sources */,
DA41954D151C17F000FF7BA0 /* ActionSubmenuItem.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 2 additions & 0 deletions Source/Action.h
Expand Up @@ -36,6 +36,7 @@
+ (NSString *)helpText;
+ (NSString *)creationHelpText;
+ (NSString *)friendlyName;
+ (NSString *)menuCategory;

// Helpers
- (BOOL)executeAppleScript:(NSString *)script; // returns YES on success, NO on failure
Expand All @@ -61,6 +62,7 @@
@interface ActionSetController : NSObject {
IBOutlet NSWindowController *prefsWindowController;
NSArray *classes; // array of class objects
NSDictionary *menuCategories;
}

- (NSArray *)types;
Expand Down
91 changes: 74 additions & 17 deletions Source/Action.m
Expand Up @@ -8,6 +8,7 @@
#import "Action.h"
#import "DSLogger.h"
#import "PrefsWindowController.h"
#import "ActionSubmenuItem.h"

@implementation Action

Expand Down Expand Up @@ -146,6 +147,10 @@ + (NSString *)friendlyName {
return @"Not implemented";
}

+ (NSString *)menuCategory {
return @"";
}

- (void)executeAppleScriptForReal:(NSString *)script
{
appleScriptResult_ = nil;
Expand Down Expand Up @@ -258,9 +263,6 @@ - (id)init
[DesktopBackgroundAction class],
[DisplayBrightnessAction class],
[DisplaySleepTimeAction class],
#ifdef DEBUG_MODE
[FirewallRuleAction class],
#endif
[IChatAction class],
[ITunesPlaylistAction class],
[LockKeychainAction class],
Expand Down Expand Up @@ -297,9 +299,6 @@ - (id)init
NSLocalizedString(@"DefaultPrinter", @"Action type");
NSLocalizedString(@"DesktopBackground", @"Action type");
NSLocalizedString(@"DisplayBrightness", @"Action type");
#ifdef DEBUG_MODE
NSLocalizedString(@"FirewallRule", @"Action type");
#endif
NSLocalizedString(@"iChat", @"Action type");
NSLocalizedString(@"iTunesPlaylist", @"Action type");
NSLocalizedString(@"LockKeychain", @"Action type");
Expand Down Expand Up @@ -335,15 +334,53 @@ - (id)init
[tmp removeObject:[DisplayBrightnessAction class]];
classes = tmp;
}


// build a list of menu categories
NSMutableDictionary *menuCategoryBuilder = [NSMutableDictionary dictionary];
NSMutableDictionary *tmpDict = nil;
ActionSubmenuItem *tmp = nil;



for (id currentClass in classes) {

// if the object exists then we've seen this category before
// and we simply want to add the class to the object we just found
if ([menuCategoryBuilder objectForKey:[currentClass menuCategory]]) {
tmp = [menuCategoryBuilder objectForKey:[currentClass menuCategory]];
tmpDict = [NSMutableDictionary dictionaryWithCapacity:3];
[tmpDict setObject:currentClass forKey:@"class"];

[tmpDict setObject:[currentClass class] forKey:@"representedObject"];
[tmp setTarget:prefsWindowController];
[tmp addObject:tmpDict];

}
else {
tmp = [[ActionSubmenuItem alloc] init];
tmpDict = [NSMutableDictionary dictionaryWithCapacity:3];
[tmpDict setObject:currentClass forKey:@"class"];
//[tmpDict setObject:prefsWindowController forKey:@"target"];
[tmpDict setObject:[currentClass class] forKey:@"representedObject"];
[tmp setTarget:prefsWindowController];
[tmp addObject:tmpDict];

[menuCategoryBuilder setObject:tmp forKey:[currentClass menuCategory]];

[tmp release];
}
}

NSLog(@"looks like %@", menuCategoryBuilder);

menuCategories = [menuCategoryBuilder copy];
return self;
}

- (void)dealloc
{
[classes release];

[menuCategories release];
[super dealloc];
}

Expand All @@ -362,18 +399,38 @@ - (NSArray *)types

- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(int)index shouldCancel:(BOOL)shouldCancel
{
Class klass = [classes objectAtIndex:index];
//Class klass = [classes objectAtIndex:index];


NSArray *menuCategoryList = [menuCategories allKeys];
menuCategoryList = [menuCategoryList sortedArrayUsingSelector:@selector(compare:)];

NSString *friendlyName = [[classes objectAtIndex:index] friendlyName];
//NSString *localisedType = NSLocalizedString(type, @"Action type");
NSString *categoryName = [menuCategoryList objectAtIndex:index];
//NSString *localisedType = NSLocalizedString(type, @"Action type");

NSMenu *newSubMenu = [[[NSMenu alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"%@ Actions", @""), friendlyName]] retain];

[newSubMenu setDelegate:[menuCategories objectForKey:categoryName]];


//NSString *title = [NSString stringWithFormat:NSLocalizedString(@"'%@' Actions...", @"Menu item"),
//friendlyName];
//[item setTitle:title];

[item setSubmenu:newSubMenu];
[item setTitle:[NSString stringWithFormat:NSLocalizedString(@"%@ Actions", @""), categoryName]];
[item setRepresentedObject:[classes objectAtIndex:index]];




NSString *title = [NSString stringWithFormat:NSLocalizedString(@"Add '%@' Action...", @"Menu item"),
friendlyName];
[item setTitle:title];
//DSLog(@"menu category %@", [[classes objectAtIndex:index] menuCategory]);


[item setTarget:prefsWindowController];
[item setAction:@selector(addAction:)];
[item setRepresentedObject:klass];
//[item setTarget:prefsWindowController];
//[item setAction:@selector(addAction:)];
//[item setRepresentedObject:klass];

return YES;
}
Expand All @@ -386,7 +443,7 @@ - (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id

- (NSUInteger)numberOfItemsInMenu:(NSMenu *)menu
{
return [classes count];
return [menuCategories count];
}

@end
23 changes: 23 additions & 0 deletions Source/ActionSubmenuItem.h
@@ -0,0 +1,23 @@
//
// ActionSubmenuItem.h
// ControlPlane
//
// Created by Dustin Rue on 3/22/12.
// Copyright (c) 2012. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ActionSubmenuItem : NSObject <NSMenuDelegate> {
NSMutableArray *items;
id target;
id representedObject;
}

@property (readwrite, retain) NSMutableArray *items;
@property (readwrite, assign) id target;
@property (readwrite, assign) id representedObject;

- (void) addObject:(id) object;

@end
53 changes: 53 additions & 0 deletions Source/ActionSubmenuItem.m
@@ -0,0 +1,53 @@
//
// ActionSubmenuItem.m
// ControlPlane
//
// Created by Dustin Rue on 3/22/12.
// Copyright (c) 2012. All rights reserved.
//

#import "ActionSubmenuItem.h"
#import "Action.h"

@implementation ActionSubmenuItem

@synthesize items;
@synthesize target;
@synthesize representedObject;

- (id) init {
self = [super init];

if (self) {
items = [[NSMutableArray alloc] init];
}

return self;
}

- (void) dealloc {
[items release];

[super dealloc];
}
- (void) addObject:(id) object {
[items addObject:object];
}
- (BOOL) menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel {

SEL addAction = NSSelectorFromString(@"addAction:");

[item setTitle:[[[items objectAtIndex:index] objectForKey:@"class"] friendlyName]];
[item setTarget:target];
[item setRepresentedObject:[[items objectAtIndex:index] objectForKey:@"representedObject"]];
[item setAction:addAction];

return YES;
}

- (NSInteger) numberOfItemsInMenu:(NSMenu *)menu {
return [items count];
}


@end
4 changes: 4 additions & 0 deletions Source/DefaultBrowserAction.m
Expand Up @@ -116,4 +116,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Default Browser", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Web", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/DefaultPrinterAction.m
Expand Up @@ -111,4 +111,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Default Printer", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"System Preferences", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/DesktopBackgroundAction.m
Expand Up @@ -92,4 +92,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Desktop Background", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"System Preferences", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/DisplayBrightnessAction.m
Expand Up @@ -165,4 +165,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Display Brightness", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"System Preferences", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/DisplaySleepTimeAction.m
Expand Up @@ -130,4 +130,8 @@ - (id)initWithOption:(NSString *)option
+ (NSString *) friendlyName {
return NSLocalizedString(@"Display Sleep Time", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"System Preferences", @"");
}
@end
4 changes: 4 additions & 0 deletions Source/IChatAction.m
Expand Up @@ -83,4 +83,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"iChat Status", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Applications", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/ITunesPlaylistAction.m
Expand Up @@ -118,4 +118,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Play iTunes Playlist", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Sound and Music", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/LockKeychainAction.m
Expand Up @@ -60,4 +60,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Lock Keychain", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Keychain", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/MailIMAPServerAction.m
Expand Up @@ -86,4 +86,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Change Mail IMAP Server", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Mail", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/MailIntervalAction.m
Expand Up @@ -131,4 +131,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Change New Mail Check Interval", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Mail", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/MailSMTPServerAction.m
Expand Up @@ -119,4 +119,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Change Mail SMTP Server", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Mail", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/MountAction.m
Expand Up @@ -84,4 +84,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Mount", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Finder", @"");
}

@end
4 changes: 4 additions & 0 deletions Source/MuteAction.m
Expand Up @@ -117,4 +117,8 @@ + (NSString *) friendlyName {
return NSLocalizedString(@"Mute", @"");
}

+ (NSString *)menuCategory {
return NSLocalizedString(@"Sound and Music", @"");
}

@end

0 comments on commit 8af7926

Please sign in to comment.