2002-07-11 07:38:14 +02:00
|
|
|
# Tk UI
|
|
|
|
# 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
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2002-07-11 08:53:09 +02:00
|
|
|
from Tkinter import *
|
|
|
|
from threading import *
|
2002-07-11 12:02:36 +02:00
|
|
|
import thread, traceback, time
|
2002-07-11 09:51:24 +02:00
|
|
|
from StringIO import StringIO
|
|
|
|
from ScrolledText import ScrolledText
|
2002-07-11 12:02:36 +02:00
|
|
|
from offlineimap import threadutil, version
|
2002-07-11 08:53:09 +02:00
|
|
|
from Queue import Queue
|
2002-07-11 09:15:57 +02:00
|
|
|
from UIBase import UIBase
|
2002-07-11 08:53:09 +02:00
|
|
|
|
|
|
|
class PasswordDialog:
|
|
|
|
def __init__(self, accountname, config, master=None):
|
|
|
|
self.top = Toplevel(master)
|
2002-07-11 12:02:36 +02:00
|
|
|
self.top.title(version.productname + " Password Entry")
|
2002-07-11 09:15:57 +02:00
|
|
|
self.label = Label(self.top,
|
2002-07-11 08:53:09 +02:00
|
|
|
text = "%s: Enter password for %s on %s: " % \
|
|
|
|
(accountname, config.get(accountname, "remoteuser"),
|
|
|
|
config.get(accountname, "remotehost")))
|
|
|
|
self.label.pack()
|
|
|
|
|
|
|
|
self.entry = Entry(self.top, show='*')
|
2002-07-11 12:02:36 +02:00
|
|
|
self.entry.bind("<Return>", self.ok)
|
2002-07-11 08:53:09 +02:00
|
|
|
self.entry.pack()
|
2002-07-11 12:02:36 +02:00
|
|
|
self.entry.focus_force()
|
2002-07-11 08:53:09 +02:00
|
|
|
|
|
|
|
self.button = Button(self.top, text = "OK", command=self.ok)
|
|
|
|
self.button.pack()
|
|
|
|
|
2002-07-11 12:02:36 +02:00
|
|
|
self.entry.focus_force()
|
2002-07-11 08:53:09 +02:00
|
|
|
self.top.wait_window(self.label)
|
|
|
|
|
2002-07-11 12:02:36 +02:00
|
|
|
def ok(self, args = None):
|
2002-07-11 08:53:09 +02:00
|
|
|
self.password = self.entry.get()
|
|
|
|
self.top.destroy()
|
|
|
|
|
|
|
|
def getpassword(self):
|
|
|
|
return self.password
|
|
|
|
|
2002-07-11 09:51:24 +02:00
|
|
|
class TextOKDialog:
|
2002-07-11 15:25:53 +02:00
|
|
|
def __init__(self, title, message, blocking = 1, master = None):
|
|
|
|
if not master:
|
|
|
|
self.top = Tk()
|
|
|
|
else:
|
|
|
|
self.top = Toplevel(master)
|
2002-07-11 12:02:36 +02:00
|
|
|
self.top.title(title)
|
2002-07-11 09:51:24 +02:00
|
|
|
self.text = ScrolledText(self.top, font = "Courier 10")
|
|
|
|
self.text.pack()
|
|
|
|
self.text.insert(END, message)
|
|
|
|
self.text['state'] = DISABLED
|
|
|
|
self.button = Button(self.top, text = "OK", command=self.ok)
|
|
|
|
self.button.pack()
|
|
|
|
|
2002-07-11 15:25:53 +02:00
|
|
|
if blocking:
|
|
|
|
self.top.wait_window(self.button)
|
2002-07-11 09:51:24 +02:00
|
|
|
|
|
|
|
def ok(self):
|
|
|
|
self.top.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-07-11 08:53:09 +02:00
|
|
|
class ThreadFrame(Frame):
|
|
|
|
def __init__(self, master=None):
|
2002-07-11 12:55:32 +02:00
|
|
|
self.threadextraframe = None
|
2002-07-11 08:53:09 +02:00
|
|
|
self.thread = currentThread()
|
|
|
|
self.threadid = thread.get_ident()
|
2002-07-11 12:02:36 +02:00
|
|
|
Frame.__init__(self, master, relief = RIDGE, borderwidth = 2)
|
|
|
|
self.pack(fill = 'x')
|
2002-07-11 12:55:32 +02:00
|
|
|
self.threadlabel = Label(self, foreground = '#FF0000',
|
|
|
|
text ="Thread %d (%s)" % (self.threadid,
|
|
|
|
self.thread.getName()))
|
|
|
|
self.threadlabel.pack()
|
|
|
|
self.setthread(currentThread())
|
2002-07-11 08:53:09 +02:00
|
|
|
|
|
|
|
self.account = "Unknown"
|
|
|
|
self.mailbox = "Unknown"
|
2002-07-11 12:55:32 +02:00
|
|
|
self.loclabel = Label(self,
|
2002-07-11 09:15:57 +02:00
|
|
|
text = "Account/mailbox information unknown")
|
2002-07-11 12:02:36 +02:00
|
|
|
#self.loclabel.pack()
|
2002-07-11 08:53:09 +02:00
|
|
|
|
|
|
|
self.updateloclabel()
|
|
|
|
|
2002-07-11 12:55:32 +02:00
|
|
|
self.message = Label(self, text="Messages will appear here.\n",
|
|
|
|
foreground = '#0000FF')
|
|
|
|
self.message.pack(fill = 'x')
|
|
|
|
|
|
|
|
def setthread(self, newthread):
|
|
|
|
if newthread:
|
|
|
|
self.threadlabel['text'] = newthread.getName()
|
|
|
|
else:
|
|
|
|
self.threadlabel['text'] = "No thread"
|
|
|
|
self.destroythreadextraframe()
|
|
|
|
|
|
|
|
def destroythreadextraframe(self):
|
|
|
|
if self.threadextraframe:
|
|
|
|
self.threadextraframe.destroy()
|
|
|
|
self.threadextraframe = None
|
|
|
|
|
|
|
|
def getthreadextraframe(self):
|
|
|
|
if self.threadextraframe:
|
|
|
|
return self.threadextraframe
|
2002-07-11 15:25:53 +02:00
|
|
|
self.threadextraframe = Frame(self)
|
2002-07-11 12:55:32 +02:00
|
|
|
self.threadextraframe.pack(fill = 'x')
|
2002-07-11 15:25:53 +02:00
|
|
|
return self.threadextraframe
|
2002-07-11 08:53:09 +02:00
|
|
|
|
|
|
|
def setaccount(self, account):
|
|
|
|
self.account = account
|
|
|
|
self.mailbox = "Unknown"
|
|
|
|
self.updateloclabel()
|
|
|
|
|
|
|
|
def setmailbox(self, mailbox):
|
|
|
|
self.mailbox = mailbox
|
|
|
|
self.updateloclabel()
|
|
|
|
|
|
|
|
def updateloclabel(self):
|
|
|
|
self.loclabel['text'] = "Processing %s: %s" % (self.account,
|
|
|
|
self.mailbox)
|
|
|
|
|
|
|
|
def appendmessage(self, newtext):
|
|
|
|
self.message['text'] += "\n" + newtext
|
|
|
|
|
|
|
|
def setmessage(self, newtext):
|
|
|
|
self.message['text'] = newtext
|
|
|
|
|
|
|
|
|
|
|
|
class TkUI(UIBase):
|
2002-07-11 09:51:24 +02:00
|
|
|
def __init__(self, verbose = 0):
|
|
|
|
self.verbose = verbose
|
2002-07-12 03:58:51 +02:00
|
|
|
|
|
|
|
def isusable(s):
|
|
|
|
try:
|
|
|
|
Tk().destroy()
|
|
|
|
return 1
|
|
|
|
except TclError:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def _createTopWindow(self):
|
2002-07-11 08:53:09 +02:00
|
|
|
self.top = Tk()
|
2002-07-11 12:02:36 +02:00
|
|
|
self.top.title(version.productname + " " + version.versionstr)
|
2002-07-11 08:53:09 +02:00
|
|
|
self.threadframes = {}
|
2002-07-11 11:14:29 +02:00
|
|
|
self.availablethreadframes = []
|
|
|
|
self.tflock = Lock()
|
2002-07-11 12:02:36 +02:00
|
|
|
self.notdeleted = 1
|
2002-07-11 09:15:57 +02:00
|
|
|
|
2002-07-12 03:58:51 +02:00
|
|
|
t = threadutil.ExitNotifyThread(target = self._runmainloop,
|
2002-07-11 09:15:57 +02:00
|
|
|
name = "Tk Mainloop")
|
|
|
|
t.setDaemon(1)
|
|
|
|
t.start()
|
2002-07-11 12:02:36 +02:00
|
|
|
|
|
|
|
t = threadutil.ExitNotifyThread(target = self.idlevacuum,
|
|
|
|
name = "Tk idle vacuum")
|
|
|
|
t.setDaemon(1)
|
|
|
|
t.start()
|
|
|
|
|
2002-07-12 03:58:51 +02:00
|
|
|
def _runmainloop(s):
|
2002-07-11 12:02:36 +02:00
|
|
|
s.top.mainloop()
|
|
|
|
s.notdeleted = 0
|
|
|
|
|
2002-07-11 08:53:09 +02:00
|
|
|
def getpass(s, accountname, config):
|
2002-07-11 09:51:24 +02:00
|
|
|
pd = PasswordDialog(accountname, config)
|
2002-07-11 08:53:09 +02:00
|
|
|
return pd.getpassword()
|
|
|
|
|
|
|
|
def gettf(s):
|
|
|
|
threadid = thread.get_ident()
|
2002-07-11 11:14:29 +02:00
|
|
|
s.tflock.acquire()
|
|
|
|
try:
|
|
|
|
if threadid in s.threadframes:
|
|
|
|
return s.threadframes[threadid]
|
|
|
|
if len(s.availablethreadframes):
|
|
|
|
tf = s.availablethreadframes.pop(0)
|
2002-07-11 12:55:32 +02:00
|
|
|
tf.setthread(currentThread())
|
2002-07-11 11:14:29 +02:00
|
|
|
else:
|
|
|
|
tf = ThreadFrame(s.top)
|
|
|
|
s.threadframes[threadid] = tf
|
|
|
|
return tf
|
|
|
|
finally:
|
|
|
|
s.tflock.release()
|
2002-07-11 08:53:09 +02:00
|
|
|
|
|
|
|
def _msg(s, msg):
|
|
|
|
s.gettf().setmessage(msg)
|
|
|
|
|
|
|
|
def threadExited(s, thread):
|
2002-07-11 09:51:24 +02:00
|
|
|
threadid = thread.threadid
|
2002-07-11 11:14:29 +02:00
|
|
|
s.tflock.acquire()
|
2002-07-11 09:15:57 +02:00
|
|
|
if threadid in s.threadframes:
|
|
|
|
tf = s.threadframes[threadid]
|
2002-07-11 12:55:32 +02:00
|
|
|
tf.setthread(None)
|
2002-07-11 11:14:29 +02:00
|
|
|
tf.setaccount("Unknown")
|
|
|
|
tf.setmessage("Idle")
|
|
|
|
s.availablethreadframes.append(tf)
|
2002-07-11 09:51:24 +02:00
|
|
|
del s.threadframes[threadid]
|
2002-07-11 11:14:29 +02:00
|
|
|
s.tflock.release()
|
2002-07-11 12:02:36 +02:00
|
|
|
|
|
|
|
def idlevacuum(s):
|
|
|
|
while s.notdeleted:
|
|
|
|
time.sleep(10)
|
|
|
|
s.tflock.acquire()
|
|
|
|
while len(s.availablethreadframes):
|
|
|
|
tf = s.availablethreadframes.pop()
|
|
|
|
tf.destroy()
|
|
|
|
s.tflock.release()
|
2002-07-11 09:51:24 +02:00
|
|
|
|
|
|
|
def threadException(s, thread):
|
|
|
|
msg = "Thread '%s' terminated with exception:\n%s" % \
|
|
|
|
(thread.getName(), thread.getExitStackTrace())
|
|
|
|
print msg
|
2002-07-11 08:53:09 +02:00
|
|
|
|
2002-07-11 09:51:24 +02:00
|
|
|
s.top.destroy()
|
2002-07-11 15:25:53 +02:00
|
|
|
s.top = None
|
2002-07-11 09:51:24 +02:00
|
|
|
TextOKDialog("Thread Exception", msg)
|
|
|
|
s.terminate(100)
|
|
|
|
|
|
|
|
def mainException(s):
|
|
|
|
sbuf = StringIO()
|
|
|
|
traceback.print_exc(file = sbuf)
|
|
|
|
msg = "Main program terminated with exception:\n" + sbuf.getvalue()
|
|
|
|
print msg
|
|
|
|
|
|
|
|
s.top.destroy()
|
2002-07-11 15:25:53 +02:00
|
|
|
s.top = None
|
2002-07-11 09:51:24 +02:00
|
|
|
TextOKDialog("Main Program Exception", msg)
|
|
|
|
|
2002-07-11 15:25:53 +02:00
|
|
|
def init_banner(s):
|
2002-07-12 03:58:51 +02:00
|
|
|
s._createTopWindow()
|
2002-07-11 15:25:53 +02:00
|
|
|
s._msg(version.productname + " " + version.versionstr + ", " +\
|
|
|
|
version.copyright)
|
|
|
|
tf = s.gettf().getthreadextraframe()
|
|
|
|
|
|
|
|
def showlicense():
|
|
|
|
TextOKDialog(version.productname + " License",
|
|
|
|
version.bigcopyright + "\n" +
|
|
|
|
version.homepage + "\n\n" + version.license,
|
|
|
|
blocking = 0, master = tf)
|
|
|
|
b = Button(tf, text = "About", command = showlicense)
|
|
|
|
b.pack(side = LEFT)
|
|
|
|
|
|
|
|
b = Button(tf, text = "Exit", command = s.terminate)
|
|
|
|
b.pack(side = RIGHT)
|
|
|
|
|
2002-07-11 11:42:27 +02:00
|
|
|
def deletingmessages(s, uidlist, destlist):
|
|
|
|
ds = s.folderlist(destlist)
|
|
|
|
s._msg("Deleting %d messages in %s" % (len(uidlist), ds))
|
2002-07-11 09:51:24 +02:00
|
|
|
|
2002-07-11 15:25:53 +02:00
|
|
|
def _sleep_cancel(s, args = None):
|
|
|
|
s.sleeping_abort = 1
|
|
|
|
|
|
|
|
def sleep(s, sleepsecs):
|
|
|
|
s.sleeping_abort = 0
|
|
|
|
tf = s.gettf().getthreadextraframe()
|
|
|
|
|
|
|
|
sleepbut = Button(tf, text = 'Sync immediately',
|
|
|
|
command = s._sleep_cancel)
|
|
|
|
sleepbut.pack()
|
|
|
|
UIBase.sleep(s, sleepsecs)
|
|
|
|
|
|
|
|
def sleeping(s, sleepsecs, remainingsecs):
|
|
|
|
if remainingsecs:
|
|
|
|
s._msg("Next sync in %d:%02d" % (remainingsecs / 60,
|
|
|
|
remainingsecs % 60))
|
|
|
|
else:
|
|
|
|
s._msg("Wait done; synchronizing now.")
|
|
|
|
s.gettf().destroythreadextraframe()
|
|
|
|
time.sleep(sleepsecs)
|
|
|
|
return s.sleeping_abort
|
|
|
|
|
2002-07-11 09:51:24 +02:00
|
|
|
################################################## Copied from TTY
|
|
|
|
|
|
|
|
def syncingmessages(s, sr, sf, dr, df):
|
|
|
|
if s.verbose:
|
|
|
|
UIBase.syncingmessages(s, sr, sf, dr, df)
|
|
|
|
|
|
|
|
def loadmessagelist(s, repos, folder):
|
|
|
|
if s.verbose:
|
|
|
|
UIBase.syncingmessages(s, repos, folder)
|
2002-07-11 08:53:09 +02:00
|
|
|
|
2002-07-11 09:51:24 +02:00
|
|
|
def messagelistloaded(s, repos, folder, count):
|
|
|
|
if s.verbose:
|
|
|
|
UIBase.messagelistloaded(s, repos, folder, count)
|
|
|
|
|