source: trunk/macosx/StatusBarController.m @ 12687

Last change on this file since 12687 was 12687, checked in by livings124, 11 years ago

#4423 Fix coding style of StatusBarController::updateWithDownload:upload:. Patch by Benjamin.

  • Property svn:keywords set to Date Rev Author Id
File size: 9.6 KB
Line 
1/******************************************************************************
2 * $Id: StatusBarController.m 12687 2011-08-15 11:16:44Z livings124 $
3 *
4 * Copyright (c) 2011 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "StatusBarController.h"
26#import "NSStringAdditions.h"
27
28#import "transmission.h"
29
30#define STATUS_RATIO_TOTAL      @"RatioTotal"
31#define STATUS_RATIO_SESSION    @"RatioSession"
32#define STATUS_TRANSFER_TOTAL   @"TransferTotal"
33#define STATUS_TRANSFER_SESSION @"TransferSession"
34
35typedef enum
36{
37    STATUS_RATIO_TOTAL_TAG = 0,
38    STATUS_RATIO_SESSION_TAG = 1,
39    STATUS_TRANSFER_TOTAL_TAG = 2,
40    STATUS_TRANSFER_SESSION_TAG = 3
41} statusTag;
42
43@interface StatusBarController (Private)
44
45- (void) resizeStatusButton;
46
47@end
48
49@implementation StatusBarController
50
51- (id) initWithLib: (tr_session *) lib
52{
53    if ((self = [super initWithNibName: @"StatusBar" bundle: nil]))
54    {
55        fLib = lib;
56       
57        fPreviousDownloadRate = -1.0;
58        fPreviousUploadRate = -1.0;
59    }
60   
61    return self;
62}
63
64- (void) awakeFromNib
65{
66    //localize menu items
67    [[[fStatusButton menu] itemWithTag: STATUS_RATIO_TOTAL_TAG] setTitle: NSLocalizedString(@"Total Ratio",
68        "Status Bar -> status menu")];
69    [[[fStatusButton menu] itemWithTag: STATUS_RATIO_SESSION_TAG] setTitle: NSLocalizedString(@"Session Ratio",
70        "Status Bar -> status menu")];
71    [[[fStatusButton menu] itemWithTag: STATUS_TRANSFER_TOTAL_TAG] setTitle: NSLocalizedString(@"Total Transfer",
72        "Status Bar -> status menu")];
73    [[[fStatusButton menu] itemWithTag: STATUS_TRANSFER_SESSION_TAG] setTitle: NSLocalizedString(@"Session Transfer",
74        "Status Bar -> status menu")];
75   
76    [[fStatusButton cell] setBackgroundStyle: NSBackgroundStyleRaised];
77    [[fTotalDLField cell] setBackgroundStyle: NSBackgroundStyleRaised];
78    [[fTotalULField cell] setBackgroundStyle: NSBackgroundStyleRaised];
79   
80    [self updateSpeedFieldsToolTips];
81   
82    //update when speed limits are changed
83    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateSpeedFieldsToolTips)
84        name: @"SpeedLimitUpdate" object: nil];
85    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeStatusButton)
86        name: NSWindowDidResizeNotification object: [[self view] window]];
87}
88
89- (void) dealloc
90{
91    [[NSNotificationCenter defaultCenter] removeObserver: self];
92   
93    [super dealloc];
94}
95
96- (void) updateWithDownload: (CGFloat) dlRate upload: (CGFloat) ulRate
97{
98    //set rates
99    if (dlRate != fPreviousDownloadRate)
100    {
101        [fTotalDLField setStringValue: [NSString stringForSpeed: dlRate]];
102        fPreviousDownloadRate = dlRate;
103    }
104   
105    if (ulRate != fPreviousUploadRate)
106    {
107        [fTotalULField setStringValue: [NSString stringForSpeed: ulRate]];
108        fPreviousUploadRate = ulRate;
109    }
110   
111    //set status button text
112    NSString * statusLabel = [[NSUserDefaults standardUserDefaults] stringForKey: @"StatusLabel"], * statusString;
113    BOOL total;
114    if ((total = [statusLabel isEqualToString: STATUS_RATIO_TOTAL]) || [statusLabel isEqualToString: STATUS_RATIO_SESSION])
115    {
116        tr_session_stats stats;
117        if (total)
118            tr_sessionGetCumulativeStats(fLib, &stats);
119        else
120            tr_sessionGetStats(fLib, &stats);
121       
122        statusString = [NSLocalizedString(@"Ratio", "status bar -> status label") stringByAppendingFormat: @": %@",
123                        [NSString stringForRatio: stats.ratio]];
124    }
125    else //STATUS_TRANSFER_TOTAL or STATUS_TRANSFER_SESSION
126    {
127        total = [statusLabel isEqualToString: STATUS_TRANSFER_TOTAL];
128       
129        tr_session_stats stats;
130        if (total)
131            tr_sessionGetCumulativeStats(fLib, &stats);
132        else
133            tr_sessionGetStats(fLib, &stats);
134       
135        statusString = [NSString stringWithFormat: @"%@: %@  %@: %@",
136                NSLocalizedString(@"DL", "status bar -> status label"), [NSString stringForFileSize: stats.downloadedBytes],
137                NSLocalizedString(@"UL", "status bar -> status label"), [NSString stringForFileSize: stats.uploadedBytes]];
138    }
139   
140   
141    if (![[fStatusButton title] isEqualToString: statusString])
142    {
143        [fStatusButton setTitle: statusString];
144        [self resizeStatusButton];
145    }
146}
147
148- (void) setStatusLabel: (id) sender
149{
150    NSString * statusLabel;
151    switch ([sender tag])
152    {
153        case STATUS_RATIO_TOTAL_TAG:
154            statusLabel = STATUS_RATIO_TOTAL;
155            break;
156        case STATUS_RATIO_SESSION_TAG:
157            statusLabel = STATUS_RATIO_SESSION;
158            break;
159        case STATUS_TRANSFER_TOTAL_TAG:
160            statusLabel = STATUS_TRANSFER_TOTAL;
161            break;
162        case STATUS_TRANSFER_SESSION_TAG:
163            statusLabel = STATUS_TRANSFER_SESSION;
164            break;
165        default:
166            NSAssert1(NO, @"Unknown status label tag received: %d", [sender tag]);
167            return;
168    }
169   
170    [[NSUserDefaults standardUserDefaults] setObject: statusLabel forKey: @"StatusLabel"];
171   
172    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
173}
174
175- (void) updateSpeedFieldsToolTips
176{
177    NSString * uploadText, * downloadText;
178   
179    if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SpeedLimit"])
180    {
181        NSString * speedString = [NSString stringWithFormat: @"%@ (%@)", NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
182                                    NSLocalizedString(@"Speed Limit", "Status Bar -> speed tooltip")];
183       
184        uploadText = [NSString stringWithFormat: speedString,
185                        [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitUploadLimit"]];
186        downloadText = [NSString stringWithFormat: speedString,
187                        [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitDownloadLimit"]];
188    }
189    else
190    {
191        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckUpload"])
192            uploadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
193                            [[NSUserDefaults standardUserDefaults] integerForKey: @"UploadLimit"]];
194        else
195            uploadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
196       
197        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckDownload"])
198            downloadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
199                            [[NSUserDefaults standardUserDefaults] integerForKey: @"DownloadLimit"]];
200        else
201            downloadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
202    }
203   
204    uploadText = [NSLocalizedString(@"Global upload limit", "Status Bar -> speed tooltip")
205                    stringByAppendingFormat: @": %@", uploadText];
206    downloadText = [NSLocalizedString(@"Global download limit", "Status Bar -> speed tooltip")
207                    stringByAppendingFormat: @": %@", downloadText];
208   
209    [fTotalULField setToolTip: uploadText];
210    [fTotalDLField setToolTip: downloadText];
211}
212
213- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
214{
215    const SEL action = [menuItem action];
216   
217    //enable sort options
218    if (action == @selector(setStatusLabel:))
219    {
220        NSString * statusLabel;
221        switch ([menuItem tag])
222        {
223            case STATUS_RATIO_TOTAL_TAG:
224                statusLabel = STATUS_RATIO_TOTAL;
225                break;
226            case STATUS_RATIO_SESSION_TAG:
227                statusLabel = STATUS_RATIO_SESSION;
228                break;
229            case STATUS_TRANSFER_TOTAL_TAG:
230                statusLabel = STATUS_TRANSFER_TOTAL;
231                break;
232            case STATUS_TRANSFER_SESSION_TAG:
233                statusLabel = STATUS_TRANSFER_SESSION;
234                break;
235            default:
236                NSAssert1(NO, @"Unknown status label tag received: %d", [menuItem tag]);
237        }
238       
239        [menuItem setState: [statusLabel isEqualToString: [[NSUserDefaults standardUserDefaults] stringForKey: @"StatusLabel"]]
240                            ? NSOnState : NSOffState];
241        return YES;
242    }
243   
244    return YES;
245}
246
247@end
248
249@implementation StatusBarController (Private)
250
251- (void) resizeStatusButton
252{
253    [fStatusButton sizeToFit];
254   
255    //width ends up being too long
256    NSRect statusFrame = [fStatusButton frame];
257    statusFrame.size.width -= 25.0;
258   
259    const CGFloat difference = NSMaxX(statusFrame) + 5.0 - NSMinX([fTotalDLImageView frame]);
260    if (difference > 0.0)
261        statusFrame.size.width -= difference;
262   
263    [fStatusButton setFrame: statusFrame];
264}
265
266@end
Note: See TracBrowser for help on using the repository browser.