William Bowling is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

wbowling / adium (fork of adium / adium)

Fork of Adium for patches/improvements

Clone this repository (size: 338.7 MB): HTTPS / SSH
hg clone https://bitbucket.org/wbowling/adium
hg clone ssh://hg@bitbucket.org/wbowling/adium

adium / Plugins / Purple Service / PurpleFacebookAccount.m

//
//  PurpleFacebookAccount.m
//  Adium
//
//  Created by Evan Schoenberg on 1/15/09.
//  Copyright 2009 Adium X. All rights reserved.
//

#import "PurpleFacebookAccount.h"
#import <Adium/AIHTMLDecoder.h>
#import <Adium/AIListContact.h>
#import <Adium/AIStatus.h>

#import "PurpleFacebookService.h"
#import <AIUtilities/AIMenuAdditions.h>
#import <Adium/AIMetaContact.h>

@implementation PurpleFacebookAccount

- (const char*)protocolPlugin
{
    return "prpl-bigbrownchunx-facebookim";
}

- (NSString *)webProfileStringForContact:(AIListContact *)contact
{
        return [NSString stringWithFormat:AILocalizedString(@"View %@'s Facebook profile", nil), 
                        contact.displayName];
}

- (void)configurePurpleAccount
{
        [super configurePurpleAccount];
        
        /* We could add a pref for this, but not without some enhancements to mail notifications. Currently, this being
         * enabled means ugly nasty "You have new mail!" popups continuously, since that's how 'notifications' are passed
         * to us.
         */
        purple_account_set_bool(account, "facebook_get_notifications", FALSE);
}

- (NSString *)host
{
        return @"login.facebook.com";
}

- (const char *)purpleStatusIDForStatus:(AIStatus *)statusState
                                                          arguments:(NSMutableDictionary *)arguments
{
        if (statusState.statusType == AIOfflineStatusType) {
                return "offline";
        } else {
                return "available";
        }
}

- (void)setSocialNetworkingStatusMessage:(NSAttributedString *)statusMessage
{
        NSMutableDictionary *arguments = [NSMutableDictionary dictionary];
        NSString *encodedStatusMessage = (statusMessage ? 
                                                                          [self encodedAttributedString:statusMessage
                                                                                                         forStatusState:nil] :
                                                                          nil);
        if (encodedStatusMessage) {
                [arguments setObject:encodedStatusMessage
                                          forKey:@"message"];
        }

        purple_account_set_bool(account, "facebook_set_status_through_pidgin", TRUE);
        [self setStatusState:nil
                                statusID:"available" /* facebook only supports available */
                                isActive:[NSNumber numberWithBool:YES]
                           arguments:arguments];
        purple_account_set_bool(account, "facebook_set_status_through_pidgin", FALSE);
}

- (NSString *)encodedAttributedString:(NSAttributedString *)inAttributedString forListObject:(AIListObject *)inListObject
{
        return [AIHTMLDecoder encodeHTML:inAttributedString
                                                         headers:YES
                                                        fontTags:YES
                                  includingColorTags:YES
                                           closeFontTags:YES
                                                   styleTags:YES
                  closeStyleTagsOnFontChange:YES
                                          encodeNonASCII:NO
                                                encodeSpaces:NO
                                                  imagesPath:nil
                                   attachmentsAsText:YES
                   onlyIncludeOutgoingImages:NO
                                          simpleTagsOnly:NO
                                          bodyBackground:NO
                                 allowJavascriptURLs:YES];
}

/*!
 * @brief Set an alias for a contact
 *
 * Normally, we consider the name a 'serverside alias' unless it matches the UID's characters
 * However, the UID in facebook should never be presented to the user if possible; it's for internal use
 * only.  We'll therefore consider any alias a formatted UID such that it will replace the UID when displayed
 * in Adium.
 */
- (void)updateContact:(AIListContact *)theContact toAlias:(NSString *)purpleAlias
{
        if (![purpleAlias isEqualToString:theContact.formattedUID] && 
                ![purpleAlias isEqualToString:theContact.UID]) {
                [theContact setFormattedUID:purpleAlias
                                                         notify:NotifyLater];
                
                //Apply any changes
                [theContact notifyOfChangedPropertiesSilently:silentAndDelayed];
        }
}

#pragma mark Menu Items
/*!
 * @brief Menu items for contact
 *
 * Returns an array of menu items for a contact on this account.  This is the best place to add protocol-specific
 * actions that aren't otherwise supported by Adium.
 * @param inContact AIListContact for menu items
 * @return NSArray of NSMenuItem instances for the passed contact
 */
- (NSArray *)menuItemsForContact:(AIListContact *)inContact
{
        NSMutableArray                  *menuItemArray = [NSMutableArray array];
        
        //Fill with any items supplied by libpurple
        [menuItemArray addObjectsFromArray:[super menuItemsForContact:inContact]];
        
        
        //Add a new menu item with the facebook icon to view the contacts profile
        NSMenuItem *menuItem;
        NSImage *serviceIcon = [AIServiceIcons serviceIconForService:self.service
                                                                                                                        type:AIServiceIconSmall
                                                                                                           direction:AIIconNormal];
        
        menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:[NSString stringWithFormat:AILocalizedString(@"View %@'s Facebook profile", nil), inContact.displayName]
                                                                                                                                         target:self
                                                                                                                                         action:@selector(viewFacebookProfile:)
                                                                                                                          keyEquivalent:@""] autorelease];
        [menuItem setImage:serviceIcon];
        [menuItem setRepresentedObject:inContact];
        [menuItemArray addObject:menuItem];     
        
        
        //Don't return an empty array
        if (![menuItemArray count]) menuItemArray = nil;
        
        return menuItemArray;   
}


/*!
 * @brief Open Facebook Profile
 *
 * Opens the selected contacts or chats facebook profile in the default web browser
 */
- (void)viewFacebookProfile:(NSMenuItem *)menuItem
{       
        AIListContact *contact = (AIListContact *)menuItem.representedObject;
        NSString *url = [NSString stringWithFormat:@"http://www.facebook.com/profile.php?id=%@",[contact UID]];
        [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
}




@end