#
# A GtkVBox Inherited objects for selecting storage method
# of a mailbox and converting stuff from one method to
# another.
#

import gtk
import os
import os.path
import __init__
import utils
import ptk.misc_widgets
import pynei18n

class configbox(gtk.VBox):
	def __init__(self, mailbox, user, parent_win):
		self.user = user
		self.mailbox = mailbox
		self.parent_win = parent_win
		#self.change_to = self.mailbox.export_format
		self.change_to = (0,0)

		gtk.VBox.__init__(self, spacing=5)
		self.set_border_width(5)

		##		
		box1 = gtk.HBox(spacing=5)
		self.pack_start(box1, expand=False)
		box1.show()

		label = gtk.Label(_("Current Format: "))
		box1.pack_start(label, expand=False)
		label.show()

		entry = gtk.Entry()
		entry.set_text(__init__.bignames[self.mailbox.export_format[0]])
		box1.pack_start(entry)
		entry.set_editable(False)
		entry.show()

		##
		box2 = gtk.HBox(spacing=5)
		self.pack_start(box2, expand=False)
		box2.show()

		def update_do_button ():
			do_button.set_sensitive (self.change_to[0] != self.mailbox.export_format[0])

		def change_boxtype(item, self=self):
			type = combo.get_active ()
			if type == __init__.BF_MHBOX:
				location = os.environ["HOME"]+"/mhmail"
			else:
				location = ""
			self.change_to = (type, location)
			if type == __init__.BF_MHBOX:
				self.location_box.set_editable(True)
				self.location_box.set_text(location)
			else:
				self.location_box.set_editable(False)
				self.location_box.set_text("")
			update_do_button ()

		# menu of possible box format types
		combo = gtk.combo_box_new_text ()
		for i in __init__.bignames:
			combo.append_text (i)
		combo.connect ("changed", change_boxtype)
		combo.set_active (self.mailbox.export_format[0])
		box2.pack_end (combo, expand=False)
		combo.show ()

		label = gtk.Label(_("Change to: "))
		box2.pack_end(label, expand=False)
		label.show()

		##
		box3 = gtk.HBox(spacing=5)
		self.pack_start(box3, expand=False)
		box3.show()

		# by default we convert existing messages to new format
		self.convert_toggle = gtk.CheckButton(_("Convert existing messages"))
		self.convert_toggle.set_active(True)
		box3.pack_end(self.convert_toggle, expand=False)
		self.convert_toggle.show()

		##
		box4 = gtk.HBox(spacing=5)
		self.pack_start(box4, expand=False)
		box4.show()

		label = gtk.Label(_("Location: "))
		box4.pack_start(label, expand=False)
		label.show()

		self.location_box = gtk.Entry()
		if self.mailbox.export_format[0] == __init__.BF_MHBOX:
			self.location_box.set_text(self.mailbox.export_format[1])
			self.location_box.set_editable(True)
		else:
			self.location_box.set_editable(False)
		box4.pack_start(self.location_box)
		self.location_box.show()

		def get_location(_button, self=self):
			# only mhboxes need location
			if self.change_to[0] == __init__.BF_MHBOX:
				location = utils.modal_file_sel_box(_("Location of mh mailbox"))
				if location == None:
					return
				if os.path.isdir(location):
					self.location_box.set_text(location)
					self.change_to = (self.change_to[0], location)
				else:
					utils.info_box(_("Error"), _("You must select a directory\nfor an MH Mailbox"))
			print self.change_to

		button = gtk.Button(" "+_("Change")+" ")
		button.connect("clicked", get_location)
		box4.pack_start(button, expand=False)
		button.show()

		##
		box5 = gtk.HBox(spacing=5)
		self.pack_end(box5, expand=False)
		box5.show()

		do_button = gtk.Button(" "+_("Do conversion")+" ")
		do_button.connect("clicked", self.do_conversion)
		box5.pack_end(do_button, expand=False)
		update_do_button ()
		do_button.show()

	def do_conversion(self, _button=None):
		if self.change_to[0] == self.mailbox.export_format[0]:
			# nothing to do
			return
		# one last chance
		if ptk.misc_widgets.ReturnDialog (_("Confirm"), _("Are you sure you want to convert\nyour mailbox?"), ((gtk.STOCK_YES,1), (gtk.STOCK_NO,0)), parent_win=self.parent_win) == 0:
			return
		# okay, let's do it
		do_convert = self.convert_toggle.get_active()

		self.mailbox.export_format = self.change_to	

		if do_convert == False:
			# trash old mailbox
			self.mailbox.nuke_storage()
			self.mailbox.startup(self.user)
		else:
			# convert
			old_io = self.mailbox._io
			self.mailbox.startup(self.user)
			# copy messages from old to new
			for x in old_io.get_contents():
				msg = old_io.load_article(x)
				if msg != None:
					self.mailbox.save_article(msg)
			# XXX BUG XXX this really messes up nntpboxes
			self.mailbox.messages = self.mailbox._io.get_contents()
			old_io.nuke()
		self.user.update()



syntax highlighted by Code2HTML, v. 0.9.1