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 / ESFileTransferProgressWindowController.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
/*
 * 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 "ESFileTransferProgressRow.h"
#import "ESFileTransferProgressView.h"
#import "ESFileTransferProgressWindowController.h"
#import "ESFileTransfer.h"
#import <AIUtilities/AIVariableHeightOutlineView.h>
#import <AIUtilities/AIArrayAdditions.h>
#import <AIUtilities/AIGenericViewCell.h>

#define FILE_TRANSFER_PROGRESS_NIB                      @"FileTransferProgressWindow"
#define KEY_TRANSFER_PROGRESS_WINDOW_FRAME      @"Transfer Progress Window Frame"

@interface ESFileTransferProgressWindowController ()
- (void)addFileTransfer:(ESFileTransfer *)fileTransfer;
- (void)updateStatusBar;
- (void)reloadAllData;
- (void)_removeFileTransfer:(ESFileTransfer *)inFileTransfer;
- (ESFileTransferProgressRow *)existingRowForFileTransfer:(ESFileTransfer *)inFileTransfer;
@end

@interface ESFileTransferController ()
- (void)_removeFileTransfer:(ESFileTransfer *)fileTransfer;
@end

#ifndef NSAppKitVersionNumber10_3
#       define NSTableViewUniformColumnAutoresizingStyle 1
#endif

@implementation ESFileTransferProgressWindowController

static ESFileTransferProgressWindowController *sharedTransferProgressInstance = nil;

//Return the shared contact info window
#pragma mark Class Methods
+ (id)sharedTransferProgressWindowController
{
        //Create the window
    if (!sharedTransferProgressInstance) {
        sharedTransferProgressInstance = [[self alloc] initWithWindowNibName:FILE_TRANSFER_PROGRESS_NIB];
        }

        return sharedTransferProgressInstance;
}

+ (id)showFileTransferProgressWindow
{
        //Configure and show window
        [[self sharedTransferProgressWindowController] showWindow:nil];

        return (sharedTransferProgressInstance);
}

+ (id)showFileTransferProgressWindowIfNotOpen
{
        [[[self sharedTransferProgressWindowController] window] orderFront:nil];
        
        return (sharedTransferProgressInstance);
}

//Close the info window
+ (void)closeTransferProgressWindow
{
    if (sharedTransferProgressInstance) {
        [sharedTransferProgressInstance closeWindow:nil];
    }
}

+ (void)removeFileTransfer:(ESFileTransfer *)inFileTransfer
{
    if (sharedTransferProgressInstance) {
        [sharedTransferProgressInstance _removeFileTransfer:inFileTransfer];
    }
}
//init
#pragma mark Basic window controller functionality
- (id)initWithWindowNibName:(NSString *)windowNibName
{
    if ((self = [super initWithWindowNibName:windowNibName])) {
                progressRows = [[NSMutableArray alloc] init];
                
                [self.window setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
                [self.window setContentBorderThickness:24.0 forEdge: NSMinYEdge];
        }
        
        return self;
}

- (void)dealloc
{
        [[NSNotificationCenter defaultCenter] removeObserver:self];

        [progressRows release]; progressRows = nil;

    [super dealloc];
}

- (NSString *)adiumFrameAutosaveName
{
        return KEY_TRANSFER_PROGRESS_WINDOW_FRAME;
}

//Setup the window before it is displayed
- (void)windowDidLoad
{
        NSEnumerator    *enumerator;
        ESFileTransfer  *fileTransfer;

        //Set the localized title
        [[self window] setTitle:AILocalizedString(@"File Transfers",nil)];

        //There's already a menu item in the Window menu; no reason to duplicate it
        [[self window] setExcludedFromWindowsMenu:YES];

        //Configure the scroll view
        [scrollView setHasVerticalScroller:YES];
        [scrollView setHasHorizontalScroller:NO];
        [[scrollView contentView] setCopiesOnScroll:NO];
        if ([scrollView respondsToSelector:@selector(setAutohidesScrollers:)]) {
                [scrollView setAutohidesScrollers:YES];
        }

        //Configure the outline view
        [[[outlineView tableColumns] objectAtIndex:0] setDataCell:[[[AIGenericViewCell alloc] init] autorelease]];

        [outlineView sizeLastColumnToFit];
        [outlineView setAutoresizesSubviews:YES];
        [outlineView setColumnAutoresizingStyle:NSTableViewUniformColumnAutoresizingStyle];
        [outlineView setUsesAlternatingRowBackgroundColors:YES];
        [outlineView setDataSource:self];
        [outlineView setDelegate:self];

        //Set up and size our Clear button
        {
                NSRect  newFrame, oldFrame;
                
                //Clear
                [button_clear setAutoresizingMask:(NSViewMaxXMargin | NSViewMaxYMargin)];

                oldFrame = [button_clear frame];
                [button_clear setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
                [button_clear setTitle:AILocalizedString(@"Clear",nil)];
                [button_clear sizeToFit];
                newFrame = [button_clear frame];
                
                //Don't let the button get smaller than it was initially
                if (newFrame.size.width < oldFrame.size.width) newFrame.size.width = oldFrame.size.width;
                
                //Keep the origin and height the same - we just want to size for width
                newFrame.origin = oldFrame.origin;
                newFrame.size.height = oldFrame.size.height;
                [button_clear setFrame:newFrame];
                [button_clear setNeedsDisplay:YES];

                //Resize the status bar text
                NSInteger widthChange = oldFrame.size.width - newFrame.size.width;
                if (widthChange) {
                        NSRect  statusFrame;
                        
                        statusFrame = [textField_statusBar frame];
                        statusFrame.origin.x += widthChange;
                        statusFrame.size.width -= widthChange;
                        [textField_statusBar setFrame:statusFrame];
                        [textField_statusBar setNeedsDisplay:YES];
                }
        }
        
        [outlineView accessibilitySetOverrideValue:AILocalizedString(@"File Transfers", nil)
                                                                  forAttribute:NSAccessibilityDescriptionAttribute];

        //Call super's implementation
        [super windowDidLoad];

        //Observe for new file transfers
        [[NSNotificationCenter defaultCenter] addObserver:self
                                   selector:@selector(newFileTransfer:)
                                       name:FileTransfer_NewFileTransfer
                                                                         object:nil];
        
        //Create progress rows for all existing file transfers
        shouldScrollToNewFileTransfer = NO;
        enumerator = [[adium.fileTransferController fileTransferArray] objectEnumerator];
        while ((fileTransfer = [enumerator nextObject])) {
                [self addFileTransfer:fileTransfer];
        }
        
        //Go time
        [self reloadAllData];
        
        shouldScrollToNewFileTransfer = YES;
        [outlineView scrollRectToVisible:[outlineView rectOfRow:([progressRows count]-1)]];
}

//called as the window closes
- (void)windowWillClose:(id)sender
{
        [super windowWillClose:sender];

        //release the window controller (ourself)
    sharedTransferProgressInstance = nil;
    [self autorelease];
}

- (void)configureControlDimming
{       
        ESFileTransferProgressRow       *row;
        BOOL                                            enableClear = NO;
        
        for (row in progressRows) {
                if ([[row fileTransfer] isStopped]) {
                        enableClear = YES;
                        break;
                }
        }
        
        [button_clear setEnabled:enableClear];
}

//Called when a progress row has loaded its view and is ready to be added to our window
#pragma mark Progress row addition to the window
- (void)progressRowDidAwakeFromNib:(ESFileTransferProgressRow *)progressRow
{
        if (![progressRows containsObjectIdenticalTo:progressRow]) {
                [progressRows addObject:progressRow];
        }

        if (shouldScrollToNewFileTransfer) {
                [self reloadAllData];
                
                [outlineView scrollRectToVisible:[outlineView rectOfRow:[progressRows indexOfObject:progressRow]]];
        }
}

#pragma mark Progress row details twiddle
//Called when the file transfer view's twiddle is clicked.
- (void)fileTransferProgressRow:(ESFileTransferProgressRow *)progressRow
                          heightChangedFrom:(CGFloat)oldHeight
                                                         to:(CGFloat)newHeight
{
        if (shouldScrollToNewFileTransfer) {
                [self reloadAllData];
                
                [outlineView scrollRectToVisible:[outlineView rectOfRow:[progressRows indexOfObject:progressRow]]];
        }
}

#pragma mark Adding file transfers
//Notification of a new file transfer; add it to the window
- (void)newFileTransfer:(NSNotification *)notification
{
        ESFileTransfer  *fileTransfer;

        if ((fileTransfer = [notification object])) {
                [self addFileTransfer:fileTransfer];
        }
}

//Add a file transfer's progress row if we don't already have one for the fileTransfer.
//This will call back on progressRowDidAwakeFromNib: if it adds a new row.
- (void)addFileTransfer:(ESFileTransfer *)inFileTransfer
{
        if (![self existingRowForFileTransfer:inFileTransfer]) {
                [ESFileTransferProgressRow rowForFileTransfer:inFileTransfer withOwner:self];
        }
}

- (void)_removeFileTransfer:(ESFileTransfer *)inFileTransfer
{
        ESFileTransferProgressRow       *row;

        if ((row = [self existingRowForFileTransfer:inFileTransfer])) [self _removeFileTransferRow:row];
}

- (ESFileTransferProgressRow *)existingRowForFileTransfer:(ESFileTransfer *)inFileTransfer
{
        ESFileTransferProgressRow       *row;

        for (row in progressRows) {
                if ([row fileTransfer] == inFileTransfer) break;
        }

        return row;
}

//Remove a file transfer row from the window. This is coupled to the file transfer controller; care must be taken
//that we don't remove a row which is in progress, as this will remove the file transfer controller's tracking of it.
//This must be done so we don't see the file transfer again if the progress window is closed and then reopened.
- (void)_removeFileTransferRow:(ESFileTransferProgressRow *)progressRow
{
        ESFileTransfer  *fileTransfer = [progressRow fileTransfer];

        if ([fileTransfer isStopped]) {
                NSClipView              *clipView = [scrollView contentView];
                NSUInteger              row;

                //Protect
                [progressRow retain];

                //Remove the row from our array, and its file transfer from the fileTransferController
                row = [progressRows indexOfObject:progressRow];
                [progressRows removeObject:progressRow];
                [adium.fileTransferController _removeFileTransfer:fileTransfer];
                
                if (shouldScrollToNewFileTransfer) {
                        //Refresh the outline view
                        [self reloadAllData];
                        
                        //Determine the row to reselect.  If the current row is valid, keep it.  If it isn't, use the last row.
                        if (row >= [progressRows count]) {
                                row = [progressRows count] - 1;
                        }
                        [clipView scrollToPoint:[clipView constrainScrollPoint:([outlineView rectOfRow:row].origin)]];
                        
                        [self updateStatusBar];
                }
                
                //Clean up
                [progressRow release];
        }
}

#pragma mark Status bar
//Called when a progress row changes its type, typically from Unknown to either Incoming or Outgoing
- (void)progressRowDidChangeType:(ESFileTransferProgressRow *)progressRow
{
        /* We get here as a progress row intializes itself, before it claims to be ready for display and therefore before
         * we have it in the progressRows array.  Add it now if necessary */
        if (![progressRows containsObjectIdenticalTo:progressRow]) {
                [progressRows addObject:progressRow];
        }
        
        [self updateStatusBar];
}

