source: trunk/macosx/StatusBarController.m @ 12534

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

explicitly set the status bar's "status button"'s background style

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