Browse Source

Fixed Typo. Added RedditItem class. Added GET method. Check authentication.

Thomas Buck 11 years ago
parent
commit
260317ab0f

+ 6
- 0
RedditBar.xcodeproj/project.pbxproj View File

@@ -9,6 +9,7 @@
9 9
 /* Begin PBXBuildFile section */
10 10
 		E9341914184A1A1A00D03488 /* StateModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E9341913184A1A1A00D03488 /* StateModel.m */; };
11 11
 		E934191B184A47DE00D03488 /* Prefs.xib in Resources */ = {isa = PBXBuildFile; fileRef = E9341919184A47DE00D03488 /* Prefs.xib */; };
12
+		E9A129B1184BD24000F50D1D /* RedditItem.m in Sources */ = {isa = PBXBuildFile; fileRef = E9A129B0184BD24000F50D1D /* RedditItem.m */; };
12 13
 		E9B6773E184B73DC00850559 /* Reddit.m in Sources */ = {isa = PBXBuildFile; fileRef = E9B6773D184B73DC00850559 /* Reddit.m */; };
13 14
 		E9CF1F21184961010004AE02 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E9CF1F20184961010004AE02 /* Cocoa.framework */; };
14 15
 		E9CF1F2B184961010004AE02 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9CF1F29184961010004AE02 /* InfoPlist.strings */; };
@@ -38,6 +39,8 @@
38 39
 		E9341912184A1A1A00D03488 /* StateModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StateModel.h; sourceTree = "<group>"; };
39 40
 		E9341913184A1A1A00D03488 /* StateModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StateModel.m; sourceTree = "<group>"; };
40 41
 		E934191A184A47DE00D03488 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/Prefs.xib; sourceTree = "<group>"; };
42
+		E9A129AF184BD24000F50D1D /* RedditItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RedditItem.h; sourceTree = "<group>"; };
43
+		E9A129B0184BD24000F50D1D /* RedditItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RedditItem.m; sourceTree = "<group>"; };
41 44
 		E9B6773C184B73DC00850559 /* Reddit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reddit.h; sourceTree = "<group>"; };
42 45
 		E9B6773D184B73DC00850559 /* Reddit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reddit.m; sourceTree = "<group>"; };
43 46
 		E9CF1F1D184961010004AE02 /* RedditBar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RedditBar.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -130,6 +133,8 @@
130 133
 				E9CF1F33184961010004AE02 /* AppDelegate.m */,
131 134
 				E9B6773C184B73DC00850559 /* Reddit.h */,
132 135
 				E9B6773D184B73DC00850559 /* Reddit.m */,
136
+				E9A129AF184BD24000F50D1D /* RedditItem.h */,
137
+				E9A129B0184BD24000F50D1D /* RedditItem.m */,
133 138
 				E9341912184A1A1A00D03488 /* StateModel.h */,
134 139
 				E9341913184A1A1A00D03488 /* StateModel.m */,
135 140
 				E9CF1F35184961020004AE02 /* MainMenu.xib */,
@@ -290,6 +295,7 @@
290 295
 			files = (
291 296
 				E9CF1F34184961010004AE02 /* AppDelegate.m in Sources */,
292 297
 				E9341914184A1A1A00D03488 /* StateModel.m in Sources */,
298
+				E9A129B1184BD24000F50D1D /* RedditItem.m in Sources */,
293 299
 				E9CF1F2D184961010004AE02 /* main.m in Sources */,
294 300
 				E9CF1F5C1849685C0004AE02 /* PrefController.m in Sources */,
295 301
 				E9B6773E184B73DC00850559 /* Reddit.m in Sources */,

+ 35
- 9
RedditBar/AppDelegate.m View File

@@ -25,12 +25,7 @@
25 25
     currentState = [[StateModel alloc] init];
26 26
     [self defaultPreferences];
27 27
     [self loadPreferences];
28
-    
29
-    if ([currentState.modhash isEqualToString:@""]) {
30
-        [firstMenuItem setTitle:@"Not logged in!"];
31
-    } else {
32
-        [self reloadListWithOptions];
33
-    }
28
+    [self reloadListWithOptions];
34 29
 }
35 30
 
36 31
 -(void)defaultPreferences {
@@ -46,7 +41,7 @@
46 41
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
47 42
     [store setObject:currentState.username forKey:@"username"];
48 43
     [store setObject:currentState.modhash forKey:@"modhash"];
49
-    [store setBool:currentState.useSubsciptions forKey:@"subscriptions"];
44
+    [store setBool:currentState.useSubscriptions forKey:@"subscriptions"];
50 45
     [store setObject:currentState.subreddits forKey:@"subreddits"];
51 46
     [store setInteger:currentState.length forKey:@"length"];
52 47
     [store synchronize];
@@ -57,7 +52,7 @@
57 52
     [store synchronize];
58 53
     [currentState setUsername:[store stringForKey:@"username"]];
59 54
     [currentState setModhash:[store stringForKey:@"modhash"]];
60
-    [currentState setUseSubsciptions:[store boolForKey:@"subscriptions"]];
55
+    [currentState setUseSubscriptions:[store boolForKey:@"subscriptions"]];
61 56
     [currentState setSubreddits:[store arrayForKey:@"subreddits"]];
62 57
     [currentState setLength:[store integerForKey:@"length"]];
63 58
 }