- (void)progressRowDidChangeStatus:(ESFileTransferProgressRow *)progressRow
{
        [self configureControlDimming];
}

//Update the status bar at the bottom of the window
- (void)updateStatusBar
{
        ESFileTransferProgressRow       *aRow;
        NSString                                        *statusBarString, *downloadsString = nil, *uploadsString = nil;
        NSUInteger                                      downloads = 0, uploads = 0;
        
        for (aRow in progressRows) {
                AIFileTransferType type = [aRow type];
                if (type == Incoming_FileTransfer) {
                        downloads++;
                } else if (type == Outgoing_FileTransfer) {
                        uploads++;
                }
        }

        if (downloads > 0) {
                if (downloads == 1)
                        downloadsString = AILocalizedString(@"1 download",nil);
                else
                        downloadsString = [NSString stringWithFormat:AILocalizedString(@"%lu downloads","(number) downloads"), downloads];
        }

        if (uploads > 0) {
                if (uploads == 1)
                        uploadsString = AILocalizedString(@"1 upload",nil);
                else
                        uploadsString = [NSString stringWithFormat:AILocalizedString(@"%lu uploads","(number) uploads"), uploads];
        }

        if (downloadsString && uploadsString) {
                statusBarString = [NSString stringWithFormat:@"%@; %@",downloadsString,uploadsString];
        } else if (downloadsString) {
                statusBarString = downloadsString;
        } else if (uploadsString) {
                statusBarString = uploadsString;
        } else {
                statusBarString = @"";
        }

        [textField_statusBar setStringValue:statusBarString];
}

