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 / Source / ESStatusSort.m

  1
  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
/* 
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import <Adium/AIContactControllerProtocol.h>
#import "ESStatusSort.h"
#import <AIUtilities/AIDictionaryAdditions.h>
#import <Adium/AIListObject.h>
#import <Adium/AILocalizationTextField.h>
#import <Adium/AIContactList.h>

#define STATUS_SORT_DEFAULT_PREFS   @"StatusSortDefaults"

#define KEY_GROUP_AVAILABLE                     @"Status:Group Available"
#define KEY_GROUP_MOBILE                        @"Status:Group Mobile"
#define KEY_GROUP_UNAVAILABLE           @"Status:Group Unavailable"
#define KEY_GROUP_AWAY                          @"Status:Group Away"
#define KEY_GROUP_IDLE                          @"Status:Group Idle"
#define KEY_GROUP_IDLE_AND_AWAY         @"Status:Group Idle+Away"
#define KEY_SORT_IDLE_TIME                      @"Status:Sort by Idle Time"
#define KEY_RESOLVE_ALPHABETICALLY  @"Status:Resolve Alphabetically"
#define KEY_SORT_ORDER                          @"Status:Sort Order"
#define KEY_RESOLVE_BY_LAST_NAME        @"Status:Resolve Alphabetically By Last Name"
#define KEY_SORT_GROUPS_ALPHA           @"Status:Sort Groups Alphabetically"

#define AVAILABLE                                       AILocalizedString(@"Available",nil)
#define AWAY                                            AILocalizedString(@"Away",nil)
#define IDLE                                            AILocalizedString(@"Idle",nil)
#define AWAY_AND_IDLE                           AILocalizedString(@"Away and Idle",nil)
#define UNAVAILABLE                                     AILocalizedString(@"Unavailable",nil)
#define OTHER_UNAVAILABLE                       AILocalizedString(@"Other Unavailable",nil)             
#define ONLINE                                          AILocalizedString(@"Online",nil)                
#define MOBILE                                          AILocalizedString(@"Mobile",nil)

#define STATUS_DRAG_TYPE                        @"Status Sort"

typedef enum {
        Available = 0,
        Away,
        Idle,
        Away_And_Idle,
        Unavailable,
        Online,
        Mobile,
        MAX_SORT_ORDER_DIMENSION
} Status_Sort_Type;

static BOOL groupAvailable;
static BOOL     groupMobile;
static BOOL groupUnavailable;
static BOOL     groupAway;
static BOOL     groupIdle;
static BOOL groupIdleAndAway;
static BOOL     sortIdleTime;
static BOOL sortGroupsAlphabetically;

static BOOL     resolveAlphabetically;
static BOOL resolveAlphabeticallyByLastName;

static NSInteger  sortOrder[MAX_SORT_ORDER_DIMENSION];
static NSInteger  sizeOfSortOrder;

@interface ESStatusSort ()
- (void)configureControlDimming;
- (void)pruneAndSetSortOrderFromArray:(NSArray *)sortOrderArray;
@end

/*!
 * @class ESStatusSort
 * @brief AISortController to sort by contacts and groups
 *
 * Extensive configuration is allowed.
 */
@implementation ESStatusSort

/*!
 * @brief Did become active first time
 *
 * Called only once; gives the sort controller an opportunity to set defaults and load preferences lazily.
 */
