Browse Source

Get mod hash from reddit.

Thomas Buck 10 years ago
parent
commit
2ebe56ad1e

+ 1
- 0
RedditBar/AppDelegate.h View File

@@ -15,6 +15,7 @@
15 15
 
16 16
 @property (atomic, retain) IBOutlet NSMenu *statusMenu;
17 17
 @property (atomic, retain) IBOutlet NSApplication *application;
18
+@property (weak) IBOutlet NSMenuItem *firstMenuItem;
18 19
 
19 20
 @property (atomic, retain) NSStatusItem *statusItem;
20 21
 @property (atomic, retain) NSImage *statusImage;

+ 11
- 2
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;
13
+@synthesize statusMenu, statusItem, statusImage, statusHighlightImage, prefWindow, currentState, application, api, firstMenuItem;
14 14
 
15 15
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
16 16
     statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
@@ -25,7 +25,12 @@
25 25
     currentState = [[StateModel alloc] init];
26 26
     [self defaultPreferences];
27 27
     [self loadPreferences];
28
-    [self reloadListWithOptions];
28
+    
29
+    if ([currentState.modhash isEqualToString:@""]) {
30
+        [firstMenuItem setTitle:@"Not logged in!"];
31
+    } else {
32
+        [self reloadListWithOptions];
33
+    }
29 34
 }
30 35
 
31 36
 -(void)defaultPreferences {
@@ -58,6 +63,10 @@
58 63
 }
59 64
 
60 65
 -(void)reloadListWithOptions {
66
+    if ([currentState.modhash isEqualToString:@""]) {
67
+        [firstMenuItem setTitle:@"Not logged in!"];
68
+        return;
69
+    }
61 70
     api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
62 71
     
63 72
 }

+ 2
- 0
RedditBar/Base.lproj/MainMenu.xib View File

@@ -1,6 +1,7 @@
1 1
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 2
 <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="4514" systemVersion="13A603" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
3 3
     <dependencies>
4
+        <deployment defaultVersion="1080" identifier="macosx"/>
4 5
         <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="4514"/>
5 6
     </dependencies>
6 7
     <objects>
@@ -14,6 +15,7 @@
14 15
         <customObject id="494" customClass="AppDelegate">
15 16
             <connections>
16 17
                 <outlet property="application" destination="-3" id="1K5-sF-Im7"/>
18
+                <outlet property="firstMenuItem" destination="jfb-mv-iN9" id="2QT-D1-leO"/>
17 19
                 <outlet property="statusMenu" destination="fuB-VS-nSq" id="AZY-3d-h8G"/>
18 20
             </connections>
19 21
         </customObject>

+ 82
- 0
RedditBar/Base.lproj/Prefs.xib View File

@@ -190,5 +190,87 @@ Gw
190 190
                 <real key="inspectorSampleValue" value="1"/>
191 191
             </metadata>
192 192
         </numberFormatter>
193
+        <menu title="Main Menu" systemMenu="main" id="IWp-hb-gSM">
194
+            <items>
195
+                <menuItem title="RedditBar" id="U8B-tM-Hrf">
196
+                    <modifierMask key="keyEquivalentModifierMask"/>
197
+                    <menu key="submenu" title="RedditBar" systemMenu="apple" id="94G-O2-jwC">
198
+                        <items>
199
+                            <menuItem title="About RedditBar" id="YW3-K5-K2j">
200
+                                <modifierMask key="keyEquivalentModifierMask"/>
201
+                            </menuItem>
202
+                            <menuItem isSeparatorItem="YES" id="twJ-Mg-T1U"/>
203
+                            <menuItem title="Preferences…" keyEquivalent="," id="fNd-08-bdx"/>
204
+                            <menuItem isSeparatorItem="YES" id="s0T-Bl-Th7"/>
205
+                            <menuItem title="Services" id="Tkq-PT-Dyn">
206
+                                <modifierMask key="keyEquivalentModifierMask"/>
207
+                                <menu key="submenu" title="Services" systemMenu="services" id="sdR-Rd-lph"/>
208
+                            </menuItem>
209
+                            <menuItem isSeparatorItem="YES" id="obO-uU-IXK"/>
210
+                            <menuItem title="Hide NewApplication" keyEquivalent="h" id="fH1-kn-NXO"/>
211
+                            <menuItem title="Hide Others" keyEquivalent="h" id="J40-1v-jag">
212
+                                <modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
213
+                            </menuItem>
214
+                            <menuItem title="Show All" id="UbR-hU-EII">
215
+                                <modifierMask key="keyEquivalentModifierMask"/>
216
+                            </menuItem>
217
+                            <menuItem isSeparatorItem="YES" id="6ba-Ih-432"/>
218
+                            <menuItem title="Quit NewApplication" keyEquivalent="q" id="ROS-gn-vTj"/>
219
+                        </items>
220
+                    </menu>
221
+                </menuItem>
222
+                <menuItem title="File" id="c7K-B5-rY5">
223
+                    <modifierMask key="keyEquivalentModifierMask"/>
224
+                    <menu key="submenu" title="File" id="24r-2D-3jT">
225
+                        <items>
226
+                            <menuItem title="Close" keyEquivalent="w" id="bqC-fi-B0U">
227
+                                <connections>
228
+                                    <action selector="close:" target="-1" id="qY7-vk-B61"/>
229
+                                </connections>
230
+                            </menuItem>
231
+                        </items>
232
+                    </menu>
233
+                </menuItem>
234
+                <menuItem title="Edit" id="czz-51-wxr">
235
+                    <modifierMask key="keyEquivalentModifierMask"/>
236
+                    <menu key="submenu" title="Edit" id="YpZ-Vi-h1u">
237
+                        <items>
238
+                            <menuItem title="Undo" keyEquivalent="z" id="IyX-7D-esH">
239
+                                <connections>
240
+                                    <action selector="undo:" target="-1" id="6su-5K-AYO"/>
241
+                                </connections>
242
+                            </menuItem>
243
+                            <menuItem isSeparatorItem="YES" id="v8m-cQ-DEf"/>
244
+                            <menuItem title="Cut" keyEquivalent="x" id="32T-3T-EcY">
245
+                                <connections>
246
+                                    <action selector="cut:" target="-1" id="TeB-yv-dFt"/>
247
+                                </connections>
248
+                            </menuItem>
249
+                            <menuItem title="Copy" keyEquivalent="c" id="wSY-To-FDI">
250
+                                <connections>
251
+                                    <action selector="copy:" target="-1" id="9qj-dU-ORV"/>
252
+                                </connections>
253
+                            </menuItem>
254
+                            <menuItem title="Paste" keyEquivalent="v" id="j3h-hj-rfI">
255
+                                <connections>
256
+                                    <action selector="paste:" target="-1" id="7m5-ZQ-jR1"/>
257
+                                </connections>
258
+                            </menuItem>
259
+                            <menuItem title="Delete" id="EtB-yf-Wow">
260
+                                <modifierMask key="keyEquivalentModifierMask"/>
261
+                                <connections>
262
+                                    <action selector="delete:" target="-1" id="RD6-ys-5oS"/>
263
+                                </connections>
264
+                            </menuItem>
265
+                            <menuItem title="Select All" keyEquivalent="a" id="qSu-ha-DhM">
266
+                                <connections>
267
+                                    <action selector="selectAll:" target="-1" id="TBx-Re-gUK"/>
268
+                                </connections>
269
+                            </menuItem>
270
+                        </items>
271
+                    </menu>
272
+                </menuItem>
273
+            </items>
274
+        </menu>
193 275
     </objects>
