Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PyObjC Code to import Apple Mail account info
- # By Michael McCracken,
- # This code is in the public domain.
- # It's used with a basic XIB file that consists mostly of one table view that has one tablecolumn as a checkbox to en/disable importing that account.
- # there is also a button in the XIB that calls "importSettingsAction_".
- from objc import YES, NO, IBAction, IBOutlet
- from Foundation import *
- from AppKit import *
- import time
- class AccountSetupController(NSObject):
- view = IBOutlet()
- accountsTableView = IBOutlet()
- def initWithAccountLoadingDelegate_(self, delegate):
- self = super(AccountSetupController, self).init()
- if self is None: return None
- self.delegate = delegate
- self.imapAccounts = []
- self.smtpAccounts = {}
- sud = NSUserDefaults.standardUserDefaults()
- self.mailDefaults = sud.persistentDomainForName_("com.apple.mail")
- # stores which of appleMailAccounts we should import
- self.importIndexes = []
- return self
- def getView(self):
- NSBundle.loadNibNamed_owner_("AppleMailAccountImportView", self)
- return self.view
- def awakeFromNib(self):
- if self.mailDefaults is not None:
- self.getSMTPAccounts(self.mailDefaults)
- self.getIMAPAccounts(self.mailDefaults)
- else:
- pass # FIXME: do something smart if there are no mail.app defaults.
- self.accountsTableView.reloadData()
- @IBAction
- def addNewBlankAccount_(self, sender):
- acct = {'Hostname' : 'mailserver address',
- 'Username' : 'username',
- 'AccountName': 'new account',
- 'importedFromAppleMail' : NO}
- self.imapAccounts.append(acct)
- self.importIndexes.append(True)
- newIdx = len(self.imapAccounts) - 1
- self.accountsTableView.reloadData()
- self.accountsTableView.editColumn_row_withEvent_select_(1, newIdx, None, YES)
- @IBAction
- def importSettingsAction_(self, sender):
- accountsToImport = [a for a in self.imapAccounts if \
- self.importIndexes[self.imapAccounts.index(a)]]
- finalAccts = []
- for a in accountsToImport:
- smtpserver = ""
- smtpuser = ""
- if a.has_key('SMTPAccount'):
- smtpserver = a['SMTPAccount']['Hostname']
- smtpuser = a['SMTPAccount']['Username']
- if 'ServerPathPrefix' in a:
- prefix = a['ServerPathPrefix'] + '/'
- else:
- prefix = ''
- # NOTE: IMAPServer / SMTPServer / Account are implemented elsewhere.
- # They're roughly what you'd think.
- imapsrv = IMAPServer()
- imapsrv.hostname = a['Hostname']
- imapsrv.username = a['Username']
- imapsrv.imapprefix = prefix
- smtpsrv = SMTPServer()
- smtpsrv.hostname = smtpserver
- smtpsrv.username = smtpuser
- a = Account()
- a.name = "%s - %s" % (imapsrv.hostname, imapsrv.username)
- a.imap_server = imapsrv
- a.smtp_server = smtpsrv
- a.enabled = True
- self.delegate.handleMailAccountImportDone()
- def getSMTPAccounts(self, mailDefaults):
- smtpaccts = mailDefaults['DeliveryAccounts']
- for a in smtpaccts:
- key = a['Hostname']
- if a.has_key('Username'):
- username = a['Username']
- key += ":" + username
- self.smtpAccounts[key] = a
- def getIMAPAccounts(self, mailDefaults):
- mailAccounts = mailDefaults['MailAccounts']
- excludedAccountTypes = ["LocalAccount", "RSSAccount", "MailCalDAVAccount"]
- mailAccounts = [a for a in mailAccounts if a['AccountType'] not in excludedAccountTypes]
- finalMailAccounts = []
- # fix up entries itools accounts:
- for a in mailAccounts:
- a = a.mutableCopy()
- if a['AccountType'] == "iToolsAccount":
- a['Hostname'] = 'mail.mac.com'
- if a.has_key('SMTPIdentifier'):
- a['SMTPAccount'] = self.smtpAccounts[a['SMTPIdentifier']]
- a['importedFromAppleMail'] = YES
- finalMailAccounts.append(a)
- self.imapAccounts = finalMailAccounts
- print "these accounts have been read successfully:" , finalMailAccounts
- self.importIndexes = [False for _ in range(len(mailAccounts))]
- # tableView stuff:
- def numberOfRowsInTableView_(self, tv):
- return len(self.imapAccounts)
- def tableView_objectValueForTableColumn_row_(self, tv, tc, row):
- a = self.imapAccounts[row]
- tcid = tc.identifier()
- try:
- if tcid == "hostName":
- return a['Hostname']
- if tcid == "userName":
- return a['Username']
- if tcid == "accountName":
- return a['AccountName']
- if tcid == "doImport":
- return self.importIndexes[row]
- except:
- print "problem with identifier", tcid, "and dict", a
- return "dunno, check the console?"
- return "hey, kill this column!"
- def tableView_shouldEditTableColumn_row_(self, tv, tc, row):
- tcid = tc.identifier()
- a = self.imapAccounts[row]
- if a['importedFromAppleMail'] == YES and tcid != "doImport":
- return NO
- else:
- return YES
- def tableView_setObjectValue_forTableColumn_row_(self, tv, newValue, tc, row):
- a = self.imapAccounts[row]
- tcid = tc.identifier()
- try:
- if tcid == "hostName":
- a['Hostname'] = newValue
- if tcid == "userName":
- a['Username'] = newValue
- if tcid == "accountName":
- a['AccountName'] = newValue
- if tcid == "doImport":
- self.importIndexes[row] = newValue
- except:
- print "problem with identifier", tcid, "and dict", a
Add Comment
Please, Sign In to add comment