| 1 |
/* |
| 2 |
** CWLocalStore.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (c) 2017-2020 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/CWLocalStore.h> |
| 25 |
|
| 26 |
#import <Pantomime/CWConstants.h> |
| 27 |
#import <Pantomime/CWLocalCacheManager.h> |
| 28 |
#import <Pantomime/CWLocalFolder.h> |
| 29 |
#import <Pantomime/CWLocalFolder+mbox.h> |
| 30 |
#import <Pantomime/CWLocalMessage.h> |
| 31 |
#import <Pantomime/NSFileManager+Extensions.h> |
| 32 |
#import <Pantomime/NSString+Extensions.h> |
| 33 |
#import <Pantomime/CWURLName.h> |
| 34 |
|
| 35 |
#import <Foundation/NSAutoreleasePool.h> |
| 36 |
#import <Foundation/NSHost.h> |
| 37 |
#import <Foundation/NSNotification.h> |
| 38 |
#import <Foundation/NSPathUtilities.h> |
| 39 |
#import <Foundation/NSValue.h> |
| 40 |
|
| 41 |
#include <sys/types.h> |
| 42 |
#include <time.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <stdlib.h> // rand() |
| 45 |
|
| 46 |
// |
| 47 |
// Private interface |
| 48 |
// |
| 49 |
@interface CWLocalStore (Private) |
| 50 |
- (NSEnumerator *) _rebuildFolderEnumerator; |
| 51 |
@end |
| 52 |
|
| 53 |
// |
| 54 |
// |
| 55 |
// |
| 56 |
@implementation CWLocalStore |
| 57 |
|
| 58 |
- (void) dealloc |
| 59 |
{ |
| 60 |
RELEASE(_path); |
| 61 |
RELEASE(_openFolders); |
| 62 |
RELEASE(_folders); |
| 63 |
|
| 64 |
[super dealloc]; |
| 65 |
} |
| 66 |
|
| 67 |
// |
| 68 |
// |
| 69 |
// |
| 70 |
- (id) initWithPath: (NSString *) thePath |
| 71 |
{ |
| 72 |
self = [super init]; |
| 73 |
if (self) |
| 74 |
{ |
| 75 |
BOOL isDirectory; |
| 76 |
|
| 77 |
[self setPath: thePath]; |
| 78 |
|
| 79 |
_openFolders = [[NSMutableDictionary alloc] init]; |
| 80 |
_folders = [[NSMutableArray alloc] init]; |
| 81 |
_secure = YES; |
| 82 |
|
| 83 |
if ([[NSFileManager defaultManager] fileExistsAtPath: thePath isDirectory: &isDirectory]) |
| 84 |
{ |
| 85 |
if (!isDirectory) |
| 86 |
{ |
| 87 |
AUTORELEASE(self); |
| 88 |
return nil; |
| 89 |
} |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
AUTORELEASE(self); |
| 94 |
return nil; |
| 95 |
} |
| 96 |
} |
| 97 |
return self; |
| 98 |
} |
| 99 |
|
| 100 |
// |
| 101 |
// |
| 102 |
// |
| 103 |
- (id) initWithURL: (CWURLName *) theURL |
| 104 |
{ |
| 105 |
return [self initWithPath: [theURL path]]; |
| 106 |
} |
| 107 |
|
| 108 |
// |
| 109 |
// This method will open automatically Inbox (case-insensitive). |
| 110 |
// It may return nil if the opening failed or Inbox wasn't found. |
| 111 |
// |
| 112 |
- (id) defaultFolder |
| 113 |
{ |
| 114 |
return [self folderForName: @"Inbox"]; |
| 115 |
} |
| 116 |
|
| 117 |
// |
| 118 |
// This method is used to open the folder theName in the current |
| 119 |
// directory of this local store. |
| 120 |
// |
| 121 |
- (id) folderForName: (NSString *) theName |
| 122 |
{ |
| 123 |
CWLocalFolder *cachedFolder; |
| 124 |
|
| 125 |
if (!theName) return nil; |
| 126 |
|
| 127 |
cachedFolder = [_openFolders objectForKey: theName]; |
| 128 |
|
| 129 |
if (!cachedFolder) |
| 130 |
{ |
| 131 |
NSEnumerator *anEnumerator; |
| 132 |
NSString *aString; |
| 133 |
|
| 134 |
anEnumerator = [self folderEnumerator]; |
| 135 |
|
| 136 |
while ((aString = [anEnumerator nextObject])) |
| 137 |
{ |
| 138 |
if ([aString compare: theName] == NSOrderedSame) |
| 139 |
{ |
| 140 |
CWLocalFolder *aFolder; |
| 141 |
|
| 142 |
aFolder = [[CWLocalFolder alloc] initWithPath: [NSString stringWithFormat:@"%@/%@", _path, aString]]; |
| 143 |
|
| 144 |
if (aFolder) |
| 145 |
{ |
| 146 |
[aFolder setStore: self]; |
| 147 |
[aFolder setName: theName]; |
| 148 |
|
| 149 |
// We now cache it and return it |
| 150 |
[_openFolders setObject: aFolder forKey: theName]; |
| 151 |
|
| 152 |
POST_NOTIFICATION(PantomimeFolderOpenCompleted, self, [NSDictionary dictionaryWithObject: aFolder forKey: @"Folder"]); |
| 153 |
PERFORM_SELECTOR_2(self, @selector(folderOpenCompleted:), PantomimeFolderOpenCompleted, aFolder, @"Folder"); |
| 154 |
|
| 155 |
// tell the cache manager to read all messages |
| 156 |
if (![aFolder cacheManager]) |
| 157 |
{ |
| 158 |
[aFolder setCacheManager: [[[CWLocalCacheManager alloc] initWithPath: [NSString stringWithFormat: @"%@.%@.cache", [[aFolder path] substringToIndex: ([[aFolder path] length] - [[[aFolder path] lastPathComponent] length])], [[aFolder path] lastPathComponent]] folder: aFolder] autorelease]]; |
| 159 |
[(CWLocalCacheManager *)[aFolder cacheManager] readAllMessages]; |
| 160 |
[aFolder parse: NO]; |
| 161 |
} |
| 162 |
RELEASE(aFolder); |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
POST_NOTIFICATION(PantomimeFolderOpenFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"FolderName"]); |
| 167 |
PERFORM_SELECTOR_2(self, @selector(folderOpenFailed:), PantomimeFolderOpenFailed, theName, @"FolderName"); |
| 168 |
} |
| 169 |
|
| 170 |
return aFolder; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return nil; |
| 175 |
} |
| 176 |
|
| 177 |
//NSLog(@"Returning cached folder!"); |
| 178 |
return cachedFolder; |
| 179 |
} |
| 180 |
|
| 181 |
// |
| 182 |
// |
| 183 |
// |
| 184 |
- (id) folderForURL: (NSString *) theURL; |
| 185 |
{ |
| 186 |
CWURLName *theURLName; |
| 187 |
id aFolder; |
| 188 |
|
| 189 |
theURLName = [[CWURLName alloc] initWithString: theURL]; |
| 190 |
|
| 191 |
aFolder = [self folderForName: [theURLName foldername]]; |
| 192 |
|
| 193 |
RELEASE(theURLName); |
| 194 |
|
| 195 |
return aFolder; |
| 196 |
} |
| 197 |
|
| 198 |
// |
| 199 |
// This method returns the list of folders contained in |
| 200 |
// a specific directory. It'll currently ignore some things |
| 201 |
// like Netscape Mail's summary files and Pantomime's local |
| 202 |
// cache files. |
| 203 |
// |
| 204 |
- (NSEnumerator *) folderEnumerator |
| 205 |
{ |
| 206 |
if ([_folders count] > 0) |
| 207 |
{ |
| 208 |
POST_NOTIFICATION(PantomimeFolderListCompleted, self, [NSDictionary dictionaryWithObject: [_folders objectEnumerator] forKey: @"NSEnumerator"]); |
| 209 |
PERFORM_SELECTOR_2(self, @selector(folderListCompleted:), PantomimeFolderListCompleted, [_folders objectEnumerator], @"NSEnumerator"); |
| 210 |
return [_folders objectEnumerator]; |
| 211 |
} |
| 212 |
|
| 213 |
return [self _rebuildFolderEnumerator]; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
// |
| 218 |
// |
| 219 |
// |
| 220 |
- (NSEnumerator *) subscribedFolderEnumerator |
| 221 |
{ |
| 222 |
return [self folderEnumerator]; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
// |
| 227 |
// |
| 228 |
// |
| 229 |
- (id) delegate |
| 230 |
{ |
| 231 |
return _delegate; |
| 232 |
} |
| 233 |
|
| 234 |
- (void) setDelegate: (id) theDelegate |
| 235 |
{ |
| 236 |
_delegate = theDelegate; |
| 237 |
} |
| 238 |
|
| 239 |
// |
| 240 |
// |
| 241 |
// |
| 242 |
- (NSString *) path |
| 243 |
{ |
| 244 |
return _path; |
| 245 |
} |
| 246 |
|
| 247 |
// |
| 248 |
// |
| 249 |
// |
| 250 |
- (void) setPath: (NSString *) thePath |
| 251 |
{ |
| 252 |
ASSIGN(_path, thePath); |
| 253 |
} |
| 254 |
|
| 255 |
// |
| 256 |
// |
| 257 |
// |
| 258 |
- (void) setEnforceMode: (BOOL) theBOOL; |
| 259 |
{ |
| 260 |
_secure = theBOOL; |
| 261 |
} |
| 262 |
|
| 263 |
// |
| 264 |
// |
| 265 |
// |
| 266 |
- (BOOL) isEnforcingMode |
| 267 |
{ |
| 268 |
return _secure; |
| 269 |
} |
| 270 |
|
| 271 |
// |
| 272 |
// |
| 273 |
// |
| 274 |
- (void) close |
| 275 |
{ |
| 276 |
NSEnumerator *anEnumerator; |
| 277 |
CWLocalFolder *aFolder; |
| 278 |
|
| 279 |
anEnumerator = [self openFoldersEnumerator]; |
| 280 |
|
| 281 |
while ((aFolder = [anEnumerator nextObject])) |
| 282 |
{ |
| 283 |
[aFolder close]; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
// |
| 289 |
// |
| 290 |
// |
| 291 |
- (NSEnumerator *) openFoldersEnumerator |
| 292 |
{ |
| 293 |
return [_openFolders objectEnumerator]; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
// |
| 298 |
// |
| 299 |
// |
| 300 |
- (void) removeFolderFromOpenFolders: (CWFolder *) theFolder |
| 301 |
{ |
| 302 |
[_openFolders removeObjectForKey: [(CWLocalFolder *)theFolder name]]; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
// |
| 307 |
// |
| 308 |
// |
| 309 |
- (BOOL) folderForNameIsOpen: (NSString *) theName |
| 310 |
{ |
| 311 |
NSEnumerator *anEnumerator; |
| 312 |
CWLocalFolder *aFolder; |
| 313 |
|
| 314 |
anEnumerator = [self openFoldersEnumerator]; |
| 315 |
|
| 316 |
while ((aFolder = [anEnumerator nextObject])) |
| 317 |
{ |
| 318 |
if ([[aFolder name] compare: theName] == NSOrderedSame) |
| 319 |
{ |
| 320 |
return YES; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return NO; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
// |
| 329 |
// |
| 330 |
// |
| 331 |
- (PantomimeFolderType) folderTypeForFolderName: (NSString *) theName |
| 332 |
{ |
| 333 |
NSString *aString; |
| 334 |
BOOL isDir; |
| 335 |
|
| 336 |
aString = [NSString stringWithFormat: @"%@/%@", _path, theName]; |
| 337 |
|
| 338 |
[[NSFileManager defaultManager] fileExistsAtPath: aString |
| 339 |
isDirectory: &isDir]; |
| 340 |
|
| 341 |
if (isDir) |
| 342 |
{ |
| 343 |
// This could be a maildir store. Check for maildir specific subfolders. |
| 344 |
aString = [NSString stringWithFormat: @"%@/%@/cur", _path, theName]; |
| 345 |
|
| 346 |
if ( [[NSFileManager defaultManager] fileExistsAtPath: aString |
| 347 |
isDirectory: &isDir] && isDir ) |
| 348 |
{ |
| 349 |
return PantomimeHoldsMessages; |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
return PantomimeHoldsFolders; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
return PantomimeHoldsMessages; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
// |
| 362 |
// |
| 363 |
// |
| 364 |
- (unichar) folderSeparator |
| 365 |
{ |
| 366 |
return '/'; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
// |
| 371 |
// |
| 372 |
// |
| 373 |
- (void) createFolderWithName: (NSString *) theName |
| 374 |
type: (PantomimeFolderFormat) theType |
| 375 |
contents: (NSData *) theContents |
| 376 |
{ |
| 377 |
NSString *aName, *pathToFile; |
| 378 |
NSFileManager *aFileManager; |
| 379 |
NSEnumerator *anEnumerator; |
| 380 |
BOOL b, is_dir; |
| 381 |
NSUInteger count; |
| 382 |
|
| 383 |
aFileManager = [NSFileManager defaultManager]; |
| 384 |
anEnumerator = [self folderEnumerator]; |
| 385 |
count = 0; |
| 386 |
|
| 387 |
pathToFile = [NSString stringWithFormat: @"%@/%@", _path, theName]; |
| 388 |
pathToFile = [pathToFile substringToIndex: ([pathToFile length]-[[pathToFile lastPathComponent] length]-1)]; |
| 389 |
|
| 390 |
|
| 391 |
// We verify if the folder with that name does already exist |
| 392 |
while ((aName = [anEnumerator nextObject])) |
| 393 |
{ |
| 394 |
if ([aName compare: theName options: NSCaseInsensitiveSearch] == NSOrderedSame) |
| 395 |
{ |
| 396 |
POST_NOTIFICATION(PantomimeFolderCreateFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 397 |
PERFORM_SELECTOR_2(self, @selector(folderCreateFailed:), PantomimeFolderCreateFailed, theName, @"Name"); |
| 398 |
return; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
// Ok, the folder doesn't already exist. |
| 403 |
// Check if we want to create a simple folder/directory. |
| 404 |
if (theType == PantomimeFormatFolder) |
| 405 |
{ |
| 406 |
NSString *aString; |
| 407 |
|
| 408 |
aString = [NSString stringWithFormat: @"%@/%@", _path, theName]; |
| 409 |
b = [aFileManager createDirectoryAtPath: aString attributes: nil]; |
| 410 |
|
| 411 |
if (b) |
| 412 |
{ |
| 413 |
NSDictionary *info; |
| 414 |
|
| 415 |
if (_secure) [[NSFileManager defaultManager] enforceMode: 0700 atPath: aString]; |
| 416 |
[self _rebuildFolderEnumerator]; |
| 417 |
|
| 418 |
info = [NSDictionary dictionaryWithObjectsAndKeys: theName, @"Name", [NSNumber numberWithUnsignedInt: 0], @"Count", nil]; |
| 419 |
POST_NOTIFICATION(PantomimeFolderCreateCompleted, self, info); |
| 420 |
PERFORM_SELECTOR_3(self, @selector(folderCreateCompleted:), PantomimeFolderCreateCompleted, info); |
| 421 |
} |
| 422 |
else |
| 423 |
{ |
| 424 |
POST_NOTIFICATION(PantomimeFolderCreateFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 425 |
PERFORM_SELECTOR_2(self, @selector(folderCreateFailed:), PantomimeFolderCreateFailed, theName, @"Name"); |
| 426 |
} |
| 427 |
|
| 428 |
return; |
| 429 |
} |
| 430 |
|
| 431 |
b = NO; |
| 432 |
|
| 433 |
// We want to create a mailbox store; check if it already exists. |
| 434 |
if ([aFileManager fileExistsAtPath: pathToFile isDirectory: &is_dir]) |
| 435 |
{ |
| 436 |
int size; |
| 437 |
|
| 438 |
size = [[[aFileManager fileAttributesAtPath: pathToFile traverseLink: NO] objectForKey: NSFileSize] intValue]; |
| 439 |
|
| 440 |
// If we got an empty file or simply a directory... |
| 441 |
if (size == 0 || is_dir) |
| 442 |
{ |
| 443 |
NSString *aString; |
| 444 |
|
| 445 |
// If the size is 0, that means we have an empty file. We first convert this |
| 446 |
// file to a directory. We also remove the cache file. |
| 447 |
if (size == 0) |
| 448 |
{ |
| 449 |
[aFileManager removeFileAtPath: |
| 450 |
[NSString stringWithFormat: @"%@/.%@.cache", |
| 451 |
[pathToFile substringToIndex: ([pathToFile length]-[[pathToFile lastPathComponent] length]-1)], |
| 452 |
[pathToFile lastPathComponent]] handler: nil]; |
| 453 |
[aFileManager removeFileAtPath: pathToFile handler: nil]; |
| 454 |
[aFileManager createDirectoryAtPath: pathToFile attributes: nil]; |
| 455 |
} |
| 456 |
|
| 457 |
// We can now proceed with the creation of our store. |
| 458 |
// Check the type of store we want to create |
| 459 |
switch (theType) |
| 460 |
{ |
| 461 |
case PantomimeFormatMaildir: |
| 462 |
// Create the main maildir directory |
| 463 |
aString = [NSString stringWithFormat: @"%@/%@", _path, theName]; |
| 464 |
b = [aFileManager createDirectoryAtPath: aString attributes: nil]; |
| 465 |
if (_secure) [[NSFileManager defaultManager] enforceMode: 0700 atPath: aString]; |
| 466 |
|
| 467 |
// Now create the cur, new, and tmp sub-directories. |
| 468 |
aString = [NSString stringWithFormat: @"%@/%@/cur", _path, theName]; |
| 469 |
b = b & [aFileManager createDirectoryAtPath: aString attributes: nil]; |
| 470 |
if (_secure) [[NSFileManager defaultManager] enforceMode: 0700 atPath: aString]; |
| 471 |
|
| 472 |
// new |
| 473 |
aString = [NSString stringWithFormat: @"%@/%@/new", _path, theName]; |
| 474 |
b = b & [aFileManager createDirectoryAtPath: aString attributes: nil]; |
| 475 |
if (_secure) [[NSFileManager defaultManager] enforceMode: 0700 atPath: aString]; |
| 476 |
|
| 477 |
// tmp |
| 478 |
aString = [NSString stringWithFormat: @"%@/%@/tmp", _path, theName]; |
| 479 |
b = b & [aFileManager createDirectoryAtPath: aString attributes: nil]; |
| 480 |
if (_secure) [[NSFileManager defaultManager] enforceMode: 0700 atPath: aString]; |
| 481 |
|
| 482 |
// We create our fist message |
| 483 |
if (theContents) |
| 484 |
{ |
| 485 |
aString = [NSString stringWithFormat: @"%@/%@/cur/%@:2,", |
| 486 |
_path, |
| 487 |
theName, |
| 488 |
[NSString stringWithFormat: @"%lld.%d%d.%@", |
| 489 |
(long long)time(NULL), |
| 490 |
getpid(), |
| 491 |
rand(), |
| 492 |
[[NSHost currentHost] name]]]; |
| 493 |
[theContents writeToFile: aString atomically: YES]; |
| 494 |
} |
| 495 |
break; |
| 496 |
|
| 497 |
case PantomimeFormatMbox: |
| 498 |
default: |
| 499 |
b = [aFileManager createFileAtPath: [NSString stringWithFormat: @"%@/%@", _path, theName] |
| 500 |
contents: theContents |
| 501 |
attributes: nil]; |
| 502 |
|
| 503 |
count = [CWLocalFolder numberOfMessagesFromData: theContents]; |
| 504 |
|
| 505 |
// We now enforce the mode (0600) on this new mailbox |
| 506 |
if (_secure) [[NSFileManager defaultManager] enforceMode: 0600 |
| 507 |
atPath: [NSString stringWithFormat: @"%@/%@", _path, theName]]; |
| 508 |
break; |
| 509 |
} |
| 510 |
|
| 511 |
// rebuild the folder list |
| 512 |
[self _rebuildFolderEnumerator]; |
| 513 |
} |
| 514 |
else |
| 515 |
{ |
| 516 |
b = NO; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
if (b) |
| 521 |
{ |
| 522 |
NSDictionary *info; |
| 523 |
|
| 524 |
info = [NSDictionary dictionaryWithObjectsAndKeys: theName, @"Name", [NSNumber numberWithUnsignedInt: (unsigned int)count], @"Count", nil]; |
| 525 |
POST_NOTIFICATION(PantomimeFolderCreateCompleted, self, info); |
| 526 |
PERFORM_SELECTOR_3(self, @selector(folderCreateCompleted:), PantomimeFolderCreateCompleted, info); |
| 527 |
} |
| 528 |
else |
| 529 |
{ |
| 530 |
POST_NOTIFICATION(PantomimeFolderCreateFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 531 |
PERFORM_SELECTOR_2(self, @selector(folderCreateFailed:), PantomimeFolderCreateFailed, theName, @"Name"); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
// |
| 537 |
// theName must be the full path of the mailbox. |
| 538 |
// |
| 539 |
- (void) deleteFolderWithName: (NSString *) theName |
| 540 |
{ |
| 541 |
NSFileManager *aFileManager; |
| 542 |
BOOL aBOOL, is_dir; |
| 543 |
|
| 544 |
aFileManager = [NSFileManager defaultManager]; |
| 545 |
aBOOL = NO; |
| 546 |
|
| 547 |
if ([aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@", _path, theName] |
| 548 |
isDirectory: &is_dir]) |
| 549 |
{ |
| 550 |
if (is_dir) |
| 551 |
{ |
| 552 |
NSEnumerator *theEnumerator; |
| 553 |
NSArray *theEntries; |
| 554 |
|
| 555 |
theEnumerator = [aFileManager enumeratorAtPath: [NSString stringWithFormat: @"%@/%@", |
| 556 |
_path, theName]]; |
| 557 |
|
| 558 |
// FIXME - Verify the Store's path. |
| 559 |
// If it doesn't contain any mailboxes and it's actually not or Store's path, we remove it. |
| 560 |
theEntries = [theEnumerator allObjects]; |
| 561 |
if ([theEntries count] == 0) |
| 562 |
{ |
| 563 |
aBOOL = [aFileManager removeFileAtPath: [NSString stringWithFormat: @"%@/%@", |
| 564 |
_path, theName] |
| 565 |
handler: nil]; |
| 566 |
|
| 567 |
// Rebuild the folder tree |
| 568 |
if (aBOOL) |
| 569 |
{ |
| 570 |
[self _rebuildFolderEnumerator]; |
| 571 |
POST_NOTIFICATION(PantomimeFolderDeleteCompleted, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 572 |
PERFORM_SELECTOR_1(self, @selector(folderDeleteCompleted:), PantomimeFolderDeleteCompleted); |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
POST_NOTIFICATION(PantomimeFolderDeleteFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 577 |
PERFORM_SELECTOR_1(self, @selector(folderDeleteFailed:), PantomimeFolderDeleteFailed); |
| 578 |
} |
| 579 |
|
| 580 |
return; |
| 581 |
} |
| 582 |
// We could also be trying to delete a maildir mailbox which |
| 583 |
// has a directory structure with 3 sub-directories: cur, new, tmp |
| 584 |
else if ([aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@/cur", _path, theName] |
| 585 |
isDirectory: &is_dir]) |
| 586 |
{ |
| 587 |
// Make sure that these are the maildir directories and not something else. |
| 588 |
if (![aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@/new", _path, theName] |
| 589 |
isDirectory: &is_dir]) |
| 590 |
{ |
| 591 |
POST_NOTIFICATION(PantomimeFolderDeleteFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 592 |
PERFORM_SELECTOR_1(self, @selector(folderDeleteFailed:), PantomimeFolderDeleteFailed); |
| 593 |
return; |
| 594 |
} |
| 595 |
if (![aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@/tmp", _path, theName] |
| 596 |
isDirectory: &is_dir] ) |
| 597 |
{ |
| 598 |
POST_NOTIFICATION(PantomimeFolderDeleteFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 599 |
PERFORM_SELECTOR_1(self, @selector(folderDeleteFailed:), PantomimeFolderDeleteFailed); |
| 600 |
return; |
| 601 |
} |
| 602 |
} |
| 603 |
else |
| 604 |
{ |
| 605 |
POST_NOTIFICATION(PantomimeFolderDeleteFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 606 |
PERFORM_SELECTOR_1(self, @selector(folderDeleteFailed:), PantomimeFolderDeleteFailed); |
| 607 |
return; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
// We remove the mbox or maildir store |
| 612 |
aBOOL = [aFileManager removeFileAtPath: [NSString stringWithFormat: @"%@/%@", |
| 613 |
_path, theName] |
| 614 |
handler: nil]; |
| 615 |
|
| 616 |
// We remove the cache, if the store deletion was successful |
| 617 |
if (aBOOL) |
| 618 |
{ |
| 619 |
NSString *aString; |
| 620 |
|
| 621 |
aString = [theName lastPathComponent]; |
| 622 |
|
| 623 |
[[NSFileManager defaultManager] removeFileAtPath: [NSString stringWithFormat: @"%@/%@.%@.cache", |
| 624 |
_path, |
| 625 |
[theName substringToIndex: ([theName length]-[aString length])], |
| 626 |
aString] |
| 627 |
handler: nil]; |
| 628 |
} |
| 629 |
|
| 630 |
// Rebuild the folder tree |
| 631 |
[self _rebuildFolderEnumerator]; |
| 632 |
} |
| 633 |
|
| 634 |
if (aBOOL) |
| 635 |
{ |
| 636 |
POST_NOTIFICATION(PantomimeFolderDeleteCompleted, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 637 |
PERFORM_SELECTOR_1(self, @selector(folderDeleteCompleted:), PantomimeFolderDeleteCompleted); |
| 638 |
} |
| 639 |
else |
| 640 |
{ |
| 641 |
POST_NOTIFICATION(PantomimeFolderDeleteFailed, self, [NSDictionary dictionaryWithObject: theName forKey: @"Name"]); |
| 642 |
PERFORM_SELECTOR_1(self, @selector(folderDeleteFailed:), PantomimeFolderDeleteFailed); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
// |
| 648 |
// theName and theNewName MUST be the full path of those mailboxes. |
| 649 |
// If they begin with the folder separator (ie., '/'), the character is |
| 650 |
// automatically stripped. |
| 651 |
// |
| 652 |
// This method supports renaming mailboxes that are open. |
| 653 |
// |
| 654 |
- (void) renameFolderWithName: (NSString *) theName |
| 655 |
toName: (NSString *) theNewName |
| 656 |
{ |
| 657 |
NSFileManager *aFileManager; |
| 658 |
NSDictionary *info; |
| 659 |
BOOL aBOOL, is_dir; |
| 660 |
|
| 661 |
aFileManager = [NSFileManager defaultManager]; |
| 662 |
aBOOL = NO; |
| 663 |
|
| 664 |
theName = [theName stringByDeletingFirstPathSeparator: [self folderSeparator]]; |
| 665 |
theNewName = [theNewName stringByDeletingFirstPathSeparator: [self folderSeparator]]; |
| 666 |
info = [NSDictionary dictionaryWithObjectsAndKeys: theName, @"Name", theNewName, @"NewName", nil]; |
| 667 |
|
| 668 |
// We do basic verifications on the passed parameters. We also verify if the destination path exists. |
| 669 |
// If it does, we abort the rename operation since we don't want to overwrite the folder. |
| 670 |
if (!theName || !theNewName || |
| 671 |
[[theName stringByTrimmingWhiteSpaces] length] == 0 || |
| 672 |
[[theNewName stringByTrimmingWhiteSpaces] length] == 0 || |
| 673 |
[aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@", _path, theNewName]]) |
| 674 |
{ |
| 675 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, info); |
| 676 |
PERFORM_SELECTOR_3(self, @selector(folderRenameFailed:), PantomimeFolderRenameFailed, info); |
| 677 |
return; |
| 678 |
} |
| 679 |
|
| 680 |
// We verify if the source path is valid |
| 681 |
if ([aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@", _path, theName] |
| 682 |
isDirectory: &is_dir]) |
| 683 |
{ |
| 684 |
CWLocalFolder *aFolder; |
| 685 |
|
| 686 |
if (is_dir) |
| 687 |
{ |
| 688 |
NSEnumerator *theEnumerator; |
| 689 |
NSArray *theEntries; |
| 690 |
|
| 691 |
theEnumerator = [aFileManager enumeratorAtPath: [NSString stringWithFormat: @"%@/%@", |
| 692 |
_path, theName]]; |
| 693 |
|
| 694 |
// FIXME - Verify the Store's path. |
| 695 |
// If it doesn't contain any mailboxes and it's actually not or Store's path, we rename it. |
| 696 |
theEntries = [theEnumerator allObjects]; |
| 697 |
|
| 698 |
if ([theEntries count] == 0) |
| 699 |
{ |
| 700 |
aBOOL = [aFileManager movePath: [NSString stringWithFormat: @"%@/%@",_path, theName] |
| 701 |
toPath: [NSString stringWithFormat: @"%@/%@", _path, theNewName] |
| 702 |
handler: nil]; |
| 703 |
if (aBOOL) |
| 704 |
{ |
| 705 |
POST_NOTIFICATION(PantomimeFolderRenameCompleted, self, info); |
| 706 |
PERFORM_SELECTOR_3(self, @selector(folderRenameCompleted:), PantomimeFolderRenameCompleted, info); |
| 707 |
} |
| 708 |
else |
| 709 |
{ |
| 710 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, info); |
| 711 |
PERFORM_SELECTOR_3(self, @selector(folderRenameFailed:), PantomimeFolderRenameFailed, info); |
| 712 |
} |
| 713 |
} |
| 714 |
// We could also be trying to delete a maildir mailbox which |
| 715 |
// has a directory structure with 3 sub-directories: cur, new, tmp |
| 716 |
else if ([aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@/cur", _path, theName] |
| 717 |
isDirectory: &is_dir]) |
| 718 |
{ |
| 719 |
// Make sure that these are the maildir directories and not something else. |
| 720 |
if (![aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@/new", _path, theName] |
| 721 |
isDirectory: &is_dir]) |
| 722 |
{ |
| 723 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, info); |
| 724 |
PERFORM_SELECTOR_3(self, @selector(folderRenameFailed:), PantomimeFolderRenameFailed, info); |
| 725 |
return; |
| 726 |
} |
| 727 |
if (![aFileManager fileExistsAtPath: [NSString stringWithFormat: @"%@/%@/tmp", _path, theName] |
| 728 |
isDirectory: &is_dir]) |
| 729 |
{ |
| 730 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, info); |
| 731 |
PERFORM_SELECTOR_3(self, @selector(folderRenameFailed:), PantomimeFolderRenameFailed, info); |
| 732 |
return; |
| 733 |
} |
| 734 |
} |
| 735 |
else |
| 736 |
{ |
| 737 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, info); |
| 738 |
PERFORM_SELECTOR_3(self, @selector(folderRenameFailed:), PantomimeFolderRenameFailed, info); |
| 739 |
return; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
// If the mailbox is open, we "close" it first. |
| 744 |
aFolder = [_openFolders objectForKey: theName]; |
| 745 |
|
| 746 |
if (aFolder) |
| 747 |
{ |
| 748 |
if ([aFolder type] == PantomimeFormatMbox) |
| 749 |
{ |
| 750 |
[aFolder close_mbox]; |
| 751 |
} |
| 752 |
[[aFolder cacheManager] synchronize]; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
// We rename the mailbox |
| 757 |
aBOOL = [aFileManager movePath: [NSString stringWithFormat: @"%@/%@", _path, theName] |
| 758 |
toPath: [NSString stringWithFormat: @"%@/%@", _path, theNewName] |
| 759 |
handler: nil]; |
| 760 |
|
| 761 |
// We rename the cache, if the store rename was successful |
| 762 |
if (aBOOL) |
| 763 |
{ |
| 764 |
NSString *str1, *str2; |
| 765 |
|
| 766 |
str1 = [theName lastPathComponent]; |
| 767 |
str2 = [theNewName lastPathComponent]; |
| 768 |
|
| 769 |
[[NSFileManager defaultManager] movePath: [NSString stringWithFormat: @"%@/%@.%@.cache", |
| 770 |
_path, |
| 771 |
[theName substringToIndex: |
| 772 |
([theName length] - [str1 length])], |
| 773 |
str1] |
| 774 |
toPath: [NSString stringWithFormat: @"%@/%@.%@.cache", |
| 775 |
_path, |
| 776 |
[theNewName substringToIndex: |
| 777 |
([theNewName length] - [str2 length])], |
| 778 |
str2] |
| 779 |
handler: nil]; |
| 780 |
} |
| 781 |
|
| 782 |
// If the folder was open, we must re-open and re-lock the mbox file, |
| 783 |
// recache the folder, adjust some paths and more. |
| 784 |
if (aFolder) |
| 785 |
{ |
| 786 |
// We update its name and path |
| 787 |
[aFolder setName: theNewName]; |
| 788 |
[aFolder setPath: [NSString stringWithFormat: @"%@/%@", _path, theNewName]]; |
| 789 |
|
| 790 |
[[aFolder cacheManager] setPath: [NSString stringWithFormat: @"%@/%@.%@.cache", |
| 791 |
_path, |
| 792 |
[theNewName substringToIndex: ([theNewName length] - [[theNewName lastPathComponent] length])], |
| 793 |
[theNewName lastPathComponent]]]; |
| 794 |
// We recache the mailbox with its new name. |
| 795 |
RETAIN(aFolder); |
| 796 |
[_openFolders removeObjectForKey: theName]; |
| 797 |
[_openFolders setObject: aFolder forKey: theNewName]; |
| 798 |
RELEASE(aFolder); |
| 799 |
|
| 800 |
// We now open and lock the mbox file. If we use maildir, we must adjust the "mail filename" |
| 801 |
// of every message in the maildir. |
| 802 |
if ([aFolder type] == PantomimeFormatMbox) |
| 803 |
{ |
| 804 |
[aFolder open_mbox]; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
// Rebuild the folder tree |
| 809 |
[self _rebuildFolderEnumerator]; |
| 810 |
} |
| 811 |
|
| 812 |
if (aBOOL) |
| 813 |
{ |
| 814 |
POST_NOTIFICATION(PantomimeFolderRenameCompleted, self, info); |
| 815 |
PERFORM_SELECTOR_3(self, @selector(folderRenameCompleted:), PantomimeFolderRenameCompleted, info); |
| 816 |
} |
| 817 |
else |
| 818 |
{ |
| 819 |
POST_NOTIFICATION(PantomimeFolderRenameFailed, self, info); |
| 820 |
PERFORM_SELECTOR_3(self, @selector(folderRenameFailed:), PantomimeFolderRenameFailed, info); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
@end |
| 825 |
|
| 826 |
|
| 827 |
// |
| 828 |
// Private interface |
| 829 |
// |
| 830 |
@implementation CWLocalStore (Private) |
| 831 |
|
| 832 |
- (NSEnumerator *) _rebuildFolderEnumerator |
| 833 |
{ |
| 834 |
NSString *aString, *lastPathComponent, *pathToFolder; |
| 835 |
NSEnumerator *tmpEnumerator; |
| 836 |
NSArray *tmpArray; |
| 837 |
NSUInteger i; |
| 838 |
|
| 839 |
// Clear out our cached folder structure and refresh from the file system |
| 840 |
[_folders removeAllObjects]; |
| 841 |
[_folders setArray: [[[NSFileManager defaultManager] enumeratorAtPath: _path] allObjects]]; |
| 842 |
|
| 843 |
// |
| 844 |
// We iterate through our array. If mbox A and .A.summary (or .A.cache) exists, we |
| 845 |
// remove .A.summary (or .A.cache) from our mutable array. |
| 846 |
// We do this in two runs: |
| 847 |
// First run: remove maildir sub-directory structure so that is appears as a regular folder. |
| 848 |
// Second run: remove other stuff like *.cache, *.summary |
| 849 |
// |
| 850 |
for (i = 0; i < [_folders count]; i++) |
| 851 |
{ |
| 852 |
BOOL bIsMailDir; |
| 853 |
|
| 854 |
aString = [_folders objectAtIndex: i]; |
| 855 |
|
| 856 |
// |
| 857 |
// First run: |
| 858 |
// If this is a maildir directory, remove its sub-directory structure from the list, |
| 859 |
// so that the maildir store appears just like a regular mail store. |
| 860 |
// |
| 861 |
if ([[NSFileManager defaultManager] fileExistsAtPath: |
| 862 |
[NSString stringWithFormat: @"%@/%@/cur", _path, aString] |
| 863 |
isDirectory: &bIsMailDir] && bIsMailDir) |
| 864 |
{ |
| 865 |
NSArray *subpaths; |
| 866 |
|
| 867 |
// Wust ensure 700 mode un cur/new/tmp folders and 600 on all files (ie., messages) |
| 868 |
if (_secure) |
| 869 |
{ |
| 870 |
[[NSFileManager defaultManager] enforceMode: 0700 |
| 871 |
atPath: [NSString stringWithFormat: @"%@/%@/cur", _path, aString]]; |
| 872 |
|
| 873 |
[[NSFileManager defaultManager] enforceMode: 0700 |
| 874 |
atPath: [NSString stringWithFormat: @"%@/%@/new", _path, aString]]; |
| 875 |
|
| 876 |
[[NSFileManager defaultManager] enforceMode: 0700 |
| 877 |
atPath: [NSString stringWithFormat: @"%@/%@/tmp", _path, aString]]; |
| 878 |
} |
| 879 |
|
| 880 |
// Get all the children of this directory an remove them from our mutable array. |
| 881 |
subpaths = [[NSFileManager defaultManager] subpathsAtPath: |
| 882 |
[NSString stringWithFormat: @"%@/%@", _path, aString]]; |
| 883 |
[_folders removeObjectsInRange: NSMakeRange(i+1,[subpaths count])]; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
// |
| 888 |
// Second Run: Get rid of cache, summary and OS specific stuff |
| 889 |
// |
| 890 |
tmpArray = [[NSArray alloc] initWithArray: _folders]; |
| 891 |
AUTORELEASE(tmpArray); |
| 892 |
tmpEnumerator = [tmpArray objectEnumerator]; |
| 893 |
|
| 894 |
while ((aString = [tmpEnumerator nextObject])) |
| 895 |
{ |
| 896 |
lastPathComponent = [aString lastPathComponent]; |
| 897 |
pathToFolder = [aString substringToIndex: ([aString length] - [lastPathComponent length])]; |
| 898 |
|
| 899 |
// We remove Netscape/Mozilla summary file. |
| 900 |
[_folders removeObject: [NSString stringWithFormat: @"%@.%@.summary", pathToFolder, lastPathComponent]]; |
| 901 |
|
| 902 |
// We remove Pantomime's cache file. Before doing so, we ensure it's 600 mode. |
| 903 |
[_folders removeObject: [NSString stringWithFormat: @"%@.%@.cache", pathToFolder, lastPathComponent]]; |
| 904 |
|
| 905 |
if (_secure) [[NSFileManager defaultManager] enforceMode: 0600 |
| 906 |
atPath: [NSString stringWithFormat: @"%@/%@.%@.cache", _path, pathToFolder, lastPathComponent]]; |
| 907 |
|
| 908 |
// We also remove Apple Mac OS X .DS_Store directory |
| 909 |
[_folders removeObject: [NSString stringWithFormat: @"%@.DS_Store", pathToFolder]]; |
| 910 |
} |
| 911 |
|
| 912 |
return [_folders objectEnumerator]; |
| 913 |
} |
| 914 |
|
| 915 |
@end |