summaryrefslogtreecommitdiffstats
path: root/Demo/tkinter/guido/mbox.py
blob: 6d7a4106ccd809535a6c39a7f3c5b85d5a19d116 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#! /usr/bin/env python

# Scan MH folder, display results in window

import os
import sys
import regex
import getopt
import string
import mhlib

from Tkinter import *

from dialog import dialog

mailbox = os.environ['HOME'] + '/Mail'

def main():
	global root, tk, top, mid, bot
	global folderbox, foldermenu, scanbox, scanmenu, viewer
	global folder, seq
	global mh, mhf

	# Parse command line options

	folder = 'inbox'
	seq = 'all'
	try:
		opts, args = getopt.getopt(sys.argv[1:], '')
	except getopt.error, msg:
		print msg
		sys.exit(2)
	for arg in args:
		if arg[:1] == '+':
			folder = arg[1:]
		else:
			seq = arg

	# Initialize MH

	mh = mhlib.MH()
	mhf = mh.openfolder(folder)

	# Build widget hierarchy

	root = Tk()
	tk = root.tk

	top = Frame(root)
	top.pack({'expand': 1, 'fill': 'both'})

	# Build right part: folder list

	right = Frame(top)
	right.pack({'fill': 'y', 'side': 'right'})

	folderbar = Scrollbar(right, {'relief': 'sunken', 'bd': 2})
	folderbar.pack({'fill': 'y', 'side': 'right'})

	folderbox = Listbox(right, {'exportselection': 0})
	folderbox.pack({'expand': 1, 'fill': 'both', 'side': 'left'})

	foldermenu = Menu(root)
	foldermenu.add('command',
		       {'label': 'Open Folder',
			'command': open_folder})
	foldermenu.add('separator')
	foldermenu.add('command',
		       {'label': 'Quit',
			'command': 'exit'})
	foldermenu.bind('<ButtonRelease-3>', folder_unpost)

	folderbox['yscrollcommand'] = (folderbar, 'set')
	folderbar['command'] = (folderbox, 'yview')
	folderbox.bind('<Double-1>', open_folder, 1)
	folderbox.bind('<3>', folder_post)

	# Build left part: scan list

	left = Frame(top)
	left.pack({'expand': 1, 'fill': 'both', 'side': 'left'})

	scanbar = Scrollbar(left, {'relief': 'sunken', 'bd': 2})
	scanbar.pack({'fill': 'y', 'side': 'right'})

	scanbox = Listbox(left, {'font': 'fixed'})
	scanbox.pack({'expand': 1, 'fill': 'both', 'side': 'left'})

	scanmenu = Menu(root)
	scanmenu.add('command',
		     {'label': 'Open Message',
		      'command': open_message})
	scanmenu.add('command',
		     {'label': 'Remove Message',
		      'command': remove_message})
	scanmenu.add('command',
		     {'label': 'Refile Message',
		      'command': refile_message})
	scanmenu.add('separator')
	scanmenu.add('command',
		     {'label': 'Quit',
		      'command': 'exit'})
	scanmenu.bind('<ButtonRelease-3>', scan_unpost)

	scanbox['yscrollcommand'] = (scanbar, 'set')
	scanbar['command'] = (scanbox, 'yview')
	scanbox.bind('<Double-1>', open_message)
	scanbox.bind('<3>', scan_post)

	# Separator between middle and bottom part

	rule2 = Frame(root, {'bg': 'black'})
	rule2.pack({'fill': 'x'})

	# Build bottom part: current message

	bot = Frame(root)
	bot.pack({'expand': 1, 'fill': 'both'})
	#
	viewer = None

	# Window manager commands

	root.minsize(800, 1) # Make window resizable

	# Fill folderbox with text

	setfolders()

	# Fill scanbox with text

	rescan()

	# Enter mainloop

	root.mainloop()

def folder_post(e):
	x, y = e.x_root, e.y_root
	foldermenu.post(x - 10, y - 10)
	foldermenu.grab_set()

def folder_unpost(e):
	tk.call('update', 'idletasks')
	foldermenu.grab_release()
	foldermenu.unpost()
	foldermenu.invoke('active')