- (void)didBecomeActiveFirstTime
{
        //Register our default preferences
        [adium.preferenceController registerDefaults:[NSDictionary dictionaryNamed:STATUS_SORT_DEFAULT_PREFS 
                                                                                                                                                forClass:[self class]] 
                                                                                  forGroup:PREF_GROUP_CONTACT_SORTING];
        
        //Load our preferences
        NSDictionary *prefDict = [adium.preferenceController preferencesForGroup:PREF_GROUP_CONTACT_SORTING];
        
        groupAvailable = [[prefDict objectForKey:KEY_GROUP_AVAILABLE] boolValue];
        groupMobile = [[prefDict objectForKey:KEY_GROUP_MOBILE] boolValue];
        groupUnavailable = [[prefDict objectForKey:KEY_GROUP_UNAVAILABLE] boolValue];
        
        groupAway = [[prefDict objectForKey:KEY_GROUP_AWAY] boolValue];
        groupIdle = [[prefDict objectForKey:KEY_GROUP_IDLE] boolValue];
        groupIdleAndAway = [[prefDict objectForKey:KEY_GROUP_IDLE_AND_AWAY] boolValue];
        
        sortIdleTime = [[prefDict objectForKey:KEY_SORT_IDLE_TIME] boolValue];
        sortGroupsAlphabetically = [[prefDict objectForKey:KEY_SORT_GROUPS_ALPHA] boolValue];
        
        resolveAlphabetically = [[prefDict objectForKey:KEY_RESOLVE_ALPHABETICALLY] boolValue];
        resolveAlphabeticallyByLastName = [[prefDict objectForKey:KEY_RESOLVE_BY_LAST_NAME] boolValue];
        
        [self pruneAndSetSortOrderFromArray:[prefDict objectForKey:KEY_SORT_ORDER]];
}

/*!
 * @brief Determines how the statusSort() method will operate.
 *
 * The sortOrder array, when it is done, contains, in order, the statuses which will be sorted upon.
 *
 * @param sortOrderArray An <tt>NSArray</tt> of <tt>NSNumber</tt>s whose values are Status_Sort_Type
 */
- (void)pruneAndSetSortOrderFromArray:(NSArray *)sortOrderArray
{
        NSNumber                *sortTypeNumber;
        NSInteger i;
        
        for (i = 0; i < MAX_SORT_ORDER_DIMENSION; i++) {
                sortOrder[i] = -1;
        }
        
        i = 0;
        
        //Enumerate the ordering array.  For all sort types which are valid given the active sorting types,
        //add to sortOrder[].  Finalize sortOrder with -1.
        
        BOOL    groupIdleOrIdleTime = (groupIdle || sortIdleTime);

        for (sortTypeNumber in sortOrderArray) {
                switch ([sortTypeNumber integerValue]) {
                        case Available: 
                                /* Group available if:
                                        Group available,
                                        Group all unavailable, or 
                                        Group separetely the idle and the away (such that the remaining alternative is Available)
                                */
                                if (groupAvailable || 
                                        groupUnavailable ||
                                        (/*!groupUnavailable &&*/ groupAway && groupIdleOrIdleTime)) sortOrder[i++] = Available;
                                break;
                                
                        case Away:
                                if (!groupUnavailable && groupAway) sortOrder[i++] = Away;
                                break;
                                
                        case Idle:
                                if ((!groupUnavailable && groupIdle) || sortIdleTime) sortOrder[i++] = Idle;
                                break;
                                
                        case Away_And_Idle:
                                if (!groupUnavailable && groupIdleAndAway) sortOrder[i++] = Away_And_Idle;
                                break;
                                
                        case Unavailable: 
                                //If one of groupAway or groupIdle is off, or we need a generic unavailable sort
                                if (groupUnavailable ||
                                        ((groupAvailable && (!groupAway || !groupIdleOrIdleTime)))) {
                                        sortOrder[i++] = Unavailable;
                                }
                                break;
                                
                        case Online:
                                /* Show Online category if:
                                        We aren't grouping all the available ones (this would imply grouping unavailable)
                                        We aren't grouping all the unavailable ones (this would imply grouping available)
                                        We aren't grouping both the away and the idle ones (this would imply grouping available)
                                */
                                if (!groupAvailable && !groupUnavailable && !(groupAway && (groupIdleOrIdleTime))) {
                                        sortOrder[i++] = Online;
                                }
                                break;
                                
                        case Mobile:
                                if (groupAvailable && groupMobile) {
                                        sortOrder[i++] = Mobile;
                                }
                                break;
                }
        }
        
        sortOrder[i] = -1;
        
        sizeOfSortOrder = i;
        
        [tableView_sortOrder reloadData];
}

/*!
 * @brief Non-localized identifier
 */