@@ -68,7 +63,38 @@
68 63
         return;
69 64
     }
70 65
     api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
66
+    NSString *tmp = @"";
67
+    if (![api isAuthenticatedNewModhash:&tmp]) {
68
+        [firstMenuItem setTitle:@"Not logged in!"];
69
+        return;
70
+    }
71 71
     
72
+    // Reddit gives out new modhashes all the time??
73
+    //if (![tmp isEqualToString:@""]) {
74
+    //    NSLog(@"Modhash has changed!\n");
75
+    //    currentState.modhash = tmp; // We got a new modhash from reddit
76
+    //    [self savePreferences];
77
+    //}
78
+    
79
+    if (currentState.useSubscriptions) {
80
+        NSArray *items = [api readFrontpageLength:currentState.length];
81
+        if (items == nil) {
82
+            [firstMenuItem setTitle:@"Error reading Frontpage!"];
83
+            return;
84
+        }
85
+        [self putItemArrayInMenu:items];
86
+    } else {
87
+        NSArray *items = [api readSubreddits:currentState.subreddits Length:currentState.length];
88
+        if (items == nil) {
89
+            [firstMenuItem setTitle:@"Error reading Subreddits!"];
90
+            return;
91
+        }
92
+        [self putItemArrayInMenu:items];
93
+    }
94
+}
95
+
96
+-(void)putItemArrayInMenu:(NSArray *)array {
97
+    // TODO populate menu
72 98
 }
73 99
 
74 100
 -(IBAction)showPreferences:(id)sender {
@@ -87,7 +113,7 @@
87 113
 -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
88 114
     currentState.username = name;
89 115
     currentState.modhash = modhash;
90
-    currentState.useSubsciptions = subscriptions;
116
+    currentState.useSubscriptions = subscriptions;
91 117
     currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
92 118
     currentState.length = length;
93 119
     [self savePreferences];

+ 1
- 1
RedditBar/PrefController.m View File

@@ -31,7 +31,7 @@ NSString *subredditCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST
31 31
     if (![state.modhash isEqualToString:@""]) {
32 32
         [password setStringValue:modhashSetLiteral];
33 33
     }
34
-    [subscriptions setState:[NSNumber numberWithBool:state.useSubsciptions].integerValue];
34
+    [subscriptions setState:[NSNumber numberWithBool:state.useSubscriptions].integerValue];
35 35
     [self toggleSubs:nil]; // Maybe the subreddits field needs to be editable
36 36
     NSMutableString *reddits = [[NSMutableString alloc] init];
37 37
     for(int i = 0; i < [state.subreddits count]; i++) {

+ 5
- 1
RedditBar/Reddit.h View File

@@ -7,6 +7,7 @@
7 7
 //
8 8
 
9 9
 #import <Foundation/Foundation.h>
10
+#import "RedditItem.h"
10 11
 
11 12
 @interface Reddit : NSObject
12 13
 
@@ -18,7 +19,10 @@
18 19
 -(NSString *)queryModhash;
19 20
 
20 21
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash;
21
--(BOOL)isAuthenticated;
22
+-(BOOL)isAuthenticatedNewModhash:(NSString **)newModHash;
23
+
24
+-(NSArray *)readFrontpageLength:(NSInteger)length;
25
+-(NSArray *)readSubreddits:(NSArray *)source Length:(NSInteger)length;
22 26
 
23 27
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res;
24 28
 -(NSString *)urlencode:(NSString *)string;

+ 51
- 4
RedditBar/Reddit.m View File

@@ -36,6 +36,28 @@ NSString *appName = @"RedditBar";
36 36
     return self;
37 37
 }
38 38
 
39
+-(BOOL)isAuthenticatedNewModhash:(NSString **)newModHash {
40
+    NSHTTPURLResponse *response;
41
+    NSData *data = [self queryAPI:@"api/me.json" withResponse:&response];
42
+    if ((data != nil) && ([response statusCode] == 200)) {
43
+        NSError *error;
44
+        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
45
+        NSDictionary *data = [json valueForKey:@"data"];
46
+        if (data == nil)
47
+            return FALSE;
48
+        NSString *newHash = [data valueForKey:@"modhash"];
49
+        if ((newHash == nil) || ([newHash isEqualToString:@""]))
50
+            return FALSE;
51
+        if (![newHash isEqualToString:modhash]) {
52
+            modhash = newHash;
53
+            *newModHash = newHash;
54
+        }
55
+        return TRUE;
56
+        
57
+    }
58
+    return FALSE;
59
+}
60
+
39 61
 -(NSString *)queryModhash {
40 62
     NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
41 63
     [stringData appendFormat:@"&user=%@", [self urlencode: username]];
@@ -70,6 +92,33 @@ NSString *appName = @"RedditBar";
70 92
     }
71 93
 }
