1 /******************************************************************************
2 * $Id: StatusBarController.m 12534 2011-07-04 20:51:54Z livings124 $
4 * Copyright (c) 2011 Transmission authors and contributors
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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 *****************************************************************************/
25 #import "StatusBarController.h"
26 #import "NSStringAdditions.h"
28 #import "transmission.h"
30 #define STATUS_RATIO_TOTAL @"RatioTotal"
31 #define STATUS_RATIO_SESSION @"RatioSession"
32 #define STATUS_TRANSFER_TOTAL @"TransferTotal"
33 #define STATUS_TRANSFER_SESSION @"TransferSession"
37 STATUS_RATIO_TOTAL_TAG = 0,
38 STATUS_RATIO_SESSION_TAG = 1,
39 STATUS_TRANSFER_TOTAL_TAG = 2,
40 STATUS_TRANSFER_SESSION_TAG = 3
43 @interface StatusBarController (Private)
45 - (void) resizeStatusButton;
49 @implementation StatusBarController
51 - (id) initWithLib: (tr_session *) lib
53 if ((self = [super initWithNibName: @"StatusBar" bundle: nil]))
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")];
73 [[fStatusButton cell] setBackgroundStyle: NSBackgroundStyleRaised];
74 [[fTotalDLField cell] setBackgroundStyle: NSBackgroundStyleRaised];
75 [[fTotalULField cell] setBackgroundStyle: NSBackgroundStyleRaised];
77 [self updateSpeedFieldsToolTips];
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]];
88 [[NSNotificationCenter defaultCenter] removeObserver: self];
93 - (void) updateWithDownload: (CGFloat) dlRate upload: (CGFloat) ulRate
96 [fTotalDLField setStringValue: [NSString stringForSpeed: dlRate]];
97 [fTotalULField setStringValue: [NSString stringForSpeed: ulRate]];
99 //set status button text
100 NSString * statusLabel = [[NSUserDefaults standardUserDefaults] stringForKey: @"StatusLabel"], * statusString;
102 if ((total = [statusLabel isEqualToString: STATUS_RATIO_TOTAL]) || [statusLabel isEqualToString: STATUS_RATIO_SESSION])
104 tr_session_stats stats;
106 tr_sessionGetCumulativeStats(fLib, &stats);
108 tr_sessionGetStats(fLib, &stats);
110 statusString = [NSLocalizedString(@"Ratio", "status bar -> status label") stringByAppendingFormat: @": %@",
111 [NSString stringForRatio: stats.ratio]];
113 else //STATUS_TRANSFER_TOTAL or STATUS_TRANSFER_SESSION
115 total = [statusLabel isEqualToString: STATUS_TRANSFER_TOTAL];
117 tr_session_stats stats;
119 tr_sessionGetCumulativeStats(fLib, &stats);
121 tr_sessionGetStats(fLib, &stats);
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]];
128 [fStatusButton setTitle: statusString];
129 [self resizeStatusButton];
132 - (void) setStatusLabel: (id) sender
134 NSString * statusLabel;
135 switch ([sender tag])
137 case STATUS_RATIO_TOTAL_TAG:
138 statusLabel = STATUS_RATIO_TOTAL;
140 case STATUS_RATIO_SESSION_TAG:
141 statusLabel = STATUS_RATIO_SESSION;
143 case STATUS_TRANSFER_TOTAL_TAG:
144 statusLabel = STATUS_TRANSFER_TOTAL;
146 case STATUS_TRANSFER_SESSION_TAG:
147 statusLabel = STATUS_TRANSFER_SESSION;
150 NSAssert1(NO, @"Unknown status label tag received: %d", [sender tag]);
154 [[NSUserDefaults standardUserDefaults] setObject: statusLabel forKey: @"StatusLabel"];
156 [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: nil];
159 - (void) updateSpeedFieldsToolTips
161 NSString * uploadText, * downloadText;
163 if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SpeedLimit"])
165 NSString * speedString = [NSString stringWithFormat: @"%@ (%@)", NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
166 NSLocalizedString(@"Speed Limit", "Status Bar -> speed tooltip")];
168 uploadText = [NSString stringWithFormat: speedString,
169 [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitUploadLimit"]];
170 downloadText = [NSString stringWithFormat: speedString,
171 [[NSUserDefaults standardUserDefaults] integerForKey: @"SpeedLimitDownloadLimit"]];
175 if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckUpload"])
176 uploadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
177 [[NSUserDefaults standardUserDefaults] integerForKey: @"UploadLimit"]];
179 uploadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
181 if ([[NSUserDefaults standardUserDefaults] boolForKey: @"CheckDownload"])
182 downloadText = [NSString stringWithFormat: NSLocalizedString(@"%d KB/s", "Status Bar -> speed tooltip"),
183 [[NSUserDefaults standardUserDefaults] integerForKey: @"DownloadLimit"]];
185 downloadText = NSLocalizedString(@"unlimited", "Status Bar -> speed tooltip");
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];
193 [fTotalULField setToolTip: uploadText];
194 [fTotalDLField setToolTip: downloadText];
197 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
199 const SEL action = [menuItem action];
201 //enable sort options
202 if (action == @selector(setStatusLabel:))
204 NSString * statusLabel;
205 switch ([menuItem tag])
207 case STATUS_RATIO_TOTAL_TAG:
208 statusLabel = STATUS_RATIO_TOTAL;
210 case STATUS_RATIO_SESSION_TAG:
211 statusLabel = STATUS_RATIO_SESSION;
213 case STATUS_TRANSFER_TOTAL_TAG:
214 statusLabel = STATUS_TRANSFER_TOTAL;
216 case STATUS_TRANSFER_SESSION_TAG:
217 statusLabel = STATUS_TRANSFER_SESSION;
220 NSAssert1(NO, @"Unknown status label tag received: %d", [menuItem tag]);
223 [menuItem setState: [statusLabel isEqualToString: [[NSUserDefaults standardUserDefaults] stringForKey: @"StatusLabel"]]
224 ? NSOnState : NSOffState];
233 @implementation StatusBarController (Private)
235 - (void) resizeStatusButton
237 [fStatusButton sizeToFit];
239 //width ends up being too long
240 NSRect statusFrame = [fStatusButton frame];
241 statusFrame.size.width -= 25.0;
243 const CGFloat difference = NSMaxX(statusFrame) + 5.0 - NSMinX([fTotalDLImageView frame]);
244 if (difference > 0.0)
245 statusFrame.size.width -= difference;
247 [fStatusButton setFrame: statusFrame];