def scan_post(e):
	x, y = e.x_root, e.y_root
	scanmenu.post(x - 10, y - 10)
	scanmenu.grab_set()

def scan_unpost(e):
	tk.call('update', 'idletasks')
	scanmenu.grab_release()
	scanmenu.unpost()
	scanmenu.invoke('active')

scanparser = regex.compile('^ *\([0-9]+\)')

def open_folder(e=None):
	global folder, mhf
	sel = folderbox.curselection()
	if len(sel) != 1:
		if len(sel) > 1:
			msg = "Please open one folder at a time"
		else:
			msg = "Please select a folder to open"
		dialog(root, "Can't Open Folder", msg, "", 0, "OK")
		return
	i = sel[0]
	folder = folderbox.get(i)
	mhf = mh.openfolder(folder)
	rescan()

def open_message(e=None):
	global viewer
	sel = scanbox.curselection()
	if len(sel) != 1:
		if len(sel) > 1:
			msg = "Please open one message at a time"
		else:
			msg = "Please select a message to open"
		dialog(root, "Can't Open Message", msg, "", 0, "OK")
		return
	cursor = scanbox['cursor']
	scanbox['cursor'] = 'watch'
	tk.call('update', 'idletasks')
	i = sel[0]
	line = scanbox.get(i)
	if scanparser.match(line) >= 0:
		num = string.atoi(scanparser.group(1))
		m = mhf.openmessage(num)
		if viewer: viewer.destroy()
		from MimeViewer import MimeViewer
		viewer = MimeViewer(bot, '+%s/%d' % (folder, num), m)
		viewer.pack()
		viewer.show()
	scanbox['cursor'] = cursor

def interestingheader(header):
	return header != 'received'

def remove_message(e=None):
	itop = scanbox.nearest(0)
	sel = scanbox.curselection()
	if not sel:
		dialog(root, "No Message To Remove",
		       "Please select a message to remove", "", 0, "OK")
		return
	todo = []
	for i in sel:
		line = scanbox.get(i)
		if scanparser.match(line) >= 0:
			todo.append(string.atoi(scanparser.group(1)))
	mhf.removemessages(todo)
	rescan()
	fixfocus(min(todo), itop)

lastrefile = ''
tofolder = None
def refile_message(e=None):
	global lastrefile, tofolder
	itop = scanbox.nearest(0)
	sel = scanbox.curselection()
	if not sel:
		dialog(root, "No Message To Refile",
		       "Please select a message to refile", "", 0, "OK")
		return
	foldersel = folderbox.curselection()
	if len(foldersel) != 1:
		if not foldersel:
			msg = "Please select a folder to refile to"
		else:
			msg = "Please select exactly one folder to refile to"
		dialog(root, "No Folder To Refile", msg, "", 0, "OK")
		return
	refileto = folderbox.get(foldersel[0])
	todo = []
	for i in sel:
		line = scanbox.get(i)
		if scanparser.match(line) >= 0:
			todo.append(string.atoi(scanparser.group(1)))
	if lastrefile != refileto or not tofolder:
		lastrefile = refileto
		tofolder = None
		tofolder = mh.openfolder(lastrefile)
	mhf.refilemessages(todo, tofolder)
	rescan()
	fixfocus(min(todo), itop)

def fixfocus(near, itop):
	n = scanbox.size()
	for i in range(n):
		line = scanbox.get(repr(i))
		if scanparser.match(line) >= 0:
			num = string.atoi(scanparser.group(1))
			if num >= near:
				break
	else:
		i = 'end'
	scanbox.select_from(i)
	scanbox.yview(itop)

def setfolders():
	folderbox.delete(0, 'end')
	for fn in mh.listallfolders():
		folderbox.insert('end', fn)

def rescan():
	global viewer
	if viewer:
		viewer.destroy()
		viewer = None
	scanbox.delete(0, 'end')
	for line in scanfolder(folder, seq):
		scanbox.insert('end', line)

def scanfolder(folder = 'inbox', sequence = 'all'):
	return map(
		lambda line: line[:-1],
		os.popen('scan +%s %s' % (folder, sequence), 'r').readlines())

main()