-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSIProductMO.m
182 lines (152 loc) · 6.38 KB
/
SIProductMO.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
//
// Copyright (c) 2013 Hannes Juutilainen. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "SIProductMO.h"
@interface SIProductMO ()
// Private interface goes here.
@end
@implementation SIProductMO
- (NSString *)statusDescription
{
if (self.productIsDeprecatedValue)
return @"Deprecated";
else
return @"Active";
}
- (SIDistributionMO *)preferredDistribution
{
/*
Get the preferred language code from preferences
*/
NSString *preferredLanguage = [[NSUserDefaults standardUserDefaults] stringForKey:@"preferredLanguageForDistFiles"];
/*
Get a display representation for the language code
*/
NSString *preferredLangdisplayName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:preferredLanguage];
/*
Check if this product has a distribution in the preferred language
*/
NSPredicate *prefLangPredicate = [NSPredicate predicateWithFormat:@"(distributionLanguage == %@) OR (distributionLanguageDisplayName == %@)", preferredLanguage, preferredLangdisplayName];
NSSet *matchingLangs = [self.distributions filteredSetUsingPredicate:prefLangPredicate];
if ([matchingLangs count] == 1) {
return [matchingLangs anyObject];
}
/*
Did not find a dist in the preferred language, try English
*/
else if ([matchingLangs count] == 0) {
NSPredicate *englishPredicate = [NSPredicate predicateWithFormat:@"(distributionLanguage == %@) OR (distributionLanguageDisplayName == %@)", @"en", @"English"];
NSSet *englishLangs = [self.distributions filteredSetUsingPredicate:englishPredicate];
return [englishLangs anyObject];
}
else {
NSLog(@"Found multiple dist files for a lang code. This shouldn't happen...");
}
return nil;
}
- (NSString *)pkginfoFilename
{
/*
Suggest a sensible filename based on:
- Display Name
- Product ID
- Version
*/
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *whiteSpaceReplacement = @"-";
NSString *joinWithString = @"-";
NSArray *nameComponents = @[self.productTitle, self.productVersion, self.productID];
NSMutableArray *processedComponents = [[NSMutableArray alloc] init];
for (NSString *component in nameComponents) {
NSString *newValue = [component stringByReplacingOccurrencesOfString:@" " withString:whiteSpaceReplacement];
[processedComponents addObject:newValue];
}
NSString *newName = [processedComponents componentsJoinedByString:joinWithString];
NSString *fileExtension = [defaults stringForKey:@"pkginfoDefaultFileExtension"];
NSString *filenameWithExtension = [NSString stringWithFormat:@"%@%@", newName, fileExtension];
return filenameWithExtension;
}
- (NSString *)plainTextFromHTMLString:(NSString *)htmlString
{
/*
Convert the htmlString to data, parse it to a new NSAttributedString
and return the plain text value. Rude, I know...
*/
NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *html = [[NSAttributedString alloc] initWithHTML:data documentAttributes:nil];
return [html string];
}
- (NSDictionary *)pkginfoDictionary
{
/*
Create a pkginfo representation of current values.
If some field is empty, we don't want it at all in the resulting string.
*/
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"apple_update_metadata" forKey:@"installer_type"];
if (self.productID) [dict setObject:self.productID forKey:@"name"];
if (self.productTitle) [dict setObject:self.productTitle forKey:@"display_name"];
if (self.productVersion) [dict setObject:self.productVersion forKey:@"version"];
/*
Set the description
*/
NSString *preferredDescription = @"";
if ([defaults boolForKey:@"pkginfoPrefillDescription"]) {
if ([defaults integerForKey:@"pkginfoPrefillDescriptionType"] == 0) {
preferredDescription = [self plainTextFromHTMLString:self.productDescription];
} else if ([defaults integerForKey:@"pkginfoPrefillDescriptionType"] == 1) {
preferredDescription = self.productDescription;
}
}
[dict setObject:preferredDescription forKey:@"description"];
/*
Set the default munki catalogs
*/
NSArray *catalogDictsFromDefaults = [[NSUserDefaults standardUserDefaults] arrayForKey:@"defaultMunkiCatalogs"];
NSArray *catalogs = [catalogDictsFromDefaults valueForKeyPath:@"title"];
if ([catalogs count] > 0) {
[dict setObject:catalogs forKey:@"catalogs"];
}
/*
Only add the unattended boolean if it's enabled.
Munki defaults to false if it is missing.
*/
BOOL unattendedInstall = ([defaults boolForKey:@"pkginfoUnattendedInstall"]) ? YES : NO;
if (unattendedInstall) {
[dict setValue:(id)kCFBooleanTrue forKey:@"unattended_install"];
} else {
[dict setValue:(id)kCFBooleanFalse forKey:@"unattended_install"];
}
return [NSDictionary dictionaryWithDictionary:dict];
}
- (NSString *)pkginfo
{
/*
Create a string representation of the dictionary
*/
NSError *error;
id plist = [NSPropertyListSerialization dataWithPropertyList:self.pkginfoDictionary
format:NSPropertyListXMLFormat_v1_0
options:NSPropertyListImmutable
error:&error];
NSString *returnString = [[NSString alloc] initWithData:plist encoding:NSUTF8StringEncoding];
return returnString;
}
- (NSString *)productTitleWithVersion
{
return [NSString stringWithFormat:@"%@ - Version %@", self.productTitle, self.productVersion];
}
@end