Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. # PyObjC Code to import Apple Mail account info
  2. # By Michael McCracken,
  3. # This code is in the public domain.
  4.  
  5. # 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.
  6. # there is also a button in the XIB that calls "importSettingsAction_".
  7.  
  8. from objc import YES, NO, IBAction, IBOutlet
  9. from Foundation import *
  10. from AppKit import *
  11. import time
  12.  
  13. class AccountSetupController(NSObject):
  14.  
  15. view = IBOutlet()
  16. accountsTableView = IBOutlet()
  17.  
  18. def initWithAccountLoadingDelegate_(self, delegate):
  19. self = super(AccountSetupController, self).init()
  20. if self is None: return None
  21. self.delegate = delegate
  22.  
  23. self.imapAccounts = []
  24. self.smtpAccounts = {}
  25.  
  26. sud = NSUserDefaults.standardUserDefaults()
  27. self.mailDefaults = sud.persistentDomainForName_("com.apple.mail")
  28.  
  29. # stores which of appleMailAccounts we should import
  30. self.importIndexes = []
  31. return self
  32.  
  33. def getView(self):
  34. NSBundle.loadNibNamed_owner_("AppleMailAccountImportView", self)
  35. return self.view
  36.  
  37.  
  38. def awakeFromNib(self):
  39. if self.mailDefaults is not None:
  40. self.getSMTPAccounts(self.mailDefaults)
  41. self.getIMAPAccounts(self.mailDefaults)
  42. else:
  43. pass # FIXME: do something smart if there are no mail.app defaults.
  44. self.accountsTableView.reloadData()
  45.  
  46. @IBAction
  47. def addNewBlankAccount_(self, sender):
  48. acct = {'Hostname' : 'mailserver address',
  49. 'Username' : 'username',
  50. 'AccountName': 'new account',
  51. 'importedFromAppleMail' : NO}
  52.  
  53. self.imapAccounts.append(acct)
  54. self.importIndexes.append(True)
  55. newIdx = len(self.imapAccounts) - 1
  56. self.accountsTableView.reloadData()
  57. self.accountsTableView.editColumn_row_withEvent_select_(1, newIdx, None, YES)
  58.  
  59.  
  60. @IBAction
  61. def importSettingsAction_(self, sender):
  62. accountsToImport = [a for a in self.imapAccounts if \
  63. self.importIndexes[self.imapAccounts.index(a)]]
  64. finalAccts = []
  65. for a in accountsToImport:
  66. smtpserver = ""
  67. smtpuser = ""
  68. if a.has_key('SMTPAccount'):
  69. smtpserver = a['SMTPAccount']['Hostname']
  70. smtpuser = a['SMTPAccount']['Username']
  71.  
  72. if 'ServerPathPrefix' in a:
  73. prefix = a['ServerPathPrefix'] + '/'
  74. else:
  75. prefix = ''
  76.  
  77.  
  78. # NOTE: IMAPServer / SMTPServer / Account are implemented elsewhere.
  79. # They're roughly what you'd think.
  80.  
  81. imapsrv = IMAPServer()
  82. imapsrv.hostname = a['Hostname']
  83. imapsrv.username = a['Username']
  84. imapsrv.imapprefix = prefix
  85.  
  86. smtpsrv = SMTPServer()
  87. smtpsrv.hostname = smtpserver
  88. smtpsrv.username = smtpuser
  89.  
  90. a = Account()
  91. a.name = "%s - %s" % (imapsrv.hostname, imapsrv.username)
  92. a.imap_server = imapsrv
  93. a.smtp_server = smtpsrv
  94. a.enabled = True
  95.  
  96. self.delegate.handleMailAccountImportDone()
  97.  
  98.  
  99. def getSMTPAccounts(self, mailDefaults):
  100. smtpaccts = mailDefaults['DeliveryAccounts']
  101. for a in smtpaccts:
  102. key = a['Hostname']
  103. if a.has_key('Username'):
  104. username = a['Username']
  105. key += ":" + username
  106. self.smtpAccounts[key] = a
  107.  
  108.  
  109. def getIMAPAccounts(self, mailDefaults):
  110. mailAccounts = mailDefaults['MailAccounts']
  111.  
  112. excludedAccountTypes = ["LocalAccount", "RSSAccount", "MailCalDAVAccount"]
  113. mailAccounts = [a for a in mailAccounts if a['AccountType'] not in excludedAccountTypes]
  114.  
  115. finalMailAccounts = []
  116. # fix up entries itools accounts:
  117. for a in mailAccounts:
  118. a = a.mutableCopy()
  119. if a['AccountType'] == "iToolsAccount":
  120. a['Hostname'] = 'mail.mac.com'
  121. if a.has_key('SMTPIdentifier'):
  122. a['SMTPAccount'] = self.smtpAccounts[a['SMTPIdentifier']]
  123.  
  124. a['importedFromAppleMail'] = YES
  125. finalMailAccounts.append(a)
  126.  
  127. self.imapAccounts = finalMailAccounts
  128. print "these accounts have been read successfully:" , finalMailAccounts
  129. self.importIndexes = [False for _ in range(len(mailAccounts))]
  130.  
  131.  
  132. # tableView stuff:
  133.  
  134. def numberOfRowsInTableView_(self, tv):
  135. return len(self.imapAccounts)
  136.  
  137. def tableView_objectValueForTableColumn_row_(self, tv, tc, row):
  138. a = self.imapAccounts[row]
  139. tcid = tc.identifier()
  140. try:
  141. if tcid == "hostName":
  142. return a['Hostname']
  143. if tcid == "userName":
  144. return a['Username']
  145. if tcid == "accountName":
  146. return a['AccountName']
  147. if tcid == "doImport":
  148. return self.importIndexes[row]
  149. except:
  150. print "problem with identifier", tcid, "and dict", a
  151. return "dunno, check the console?"
  152. return "hey, kill this column!"
  153.  
  154.  
  155. def tableView_shouldEditTableColumn_row_(self, tv, tc, row):
  156. tcid = tc.identifier()
  157. a = self.imapAccounts[row]
  158. if a['importedFromAppleMail'] == YES and tcid != "doImport":
  159. return NO
  160. else:
  161. return YES
  162.  
  163.  
  164. def tableView_setObjectValue_forTableColumn_row_(self, tv, newValue, tc, row):
  165. a = self.imapAccounts[row]
  166. tcid = tc.identifier()
  167. try:
  168. if tcid == "hostName":
  169. a['Hostname'] = newValue
  170. if tcid == "userName":
  171. a['Username'] = newValue
  172. if tcid == "accountName":
  173. a['AccountName'] = newValue
  174. if tcid == "doImport":
  175. self.importIndexes[row] = newValue
  176. except:
  177. print "problem with identifier", tcid, "and dict", a
Add Comment
Please, Sign In to add comment