- (NSString *)identifier{
    return @"by Status";
}

/*!
 * @brief Localized display name
 */
- (NSString *)displayName{
    return AILocalizedString(@"Sort Contacts by Status",nil);
}

/*!
 * @brief Properties which, when changed, should trigger a resort
 */
- (NSSet *)statusKeysRequiringResort{
        return [NSSet setWithObjects:@"Online",@"IsIdle",@"StatusType",@"IsMobile",nil];
}

/*!
 * @brief Attribute keys which, when changed, should trigger a resort
 */
- (NSSet *)attributeKeysRequiringResort{
        return [NSSet setWithObject:@"Display Name"];
}

/*!
 * @brief Can the user manually reorder when this sort controller is active?
 *
 * The status sort can sort within status groupings either manually or alphabetically. Only the former should allow user reordering.
 */
- (BOOL)canSortManually {
        return !resolveAlphabetically;
}

//Configuration
#pragma mark Configuration
/*!
 * @brief Window title when configuring the sort
 *
 * Subclasses should provide a title for configuring the sort only if configuration is possible.
 * @result Localized title. If nil, the menu item will be disabled.
 */
- (NSString *)configureSortWindowTitle{
        return AILocalizedString(@"Configure Status Sort",nil); 
}

/*!
 * @brief Nib name for configuration
 */
- (NSString *)configureNibName{
        return @"StatusSortConfiguration";
}

/*!
 * @brief View did load
 */
- (void)viewDidLoad
{
        [checkBox_groupAvailable setState:groupAvailable];
        [checkBox_groupMobileSeparately setState:groupMobile];
        [checkBox_groupAway setState:groupAway];
        [checkBox_groupIdle setState:groupIdle];
        [checkBox_groupIdleAndAway setState:groupIdleAndAway];
        [checkBox_sortIdleTime setState:sortIdleTime];
        [checkBox_sortGroupsAlphabetically setState:sortGroupsAlphabetically];
        
        [checkBox_alphabeticallyByLastName setState:resolveAlphabeticallyByLastName];
        
        [buttonCell_alphabetically setState:(resolveAlphabetically ? NSOnState : NSOffState)];
        [buttonCell_manually setState:(resolveAlphabetically ? NSOffState : NSOnState)];

        [buttonCell_allUnavailable setState:(groupUnavailable ? NSOnState : NSOffState)];
        [buttonCell_separateUnavailable setState:(groupUnavailable ? NSOffState : NSOnState)];
        
        [self configureControlDimming];
        
        [tableView_sortOrder setDataSource:self];
        [tableView_sortOrder setDelegate:self];
    [tableView_sortOrder registerForDraggedTypes:[NSArray arrayWithObject:STATUS_DRAG_TYPE]];
}

/*!
 * @brief Preference changed
 *
 * Sort controllers should live update as preferences change.
 */
