import os, threading
#import os, socket, sys, string, time, threading
from wxPython.wx import *

snarfdir = '.'


class sendFmailFrame(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self,parent, -1, title,wxDefaultPosition, wxSize(450,330))
        panel = wxPanel(self, -1,style=wxTAB_TRAVERSAL)
        buttonok = wxButton(panel, 1002, "Send")
        buttonok.SetPosition(wxPoint(105, 280))

        button = wxButton(panel, 1003, "Cancel")
        button.SetPosition(wxPoint(255, 280))

        EVT_BUTTON(self, 1002, self.OnOK)
        EVT_BUTTON(self, 1003, self.OnCloseMe)
        EVT_CLOSE(self, self.OnCloseWindow)

        wxStaticText(panel, -1, "Fmail Index Name:", wxPoint(15, 20), wxSize(135, 20))
        self.indexname = wxTextCtrl(panel, wxNewId(), "", wxPoint(130, 20), wxSize(300, 20))
        self.msg = wxTextCtrl(panel, wxNewId(), '', wxPoint(15, 60), wxSize(420,200), style=wxTE_MULTILINE)
        #wxStaticText(panel, -1, "", wxPoint(15, row + 30), wxSize(145, 20))

    def OnOK(self, event):
        msg = self.msg.GetValue()
        print msg
        f = open('tmpmail', 'w')
        f.write(msg)
        f.close()
        indexname = self.indexname.GetValue()
        from prefs import *
        if sys.platform == 'win32':
            cmd = ('"%s" -cp /freenet/freenet.jar Freenet.client.IndexClient -serverAddress %s -htl 30 -insert %s -file tmpmail' % (java, serverAddress, indexname))
            print indexname
            print len(indexname)
            fmailThread = sendFmail(cmd)
            fmailThread.start()
        else:
            cmd = ("%s -cp %s/freenet.jar Freenet.client.IndexClient -htl 30 -insert %s -file tmpmail &" % (java, freenetdir, indexname))
            os.system(cmd)
        print cmd
        dlg = wxMessageDialog(self, 'Your mail is being sent.', 'Sending...', wxOK | wxICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()
        self.Close(true)

    def OnCloseMe(self, event):
        self.Close(true)

    def OnCloseWindow(self, event):
        self.Destroy()

class sendFmail(threading.Thread):
    def __init__(self, cmd):
        threading.Thread.__init__(self)
        self.setDaemon(1)
        self.cmd = cmd
        
    def run(self):
        os.system(self.cmd)