* Fixed the objectSpecifier of `AIStatusItem`s to be based on the unique ID rather...
[adiumx.git] / Source / AdiumServices.m
blobd12e7a0fcddec6f1e0d8ed4ae7743cb818ecc501
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import <Adium/AIAccountControllerProtocol.h>
18 #import "AdiumServices.h"
19 #import <Adium/AIService.h>
20 #import <Adium/AIAccount.h>
22 @implementation AdiumServices
24 /*!
25  * @brief Init
26  */
27 - (id)init
29         if ((self = [super init])) {
30                 services = [[NSMutableDictionary alloc] init];
31         }
32         
33         return self;
36 - (void)dealloc
38         [services release]; services = nil;
39         [super dealloc];
42 /*!
43  * @brief Register an AIService instance
44  *
45  * All services should be registered before they are used
46  */
47 - (void)registerService:(AIService *)inService
49     [services setObject:inService forKey:[inService serviceCodeUniqueID]];
52 /*!
53  * @brief Returns an array of all available services
54  *
55  * @return NSArray of AIService instances
56  */
57 - (NSArray *)services
59         return [services allValues];
62 /*!
63  * @brief Returns an array of all active services
64  *
65  * "Active" services are those for which the user has an enabled account.
66  * @param includeCompatible Include services which are compatible with an enabled account but not specifically active.
67  *        For example, if an AIM account is enabled, the ICQ service will be included if this is YES.
68  * @return NSArray of AIService instances
69  */
70 - (NSSet *)activeServicesIncludingCompatibleServices:(BOOL)includeCompatible
72         NSMutableSet    *activeServices = [NSMutableSet set];
73         NSEnumerator    *accountEnumerator = [[[adium accountController] accounts] objectEnumerator];
74         AIAccount               *account;
76         if (includeCompatible) {
77                 //Scan our user's accounts and build a list of service classes that they cover
78                 NSMutableSet    *serviceClasses = [NSMutableSet set];
79                 
80                 while ((account = [accountEnumerator nextObject])) {
81                         if ([account enabled]) {
82                                 [serviceClasses addObject:[[account service] serviceClass]];
83                         }
84                 }
85                 
86                 //Gather and return all services compatible with these service classes
87                 NSEnumerator    *serviceEnumerator = [services objectEnumerator];
88                 AIService               *service;
89                 
90                 while ((service = [serviceEnumerator nextObject])) {
91                         if ([serviceClasses containsObject:[service serviceClass]]) {
92                                 [activeServices addObject:service];
93                         }
94                 }
95                 
96         } else {
97                 while ((account = [accountEnumerator nextObject])) {
98                         if ([account enabled]) {
99                                 [activeServices addObject:[account service]];
100                         }
101                 }               
102         }
104         return activeServices;
108  * @brief Retrieves a service by its unique ID
110  * Unique IDs are returned by -[AIService serviceCodeUniqueID]. An example is @"libpurple-oscar-AIM".
111  * @param uniqueID The serviceCodeUniqueID of the desired service
112  * @return AIService if found, nil if not found
113  */
114 - (AIService *)serviceWithUniqueID:(NSString *)uniqueID
116     return [services objectForKey:uniqueID];
120  * @brief Retrieves a service by service ID.
122  * Service IDs may be shared by multiple services if the same service is provided by two different plugins.
123  * -[AIService serviceID] returns serviceIDs. An example is @"AIM".
124  * @return The first service with the matching service ID, or nil if none is found.
125  */
126 - (AIService *)firstServiceWithServiceID:(NSString *)serviceID
128         NSEnumerator    *enumerator = [services objectEnumerator];
129         AIService               *service;
130         
131         while ((service = [enumerator nextObject])) {
132                 if ([[service serviceID] isEqualToString:serviceID]) break;
133         }
134         
135         return service;
138 @end