194 276
 </document>

+ 67
- 2
RedditBar/Reddit.m View File

@@ -10,8 +10,32 @@
10 10
 
11 11
 @implementation Reddit
12 12
 
13
+NSString *version = @"1.0.0";
14
+NSString *author = @"xythobuz";
15
+NSString *appName = @"RedditBar";
16
+
13 17
 @synthesize username, modhash, password;
14 18
 
19
+-(NSString *)urlencode:(NSString *)string {
20
+    NSMutableString *output = [NSMutableString string];
21
+    const unsigned char *source = (const unsigned char *)[string UTF8String];
22
+    long sourceLen = strlen((const char *)source);
23
+    for (int i = 0; i < sourceLen; i++) {
24
+        const unsigned char thisChar = source[i];
25
+        if (thisChar == ' '){
26
+            [output appendString:@"+"];
27
+        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
28
+                   (thisChar >= 'a' && thisChar <= 'z') ||
29
+                   (thisChar >= 'A' && thisChar <= 'Z') ||
30
+                   (thisChar >= '0' && thisChar <= '9')) {
31
+            [output appendFormat:@"%c", thisChar];
32
+        } else {
33
+            [output appendFormat:@"%%%02X", thisChar];
34
+        }
35
+    }
36
+    return output;
37
+}
38
+
15 39
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
16 40
     self = [super init];
17 41
     if (self) {
@@ -33,8 +57,49 @@
33 57
 }
34 58
 
35 59
 -(NSString *)queryModhash {
36
-    sleep(1);
37
-    return nil;
60
+    NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
61
+    [stringData appendFormat:@"&user=%@", [self urlencode: username]];
62
+    [stringData appendFormat:@"&passwd=%@", [self urlencode: password]];
63
+    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
64
+    NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
65
+    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://ssl.reddit.com/api/login"]];
66
+    [request setTimeoutInterval:5.0];
67
+    [request setCachePolicy:NSURLCacheStorageNotAllowed];
68
+    [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
69
+    [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
70
+    [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
71
+    [request setHTTPMethod:@"POST"];
72
+    [request setHTTPBody:requestBodyData];
73
+    NSURLResponse *response = nil;
74
+    NSError *error = nil;
75
+    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
76
+    if (error != nil) {
77
+        return nil;
78
+    } else {
79
+        NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
80
+        long code = [resp statusCode];
81
+        if (code == 200) {
82
+            id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
83
+            
84
+            if (error)
85
+                return nil;
86
+            
87
+            if([object isKindOfClass:[NSDictionary class]]) {
88
+                NSDictionary *results = object;
89
+                NSDictionary *json = [results valueForKey:@"json"];
90
+                if (json == nil)
91
+                    return nil;
92
+                NSDictionary *data = [json valueForKey:@"data"];
93
+                if (data == nil)
94
+                    return nil;
95
+                return [data valueForKey:@"modhash"];
96
+            } else {
97
+                return nil;
98
+            }
99
+        } else {
100
+            return nil;
101
+        }
102
+    }
38 103
 }
39 104
 
40 105
 -(BOOL)isAuthenticated {

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

Loading…
Cancel
Save