72 94
 
95
+-(NSArray *)readFrontpageLength:(NSInteger)length {
96
+    // TODO read frontpage
97
+    return nil;
98
+}
99
+
100
+-(NSArray *)readSubreddits:(NSArray *)source Length:(NSInteger)length {
101
+    // TODO read subreddits
102
+    return nil;
103
+}
104
+
105
+-(NSData *)queryAPI:(NSString *)api withResponse:(NSHTTPURLResponse **)res {
106
+    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
107
+    [request setTimeoutInterval:5.0];
108
+    [request setCachePolicy:NSURLCacheStorageNotAllowed];
109
+    [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
110
+    [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
111
+    if ((modhash != nil) && (![modhash isEqualToString:@""]))
112
+        [request addValue:modhash forHTTPHeaderField:@"X-Modhash"];
113
+    [request setHTTPMethod:@"GET"];
114
+    NSError *error = nil;
115
+    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
116
+    if (error)
117
+        return nil;
118
+    else
119
+        return data;
120
+}
121
+
73 122
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
74 123
     NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
75 124
     NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
@@ -79,6 +128,8 @@ NSString *appName = @"RedditBar";
79 128
     [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
80 129
     [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
81 130
     [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
131
+    if ((modhash != nil) && (![modhash isEqualToString:@""]))
132
+        [request addValue:modhash forHTTPHeaderField:@"X-Modhash"];
82 133
     [request setHTTPMethod:@"POST"];
83 134
     [request setHTTPBody:requestBodyData];
84 135
     NSError *error = nil;
@@ -89,10 +140,6 @@ NSString *appName = @"RedditBar";
89 140
         return data;
90 141
 }
91 142
 
92
--(BOOL)isAuthenticated {
93
-    return FALSE;
94
-}
95
-
96 143
 -(NSURL *)getAPIPoint:(NSString *)where {
97 144
     NSString *url = @"https://ssl.reddit.com";
98 145
     return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];

+ 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>115</string>
24
+	<string>120</string>
25 25
 	<key>LSApplicationCategoryType</key>
26 26
 	<string>public.app-category.utilities</string>
27 27
 	<key>LSMinimumSystemVersion</key>

+ 20
- 0
RedditBar/RedditItem.h View File

@@ -0,0 +1,20 @@
1
+//
2
+//  RedditItem.h
3
+//  RedditBar
4
+//
5
+//  Created by Thomas Buck on 01.12.13.
6
+//  Copyright (c) 2013 xythobuz. All rights reserved.
7
+//
8
+
9
+#import <Foundation/Foundation.h>
10
+
11
+@interface RedditItem : NSObject
12
+
13
+@property (atomic, retain) NSString *name;
14
+@property (atomic, retain) NSString *link;
15
+@property (atomic, retain) NSString *comments;
16
+@property (atomic) BOOL isSelf;
17
+
18
++(RedditItem *)itemWithName:(NSString *)name Link:(NSString *)link Comments:(NSString *)comments Self:(BOOL)isSelf;
19
+
20
+@end

+ 24
- 0
RedditBar/RedditItem.m View File

@@ -0,0 +1,24 @@
1
+//
2
+//  RedditItem.m
3
+//  RedditBar
4
+//
5
+//  Created by Thomas Buck on 01.12.13.
6
+//  Copyright (c) 2013 xythobuz. All rights reserved.
7
+//
8
+
9
+#import "RedditItem.h"
10
+
11
+@implementation RedditItem
12
+
13
+@synthesize name, link, comments, isSelf;
14
+
15
++(RedditItem *)itemWithName:(NSString *)name Link:(NSString *)link Comments:(NSString *)comments Self:(BOOL)isSelf {
16
+    RedditItem *i = [[RedditItem alloc] init];
17
+    [i setName:name];
18
+    [i setLink:link];
19
+    [i setComments:comments];
20
+    [i setIsSelf:isSelf];
21
+    return i;
22
+}
23
+
24
+@end

+ 1
- 1
RedditBar/StateModel.h View File

@@ -12,7 +12,7 @@
12 12
 
13 13
 @property (atomic, retain) NSString *username;
14 14
 @property (atomic, retain) NSString *modhash;
15
-@property (atomic) Boolean useSubsciptions;
15
+@property (atomic) Boolean useSubscriptions;
16 16
 @property (atomic, retain) NSArray *subreddits;
17 17
 @property (atomic) NSInteger length;
18 18
 

+ 1
- 1
RedditBar/StateModel.m View File

@@ -10,6 +10,6 @@
10 10
 
11 11
 @implementation StateModel
12 12
 
13
-@synthesize username, modhash, useSubsciptions, subreddits;
13
+@synthesize username, modhash, useSubscriptions, subreddits;
14 14
 
15 15
 @end

Loading…
Cancel
Save