Merged [15040]: Trying some magic: 5 seconds after the last unreachable host is repor...
[adiumx.git] / Source / AIContactAccountsPane.m
blob33ca29eeadd25d941f64cf33023f67344af99bbc
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 "AIAccountController.h"
18 #import "AIContactAccountsPane.h"
19 #import "AIContactController.h"
20 #import <AIUtilities/AIAlternatingRowTableView.h>
21 #import <AIUtilities/AIMenuAdditions.h>
22 #import <AIUtilities/AIPopUpButtonAdditions.h>
23 #import <Adium/AIAccount.h>
24 #import <Adium/AIListContact.h>
25 #import <Adium/AIListObject.h>
26 #import <Adium/AIListGroup.h>
27 #import <Adium/AILocalizationTextField.h>
28 #import <Adium/AIMetaContact.h>
30 @interface AIContactAccountsPane (PRIVATE)
31 - (void)updateAccountList;
32 - (void)updateGroupList;
33 @end
35 /*!
36  * @class AIContactAccountsPane
37  * @brief Accounts pane in the contact info window
38  *
39  * Provides a list of what accounts list a contact and in what group.
40  */
41 @implementation AIContactAccountsPane
43 //Preference pane properties
44 /*!
45  * @brief Category
46  */
47 - (CONTACT_INFO_CATEGORY)contactInfoCategory{
48     return(AIInfo_Accounts);
51 /*!
52  * @brief Nib name
53  */
54 - (NSString *)nibName{
55     return(@"ContactAccounts");
58 /*!
59  * @brief Configure the preference view
60  */
61 - (void)viewDidLoad
63         [label_listedOnTheFollowingOfYourAccounts setLocalizedString:AILocalizedString(@"Listed on the following of your accounts:",nil)];
65         //Configure Table view
66         [tableView_accounts setDrawsAlternatingRows:YES];
67         [tableView_accounts setAcceptsFirstMouse:YES];
68         [[[tableView_accounts tableColumnWithIdentifier:@"account"] headerCell] setTitle:AILocalizedString(@"On Account",nil)];
69         [[[tableView_accounts tableColumnWithIdentifier:@"group"] headerCell] setTitle:AILocalizedString(@"In Group",nil)];
71         //Observe contact list changes
72         [[adium notificationCenter] addObserver:self
73                                                                    selector:@selector(updateGroupList)
74                                                                            name:Contact_ListChanged
75                                                                          object:nil];
76         [self updateGroupList];
77         
78         //Observe account changes
79         [[adium notificationCenter] addObserver:self
80                                                                    selector:@selector(updateAccountList)
81                                                                            name:Account_ListChanged
82                                                                          object:nil];
83         [self updateAccountList];
86 /*!
87  * @brief Preference view is closing
88  */
89 - (void)viewWillClose
91         [accounts release]; accounts = nil;
92     [listObject release]; listObject = nil;
93         [[adium notificationCenter] removeObserver:self]; 
96 /*!
97  * @brief Configure the pane for a list object
98  */
99 - (void)configureForListObject:(AIListObject *)inObject
101         //New list object
102         [listObject release];
103         listObject = [inObject retain];
105         //Rebuild our account list
106         [self updateAccountList];
110  * @brief Update our list of accounts
111  */
112 - (void)updateAccountList
114         //Get the new accounts
115         [accounts release];
116         
117         if ([listObject isKindOfClass:[AIMetaContact class]]){
118                 NSEnumerator    *enumerator;
119                 NSString                *serviceClass;
120                 
121                 accounts = [[NSMutableArray alloc] init];
122                 enumerator = [[(AIMetaContact *)listObject dictionaryOfServiceClassesAndListContacts] keyEnumerator];
123                 while (serviceClass = [enumerator nextObject]){
124                         [(NSMutableArray *)accounts addObjectsFromArray:[[adium accountController] accountsWithServiceClass:serviceClass]];
125                 }
126                 
127         }else{
128                 accounts = [[[adium accountController] accountsWithServiceClassOfService:[listObject service]] retain];
129         }
130         
131         //Refresh our table
132         [tableView_accounts reloadData];
136  * @brief Update our list of groups
137  */
138 - (void)updateGroupList
140         //Get the new groups
141         NSMenu          *groupMenu = [[adium contactController] menuOfAllGroupsInGroup:nil withTarget:self];
142         NSMenuItem      *unlistedItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:AILocalizedString(@"(Not Listed)",nil)
143                                                                                                                                                                           target:self
144                                                                                                                                                                           action:@selector(selectGroup:)
145                                                                                                                                                            keyEquivalent:@""] autorelease];
146         [groupMenu insertItem:[NSMenuItem separatorItem] atIndex:0];
147         [groupMenu insertItem:unlistedItem atIndex:0];
148         
149         [[[tableView_accounts tableColumnWithIdentifier:@"group"] dataCell] setMenu:groupMenu];
150         
151         //Refresh our table
152         [tableView_accounts reloadData];
156 //Table View Data Sources ----------------------------------------------------------------------------------------------
157 #pragma mark TableView Data Sources
159  * @brief Number of table view rows
160  */
161 - (int)numberOfRowsInTableView:(NSTableView *)tableView
163         return([accounts count]);
167  * @brief Table view object value
168  */
169 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
171         NSString                *identifier = [tableColumn identifier];
172         AIAccount               *account = [accounts objectAtIndex:row];
174         if([identifier isEqualToString:@"account"]){
175                 NSString        *accountFormattedUID = [account formattedUID];
176                 
177                 if([account integerStatusObjectForKey:@"Online"]){
178                         return(accountFormattedUID);
179                         
180                 }else{
181                         //Gray the names of offline accounts
182                         NSDictionary            *attributes = [NSDictionary dictionaryWithObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
183                         NSAttributedString      *string = [[NSAttributedString alloc] initWithString:accountFormattedUID attributes:attributes];
184                         return([string autorelease]);
185                 }
186                 
187         }
188         
189         return(@"");
193  * @brief Table view will display a cell
194  */
195 - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)row
197         NSString                *identifier = [tableColumn identifier];
198         AIAccount               *account;
199         AIListContact   *exactContact;
200         BOOL                    accountOnline;
201                 
202         account =  [accounts objectAtIndex:row];
203         if ([listObject isKindOfClass:[AIMetaContact class]]){
204                 //If we're dealing with a metaContact, make sure it's the topmost one
205                 exactContact = (AIListContact *)[[adium contactController] parentContactForListObject:listObject];
207         }else{
208                 //Retrieve an AIListContact on this account
209                 exactContact = [[adium contactController] existingContactWithService:[listObject service]
210                                                                                                                                          account:account
211                                                                                                                                                  UID:[listObject UID]];
212         }
213                                 
214         accountOnline = [account online];
216         //Disable cells for offline accounts
217         [cell setEnabled:accountOnline];
218         
219         //Select active group
220         if([identifier isEqualToString:@"group"]){
221                 if(accountOnline){
222                         AIListGroup     *group;
223                         
224                         if(group = [[adium contactController] remoteGroupForContact:exactContact]){
225                                 [cell selectItemWithRepresentedObject:group];
226                         }else{
227                                 [cell selectItemAtIndex:0];                     
228                         }
229                 }else{
230                         [cell setTitle:AILocalizedString(@"(Unavailable)",nil)];
231                 }
232         }
233         
237  * @brief Empty.  This method is the target of our menus, and needed for menu validation.
238  */
239 - (void)selectGroup:(id)sender {};
242  * @brief Table view set object value
243  */
244 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
246         NSString                *identifier = [tableColumn identifier];
247         AIAccount               *account = [accounts objectAtIndex:row];
248         AIListContact   *exactContact;
249         
250         if([identifier isEqualToString:@"group"]){
251                 NSMenu          *menu = [[tableColumn dataCell] menu];
252                 int                     menuIndex = [object intValue];
253                 
254                 if(menuIndex >= 0 && menuIndex < [menu numberOfItems]){
255                         AIListGroup     *group = [[menu itemAtIndex:menuIndex] representedObject];
256                         
257                         if ([listObject isKindOfClass:[AIMetaContact class]]){
258                                 //If we're dealing with a metaContact, make sure it's the topmost one
259                                 exactContact = (AIListContact *)[[adium contactController] parentContactForListObject:listObject];
260                                 
261                         }else{
262                                 //Retrieve an AIListContact on this account
263                                 exactContact = [[adium contactController] existingContactWithService:[listObject service]
264                                                                                                                                                          account:account
265                                                                                                                                                                  UID:[listObject UID]];
266                         }
267                         
268                         if (group){
269                                 if (group != [exactContact containingObject]){
270                                         
271                                         if (exactContact && ([exactContact containingObject] ||
272                                                                                  [exactContact isKindOfClass:[AIMetaContact class]])){
273                                                 //Move contact
274                                                 [[adium contactController] moveContact:exactContact toGroup:group];
275                                                 
276                                         }else{
277                                                 //Add contact
278                                                 if (!exactContact){
279                                                         exactContact = [[adium contactController] contactWithService:[listObject service]
280                                                                                                                                                                  account:account
281                                                                                                                                                                          UID:[listObject UID]];
282                                                 }
283                                                 
284                                                 [[adium contactController] addContacts:[NSArray arrayWithObject:exactContact] 
285                                                                                                            toGroup:group];
286                                         }
287                                 }
288                         }else{
289                                 if(exactContact){
290                                         //User selected not listed, so we'll remove that contact
291                                         [[adium contactController] removeListObjects:[NSArray arrayWithObject:exactContact]];
292                                 }
293                         }
294                 }
295         }
298 @end