forked from slip/CourseBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBTemplateController.m
233 lines (191 loc) · 7.34 KB
/
CBTemplateController.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
//
// CBTemplateController.m
// CourseBuilder
//
// Created by Ian Kennedy on 12/3/08.
// Copyright 2008 normal software. All rights reserved.
//
#import "CBTemplateController.h"
@implementation CBTemplateController
@synthesize defaultTemplates;
- (id) init
{
if (self = [super init]) {
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DefaultTemplates" ofType:@"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
if (!temp) {
[errorDesc release];
}
self.defaultTemplates = [NSMutableArray arrayWithArray:(NSArray *)temp];
}
return self;
}
- (void)dealloc
{
[path release];
path = nil;
[super dealloc];
}
- (IBAction)addTemplate:(id)sender
{
// create the newTemplate and add it to the defaultTemplates
NSDictionary *newTemplate;
NSString *templateType = @"User";
NSString *templatePath;
NSString *newTemplatePath;
NSString *templateName;
NSString *pathToCustomTemplates = [[self applicationSupportFolder] stringByAppendingPathComponent:@"/_templates"];
// check to see if the _templates directory exists
if (![[NSFileManager defaultManager] fileExistsAtPath:pathToCustomTemplates]) {
NSLog(@"Custom templates directory doesn't exist. Creating it.");
NSLog(@"failed: %@::%@ at line %d", [self class], NSStringFromSelector(_cmd), __LINE__);
[[NSFileManager defaultManager] createDirectoryAtPath:pathToCustomTemplates attributes:nil];
}
// we want to allow selection of swfs or flas
NSArray *fileTypes = [NSArray arrayWithObject:@"fla"];
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setAllowsMultipleSelection:NO];
// get user input fla from an open panel
int result = [oPanel runModalForDirectory:NSHomeDirectory()
file:nil
types:fileTypes];
if (result == NSOKButton) {
NSArray *filesToAdd = [oPanel filenames];
templatePath = [filesToAdd objectAtIndex:0];
templateName = [[templatePath lastPathComponent] stringByDeletingPathExtension];
}
else
{
return;
}
NSString *templateLastPathComponent;
templateLastPathComponent = [templatePath lastPathComponent];
newTemplatePath = [pathToCustomTemplates stringByAppendingPathComponent:templateLastPathComponent];
// create the template dictionary using the user input
newTemplate = [NSDictionary dictionaryWithObjectsAndKeys:templateName, @"Name", newTemplatePath, @"Path", templateType, @"Type", nil];
// add to array controller
[templatesController addObject:newTemplate];
//NSLog(@"trying to copy %@ to %@.", templatePath, newTemplatePath);
// copy the fla
if(![[NSFileManager defaultManager] copyItemAtPath:templatePath
toPath:newTemplatePath
error:nil])
{
NSLog(@"CBTemplateController failed to copy file.");
NSLog(@"failed: %@::%@ at line %d", [self class], NSStringFromSelector(_cmd), __LINE__);
}
// write the plist
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DefaultTemplates" ofType:@"plist"];
[defaultTemplates writeToFile:plistPath atomically:YES];
}
- (void)createTemplateFromFile:(NSString *)pathToFile
{
// TODO: templateFromFile
}
- (IBAction)removeTemplate:(id)sender
{
NSDictionary *selectedTemplate = [defaultTemplates objectAtIndex:[templatesTableView selectedRow]];
// don't allow user to delete default templates
if ([[selectedTemplate valueForKey:@"Type"] isEqualTo:@"Default"]) {
NSBeep();
return;
}
NSString *selectedPath = [selectedTemplate valueForKey:@"Path"];
NSString *selectedFile = [selectedPath lastPathComponent];
[templatesController remove:sender];
// write the plist
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DefaultTemplates" ofType:@"plist"];
[defaultTemplates writeToFile:plistPath atomically:YES];
NSArray *filesToDelete = [NSArray arrayWithObject:selectedFile];
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation
source:[selectedPath stringByDeletingLastPathComponent]
destination:@""
files:filesToDelete
tag:NULL];
}
- (IBAction)editTemplate:(id)sender
{
NSString *pathToTemplates = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"flash_templates"];
NSDictionary *selectedTemplate = [defaultTemplates objectAtIndex:[templatesTableView selectedRow]];
NSString *selectedPath = [selectedTemplate valueForKey:@"Path"];
NSFileManager *fm = [NSFileManager defaultManager];
// if the selected template is a default template, change the path
if ([[selectedTemplate valueForKey:@"Type"] isEqualTo:@"Default"]) {
selectedPath = [pathToTemplates stringByAppendingPathComponent:selectedPath];
}
// open the file if it exists
if ([fm fileExistsAtPath:selectedPath]) {
[[NSWorkspace sharedWorkspace] openFile:selectedPath];
return;
}
// if not, tell the user it doesn't exist
else {
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"File doesn't exist"];
[alert setInformativeText:@"That template doesn't seem to exist."];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
[alert release];
return;
}
}
- (NSString *)applicationSupportFolder {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
return [basePath stringByAppendingPathComponent:@"CourseBuilder"];
}
//===========================================================
// path
//===========================================================
- (NSString *)path
{
NSDictionary *selectedTemplate = [defaultTemplates objectAtIndex:[templatesTableView selectedRow]];
NSString *tempPath = [selectedTemplate valueForKey:@"Path"];
if ([[selectedTemplate valueForKey:@"Type"] isEqualToString:@"Default"]) {
path = [[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"/flash_templates"] stringByAppendingPathComponent:tempPath];
} else {
path = tempPath;
}
return path;
}
- (void)setPath:(NSString *)aPath
{
if (path != aPath) {
[path release];
path = [aPath copy];
}
}
//===========================================================
// table delegate
//===========================================================
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return self.defaultTemplates.count;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
return [[defaultTemplates objectAtIndex:row] valueForKey:@"Name"];
}
//- (void)tableView:(NSTableView *)tableView
// setObjectValue:(id)object
// forTableColumn:(NSTableColumn *)tableColumn
// row:(NSInteger)row
//{
// [defaultTemplates replaceObjectsAtIndexes:[NSIndexSet indexSetWithIndex:row]
// withObjects:[NSArray arrayWithObject:object]];
//}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
if ([templatesTableView selectedRow] != -1) {
//NSLog(@"CBTemplateController[self path] : %@", [self path]);
}
}
@end