Browse Source

Implemented Link opening

Thomas Buck 10 years ago
parent
commit
0982da8f66
4 changed files with 71 additions and 6 deletions
  1. 3
    0
      RedditBar/AppDelegate.h
  2. 61
    4
      RedditBar/AppDelegate.m
  3. 6
    1
      RedditBar/Reddit.m
  4. 1
    1
      RedditBar/RedditBar-Info.plist

+ 3
- 0
RedditBar/AppDelegate.h View File

@@ -23,9 +23,12 @@
23 23
 @property (atomic, retain) PrefController *prefWindow;
24 24
 @property (atomic, retain) StateModel *currentState;
25 25
 @property (atomic, retain) Reddit *api;
26
+@property (atomic, retain) NSArray *menuItems;
27
+@property (atomic, retain) NSArray *redditItems;
26 28
 
27 29
 -(IBAction)showPreferences:(id)sender;
28 30
 -(IBAction)showAbout:(id)sender;
31
+-(IBAction)linkToOpen:(id)sender;
29 32
 
30 33
 -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length;
31 34
 

+ 61
- 4
RedditBar/AppDelegate.m View File

@@ -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 {

+ 6
- 1
RedditBar/Reddit.m View File

@@ -94,7 +94,12 @@ NSString *appName = @"RedditBar";
94 94
 
95 95
 -(NSArray *)readFrontpageLength:(NSInteger)length {
96 96
     // TODO read frontpage
97
-    return nil;
97
+    
98
+    RedditItem *a = [RedditItem itemWithName:@"Test 1" Link:@"http://google.de" Comments:@"http://google.de" Self:NO];
99
+    RedditItem *b = [RedditItem itemWithName:@"Test 2" Link:@"http://reddit.com" Comments:@"http://reddit.com" Self:NO];
100
+    RedditItem *c = [RedditItem itemWithName:@"Test 3" Link:@"http://google.de" Comments:nil Self:YES];
101
+    NSMutableArray *array = [NSMutableArray arrayWithObjects:a, b, c, nil];
102
+    return array;
98 103
 }
99 104
 
100 105
 -(NSArray *)readSubreddits:(NSArray *)source Length:(NSInteger)length {

+ 1
- 1
RedditBar/RedditBar-Info.plist View File

@@ -21,7 +21,7 @@
21 21
 	<key>CFBundleSignature</key>
22 22
 	<string>????</string>
23 23
 	<key>CFBundleVersion</key>
24
-	<string>120</string>
24
+	<string>127</string>
25 25
 	<key>LSApplicationCategoryType</key>
26 26
 	<string>public.app-category.utilities</string>
27 27
 	<key>LSMinimumSystemVersion</key>

Loading…
Cancel
Save