#!/usr/bin/python

"""
tuxpaint-savedir.py

Uses the 'wxWidgets' module to display a graphical user interface (GUI)
which asks the user who they are.  It then uses that choice
to determine what directory Tux Paint should save its files in
(via the "--savedir" command-line option)

Bill Kendrick
bill@newbreedsoftware.com
http://www.newbreedsoftware.com/tuxpaint/

Friendly 'mkdir()' by Trent Mick, 2001.Oct.18
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82465

2005.Feb.10 - 2005.Feb.11  (v2)
"""

# File containing a list of names and corresponding directories, i.e.:
#   John Smith
#   john
#   Jane Doe
#   jdoe
USERS_FILE='users.txt'

# Directory that all of the ones listed in the file are under (OPTIONAL)
BASEDIR="C:/tuxpaint-users/"

# Whether to try and make the directories ourself  (0=no, 1=yes)
MAKE_DIR=1

# Command to execute, and its location
# (e.g., the directory "C:\Program Files\TuxPaint\" and the file "TuxPaint.exe")
CMD_DIR='C:/Program Files/TuxPaint/'
CMD='TuxPaint.exe'


import wx
import os

LIST=wx.NewId()
LAUNCH=wx.NewId()
QUIT=wx.NewId()


class frame_tuxpaint_launcher(wx.Frame):
    def __init__(self):
        self.frame = wx.Frame.__init__(self, None, -1, "Tux Paint Launcher")
	
        self.list_users = wx.ListBox(self, LIST, (0, 0), (-1, 120))
	""" FIXME: There's got to be a way to get this to fill the space """

	file = open(USERS_FILE)
	""" FIXME: Show an error if the file could not be opened! """

	while 1:
            line_name = file.readline()
            line_dir = file.readline()
            if not line_dir or not line_name: break

            line_name = (line_name.replace('\n', '')).replace('\r', '')
            line_dir = (line_dir.replace('\n', '')).replace('\r', '')
	    
            self.list_users.Append(line_name, line_dir)
	file.close()
	
        self.button_launch = wx.Button(self, LAUNCH, "Launch!")
	wx.EVT_BUTTON(self, LAUNCH, self.cb_launch)
	""" FIXME: It'd be nice if this button could be green """
	""" FIXME: [Enter]/[Return] should be the same as clicking 'Launch' """
	
        self.button_quit = wx.Button(self, QUIT, "Quit")
	wx.EVT_BUTTON(self, QUIT, self.cb_quit)
	""" FIXME: It'd be nice if this button could be red """
	""" FIXME: [Enter]/[Return] should be the same as clicking 'Quit' """
	
        row = wx.BoxSizer(wx.VERTICAL)
        row.Add(self.list_users, 0, wx.ALIGN_CENTER)
        col = wx.BoxSizer(wx.HORIZONTAL)
        col.Add(self.button_launch, 1, wx.EXPAND)
        col.Add(self.button_quit, 1, wx.EXPAND)

	row.Add(col, 0, wx.ALIGN_CENTER)
	
        self.SetSizer(row)

    def cb_quit(self, event):
        self.Destroy()

    def cb_launch(self, event):
    	if self.list_users.GetSelection() != -1:
            DIR = BASEDIR + self.list_users.GetClientData(self.list_users.GetSelection())
            if MAKE_DIR == 1:
                _mkdir(DIR)

            os.chdir(CMD_DIR)
            print CMD + " --savedir " + DIR
            print os.spawnl(os.P_NOWAIT, CMD, "./tuxpaint", "--savedir", '"' + DIR + '"')
            self.Destroy()

def _mkdir(newdir):
    """works the way a good mkdir should :)
        - already exists, silently complete
        - regular file in the way, raise an exception
        - parent directory(ies) does not exist, make them as well
    """
    if os.path.isdir(newdir):
        pass
    elif os.path.isfile(newdir):
        raise OSError("a file with the same name as the desired " \
                      "dir, '%s', already exists." % newdir)
    else:
        head, tail = os.path.split(newdir)
        if head and not os.path.isdir(head):
            _mkdir(head)
        #print "_mkdir %s" % repr(newdir)
        if tail:
            os.mkdir(newdir)


app = wx.PySimpleApp()
frame = frame_tuxpaint_launcher()
frame.Show(1)
app.MainLoop()

