forked from mipstian/catch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTCMenuController.m
188 lines (145 loc) · 6.75 KB
/
CTCMenuController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#import "CTCMenuController.h"
#import "CTCDefaults.h"
#import "CTCScheduler.h"
@interface CTCMenuController ()
@property (strong, nonatomic) IBOutlet NSMenu *menu;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuVersion;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuPauseResume;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuLastUpdate;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuRecentTorrents;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuShowInFinder;
@property (strong, nonatomic) NSStatusItem *menuBarItem;
@end
@implementation CTCMenuController
- (void)awakeFromNib {
[self setupMenuItem];
// Update UI with initial values
[self refreshSchedulerStatus];
[self setLastUpdateStatus:YES time:nil];
[self setupObservers];
}
- (void)setupMenuItem {
// Create the NSStatusItem and set its length
self.menuBarItem = [NSStatusBar.systemStatusBar statusItemWithLength:NSSquareStatusItemLength];
// Tell the NSStatusItem what menu to load
self.menuBarItem.menu = self.menu;
// Enable highlighting
self.menuBarItem.highlightMode = YES;
// Set current name and version
self.menuVersion.title = [NSString stringWithFormat:@"%@ %@", CTCDefaults.appName, CTCDefaults.appVersion];
// Disable the recent torrents menu until there's something to show
self.menuRecentTorrents.enabled = NO;
}
- (void)setupObservers {
__weak typeof(self) weakSelf = self;
void (^handleSchedulerStatusChange)(NSNotification *) = ^(NSNotification *notification) {
[weakSelf refreshSchedulerStatus];
};
void (^handleSchedulerLastUpdateStatusChange)(NSNotification *) = ^(NSNotification *notification) {
BOOL wasSuccessful = [notification.userInfo[@"successful"] boolValue];
NSDate *lastUpdateDate = notification.userInfo[@"time"];
[weakSelf setLastUpdateStatus:wasSuccessful time:lastUpdateDate];
[weakSelf refreshRecentsMenu];
};
[NSNotificationCenter.defaultCenter addObserverForName:kCTCSchedulerStatusNotificationName
object:CTCScheduler.sharedScheduler
queue:nil
usingBlock:handleSchedulerStatusChange];
[NSNotificationCenter.defaultCenter addObserverForName:kCTCSchedulerLastUpdateStatusNotificationName
object:CTCScheduler.sharedScheduler
queue:nil
usingBlock:handleSchedulerLastUpdateStatusChange];
}
- (IBAction)openTorrentFolder:(id)sender {
if (!CTCDefaults.isConfigurationValid) return;
// Launch finder with the torrent folder open
[NSWorkspace.sharedWorkspace openFile:CTCDefaults.torrentsSavePath];
}
- (IBAction)checkNow:(id)sender {
[CTCScheduler.sharedScheduler forceCheck];
}
- (IBAction)togglePause:(id)sender {
[CTCScheduler.sharedScheduler togglePause];
}
- (void)refreshSchedulerStatus {
if (CTCScheduler.sharedScheduler.isChecking) {
[self setRefreshing];
}
else {
if (CTCScheduler.sharedScheduler.isPolling) {
[self setIdle];
}
else {
[self setDisabled];
}
}
}
- (void)setLastUpdateStatus:(BOOL)lastUpdateWasSuccessful time:(NSDate *)time {
// Create something like "Last update: 3:45 AM" and place it in the menu
NSString *baseLastUpdateString = nil;
NSString *lastUpdateString = nil;
if (lastUpdateWasSuccessful) {
baseLastUpdateString = NSLocalizedString(@"lastupdate", @"Title for the last update time");
}
else {
baseLastUpdateString = NSLocalizedString(@"lastupdatefailed", @"Title for the last update time if it fails");
}
if (time) {
NSDateFormatter *dateFormatter = NSDateFormatter.new;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
NSString *lastUpdateTime = [dateFormatter stringFromDate:time];
lastUpdateString = [NSString stringWithFormat:baseLastUpdateString, lastUpdateTime];
}
else {
lastUpdateString = [NSString stringWithFormat:baseLastUpdateString, NSLocalizedString(@"never", @"Never happened")];
}
[self.menuLastUpdate setTitle:lastUpdateString];
}
- (void)setIdle {
// Sets the images (status: idle)
[self.menuBarItem setImage:[NSImage imageNamed:@"menubar_idle"]];
[self.menuBarItem setAlternateImage:[NSImage imageNamed:@"menubar_idle-inv"]];
// Set pause/resume to "pause"
[self.menuPauseResume setTitle:NSLocalizedString(@"pause", @"Description of pause action")];
}
- (void)setRefreshing {
// Sets the images (status: refreshing)
[self.menuBarItem setImage:[NSImage imageNamed:@"menubar_refreshing"]];
[self.menuBarItem setAlternateImage:[NSImage imageNamed:@"menubar_refreshing-inv"]];
// Set pause/resume to "pause"
[self.menuPauseResume setTitle:NSLocalizedString(@"pause", @"Description of pause action")];
// Also overwrite the last update string with "Updating now"
[self.menuLastUpdate setTitle:NSLocalizedString(@"updatingnow", @"An update is in progress")];
}
- (void)setDisabled {
// Sets the images (status: disabled)
[self.menuBarItem setImage:[NSImage imageNamed:@"menubar_disabled"]];
[self.menuBarItem setAlternateImage:[NSImage imageNamed:@"menubar_disabled-inv"]];
// Set pause/resume to "resume"
[self.menuPauseResume setTitle:NSLocalizedString(@"resume", @"Description of resume action")];
}
- (void)refreshRecentsMenu {
// Also refresh the list of recently downloaded torrents
// Get the full list
NSArray *downloadHistory = CTCDefaults.downloadHistory;
// Get last 10 elements
NSUInteger recentsCount = MIN(downloadHistory.count, 10U);
NSArray *recents = [downloadHistory subarrayWithRange:NSMakeRange(0U, recentsCount)];
// Extract titles
NSArray *recentTorrentNames = [recents valueForKey:@"title"];
// Clear menu
[self.menuRecentTorrents.submenu removeAllItems];
// Add new items
[recentTorrentNames enumerateObjectsUsingBlock:^(NSString *title, NSUInteger index, BOOL *stop) {
NSString *menuTitle = [NSString stringWithFormat:@"%lu %@", index + 1, title];
NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle
action:NULL
keyEquivalent:@""];
newItem.enabled = NO;
[self.menuRecentTorrents.submenu addItem:newItem];
}];
// Put the Show in finder menu back
[self.menuRecentTorrents.submenu addItem:self.menuShowInFinder];
self.menuRecentTorrents.enabled = YES;
}
@end