- (IBAction)changePreference:(id)sender
{
        NSArray *sortOrderArray =  [adium.preferenceController preferenceForKey:KEY_SORT_ORDER
                                                                                                                                                group:PREF_GROUP_CONTACT_SORTING];
        if (sender == checkBox_groupAvailable) {
                groupAvailable = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:groupAvailable]
                                             forKey:KEY_GROUP_AVAILABLE
                                              group:PREF_GROUP_CONTACT_SORTING];

                [self configureControlDimming];
                
        } else if (sender == checkBox_groupMobileSeparately) {
                groupMobile = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:groupMobile]
                                             forKey:KEY_GROUP_MOBILE
                                              group:PREF_GROUP_CONTACT_SORTING];                
                
                //Ensure the mobile item is in our sort order array, as the old defaults didn't include it
                if ([sortOrderArray indexOfObject:[NSNumber numberWithInteger:Mobile]] == NSNotFound) {
                        NSMutableArray  *newSortOrderArray = [[sortOrderArray mutableCopy] autorelease];
                        [newSortOrderArray addObject:[NSNumber numberWithInteger:Mobile]];
                        
                        [adium.preferenceController setPreference:newSortOrderArray
                                                                                                 forKey:KEY_SORT_ORDER
                                                                                                  group:PREF_GROUP_CONTACT_SORTING];
                        
                        sortOrderArray = newSortOrderArray;
                }
                
        } else if (sender == checkBox_groupAway) {
                groupAway = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:groupAway]
                                             forKey:KEY_GROUP_AWAY
                                              group:PREF_GROUP_CONTACT_SORTING];                
        } else if (sender == checkBox_groupIdle) {
                groupIdle = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:groupIdle]
                                             forKey:KEY_GROUP_IDLE
                                              group:PREF_GROUP_CONTACT_SORTING];
                
        } else if (sender == checkBox_groupIdleAndAway) {
                groupIdleAndAway = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:groupIdleAndAway]
                                             forKey:KEY_GROUP_IDLE_AND_AWAY
                                              group:PREF_GROUP_CONTACT_SORTING];
                
        } else if (sender == checkBox_sortIdleTime) {
                sortIdleTime = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:sortIdleTime]
                                             forKey:KEY_SORT_IDLE_TIME
                                              group:PREF_GROUP_CONTACT_SORTING];                                
        } else if (sender == checkBox_sortGroupsAlphabetically) {
                sortGroupsAlphabetically = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:sortGroupsAlphabetically]
                                                                                   forKey:KEY_SORT_GROUPS_ALPHA
                                                                                        group:PREF_GROUP_CONTACT_SORTING];                                              
        } else if (sender == matrix_resolution) {
                id selectedCell = [sender selectedCell];
                
                resolveAlphabetically = (selectedCell == buttonCell_alphabetically);
                [adium.preferenceController setPreference:[NSNumber numberWithBool:resolveAlphabetically]
                                                                                         forKey:KEY_RESOLVE_ALPHABETICALLY
                                                                                          group:PREF_GROUP_CONTACT_SORTING];
                
                [self configureControlDimming];
                
        } else if (sender == matrix_unavailableGrouping) {
                id selectedCell = [sender selectedCell];
                
                groupUnavailable = (selectedCell == buttonCell_allUnavailable);
                [adium.preferenceController setPreference:[NSNumber numberWithBool:groupUnavailable]
                                                                                         forKey:KEY_GROUP_UNAVAILABLE
                                                                                          group:PREF_GROUP_CONTACT_SORTING];
                
                [self configureControlDimming];
                
        } else if (sender == checkBox_alphabeticallyByLastName) {
                resolveAlphabeticallyByLastName = [sender state];
                [adium.preferenceController setPreference:[NSNumber numberWithBool:resolveAlphabeticallyByLastName]
                                             forKey:KEY_RESOLVE_BY_LAST_NAME
                                              group:PREF_GROUP_CONTACT_SORTING];
        }
        
        [self pruneAndSetSortOrderFromArray:sortOrderArray];
        
        [adium.contactController sortContactList];
}

/*!
 * @brief Configure control dimming
 */
- (void)configureControlDimming
{
        [checkBox_alphabeticallyByLastName setEnabled:resolveAlphabetically];
        [checkBox_groupAway setEnabled:!groupUnavailable];
        [checkBox_groupIdle setEnabled:!groupUnavailable];
        [checkBox_groupIdleAndAway setEnabled:!groupUnavailable];
        
        [checkBox_groupMobileSeparately setEnabled:groupAvailable];
}

#pragma mark Sort Order Tableview datasource
/*!
 * @brief Table view number of rows
 */
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
        return sizeOfSortOrder;
}

/*!
 * @brief Table view object value
 */
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
        switch (sortOrder[rowIndex]) {
                case Available:
                        return AVAILABLE;
                        break;
                        
                case Away:
                        return AWAY;
                        break;
                        
                case Idle:
                        return IDLE;
                        break;
                        
                case Away_And_Idle:
                        return AWAY_AND_IDLE;
                        break;
                        
                case Unavailable:
                        //Unavailable is always the same sort, but to the user it can be either "Unavailable" or "Other Unavailable"
                        //depending upon what other options are active.  The test here is purely cosmetic.
                        return ((!sortIdleTime && (groupUnavailable || !(groupAway || groupIdle || groupIdleAndAway))) ?
                                        UNAVAILABLE :
                                        OTHER_UNAVAILABLE);
                        break;
                
                case Online:
                        return ONLINE;
                        break;
                        
                case Mobile:
                        return MOBILE;
                        break;
        }
        
        return @"";
}

