-
Notifications
You must be signed in to change notification settings - Fork 18
/
FeedItemsDisplay.m
executable file
·291 lines (230 loc) · 10.1 KB
/
FeedItemsDisplay.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// FeedItemsDisplay.m
// Frenzy
//
// Created by John Winter on 8/01/11.
// Copyright 2011 Aptonic Software. All rights reserved.
//
#import "FeedItemsDisplay.h"
#import "FrenzyAppDelegate.h"
@implementation FeedItemsDisplay
@synthesize currentFeedItems;
- (FeedItemsDisplay *)initWithWebView:(WebView *)aWebView
{
self = [super init];
if (self) {
webView = [aWebView retain];
[webView setUIDelegate:self];
[webView setPolicyDelegate:self];
[webView setFrameLoadDelegate:self];
feedElements = [[FeedElements alloc] init];
feedFileElements = [[FeedFileElements alloc] init];
[feedFileElements setWebView:webView];
sharedFilesHelper = [ShareFilesHelper sharedFilesHelper];
fm = [NSFileManager defaultManager];
}
return self;
}
- (void)loadItems
{
NSArray *feedItems = [[FeedStorage sharedFeedStorage] feedItems];
[self setCurrentFeedItems:feedItems];
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:@"/feed.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deleteToggle:) name:@"DeleteToggle" object:nil];
}
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id)listener
{
NSURL *url = [request URL];
if ([url isFileURL]) {
[listener use];
} else {
[[NSWorkspace sharedWorkspace] openURL:url];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MenuItemClicked" object:self
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"noRefocus", nil]];
[self simulateMouseUp:NSMakePoint(10, 10)];
}
}
- (NSUInteger)webView:(WebView *)sender dragDestinationActionMaskForDraggingInfo:(id <NSDraggingInfo>)draggingInfo
{
return WebDragDestinationActionNone;
}
- (NSUInteger)webView:(WebView *)webView dragSourceActionMaskForPoint:(NSPoint)point
{
return WebDragDestinationActionNone;
}
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems
{
NSMutableArray *menuItems = [NSMutableArray arrayWithArray:defaultMenuItems];
NSArray *itemsToRemove = [NSArray arrayWithObjects:[NSNumber numberWithInteger:WebMenuItemTagOpenLinkInNewWindow],
[NSNumber numberWithInteger:WebMenuItemTagDownloadLinkToDisk],
[NSNumber numberWithInteger:WebMenuItemTagReload],
[NSNumber numberWithInteger:WebMenuItemTagOpenImageInNewWindow],
[NSNumber numberWithInteger:WebMenuItemTagDownloadImageToDisk],
[NSNumber numberWithInteger:WebMenuItemTagOpenFrameInNewWindow], nil];
NSString *webLinkURLKey = [[element objectForKey:WebElementLinkURLKey] absoluteString];
if ([webLinkURLKey hasPrefix:@"javascript:"]) {
if ([webLinkURLKey rangeOfString:@"open_fileIndex_"].location != NSNotFound) {
// Right clicking on a file
NSMenuItem *revealMenuItem = [[NSMenuItem alloc] initWithTitle:@"Reveal in Finder" action:@selector(revealFromMenuItem:) keyEquivalent:@""];
[revealMenuItem setTarget:self];
[revealMenuItem setRepresentedObject:[element objectForKey:@"WebElementDOMNode"]];
return [NSArray arrayWithObject:revealMenuItem];
} else {
return nil;
}
}
for (NSNumber *n in itemsToRemove) {
NSUInteger toRemove = [[menuItems valueForKey:@"tag"] indexOfObject:n];
if (toRemove != NSNotFound)
[menuItems removeObjectAtIndex:toRemove];
}
if ([menuItems count] >= 1)
if ([[menuItems objectAtIndex:0] isSeparatorItem]) [menuItems removeObjectAtIndex:0];
return menuItems;
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
[[webView windowScriptObject] setValue:self forKey:@"AppController"];
[[webView windowScriptObject] setValue:feedFileElements forKey:@"FileController"];
NSArray *feedItems = [self currentFeedItems];
//NSLog(@"Loading %d items", [feedItems count]);
int index = 0;
DOMDocument *myDOMDocument = [[webView mainFrame] DOMDocument];
DOMElement *mainBlock = [myDOMDocument getElementById:@"feed-list"];
[feedElements setMainDOMDocument:myDOMDocument];
[feedElements setMainBlock:mainBlock];
[feedFileElements setMainDOMDocument:myDOMDocument];
[feedElements setFeedItems:feedItems];
[feedFileElements setFeedItems:feedItems];
if ([feedItems count] <= 0)
[mainBlock appendChild:[feedElements feedEmptyDiv]];
for (FeedItem *feedItem in feedItems) {
DOMElement *item = [feedElements itemDiv:index];
NSString *avatarPath = [feedItem avatarPathForUniqueID:[feedItem originalSender]];
DOMElement *avatarImage = [feedElements avatarImage:avatarPath];
if (![fm fileExistsAtPath:avatarPath])
[item appendChild:[feedElements avatarPlaceholder]];
[item appendChild:avatarImage];
[item appendChild:[feedElements avatarShadow]];
DOMElement *infoDiv = [feedElements infoDiv];
[item appendChild:infoDiv];
DOMElement *deleteButton = [feedElements deleteButton:feedItem index:index];
if (deleteButton != nil) [infoDiv appendChild:deleteButton];
DOMElement *replyButton = [feedElements replyButton:feedItem index:index];
if (replyButton != nil) [infoDiv appendChild:replyButton];
[infoDiv appendChild:[feedElements mainHeading:[feedItem actionText]]];
DOMElement *itemURL = [feedElements itemURL:feedItem];
if (itemURL != nil) [infoDiv appendChild:itemURL];
DOMElement *itemMessage = [feedElements itemMessage:feedItem];
if (itemMessage != nil) [infoDiv appendChild:itemMessage];
DOMElement *filesInfo = [feedFileElements filesInfo:feedItem index:index];
if (filesInfo != nil) [infoDiv appendChild:filesInfo];
[infoDiv appendChild:[feedElements footerInfo:feedItem]];
DOMElement *itemMessages = [feedElements messages:feedItem index:index];
if (itemMessages != nil) [item appendChild:itemMessages];
[mainBlock appendChild:item];
index++;
}
[self finalJSInit];
[sender scrollPoint:NSMakePoint(0, 0)];
[self performSelector:@selector(getScrollViewStatus) withObject:nil afterDelay:0.0];
}
- (void)getScrollViewStatus
{
NSScrollView *scrollView = [[[[webView mainFrame] frameView] documentView] enclosingScrollView];
if ([scrollView hasVerticalScroller])
[self webViewHasVerticalScrollbar];
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
{
if (aSelector == @selector(reply:) || aSelector == @selector(deleteItem:) ||
aSelector == @selector(getPathStatus:) || aSelector == @selector(getLatestFooterInfo:)) {
return NO;
}
return YES;
}
- (void)reply:(int)index
{
FeedItem *feedItem = [[self currentFeedItems] objectAtIndex:index];
[self performSelector:@selector(replyNotify:) withObject:feedItem afterDelay:0];
}
- (void)replyNotify:(FeedItem *)feedItem
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"FeedItemReply" object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
feedItem, @"feedItem", nil]];
}
- (void)deleteItem:(int)index
{
FeedItem *feedItem = [[self currentFeedItems] objectAtIndex:index];
[NSApp activateIgnoringOtherApps:YES];
int result = NSRunAlertPanel(@"Delete feed item", @"Are you sure?", @"Delete", @"Cancel", nil);
if (result) {
[[FeedStorage sharedFeedStorage] deleteItem:feedItem fromSharedFolders:([feedItem sharedFolders])];
[self loadItems];
}
}
- (void)deleteToggle:(NSNotification *)notif
{
WebScriptObject *scriptObject = [webView windowScriptObject];
BOOL optionHeld = [[[notif userInfo] objectForKey:@"modifiers"] boolValue];
NSString *script = [NSString stringWithFormat:@"deleteButtonsToggle(%@)", (optionHeld ? @"true" : @"false")];
[scriptObject evaluateWebScript:script];
}
- (void)finalJSInit
{
WebScriptObject *scriptObject = [webView windowScriptObject];
NSString *script = [NSString stringWithFormat:@"finalJSInit()"];
[scriptObject evaluateWebScript:script];
}
- (void)webViewHasVerticalScrollbar
{
WebScriptObject *scriptObject = [webView windowScriptObject];
NSString *script = [NSString stringWithFormat:@"webViewHasVerticalScrollbar()"];
[scriptObject evaluateWebScript:script];
}
- (NSString *)getLatestFooterInfo:(int)index
{
FeedItem *feedItem = [[self currentFeedItems] objectAtIndex:index];
DOMElement *footerInfo = [feedElements footerInfo:feedItem];
return [(DOMText *)[footerInfo firstChild] wholeText];
}
// Used for checking if a users avatar exists
- (BOOL)getPathStatus:(NSString *)path
{
if (IsEmpty(path) || [[path className] isEqualToString:@"WebUndefined"]) return NO;
if ([fm fileExistsAtPath:[[NSURL URLWithString:path] path]])
return YES;
else
return NO;
}
- (void)revealFromMenuItem:(id)sender
{
id element = [sender representedObject];
int feedItemIndex = [[element valueForKey:@"feedItemIndex"] intValue];
int fileIndex = [[element valueForKey:@"fileIndex"] intValue];
[feedFileElements openOrReveal:feedItemIndex fileIndex:fileIndex reveal:YES viaMenu:YES];
[self simulateMouseUp:NSMakePoint(10, 10)];
}
- (void)simulateMouseUp:(NSPoint)where
{
NSGraphicsContext *context = [NSGraphicsContext currentContext];
NSEvent* mouseUpEvent = [NSEvent mouseEventWithType:NSLeftMouseUp location:where
modifierFlags:0 timestamp:GetCurrentEventTime() windowNumber:0 context:context eventNumber:1 clickCount:1 pressure:0];
NSView *subView = [webView hitTest:[mouseUpEvent locationInWindow]];
if(subView)
[subView mouseUp: mouseUpEvent];
}
- (void)dealloc
{
[webView release];
[webWindow release];
[feedElements release];
[feedFileElements release];
[super dealloc];
}
@end