|
@@ -10,7 +10,7 @@
|
10
|
10
|
|
11
|
11
|
@implementation AppDelegate
|
12
|
12
|
|
13
|
|
-@synthesize statusMenu, statusItem, statusImage, statusHighlightImage, prefWindow, currentState, application, api, firstMenuItem;
|
|
13
|
+@synthesize statusMenu, statusItem, statusImage, statusHighlightImage, prefWindow, currentState, application, api, firstMenuItem, menuItems, redditItems;
|
14
|
14
|
|
15
|
15
|
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
|
16
|
16
|
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
|
|
@@ -82,19 +82,76 @@
|
82
|
82
|
[firstMenuItem setTitle:@"Error reading Frontpage!"];
|
83
|
83
|
return;
|
84
|
84
|
}
|
85
|
|
- [self putItemArrayInMenu:items];
|
|
85
|
+ redditItems = items;
|
86
|
86
|
} else {
|
87
|
87
|
NSArray *items = [api readSubreddits:currentState.subreddits Length:currentState.length];
|
88
|
88
|
if (items == nil) {
|
89
|
89
|
[firstMenuItem setTitle:@"Error reading Subreddits!"];
|
90
|
90
|
return;
|
91
|
91
|
}
|
92
|
|
- [self putItemArrayInMenu:items];
|
|
92
|
+ redditItems = items;
|
|
93
|
+ }
|
|
94
|
+ [self putItemArrayInMenu:redditItems];
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+-(IBAction)linkToOpen:(id)sender {
|
|
98
|
+ NSString *title = [(NSMenuItem *)sender title];
|
|
99
|
+ if ([title isEqualToString:@"Link..."]) {
|
|
100
|
+ for (NSUInteger i = 0; i < [menuItems count]; i++) {
|
|
101
|
+ NSMenuItem *item = [menuItems objectAtIndex:i];
|
|
102
|
+ NSMenu *submenu = item.submenu;
|
|
103
|
+ if (submenu != nil) {
|
|
104
|
+ if (sender == [submenu itemAtIndex:0]) {
|
|
105
|
+ RedditItem *rItem = [redditItems objectAtIndex:i];
|
|
106
|
+ [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
|
|
107
|
+ return;
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+ } else if ([title isEqualToString:@"Comments..."]) {
|
|
112
|
+ for (NSUInteger i = 0; i < [menuItems count]; i++) {
|
|
113
|
+ NSMenuItem *item = [menuItems objectAtIndex:i];
|
|
114
|
+ NSMenu *submenu = item.submenu;
|
|
115
|
+ if (submenu != nil) {
|
|
116
|
+ if (sender == [submenu itemAtIndex:1]) {
|
|
117
|
+ RedditItem *rItem = [redditItems objectAtIndex:i];
|
|
118
|
+ [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem comments]]];
|
|
119
|
+ return;
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ } else {
|
|
124
|
+ for (NSUInteger i = 0; i < [menuItems count]; i++) {
|
|
125
|
+ NSMenuItem *item = [menuItems objectAtIndex:i];
|
|
126
|
+ if (sender == item) {
|
|
127
|
+ RedditItem *rItem = [redditItems objectAtIndex:i];
|
|
128
|
+ [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[rItem link]]];
|
|
129
|
+ return;
|
|
130
|
+ }
|
|
131
|
+ }
|
93
|
132
|
}
|
94
|
133
|
}
|
95
|
134
|
|
96
|
135
|
-(void)putItemArrayInMenu:(NSArray *)array {
|
97
|
|
- // TODO populate menu
|
|
136
|
+ [firstMenuItem setHidden:YES];
|
|
137
|
+ NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
|
|
138
|
+ for (NSUInteger i = 0; i < [array count]; i++) {
|
|
139
|
+ RedditItem *reddit = [array objectAtIndex:i];
|
|
140
|
+ NSMenuItem *item = [[NSMenuItem alloc] init];
|
|
141
|
+ [item setTitle:reddit.name];
|
|
142
|
+ if (reddit.isSelf) {
|
|
143
|
+ [item setAction:@selector(linkToOpen:)];
|
|
144
|
+ [item setKeyEquivalent:@""];
|
|
145
|
+ } else {
|
|
146
|
+ NSMenu *submenu = [[NSMenu alloc] init];
|
|
147
|
+ [submenu addItemWithTitle:@"Link..." action:@selector(linkToOpen:) keyEquivalent:@""];
|
|
148
|
+ [submenu addItemWithTitle:@"Comments..." action:@selector(linkToOpen:) keyEquivalent:@""];
|
|
149
|
+ [item setSubmenu:submenu];
|
|
150
|
+ }
|
|
151
|
+ [items addObject:item];
|
|
152
|
+ [statusMenu insertItem:item atIndex:i];
|
|
153
|
+ }
|
|
154
|
+ menuItems = items;
|
98
|
155
|
}
|
99
|
156
|
|
100
|
157
|
-(IBAction)showPreferences:(id)sender {
|