// // BPNewMailGetter.m // Piles3 // // Created by Bea on 5/09/05. // Copyright 2005 __MyCompanyName__. All rights reserved. // #import "BPNewMailGetter.h" #import "BPEmailDocument.h" #import "BPPilesDataController.h" #import "BPMailAppData.h" #import "BPAppController.h" #import "BPPerson.h" #import "BPFetchMailIndicator.h" #import #import @implementation BPNewMailGetter - (void)saveLastGottenMailUid { /* NSString *configFilePath = [[appController rootSavePath] stringByAppendingPathComponent:@"config"]; NSDictionary *rootObject = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:lastGottenMailUid] forKey:@"lastGottenMailUid"]; // archive to file int result = [NSKeyedArchiver archiveRootObject:rootObject toFile:configFilePath]; NSLog(@"saved lastGottenMailUid %d", lastGottenMailUid); */ NSString *configFilePath = [[appController rootSavePath] stringByAppendingPathComponent:@"config.plist"]; NSDictionary *configData = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:lastGottenMailUid] forKey:@"lastGottenMailUid"]; [configData writeToFile:configFilePath atomically:YES]; NSLog(@"saved lastGottenMailUid %d", lastGottenMailUid); } - (void)loadLastGottenMailUid { NSString *configFilePath = [[appController rootSavePath] stringByAppendingPathComponent:@"config.plist"]; NSDictionary *configData = [NSDictionary dictionaryWithContentsOfFile:configFilePath]; if (configData != nil) { NSNumber *value = [configData objectForKey:@"lastGottenMailUid"]; if ([value isKindOfClass:[NSNumber class]]) { lastGottenMailUid = [value intValue]; NSLog(@"loaded lastGottenMailUid %d", lastGottenMailUid); return; } } // otherwise... NSLog(@"no lastGottenMailUid"); lastGottenMailUid = -1; } - (void)awakeFromNib { mailLimitOnFirstGet = 40; [self loadLastGottenMailUid]; importOrderPref = ([[NSUserDefaults standardUserDefaults] boolForKey:@"MailStacker_reverseImports"]); } - (void)setInbox:(MailboxUid *)theInbox { [theInbox retain]; [inbox release]; inbox = theInbox; // use account email address as email address for "me" [BPPerson setMyEmail:[[inbox account] firstEmailAddress]]; } - (void)setLastGottenMailUid:(int)uid { lastGottenMailUid = uid; [self saveLastGottenMailUid]; } /* // next time I get mail, retrieve the last --- emails as well - (void)setMailLimitOnFirstGet:(int)limit { mailLimitOnFirstGet = limit; //[self setLastGottenMailUid:-1]; } */ /* - (void)checkForNewMail { // start with a scratch string of a decent length NSMutableString *s = [NSMutableString stringWithCapacity:1000]; } */ - (MailboxUid *)findInbox { int numOfAccounts = [[mailAppData mailAccounts] count]; NSArray *mailboxes; NSString *mailboxName; int i, j; for (i=0; i oldest firstMsg = [allMsgs lastObject]; } mostRecentEmailUid = (int)[firstMsg uid]; int newMailCount; if (lastGottenMailUid == -1) { newMailCount = mailLimitOnFirstGet; } else { newMailCount = (int)[firstMsg uid] - lastGottenMailUid; } NSLog(@"newMailCount %d", newMailCount); if (newMailCount > 0) { [fetchMailIndicator setFetchCount:newMailCount]; } else { [fetchMailIndicator setMessage:@"No new mail."]; [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.5]]; } while ((currentMsg = [enumerator nextObject]) != nil) { if (!doImportMail) break; //currentMsg = [allMsgs objectAtIndex:i]; //NSLog(@"New msg: %@", [currentMsg subject]); if (lastGottenMailUid == -1) { if ([emailDocuments count] >= mailLimitOnFirstGet) { NSLog(@"reached mailLimitOnFirstGet"); break; } } else { if ((int)[currentMsg uid] <= lastGottenMailUid) { NSLog(@"reached UID for last gotten msg %@", [currentMsg subject]); break; } } [fetchMailIndicator parsingMessageWithSubject:[currentMsg subject]]; if (![currentMsg isJunk] && ![currentMsg isDeleted]) { emailDoc = [BPEmailDocument emailDocumentWithMessage:currentMsg]; [emailDocuments addObject:emailDoc]; } } [self setLastGottenMailUid:mostRecentEmailUid]; if ([emailDocuments count] > 0) { // send to inbox in reverse (older emails first) int i; for (i=[emailDocuments count]-1; i>=0; i--) { [pileDataController addPileDocument:[emailDocuments objectAtIndex:i] toPile:[BPPilesDataController INBOX]]; } // save newly generated favicons [appController saveFavicons]; } // sometimes getting mail somehow stuffs up the ordering of mailboxes and it's // no longer in the same order if you get it again, so refresh so the mailbox // browser will show the mailbox list properly, otherwise imports don't work [mailAppData refreshAccountData]; [fetchMailIndicator stopIndicator]; } - (IBAction)getMail:(id)sender { if (inbox == nil) { //[inboxChooser startInboxChooser:nil]; [self setInbox:[self findInbox]]; } if (inbox == nil) { NSLog(@"No inbox found, can't get mail."); return; } // if ordering pref has been changed, reset marker for where to import up to BOOL newImportOrderPref = [[NSUserDefaults standardUserDefaults] boolForKey:@"MailStacker_reverseImports"]; if (importOrderPref != newImportOrderPref) { [self setLastGottenMailUid:-1]; importOrderPref = newImportOrderPref; } [NSApplication detachDrawingThread:@selector(doGetMail) toTarget:self withObject:nil]; } - (void)dealloc { NSLog(@"destroying %@", self); [inbox release]; [super dealloc]; } @end