| 1 |
/* |
| 2 |
* Copyright (c) 2004-2005 The Lupin Project. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without modification, are permitted |
| 5 |
* provided that the following conditions are met: |
| 6 |
* |
| 7 |
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 8 |
* and the following disclaimer. |
| 9 |
* |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 11 |
* conditions and the following disclaimer in the documentation and/or other materials provided |
| 12 |
* with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE LUPIN PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 15 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LUPIN PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 17 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 18 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 20 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import "LPDocumentController.h" |
| 25 |
|
| 26 |
@implementation LPDocumentController |
| 27 |
|
| 28 |
- (void)dealloc |
| 29 |
{ |
| 30 |
[_temporaryDirectories release]; |
| 31 |
[super dealloc]; |
| 32 |
} |
| 33 |
|
| 34 |
- (void)awakeFromNib |
| 35 |
{ |
| 36 |
_temporaryDirectories = [[NSMutableArray array] retain]; |
| 37 |
_pluginsController = [[LPPluginsController alloc] initWithWindowNibName:@"LPPluginInfo"]; |
| 38 |
[_pluginsController window]; |
| 39 |
_preferencesController = [[LPPreferencesController alloc] initWithWindowNibName:@"LPPreferences"]; |
| 40 |
[_preferencesController window]; |
| 41 |
// ������������������������������������������������������������������������ |
| 42 |
[_preferencesController setDefaultPreference]; |
| 43 |
// ������������������������������������������������������ |
| 44 |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 45 |
NSArray* temporaryDirectories = [defaults stringArrayForKey:@"temporaryDirectories"]; |
| 46 |
if (temporaryDirectories && [temporaryDirectories count] > 0) { |
| 47 |
if (NSAlertDefaultReturn == NSRunAlertPanel(NSLocalizedString(@"DialogAskRemoveTemporaryFolderTitle",nil), |
| 48 |
[NSString stringWithFormat:NSLocalizedString(@"DialogAskRemoveTemporaryFolderMessage",nil),[temporaryDirectories count]], |
| 49 |
NSLocalizedString(@"DialogYes",nil), |
| 50 |
NSLocalizedString(@"DialogNo",nil), |
| 51 |
NULL)) { |
| 52 |
NSFileManager *manager = [NSFileManager defaultManager]; |
| 53 |
NSEnumerator *enumerator = [temporaryDirectories objectEnumerator]; |
| 54 |
NSString* directoryPath; |
| 55 |
while (directoryPath = [enumerator nextObject]) { |
| 56 |
if ([manager fileExistsAtPath:directoryPath]) |
| 57 |
if ([manager removeFileAtPath:directoryPath handler:nil]) |
| 58 |
[[NSDocumentController sharedDocumentController] removeDirectory:directoryPath]; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
- (LPPluginsController*)pluginsController |
| 65 |
{ |
| 66 |
/* |
| 67 |
if (!_pluginsController) { |
| 68 |
_pluginsController = [[LPPluginsController alloc] initWithWindowNibName:@"LPPluginInfo"]; |
| 69 |
if (!_pluginsController) { |
| 70 |
// Fatal Error |
| 71 |
NSLog(@"Could not load LPPluginInfo.nib"); |
| 72 |
} |
| 73 |
[_pluginsController window]; |
| 74 |
} |
| 75 |
*/ |
| 76 |
return _pluginsController; |
| 77 |
} |
| 78 |
|
| 79 |
- (LPPreferencesController*)preferencesController |
| 80 |
{ |
| 81 |
return _preferencesController; |
| 82 |
} |
| 83 |
|
| 84 |
#pragma mark - |
| 85 |
#pragma mark Reserving Temporary directories |
| 86 |
- (void)addDirectory:(NSString*)path |
| 87 |
{ |
| 88 |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 89 |
[_temporaryDirectories addObject:path]; |
| 90 |
[defaults setObject:_temporaryDirectories forKey:@"temporaryDirectories"]; |
| 91 |
[defaults synchronize]; |
| 92 |
} |
| 93 |
|
| 94 |
- (void)removeDirectory:(NSString*)path |
| 95 |
{ |
| 96 |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 97 |
[_temporaryDirectories removeObjectIdenticalTo:path]; |
| 98 |
[defaults setObject:_temporaryDirectories forKey:@"temporaryDirectories"]; |
| 99 |
[defaults synchronize]; |
| 100 |
} |
| 101 |
|
| 102 |
#pragma mark - |
| 103 |
#pragma mark Uncompressing |
| 104 |
|
| 105 |
// ���������������������������������������������������������������������NSDocument������������������ |
| 106 |
- (void)inspect:(id)sender |
| 107 |
{ |
| 108 |
NSLog(@"(LPDocumentController) Inspect selected."); |
| 109 |
NSOpenPanel* openPanel = [NSOpenPanel openPanel]; |
| 110 |
[openPanel setTitle:NSLocalizedString(@"DialogChooseArchiveTitle",nil)]; |
| 111 |
[openPanel setMessage:NSLocalizedString(@"DialogInspectChooseArchiveMessage",nil)]; |
| 112 |
int result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil]; |
| 113 |
if (result != NSOKButton) |
| 114 |
return; |
| 115 |
LPDocument* document = [self makeDocumentWithContentsOfFile:[openPanel filename] ofType:@"LupinInspectType"]; |
| 116 |
if (!document) return; |
| 117 |
[document makeWindowControllers]; |
| 118 |
[document showWindows]; |
| 119 |
[self addDocument:document]; |
| 120 |
} |
| 121 |
|
| 122 |
- (void)uncompress:(id)sender |
| 123 |
{ |
| 124 |
NSLog(@"(LPDocumentController) Uncompress selected."); |
| 125 |
NSOpenPanel* openPanel = [NSOpenPanel openPanel]; |
| 126 |
[openPanel setTitle:NSLocalizedString(@"DialogChooseArchiveTitle",nil)]; |
| 127 |
[openPanel setMessage:NSLocalizedString(@"DialogUncompressChooseFileMessage",nil)]; |
| 128 |
int result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil]; |
| 129 |
if (result != NSOKButton) |
| 130 |
return; |
| 131 |
[self uncompressFile:[openPanel filename]]; |
| 132 |
} |
| 133 |
|
| 134 |
- (void)uncompressFile:(NSString*)fileName |
| 135 |
{ |
| 136 |
LPDocument* document = [self makeDocumentWithContentsOfFile:fileName ofType:@"LupinUncompressType"]; |
| 137 |
//[document setPlugin:[[self pluginsController] initPlugin:[savePanel filename] index:[compress index]]]; |
| 138 |
if (!document) return; // ���������������������������������������������������������������������������������������������������openDocument��������������������������� |
| 139 |
[document makeWindowControllers]; |
| 140 |
[document showWindows]; |
| 141 |
//LPDocument* document = [self openUntitledDocumentOfType:@"LupinCompressType" display:YES]; |
| 142 |
//[document readFromFile:[savePanel filename] ofType:@"LupinCompressType"]; |
| 143 |
[self addDocument:document]; |
| 144 |
[document uncompress:self]; |
| 145 |
} |
| 146 |
|
| 147 |
#pragma mark - |
| 148 |
#pragma mark Compressing |
| 149 |
|
| 150 |
- (void)loadCompress |
| 151 |
{ |
| 152 |
[NSBundle loadNibNamed:@"LPCompress" owner:self]; |
| 153 |
[_compressSelectButton removeAllItems]; |
| 154 |
NSEnumerator* enumerator = [[[self pluginsController] compressess] objectEnumerator]; |
| 155 |
LPCompress* compress; |
| 156 |
while (compress = [enumerator nextObject]) { |
| 157 |
[_compressSelectButton addItemWithTitle:[compress compressDisplayName]]; |
| 158 |
} |
| 159 |
if ([_compressSelectButton numberOfItems] > 0) |
| 160 |
[_compressSelectButton selectItemAtIndex:0]; |
| 161 |
[_compressSelectButton setAutoenablesItems:YES]; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
//! ������������������������������������������������NSPopUpButton���������������������������YES��������� |
| 166 |
- (BOOL)validateMenuItem:(NSMenuItem *)anItem |
| 167 |
{ |
| 168 |
return YES; |
| 169 |
} |
| 170 |
|
| 171 |
- (IBAction)compress:(id)sender |
| 172 |
{ |
| 173 |
NSLog(@"(LPDocumentController) compress selected."); |
| 174 |
NSOpenPanel* openPanel = [NSOpenPanel openPanel]; |
| 175 |
[openPanel setTitle:NSLocalizedString(@"DialogChooseFileTitle",nil)]; |
| 176 |
[openPanel setMessage:NSLocalizedString(@"DialogCompressChooseFileMessage",nil)]; |
| 177 |
[openPanel setPrompt:NSLocalizedString(@"DialogChooseFolderButtonTitle",nil)]; |
| 178 |
[openPanel setAllowsMultipleSelection:YES]; |
| 179 |
[openPanel setCanChooseDirectories:YES]; |
| 180 |
int result = [openPanel runModalForTypes:nil]; |
| 181 |
if (result != NSOKButton) |
| 182 |
return; |
| 183 |
[self compressFiles:[openPanel filenames]]; |
| 184 |
} |
| 185 |
|
| 186 |
- (void)compressFiles:(NSArray*)files |
| 187 |
{ |
| 188 |
if (!_compressSelectView) [self loadCompress]; |
| 189 |
NSSavePanel* savePanel = [NSSavePanel savePanel]; |
| 190 |
[savePanel setTitle:NSLocalizedString(@"DialogCompressChooseArchiveTitle",nil)]; |
| 191 |
[savePanel setPrompt:NSLocalizedString(@"DialogCompressChooseArchiveButtonTitle",nil)]; |
| 192 |
[savePanel setMessage:NSLocalizedString(@"DialogCompressChooseArchiveMessage",nil)]; |
| 193 |
[savePanel setCanSelectHiddenExtension:YES]; |
| 194 |
[savePanel setAccessoryView:_compressSelectView]; |
| 195 |
[savePanel setRequiredFileType:[[[[self pluginsController] compressess] objectAtIndex:[_compressSelectButton indexOfSelectedItem]] compressExtension]]; |
| 196 |
[_compressOptionButton setEnabled:[[[[self pluginsController] compressess] objectAtIndex:[_compressSelectButton indexOfSelectedItem]] hasOptionView]]; |
| 197 |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 198 |
NSString* destination = NULL; |
| 199 |
switch ([defaults integerForKey:@"compressCreateAtMatrix"]) { |
| 200 |
case 0: |
| 201 |
destination = [[files objectAtIndex:0] stringByDeletingLastPathComponent]; |
| 202 |
break; |
| 203 |
case 1: |
| 204 |
destination = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"]; |
| 205 |
break; |
| 206 |
case 2: |
| 207 |
destination = NSHomeDirectory(); |
| 208 |
break; |
| 209 |
case 3: |
| 210 |
destination = [defaults objectForKey:@"compressCreateAt"]; |
| 211 |
break; |
| 212 |
} |
| 213 |
int result = [savePanel runModalForDirectory:destination file:nil]; |
| 214 |
if (result != NSOKButton) |
| 215 |
return; |
| 216 |
NSFileManager* manager = [NSFileManager defaultManager]; |
| 217 |
if ([manager fileExistsAtPath:[savePanel filename]]) { |
| 218 |
if (![manager removeFileAtPath:[savePanel filename] handler:nil]) { |
| 219 |
NSRunAlertPanel(NSLocalizedString(@"DialogCantRemoveFileTitle",nil) |
| 220 |
,[NSString stringWithFormat:NSLocalizedString(@"DialogCantRemoveFileInfo",nil),[savePanel filename]] |
| 221 |
,NSLocalizedString(@"DialogAgree",nil) |
| 222 |
,NULL,NULL); |
| 223 |
return; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
LPCompress* compress = [[[self pluginsController] compressess] objectAtIndex:[_compressSelectButton indexOfSelectedItem]]; |
| 228 |
LPDocument* document = [self makeDocumentWithContentsOfFile:[savePanel filename] ofType:@"LupinCompressType"]; |
| 229 |
[document setPlugin:[[self pluginsController] initPlugin:[savePanel filename] index:[compress index]]]; |
| 230 |
[document makeWindowControllers]; |
| 231 |
[document showWindows]; |
| 232 |
//LPDocument* document = [self openUntitledDocumentOfType:@"LupinCompressType" display:YES]; |
| 233 |
//[document readFromFile:[savePanel filename] ofType:@"LupinCompressType"]; |
| 234 |
[self addDocument:document]; |
| 235 |
|
| 236 |
NSEnumerator* enumerator = [files objectEnumerator]; |
| 237 |
NSMutableArray* filenames = [NSMutableArray array]; |
| 238 |
NSString* directoryName; |
| 239 |
NSString* currentPath = [[files objectAtIndex:0] stringByDeletingLastPathComponent]; |
| 240 |
while (directoryName = [enumerator nextObject]) { |
| 241 |
//NSEnumerator* subpathsEnumerator = [[manager subpathsAtPath:directoryName] objectEnumerator]; |
| 242 |
//while (fileName = [subpathsEnumerator nextObject]) { |
| 243 |
// [filenames addObject:[NSString stringWithFormat:@"%@/%@",[directoryName lastPathComponent],fileName]]; |
| 244 |
//} |
| 245 |
[filenames addObject:[directoryName lastPathComponent]]; |
| 246 |
} |
| 247 |
|
| 248 |
[document archiveCompress:[savePanel filename] files:filenames exclude:nil currentPath:currentPath compress:compress option:nil]; |
| 249 |
} |
| 250 |
|
| 251 |
- (IBAction)optionAccept:(id)sender |
| 252 |
{ |
| 253 |
[NSApp endSheet:_compressOptionPanel]; |
| 254 |
[_compressOptionPanel orderOut:self]; |
| 255 |
} |
| 256 |
|
| 257 |
- (IBAction)optionCancel:(id)sender |
| 258 |
{ |
| 259 |
[NSApp endSheet:_compressOptionPanel]; |
| 260 |
[_compressOptionPanel orderOut:self]; |
| 261 |
} |
| 262 |
|
| 263 |
- (IBAction)optionSelected:(id)sender |
| 264 |
{ |
| 265 |
[(NSSavePanel*)[sender window] setRequiredFileType:[[[[self pluginsController] compressess] objectAtIndex:[_compressSelectButton indexOfSelectedItem]] compressExtension]]; |
| 266 |
[_compressOptionButton setEnabled:[[[[self pluginsController] compressess] objectAtIndex:[_compressSelectButton indexOfSelectedItem]] hasOptionView]]; |
| 267 |
} |
| 268 |
|
| 269 |
- (IBAction)showOption:(id)sender |
| 270 |
{ |
| 271 |
// ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� |
| 272 |
/* |
| 273 |
[_compressOptionPanel setContentView:_compressSelectView]; |
| 274 |
[NSApp beginSheet:_compressOptionPanel |
| 275 |
modalForWindow:[sender window] |
| 276 |
modalDelegate:self |
| 277 |
didEndSelector:NULL |
| 278 |
contextInfo:nil]; |
| 279 |
*/ |
| 280 |
} |
| 281 |
|
| 282 |
@end |