-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMicrosoftOfficePluginSource.m
173 lines (132 loc) · 6.58 KB
/
MicrosoftOfficePluginSource.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
//
// MicrosoftOfficePluginSource.m
// MicrosoftOfficePlugin
//
// Created by Patrick Robertson on 17/06/2011.
// Copyright Patrick Robertson 2011. All rights reserved.
//
#import "MicrosoftOfficePluginSource.h"
#define kWordBundleId @"com.microsoft.Word"
#define kExcelBundleId @"com.microsoft.Excel"
#define kPowerpointBundleId @"com.microsoft.Powerpoint"
#define kOfficeBundleId @"com.microsoft.office"
#define kOffice2016RecentDocumentsPlist @"~/Library/Containers/%@/Data/Library/Preferences/%@.securebookmarks.plist"
@implementation MicrosoftOfficePluginSource
- (BOOL)indexIsValidFromDate:(NSDate *)indexDate forEntry:(NSDictionary *)theEntry{
return YES;
}
- (NSImage *) iconForEntry:(NSDictionary *)dict{
return nil;
}
- (NSArray *) objectsForEntry:(NSDictionary *)theEntry{
return nil;
}
- (BOOL)loadChildrenForObject:(QSObject *)object {
// Structure of the com.microsoft.office.plist file — where the recent docs are stored (MS Office 2011)
NSDictionary *IDPreferenceValuePairs = @{kWordBundleId: @"14\\File MRU\\MSWD",
kExcelBundleId: @"\\File MRU\\XCEL",
kPowerpointBundleId: @"\\File MRU\\PPT3"};
NSString *preferencesValue = nil, *bundleIdentifier = nil;
// Find the correct preferences value for this app (bundle ID)
NSString *path = [object singleFilePath];
bundleIdentifier = [[NSBundle bundleWithPath:path] bundleIdentifier];
preferencesValue = [IDPreferenceValuePairs objectForKey:bundleIdentifier];
// incase something went wrong
if (!preferencesValue) {
return NO;
}
// ms2016
NSDictionary *ms2016Dict = [NSDictionary dictionaryWithContentsOfFile:[[NSString stringWithFormat:kOffice2016RecentDocumentsPlist, bundleIdentifier, bundleIdentifier] stringByExpandingTildeInPath]];
NSMutableArray *documentsArray = [[NSMutableArray alloc] initWithCapacity:20];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *recentDocuments = nil;
if (ms2016Dict) { // MS Office 2016
NSArray *vals = [[ms2016Dict allValues] sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *d1, NSDictionary *d2) {
return -[[d1 objectForKey:@"kLastUsedDateKey"] compare:[d2 objectForKey:@"kLastUsedDateKey"]];
}];
for (NSDictionary *fileDict in vals) {
NSData *d = [fileDict objectForKey:@"kBookmarkDataKey"];
NSDictionary *dd = [NSURL resourceValuesForKeys:@[NSURLPathKey] fromBookmarkData:d];
if (![dd count]) {
continue;
}
NSString *posixPath = [dd objectForKey:NSURLPathKey];
if ([fm fileExistsAtPath:posixPath]) {
[documentsArray addObject:posixPath];
}
}
} else {
// synchronise the file to save the latest changes
CFPreferencesSynchronize((CFStringRef) kOfficeBundleId,
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
// Get an array of recent docs from the office .plist (MS 2011 case)
recentDocuments = [(NSArray *)CFPreferencesCopyValue((CFStringRef) preferencesValue,
(CFStringRef) kOfficeBundleId,
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost) autorelease];
NSData *fileData;
NSString *filepath;
if (recentDocuments) { // MS Office 2011
for(NSDictionary *eachFile in recentDocuments) {
fileData = [eachFile objectForKey:@"File Alias"];
filepath = [[NDAlias aliasWithData:fileData] quickPath];
if (filepath == nil) {
// couldn't resolve bookmark, so skip
continue;
}
if ([fm fileExistsAtPath:filepath]) {
[documentsArray addObject:filepath];
}
if ([documentsArray count] > 20) {
break;
}
}
} else { // MS Office 2008
// Recent docs are stored in different key/value pairs for MS 2008
NSDictionary *IDPreferenceValuePairs = @{kWordBundleId: @"2008\\File Aliases\\MSWD",
kExcelBundleId: @"2008\\File Aliases\\XCEL",
kPowerpointBundleId: @"\\File Aliases\\PPT3"};
preferencesValue = [IDPreferenceValuePairs objectForKey:bundleIdentifier];
// incase something went wrong
if (!preferencesValue) {
return NO;
}
NSData *fileData;
NSUInteger i;
@autoreleasepool {
for (i = 1 ; i <= 100; ++i) {
// MS Office '08 recent docs are stored in the format 2008\\FileAliases\\MSWD1,2,3...
fileData = [(NSData *)CFPreferencesCopyValue((CFStringRef) [NSString stringWithFormat:@"%@%lu",preferencesValue,(unsigned long)i],
(CFStringRef) kOfficeBundleId,
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost) autorelease];
// break if there are no more key/value pairs
if (!fileData) {
break;
}
filepath = [[NDAlias aliasWithData:fileData] quickPath];
if (filepath == nil) {
// couldn't resolve bookmark, so skip
continue;
}
if ([fm fileExistsAtPath:filepath]) {
[documentsArray addObject:filepath];
}
}
} // end @autoreleasepool
} // End MS Office 2008
}
// If there's been some kind of problem
if (!documentsArray) {
return NO;
}
NSArray *newChildren = [QSObject fileObjectsWithPathArray:documentsArray];
for(QSObject * child in newChildren) {
[child setObject:bundleIdentifier forMeta:@"QSPreferredApplication"];
}
[object setChildren:newChildren];
[documentsArray release];
return YES;
}
@end