-
Notifications
You must be signed in to change notification settings - Fork 0
/
K2LinkBackSupport.m
259 lines (191 loc) · 8.96 KB
/
K2LinkBackSupport.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
//
// K2LinkBackSupport.m
// K2LinkBackSupport
//
// Created by King Chung Huang on 3/12/05.
// Copyright 2005, 2007 King Chung Huang. All rights reserved.
//
#import "K2LinkBackSupport.h"
#import <LinkBack/LinkBack.h>
#import <unistd.h>
#import <mach-o/dyld.h>
#import "APELite.h"
#import <libxml/tree.h>
#import "SFDDrawableInfo_LinkBack.h"
#import "SFDCanvas_LinkBack.h"
struct objc_method {
char *method_name;
char *method_types;
void *method_imp;
};
#pragma mark Swizzled methods typedefs
extern struct objc_method *class_getInstanceMethod(void *inClass, void *selector);
typedef void *(*BGC_makeInfoFromPasteboardProcPtr)(id inObj, SEL inSel, NSPasteboard *pasteboard, BOOL style);
typedef void (*NSO_deallocProcPtr)(id inObj, SEL inSel);
typedef void (*BGC_beginEditingRepWithEventProcPtr)(id inObj, SEL inSel, id rep, NSEvent *event);
typedef BOOL (*SFDRep_isEditableProcPtr)(id inObj, SEL inSel);
typedef void *(*SLDC_importDrawablesFromPasteboard1ProcPtr)(id inObj, SEL inSel, NSPasteboard *pasteboard, id type, BOOL includeFormatting, id *errors);
typedef BOOL (*BGC_validateMenuItemProcPtr)(id inObj, SEL inSel, NSMenuItem *cell);
typedef void *(*SFDDI_initWithXMLUnarchiverProcPtr)(id inObj, SEL inSel, SFAXMLUnarchiver *unarchiver, xmlNodePtr node, unsigned long version);
typedef void (*SFDDI_encodeWithXMLArchievrProcPtr)(id inObj, SEL inSel, SFAXMLArchiver *archiver, xmlNodePtr node, unsigned long version);
typedef void (*SFDDI_deallocProcPtr)(id inObj, SEL inSel);
BGC_makeInfoFromPasteboardProcPtr gBGC_makeInfoFromPasteboard = NULL;
NSO_deallocProcPtr gBGC_dealloc = NULL;
BGC_beginEditingRepWithEventProcPtr gBGC_beginEditingRepWithEvent = NULL;
SFDRep_isEditableProcPtr gSFDImageRep_editable = NULL;
SLDC_importDrawablesFromPasteboard1ProcPtr gSLDC_importDrawablesFromPasteboard1 = NULL;
BGC_validateMenuItemProcPtr gBGC_validateMenuItem = NULL;
SFDDI_initWithXMLUnarchiverProcPtr gSFDDI_initWithXMLUnarchiver = NULL;
SFDDI_encodeWithXMLArchievrProcPtr gSFDDI_encodeWithXMLArchiver = NULL;
SFDDI_deallocProcPtr gSFDDI_dealloc = NULL;
#pragma mark Swizzled methods implementation
void *LB_BGC_makeInfoFromPasteboard(id inObj, SEL inSel, NSPasteboard *pasteboard, BOOL style) {
id info = gBGC_makeInfoFromPasteboard(inObj, inSel, pasteboard, style);
if ([info respondsToSelector:@selector(setLinkBackData:)] && [[pasteboard types] containsObject:LinkBackPboardType]) {
id lbd = [pasteboard propertyListForType:LinkBackPboardType];
[info performSelector:@selector(setLinkBackData:) withObject:lbd];
}
return info;
}
void *LB_BGC_dealloc(id canvas, SEL inSel) {
if ([canvas respondsToSelector:@selector(deallocLinks)]) {
[canvas performSelector:@selector(deallocLinks)];
}
gBGC_dealloc(canvas, inSel);
}
void *LB_BGC_beginEditingRepWithEvent(id inObj, SEL inSel, id rep, NSEvent *event) {
NSLog(@"LB_BGC_beginEditingRepWithEvent");
if ([rep respondsToSelector:@selector(info)]) {
id info = [rep performSelector:@selector(info)];
if ([info respondsToSelector:@selector(hasLinkBackData)] && [info performSelector:@selector(hasLinkBackData)]) {
id canvas = inObj;
NSLog(@"YES");
[canvas beginLinkBackForInfo:info canvas:[rep performSelector:@selector(canvas)]];
} else {
NSLog(@"NO");
gBGC_beginEditingRepWithEvent(inObj, inSel, rep, event);
}
}
}
BOOL LB_SFDImageRep_editable(id rep, SEL inSel) {
NSLog(@"LB_SFDImageRep_editable");
if ([rep respondsToSelector:@selector(info)]) {
id info = [rep performSelector:@selector(info)];
if ([info respondsToSelector:@selector(hasLinkBackData)] && [info performSelector:@selector(hasLinkBackData)]) {
NSLog(@"\tYES");
return YES;
} else {
NSLog(@"\tNO");
return gSFDImageRep_editable(rep, inSel);
}
}
}
void *LB_SLDC_importDrawablesFromPasteboard1(id inObj, SEL inSel, NSPasteboard *pasteboard, id type, BOOL includeFormatting, id *errors) {
id drawables = gSLDC_importDrawablesFromPasteboard1(inObj, inSel, pasteboard, type, includeFormatting, errors);
NSEnumerator *infos = [drawables objectEnumerator];
id info;
while (info = [infos nextObject]) {
if ([info respondsToSelector:@selector(setLinkBackData:)] && [[pasteboard types] containsObject:LinkBackPboardType]) {
id lbd = [pasteboard propertyListForType:LinkBackPboardType];
[info takeValue:lbd forKey:@"linkBackData"];
}
}
return drawables;
}
BOOL LB_BGC_validateMenuItem(id inObj, SEL inSel, NSMenuItem *cell) {
if ([cell action] == @selector(beginLinkBackForSelection:)) {
BOOL ret;
NSString *title;
id canvas = inObj;
NSArray *infos = [[canvas selectionController] performSelector:@selector(selectedLinkBackInfos)];
if ([infos count] == 1) {
title = [[[infos objectAtIndex:0] linkBackData] linkBackEditMenuTitle];
ret = YES;
} else if ([infos count] > 1) {
title = LinkBackEditMultipleMenuTitle();
ret = YES;
} else {
title = LinkBackEditNoneMenuTitle();
ret = NO;
}
[cell setTitle:title];
return ret;
}
return gBGC_validateMenuItem(inObj, inSel, cell);
}
void *LB_SFDDI_initWithXMLUnarchiver(id inObj, SEL inSel, SFAXMLUnarchiver *unarchiver, xmlNodePtr node, unsigned long version) {
id obj = nil;
if (obj = gSFDDI_initWithXMLUnarchiver(inObj, inSel, unarchiver, node, version)) {
xmlNode *cur_node = node->children;
NSData *lbd = nil;
for (cur_node = node->children; cur_node && (lbd == nil); cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE && strcmp(cur_node->name, "LinkBackData") == 0) {
lbd = [unarchiver createObjectOfClass:[NSData class] fromNode:cur_node];
}
}
if (lbd != nil) {
[obj setLinkBackData:[NSPropertyListSerialization propertyListFromData:lbd mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil]];
}
}
return obj;
}
void LB_SFDDI_encodeWithXMLArchiver(id inObj, SEL inSel, SFAXMLArchiver *archiver, xmlNodePtr node, unsigned long version) {
gSFDDI_encodeWithXMLArchiver(inObj, inSel, archiver, node, version);
id obj = inObj;
id lbd = [obj linkBackData];
if (lbd != nil) {
NSData *data = [NSPropertyListSerialization dataFromPropertyList:lbd format:NSPropertyListBinaryFormat_v1_0 errorDescription:nil];
if (data != nil) {
[archiver encodeObject:data asChildOf:node withName:"LinkBackData" xmlNamespace:nil];
}
}
}
void LB_SFDDI_dealloc(id inObj, SEL inSel) {
id info = inObj;
if ([info respondsToSelector:@selector(deallocLinkBack)]) {
[info performSelector:@selector(deallocLinkBack)];
}
gSFDDI_dealloc(inObj, inSel);
}
#pragma mark Plugin initialization
@implementation K2LinkBackSupport
+ (void)initialize {
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
// Note: Leopard has its own functions. Check compatibility.
if ([identifier isEqualToString:@"com.apple.iWork.Keynote"]) {
Class bgclass;
struct objc_method *method;
if (bgclass = objc_getClass("BGCanvas")) {
[bgclass initialize];
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"makeInfoFromPasteboard:withStyle:")))
gBGC_makeInfoFromPasteboard = APEPatchCreate(method->method_imp, &LB_BGC_makeInfoFromPasteboard);
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"dealloc")))
gBGC_dealloc = APEPatchCreate(method->method_imp, &LB_BGC_dealloc);
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"beginEditingRep:withEvent:")))
gBGC_beginEditingRepWithEvent = APEPatchCreate(method->method_imp, &LB_BGC_beginEditingRepWithEvent);
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"validateMenuItem:")))
gBGC_validateMenuItem = APEPatchCreate(method->method_imp, &LB_BGC_validateMenuItem);
}
if (bgclass = objc_getClass("SFDImageRep")) {
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"isEditable")))
gSFDImageRep_editable = APEPatchCreate(method->method_imp, &LB_SFDImageRep_editable);
}
if (bgclass = objc_getClass("SFDDrawableInfo")) {
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"initWithXMLUnarchiver:node:version:")))
gSFDDI_initWithXMLUnarchiver = APEPatchCreate(method->method_imp, &LB_SFDDI_initWithXMLUnarchiver);
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"encodeWithXMLArchiver:node:version:")))
gSFDDI_encodeWithXMLArchiver = APEPatchCreate(method->method_imp, &LB_SFDDI_encodeWithXMLArchiver);
if (method = class_getInstanceMethod(bgclass, NSSelectorFromString(@"dealloc")))
gSFDDI_dealloc = APEPatchCreate(method->method_imp, &LB_SFDDI_dealloc);
}
} else if ([identifier isEqualToString:@"com.apple.iWork.Pages"]) {
// Removed for public code release
}
// Add the LinkBack menu item
NSMenu *mainMenu = [NSApp mainMenu];
NSMenu *editMenu = [[mainMenu itemAtIndex:2] submenu];
[editMenu insertItem:[NSMenuItem separatorItem] atIndex:[editMenu numberOfItems]];
NSMenuItem *linkBackEdit = [editMenu addItemWithTitle:LinkBackEditNoneMenuTitle() action:@selector(beginLinkBackForSelection:) keyEquivalent:@""];
[linkBackEdit setEnabled:NO];
}
@end