- (IBAction)clearAllCompleteTransfers:(id)sender
{
        NSEnumerator                            *enumerator;
        ESFileTransferProgressRow       *row;
        
        shouldScrollToNewFileTransfer = NO;
        enumerator = [[[progressRows copy] autorelease] objectEnumerator];
        while ((row = [enumerator nextObject])) {
                if ([[row fileTransfer] isStopped]) [self _removeFileTransferRow:row];
        }       
        shouldScrollToNewFileTransfer = YES;
        
        [self reloadAllData];

        [outlineView scrollRectToVisible:[outlineView rectOfRow:0]];    
}

#pragma mark OutlineView dataSource
- (id)outlineView:(NSOutlineView *)inOutlineView child:(NSInteger)index ofItem:(id)item
{
        if (index < [progressRows count]) {
                return [progressRows objectAtIndex:index];
        } else {
                return nil;
        }
}

- (NSInteger)outlineView:(NSOutlineView *)inOutlineView numberOfChildrenOfItem:(id)item
{
        return [progressRows count];
}

//No items are expandable for the outline view
- (BOOL)outlineView:(NSOutlineView *)inOutlineView isItemExpandable:(id)item
{
        return NO;
}

//We don't use object values
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
        return @"";
}

//Each row should be the height of its item's view
- (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
{
        NSView *view = [(ESFileTransferProgressRow *)item view];
        
        return (view ? [view frame].size.height : 0);
}

//Before a cell is display, set its embedded view
- (void)outlineView:(NSOutlineView *)inOutlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
        [cell setEmbeddedView:[(ESFileTransferProgressRow *)item view]];
}

