source: trunk/macosx/StatusBarController.m @ 12685

Last change on this file since 12685 was 12685, checked in by livings124, 10 years ago

#4420 The StatusBar? updates itself a bit too much

  • Property svn:keywords set to Date Rev Author Id
File size: 9.6 KB
Line 
1/******************************************************************************
2 * $Id: StatusBarController.m 12685 2011-08-15 00:17:18Z 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        [fStatusButton setTitle: statusString];
143        [self resizeStatusButton];
144    }
145}
146
147- (void) setStatusLabel: (id) sender
148{
149    NSString * statusLabel;
150    switch ([sender tag])
151    {
152        case STATUS_RATIO_TOTAL_TAG:
153            statusLabel = STATUS_RATIO_TOTAL;
154            break;
155        case STATUS_RATIO_SESSION_TAG:
156            statusLabel = STATUS_RATIO_SESSION;
157            break;
158        case STATUS_TRANSFER_TOTAL_TAG:
159            statusLabel = STATUS_TRANSFER_TOTAL;
160            break;
161        case STATUS_TRANSFER_SESSION_TAG:
162            statusLabel = STATUS_TRANSFER_SESSION;
163            break;
164        default:
165            NSAssert1(NO, @"Unknown status label tag received: %d", [sender tag]);
166            return;
167    }
168   
169    [[NSUserDefaults standardUserDefaults] setObject: statusLabel forKey: @"StatusLabel"];
170   
171    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
172}
173
174- (void) updateSpeedFieldsToolTips
175{
176    NSString * uploadText, * downloadText;
177   
178    if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SpeedLimit"])
179    {
180        NSString * speedString = [NSString stringWithFormat: @"%@ (%@)", NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
181                                    NSLocalizedString(@"Speed Limit", "Status Bar -> speed tooltip")];
182       
183        uploadText = [NSString stringWithFormat: speedString,
184                        [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitUploadLimit"]];
185        downloadText = [NSString stringWithFormat: speedString,
186                        [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitDownloadLimit"]];
187    }
188    else
189    {
190        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckUpload"])
191            uploadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
192                            [[NSUserDefaults standardUserDefaults] integerForKey: @"UploadLimit"]];
193        else
194            uploadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
195       
196        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckDownload"])
197            downloadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
198                            [[NSUserDefaults standardUserDefaults] integerForKey: @"DownloadLimit"]];
199        else
200            downloadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
201    }
202   
203    uploadText = [NSLocalizedString(@"Global upload limit", "Status Bar -> speed tooltip")
204                    stringByAppendingFormat: @": %@", uploadText];
205    downloadText = [NSLocalizedString(@"Global download limit", "Status Bar -> speed tooltip")
206                    stringByAppendingFormat: @": %@", downloadText];
207   
208    [fTotalULField setToolTip: uploadText];
209    [fTotalDLField setToolTip: downloadText];
210}
211
212- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
213{
214    const SEL action = [menuItem action];
215   
216    //enable sort options
217    if (action == @selector(setStatusLabel:))
218    {
219        NSString * statusLabel;
220        switch ([menuItem tag])
221        {
222            case STATUS_RATIO_TOTAL_TAG:
223                statusLabel = STATUS_RATIO_TOTAL;
224                break;
225            case STATUS_RATIO_SESSION_TAG:
226                statusLabel = STATUS_RATIO_SESSION;
227                break;
228            case STATUS_TRANSFER_TOTAL_TAG:
229                statusLabel = STATUS_TRANSFER_TOTAL;
230                break;
231            case STATUS_TRANSFER_SESSION_TAG:
232                statusLabel = STATUS_TRANSFER_SESSION;
233                break;
234            default:
235                NSAssert1(NO, @"Unknown status label tag received: %d", [menuItem tag]);
236        }
237       
238        [menuItem setState: [statusLabel isEqualToString: [[NSUserDefaults standardUserDefaults] stringForKey: @"StatusLabel"]]
239                            ? NSOnState : NSOffState];
240        return YES;
241    }
242   
243    return YES;
244}
245
246@end
247
248@implementation StatusBarController (Private)
249
250- (void) resizeStatusButton
251{
252    [fStatusButton sizeToFit];
253   
254    //width ends up being too long
255    NSRect statusFrame = [fStatusButton frame];
256    statusFrame.size.width -= 25.0;
257   
258    const CGFloat difference = NSMaxX(statusFrame) + 5.0 - NSMinX([fTotalDLImageView frame]);
259    if (difference > 0.0)
260        statusFrame.size.width -= difference;
261   
262    [fStatusButton setFrame: statusFrame];
263}
264
265@end
Note: See TracBrowser for help on using the repository browser.