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
):
157 def syncfolder(accountname
, remoterepos
, remotefolder
, localrepos
,
160 ui
= UIBase
.getglobalui()
161 ui
.registerthread(accountname
)
163 localfolder
= localrepos
.\
164 getfolder(remotefolder
.getvisiblename().\
165 replace(remoterepos
.getsep(), localrepos
.getsep()))
166 # Write the mailboxes
167 mbnames
.add(accountname
, localfolder
.getvisiblename())
169 ui
.syncingfolder(remoterepos
, remotefolder
, localrepos
, localfolder
)
170 ui
.loadmessagelist(localrepos
, localfolder
)
171 localfolder
.cachemessagelist()
172 ui
.messagelistloaded(localrepos
, localfolder
, len(localfolder
.getmessagelist().keys()))
175 # Load status folder.
176 statusfolder
= statusrepos
.getfolder(remotefolder
.getvisiblename().\
177 replace(remoterepos
.getsep(),
178 statusrepos
.getsep()))
179 if localfolder
.getuidvalidity() == None:
180 # This is a new folder, so delete the status cache to be sure
181 # we don't have a conflict.
182 statusfolder
.deletemessagelist()
184 statusfolder
.cachemessagelist()
186 # If either the local or the status folder has messages and there is a UID
187 # validity problem, warn and abort. If there are no messages, UW IMAPd
188 # loses UIDVALIDITY. But we don't really need it if both local folders are
189 # empty. So, in that case, just save it off.
190 if len(localfolder
.getmessagelist()) or len(statusfolder
.getmessagelist()):
191 if not localfolder
.isuidvalidityok():
192 ui
.validityproblem(localfolder
, localfolder
.getsaveduidvalidity(),
193 localfolder
.getuidvalidity())
195 if not remotefolder
.isuidvalidityok():
196 ui
.validityproblem(remotefolder
, remotefolder
.getsaveduidvalidity(),
197 remotefolder
.getuidvalidity())
200 localfolder
.saveuidvalidity()
201 remotefolder
.saveuidvalidity()
203 # Load remote folder.
204 ui
.loadmessagelist(remoterepos
, remotefolder
)
205 remotefolder
.cachemessagelist()
206 ui
.messagelistloaded(remoterepos
, remotefolder
,
207 len(remotefolder
.getmessagelist().keys()))
212 if not statusfolder
.isnewfolder():
213 # Delete local copies of remote messages. This way,
214 # if a message's flag is modified locally but it has been
215 # deleted remotely, we'll delete it locally. Otherwise, we
216 # try to modify a deleted message's flags! This step
217 # need only be taken if a statusfolder is present; otherwise,
218 # there is no action taken *to* the remote repository.
220 remotefolder
.syncmessagesto_delete(localfolder
, [localfolder
,
222 ui
.syncingmessages(localrepos
, localfolder
, remoterepos
, remotefolder
)
223 localfolder
.syncmessagesto(statusfolder
, [remotefolder
, statusfolder
])
225 # Synchronize remote changes.
226 ui
.syncingmessages(remoterepos
, remotefolder
, localrepos
, localfolder
)
227 remotefolder
.syncmessagesto(localfolder
, [localfolder
, statusfolder
])
229 # Make sure the status folder is up-to-date.
230 ui
.syncingmessages(localrepos
, localfolder
, statusrepos
, statusfolder
)
231 localfolder
.syncmessagesto(statusfolder
)