#pragma mark Outline view delegate
- (void)outlineViewDeleteSelectedRows:(NSOutlineView *)inOutlineView
{
        NSInteger               row = [inOutlineView selectedRow];
        BOOL    didDelete = NO;
        if (row != -1) {
                ESFileTransferProgressRow       *progressRow = [inOutlineView itemAtRow:row];
                if ([[progressRow fileTransfer] isStopped]) {
                        [self _removeFileTransferRow:progressRow];
                        didDelete = YES;
                }
        }

        //If they tried to delete a row that isn't finished, or we got here with no valid selection, sound the system beep
        if (!didDelete) {
                NSBeep();
        } else {
                [inOutlineView deselectAll:nil];
        }
}

- (NSMenu *)outlineView:(NSOutlineView *)inOutlineView menuForEvent:(NSEvent *)inEvent
{
        NSMenu  *menu = nil;
    NSPoint     location;
    NSInteger           row;

    //Get the clicked item
    location = [inOutlineView convertPoint:[inEvent locationInWindow]
                                                                  fromView:nil];
    row = [inOutlineView rowAtPoint:location];

        if (row != -1) {
                ESFileTransferProgressRow       *progressRow = [inOutlineView itemAtRow:row];
                menu = [progressRow menuForEvent:inEvent];
        }

        return menu;
}

/*!
 * @brief Reload all data
 *
 * After removing the subviews of the outline view, reload the data.
 * Next, ensure the height of the outline view is still correct.
 * Finally, update our display and associated controls.
 */
- (void)reloadAllData
{
        [[[[outlineView subviews] copy] autorelease] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [outlineView reloadData];

        NSRect  outlineFrame = [outlineView frame];
        NSInteger               totalHeight = [outlineView totalHeight];

        if (outlineFrame.size.height != totalHeight) {
                outlineFrame.size.height = totalHeight;
                [outlineView setFrame:outlineFrame];
                [outlineView setNeedsDisplay:YES];
        }

        //Update our status bar
        [self updateStatusBar];

        //Enable/disable our controls
        [self configureControlDimming];
}

#pragma mark Window zoom
//Size for window zoom
- (NSRect)windowWillUseStandardFrame:(NSWindow *)inWindow defaultFrame:(NSRect)defaultFrame
{
        NSRect  oldWindowFrame = [inWindow frame];
        NSRect  windowFrame = oldWindowFrame;
        NSSize  minSize = [inWindow minSize];
        NSSize  maxSize = [inWindow maxSize];

        //Take the desired height and add the parts of the window which aren't in the scrollView.
        NSInteger desiredHeight = ([outlineView totalHeight] + (windowFrame.size.height - [scrollView frame].size.height));

        windowFrame.size.height = desiredHeight;
        windowFrame.size.width = 300;

        //Respect the min and max sizes
        if (windowFrame.size.width < minSize.width) windowFrame.size.width = minSize.width;
        if (windowFrame.size.height < minSize.height) windowFrame.size.height = minSize.height;
        if (windowFrame.size.width > maxSize.width) windowFrame.size.width = maxSize.width;
        if (windowFrame.size.height > maxSize.height) windowFrame.size.height = maxSize.height;

        //Keep the top-left corner the same
        windowFrame.origin.y = oldWindowFrame.origin.y + oldWindowFrame.size.height - windowFrame.size.height;

    return windowFrame;
}

@end