forked from NSGod/ichm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHMAppController.m
126 lines (90 loc) · 3.75 KB
/
CHMAppController.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
//
// CHMAppController.m
// ichm
//
// Created by Robin Lu on 7/16/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CHMAppController.h"
#import <CHMKit/CHMKit.h>
#import "BookmarkController.h"
#define MD_DEBUG 0
#if MD_DEBUG
#define MDLog(...) NSLog(__VA_ARGS__)
#else
#define MDLog(...)
#endif
@implementation CHMAppController
+ (void)initialize {
[NSURLProtocol registerClass:[CHMITSSURLProtocol class]];
}
- (void)awakeFromNib {
MDLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
NSString *path = [[NSBundle mainBundle] pathForResource:@"textEncodings" ofType:@"plist"];
if (path == nil) {
NSBeep();
NSLog(@"[%@ %@] failed to find textEncodings.plist!", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
return;
}
NSArray *plist = [NSArray arrayWithContentsOfFile:path];
if (plist == nil) {
NSBeep();
NSLog(@"[%@ %@] failed to load textEncodings.plist!", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
return;
}
NSUInteger sectionCount = plist.count;
NSUInteger sectionIndex = 0;
NSUInteger itemIndex = 0;
for (NSArray *section in plist) {
itemIndex = 0;
for (NSDictionary *item in section) {
NSString *title = [item objectForKey:@"title"];
NSString *encodingName = [item objectForKey:@"name"];
MDLog(@"[%@ %@] encodingName == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), encodingName);
CFStringEncoding cfStringEncoding = kCFStringEncodingInvalidId;
NSStringEncoding nsStringEncoding = 0;
if (encodingName.length) {
cfStringEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)encodingName);
if (cfStringEncoding == kCFStringEncodingInvalidId) {
NSLog(@"[%@ %@] *** WARNING: CFStringConvertIANACharSetNameToEncoding() returned kCFStringEncodingInvalidId for \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), encodingName);
} else {
MDLog(@"cfStringEncoding == %lu", (unsigned long)cfStringEncoding);
MDLog(@"cfStringEncoding == \"%@\"", CFStringGetNameOfEncoding(cfStringEncoding));
}
nsStringEncoding = CFStringConvertEncodingToNSStringEncoding(cfStringEncoding);
MDLog(@"nsStringEncoding == %lu", (unsigned long)nsStringEncoding);
MDLog(@"locDescrp == %@", [NSString localizedNameOfStringEncoding:nsStringEncoding]);
}
if (cfStringEncoding != kCFStringEncodingInvalidId || encodingName.length == 0) {
NSMenuItem *newItem = [[[NSMenuItem alloc] init] autorelease];
[newItem setTitle:title];
[newItem setAction:@selector(changeEncoding:)];
[newItem setTarget:nil];
[newItem setTag:(encodingName.length == 0 ? 0 : nsStringEncoding)];
if (encodingName.length) [newItem setRepresentedObject:encodingName];
[textEncodingMenu addItem:newItem];
itemIndex++;
}
}
if (itemIndex) sectionIndex++;
if (sectionIndex < sectionCount) {
[textEncodingMenu addItem:[NSMenuItem separatorItem]];
}
}
}
- (void)applicationWillTerminate:(NSNotification *)notification {
[NSURLProtocol unregisterClass:[CHMITSSURLProtocol class]];
}
#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=iCHM&amount=4%2e99&no_shipping=0&no_note=1&tax=0¤cy_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8"]];
}
- (IBAction)homepage:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.robinlu.com/blog/ichm"]];
}
#pragma mark bookmark
- (BookmarkController *)bookmarkController {
if (bookmarkController == nil) bookmarkController = [[BookmarkController alloc] init];
return bookmarkController;
}
@end