Transmission 2.33
[tomato.git] / release / src / router / transmission / macosx / StatusBarController.m
blobec302c4a75884725d99e4e60dbb717a4c1e58eb9
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  *****************************************************************************/
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"
35 typedef enum
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;
43 @interface StatusBarController (Private)
45 - (void) resizeStatusButton;
47 @end
49 @implementation StatusBarController
51 - (id) initWithLib: (tr_session *) lib
53     if ((self = [super initWithNibName: @"StatusBar" bundle: nil]))
54     {
55         fLib = lib;
56     }
57     
58     return self;
61 - (void) awakeFromNib
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]];
86 - (void) dealloc
88     [[NSNotificationCenter defaultCenter] removeObserver: self];
89     
90     [super dealloc];
93 - (void) updateWithDownload: (CGFloat) dlRate upload: (CGFloat) ulRate
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];
132 - (void) setStatusLabel: (id) sender
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];
159 - (void) updateSpeedFieldsToolTips
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];
197 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
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;
231 @end
233 @implementation StatusBarController (Private)
235 - (void) resizeStatusButton
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];
250 @end