/
PasteSheet.m
240 lines (196 loc) · 5.21 KB
/
PasteSheet.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
// LimeChat is copyrighted free software by Satoshi Nakagawa <psychs AT limechat DOT net>.
// You can redistribute it and/or modify it under the terms of the GPL version 2 (see the file GPL.txt).
#import "PasteSheet.h"
#import "NSStringHelper.h"
static NSArray* SYNTAXES;
static NSDictionary* SYNTAX_EXT_MAP;
@interface PasteSheet (Private)
- (void)setRequesting:(BOOL)value;
- (int)tagFromSyntax:(NSString*)s;
- (NSString*)syntaxFromTag:(int)tag;
@end
@implementation PasteSheet
@synthesize nick;
@synthesize uid;
@synthesize cid;
@synthesize originalText;
@synthesize syntax;
@synthesize command;
@synthesize size;
@synthesize editMode;
@synthesize isShortText;
- (id)init
{
if (self = [super init]) {
[NSBundle loadNibNamed:@"PasteSheet" owner:self];
if (!SYNTAXES) {
SYNTAXES = [[NSArray arrayWithObjects:
@"privmsg", @"notice", @"c", @"css", @"diff", @"html",
@"java", @"javascript", @"php", @"plain text", @"python",
@"ruby", @"sql", @"shell script", @"perl", @"haskell",
@"scheme", @"objective-c",
nil] retain];
}
if (!SYNTAX_EXT_MAP) {
SYNTAX_EXT_MAP = [[NSDictionary dictionaryWithObjectsAndKeys:
@".h", @"c",
@".css", @"css",
@".diff", @"diff",
@".lhs", @"haskell",
@".html", @"html",
@".groovy", @"java",
@".js", @"javascript",
@".mm", @"objective-c",
@".ph", @"perl",
@".php[345]", @"php",
@".txt", @"plain_text",
@".pyw", @"python",
@".ru", @"ruby",
@".sls", @"scheme",
@".sh", @"shell script",
@".sql", @"sql",
nil, nil] retain];
}
}
return self;
}
- (void)dealloc
{
[nick release];
[originalText release];
[syntax release];
[command release];
[gist cancel];
[gist autorelease];
[super dealloc];
}
- (void)start
{
if (editMode) {
NSArray* lines = [originalText splitIntoLines];
isShortText = lines.count <= 3;
if (isShortText) {
self.syntax = @"privmsg";
}
[sheet makeFirstResponder:bodyText];
}
[syntaxPopup selectItemWithTag:[self tagFromSyntax:syntax]];
[commandPopup selectItemWithTag:[self tagFromSyntax:command]];
[bodyText setString:originalText];
if (!NSEqualSizes(size, NSZeroSize)) {
[sheet setContentSize:size];
}
[self startSheet];
}
- (void)pasteOnline:(id)sender
{
[self setRequesting:YES];
if (gist) {
[gist cancel];
[gist autorelease];
}
NSString* s = bodyText.string;
NSString* fileType = [SYNTAX_EXT_MAP objectForKey:[self syntaxFromTag:syntaxPopup.selectedTag]];
if (!fileType) {
fileType = @".txt";
}
gist = [GistClient new];
gist.delegate = self;
[gist send:s fileType:fileType private:YES];
}
- (void)sendInChannel:(id)sender
{
[command release];
command = [[self syntaxFromTag:commandPopup.selectedTag] retain];
NSString* s = bodyText.string;
if ([delegate respondsToSelector:@selector(pasteSheet:onPasteText:)]) {
[delegate pasteSheet:self onPasteText:s];
}
[self endSheet];
}
- (void)cancel:(id)sender
{
if ([delegate respondsToSelector:@selector(pasteSheetOnCancel:)]) {
[delegate pasteSheetOnCancel:self];
}
[super cancel:nil];
}
- (void)setRequesting:(BOOL)value
{
errorLabel.stringValue = value ? @"Sending…" : @"";
if (value) {
[uploadIndicator startAnimation:nil];
}
else {
[uploadIndicator stopAnimation:nil];
}
[pasteOnlineButton setEnabled:!value];
[sendInChannelButton setEnabled:!value];
[syntaxPopup setEnabled:!value];
[commandPopup setEnabled:!value];
if (value) {
[bodyText setEditable:NO];
[bodyText setTextColor:[NSColor disabledControlTextColor]];
[bodyText setBackgroundColor:[NSColor windowBackgroundColor]];
}
else {
[bodyText setTextColor:[NSColor textColor]];
[bodyText setBackgroundColor:[NSColor textBackgroundColor]];
[bodyText setEditable:YES];
}
}
- (int)tagFromSyntax:(NSString*)s
{
NSUInteger n = [SYNTAXES indexOfObject:s];
if (n != NSNotFound) {
return n;
}
return -1;
}
- (NSString*)syntaxFromTag:(int)tag
{
if (0 <= tag && tag < SYNTAXES.count) {
return [SYNTAXES objectAtIndex:tag];
}
return nil;
}
#pragma mark -
#pragma mark GistClient Delegate
- (void)gistClient:(GistClient*)sender didReceiveResponse:(NSString*)url
{
[gist autorelease];
gist = nil;
[self setRequesting:NO];
if (url.length) {
[errorLabel setStringValue:@""];
if ([delegate respondsToSelector:@selector(pasteSheet:onPasteURL:)]) {
[delegate pasteSheet:self onPasteURL:url];
}
[self endSheet];
}
else {
[errorLabel setStringValue:@"Could not get an URL from Gist"];
}
}
- (void)gistClient:(GistClient*)sender didFailWithError:(NSString*)error statusCode:(int)statusCode
{
[gist autorelease];
gist = nil;
[self setRequesting:NO];
[errorLabel setStringValue:[NSString stringWithFormat:@"Gist error: %@", error]];
}
#pragma mark -
#pragma mark NSWindow Delegate
- (void)windowWillClose:(NSNotification*)note
{
[syntax release];
[command release];
syntax = [[self syntaxFromTag:syntaxPopup.selectedTag] retain];
command = [[self syntaxFromTag:commandPopup.selectedTag] retain];
NSView* contentView = [sheet contentView];
size = contentView.frame.size;
if ([delegate respondsToSelector:@selector(pasteSheetWillClose:)]) {
[delegate pasteSheetWillClose:self];
}
}
@end