1 # Copyright (C) 2003 John Goerzen
2 # <jgoerzen@complete.org>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 from offlineimap
import threadutil
, mbnames
, CustomConfig
19 import offlineimap
.repository
.Base
, offlineimap
.repository
.LocalStatus
20 from offlineimap
.ui
import UIBase
21 from offlineimap
.threadutil
import InstanceLimitedThread
, ExitNotifyThread
22 from threading
import Event
25 def getaccountlist(customconfig
):
26 return customconfig
.getsectionlist('Account')
28 def AccountListGenerator(customconfig
):
29 return [Account(customconfig
, accountname
)
30 for accountname
in getaccountlist(customconfig
)]
32 def AccountHashGenerator(customconfig
):
34 for item
in AccountListGenerator(customconfig
):
35 retval
[item
.getname()] = item
40 class Account(CustomConfig
.ConfigHelperMixin
):
41 def __init__(self
, config
, name
):
44 self
.metadatadir
= config
.getmetadatadir()
45 self
.localeval
= config
.getlocaleval()
46 self
.ui
= UIBase
.getglobalui()
47 self
.refreshperiod
= self
.getconffloat('autorefresh', 0.0)
48 if self
.refreshperiod
== 0.0:
49 self
.refreshperiod
= None
51 def getlocaleval(self
):
61 return 'Account ' + self
.getname()
64 """Sleep handler. Returns same value as UIBase.sleep:
65 0 if timeout expired, 1 if there was a request to cancel the timer,
66 and 2 if there is a request to abort the program.
68 Also, returns 100 if configured to not sleep at all."""
70 if not self
.refreshperiod
:
75 if hasattr(self
, 'localrepos'):
76 kaobjs
.append(self
.localrepos
)
77 if hasattr(self
, 'remoterepos'):
78 kaobjs
.append(self
.remoterepos
)
83 refreshperiod
= int(self
.refreshperiod
* 60)
84 sleepresult
= self
.ui
.sleep(refreshperiod
)
86 # Cancel keep-alive, but don't bother terminating threads
88 item
.stopkeepalive(abrupt
= 1)
91 # Cancel keep-alive and wait for thread to terminate.
93 item
.stopkeepalive(abrupt
= 0)
96 class AccountSynchronizationMixin
:
98 self
.ui
.registerthread(self
.name
)
99 self
.ui
.acct(self
.name
)
100 accountmetadata
= self
.getaccountmeta()
101 if not os
.path
.exists(accountmetadata
):
102 os
.mkdir(accountmetadata
, 0700)
104 self
.remoterepos
= offlineimap
.repository
.Base
.LoadRepository(self
.getconf('remoterepository'), self
, 'remote')
106 # Connect to the local repository.
107 self
.localrepos
= offlineimap
.repository
.Base
.LoadRepository(self
.getconf('localrepository'), self
, 'local')
109 # Connect to the local cache.
110 self
.statusrepos
= offlineimap
.repository
.LocalStatus
.LocalStatusRepository(self
.getconf('localrepository'), self
)
112 if not self
.refreshperiod
:
114 self
.ui
.acctdone(self
.name
)
119 looping
= self
.sleeper() != 2
120 self
.ui
.acctdone(self
.name
)
122 def getaccountmeta(self
):
123 return os
.path
.join(self
.metadatadir
, 'Account-' + self
.name
)
126 # We don't need an account lock because syncitall() goes through
127 # each account once, then waits for all to finish.
129 remoterepos
= self
.remoterepos
130 localrepos
= self
.localrepos
131 statusrepos
= self
.statusrepos
132 self
.ui
.syncfolders(remoterepos
, localrepos
)
133 remoterepos
.syncfoldersto(localrepos
)
136 for remotefolder
in remoterepos
.getfolders():
137 thread
= InstanceLimitedThread(\
138 instancename
= 'FOLDER_' + self
.remoterepos
.getname(),
140 name
= "Folder sync %s[%s]" % \
141 (self
.name
, remotefolder
.getvisiblename()),
142 args
= (self
.name
, remoterepos
, remotefolder
, localrepos
,
146 folderthreads
.append(thread
)
147 threadutil
.threadsreset(folderthreads
)
149 localrepos
.holdordropconnections()
150 remoterepos
.holdordropconnections()
154 class SyncableAccount(Account
, AccountSynchronizationMixin
):
159 def reset_time(folder
, atime
, mtime
):
163 def syncfolder(accountname
, remoterepos
, remotefolder
, localrepos
,
166 ui
= UIBase
.getglobalui()
167 ui
.registerthread(accountname
)
169 localfolder
= localrepos
.\
170 getfolder(remotefolder
.getvisiblename().\
171 replace(remoterepos
.getsep(), localrepos
.getsep()))
172 if localrepos
.getrestoreatime():
173 cur_atime
= os
.stat(localfolder
.getfullname() + "/cur")[ST_ATIME
]
174 new_atime
= os
.stat(localfolder
.getfullname() + "/new")[ST_ATIME
]
175 # Write the mailboxes
176 mbnames
.add(accountname
, localfolder
.getvisiblename())
178 ui
.syncingfolder(remoterepos
, remotefolder
, localrepos
, localfolder
)
179 ui
.loadmessagelist(localrepos
, localfolder
)
180 localfolder
.cachemessagelist()
181 ui
.messagelistloaded(localrepos
, localfolder
, len(localfolder
.getmessagelist().keys()))
184 # Load status folder.
185 statusfolder
= statusrepos
.getfolder(remotefolder
.getvisiblename().\
186 replace(remoterepos
.getsep(),
187 statusrepos
.getsep()))
188 if localfolder
.getuidvalidity() == None:
189 # This is a new folder, so delete the status cache to be sure
190 # we don't have a conflict.
191 statusfolder
.deletemessagelist()
193 statusfolder
.cachemessagelist()
195 # If either the local or the status folder has messages and there is a UID
196 # validity problem, warn and abort. If there are no messages, UW IMAPd
197 # loses UIDVALIDITY. But we don't really need it if both local folders are
198 # empty. So, in that case, just save it off.
199 if len(localfolder
.getmessagelist()) or len(statusfolder
.getmessagelist()):
200 if not localfolder
.isuidvalidityok():
201 ui
.validityproblem(localfolder
, localfolder
.getsaveduidvalidity(),
202 localfolder
.getuidvalidity())
203 if localrepos
.getrestoreatime():
204 reset_time(localfolder
.getfullname() + "/new", new_atime
, \
205 os
.stat(localfolder
.getfullname() + "/new")[ST_MTIME
])
206 reset_time(localfolder
.getfullname() + "/cur", new_atime
, \
207 os
.stat(localfolder
.getfullname() + "/cur")[ST_MTIME
])
209 if not remotefolder
.isuidvalidityok():
210 ui
.validityproblem(remotefolder
, remotefolder
.getsaveduidvalidity(),
211 remotefolder
.getuidvalidity())
212 if localrepos
.getrestoreatime():
213 reset_time(localfolder
.getfullname() + "/new", new_atime
, \
214 os
.stat(localfolder
.getfullname() + "/new")[ST_MTIME
])
215 reset_time(localfolder
.getfullname() + "/cur", new_atime
, \
216 os
.stat(localfolder
.getfullname() + "/cur")[ST_MTIME
])
219 localfolder
.saveuidvalidity()
220 remotefolder
.saveuidvalidity()
222 # Load remote folder.
223 ui
.loadmessagelist(remoterepos
, remotefolder
)
224 remotefolder
.cachemessagelist()
225 ui
.messagelistloaded(remoterepos
, remotefolder
,
226 len(remotefolder
.getmessagelist().keys()))
231 if not statusfolder
.isnewfolder():
232 # Delete local copies of remote messages. This way,
233 # if a message's flag is modified locally but it has been
234 # deleted remotely, we'll delete it locally. Otherwise, we
235 # try to modify a deleted message's flags! This step
236 # need only be taken if a statusfolder is present; otherwise,
237 # there is no action taken *to* the remote repository.
239 remotefolder
.syncmessagesto_delete(localfolder
, [localfolder
,
241 ui
.syncingmessages(localrepos
, localfolder
, remoterepos
, remotefolder
)
242 localfolder
.syncmessagesto(statusfolder
, [remotefolder
, statusfolder
])
244 # Synchronize remote changes.
245 ui
.syncingmessages(remoterepos
, remotefolder
, localrepos
, localfolder
)
246 remotefolder
.syncmessagesto(localfolder
, [localfolder
, statusfolder
])
248 # Make sure the status folder is up-to-date.
249 ui
.syncingmessages(localrepos
, localfolder
, statusrepos
, statusfolder
)
250 localfolder
.syncmessagesto(statusfolder
)
252 if localrepos
.getrestoreatime():
253 reset_time(localfolder
.getfullname() + "/new", new_atime
, \
254 os
.stat(localfolder
.getfullname() + "/new")[ST_MTIME
])
255 reset_time(localfolder
.getfullname() + "/cur", new_atime
, \
256 os
.stat(localfolder
.getfullname() + "/cur")[ST_MTIME
])