/*!
 * @brief The NSNumber Status_Sort_Type which corresponds to a string
 *
 * @param string A string such as AVAILABLE or AWAY (localized)
 * @result The NSNumber Status_Sort_Type which corresponds to the string 
 */
- (NSNumber *)numberForString:(NSString *)string
{
        NSInteger equivalent = -1;

        if ([string isEqualToString:AVAILABLE]) {
                equivalent = Available;
        } else if ([string isEqualToString:AWAY]) {
                equivalent = Away;
        } else if ([string isEqualToString:IDLE]) {
                equivalent = Idle;
        } else if ([string isEqualToString:AWAY_AND_IDLE]) {
                equivalent = Away_And_Idle;
        } else if ([string isEqualToString:UNAVAILABLE] || ([string isEqualToString:OTHER_UNAVAILABLE])) {
                equivalent = Unavailable;
        } else if ([string isEqualToString:ONLINE]) {
                equivalent = Online;
        } else if ([string isEqualToString:MOBILE]) {
                equivalent = Mobile;
        }
        
        return [NSNumber numberWithInteger:equivalent];
}

/*!
 * @brief Table view write rows
 */
-  (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray *)rows toPasteboard:(NSPasteboard *)pboard
{
    [pboard declareTypes:[NSArray arrayWithObject:STATUS_DRAG_TYPE] owner:self];
        
    //Build a list of all the highlighted aways
    NSString    *dragItem = [self tableView:tableView
                                  objectValueForTableColumn:nil
                                                                                row:[[rows objectAtIndex:0] integerValue]];
        
    //put it on the pasteboard
    [pboard setString:dragItem forType:STATUS_DRAG_TYPE];
        
    return YES;
}

/*!
 * @brief Table view validate drop
 */
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation
{
    NSString    *avaliableType = [[info draggingPasteboard] availableTypeFromArray:[NSArray arrayWithObject:STATUS_DRAG_TYPE]];

        if ([avaliableType isEqualToString:STATUS_DRAG_TYPE]) {
        if (operation == NSTableViewDropAbove && row != -1) {
            return NSDragOperationMove;
        } else {
            return NSDragOperationNone;
                }
        }
        
    return NSDragOperationNone;
}

/*!
 * @brief Table view accept drop
 */
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSString            *availableType = [[info draggingPasteboard] availableTypeFromArray:[NSArray arrayWithObject:STATUS_DRAG_TYPE]];

    if ([availableType isEqualToString:STATUS_DRAG_TYPE]) {
                NSString                *item = [[info draggingPasteboard] stringForType:STATUS_DRAG_TYPE];
                
                //Remember, sortOrderPref contains all possible sorting types, not just the ones presently visible in the table!
                NSMutableArray  *sortOrderPref = [[adium.preferenceController preferenceForKey:KEY_SORT_ORDER
                                                                                                                                                                   group:PREF_GROUP_CONTACT_SORTING] mutableCopy];
                NSNumber                *sortNumber = [self numberForString:item];
                
                //Remove it from our array
                [sortOrderPref removeObject:sortNumber];
                
                if (row == [tableView numberOfRows]) {
                        //Dropped at the bottom
                        [sortOrderPref addObject:sortNumber];
                } else {
                        //Find the object which will end up just below it
                        NSInteger targetIndex = [sortOrderPref indexOfObject:[self numberForString:[self tableView:tableView
                                                                                                                                                 objectValueForTableColumn:nil
                                                                                                                                                                                           row:row]]];
                        if (targetIndex != NSNotFound) {
                                //Insert it there
                                [sortOrderPref insertObject:sortNumber atIndex:targetIndex];
                        } else {
                                //Dropped at the bottom
                                [sortOrderPref addObject:sortNumber];
                        }
                }
                
                [adium.preferenceController setPreference:sortOrderPref
                                                                                         forKey:KEY_SORT_ORDER
                                                                                          group:PREF_GROUP_CONTACT_SORTING];
                
                [self pruneAndSetSortOrderFromArray:sortOrderPref];             
                
                //Select and scroll to the dragged object
                [tableView reloadData];
                
                [adium.contactController sortContactList];

                [sortOrderPref release];
        }
        
        
    return YES;
}


