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

+ 35
- 9
RedditBar/AppDelegate.m View File

25
     currentState = [[StateModel alloc] init];
25
     currentState = [[StateModel alloc] init];
26
     [self defaultPreferences];
26
     [self defaultPreferences];
27
     [self loadPreferences];
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
 -(void)defaultPreferences {
31
 -(void)defaultPreferences {
46
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
41
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
47
     [store setObject:currentState.username forKey:@"username"];
42
     [store setObject:currentState.username forKey:@"username"];
48
     [store setObject:currentState.modhash forKey:@"modhash"];
43
     [store setObject:currentState.modhash forKey:@"modhash"];
49
-    [store setBool:currentState.useSubsciptions forKey:@"subscriptions"];
44
+    [store setBool:currentState.useSubscriptions forKey:@"subscriptions"];
50
     [store setObject:currentState.subreddits forKey:@"subreddits"];
45
     [store setObject:currentState.subreddits forKey:@"subreddits"];
51
     [store setInteger:currentState.length forKey:@"length"];
46
     [store setInteger:currentState.length forKey:@"length"];
52
     [store synchronize];
47
     [store synchronize];
57
     [store synchronize];
52
     [store synchronize];
58
     [currentState setUsername:[store stringForKey:@"username"]];
53
     [currentState setUsername:[store stringForKey:@"username"]];
59
     [currentState setModhash:[store stringForKey:@"modhash"]];
54
     [currentState setModhash:[store stringForKey:@"modhash"]];
60
-    [currentState setUseSubsciptions:[store boolForKey:@"subscriptions"]];
55
+    [currentState setUseSubscriptions:[store boolForKey:@"subscriptions"]];
61
     [currentState setSubreddits:[store arrayForKey:@"subreddits"]];
56
     [currentState setSubreddits:[store arrayForKey:@"subreddits"]];
62
     [currentState setLength:[store integerForKey:@"length"]];
57
     [currentState setLength:[store integerForKey:@"length"]];
63
 }
58
 }
68
         return;
63
         return;
69
     }
64
     }
70
     api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
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
 -(IBAction)showPreferences:(id)sender {
100
 -(IBAction)showPreferences:(id)sender {
87
 -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
113
 -(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits length:(NSInteger)length {
88
     currentState.username = name;
114
     currentState.username = name;
89
     currentState.modhash = modhash;
115
     currentState.modhash = modhash;
90
-    currentState.useSubsciptions = subscriptions;
116
+    currentState.useSubscriptions = subscriptions;
91
     currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
117
     currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
92
     currentState.length = length;
118
     currentState.length = length;
93
     [self savePreferences];
119
     [self savePreferences];

+ 1
- 1
RedditBar/PrefController.m View File

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

+ 5
- 1
RedditBar/Reddit.h View File

7
 //
7
 //
8
 
8
 
9
 #import <Foundation/Foundation.h>
9
 #import <Foundation/Foundation.h>
10
+#import "RedditItem.h"
10
 
11
 
11
 @interface Reddit : NSObject
12
 @interface Reddit : NSObject
12
 
13
 
18
 -(NSString *)queryModhash;
19
 -(NSString *)queryModhash;
19
 
20
 
20
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash;
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
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res;
27
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res;
24
 -(NSString *)urlencode:(NSString *)string;
28
 -(NSString *)urlencode:(NSString *)string;

+ 51
- 4
RedditBar/Reddit.m View File

36
     return self;
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
 -(NSString *)queryModhash {
61
 -(NSString *)queryModhash {
40
     NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
62
     NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
41
     [stringData appendFormat:@"&user=%@", [self urlencode: username]];
63
     [stringData appendFormat:@"&user=%@", [self urlencode: username]];
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
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
122
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
74
     NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
123
     NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
75
     NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
124
     NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
79
     [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
128
     [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
80
     [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
129
     [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
81
     [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
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
     [request setHTTPMethod:@"POST"];
133
     [request setHTTPMethod:@"POST"];
83
     [request setHTTPBody:requestBodyData];
134
     [request setHTTPBody:requestBodyData];
84
     NSError *error = nil;
135
     NSError *error = nil;
89
         return data;
140
         return data;
90
 }
141
 }
91
 
142
 
92
--(BOOL)isAuthenticated {
93
-    return FALSE;
94
-}
95
-
96
 -(NSURL *)getAPIPoint:(NSString *)where {
143
 -(NSURL *)getAPIPoint:(NSString *)where {
97
     NSString *url = @"https://ssl.reddit.com";
144
     NSString *url = @"https://ssl.reddit.com";
98
     return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];
145
     return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];

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

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

+ 20
- 0
RedditBar/RedditItem.h View File

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

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

+ 1
- 1
RedditBar/StateModel.m View File

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

Loading…
Cancel
Save