public
Description: Gem Document Viewer for Mac OS X
Homepage: http://www.robinlu.com/blog/goo/
Clone URL: git://github.com/robin/goo.git
goo / GooController.m
100644 248 lines (210 sloc) 6.237 kb
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
//
// GooController.m
// goo
//
// Created by Robin Lu on 8/22/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
 
#import "GooController.h"
#import "GemSpecs.h"
#import <PSMTabBarControl/PSMTabBarControl.h>
#import <WebKit/WebKit.h>
 
@interface GooController (Private)
 
- (NSTabViewItem*)createViewInTab;
 
@end
 
@implementation GooController
static NSString *zoomFactorIdentifier = @"zoom factor";
 
- (void)setHistoryBar
{
    [historyItemView setEnabled:[webView canGoBack] forSegment:0];
    [historyItemView setEnabled:[webView canGoForward] forSegment:1];
}
 
- (void)setupTabBar
{
[tabBar setTabView:tabView];
[tabBar setPartnerView:tabView];
[tabBar setStyleNamed:@"Unified"];
[tabBar setDelegate:self];
[tabBar setShowAddTabButton:YES];
[tabBar setSizeCellsToFit:YES];
[[tabBar addTabButton] setTarget:self];
[[tabBar addTabButton] setAction:@selector(addNewTab:)];
    [self addNewTab:self];
}
 
- (void)awakeFromNib
{
float zoomFactor = [[NSUserDefaults standardUserDefaults] floatForKey:zoomFactorIdentifier];
    [self setupTabBar];
    [self setHistoryBar];
if(0!=zoomFactor)
{
[webView setTextSizeMultiplier:zoomFactor];
}
}
 
#pragma mark table view delegate
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
[gemInfoView reloadData];
 
NSDictionary *selected = [[gemArrayController selectedObjects] objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/index.html", [selected objectForKey:@"doc_path"]];
 
NSURL *url;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( ![fileManager fileExistsAtPath:path isDirectory:NULL] ) {
path = @"about:blank";
url = [NSURL URLWithString:path];
}
else
url = [NSURL fileURLWithPath:path];
 
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[webView mainFrame] loadRequest:request];
}
 
#pragma mark window view delegate
- (void)windowWillClose:(NSNotification *)notification
{
float zoomFactor = [webView textSizeMultiplier];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setFloat:zoomFactor forKey:zoomFactorIdentifier];
[userDefaults synchronize];
}
 
#pragma mark table view menu
- (IBAction)openInFinder:(id)sender
{
NSDictionary *selected = [[gemArrayController selectedObjects] objectAtIndex:0];
NSString *path = [selected objectForKey:@"path"];
[[NSWorkspace sharedWorkspace] openFile:path];
}
 
- (IBAction)openInEditor:(id)sender
{
NSDictionary *selected = [[gemArrayController selectedObjects] objectAtIndex:0];
NSString *path = [selected objectForKey:@"path"];
[[NSWorkspace sharedWorkspace] openFile:path withApplication:@"TextMate"];
}
 
- (IBAction)openInHomepage:(id)sender
{
NSDictionary *selected = [[gemArrayController selectedObjects] objectAtIndex:0];
NSString *path = [selected objectForKey:@"homepage"];
NSURL *url = [NSURL URLWithString:path];
[[NSWorkspace sharedWorkspace] openURL:url];
}
 
#pragma mark browser
- (IBAction)history:(id)sender
{
    NSSegmentedCell * segCell = sender;
switch ([segCell selectedSegment]) {
case 0:
            [webView goBack];
break;
case 1:
[webView goForward];
break;
default:
break;
}
    [self setHistoryBar];
}
 
- (IBAction)search:(id)sender
{
    NSString *searchString = [searchField stringValue];
    [webView searchFor:searchString direction:YES caseSensitive:NO wrap:NO];
}
#pragma mark Links
- (IBAction)donate:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=iamawalrus%40gmail%2ecom&item_name=Goo&amount=4%2e99&no_shipping=0&no_note=1&tax=0&currency_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8"]];
}
 
- (IBAction)homepage:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.robinlu.com/blog/goo"]];
}
 
#pragma mark webview frame delegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    [self setHistoryBar];
    
    NSTabViewItem *tabItem = [tabView selectedTabViewItem];
    [tabItem setLabel:[webView mainFrameTitle]];
}
 
#pragma mark actions
- (IBAction)reload:(id)sender
{
    [gemSpecs release];
    gemSpecs = [[GemSpecs alloc] init];
    [gemArrayController setContent:gemSpecs.gemSpecIndex];
    [gemListView reloadData];
}
 
#pragma mark tab view
- (NSTabViewItem*)createViewInTab
{
// init the webview
WebView *newView = [[WebView alloc] init];
[newView setPolicyDelegate:self];
[newView setFrameLoadDelegate:self];
[newView setUIDelegate:self];
[newView setResourceLoadDelegate:self];
if([[NSUserDefaults standardUserDefaults] floatForKey:zoomFactorIdentifier]!=0)
{
[newView setTextSizeMultiplier:[[NSUserDefaults standardUserDefaults] floatForKey:zoomFactorIdentifier]];
}
 
// create new tab item
NSTabViewItem *newItem = [[[NSTabViewItem alloc] init] autorelease];
[newItem setView:newView];
    [newItem setLabel:@"(Untitled)"];
[newItem setIdentifier:newView];
 
// add to tab view
    [tabView addTabViewItem:newItem];
 
[newView release];
return newItem;
}
 
- (IBAction)addNewTab:(id)sender
{
NSTabViewItem *item = [self createViewInTab];
webView = [item identifier];
[tabView selectTabViewItem:item];
}
 
- (IBAction)closeTab:(id)sender
{
    if([tabView numberOfTabViewItems] > 1)
{
NSTabViewItem * item = [tabView selectedTabViewItem];
[tabView removeTabViewItem:item];
}
else
[window close];
}
 
- (void)tabView:(NSTabView *)tabView didCloseTabViewItem:(NSTabViewItem *)tabViewItem
{
}
 
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
webView = [tabViewItem identifier];
    [self setHistoryBar];
}
 
- (IBAction)selectNextTabViewItem:(id)sender
{
[tabView selectNextTabViewItem:sender];
}
 
- (IBAction)selectPreviousTabViewItem:(id)sender
{
[tabView selectPreviousTabViewItem:sender];
}
 
#pragma mark web view
- (IBAction)makeTextLarger:(id)sender
{
    [webView makeTextLarger:sender];
}
 
- (IBAction)makeTextSmaller:(id)sender
{
    [webView makeTextSmaller:sender];
}
 
- (IBAction)goFoward:(id)sender
{
    [webView goForward:sender];
}
 
- (IBAction)goBack:(id)sender
{
    [webView goBack:sender];
}
 
- (IBAction)print:(id)sender
{
    [webView print:sender];
}
@end