source: trunk/macosx/StatusBarController.m @ 11999

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

silence a warning

File size: 9.2 KB
Line 
1/******************************************************************************
2 * $Id$
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   
58    return self;
59}
60
61- (void) awakeFromNib
62{
63    //localize menu items
64    [[[fStatusButton menu] itemWithTag: STATUS_RATIO_TOTAL_TAG] setTitle: NSLocalizedString(@"Total Ratio",
65        "Status Bar -> status menu")];
66    [[[fStatusButton menu] itemWithTag: STATUS_RATIO_SESSION_TAG] setTitle: NSLocalizedString(@"Session Ratio",
67        "Status Bar -> status menu")];
68    [[[fStatusButton menu] itemWithTag: STATUS_TRANSFER_TOTAL_TAG] setTitle: NSLocalizedString(@"Total Transfer",
69        "Status Bar -> status menu")];
70    [[[fStatusButton menu] itemWithTag: STATUS_TRANSFER_SESSION_TAG] setTitle: NSLocalizedString(@"Session Transfer",
71        "Status Bar -> status menu")];
72   
73    [[fTotalDLField cell] setBackgroundStyle: NSBackgroundStyleRaised];
74    [[fTotalULField cell] setBackgroundStyle: NSBackgroundStyleRaised];
75   
76    [self updateSpeedFieldsToolTips];
77   
78    //update when speed limits are changed
79    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateSpeedFieldsToolTips)
80        name: @"SpeedLimitUpdate" object: nil];
81    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeStatusButton)
82        name: NSWindowDidResizeNotification object: [[self view] window]];
83}
84
85- (void) dealloc
86{
87    [[NSNotificationCenter defaultCenter] removeObserver: self];
88   
89    [super dealloc];
90}
91
92- (void) updateWithDownload: (CGFloat) dlRate upload: (CGFloat) ulRate
93{
94    //set rates
95    [fTotalDLField setStringValue: [NSString stringForSpeed: dlRate]];
96    [fTotalULField setStringValue: [NSString stringForSpeed: ulRate]];
97   
98    //set status button text
99    NSString * statusLabel = [[NSUserDefaults standardUserDefaults] stringForKey: @"StatusLabel"], * statusString;
100    BOOL total;
101    if ((total = [statusLabel isEqualToString: STATUS_RATIO_TOTAL]) || [statusLabel isEqualToString: STATUS_RATIO_SESSION])
102    {
103        tr_session_stats stats;
104        if (total)
105            tr_sessionGetCumulativeStats(fLib, &stats);
106        else
107            tr_sessionGetStats(fLib, &stats);
108       
109        statusString = [NSLocalizedString(@"Ratio", "status bar -> status label") stringByAppendingFormat: @": %@",
110                        [NSString stringForRatio: stats.ratio]];
111    }
112    else //STATUS_TRANSFER_TOTAL or STATUS_TRANSFER_SESSION
113    {
114        total = [statusLabel isEqualToString: STATUS_TRANSFER_TOTAL];
115       
116        tr_session_stats stats;
117        if (total)
118            tr_sessionGetCumulativeStats(fLib, &stats);
119        else
120            tr_sessionGetStats(fLib, &stats);
121       
122        statusString = [NSString stringWithFormat: @"%@: %@  %@: %@",
123                NSLocalizedString(@"DL", "status bar -> status label"), [NSString stringForFileSize: stats.downloadedBytes],
124                NSLocalizedString(@"UL", "status bar -> status label"), [NSString stringForFileSize: stats.uploadedBytes]];
125    }
126   
127    [fStatusButton setTitle: statusString];
128    [self resizeStatusButton];
129}
130
131- (void) setStatusLabel: (id) sender
132{
133    NSString * statusLabel;
134    switch ([sender tag])
135    {
136        case STATUS_RATIO_TOTAL_TAG:
137            statusLabel = STATUS_RATIO_TOTAL;
138            break;
139        case STATUS_RATIO_SESSION_TAG:
140            statusLabel = STATUS_RATIO_SESSION;
141            break;
142        case STATUS_TRANSFER_TOTAL_TAG:
143            statusLabel = STATUS_TRANSFER_TOTAL;
144            break;
145        case STATUS_TRANSFER_SESSION_TAG:
146            statusLabel = STATUS_TRANSFER_SESSION;
147            break;
148        default:
149            NSAssert1(NO, @"Unknown status label tag received: %d", [sender tag]);
150            return;
151    }
152   
153    [[NSUserDefaults standardUserDefaults] setObject: statusLabel forKey: @"StatusLabel"];
154   
155    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
156}
157
158- (void) updateSpeedFieldsToolTips
159{
160    NSString * uploadText, * downloadText;
161   
162    if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SpeedLimit"])
163    {
164        NSString * speedString = [NSString stringWithFormat: @"%@ (%@)", NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
165                                    NSLocalizedString(@"Speed Limit", "Status Bar -> speed tooltip")];
166       
167        uploadText = [NSString stringWithFormat: speedString,
168                        [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitUploadLimit"]];
169        downloadText = [NSString stringWithFormat: speedString,
170                        [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitDownloadLimit"]];
171    }
172    else
173    {
174        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckUpload"])
175            uploadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
176                            [[NSUserDefaults standardUserDefaults] integerForKey: @"UploadLimit"]];
177        else
178            uploadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
179       
180        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckDownload"])
181            downloadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
182                            [[NSUserDefaults standardUserDefaults] integerForKey: @"DownloadLimit"]];
183        else
184            downloadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
185    }
186   
187    uploadText = [NSLocalizedString(@"Global upload limit", "Status Bar -> speed tooltip")
188                    stringByAppendingFormat: @": %@", uploadText];
189    downloadText = [NSLocalizedString(@"Global download limit", "Status Bar -> speed tooltip")
190                    stringByAppendingFormat: @": %@", downloadText];
191   
192    [fTotalULField setToolTip: uploadText];
193    [fTotalDLField setToolTip: downloadText];
194}
195
196- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
197{
198    const SEL action = [menuItem action];
199   
200    //enable sort options
201    if (action == @selector(setStatusLabel:))
202    {
203        NSString * statusLabel;
204        switch ([menuItem tag])
205        {
206            case STATUS_RATIO_TOTAL_TAG:
207                statusLabel = STATUS_RATIO_TOTAL;
208                break;
209            case STATUS_RATIO_SESSION_TAG:
210                statusLabel = STATUS_RATIO_SESSION;
211                break;
212            case STATUS_TRANSFER_TOTAL_TAG:
213                statusLabel = STATUS_TRANSFER_TOTAL;
214                break;
215            case STATUS_TRANSFER_SESSION_TAG:
216                statusLabel = STATUS_TRANSFER_SESSION;
217                break;
218            default:
219                NSAssert1(NO, @"Unknown status label tag received: %d", [menuItem tag]);
220        }
221       
222        [menuItem setState: [statusLabel isEqualToString: [[NSUserDefaults standardUserDefaults] stringForKey: @"StatusLabel"]]
223                            ? NSOnState : NSOffState];
224        return YES;
225    }
226   
227    return YES;
228}
229
230@end
231
232@implementation StatusBarController (Private)
233
234- (void) resizeStatusButton
235{
236    [fStatusButton sizeToFit];
237   
238    //width ends up being too long
239    NSRect statusFrame = [fStatusButton frame];
240    statusFrame.size.width -= 25.0;
241   
242    const CGFloat difference = NSMaxX(statusFrame) + 5.0 - NSMinX([fTotalDLImageView frame]);
243    if (difference > 0.0)
244        statusFrame.size.width -= difference;
245   
246    [fStatusButton setFrame: statusFrame];
247}
248
249@end
Note: See TracBrowser for help on using the repository browser.