#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 on Tue Apr 21 16:28:12 2015
#

import wx
import subprocess
import threading
import time

# begin wxGlade: dependencies
import gettext
# end wxGlade

# begin wxGlade: extracode
# end wxGlade

filename = ''

class StartThread(threading.Thread):
  def __init__(self,ref):
        threading.Thread.__init__(self)
        self.ref = ref
  
  def run(self):
    # fetch a binary file
	subprocess.call(["sudo","apachectl","start"])
	
class StopThread(threading.Thread):
  def __init__(self,ref):
        threading.Thread.__init__(self)
        self.ref = ref
  
  def run(self):
    # fetch a binary file
	subprocess.call(["sudo","apachectl","stop"])

class RestartThread(threading.Thread):
  def __init__(self,ref):
        threading.Thread.__init__(self)
        self.ref = ref
  
  def run(self):
    # fetch a binary file
	time.sleep(5)
	subprocess.call(["sudo","apachectl","restart"])
		
class DownloadThread(threading.Thread):
  def __init__(self,ref):
        threading.Thread.__init__(self)
        self.ref = ref
  
  def run(self):
    # fetch a binary file
	pass

class UploadThread(threading.Thread):
  def __init__(self,ref,wx):
        threading.Thread.__init__(self)
        self.ref = ref
        wx = wx
  
  def run(self):
    # fetch a binary file
	global filename
    #filename = ''
        #dlg = wx.FileDialog(message="Choose a file")
        #if dlg.ShowModal() == wx.ID_OK:
        #    filename = dlg.GetPath()
        #dlg.Destroy()
        #if not filename:
        #   return
        #print filename
        subprocess.call(["sudo","cp",filename,"/var/www/html"])
        subprocess.call(["ls","/var/www/html"])

class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame1.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, wx.ID_ANY, _("Swerver"), style=wx.ALIGN_CENTRE)
        self.startButton = wx.Button(self, wx.ID_ANY, _("Start"))
        self.label_5 = wx.StaticText(self, wx.ID_ANY, _("\nServer Status\n"), style=wx.ALIGN_CENTRE)
        self.stopButton = wx.Button(self, wx.ID_ANY, _("Stop"))
        self.statusText = wx.StaticText(self, wx.ID_ANY, _("\nStatus\n"), style=wx.ALIGN_CENTRE)
        self.restartButton = wx.Button(self, wx.ID_ANY, _("Restart"))
        self.goButton = wx.Button(self, wx.ID_ANY, _("Go to Server"))
        self.uploadButton = wx.Button(self, wx.ID_ANY, _("Upload"))

        self.__set_properties()
        self.__do_layout()
        self.Bind(wx.EVT_BUTTON, self.startBtn, self.startButton)
        self.Bind(wx.EVT_BUTTON, self.stopBtn, self.stopButton)
        self.Bind(wx.EVT_BUTTON, self.restartBtn, self.restartButton)
        self.Bind(wx.EVT_BUTTON, self.goBtn, self.goButton)
        self.Bind(wx.EVT_BUTTON, self.uploadBtn, self.uploadButton)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame1.__set_properties
        self.SetTitle(_("frame_2"))
        self.SetSize((459, 411))
        self.label_1.SetFont(wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
        self.label_5.SetFont(wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
        self.statusText.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame1.__do_layout
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.GridSizer(3, 2, 0, 0)
        sizer_2.Add(self.label_1, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 10)
        grid_sizer_1.Add(self.startButton, 0, wx.ALL | wx.EXPAND, 10)
        grid_sizer_1.Add(self.label_5, 0, wx.ALL | wx.EXPAND, 10)
        grid_sizer_1.Add(self.stopButton, 0, wx.ALL | wx.EXPAND, 10)
        grid_sizer_1.Add(self.statusText, 0, wx.ALL | wx.EXPAND, 10)
        grid_sizer_1.Add(self.restartButton, 0, wx.ALL | wx.EXPAND, 10)
        grid_sizer_1.Add(self.goButton, 0, wx.ALL | wx.EXPAND, 10)
        sizer_2.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        sizer_2.Add(self.uploadButton, 0, wx.ALL | wx.EXPAND, 10)
        self.SetSizer(sizer_2)
        self.Layout()
        # end wxGlade

    def startBtn(self,event):
        #subprocess.call(["sudo","apachectl","start"])
        self.statusText.SetLabel("Start")
        startthread = StartThread(self)
        startthread.start()
        self.statusText.SetLabel("Running")

    def stopBtn(self,event):
        #subprocess.call(["sudo","apachectl","stop"])
        stopthread = StopThread(self)
        stopthread.start()
        self.statusText.SetLabel("Stopped")

    def restartBtn(self,event):
        #subprocess.call(["sudo","apachectl","restart"])
        self.statusText.SetLabel("Stopped")	
        restartthread = RestartThread(self)
        restartthread.start()
        self.statusText.SetLabel("Running")	

    def uploadBtn(self,event):
        global filename        
        filename = ''
        dlg = wx.FileDialog(self, message="Choose a file")
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
        dlg.Destroy()
        if not filename:
           return
        print filename
        #subprocess.call(["sudo","cp",filename,"/var/www/html"])
        #subprocess.call(["ls","/var/www/html"])
        uploadthread = UploadThread(self,wx)
        uploadthread.start()        


    def goBtn(self,event):
        subprocess.call(["sensible-browser","localhost"])

# end of class MyFrame1
if __name__ == "__main__":
    gettext.install("app") # replace with the appropriate catalog name

    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    mainwindow = MyFrame1(None, wx.ID_ANY, "")
    app.SetTopWindow(mainwindow)
    mainwindow.Show()
    app.MainLoop()
