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 again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | /*
* ESiTunesPlugin.m
* Adium
*
* Created by Evan Schoenberg on 6/11/05.
* Assigned to Kiel Gillard
*
* Thanks to GrowlTunes from the Growl project for demonstrating how to receive notifications when
* the iTunes track changes.
*/
#import "ESiTunesPlugin.h"
#import <Adium/AIContentControllerProtocol.h>
#import <Adium/AIToolbarControllerProtocol.h>
#import "AIStatusController.h"
#import <Adium/AIMenuControllerProtocol.h>
#import <Adium/AIAccount.h>
#import <AIUtilities/AIAttributedStringAdditions.h>
#import <AIUtilities/AIToolbarUtilities.h>
#import <AIUtilities/AIImageAdditions.h>
#import <AIUtilities/MVMenuButton.h>
#import <AIUtilities/AIMenuAdditions.h>
#import <AIUtilities/AIWindowAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import <Adium/AIHTMLDecoder.h>
#import <Adium/AIStatus.h>
#import <WebKit/WebKit.h>
#define ITUNES_MINIMUM_VERSION 4.6f
#define ITUNES_STATUS_ID -8000
#pragma mark -
#define PLAYER_STATE @"Player State"
#define KEY_PLAYING @"Playing"
#define KEY_PAUSED @"Paused"
#define KEY_STOPPED @"Stopped"
#define CURRENT_TRACK_FORMAT_KEY @"Current Track Format"
#define ITMS_SEARCH_URL @"itms://itunes.com/link?"
//itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?"
#pragma mark -
#define ALBUM_TRIGGER AILocalizedString(@"%_album","Trigger for the album of the currently playing iTunes song")
#define ARTIST_TRIGGER AILocalizedString(@"%_artist","Trigger for the artist of the currently playing iTunes song")
#define COMPOSER_TRIGGER AILocalizedString(@"%_composer","Trigger for the composer of the currently playing iTunes song")
#define GENRE_TRIGGER AILocalizedString(@"%_genre","Trigger for the genre of the currently playing iTunes song")
#define STATUS_TRIGGER AILocalizedString(@"%_status","Trigger for the genre of the currently playing iTunes song")
#define TRACK_TRIGGER AILocalizedString(@"%_track","Trigger for the name of the currently playing iTunes song")
#define YEAR_TRIGGER AILocalizedString(@"%_year","Trigger for the year of the currently playing iTunes song")
#define STORE_URL_TRIGGER AILocalizedString(@"%_iTMS","Trigger for an iTunes Music Store link to the currently playing iTunes song")
#define MUSIC_TRIGGER AILocalizedString(@"%_music","Command which triggers *is listening to %_track by %_artist*")
#define CURRENT_TRACK_TRIGGER AILocalizedString(@"%_iTunes","Trigger for the song - artist of the currently playing iTunes song")
#define MUSICAL_NOTE [NSString stringWithUTF8String:"\342\231\253"]
#define CURRENT_ITUNES_TRACK [NSString stringWithFormat:@"%@ iTunes", MUSICAL_NOTE]
#define TOOLBAR_LABEL AILocalizedString(@"iTunes","Label for iTunes toolbar menu item.")
#pragma mark -
#define ITUNES_TOOLBAR_ITEM @"iTunesItem"
#define INSERT_TRIGGERS_MENU AILocalizedString(@"Insert iTunes Token", "Label used for edit and contextual menus of iTunes triggers")
#pragma mark -
@interface ESiTunesPlugin ()
- (NSMenuItem *)menuItemWithTitle:(NSString *)title action:(SEL)action representedObject:(id)representedObject kind:(KGiTunesPluginMenuItemKind)itemKind;
- (void)createiTunesCurrentTrackStatusState;
- (void)createiTunesToolbarItemWithPath:(NSString *)path;
- (void)createiTunesToolbarItemMenuItems:(NSMenu *)iTunesMenu;
- (void)createTriggersMenu;
- (void)filterAndInsertString:(NSString *)inString;
- (void)insertStringIntoMessageEntryView:(NSString *)inString;
- (void)insertAttributedStringIntoMessageEntryView:(NSAttributedString *)inString;
- (void)loadiTunesCurrentInfoViaApplescript;
@end
/*!
* @class ESiTunesPlugin
* @brief Fiiltering component to provide triggers which are replaced by information from the current iTunes track
*/
@implementation ESiTunesPlugin
#pragma mark -
#pragma mark Accessor Methods
/*!
* @brief Is iTunes stopped?
*/
- (BOOL)iTunesIsStopped
{
//Get the info if we don't already have it
if (!iTunesCurrentInfo) [self loadiTunesCurrentInfoViaApplescript];
return iTunesIsStopped;
}
/*!
* @brief Set if iTunes is stopped
*/
- (void)setiTunesIsStopped:(BOOL)yesOrNo
{
iTunesIsStopped = yesOrNo;
}
/*!
* @brief Is iTunes paused?
*/
- (BOOL)iTunesIsPaused
{
//Get the info if we don't already have it
if (!iTunesCurrentInfo) [self loadiTunesCurrentInfoViaApplescript];
return iTunesIsPaused;
}
/*!
* @brief Set if iTunes is paused
*/
- (void)setiTunesIsPaused:(BOOL)yesOrNo
{
iTunesIsPaused = yesOrNo;
}
/*!
* @brief Get current iTunes info dictionary
*/
- (NSDictionary *)iTunesCurrentInfo
{
return iTunesCurrentInfo;
}
/*!
* @brief Store local copy of iTunes information
*
* Retains new information, requests immediate content update and lets the plugin know what iTunes is doing.
*/
- (void)setiTunesCurrentInfo:(NSDictionary *)newInfo
{
if (newInfo != iTunesCurrentInfo) {
[iTunesCurrentInfo release];
NSMutableDictionary *mutableNewInfo = [newInfo mutableCopy];
//If we get a stream title, use that as the track name
if ([mutableNewInfo objectForKey:ITUNES_STREAM_TITLE] && [(NSString *)[mutableNewInfo objectForKey:ITUNES_STREAM_TITLE] length])
[mutableNewInfo setObject:[mutableNewInfo objectForKey:ITUNES_STREAM_TITLE]
forKey:ITUNES_NAME];
NSEnumerator *enumerator = [newInfo keyEnumerator];
NSString *key;
while ((key = [enumerator nextObject])) {
//Some versions of iTunes may send numbers as numbers rather than strings. Change these to numbers for our use.
id value = [newInfo objectForKey:key];
if (![value isKindOfClass:[NSString class]]) {
if ([value respondsToSelector:@selector(stringValue)]) {
[mutableNewInfo setObject:[value stringValue]
forKey:key];
} else {
//A future version might send some other data entirely. Drop it rather than having non-strings in the dict.
[mutableNewInfo removeObjectForKey:key];
}
}
}
iTunesCurrentInfo = mutableNewInfo;
[self setiTunesIsStopped:[[iTunesCurrentInfo objectForKey:PLAYER_STATE] isEqualToString:KEY_STOPPED]];
[self setiTunesIsPaused:[[iTunesCurrentInfo objectForKey:PLAYER_STATE] isEqualToString:KEY_PAUSED]];
//Cancel any requests we had to fire updates.
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(fireUpdateiTunesInfo) object:nil];
//fire an iTunes update in three seconds.
[self performSelector:@selector(fireUpdateiTunesInfo) withObject:nil afterDelay:3.0];
}
}
- (void)fireUpdateiTunesInfo
{
/* First, note that the track changed; code elsewhere cares, promise. */
[[NSNotificationCenter defaultCenter] postNotificationName:Adium_iTunesTrackChangedNotification object:iTunesCurrentInfo];
/* Next, update any dynamic content which includes iTunes triggers, including the Now Playing status itself */
[[NSNotificationCenter defaultCenter] postNotificationName:Adium_RequestImmediateDynamicContentUpdate object:nil];
}
#pragma mark -
#pragma mark Plugin Methods
/*!
* @brief Install
*/
- (void)installPlugin
{
NSDictionary *slashMusicDict = nil;
NSDictionary *conditionalArtistTrackDict = nil;
NSString *currentITunesTrackFormat = nil;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *itunesPath = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.apple.iTunes"];
iTunesCurrentInfo = nil;
//Only install our items if a copy of iTunes which meets the minimum requirements is found
if ([[[NSBundle bundleWithPath:itunesPath] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] doubleValue] > ITUNES_MINIMUM_VERSION) {
//Perform substitutions on outgoing content
[adium.contentController registerContentFilter:self
ofType:AIFilterContent
direction:AIFilterOutgoing];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(iTunesUpdate:)
name:@"com.apple.iTunes.playerInfo"
object:nil];
substitutionDict = [[NSDictionary alloc] initWithObjectsAndKeys:
ITUNES_ALBUM, ALBUM_TRIGGER,
ITUNES_ARTIST, ARTIST_TRIGGER,
ITUNES_COMPOSER, COMPOSER_TRIGGER,
ITUNES_GENRE, GENRE_TRIGGER,
ITUNES_PLAYER_STATE, STATUS_TRIGGER,
ITUNES_NAME, TRACK_TRIGGER,
ITUNES_YEAR, YEAR_TRIGGER,
ITUNES_STORE_URL, STORE_URL_TRIGGER,
nil];
slashMusicDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSString stringWithFormat:AILocalizedString(@"*is listening to %@ by %@*","Phrase sent in response to %_music. The first %%@ is the track; the second %%@ is the artist."), TRACK_TRIGGER, ARTIST_TRIGGER],
KEY_PLAYING,
AILocalizedString(@"*is listening to nothing*","Phrase sent in response to %_music when nothing is playing."),
KEY_STOPPED,
nil];
/* Provide flexibility with the %_iTunes substitution. By default, just store @"" for this key
* so the item is present in the plist for the adventurous to find... but still not hardcoded to a particular
* format. This is done so that a default installation doesn't have its format broken if the locale switches...
* since the format specifiers are themselves localized.
*/
currentITunesTrackFormat = [defaults objectForKey:CURRENT_TRACK_FORMAT_KEY];
if (!currentITunesTrackFormat) {
[defaults setObject:@"" forKey:CURRENT_TRACK_FORMAT_KEY];
currentITunesTrackFormat = @"";
}
if (![currentITunesTrackFormat length]) {
currentITunesTrackFormat = [NSString stringWithFormat:@"%@ - %@", TRACK_TRIGGER, ARTIST_TRIGGER];
}
conditionalArtistTrackDict = [[NSDictionary alloc] initWithObjectsAndKeys:
currentITunesTrackFormat,
KEY_PLAYING,
@"",
KEY_STOPPED,
nil];
phraseSubstitutionDict = [[NSDictionary alloc] initWithObjectsAndKeys:
slashMusicDict,
MUSIC_TRIGGER,
conditionalArtistTrackDict,
CURRENT_TRACK_TRIGGER,
nil];
[slashMusicDict release];
[conditionalArtistTrackDict release];
//Create the "Current iTunes Track" status item
[self createiTunesCurrentTrackStatusState];
//Create the toolbar item
[self createiTunesToolbarItemWithPath:itunesPath];
//Create the Edit > Insert and contextual menus
[self createTriggersMenu];
}
}
/*!
* @brief Uninstall
*/
- (void)uninstallPlugin
{
[adium.contentController unregisterContentFilter:self];
}
#pragma mark -
#pragma mark AppleScript + iTunes methods
/*!
* @brief Get current iTunes track info
*
* Execute an applescript located in the resources folder that obtains current iTunes track info and assembles the dictionary
*/
- (void)loadiTunesCurrentInfoViaApplescript
{
/*
* 1. get a url pointing to the script in the resources folder
* 2. prepare the script for execution
* 3. get results and create the dictionary based off it
*/
//get the path
NSString *path = [[NSBundle mainBundle] pathForResource:@"CurrentTunes" ofType:@"scpt"];
NSURL *pathURL = [NSURL fileURLWithPath:path];
//create the script complete with an error dictionary
NSDictionary *errors = [NSDictionary dictionary];
NSAppleScript *playingScript = [[NSAppleScript alloc] initWithContentsOfURL:pathURL error:&errors];
//execute the script and get the results as a string
NSAppleEventDescriptor *result = [playingScript executeAndReturnError:&errors];
NSString *concatenatediTunesData = [result stringValue];
//if the player was playing when the script was executed
if (concatenatediTunesData && ![concatenatediTunesData isEqualToString:@"None"]) {
//get the expected number of entries in the dictionary
NSUInteger infoCount = [substitutionDict count];
//get the values for the current iTunes song from the string
NSArray * iTunesValues = [concatenatediTunesData componentsSeparatedByString:@",$!$,"];
//if the two are properly matched (which they always will be, but just in case)
if ([iTunesValues count] == infoCount) {
//create the dictionary
[self setiTunesCurrentInfo:[NSDictionary dictionaryWithObjects:iTunesValues
forKeys:[NSArray arrayWithObjects:
ITUNES_ALBUM,
ITUNES_ARTIST,
ITUNES_COMPOSER,
ITUNES_GENRE,
ITUNES_PLAYER_STATE,
ITUNES_NAME,
ITUNES_YEAR,
ITUNES_STORE_URL,
nil]]];
} else {
NSLog(@"iTunesValues was %@ (%lu items), but I was expecting %lu. Perhaps CurrentTunes is not updated to match ESiTunesPlugin?",
iTunesValues, [iTunesValues count], infoCount);
}
} else {
//create a dictionary saying that iTunes is stopped
[self setiTunesCurrentInfo:[NSDictionary dictionaryWithObjectsAndKeys:KEY_STOPPED, PLAYER_STATE, nil]];
}
[playingScript release];
}
#pragma mark -
#pragma mark Status item creation
/*!
* @brief Create an available status state
*
* Create a Status which uses the current iTunes track data as it's message
*/
- (void)createiTunesCurrentTrackStatusState
{
//create an iTunes status of state "Available" with default available status settings
AIStatus *currentiTunesStatusState = [[AIStatus statusOfType:AIAvailableStatusType] retain];
//set status attributes
NSAttributedString *trackAndArtist = [NSAttributedString stringWithString:CURRENT_TRACK_TRIGGER];
[currentiTunesStatusState setStatusMessage:trackAndArtist];
[currentiTunesStatusState setTitle:CURRENT_ITUNES_TRACK];
[currentiTunesStatusState setMutabilityType:AISecondaryLockedStatusState];
[currentiTunesStatusState setUniqueStatusID:[NSNumber numberWithInteger:ITUNES_STATUS_ID]];
[currentiTunesStatusState setSpecialStatusType:AINowPlayingSpecialStatusType];
//give it to the AIStatusController
[adium.statusController addStatusState:currentiTunesStatusState];
[currentiTunesStatusState release];
}
#pragma mark -
#pragma mark Filter Protocol methods
/*!
* @brief Filter messages for keywords to replace
*
* Replace any iTunes triggers with the appropriate information
*/
- (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
{
NSMutableAttributedString *filteredMessage = nil;
NSString *stringMessage;
//get the attributed string as a regular string so we can do string processing
if ((stringMessage = [inAttributedString string])) {
NSEnumerator *enumerator;
NSString *trigger;
BOOL addStoreLinkAsSubtext = NO;
/* Replace the phrases with the string containing the triggers.
* For example, /music will become *is listening to %_track by %_artist*.
* This will then become the actual track information in the next while().
*/
enumerator = [phraseSubstitutionDict keyEnumerator];
while ((trigger = [enumerator nextObject])) {
//search for phrase in the string that needs to be filtered
if (([stringMessage rangeOfString:trigger options:(NSLiteralSearch | NSCaseInsensitiveSearch)].location != NSNotFound)) {
NSDictionary *replacementDict;
NSString *replacement;
//get the format for the current trigger
replacementDict = [phraseSubstitutionDict objectForKey:trigger];
//replacement of phrase should reflect iTunes player state
if (![self iTunesIsStopped] && ![self iTunesIsPaused]) {
replacement = [replacementDict objectForKey:KEY_PLAYING];
/* If the trigger is the trigger used for the Current iTunes Track status, we'll want to add a subtext of the store link
* so account code can send it out later on.
*/
if ([trigger isEqualToString:CURRENT_TRACK_TRIGGER]) {
addStoreLinkAsSubtext = YES;
}
} else {
replacement = [replacementDict objectForKey:KEY_STOPPED];
}
//create a attributedstring if it hasn't been created already
if (!filteredMessage) filteredMessage = [[inAttributedString mutableCopy] autorelease];
//Perform the replacement
[filteredMessage replaceOccurrencesOfString:trigger
withString:replacement
options:(NSLiteralSearch | NSCaseInsensitiveSearch)
range:NSMakeRange(0, [filteredMessage length])];
}
}
if (filteredMessage) {
//Update our string for the simple trigger replacement process so we can replace the %_ tokens
stringMessage = [filteredMessage string];
}
//Substitute simple triggers as appropriate
enumerator = [substitutionDict keyEnumerator];
while ((trigger = [enumerator nextObject])) {
//Find if the current trigger is in the string
if (([stringMessage rangeOfString:trigger options:(NSLiteralSearch | NSCaseInsensitiveSearch)].location != NSNotFound)) {
NSString *replacement;
//Get the info if we don't already have it
if (!iTunesCurrentInfo) [self loadiTunesCurrentInfoViaApplescript];
//Attempt to replace it with its proper value
if (!(replacement = [iTunesCurrentInfo objectForKey:[substitutionDict objectForKey:trigger]])) {
//If no replacement is found, replace the trigger with an empty string
replacement = @"";
}
//if a mutable attributed string for the string to be filtered doesn't exist, create it.
if (!filteredMessage) filteredMessage = [[inAttributedString mutableCopy] autorelease];
//Replace the current trigger with the value we found above
[filteredMessage replaceOccurrencesOfString:trigger
withString:replacement
options:(NSLiteralSearch | NSCaseInsensitiveSearch)
range:NSMakeRange(0, [filteredMessage length])];
}
}
if (addStoreLinkAsSubtext && filteredMessage) {
NSString *storeLinkForSubtext = [iTunesCurrentInfo objectForKey:[substitutionDict objectForKey:STORE_URL_TRIGGER]];
if (storeLinkForSubtext) {
[filteredMessage addAttribute:@"AIMessageSubtext"
value:storeLinkForSubtext
range:NSMakeRange(0, [filteredMessage length])];
}
}
}
//Give back the processed string
return (filteredMessage ? filteredMessage : inAttributedString);
}
/*!
* @brief Filter priority
*
* Filter at default priority
*/
- (CGFloat)filterPriority
{
return DEFAULT_FILTER_PRIORITY;
}
#pragma mark -
#pragma mark Notification Selector
/*!
* @brief The iTunes song changed
*
* The accessor method caches the information and then requst an immediate update to dynamic content
*/
- (void)iTunesUpdate:(NSNotification *)aNotification
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary *newInfo = [aNotification userInfo];
[self setiTunesCurrentInfo:newInfo];
[pool release];
}
#pragma mark -
#pragma mark Toolbar Item Methods
/*!
* @brief Create the toolbar item
*
* Create toolbar item and it's menu
*/
- (void)createiTunesToolbarItemWithPath:(NSString *)iTunesPath
{
NSMenu *menu = [[NSMenu alloc] init];
MVMenuButton *button = [[MVMenuButton alloc] initWithFrame:NSMakeRect(0,0,32,32)];
//configure the popup button and its menu
[button setImage:[[NSWorkspace sharedWorkspace] iconForFile:iTunesPath]];
[self createiTunesToolbarItemMenuItems:menu];
NSToolbarItem * iTunesItem = [AIToolbarUtilities toolbarItemWithIdentifier:ITUNES_TOOLBAR_ITEM
label:TOOLBAR_LABEL
paletteLabel:TOOLBAR_LABEL
toolTip:AILocalizedString(@"Insert current iTunes track information.","Label for iTunes toolbar menu item.")
target:self
settingSelector:@selector(setView:)
itemContent:button
action:NULL
menu:nil];
//configure the toolbar and button for use
[[iTunesItem view] setMenu:menu];
[iTunesItem setMinSize:NSMakeSize(32,32)];
[iTunesItem setMaxSize:NSMakeSize(32,32)];
[button setToolbarItem:iTunesItem];
//Add menu to toolbar item (for text mode)
NSMenuItem *mItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] init] autorelease];
[mItem setSubmenu:menu];
[mItem setTitle:TOOLBAR_LABEL];
[iTunesItem setMenuFormRepresentation:mItem];
//give it to adium to use
[adium.toolbarController registerToolbarItem:iTunesItem forToolbarType:@"TextEntry"];
[button release];
[menu release];
}
/*!
* @brief Create the toolbar item's menu
*
* Populate a menu with menu items that will insert appropriate values of the currently playing iTunes song.
*/
- (void)createiTunesToolbarItemMenuItems:(NSMenu *)iTunesMenu
{
NSMenu *insertTrackSubmenu = [[NSMenu alloc] init];
[iTunesMenu addItem:[self menuItemWithTitle:CURRENT_ITUNES_TRACK
action:@selector(insertFilteredString:)
representedObject:CURRENT_TRACK_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[iTunesMenu addItem:[self menuItemWithTitle:MUSIC_TRIGGER
action:@selector(insertFilteredString:)
representedObject:MUSIC_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[iTunesMenu addItem:[NSMenuItem separatorItem]];
//submenu of actions related to a track
NSMenuItem *submenuRoot = [[NSMenuItem alloc] initWithTitle:AILocalizedString(@"Track Information","Submenu for iTunes toolbar item menu for inserting current track information.")
action:NULL
keyEquivalent:@""];
[insertTrackSubmenu addItem:[self menuItemWithTitle:AILocalizedString(@"Album","Insert Current iTunes track album toolbar menu item.")
action:@selector(insertFilteredString:)
representedObject:ALBUM_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[insertTrackSubmenu addItem:[self menuItemWithTitle:AILocalizedString(@"Artist","Insert Current iTunes track artist toolbar menu item.")
action:@selector(insertFilteredString:)
representedObject:ARTIST_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[insertTrackSubmenu addItem:[self menuItemWithTitle:AILocalizedString(@"Composer","Insert Current iTunes track composer toolbar menu item.")
action:@selector(insertFilteredString:)
representedObject:COMPOSER_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[insertTrackSubmenu addItem:[self menuItemWithTitle:AILocalizedString(@"Genre","Insert Current iTunes track genre toolbar menu item.")
action:@selector(insertFilteredString:)
representedObject:GENRE_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[insertTrackSubmenu addItem:[self menuItemWithTitle:AILocalizedString(@"Name","Insert Current iTunes track name toolbar menu item.")
action:@selector(insertFilteredString:)
representedObject:TRACK_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[insertTrackSubmenu addItem:[self menuItemWithTitle:AILocalizedString(@"Year","Insert Current iTunes track year toolbar menu item.")
action:@selector(insertFilteredString:)
representedObject:YEAR_TRIGGER
kind:ENABLED_IF_ITUNES_PLAYING]];
[insertTrackSubmenu addItem:[self menuItemWithTitle:AILocalizedString(@"iTunes Music Store Link","Insert Current iTunes track store link toolbar menu item.")
action:@selector(insertiTMSLink)
representedObject:nil
kind:ENABLED_IF_ITUNES_PLAYING]];
[iTunesMenu addItem:submenuRoot];
[iTunesMenu setSubmenu:insertTrackSubmenu forItem:submenuRoot];
[submenuRoot release];
[insertTrackSubmenu release];
[iTunesMenu addItem:[NSMenuItem separatorItem]];
//this isn't implemented yet, need some advice on this one
[iTunesMenu addItem:[self menuItemWithTitle:[AILocalizedString(@"Search Selection in Music Store","iTunes toolbar menu item title to search selection in iTMS.") stringByAppendingEllipsis]
action:@selector(gatherSelection)
representedObject:nil
kind:RESPONDER_IS_WEBVIEW]];
[iTunesMenu addItem:[NSMenuItem separatorItem]];
[iTunesMenu addItem:[self menuItemWithTitle:[AILocalizedString(@"Bring iTunes to Front","iTunes toolbar menu item title to make iTunes frontmost app.") stringByAppendingEllipsis]
action:@selector(bringiTunesToFront)
representedObject:nil
kind:ALWAYS_ENABLED]];
}
/*!
* @brief Create a menu item
*
* create a menu item targeting this plugin. Determine if it should disable itself when firstResponder != [textView class].
*/
- (NSMenuItem *)menuItemWithTitle:(NSString *)title action:(SEL)action representedObject:(id)representedObject kind:(KGiTunesPluginMenuItemKind)itemKind
{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:action keyEquivalent:@""];
[item setTarget:self];
[item setTag:itemKind];
[item setRepresentedObject:representedObject];
[item setEnabled:YES];
return [item autorelease];
}
#pragma mark -
#pragma mark Toolbar Item actions
/*!
* @brief Insert current song iTMS link
*
* Get the URL from the iTunesCurrentInfo dict or create a URL if one can't be found.
*/
- (void)insertiTMSLink
{
NSMutableString *url = [[NSMutableString alloc] init];
NSString *urlLabel = nil;
//get current information
NSString *artist = [iTunesCurrentInfo objectForKey:[substitutionDict objectForKey:ARTIST_TRIGGER]];
NSString *trackName = [iTunesCurrentInfo objectForKey:[substitutionDict objectForKey:TRACK_TRIGGER]];
//see if we have a URL for us
NSString *storeURL = [iTunesCurrentInfo objectForKey:[substitutionDict objectForKey:STORE_URL_TRIGGER]];
if ([storeURL length]) {
[url appendString:storeURL];
}
//if we have no url data from the iTunes notification to begin with - probably because we got the info using the applescript
if (![url length]) {
//if iTunes is playing or paused something
if (![self iTunesIsStopped] || ![self iTunesIsPaused]) {
[url appendString:ITMS_SEARCH_URL];
//if there is a name given to this song put it in the url
if ([trackName length]) {
[url appendFormat:@"n=%@", [trackName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
} else {
trackName = @"";
}
//if there is a name and an artist, we'll use both to refine our search
if ([artist length] && [trackName length]) {
//[url appendFormat:@"?artistTerm=%@", [artist stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[url appendFormat:@"&an=%@", [artist stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
} else if ([artist length]) {
//no proper track name but we have a decent artist name to include in the url
//[url appendFormat:@"artistTerm=%@", [trackName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[url appendFormat:@"an=%@", [trackName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
} else {
artist = @"";
}
}
}
//if something has been added to our search request, create a lovely label for it
if (![url isEqualToString:ITMS_SEARCH_URL] && [url length]) {
urlLabel = [[NSString alloc] initWithFormat:@"%@ - %@", trackName, artist];
} else {
[url release]; url = nil;
}
//if we have a url, give it to the user as a nice, formatted <a> tag
if (url) {
NSAttributedString *attributedLink = [[NSAttributedString alloc] initWithAttributedString:[AIHTMLDecoder decodeHTML:[NSString stringWithFormat:@"<A HREF=\"%@\">%@</A>", url, urlLabel]]];
[self insertAttributedStringIntoMessageEntryView:attributedLink];
[attributedLink release];
[url release];
[urlLabel release];
} else {
//the artist name and or the track name is literally @""
NSBeep();
}
}
/*!
* @brief Filter and insert current iTunes song display into message entry
*
* Toolbar method. Take the trigger and filter it with real values
*
* @param sender An NSMenuItem whose representedObject is the appropriate trigger to filter
*/
- (void)insertFilteredString:(id)sender
{
[self filterAndInsertString:[sender representedObject]];
}
/*!
* @brief Search iTMS for inputtted data
*
* Build the necessary url and execute it
*/
- (void)searchMusicStoreWithSelection:(NSString *)selectedText
{
//Create a general search request
NSString *url = [NSString stringWithFormat:@"itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?term=%@", [selectedText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
}
/*!
* @brief Get the selection from the webmessageview
*
* Get the selected text in the messageview and run it thru the iTMS
*/
- (void)gatherSelection
{
id responder = [[[NSApplication sharedApplication] keyWindow] firstResponder];
[self searchMusicStoreWithSelection:[responder selectedString]];
}
/*!
* @brief Bring iTunes to foreground
*/
- (void)bringiTunesToFront
{
[[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
}
#pragma mark -
#pragma mark Edit/Contextual menu item actions
/*!
* @brief Insert triggers into message entry
*
* Used in the "Edit" and contextual menus.
* @param sender An NSMenuItem whose representedObject is the appropriate trigger to insert
*/
- (void)insertUnfilteredString:(id)sender
{
[self insertStringIntoMessageEntryView:[sender representedObject]];
}
#pragma mark -
#pragma mark Text Insertion methods
/*!
* @brief Filter and Insert plain string
*
* Converts the string to an attributed string and filters it, then inserting it into the message entry view
* Used by all the toolbar item actions.
*/
- (void)filterAndInsertString:(NSString *)inString
{
id responder = [[[NSApplication sharedApplication] keyWindow] firstResponder];
if (responder && [responder isKindOfClass:[NSTextView class]]) {
NSAttributedString *attributedResult = [[NSAttributedString alloc] initWithString:inString
attributes:[(NSTextView *)responder typingAttributes]];
[self insertAttributedStringIntoMessageEntryView:[self filterAttributedString:attributedResult context:nil]];
[attributedResult release];
}
}
/*!
* @brief Insert raw string into message view
*
* Converts the string to an attributed string and inserts it into the message entry view.
* Used with the insertUnfiltered... methods which are used by edit and contextual menus.
*/
- (void)insertStringIntoMessageEntryView:(NSString *)inString
{
id responder = [[[NSApplication sharedApplication] keyWindow] firstResponder];
if (responder && [responder isKindOfClass:[NSTextView class]]) {
NSAttributedString *attributedResult = [[NSAttributedString alloc] initWithString:inString
attributes:[(NSTextView *)responder typingAttributes]];
[self insertAttributedStringIntoMessageEntryView:attributedResult];
[attributedResult release];
}
}
/*!
* @brief Insert attributed string into message view
*
* Inserts an attributed string it into the message entry view.
* Don't check to see if the responder is of class NSTextView because the validateMenuItem method checks.
*/
- (void)insertAttributedStringIntoMessageEntryView:(NSAttributedString *)inString
{
NSResponder *textView = [[[NSApplication sharedApplication] keyWindow] firstResponder];
[textView insertText:inString];
if (![inString length]) {
NSBeep();
}
}
#pragma mark -
#pragma mark Edit/Contextual menu methods
/*!
* @brief Create Edit and Contextual menus of iTunes triggers
*
* Build the menus for the iTunes triggers that autodisables when a first responder isn't a textView
*/
- (NSMenu *)menuOfTriggers
{
NSMenu *triggersMenu = [[NSMenu alloc] init];
NSEnumerator *enumerator;
NSString *trigger;
[triggersMenu addItem:[self menuItemWithTitle:CURRENT_TRACK_TRIGGER
action:@selector(insertUnfilteredString:)
representedObject:CURRENT_TRACK_TRIGGER
kind:AUTODISABLES]];
[triggersMenu addItem:[self menuItemWithTitle:MUSIC_TRIGGER
action:@selector(insertUnfilteredString:)
representedObject:MUSIC_TRIGGER
kind:AUTODISABLES]];
[triggersMenu addItem:[NSMenuItem separatorItem]];
//Simple triggers
enumerator = [substitutionDict keyEnumerator];
while ((trigger = [enumerator nextObject])) {
[triggersMenu addItem:[self menuItemWithTitle:trigger
action:@selector(insertUnfilteredString:)
representedObject:trigger
kind:AUTODISABLES]];
}
return [triggersMenu autorelease];
}
/*!
* @brief Create Edit and Contextual menus of iTunes triggers
*
* Users can then insert %_<token name> into any text view
*/
- (void)createTriggersMenu
{
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:INSERT_TRIGGERS_MENU target:self action:NULL keyEquivalent:@""];
NSMenu *menuOfTriggers = [self menuOfTriggers];
[menuItem setSubmenu:menuOfTriggers];
[adium.menuController addMenuItem:menuItem toLocation:LOC_Edit_Additions];
[menuItem release];
menuItem = [[NSMenuItem alloc] initWithTitle:INSERT_TRIGGERS_MENU target:self action:NULL keyEquivalent:@""];
[menuItem setSubmenu:[[menuOfTriggers copy] autorelease]];
[adium.menuController addContextualMenuItem:menuItem toLocation:Context_TextView_Edit];
[menuItem release];
}
/*!
* @brief Configure accessibility of menu items
*
* Depending on whether the responder is a textview and if it should be enabled when itunes isn't playing
*/
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
NSResponder *responder = [[[NSApplication sharedApplication] keyWindow] firstResponder];
KGiTunesPluginMenuItemKind tag = [menuItem tag];
BOOL enable;
//we only insert things into textviews
if (responder && [responder isKindOfClass:[NSTextView class]]) {
//some menu items are only enabled if itunes is playing something
if ((([self iTunesIsStopped] || [self iTunesIsPaused]) && (tag == ENABLED_IF_ITUNES_PLAYING)) || (tag == RESPONDER_IS_WEBVIEW)) {
enable = NO;
} else {
enable = [(NSTextView *)responder isEditable];
}
} else if (tag == RESPONDER_IS_WEBVIEW) {
if ([responder respondsToSelector:@selector(selectedString)]) {
NSString *selectedString = [(id)responder selectedString];
if (selectedString && [selectedString length]) {
enable = YES;
} else {
enable = NO;
}
} else {
enable = NO;
}
} else {
// enable it if it is always supposed to be on, disable if otherwise
enable = (tag == ALWAYS_ENABLED);
}
return enable;
}
#pragma mark -
#pragma mark Deallocation
- (void)dealloc
{
//Remove self from notifications list
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
//Release class variables
if (iTunesCurrentInfo) [iTunesCurrentInfo release];
if (substitutionDict) [substitutionDict release];
if (phraseSubstitutionDict) [phraseSubstitutionDict release];
[super dealloc];
}
@end
|