-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoriesController.m
219 lines (172 loc) · 6.05 KB
/
RepositoriesController.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
//
// RepositoriesController.m - Manage Repositories list window
//
#import "RepositoriesController.h"
#import "MyRepository.h"
#import "NSString+MyAdditions.h"
#import "CommonUtils.h"
static NSString* const kDocType = @"repository";
static /*const*/ EditListPrefKeys kPrefKeys =
{ @"repositories", @"repEditShown", @"repPanelFrame"/*, NSURLPboardType*/ };
//----------------------------------------------------------------------------------------
@implementation RepositoriesController
- (id) init
{
kPrefKeys.dragType = NSURLPboardType;
if (self = [super init: &kPrefKeys])
{
}
return self;
}
//----------------------------------------------------------------------------------------
- (id) newObject: (NSPasteboard*) pboard
{
id obj = nil;
NSURL* url = nil;
NSString* str = [pboard stringForType: @"public.url"]; // <= *.webloc file
if (str != nil && (url = [NSURL URLWithString: str]) != nil)
{
// Name of file without extension
str = [[[[NSURL URLFromPasteboard: pboard] path]
lastPathComponent] stringByDeletingPathExtension];
}
if (url == nil)
url = [NSURL URLFromPasteboard: pboard];
if (url)
{
if (str == nil)
str = [[url path] lastPathComponent];
obj = [fAC newObject];
[obj setValue: [url absoluteString] forKey: @"url"];
[obj setValue: str forKey: @"name"];
}
return obj;
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
- (IBAction) newItem: (id) sender
{
#pragma unused(sender)
[fAC addObject:
[NSMutableDictionary dictionaryWithObjectsAndKeys: @"My Repository", @"name",
@"svn://", @"url",
@"", @"user",
@"", @"pass",
nil]];
[super newItem: self];
}
//----------------------------------------------------------------------------------------
- (BOOL) showExtantWindow: (NSString*) name
url: (NSString*) urlString
{
NSURL* const url = StringToURL(urlString, YES);
for_each_obj(en, doc, [[NSDocumentController sharedDocumentController] documents])
{
if ([[doc fileType] isEqualToString: kDocType] &&
[[doc windowTitle] isEqualToString: name] &&
[[doc url] isEqual: url])
{
[doc showWindows];
return TRUE;
}
}
return FALSE;
}
//----------------------------------------------------------------------------------------
- (void) showRepositoryBrowser: (NSDictionary*) repo
alwaysOpenNew: (BOOL) alwaysOpenNew
{
Assert(repo != nil);
NSString* const name = [repo objectForKey: @"name"];
NSString* const url = [repo objectForKey: @"url"];
if (alwaysOpenNew || ![self showExtantWindow: name url: url])
{
[self openRepositoryBrowser: url title: name
user: [repo objectForKey: @"user"]
pass: [repo objectForKey: @"pass"]];
}
}
//----------------------------------------------------------------------------------------
- (void) onDoubleClick: (id) sender
{
#pragma unused(sender)
NSArray* selectedObjects = [fAC selectedObjects];
NSDictionary* selection;
if ([selectedObjects count] != 0 && (selection = [selectedObjects objectAtIndex: 0]) != nil)
{
// If no option-key then look for & try to activate extant Repository window.
[self showRepositoryBrowser: selection alwaysOpenNew: AltOrShiftPressed()];
}
}
//----------------------------------------------------------------------------------------
- (void) openRepositoryBrowser: (NSString*) url
title: (NSString*) title
user: (NSString*) user
pass: (NSString*) pass
{
const id docController = [NSDocumentController sharedDocumentController];
MyRepository* newDoc = [docController makeUntitledDocumentOfType: kDocType];
[newDoc setupTitle: title username: user password: pass url: StringToURL(url, YES)];
[docController addDocument: newDoc];
[newDoc makeWindowControllers];
[newDoc showWindows];
}
//----------------------------------------------------------------------------------------
// Invoked from AppleScript.
- (void) openRepository: (NSString*) url
{
// Find among the open repositories one that has a matching url
for_each_obj(en1, it, [[NSDocumentController sharedDocumentController] documents])
{
if ([[it fileType] isEqualToString: kDocType] &&
[url rangeOfString: [[it rootURL] absoluteString]
options: NSLiteralSearch | NSAnchoredSearch].location == 0)
{
[it showWindows];
return;
}
}
// Find among the known repositories one that has a matching url
for_each_obj(en2, it, [fAC arrangedObjects])
{
if ([url rangeOfString: [it objectForKey: @"url"]
options: NSLiteralSearch | NSAnchoredSearch].location == 0)
{
[self showRepositoryBrowser: it alwaysOpenNew: NO];
return;
}
}
[self openRepositoryBrowser: url title: [UnEscapeURL(url) lastPathComponent] user: @"" pass: @""];
}
//----------------------------------------------------------------------------------------
- (IBAction) openPath: (id) sender
{
#pragma unused(sender)
NSString* selectionPath = NSHomeDirectory();
NSOpenPanel* const oPanel = [NSOpenPanel openPanel];
[oPanel setAllowsMultipleSelection: NO];
[oPanel setCanChooseDirectories: YES];
[oPanel setCanChooseFiles: NO];
[oPanel beginSheetForDirectory: selectionPath file: nil types: nil modalForWindow: fWindow
modalDelegate: self
didEndSelector: @selector(openPathDidEnd:returnCode:contextInfo:)
contextInfo: nil];
}
//----------------------------------------------------------------------------------------
- (void) openPathDidEnd: (NSOpenPanel*) sheet
returnCode: (int) returnCode
contextInfo: (void*) contextInfo
{
#pragma unused(contextInfo)
if (returnCode == NSOKButton)
{
NSString* pathToFile = [[sheet filenames] objectAtIndex: 0];
[fAC setValue: [NSString stringWithFormat: @"file://%@", pathToFile]
forKeyPath: @"selection.url"];
[self savePreferences];
}
}
@end
//----------------------------------------------------------------------------------------
// End of RepositoriesController.m