2002-06-21 07:29:12 +02:00
|
|
|
# UI base class
|
|
|
|
# Copyright (C) 2002 John Goerzen
|
|
|
|
# <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-21 07:29:12 +02:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-06-22 07:29:48 +02:00
|
|
|
import offlineimap.version
|
2003-01-05 12:50:01 +01:00
|
|
|
import re, time, sys, traceback, threading, thread
|
2002-07-05 04:34:39 +02:00
|
|
|
from StringIO import StringIO
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-08-08 22:03:36 +02:00
|
|
|
debugtypes = {'imap': 'IMAP protocol debugging',
|
2003-06-02 21:06:18 +02:00
|
|
|
'maildir': 'Maildir repository debugging',
|
|
|
|
'thread': 'Threading debugging'}
|
2002-08-08 22:03:36 +02:00
|
|
|
|
2002-10-07 22:59:02 +02:00
|
|
|
globalui = None
|
|
|
|
def setglobalui(newui):
|
|
|
|
global globalui
|
|
|
|
globalui = newui
|
|
|
|
def getglobalui():
|
|
|
|
global globalui
|
|
|
|
return globalui
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
class UIBase:
|
2002-07-25 00:20:42 +02:00
|
|
|
def __init__(s, config, verbose = 0):
|
2002-07-22 07:40:29 +02:00
|
|
|
s.verbose = verbose
|
2002-07-25 00:20:42 +02:00
|
|
|
s.config = config
|
2002-08-08 22:03:36 +02:00
|
|
|
s.debuglist = []
|
2002-10-17 01:27:27 +02:00
|
|
|
s.debugmessages = {}
|
|
|
|
s.debugmsglen = 50
|
2003-01-05 12:50:01 +01:00
|
|
|
s.threadaccounts = {}
|
2003-06-02 21:06:18 +02:00
|
|
|
s.logfile = None
|
2002-07-22 07:38:06 +02:00
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## UTILS
|
|
|
|
def _msg(s, msg):
|
|
|
|
"""Generic tool called when no other works."""
|
2003-06-02 21:06:18 +02:00
|
|
|
s._log(msg)
|
|
|
|
s._display(msg)
|
|
|
|
|
|
|
|
def _log(s, msg):
|
|
|
|
"""Log it to disk. Returns true if it wrote something; false
|
|
|
|
otherwise."""
|
|
|
|
if s.logfile:
|
|
|
|
s.logfile.write("%s: %s\n" % (threading.currentThread().getName(),
|
|
|
|
msg))
|
2003-06-02 21:52:33 +02:00
|
|
|
return 1
|
|
|
|
return 0
|
2003-06-02 21:06:18 +02:00
|
|
|
|
|
|
|
def setlogfd(s, logfd):
|
|
|
|
s.logfile = logfd
|
2006-10-19 03:04:28 +02:00
|
|
|
logfd.write("This is %s %s\n" % \
|
2003-06-02 21:52:33 +02:00
|
|
|
(offlineimap.version.productname,
|
2006-10-19 03:04:28 +02:00
|
|
|
offlineimap.version.versionstr))
|
2003-06-02 21:52:33 +02:00
|
|
|
logfd.write("Python: %s\n" % sys.version)
|
|
|
|
logfd.write("Platform: %s\n" % sys.platform)
|
|
|
|
logfd.write("Args: %s\n" % sys.argv)
|
2003-06-02 21:06:18 +02:00
|
|
|
|
|
|
|
def _display(s, msg):
|
|
|
|
"""Display a message."""
|
2002-07-22 08:02:24 +02:00
|
|
|
raise NotImplementedError
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-10-30 05:26:49 +01:00
|
|
|
def warn(s, msg, minor = 0):
|
|
|
|
if minor:
|
|
|
|
s._msg("warning: " + msg)
|
|
|
|
else:
|
|
|
|
s._msg("WARNING: " + msg)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2003-01-05 12:50:01 +01:00
|
|
|
def registerthread(s, account):
|
|
|
|
"""Provides a hint to UIs about which account this particular
|
|
|
|
thread is processing."""
|
2003-01-05 13:01:17 +01:00
|
|
|
if s.threadaccounts.has_key(threading.currentThread()):
|
2003-01-06 00:07:58 +01:00
|
|
|
raise ValueError, "Thread %s already registered (old %s, new %s)" %\
|
|
|
|
(threading.currentThread().getName(),
|
|
|
|
s.getthreadaccount(s), account)
|
2003-01-05 13:01:17 +01:00
|
|
|
s.threadaccounts[threading.currentThread()] = account
|
2003-01-05 12:50:01 +01:00
|
|
|
|
|
|
|
def unregisterthread(s, thr):
|
|
|
|
"""Recognizes a thread has exited."""
|
|
|
|
if s.threadaccounts.has_key(thr):
|
|
|
|
del s.threadaccounts[thr]
|
|
|
|
|
2003-01-05 13:01:17 +01:00
|
|
|
def getthreadaccount(s, thr = None):
|
|
|
|
if not thr:
|
|
|
|
thr = threading.currentThread()
|
|
|
|
if s.threadaccounts.has_key(thr):
|
|
|
|
return s.threadaccounts[thr]
|
2003-01-06 00:07:58 +01:00
|
|
|
return '*Control'
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2002-08-08 22:03:36 +02:00
|
|
|
def debug(s, debugtype, msg):
|
2002-10-17 01:27:27 +02:00
|
|
|
thisthread = threading.currentThread()
|
|
|
|
if s.debugmessages.has_key(thisthread):
|
|
|
|
s.debugmessages[thisthread].append("%s: %s" % (debugtype, msg))
|
|
|
|
else:
|
|
|
|
s.debugmessages[thisthread] = ["%s: %s" % (debugtype, msg)]
|
|
|
|
|
|
|
|
while len(s.debugmessages[thisthread]) > s.debugmsglen:
|
|
|
|
s.debugmessages[thisthread] = s.debugmessages[thisthread][1:]
|
2003-06-02 21:06:18 +02:00
|
|
|
|
2003-06-02 21:52:33 +02:00
|
|
|
if debugtype in s.debuglist:
|
|
|
|
if not s._log("DEBUG[%s]: %s" % (debugtype, msg)):
|
2003-06-02 21:06:18 +02:00
|
|
|
s._display("DEBUG[%s]: %s" % (debugtype, msg))
|
2002-08-08 22:03:36 +02:00
|
|
|
|
|
|
|
def add_debug(s, debugtype):
|
|
|
|
global debugtypes
|
|
|
|
if debugtype in debugtypes:
|
|
|
|
if not debugtype in s.debuglist:
|
|
|
|
s.debuglist.append(debugtype)
|
|
|
|
s.debugging(debugtype)
|
|
|
|
else:
|
|
|
|
s.invaliddebug(debugtype)
|
|
|
|
|
|
|
|
def debugging(s, debugtype):
|
|
|
|
global debugtypes
|
|
|
|
s._msg("Now debugging for %s: %s" % (debugtype, debugtypes[debugtype]))
|
|
|
|
|
|
|
|
def invaliddebug(s, debugtype):
|
|
|
|
s.warn("Invalid debug type: %s" % debugtype)
|
|
|
|
|
2003-01-30 02:19:53 +01:00
|
|
|
def locked(s):
|
2003-04-29 03:48:55 +02:00
|
|
|
raise Exception, "Another OfflineIMAP is running with the same metadatadir; exiting."
|
2003-01-30 02:19:53 +01:00
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
def getnicename(s, object):
|
2002-06-21 07:42:39 +02:00
|
|
|
prelimname = str(object.__class__).split('.')[-1]
|
|
|
|
# Strip off extra stuff.
|
|
|
|
return re.sub('(Folder|Repository)', '', prelimname)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-07-12 03:38:36 +02:00
|
|
|
def isusable(s):
|
|
|
|
"""Returns true if this UI object is usable in the current
|
|
|
|
environment. For instance, an X GUI would return true if it's
|
|
|
|
being run in X with a valid DISPLAY setting, and false otherwise."""
|
|
|
|
return 1
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## INPUT
|
|
|
|
|
2002-11-05 00:15:42 +01:00
|
|
|
def getpass(s, accountname, config, errmsg = None):
|
2002-07-22 08:02:24 +02:00
|
|
|
raise NotImplementedError
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-06-21 08:51:21 +02:00
|
|
|
def folderlist(s, list):
|
|
|
|
return ', '.join(["%s[%s]" % (s.getnicename(x), x.getname()) for x in list])
|
|
|
|
|
2002-08-08 03:57:17 +02:00
|
|
|
################################################## WARNINGS
|
|
|
|
def msgtoreadonly(s, destfolder, uid, content, flags):
|
|
|
|
if not (config.has_option('general', 'ignore-readonly') and config.getboolean("general", "ignore-readonly")):
|
|
|
|
s.warn("Attempted to synchronize message %d to folder %s[%s], but that folder is read-only. The message will not be copied to that folder." % \
|
|
|
|
(uid, s.getnicename(destfolder), destfolder.getname()))
|
|
|
|
|
|
|
|
def flagstoreadonly(s, destfolder, uidlist, flags):
|
|
|
|
if not (config.has_option('general', 'ignore-readonly') and config.getboolean("general", "ignore-readonly")):
|
|
|
|
s.warn("Attempted to modify flags for messages %s in folder %s[%s], but that folder is read-only. No flags have been modified for that message." % \
|
|
|
|
(str(uidlist), s.getnicename(destfolder), destfolder.getname()))
|
|
|
|
|
|
|
|
def deletereadonly(s, destfolder, uidlist):
|
|
|
|
if not (config.has_option('general', 'ignore-readonly') and config.getboolean("general", "ignore-readonly")):
|
|
|
|
s.warn("Attempted to delete messages %s in folder %s[%s], but that folder is read-only. No messages have been deleted in that folder." % \
|
|
|
|
(str(uidlist), s.getnicename(destfolder), destfolder.getname()))
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## MESSAGES
|
|
|
|
|
|
|
|
def init_banner(s):
|
2002-07-12 03:38:36 +02:00
|
|
|
"""Called when the UI starts. Must be called before any other UI
|
|
|
|
call except isusable(). Displays the copyright banner. This is
|
|
|
|
where the UI should do its setup -- TK, for instance, would
|
|
|
|
create the application window here."""
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg(offlineimap.version.banner)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-07-25 06:46:27 +02:00
|
|
|
def connecting(s, hostname, port):
|
|
|
|
if s.verbose < 0:
|
|
|
|
return
|
|
|
|
if hostname == None:
|
|
|
|
hostname = ''
|
|
|
|
if port != None:
|
2003-04-18 04:18:34 +02:00
|
|
|
port = ":%s" % str(port)
|
2002-07-25 06:46:27 +02:00
|
|
|
displaystr = ' to %s%s.' % (hostname, port)
|
|
|
|
if hostname == '' and port == None:
|
|
|
|
displaystr = '.'
|
|
|
|
s._msg("Establishing connection" + displaystr)
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
def acct(s, accountname):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg("***** Processing account %s" % accountname)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2003-01-04 05:57:46 +01:00
|
|
|
def acctdone(s, accountname):
|
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg("***** Finished processing account " + accountname)
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
def syncfolders(s, srcrepos, destrepos):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg("Copying folder structure from %s to %s" % \
|
|
|
|
(s.getnicename(srcrepos), s.getnicename(destrepos)))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
############################## Folder syncing
|
|
|
|
def syncingfolder(s, srcrepos, srcfolder, destrepos, destfolder):
|
|
|
|
"""Called when a folder sync operation is started."""
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg("Syncing %s: %s -> %s" % (srcfolder.getname(),
|
|
|
|
s.getnicename(srcrepos),
|
|
|
|
s.getnicename(destrepos)))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def validityproblem(s, folder, saved, new):
|
|
|
|
s.warn("UID validity problem for folder %s (saved %d; got %d); skipping it" % \
|
|
|
|
(folder.getname(), saved, new))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
def loadmessagelist(s, repos, folder):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose > 0:
|
2002-07-22 07:38:06 +02:00
|
|
|
s._msg("Loading message list for %s[%s]" % (s.getnicename(repos),
|
|
|
|
folder.getname()))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
def messagelistloaded(s, repos, folder, count):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose > 0:
|
2002-07-22 07:38:06 +02:00
|
|
|
s._msg("Message list for %s[%s] loaded: %d messages" % \
|
|
|
|
(s.getnicename(repos), folder.getname(), count))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
############################## Message syncing
|
|
|
|
|
|
|
|
def syncingmessages(s, sr, sf, dr, df):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose > 0:
|
2002-07-22 07:38:06 +02:00
|
|
|
s._msg("Syncing messages %s[%s] -> %s[%s]" % (s.getnicename(sr),
|
|
|
|
sf.getname(),
|
|
|
|
s.getnicename(dr),
|
|
|
|
df.getname()))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
def copyingmessage(s, uid, src, destlist):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
ds = s.folderlist(destlist)
|
|
|
|
s._msg("Copy message %d %s[%s] -> %s" % (uid, s.getnicename(src),
|
|
|
|
src.getname(), ds))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
def deletingmessage(s, uid, destlist):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
ds = s.folderlist(destlist)
|
|
|
|
s._msg("Deleting message %d in %s" % (uid, ds))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-07-03 07:05:49 +02:00
|
|
|
def deletingmessages(s, uidlist, destlist):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
ds = s.folderlist(destlist)
|
|
|
|
s._msg("Deleting %d messages (%s) in %s" % \
|
|
|
|
(len(uidlist),
|
|
|
|
", ".join([str(u) for u in uidlist]),
|
|
|
|
ds))
|
2002-07-03 07:05:49 +02:00
|
|
|
|
2003-01-09 03:16:07 +01:00
|
|
|
def addingflags(s, uidlist, flags, destlist):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
ds = s.folderlist(destlist)
|
2003-01-09 03:16:07 +01:00
|
|
|
s._msg("Adding flags %s to %d messages on %s" % \
|
|
|
|
(", ".join(flags), len(uidlist), ds))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2003-01-09 03:16:07 +01:00
|
|
|
def deletingflags(s, uidlist, flags, destlist):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
ds = s.folderlist(destlist)
|
2003-01-09 03:16:07 +01:00
|
|
|
s._msg("Deleting flags %s to %d messages on %s" % \
|
|
|
|
(", ".join(flags), len(uidlist), ds))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
################################################## Threads
|
|
|
|
|
2002-10-17 01:27:27 +02:00
|
|
|
def getThreadDebugLog(s, thread):
|
|
|
|
if s.debugmessages.has_key(thread):
|
|
|
|
message = "\nLast %d debug messages logged for %s prior to exception:\n"\
|
|
|
|
% (len(s.debugmessages[thread]), thread.getName())
|
|
|
|
message += "\n".join(s.debugmessages[thread])
|
|
|
|
else:
|
|
|
|
message = "\nNo debug messages were logged for %s." % \
|
|
|
|
thread.getName()
|
|
|
|
return message
|
|
|
|
|
|
|
|
def delThreadDebugLog(s, thread):
|
|
|
|
if s.debugmessages.has_key(thread):
|
|
|
|
del s.debugmessages[thread]
|
|
|
|
|
|
|
|
def getThreadExceptionString(s, thread):
|
|
|
|
message = "Thread '%s' terminated with exception:\n%s" % \
|
|
|
|
(thread.getName(), thread.getExitStackTrace())
|
|
|
|
message += "\n" + s.getThreadDebugLog(thread)
|
|
|
|
return message
|
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
def threadException(s, thread):
|
|
|
|
"""Called when a thread has terminated with an exception.
|
|
|
|
The argument is the ExitNotifyThread that has so terminated."""
|
2002-10-17 01:27:27 +02:00
|
|
|
s._msg(s.getThreadExceptionString(thread))
|
|
|
|
s.delThreadDebugLog(thread)
|
2002-07-05 04:34:39 +02:00
|
|
|
s.terminate(100)
|
|
|
|
|
2002-10-17 01:27:27 +02:00
|
|
|
def getMainExceptionString(s):
|
2002-07-05 04:34:39 +02:00
|
|
|
sbuf = StringIO()
|
|
|
|
traceback.print_exc(file = sbuf)
|
2002-10-17 01:27:27 +02:00
|
|
|
return "Main program terminated with exception:\n" + \
|
|
|
|
sbuf.getvalue() + "\n" + \
|
|
|
|
s.getThreadDebugLog(threading.currentThread())
|
|
|
|
|
|
|
|
def mainException(s):
|
|
|
|
s._msg(s.getMainExceptionString())
|
2002-07-05 04:34:39 +02:00
|
|
|
|
2006-12-01 11:54:12 +01:00
|
|
|
def terminate(s, exitstatus = 0, errortitle = None, errormsg = None):
|
2002-07-05 04:34:39 +02:00
|
|
|
"""Called to terminate the application."""
|
2006-12-01 11:54:12 +01:00
|
|
|
if errormsg <> None:
|
|
|
|
if errortitle <> None:
|
|
|
|
sys.stderr.write('ERROR: %s\n\n%s\n'%(errortitle, errormsg))
|
|
|
|
else:
|
|
|
|
sys.stderr.write('%s\n' % errormsg)
|
2002-07-05 04:34:39 +02:00
|
|
|
sys.exit(exitstatus)
|
|
|
|
|
|
|
|
def threadExited(s, thread):
|
|
|
|
"""Called when a thread has exited normally. Many UIs will
|
|
|
|
just ignore this."""
|
2002-10-17 01:27:27 +02:00
|
|
|
s.delThreadDebugLog(thread)
|
2003-01-05 12:50:01 +01:00
|
|
|
s.unregisterthread(thread)
|
2002-07-05 04:34:39 +02:00
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
################################################## Other
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-07-03 08:50:31 +02:00
|
|
|
def sleep(s, sleepsecs):
|
|
|
|
"""This function does not actually output anything, but handles
|
|
|
|
the overall sleep, dealing with updates as necessary. It will,
|
|
|
|
however, call sleeping() which DOES output something.
|
|
|
|
|
|
|
|
Returns 0 if timeout expired, 1 if there is a request to cancel
|
|
|
|
the timer, and 2 if there is a request to abort the program."""
|
|
|
|
|
|
|
|
abortsleep = 0
|
|
|
|
while sleepsecs > 0 and not abortsleep:
|
|
|
|
abortsleep = s.sleeping(1, sleepsecs)
|
|
|
|
sleepsecs -= 1
|
|
|
|
s.sleeping(0, 0) # Done sleeping.
|
|
|
|
return abortsleep
|
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
def sleeping(s, sleepsecs, remainingsecs):
|
|
|
|
"""Sleep for sleepsecs, remainingsecs to go.
|
|
|
|
If sleepsecs is 0, indicates we're done sleeping.
|
|
|
|
|
|
|
|
Return 0 for normal sleep, or 1 to indicate a request
|
|
|
|
to sync immediately."""
|
2002-07-11 11:42:27 +02:00
|
|
|
s._msg("Next refresh in %d seconds" % remainingsecs)
|
2002-06-21 09:25:24 +02:00
|
|
|
if sleepsecs > 0:
|
|
|
|
time.sleep(sleepsecs)
|
|
|
|
return 0
|