-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReadLater.m
116 lines (99 loc) · 4.3 KB
/
ReadLater.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#import <UIKit/UIKit2.h>
#import <WebKit/WebKit.h>
#import <WebCore/WebCore.h>
#import <Preferences/Preferences.h>
#import "ActionMenu.h"
#import "Instapaper/InstapaperSession.h"
#import "Instapaper/InstapaperRequest.h"
// Additional Private APIs
@interface UIWebBrowserView (WebPrivate)
- (WebFrame *)_focusedOrMainFrame;
@end
@interface WebFrame (WebPrivate)
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)string forceUserGesture:(BOOL)forceUserGesture;
@end
@implementation UIWebBrowserView (ReadLaterAction)
static inline void Alert(NSString *message)
{
// Helper function
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Read Later" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[av release];
}
- (BOOL)canDoReadLaterAction:(id)sender
{
WebThreadLock();
WebFrame *webFrame = [self _focusedOrMainFrame];
// Check to see if web view contains a URL we can Read Later
NSString *URL = [webFrame stringByEvaluatingJavaScriptFromString:@"location.href" forceUserGesture:NO];
WebThreadUnlock();
return [URL hasPrefix:@"http://"] || [URL hasPrefix:@"https://"];
}
static NSString *selection = nil;
- (void)performReadLaterAction:(id)sender
{
// Load Settings
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.rpetrich.readlater.plist"];
NSString *username = [settings objectForKey:@"RLLogin"];
if ([username length] == 0) {
// Username is empty
Alert(@"Instapaper login not yet set.\nEnter your Instapaper email and password in ActionMenu settings.");
} else {
// Create a session
RLAInstapaperSession *session = [[RLAInstapaperSession alloc] init];
session.username = username;
session.password = [settings objectForKey:@"RLPassword"];
// Create a request
RLAInstapaperRequest *request = [[RLAInstapaperRequest alloc] initWithSession:session];
[session release];
// Add the item with specified URL
WebThreadLock();
WebFrame *webFrame = [self _focusedOrMainFrame];
// Use selection for summary text
selection = [self selectedTextualRepresentation];
// If selection equals all then set to nil (probably nothing was selected)
if ([selection isEqualToString:[self textualRepresentation]]) selection = nil;
// Truncate length of summary (Instapaper shows upto 200 chars in main list)
#define LEN 500
selection = [selection length] <= LEN ? selection :
[[selection substringToIndex:LEN] stringByAppendingString:@".."];
// Retain to use in alert
[selection retain];
[request addItemWithURL:[NSURL URLWithString:[webFrame stringByEvaluatingJavaScriptFromString:@"location.href" forceUserGesture:NO]]
title:[webFrame stringByEvaluatingJavaScriptFromString:@"document.title" forceUserGesture:NO]
selection:selection];
WebThreadUnlock();
[request release];
}
}
+ (void)readLaterActionSucceeded:(NSNotification *)notification
{
NSString *summary = @".\n\nSummary as follows:\n\"%@\"";
summary = selection ? [NSString stringWithFormat:summary, selection] : nil;
Alert([@"Link submitted to Instapaper" stringByAppendingString:summary ?: @"."]);
[selection release];
}
+ (void)readLaterActionFailed:(NSNotification *)notification
{
Alert([[notification.userInfo objectForKey:@"error"] localizedDescription]);
[selection release];
}
+ (void)load
{
id<AMMenuItem> menuItem = [[UIMenuController sharedMenuController] registerAction:@selector(performReadLaterAction:) title:@"Read Later" canPerform:@selector(canDoReadLaterAction:) forPlugin:@"ReadLater"];
menuItem.priority = 1000;
menuItem.image = [UIImage imageWithContentsOfFile:([UIScreen mainScreen].scale == 2.0f) ? @"/Library/ActionMenu/Plugins/Instapaper@2x.png" : @"/Library/ActionMenu/Plugins/Instapaper.png"];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(readLaterActionSucceeded:) name:kInstapaperRequestSucceededNotification object:nil];
[nc addObserver:self selector:@selector(readLaterActionFailed:) name:kInstapaperRequestFailedNotification object:nil];
}
@end
// Settings bundle controller
@interface ReadLaterSettingsController : PSListController
@end
@implementation ReadLaterSettingsController
- (NSArray *)loadSpecifiersFromPlistName:(NSString *)plistName target:(id)target
{
return [super loadSpecifiersFromPlistName:@"ReadLater" target:self];
}
@end