2003-01-05 05:51:17 +01:00
|
|
|
# Curses-based interfaces
|
|
|
|
# Copyright (C) 2003 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.
|
2003-01-05 05:51:17 +01: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
|
2003-01-05 05:51:17 +01:00
|
|
|
|
|
|
|
from Blinkenlights import BlinkenBase
|
|
|
|
from UIBase import UIBase
|
|
|
|
from threading import *
|
2003-03-14 00:47:09 +01:00
|
|
|
import thread, time, sys, os, signal, time
|
2003-01-05 08:51:35 +01:00
|
|
|
from offlineimap import version, threadutil
|
2003-01-06 22:58:29 +01:00
|
|
|
from offlineimap.threadutil import MultiLock
|
2003-01-05 05:51:17 +01:00
|
|
|
|
|
|
|
import curses, curses.panel, curses.textpad, curses.wrapper
|
|
|
|
|
2003-01-07 03:18:09 +01:00
|
|
|
acctkeys = '1234567890abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-=;/.,'
|
|
|
|
|
2003-01-05 05:51:17 +01:00
|
|
|
class CursesUtil:
|
|
|
|
def __init__(self):
|
|
|
|
self.pairlock = Lock()
|
2003-01-06 22:58:29 +01:00
|
|
|
self.iolock = MultiLock()
|
2003-01-11 00:47:35 +01:00
|
|
|
self.start()
|
|
|
|
|
|
|
|
def initpairs(self):
|
|
|
|
self.pairlock.acquire()
|
|
|
|
try:
|
|
|
|
self.pairs = {self._getpairindex(curses.COLOR_WHITE,
|
|
|
|
curses.COLOR_BLACK): 0}
|
|
|
|
self.nextpair = 1
|
|
|
|
finally:
|
|
|
|
self.pairlock.release()
|
2003-01-06 22:58:29 +01:00
|
|
|
|
|
|
|
def lock(self):
|
|
|
|
self.iolock.acquire()
|
|
|
|
|
|
|
|
def unlock(self):
|
|
|
|
self.iolock.release()
|
|
|
|
|
|
|
|
def locked(self, target, *args, **kwargs):
|
|
|
|
"""Perform an operation with full locking."""
|
|
|
|
self.lock()
|
|
|
|
try:
|
|
|
|
apply(target, args, kwargs)
|
|
|
|
finally:
|
|
|
|
self.unlock()
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
def lockedstuff():
|
|
|
|
curses.panel.update_panels()
|
|
|
|
curses.doupdate()
|
|
|
|
self.locked(lockedstuff)
|
2003-01-05 05:51:17 +01:00
|
|
|
|
2003-01-05 08:51:35 +01:00
|
|
|
def isactive(self):
|
|
|
|
return hasattr(self, 'stdscr')
|
|
|
|
|
2003-01-05 05:51:17 +01:00
|
|
|
def _getpairindex(self, fg, bg):
|
|
|
|
return '%d/%d' % (fg,bg)
|
|
|
|
|
|
|
|
def getpair(self, fg, bg):
|
2003-01-11 00:47:35 +01:00
|
|
|
if not self.has_color:
|
|
|
|
return 0
|
2003-01-05 05:51:17 +01:00
|
|
|
pindex = self._getpairindex(fg, bg)
|
|
|
|
self.pairlock.acquire()
|
|
|
|
try:
|
|
|
|
if self.pairs.has_key(pindex):
|
|
|
|
return curses.color_pair(self.pairs[pindex])
|
|
|
|
else:
|
|
|
|
self.pairs[pindex] = self.nextpair
|
|
|
|
curses.init_pair(self.nextpair, fg, bg)
|
|
|
|
self.nextpair += 1
|
|
|
|
return curses.color_pair(self.nextpair - 1)
|
|
|
|
finally:
|
|
|
|
self.pairlock.release()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.stdscr = curses.initscr()
|
|
|
|
curses.noecho()
|
|
|
|
curses.cbreak()
|
|
|
|
self.stdscr.keypad(1)
|
|
|
|
try:
|
|
|
|
curses.start_color()
|
|
|
|
self.has_color = curses.has_colors()
|
|
|
|
except:
|
|
|
|
self.has_color = 0
|
|
|
|
|
2003-01-07 03:18:09 +01:00
|
|
|
self.oldcursor = None
|
|
|
|
try:
|
|
|
|
self.oldcursor = curses.curs_set(0)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2003-01-05 05:51:17 +01:00
|
|
|
self.stdscr.clear()
|
|
|
|
self.stdscr.refresh()
|
|
|
|
(self.height, self.width) = self.stdscr.getmaxyx()
|
2003-01-11 00:47:35 +01:00
|
|
|
self.initpairs()
|
2003-01-05 05:51:17 +01:00
|
|
|
|
|
|
|
def stop(self):
|
2003-01-05 08:51:35 +01:00
|
|
|
if not hasattr(self, 'stdscr'):
|
|
|
|
return
|
|
|
|
#self.stdscr.addstr(self.height - 1, 0, "\n",
|
|
|
|
# self.getpair(curses.COLOR_WHITE,
|
|
|
|
# curses.COLOR_BLACK))
|
2003-01-07 03:18:09 +01:00
|
|
|
if self.oldcursor != None:
|
|
|
|
curses.curs_set(self.oldcursor)
|
2003-01-05 08:51:35 +01:00
|
|
|
self.stdscr.refresh()
|
2003-01-05 05:51:17 +01:00
|
|
|
self.stdscr.keypad(0)
|
|
|
|
curses.nocbreak()
|
|
|
|
curses.echo()
|
|
|
|
curses.endwin()
|
|
|
|
del self.stdscr
|
|
|
|
|
2003-01-05 12:50:01 +01:00
|
|
|
def reset(self):
|
2003-01-05 08:51:35 +01:00
|
|
|
self.stop()
|
|
|
|
self.start()
|
|
|
|
|
2003-01-05 12:50:01 +01:00
|
|
|
class CursesAccountFrame:
|
2006-12-11 06:12:00 +01:00
|
|
|
def __init__(s, master, accountname, ui):
|
2003-01-05 12:50:01 +01:00
|
|
|
s.c = master
|
|
|
|
s.children = []
|
2003-01-06 00:07:58 +01:00
|
|
|
s.accountname = accountname
|
2006-12-11 06:12:00 +01:00
|
|
|
s.ui = ui
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2003-01-07 03:18:09 +01:00
|
|
|
def drawleadstr(s, secs = None):
|
|
|
|
if secs == None:
|
|
|
|
acctstr = '%s: [active] %13.13s: ' % (s.key, s.accountname)
|
|
|
|
else:
|
|
|
|
acctstr = '%s: [%3d:%02d] %13.13s: ' % (s.key,
|
|
|
|
secs / 60, secs % 60,
|
|
|
|
s.accountname)
|
|
|
|
s.c.locked(s.window.addstr, 0, 0, acctstr)
|
2003-01-06 00:07:58 +01:00
|
|
|
s.location = len(acctstr)
|
2003-01-07 03:18:09 +01:00
|
|
|
|
|
|
|
def setwindow(s, window, key):
|
|
|
|
s.window = window
|
|
|
|
s.key = key
|
|
|
|
s.drawleadstr()
|
2003-01-05 12:50:01 +01:00
|
|
|
for child in s.children:
|
2003-01-06 22:58:29 +01:00
|
|
|
child.update(window, 0, s.location)
|
2003-01-06 00:07:58 +01:00
|
|
|
s.location += 1
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def getnewthreadframe(s):
|
2006-12-11 06:12:00 +01:00
|
|
|
tf = CursesThreadFrame(s.c, s.ui, s.window, 0, s.location)
|
2003-01-06 00:07:58 +01:00
|
|
|
s.location += 1
|
2003-01-05 12:50:01 +01:00
|
|
|
s.children.append(tf)
|
|
|
|
return tf
|
|
|
|
|
2003-01-07 03:18:09 +01:00
|
|
|
def startsleep(s, sleepsecs):
|
|
|
|
s.sleeping_abort = 0
|
|
|
|
|
|
|
|
def sleeping(s, sleepsecs, remainingsecs):
|
|
|
|
if remainingsecs:
|
|
|
|
s.c.lock()
|
|
|
|
try:
|
|
|
|
s.drawleadstr(remainingsecs)
|
|
|
|
s.window.refresh()
|
|
|
|
finally:
|
|
|
|
s.c.unlock()
|
|
|
|
time.sleep(sleepsecs)
|
|
|
|
else:
|
|
|
|
s.c.lock()
|
|
|
|
try:
|
|
|
|
s.drawleadstr()
|
|
|
|
s.window.refresh()
|
|
|
|
finally:
|
|
|
|
s.c.unlock()
|
|
|
|
return s.sleeping_abort
|
|
|
|
|
|
|
|
def syncnow(s):
|
|
|
|
s.sleeping_abort = 1
|
|
|
|
|
2003-01-05 12:50:01 +01:00
|
|
|
class CursesThreadFrame:
|
2006-12-11 06:12:00 +01:00
|
|
|
def __init__(s, master, ui, window, y, x):
|
2003-01-05 08:51:35 +01:00
|
|
|
"""master should be a CursesUtil object."""
|
|
|
|
s.c = master
|
2006-12-11 06:12:00 +01:00
|
|
|
s.ui = ui
|
2003-01-05 12:50:01 +01:00
|
|
|
s.window = window
|
|
|
|
s.x = x
|
|
|
|
s.y = y
|
|
|
|
s.colors = []
|
2003-01-05 08:51:35 +01:00
|
|
|
bg = curses.COLOR_BLACK
|
|
|
|
s.colormap = {'black': s.c.getpair(curses.COLOR_BLACK, bg),
|
|
|
|
'gray': s.c.getpair(curses.COLOR_WHITE, bg),
|
|
|
|
'white': curses.A_BOLD | s.c.getpair(curses.COLOR_WHITE, bg),
|
|
|
|
'blue': s.c.getpair(curses.COLOR_BLUE, bg),
|
|
|
|
'red': s.c.getpair(curses.COLOR_RED, bg),
|
|
|
|
'purple': s.c.getpair(curses.COLOR_MAGENTA, bg),
|
|
|
|
'cyan': s.c.getpair(curses.COLOR_CYAN, bg),
|
|
|
|
'green': s.c.getpair(curses.COLOR_GREEN, bg),
|
|
|
|
'orange': s.c.getpair(curses.COLOR_YELLOW, bg),
|
|
|
|
'yellow': curses.A_BOLD | s.c.getpair(curses.COLOR_YELLOW, bg),
|
|
|
|
'pink': curses.A_BOLD | s.c.getpair(curses.COLOR_RED, bg)}
|
2003-01-06 00:07:58 +01:00
|
|
|
#s.setcolor('gray')
|
2003-01-06 22:58:29 +01:00
|
|
|
s.setcolor('black')
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def setcolor(self, color):
|
2003-01-05 08:51:35 +01:00
|
|
|
self.color = self.colormap[color]
|
2003-01-07 03:18:09 +01:00
|
|
|
self.colorname = color
|
2003-01-06 22:58:29 +01:00
|
|
|
self.display()
|
2003-01-06 00:07:58 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def display(self):
|
|
|
|
def lockedstuff():
|
2003-01-11 00:47:35 +01:00
|
|
|
if self.getcolor() == 'black':
|
|
|
|
self.window.addstr(self.y, self.x, ' ', self.color)
|
|
|
|
else:
|
2006-12-11 06:12:00 +01:00
|
|
|
self.window.addstr(self.y, self.x, self.ui.config.getdefault("ui.Curses.Blinkenlights", "statuschar", '.'), self.color)
|
2003-01-06 12:07:16 +01:00
|
|
|
self.c.stdscr.move(self.c.height - 1, self.c.width - 1)
|
2003-01-06 05:54:59 +01:00
|
|
|
self.window.refresh()
|
2003-01-06 22:58:29 +01:00
|
|
|
self.c.locked(lockedstuff)
|
2003-01-05 08:51:35 +01:00
|
|
|
|
|
|
|
def getcolor(self):
|
2003-01-07 03:18:09 +01:00
|
|
|
return self.colorname
|
|
|
|
|
|
|
|
def getcolorpair(self):
|
2003-01-05 08:51:35 +01:00
|
|
|
return self.color
|
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def update(self, window, y, x):
|
2003-01-06 00:07:58 +01:00
|
|
|
self.window = window
|
|
|
|
self.y = y
|
|
|
|
self.x = x
|
2003-01-06 22:58:29 +01:00
|
|
|
self.display()
|
2003-01-06 00:07:58 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def setthread(self, newthread):
|
|
|
|
self.setcolor('black')
|
2003-01-06 00:07:58 +01:00
|
|
|
#if newthread:
|
|
|
|
# self.setcolor('gray')
|
|
|
|
#else:
|
|
|
|
# self.setcolor('black')
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2003-01-05 08:51:35 +01:00
|
|
|
class InputHandler:
|
|
|
|
def __init__(s, util):
|
|
|
|
s.c = util
|
|
|
|
s.bgchar = None
|
2003-01-07 04:57:58 +01:00
|
|
|
s.inputlock = Lock()
|
2003-01-05 08:51:35 +01:00
|
|
|
s.lockheld = 0
|
2003-01-07 04:57:58 +01:00
|
|
|
s.statuslock = Lock()
|
2003-01-05 08:51:35 +01:00
|
|
|
s.startup = Event()
|
|
|
|
s.startthread()
|
|
|
|
|
|
|
|
def startthread(s):
|
|
|
|
s.thread = threadutil.ExitNotifyThread(target = s.bgreaderloop,
|
|
|
|
name = "InputHandler loop")
|
|
|
|
s.thread.setDaemon(1)
|
|
|
|
s.thread.start()
|
|
|
|
|
|
|
|
def bgreaderloop(s):
|
|
|
|
while 1:
|
|
|
|
s.statuslock.acquire()
|
|
|
|
if s.lockheld or s.bgchar == None:
|
|
|
|
s.statuslock.release()
|
|
|
|
s.startup.wait()
|
|
|
|
else:
|
|
|
|
s.statuslock.release()
|
|
|
|
ch = s.c.stdscr.getch()
|
|
|
|
s.statuslock.acquire()
|
|
|
|
try:
|
|
|
|
if s.lockheld or s.bgchar == None:
|
|
|
|
curses.ungetch(ch)
|
|
|
|
else:
|
|
|
|
s.bgchar(ch)
|
|
|
|
finally:
|
|
|
|
s.statuslock.release()
|
|
|
|
|
|
|
|
def set_bgchar(s, callback):
|
|
|
|
"""Sets a "background" character handler. If a key is pressed
|
|
|
|
while not doing anything else, it will be passed to this handler.
|
|
|
|
|
|
|
|
callback is a function taking a single arg -- the char pressed.
|
|
|
|
|
|
|
|
If callback is None, clears the request."""
|
|
|
|
s.statuslock.acquire()
|
|
|
|
oldhandler = s.bgchar
|
|
|
|
newhandler = callback
|
|
|
|
s.bgchar = callback
|
|
|
|
|
|
|
|
if oldhandler and not newhandler:
|
|
|
|
pass
|
|
|
|
if newhandler and not oldhandler:
|
|
|
|
s.startup.set()
|
|
|
|
|
|
|
|
s.statuslock.release()
|
|
|
|
|
|
|
|
def input_acquire(s):
|
|
|
|
"""Call this method when you want exclusive input control.
|
|
|
|
Make sure to call input_release afterwards!
|
|
|
|
"""
|
|
|
|
|
|
|
|
s.inputlock.acquire()
|
|
|
|
s.statuslock.acquire()
|
|
|
|
s.lockheld = 1
|
|
|
|
s.statuslock.release()
|
|
|
|
|
|
|
|
def input_release(s):
|
|
|
|
"""Call this method when you are done getting input."""
|
|
|
|
s.statuslock.acquire()
|
|
|
|
s.lockheld = 0
|
|
|
|
s.statuslock.release()
|
|
|
|
s.inputlock.release()
|
|
|
|
s.startup.set()
|
2003-01-05 05:51:17 +01:00
|
|
|
|
2003-01-05 08:51:35 +01:00
|
|
|
class Blinkenlights(BlinkenBase, UIBase):
|
|
|
|
def init_banner(s):
|
2003-01-05 12:50:01 +01:00
|
|
|
s.af = {}
|
2003-01-07 04:57:58 +01:00
|
|
|
s.aflock = Lock()
|
2003-01-05 08:51:35 +01:00
|
|
|
s.c = CursesUtil()
|
|
|
|
s.text = []
|
2003-01-05 12:50:01 +01:00
|
|
|
BlinkenBase.init_banner(s)
|
2003-01-06 22:58:29 +01:00
|
|
|
s.setupwindows()
|
2003-01-05 08:51:35 +01:00
|
|
|
s.inputhandler = InputHandler(s.c)
|
2003-01-06 05:54:59 +01:00
|
|
|
s.gettf().setcolor('red')
|
2003-01-05 08:51:35 +01:00
|
|
|
s._msg(version.banner)
|
|
|
|
s.inputhandler.set_bgchar(s.keypress)
|
2003-03-14 00:47:09 +01:00
|
|
|
signal.signal(signal.SIGWINCH, s.resizehandler)
|
|
|
|
s.resizelock = Lock()
|
|
|
|
s.resizecount = 0
|
|
|
|
|
|
|
|
def resizehandler(s, signum, frame):
|
|
|
|
s.resizeterm()
|
|
|
|
|
|
|
|
def resizeterm(s, dosleep = 1):
|
|
|
|
if not s.resizelock.acquire(0):
|
|
|
|
s.resizecount += 1
|
|
|
|
return
|
|
|
|
signal.signal(signal.SIGWINCH, signal.SIG_IGN)
|
|
|
|
s.aflock.acquire()
|
|
|
|
s.c.lock()
|
|
|
|
s.resizecount += 1
|
|
|
|
while s.resizecount:
|
|
|
|
s.c.reset()
|
|
|
|
s.setupwindows()
|
|
|
|
s.resizecount -= 1
|
|
|
|
s.c.unlock()
|
|
|
|
s.aflock.release()
|
|
|
|
s.resizelock.release()
|
|
|
|
signal.signal(signal.SIGWINCH, s.resizehandler)
|
|
|
|
if dosleep:
|
|
|
|
time.sleep(1)
|
|
|
|
s.resizeterm(0)
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2003-01-07 04:15:22 +01:00
|
|
|
def isusable(s):
|
|
|
|
# Not a terminal? Can't use curses.
|
|
|
|
if not sys.stdout.isatty() and sys.stdin.isatty():
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# No TERM specified? Can't use curses.
|
|
|
|
try:
|
|
|
|
if not len(os.environ['TERM']):
|
|
|
|
return 0
|
|
|
|
except: return 0
|
|
|
|
|
|
|
|
# ncurses doesn't want to start? Can't use curses.
|
|
|
|
# This test is nasty because initscr() actually EXITS on error.
|
|
|
|
# grr.
|
|
|
|
|
|
|
|
pid = os.fork()
|
|
|
|
if pid:
|
|
|
|
# parent
|
|
|
|
return not os.WEXITSTATUS(os.waitpid(pid, 0)[1])
|
|
|
|
else:
|
|
|
|
# child
|
|
|
|
curses.initscr()
|
|
|
|
curses.endwin()
|
|
|
|
# If we didn't die by here, indicate success.
|
|
|
|
sys.exit(0)
|
|
|
|
|
2003-01-05 08:51:35 +01:00
|
|
|
def keypress(s, key):
|
2003-01-07 03:18:09 +01:00
|
|
|
if key > 255:
|
|
|
|
return
|
|
|
|
|
|
|
|
if chr(key) == 'q':
|
|
|
|
# Request to quit.
|
|
|
|
s.terminate()
|
|
|
|
|
|
|
|
try:
|
|
|
|
index = acctkeys.index(chr(key))
|
|
|
|
except ValueError:
|
|
|
|
# Key not a valid one: exit.
|
|
|
|
return
|
|
|
|
|
2004-07-26 22:37:45 +02:00
|
|
|
if index >= len(s.hotkeys):
|
2003-01-07 03:18:09 +01:00
|
|
|
# Not in our list of valid hotkeys.
|
|
|
|
return
|
|
|
|
|
|
|
|
# Trying to end sleep somewhere.
|
|
|
|
|
|
|
|
s.getaccountframe(s.hotkeys[index]).syncnow()
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2003-01-05 09:07:00 +01:00
|
|
|
def getpass(s, accountname, config, errmsg = None):
|
|
|
|
s.inputhandler.input_acquire()
|
2003-01-06 22:58:29 +01:00
|
|
|
|
|
|
|
# See comment on _msg for info on why both locks are obtained.
|
|
|
|
|
|
|
|
s.tflock.acquire()
|
|
|
|
s.c.lock()
|
2003-01-05 09:07:00 +01:00
|
|
|
try:
|
2003-01-06 22:58:29 +01:00
|
|
|
s.gettf().setcolor('white')
|
2003-01-07 03:18:09 +01:00
|
|
|
s._addline(" *** Input Required", s.gettf().getcolorpair())
|
2003-01-06 22:58:29 +01:00
|
|
|
s._addline(" *** Please enter password for account %s: " % accountname,
|
2003-01-07 03:18:09 +01:00
|
|
|
s.gettf().getcolorpair())
|
2003-01-05 09:07:00 +01:00
|
|
|
s.logwindow.refresh()
|
|
|
|
password = s.logwindow.getstr()
|
|
|
|
finally:
|
2003-01-06 22:58:29 +01:00
|
|
|
s.tflock.release()
|
|
|
|
s.c.unlock()
|
2003-01-05 09:07:00 +01:00
|
|
|
s.inputhandler.input_release()
|
|
|
|
return password
|
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def setupwindows(s):
|
|
|
|
s.c.lock()
|
2003-01-05 12:50:01 +01:00
|
|
|
try:
|
|
|
|
s.bannerwindow = curses.newwin(1, s.c.width, 0, 0)
|
|
|
|
s.setupwindow_drawbanner()
|
|
|
|
s.logheight = s.c.height - 1 - len(s.af.keys())
|
|
|
|
s.logwindow = curses.newwin(s.logheight, s.c.width, 1, 0)
|
|
|
|
s.logwindow.idlok(1)
|
|
|
|
s.logwindow.scrollok(1)
|
2003-01-07 03:18:09 +01:00
|
|
|
s.logwindow.move(s.logheight - 1, 0)
|
2003-01-05 12:50:01 +01:00
|
|
|
s.setupwindow_drawlog()
|
|
|
|
accounts = s.af.keys()
|
|
|
|
accounts.sort()
|
|
|
|
accounts.reverse()
|
|
|
|
|
|
|
|
pos = s.c.height - 1
|
2003-01-07 03:18:09 +01:00
|
|
|
index = 0
|
|
|
|
s.hotkeys = []
|
2003-01-05 12:50:01 +01:00
|
|
|
for account in accounts:
|
|
|
|
accountwindow = curses.newwin(1, s.c.width, pos, 0)
|
2003-01-07 03:18:09 +01:00
|
|
|
s.af[account].setwindow(accountwindow, acctkeys[index])
|
|
|
|
s.hotkeys.append(account)
|
|
|
|
index += 1
|
2003-01-05 12:50:01 +01:00
|
|
|
pos -= 1
|
|
|
|
|
|
|
|
curses.doupdate()
|
|
|
|
finally:
|
2003-01-06 22:58:29 +01:00
|
|
|
s.c.unlock()
|
2003-01-05 12:50:01 +01:00
|
|
|
|
|
|
|
def setupwindow_drawbanner(s):
|
2003-01-11 00:47:35 +01:00
|
|
|
if s.c.has_color:
|
|
|
|
color = s.c.getpair(curses.COLOR_WHITE, curses.COLOR_BLUE) | \
|
|
|
|
curses.A_BOLD
|
|
|
|
else:
|
|
|
|
color = curses.A_REVERSE
|
|
|
|
s.bannerwindow.bkgd(' ', color) # Fill background with that color
|
2003-01-05 08:51:35 +01:00
|
|
|
s.bannerwindow.addstr("%s %s" % (version.productname,
|
|
|
|
version.versionstr))
|
|
|
|
s.bannerwindow.addstr(0, s.bannerwindow.getmaxyx()[1] - len(version.copyright) - 1,
|
|
|
|
version.copyright)
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2003-01-05 08:51:35 +01:00
|
|
|
s.bannerwindow.noutrefresh()
|
|
|
|
|
2003-01-05 12:50:01 +01:00
|
|
|
def setupwindow_drawlog(s):
|
2003-01-11 00:47:35 +01:00
|
|
|
if s.c.has_color:
|
|
|
|
color = s.c.getpair(curses.COLOR_WHITE, curses.COLOR_BLACK)
|
|
|
|
else:
|
|
|
|
color = curses.A_NORMAL
|
|
|
|
s.logwindow.bkgd(' ', color)
|
2003-01-05 12:50:01 +01:00
|
|
|
for line, color in s.text:
|
2003-01-07 03:18:09 +01:00
|
|
|
s.logwindow.addstr("\n" + line, color)
|
2003-01-06 05:54:59 +01:00
|
|
|
s.logwindow.noutrefresh()
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2003-01-07 03:18:09 +01:00
|
|
|
def getaccountframe(s, accountname = None):
|
|
|
|
if accountname == None:
|
|
|
|
accountname = s.getthreadaccount()
|
2003-01-05 12:50:01 +01:00
|
|
|
s.aflock.acquire()
|
2003-01-05 09:07:00 +01:00
|
|
|
try:
|
2003-01-05 12:50:01 +01:00
|
|
|
if accountname in s.af:
|
|
|
|
return s.af[accountname]
|
|
|
|
|
|
|
|
# New one.
|
2006-12-11 06:12:00 +01:00
|
|
|
s.af[accountname] = CursesAccountFrame(s.c, accountname, s)
|
2003-01-06 22:58:29 +01:00
|
|
|
s.c.lock()
|
|
|
|
try:
|
|
|
|
s.c.reset()
|
|
|
|
s.setupwindows()
|
|
|
|
finally:
|
|
|
|
s.c.unlock()
|
2003-01-05 09:07:00 +01:00
|
|
|
finally:
|
2003-01-05 12:50:01 +01:00
|
|
|
s.aflock.release()
|
|
|
|
return s.af[accountname]
|
2003-01-05 08:51:35 +01:00
|
|
|
|
|
|
|
|
2003-06-02 21:06:18 +02:00
|
|
|
def _display(s, msg, color = None):
|
2003-01-05 08:51:35 +01:00
|
|
|
if "\n" in msg:
|
|
|
|
for thisline in msg.split("\n"):
|
|
|
|
s._msg(thisline)
|
|
|
|
return
|
2003-01-06 22:58:29 +01:00
|
|
|
|
|
|
|
# We must acquire both locks. Otherwise, deadlock can result.
|
|
|
|
# This can happen if one thread calls _msg (locking curses, then
|
|
|
|
# tf) and another tries to set the color (locking tf, then curses)
|
|
|
|
#
|
|
|
|
# By locking both up-front here, in this order, we prevent deadlock.
|
|
|
|
|
|
|
|
s.tflock.acquire()
|
|
|
|
s.c.lock()
|
2003-01-05 08:51:35 +01:00
|
|
|
try:
|
|
|
|
if not s.c.isactive():
|
|
|
|
# For dumping out exceptions and stuff.
|
|
|
|
print msg
|
|
|
|
return
|
2003-01-05 09:07:00 +01:00
|
|
|
if color:
|
2003-01-06 22:58:29 +01:00
|
|
|
s.gettf().setcolor(color)
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
elif s.gettf().getcolor() == 'black':
|
|
|
|
s.gettf().setcolor('gray')
|
2003-01-07 03:18:09 +01:00
|
|
|
s._addline(msg, s.gettf().getcolorpair())
|
2003-01-05 08:51:35 +01:00
|
|
|
s.logwindow.refresh()
|
|
|
|
finally:
|
2003-01-06 22:58:29 +01:00
|
|
|
s.c.unlock()
|
|
|
|
s.tflock.release()
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def _addline(s, msg, color):
|
|
|
|
s.c.lock()
|
|
|
|
try:
|
2003-01-07 03:18:09 +01:00
|
|
|
s.logwindow.addstr("\n" + msg, color)
|
2003-01-06 22:58:29 +01:00
|
|
|
s.text.append((msg, color))
|
|
|
|
while len(s.text) > s.logheight:
|
|
|
|
s.text = s.text[1:]
|
|
|
|
finally:
|
|
|
|
s.c.unlock()
|
2003-01-05 09:07:00 +01:00
|
|
|
|
2006-12-01 11:54:12 +01:00
|
|
|
def terminate(s, exitstatus = 0, errortitle = None, errormsg = None):
|
2003-01-05 08:51:35 +01:00
|
|
|
s.c.stop()
|
2006-12-01 11:54:12 +01:00
|
|
|
UIBase.terminate(s, exitstatus = exitstatus, errortitle = errortitle, errormsg = errormsg)
|
2003-01-05 08:51:35 +01:00
|
|
|
|
|
|
|
def threadException(s, thread):
|
|
|
|
s.c.stop()
|
|
|
|
UIBase.threadException(s, thread)
|
|
|
|
|
|
|
|
def mainException(s):
|
|
|
|
s.c.stop()
|
2003-01-05 09:07:00 +01:00
|
|
|
UIBase.mainException(s)
|
2003-01-07 03:18:09 +01:00
|
|
|
|
|
|
|
def sleep(s, sleepsecs):
|
|
|
|
s.gettf().setcolor('red')
|
|
|
|
s._msg("Next sync in %d:%02d" % (sleepsecs / 60, sleepsecs % 60))
|
|
|
|
BlinkenBase.sleep(s, sleepsecs)
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2003-01-05 05:51:17 +01:00
|
|
|
if __name__ == '__main__':
|
2003-01-05 08:51:35 +01:00
|
|
|
x = Blinkenlights(None)
|
|
|
|
x.init_banner()
|
|
|
|
import time
|
2003-01-11 00:47:35 +01:00
|
|
|
time.sleep(5)
|
2003-01-05 08:51:35 +01:00
|
|
|
x.c.stop()
|
2003-01-05 05:51:17 +01:00
|
|
|
fgs = {'black': curses.COLOR_BLACK, 'red': curses.COLOR_RED,
|
|
|
|
'green': curses.COLOR_GREEN, 'yellow': curses.COLOR_YELLOW,
|
|
|
|
'blue': curses.COLOR_BLUE, 'magenta': curses.COLOR_MAGENTA,
|
|
|
|
'cyan': curses.COLOR_CYAN, 'white': curses.COLOR_WHITE}
|
|
|
|
|
|
|
|
x = CursesUtil()
|
|
|
|
win1 = curses.newwin(x.height, x.width / 4 - 1, 0, 0)
|
|
|
|
win1.addstr("Black/normal\n")
|
|
|
|
for name, fg in fgs.items():
|
|
|
|
win1.addstr("%s\n" % name, x.getpair(fg, curses.COLOR_BLACK))
|
|
|
|
win2 = curses.newwin(x.height, x.width / 4 - 1, 0, win1.getmaxyx()[1])
|
|
|
|
win2.addstr("Blue/normal\n")
|
|
|
|
for name, fg in fgs.items():
|
|
|
|
win2.addstr("%s\n" % name, x.getpair(fg, curses.COLOR_BLUE))
|
|
|
|
win3 = curses.newwin(x.height, x.width / 4 - 1, 0, win1.getmaxyx()[1] +
|
|
|
|
win2.getmaxyx()[1])
|
|
|
|
win3.addstr("Black/bright\n")
|
|
|
|
for name, fg in fgs.items():
|
|
|
|
win3.addstr("%s\n" % name, x.getpair(fg, curses.COLOR_BLACK) | \
|
|
|
|
curses.A_BOLD)
|
|
|
|
win4 = curses.newwin(x.height, x.width / 4 - 1, 0, win1.getmaxyx()[1] * 3)
|
|
|
|
win4.addstr("Blue/bright\n")
|
|
|
|
for name, fg in fgs.items():
|
|
|
|
win4.addstr("%s\n" % name, x.getpair(fg, curses.COLOR_BLUE) | \
|
|
|
|
curses.A_BOLD)
|
|
|
|
|
|
|
|
|
|
|
|
win1.refresh()
|
|
|
|
win2.refresh()
|
|
|
|
win3.refresh()
|
|
|
|
win4.refresh()
|
|
|
|
x.stdscr.refresh()
|
|
|
|
import time
|
2003-01-11 00:47:35 +01:00
|
|
|
time.sleep(5)
|
2003-01-05 05:51:17 +01:00
|
|
|
x.stop()
|
|
|
|
print x.has_color
|
|
|
|
print x.height
|
|
|
|
print x.width
|
|
|
|
|