-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTumblrController.m
410 lines (339 loc) · 13.6 KB
/
TumblrController.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// TumblrController.m
// Tumblor
//
// Created by orta on 19/07/2008.
// Copyright 2008 __MyCompanyName__. All rights reserved.
#import "TumblrController.h"
@implementation TumblrController
- (void)awakeFromNib {
magicFooterNumber = 21;
magicToolbarSizeNumber = 92;
superfluousVisualEffects = true;
loggedIn = false;
showingPrefs = false;
[mainWindow setContentBorderThickness: magicFooterNumber forEdge:NSMinYEdge];
[mainWindowContentView setAutoresizesSubviews:YES];
viewControllers = [NSArray arrayWithObjects:textController, imageController, quoteController,
linkController, conversationController, audioController,
videoController, welcomeController, nil ];
toolbarItems = [NSArray arrayWithObjects:textItem, imageItem, quoteItem,
linkItem, conversationItem, audioItem,
videoItem, welcomeItem, nil ];
viewsForContentTypes = [NSArray arrayWithObjects:textView, imageView, quoteView,
linkView, conversationView, audioView,
videoView, welcomeView, nil];
// wants
[viewsForContentTypes retain];
[viewControllers retain];
[toolbarItems retain];
[currentView retain];
[currentViewIndex retain];
[oldViewIndex retain];
[oldView retain];
if([connectionController email] != nil){
log(@"logged in automatically to - %@", [connectionController email]);
log(@"going to index %i", [[[NSUserDefaults standardUserDefaults] objectForKey:@"lastViewIndex"] intValue]);
[self setCurrentViewIndex: [[[NSUserDefaults standardUserDefaults] objectForKey:@"lastViewIndex"] intValue] ];
[self setLoggedIn:YES];
}else{
[self setCurrentViewIndex: [viewsForContentTypes count]-1];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkDown) name:@"ORNetworkDown" object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doubleClickInPhotosBrowser:) name:@"iMediaSelectionDoubleClicked" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doubleClickInPhotosBrowser:) name:@"iMediaSelectionChanged" object:nil];
}
- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar{
// Allow the user to select only our type categories
return [NSArray arrayWithObjects:[textItem itemIdentifier], [imageItem itemIdentifier], [quoteItem itemIdentifier],
[linkItem itemIdentifier], [conversationItem itemIdentifier], [audioItem itemIdentifier],
[videoItem itemIdentifier], [welcomeItem itemIdentifier], nil ];
}
- (IBAction)toolbarClicked:(id)sender {
if([sender tag] != [currentViewIndex intValue]){
[self setCurrentViewIndex: [sender tag] ];
}
}
- (int)currentViewIndex{
return [currentViewIndex intValue];
}
- (void)setCurrentViewIndex:(int)newIndex {
log(@"%s new %i old %@", _cmd, newIndex, currentViewIndex);
oldView = currentView;
oldViewIndex = currentViewIndex;
[currentViewIndex retain];
//remove preferences view
if(showingPrefs){
[self togglePrefs:nil];
}
if([currentViewIndex intValue] == newIndex){
//TODO : this might break tings
// [[viewControllers objectAtIndex:[currentViewIndex intValue]] hasBecomeKeyView];
if(newIndex != 0){
return;
}
}
int direction;
if(newIndex > [currentViewIndex intValue]) direction = -1;
else direction = 1;
if(_inverseDirectionOnce){
direction *= -1;
_inverseDirectionOnce = false;
}
if(currentView != nil){
//out with the old
if(superfluousVisualEffects){
NSRect newFrame = [oldView frame];
newFrame.origin.x = newFrame.size.width * direction;
// [[oldView animator] setFrame:newFrame];
[oldView setFrame:newFrame];
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(removeOldView) userInfo:nil repeats:false];
}else{
[self removeOldView];
}
}
//in with the new
currentViewIndex = [NSNumber numberWithInt:newIndex];
[[NSUserDefaults standardUserDefaults] setObject:currentViewIndex forKey:@"lastViewIndex"];
// if(newIndex != [toolbarItems count]-1){
[toolBar setSelectedItemIdentifier: [ [toolbarItems objectAtIndex: newIndex] itemIdentifier] ];
// }
currentView = [viewsForContentTypes objectAtIndex: [currentViewIndex intValue]];
[mainWindowContentView addSubview: currentView];
[currentView setAutoresizingMask:(NSViewHeightSizable|NSViewWidthSizable)];
NSRect resizedFrame = [mainWindowContentView frame];
resizedFrame.size.height -= magicFooterNumber;
resizedFrame.origin.y += magicFooterNumber;
[mainWindow setInitialFirstResponder: currentView ];
if(superfluousVisualEffects){
NSRect newFrame = resizedFrame;
newFrame.origin.x = newFrame.size.width * direction * -1;
// [currentView setFrame: newFrame];
// [[currentView animator] setFrame: resizedFrame];
[currentView setFrame:resizedFrame];
[[viewControllers objectAtIndex:[currentViewIndex intValue]] hasBecomeKeyView];
}else{
[currentView setFrame: resizedFrame];
[[viewControllers objectAtIndex:[currentViewIndex intValue]] hasBecomeKeyView];
}
//g [mainWindow setInitialFirstResponder: currentView ];
}
- (IBAction)appHelp:(id)sender {
welcomeController.defaultPage = @"welcome";
if([currentViewIndex intValue] != 7){
[self setCurrentViewIndex:7];
}else{
[welcomeController hasBecomeKeyView];
}
}
- (IBAction)advancedUsage:(id)sender {
welcomeController.defaultPage = @"advanced";
if([currentViewIndex intValue] != 7){
[self setCurrentViewIndex:7];
}else{
[welcomeController hasBecomeKeyView];
}
}
- (IBAction)nextTab:(id)sender {
//its 2, one for the fence post and another for the welcome view
if([currentViewIndex intValue] >= ([viewsForContentTypes count]-2)){
_inverseDirectionOnce = true;
[self setCurrentViewIndex: 0];
}else{
[self setCurrentViewIndex: [currentViewIndex intValue]+1];
}
}
- (IBAction)previousTab:(id)sender {
if([currentViewIndex intValue] == 0){
_inverseDirectionOnce = true;
[self setCurrentViewIndex: ([viewsForContentTypes count]-2)];
}else{
[self setCurrentViewIndex: [currentViewIndex intValue]-1];
}
}
- (IBAction)togglePrefs:(id)sender{
NSRect newFrame;
//[prefsView toggleFilter];
if(showingPrefs){
newFrame = [[mainWindow contentView] frame];
newFrame.origin.y = newFrame.size.height;
[prefsView setFrame:newFrame];
showingPrefs = false;
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(removeOldView) userInfo:nil repeats:false];
}else{
newFrame = [[mainWindow contentView] frame];
newFrame.origin.y = newFrame.size.height;
[prefsView setFrame:newFrame];
[prefsView setAutoresizingMask:(NSViewHeightSizable|NSViewWidthSizable)];
[mainWindowContentView addSubview:prefsView];
newFrame = [[mainWindow contentView] frame];
[prefsView setFrame:newFrame];
showingPrefs = true;
}
}
- (id)currentViewController{
return [viewControllers objectAtIndex:[currentViewIndex intValue]];
}
- (void) removeOldView{
[oldView removeFromSuperview];
[prefsView removeFromSuperview];
[[viewControllers objectAtIndex:[oldViewIndex intValue]] resignedAsKeyView];
}
- (IBAction)toggleExtraPostInfo:(id)sender{
int sizeOfMorePrefs = 25;
if(showingExtraInfo){
[moreOptionsButton setTitle:[NSString localizedStringWithFormat:@"More Info"]];
showingExtraInfo = false;
[moreOptionsView removeFromSuperview];
magicFooterNumber -= sizeOfMorePrefs;
NSRect newFrame = [mainWindow frame];
newFrame.size.height -= sizeOfMorePrefs;
[mainWindow setFrame:newFrame display:true animate:true];
NSRect resizedFrame = [mainWindowContentView frame];
resizedFrame.size.height -= magicFooterNumber;
resizedFrame.origin.y += magicFooterNumber;
[currentView setFrame:resizedFrame];
}else{
[moreOptionsButton setTitle:[NSString localizedStringWithFormat:@"Hide Info"]];
showingExtraInfo = true;
magicFooterNumber += sizeOfMorePrefs;
NSRect newFrame = [mainWindow frame];
newFrame.size.height += sizeOfMorePrefs;
[mainWindow setFrame:newFrame display:true animate:true];
[[mainWindow contentView] addSubview:moreOptionsView];
//resize current view
newFrame = [[mainWindow contentView] frame];
newFrame.origin.y += magicFooterNumber;
// newFrame.size.height -= sizeOfMorePrefs;
newFrame.size.height -= magicFooterNumber;
[currentView setFrame:newFrame];
//add prefs
newFrame.origin.x = 0;
newFrame.origin.y = sizeOfMorePrefs - 4;
newFrame.size.height = [moreOptionsView frame].size.height;
newFrame.size.width = [[mainWindow contentView] frame].size.width;
[moreOptionsView setFrame:newFrame];
}
}//toggleExtraPostInfo
- (void) setLoggedIn:(bool) newBool{
loggedIn = newBool;
}
- (bool)loggedIn{
return loggedIn;
}
- (void)importTextContentWithBody:(NSString *)aBody title:(NSString *) aTitle summary:(NSString *)aSummary
link:(NSString *)aLink source:(NSString *) aSource asType:(int)type{
if(type == 99){
type = [[[NSUserDefaults standardUserDefaults] objectForKey:@"importedTextIndex"] intValue];
}
switch (type) {
case 1:// quote
[quoteController setQuote: aBody];
[quoteController setDescription: aSource];
[self setCurrentViewIndex:2];
break;
case 2:// link
[linkController setLink: aLink];
[linkController setTitle: aTitle];
[linkController setDescription: aBody];
[self setCurrentViewIndex:3];
break;
case 3:// conversation
[conversationController setConversation:aBody];
[conversationController setTitle:aTitle];
[self setCurrentViewIndex:4];
break;
case 0: //Text import
default:
[textController setBody:aBody];
[textController setTitle:aTitle];
[self setCurrentViewIndex:0];
break;
}
}
- (void)importFileContentWithPath:(NSString *)path andDescription:(NSString*) description{
log(@"importing file with path %@", path );
int type = [TumblrController fileTypeForFilePath:path];
switch (type) {
case 1:
// image
[imageController setSource: path];
[imageController setCaption: description];
[self setCurrentViewIndex:1];
break;
case 6:
[videoController setSource: path];
[videoController setCaption: description];
[self setCurrentViewIndex:6];
// video
break;
case 5:
[audioController setSource: path];
[audioController setCaption: description];
[self setCurrentViewIndex:5];
// audio
break;
default:
log(@"unknown file type");
break;
}
}
- (IBAction)newTextPost:(id)sender{
[textController setBody:@""];
[self setCurrentViewIndex:0];
}
#pragma mark iMedia Delegate methods, app wide
- (void)iMediaConfiguration:(iMediaConfiguration *)configuration doubleClickedSelectedObjects:(NSArray*)selection{
log(@"click!");
[self importFileContentWithPath: [[selection objectAtIndex:0] stringValue] andDescription:@""];
}
- (void)iMediaConfiguration:(iMediaConfiguration *)configuration didSelectNode:(iMBLibraryNode *)node{
// this is when you change the type of library we're looking in
log(@"blick!");
}
- (BOOL)iMediaConfiguration:(iMediaConfiguration *)configuration willUseMediaParser:(NSString *)parserClassname forMediaType:(NSString *)media{
//these are useless, get lost.
if([parserClassname compare:@"iMBiLifeSoundEffectsParser"] == NSOrderedSame ) return NO;
if([parserClassname compare:@"iMBiMovieSoundEffectsParser"]== NSOrderedSame) return NO;
if([parserClassname compare:@"iMBLibrarySoundsParser"]== NSOrderedSame) return NO;
return YES;
}
- (void) networkDown{
[self setLoggedIn:false];
}
- (void) doubleClickInPhotosBrowser: (NSNotification *)notification{
if([[[notification userInfo] objectForKey:@"Selection"] objectAtIndex:0]){
NSString* imagePath = [[[[notification userInfo] objectForKey:@"Selection"] objectAtIndex:0] objectForKey:@"ImagePath"];
NSString* caption = [[[[notification userInfo] objectForKey:@"Selection"] objectAtIndex:0] objectForKey:@"Caption"];
[self importFileContentWithPath:imagePath andDescription: caption];
}
}
+ (int) fileTypeForFilePath: (NSString *) filename{
NSString *extension = [filename pathExtension];
NSString * filetype;
for ( filetype in [NSImage imageFileTypes] ) {
if([extension caseInsensitiveCompare: filetype] == NSOrderedSame){
log(@"found image file type");
return 1;
}
}
if (([extension caseInsensitiveCompare:@"mp3"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"aiff"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"wav"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"aac"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"m4a"] == NSOrderedSame)){
log(@"found audio filetype");
return 5;
}
if (([extension caseInsensitiveCompare:@"avi"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"mpg"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"png"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"mp4"] == NSOrderedSame) ||
([extension caseInsensitiveCompare:@"m4v"] == NSOrderedSame)){
log(@"found video filetype");
return 6;
}
log(@"not found anything for file type: %@", extension);
return 99;
}
@end