#pragma mark Sorting

/*!
 * @brief The status sort method itself
 *
 * It's magic... but it's efficient magic!
 */
NSInteger statusSort(id objA, id objB, BOOL groups, id<AIContainingObject> container)
{
        AIListObject *objectA = (AIListObject *)objA;
        AIListObject *objectB = (AIListObject *)objB;
        if (groups) {
                if (sortGroupsAlphabetically) {
                        return [((AIListObject *)objectA).displayName compare:((AIListObject *)objectB).displayName];
                } else {
                        //Keep groups in manual order if set to do so.
                        if ([container orderIndexForObject:objectA] > [container orderIndexForObject:objectB]) {
                                return NSOrderedDescending;
                        } else {
                                return NSOrderedAscending;
                        }
                }
                
        } else {
                AIStatusSummary statusSummaryA = [objectA statusSummary];
                AIStatusSummary statusSummaryB = [objectB statusSummary];
                
                //Always sort offline contacts to the bottom
                BOOL onlineA = (statusSummaryA != AIOfflineStatus);
                BOOL onlineB = (statusSummaryB != AIOfflineStatus);
                if (!onlineB && onlineA) {
                        return NSOrderedAscending;
                } else if (!onlineA && onlineB) {
                        return NSOrderedDescending;
                }
                
                //We only need to start looking at status for sorting if both are online; 
                //otherwise, skip to resolving alphabetically or manually
                if (onlineA && onlineB) {
                        NSUInteger      i = 0;
                        BOOL                    away[2];
                        BOOL                    mobile[2];
                        BOOL                    definitelyFinishedIfSuccessful, onlyIfWeAintGotNothinBetter, status;
                        NSInteger                               idle[2];
                        NSInteger                               sortIndex[2];
                        NSInteger                               objectCounter;
                        
                        //Get the away state and idle times now rather than potentially doing each twice below
                        away[0] = ((statusSummaryA == AIAwayStatus) || (statusSummaryA == AIAwayAndIdleStatus));
                        away[1] = ((statusSummaryB == AIAwayStatus) || (statusSummaryB == AIAwayAndIdleStatus));
                        
                        idle[0] = (((statusSummaryA == AIIdleStatus) || (statusSummaryA == AIAwayAndIdleStatus)) ?
                                           objectA.idleTime :
                                           0);
                        idle[1] = (((statusSummaryB == AIIdleStatus) || (statusSummaryB == AIAwayAndIdleStatus)) ?
                                           objectB.idleTime :
                                           0);
                        
                        if (groupMobile) {
                                mobile[0] = [objectA isMobile];
                                mobile[1] = [objectB isMobile];
                        }

                        for (objectCounter = 0; objectCounter < 2; objectCounter++) {
                                sortIndex[objectCounter] = 999;

                                for (i = 0; i < sizeOfSortOrder ; i++) {
                                        //Reset the internal bookkeeping
                                        onlyIfWeAintGotNothinBetter = NO;
                                        definitelyFinishedIfSuccessful = NO;
                                        
                                        //Determine the state for the status this level of sorting cares about
                                        switch (sortOrder[i]) {
                                                case Available:
                                                        status = (!away[objectCounter] && !idle[objectCounter]); // TRUE if A is available
                                                        break;
                                                
                                                case Mobile:
                                                        status = mobile[objectCounter];
                                                        definitelyFinishedIfSuccessful = YES;
                                                        break;
                                                
                                                case Away:
                                                        status = away[objectCounter];
                                                        break;

                                                case Idle:
                                                        status = (idle[objectCounter] != 0);
                                                        break;

                                                case Away_And_Idle:
                                                        status =  away[objectCounter] && (idle[objectCounter] != 0);
                                                        definitelyFinishedIfSuccessful = YES;
                                                        break;
                                                        
                                                case Unavailable:
                                                        status =  away[objectCounter] || (idle[objectCounter] != 0);
                                                        onlyIfWeAintGotNothinBetter = YES;
                                                        break;
                                                        
                                                case Online:
                                                        status = YES; //we can only get here if the person is online, anyways
                                                        onlyIfWeAintGotNothinBetter = YES;
                                                        break;
                                                
                                                default:
                                                        status = NO;
                                        }

                                        //If the object has the desired status and we want to use it, store the new index it should go to
                                        if (status &&
                                                (!onlyIfWeAintGotNothinBetter || (sortIndex[objectCounter] == 999))) {
                                                sortIndex[objectCounter] = i;
                                                
                                                //If definitelyFinishedIfSuccessful is YES, we're done sorting as soon as something fits
                                                //this category
                                                if (definitelyFinishedIfSuccessful) break;
                                        }
                                }
                        } //End for object loop

                        if (sortIndex[0] > sortIndex[1]) {
                                return NSOrderedDescending;
                        } else if (sortIndex[1] > sortIndex[0]) {
                                return NSOrderedAscending;                      
                        }
                        
                        //If one idle time is greater than the other and we want to sort on that basis, we have an ordering
                        if (sortIdleTime) {
                                //Ordering is determined if either has a idle time and their idle times are not identical
                                if (((idle[0] != 0) || (idle[1] != 0)) && (idle[0] != idle[1])) {
                                        if (idle[0] > idle[1]) {
                                                return NSOrderedDescending;
                                        } else {
                                                return NSOrderedAscending;
                                        }
                                }
                        }
                }
                
                if (!resolveAlphabetically) {
                        //If we don't want to resolve alphabetically, we do want to resolve by manual ordering if possible
                        CGFloat orderIndexA = [container orderIndexForObject:objectA];
                        CGFloat orderIndexB = [container orderIndexForObject:objectB];
                        
                        if (orderIndexA > orderIndexB) {
                                return NSOrderedDescending;
                        } else if (orderIndexA < orderIndexB) {
                                return NSOrderedAscending;
                        }
                }
                
                //If we made it here, resolve the ordering alphabetically, which is guaranteed to be consistent.
                //Note that this sort should -never- return NSOrderedSame, so as a last resort we use the internalObjectID.
                NSComparisonResult returnValue;
                
                if (resolveAlphabeticallyByLastName) {
                        //Split the displayname into parts by spacing and use the last part, the "last name," for comparison
                        NSString        *space = @" ";
                        NSString        *displayNameA = [objectA displayName];
                        NSString        *displayNameB = [objectB displayName];
                        NSArray         *componentsA = [displayNameA componentsSeparatedByString:space];
                        NSArray         *componentsB = [displayNameB componentsSeparatedByString:space];
                        
                        returnValue = [[componentsA lastObject] caseInsensitiveCompare:[componentsB lastObject]];
                        //If the last names are the same, compare the whole object, which will amount to sorting these objects
                        //by first name
                        if (returnValue == NSOrderedSame) {
                                returnValue = [displayNameA caseInsensitiveCompare:displayNameB];
                                if (returnValue == NSOrderedSame) {
                                        returnValue = [[objectA internalObjectID] caseInsensitiveCompare:[objectB internalObjectID]];
                                }
                        }
                } else {
                        returnValue = [[objectA longDisplayName] caseInsensitiveCompare:[objectB longDisplayName]];
                        if (returnValue == NSOrderedSame) {
                                returnValue = [[objectA internalObjectID] caseInsensitiveCompare:[objectB internalObjectID]];
                        }
                }
                
                return (returnValue);
        }
}

/*!
 * @brief Sort function
 */
- (sortfunc)sortFunction{
        return &statusSort;
}

@end