Skip to content
Tom von Schwerdtner edited this page Nov 16, 2015 · 11 revisions

The AMPlugin can override these methods to perform various activities

Unique calls are, as the word says, called just one time,  normally before the stack calls
if more than a plugin implement this kind of call just one plugin will be called
Stack calls are called on all plugins that implement this kind of call in the plug-in table order

Composer Calls

/**
 *  Get the html from a message to render in the composer
 *  Unique methods are called only one time from AM, if more than one plugin
 *  support Unique methods only one of them (randomly) will be used
 *
 *  @param info the message to render
 *
 *  @return the html to render
 */

- (NSString*)  ampUniqueComposerRenderHtml:(AMPComposerInfo*)info; 

Before other renders this call permits to modify the html that the composer webview will show, the input is a AMPComposerInfo that contains the infos about the current composer state (AMPCustomTemplates)

- (NSString*)  ampUniqueComposerRenderHtml:(AMPComposerInfo *)info
{
    return @"my start html";
}
/**
 *  Get the html for the new created composer
 *
 *  @param html the html to process
 *
 *  @return the html to render
 */
- (NSString*)  ampStackComposerRenderHtmlFromHtml:(NSString*)html composerInfo:(AMPComposerInfo*)info;

After other renders this call permits to modify the html that the composer webview will show, this call has in input the html pre-rendered by Airmail and from the ampUniqueComposerRenderHtml (AMPCustomTemplates)

- (NSString*)  ampStackComposerRenderHtmlFromHtml:(NSString *)html composerInfo:(AMPComposerInfo *)info
{
    if(self.cssString.length == 0)
        return html;
    
    NSString *htmlRet = [NSString stringWithFormat:@"<style id=\"%@\">%@</style>%@",  
    										ampcss_composer_css_option,self.cssString,html];
    return htmlRet;
}
/**
 *  The menu item that AM will add in the composer
 *
 *  @return the menu item
 */
- (NSMenuItem*) ampMenuComposerItem:(AMPComposerInfo*)info;

This call will show the menu item to add the plugins options to the composer window (AMPCustomTemplates)

-(NSMenuItem*) ampMenuComposerItem:(AMPComposerInfo *)info
{
    NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Change Style", @"Change Style") action:nil keyEquivalent:@""];
    [item setState:NSOffState];
    
    NSMenu *menu = [self stylesMenuComposerButton];
    [item setSubmenu:menu];
    return item;
}

Detail View

/**
 *  Get the html from a message to render in the bodyview
 *  Unique methods are called only one time from AM, if more than one plugin
 *  support Unique methods only one of them (randomly) will be used
 *
 *  @param message the message to render
 *
 *  @return the html to render
 */
- (NSString*) ampUniqueMessageRender:(AMPMessage*)message;

Before other renders this call permits to modify the html that the Message Detail webview will show (AMPMessageStyle)

/**
 *  Get the html from the html that AM create from a message, to render in the
 *  bodyview. Stack methods can be queued, so AM will call all the plugins with 
 *  a stack method in the a random order.
 *
 *  @param html the html to process
 *
 *  @return the html to render
 */
- (NSString*) ampStackMessageRenderFromHtml:(NSString*)html message:(AMPMessage*)message;

After other renders this call permits to modify the html that the Message Detail webview will show (AMPMessageStyle)

- (NSString*) ampStackMessageRenderFromHtml:(NSString*)html message:(AMPMessage*)message
{
    if(self.cssString.length == 0 || !message)
        return html;
    
    NSString *htmlRet = [NSString stringWithFormat:@"<style>%@</style>%@",self.cssString,html];
    return htmlRet;
}

Notify

/**
 *  Called after the standard AM notify. Is called for each message that AM will notify in the notification center
 *
 *  @param message the message to notify
 *
 *  @return a number with a boolean YES/NO
 */
- (NSNumber*) ampQueueNotify:(AMPMessage*)message;

This call permits to perform an action on the Airmail notify event (AMPSimpleNotifier)

Rule

/**
 *  Called as rule action
 *
 *  @param message the message filtered by the rule condition
 *
 *  @return the Number for a bool if the rule is applied to the message
 */
- (NSNumber*) ampRuleActionItem:(AMPMessage*)message;

This call permits to perform an action on the action of a rule (AMPTransalte,AMPSimpleNotifier)

Examples

- (NSNumber*) ampQueueNotify:(AMPMessage*)message
{
    return @([self CallNotifier:message]);
}

- (NSNumber*) ampRuleActionItem:(AMPMessage*)message
{
    return @([self CallNotifier:message]);
}

- (BOOL) CallNotifier:(AMPMessage*)message
{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if(self.sound)
            [self.sound play];
        
        AMPNotifyWindowController *con = [[AMPNotifyWindowController alloc] initWithRect:NSMakeRect(0,0, 400, 200)];
        
        NSScreen *screen    = [NSScreen mainScreen];
        NSRect screenRect   = [screen visibleFrame];
        NSRect frame        = NSZeroRect;
        frame.origin.x      = screenRect.size.width  - con.window.frame.size.width-20;
        frame.origin.y      = screenRect.size.height - con.window.frame.size.height;
        frame.size.width    = con.window.frame.size.width;
        frame.size.height   = con.window.frame.size.height;
        
        [con.window setDelegate:self];
        [con.window setFrame:frame display:NO];
        [con Render:message];
        [con showWindow:con];
        [self.windows addObject:con];
    });
    return YES;
}

Menu Action

/**
 *  The menu item that AM will add to the message menu
 *
 *  @param messages the selected messages when the item is activated
 *
 *  @return the menu item
 */
- (NSMenuItem*) ampMenuActionItem:(NSArray*)messages;

This call permits to perform an action on the context menu of one or more message (AMPTransalte)

- (NSMenuItem*) ampMenuActionItem:(NSArray*)messages
{
    NSMenuItem *itemNotes = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Translate", @"Translate") action:nil keyEquivalent:@""];
    [itemNotes setRepresentedObject:@""];
    [itemNotes setAction:@selector(translateAction:)];
    [itemNotes setTarget:self];
    return itemNotes;
}

- (NSNumber*) ampRuleActionItem:(AMPMessage*)message
{
    NSString *s = @"http://translate.google.com/?sl=auto#auto/";
    if(self.lang.length > 0)
        s = [s stringByAppendingFormat:@"%@/",self.lang];
    else
        s = [s stringByAppendingFormat:@"destination_language/"];

    NSString *sbody = [s stringByAppendingFormat:@"%@",[self urlencode:[message.plainBody stringByConvertingHTMLToPlainText]]];
    NSURL *url = [NSURL URLWithString:sbody];
    if(url)
        [[NSWorkspace sharedWorkspace] openURL:url];
    return @(YES);
}