Created
January 6, 2017 18:56
-
-
Save butaji/1b27d68615740a367c6ff2b0f0af28f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ZeroInboxToolbar.m | |
// GMailinator | |
// | |
// Created by Vitaly Baum on 1/5/17. | |
// Copyright © 2017 nompute. All rights reserved. | |
// | |
#import "ZeroInboxToolbar.h" | |
#import "ZeroInboxMessageViewer.h" | |
#import "Swizzler.h" | |
#import "AlertHelper.h" | |
#import "SnoozeDatePickerController.h" | |
#import "NSObjectInspect.h" | |
#import "PrivateHeaders/MFAccount.h" | |
#import "PrivateHeaders/Message.h" | |
#import "PrivateHeaders/MessageViewer.h" | |
#import <objc/runtime.h> | |
@implementation ZeroInboxToolbar | |
+(void)load { | |
Class clazz = NSClassFromString(@"NSToolbar"); | |
if (clazz) { | |
[Swizzler extendClass:@"NSToolbar" | |
withClass:@"ZeroInboxToolbar"]; | |
} | |
} | |
MessageViewer* viewer; | |
SnoozeDatePickerController *controllerWindow; | |
-(void)_setDelegate:(id) delegate { | |
if ([delegate isKindOfClass:NSClassFromString(@"MessageViewer")] && !viewer) { | |
[self initSnoozeMenu:@selector(snoozeEmail:)]; | |
//Avoid having to do this more than once! | |
viewer = delegate; | |
} | |
[self _setDelegate:delegate]; //call swizzled method | |
} | |
- (NSMenuItem *) newMenuItemWithTitle:(NSString *)title action:(SEL)action andKeyEquivalent:(NSString *)keyEquivalent inMenu:(NSMenu *)menu relativeToItemWithSelector:(SEL)selector offset:(int)offset | |
// Taken from /System/Developer/Examples/EnterpriseObjects/AppKit/ModelerBundle/EOUtil.m | |
{ | |
// Simple utility category which adds a new menu item with title, action | |
// and keyEquivalent to menu (or one of its submenus) under that item with | |
// selector as its action. Returns the new addition or nil if no such | |
// item could be found. | |
NSMenuItem *menuItem; | |
NSArray *items = menu.itemArray; | |
int iI; | |
if(!keyEquivalent) | |
keyEquivalent = @""; | |
for(iI = 0; iI < items.count; iI++){ | |
menuItem = items[iI]; | |
if(menuItem.action == selector) | |
return ([menu insertItemWithTitle:title action:action keyEquivalent:keyEquivalent atIndex:iI + offset]); | |
else if([menuItem.target isKindOfClass:[NSMenu class]]){ | |
menuItem = [self newMenuItemWithTitle:title action:action andKeyEquivalent:keyEquivalent inMenu:menuItem.target relativeToItemWithSelector:selector offset:offset]; | |
if(menuItem) | |
return menuItem; | |
} | |
} | |
return nil; | |
} | |
- (BOOL) validateMenuItem:(NSMenuItem *)theItem | |
{ | |
//do we need to show this button | |
return YES; | |
} | |
- (IBAction) snoozeEmail: sender { | |
NSArray *messages = [viewer selectedMessages]; | |
for (Message* msg in messages) | |
{ | |
MFMailAccount *account = [msg account]; | |
NSArray *mailboxes = [account allMailboxes]; | |
for (MFMailbox *mailbox in mailboxes) { | |
if ([mailbox.name isEqualToString:@"Scheduled"]) { | |
int unsigned numMethods; | |
NSMutableString *res = [NSMutableString new]; | |
Method *methods = class_copyMethodList(objc_getMetaClass("MessageViewer"), &numMethods); | |
for (int i = 0; i < numMethods; i++) { | |
[res appendString:NSStringFromSelector(method_getName(methods[i]))]; | |
[res appendString:@"\r\n"]; | |
} | |
[res copy]; | |
// [viewer _transferMessages:[NSArray arrayWithObject:msg] toMailbox:mailbox deleteOriginals:YES allowUndo:YES isDeleteOperation:NO]; | |
} | |
} | |
// if (mailbox != nil && [msg mailbox] != mailbox) | |
// { | |
// } | |
} | |
// controllerWindow = [[SnoozeDatePickerController alloc] initWithWindowNibName:@"SnoozeDatePickerController"]; | |
// [controllerWindow showWindow:self]; | |
} | |
- (void) initSnoozeMenu:(SEL)sel{ | |
NSMenu* mainMenu = [NSApplication sharedApplication].mainMenu; | |
NSMenuItem* firstMenuItem = | |
[self newMenuItemWithTitle:@"Snooze" | |
action:sel | |
andKeyEquivalent:@"d" | |
inMenu:mainMenu | |
relativeToItemWithSelector:@selector(archiveMessages:) | |
offset:1]; | |
if(!firstMenuItem) { | |
[AlertHelper show:@"Mail.app: unable to add submenu <Snooze>"]; | |
} | |
else { | |
firstMenuItem.target = self; | |
firstMenuItem.keyEquivalentModifierMask = NSCommandKeyMask; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment