Merged [21073]: When encoding an icon to JPEG, start off at 100% quality. If that...
[adiumx.git] / Source / AIServiceMenu.m
blob7950561fc072abada31388e5ac2d093c222581f5
1 //
2 //  AIServiceMenu.m
3 //  Adium
4 //
5 //  Created by Adam Iser on 5/19/05.
6 //
8 #import <Adium/AIServiceMenu.h>
9 #import <Adium/AIAccountControllerProtocol.h>
10 #import <Adium/AIService.h>
11 #import <Adium/AIServiceIcons.h>
12 #import <AIUtilities/AIMenuAdditions.h>
14 /*!
15  * @class AIServiceMenu
16  * @brief Class to provide a menu of services
17  *
18  * See menuOfServicesWithTarget:activeServicesOnly:longDescription:format:
19  */
20 @implementation AIServiceMenu
22 /*!
23  * @brief Sort menu items by title
24  */
25 int titleSort(NSMenuItem *itemA, NSMenuItem *itemB, void *context)
27         return [[itemA title] compare:[itemB title] options:NSLiteralSearch];
30 /*!
31  * @brief Returns a menu of services.
32  *
33  * Each menu item's represented object is the AIService it represents.  Services are grouped by 'importance' and sorted alphabetically within groups.
34  *
35  * @param target Target on which @selector(selectAccount:) is called when the user makes a selection
36  * @param activeServicesOnly If YES, only services for enabled accounts are included. If NO, all possible services are included.
37  * @param longDescription If YES, use the service's longer (more verbose) description -- for example, AOL Instant Messenger rather than AIM
38  * @param format Allows the description to be placed within a format string. If it is nil, the description alone will be used.
39  */
40 + (NSMenu *)menuOfServicesWithTarget:(id)target activeServicesOnly:(BOOL)activeServicesOnly
41                                          longDescription:(BOOL)longDescription format:(NSString *)format
43         id<AIAccountController> accountController = [[AIObject sharedAdiumInstance] accountController];
44         AIServiceImportance     importance;
45         unsigned                        numberOfItems = 0;
46         id                                      serviceArray;
47         
48         //Prepare our menu
49         NSMenu *menu = [[NSMenu allocWithZone:[NSMenu menuZone]] init];
50         
51         serviceArray = (activeServicesOnly ? (id)[accountController activeServicesIncludingCompatibleServices:YES] : (id)[accountController services]);
52         
53         //Divide our menu into sections.  This helps separate less important services from the others (sorry guys!)
54         for (importance = AIServicePrimary; importance <= AIServiceUnsupported; importance++) {
55                 NSEnumerator    *enumerator;
56                 AIService               *service;
57                 NSMutableArray  *menuItemArray = [[NSMutableArray alloc] init];
58                 NSMenuItem              *menuItem;
59                 unsigned                currentNumberOfItems;
60                 BOOL                    addedDivider = NO;
61                 
62                 //Divider
63                 currentNumberOfItems = [menu numberOfItems];
64                 if (currentNumberOfItems > numberOfItems) {
65                         [menu addItem:[NSMenuItem separatorItem]];
66                         numberOfItems = currentNumberOfItems + 1;
67                         addedDivider = YES;
68                 }
69                 
70                 //Insert a menu item for each service of this importance
71                 enumerator = [serviceArray objectEnumerator];
72                 while ((service = [enumerator nextObject])) {
73                         if ([service serviceImportance] == importance) {
74                                 NSString        *description = (longDescription ?
75                                                                                         [service longDescription] :
76                                                                                         [service shortDescription]);
77                                 
78                                 menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:(format ? 
79                                                                                                                                                                                 [NSString stringWithFormat:format,description] :
80                                                                                                                                                                                 description)
81                                                                                                                                                                 target:target 
82                                                                                                                                                                 action:@selector(selectServiceType:) 
83                                                                                                                                                  keyEquivalent:@""];
84                                 [menuItem setRepresentedObject:service];
85                                 [menuItem setImage:[AIServiceIcons serviceIconForService:service
86                                                                                                                                         type:AIServiceIconSmall
87                                                                                                                            direction:AIIconNormal]];
88                                 [menuItemArray addObject:menuItem];
89                                 [menuItem release];
90                         }
91                 }
93                 [menuItemArray sortUsingFunction:titleSort context:NULL];
94                 
95                 enumerator = [menuItemArray objectEnumerator];
96                 while ((menuItem = [enumerator nextObject])) {
97                         [menu addItem:menuItem];
98                 }
99                 
100                 [menuItemArray release];
102                 //If we added a divider but didn't add any items, remove it
103                 currentNumberOfItems = [menu numberOfItems];
104                 if (addedDivider && (currentNumberOfItems <= numberOfItems) && (currentNumberOfItems > 0)) {
105                         [menu removeItemAtIndex:(currentNumberOfItems-1)];
106                 }
107         }
108         
109         return [menu autorelease];
110 }       
112 @end