'''
Created on Jun 09, 2014

@author: G
'''
import sys
import subprocess
import threading
import datetime
import sqlite3
from PyQt4 import QtGui, QtCore
from log import Ui_TabWidget
import urllib2
class MyApp(QtGui.QTabWidget,QtGui.QMainWindow, Ui_TabWidget):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        QtGui.QTabWidget.__init__(self)
        Ui_TabWidget.__init__(self)
        self.setupUi(self)
        faile = True
        self.label_7.setText("Downloading Settings....")
        try:
            con1 = sqlite3.connect('server.db')
            cursor1 = con1.cursor()
            cursor1.execute('SELECT ip,settings,backup_log,deploy_log,active FROM server')
            val = cursor1.fetchone()
            print val
            s_ip = val[0]
            settings = val[1]
            print settings
            faile = False
        except Exception as e:
            self.label_7.setText("Downloading Failed")
            print e
        try:
            if faile == False:
                set = str(settings).split(",")
                for down in set:
                    down1 = urllib2.urlopen(down,None,10)
                    fil = down.split("/")
                    fil.reverse()
                    file_name = fil[0]
                    opn = open(file_name,"wb")
                    opn.write(down1.read())
                    opn.close()
            self.label_7.setText("Login Please")
        except Exception as e:
            self.label_7.setText("New Setting Failed...")
            print e
        self.pushButton.clicked.connect(self.login)
        #self.pushButton_2.clicked.connect(self.reg)
    def login(self):
   
        usr_name = self.lineEdit.text()
        password = self.lineEdit_2.text()
        con = sqlite3.connect('auth.db')
        cursor = con.cursor()
        cursor.execute('SELECT username, password,type,name,status FROM auth where username=\''+str(usr_name)+'\' and password=\''+str(password)+'\'')
        all_val = cursor.fetchall()
        if len(all_val) > 0:
            u_name = all_val[0][0]
            passwd = all_val[0][1]
            type = all_val[0][2]
            name2 = all_val[0][3]
            status = all_val[0][4]
            self.label_7.setText("Authendication Success")
            if status == "DEACTIVATE":
                self.label_7.setText("ACCOUNT DEACTIVATED CONTACT ADMIN")
                return None
            if status != "ACTIVATE":
                self.label_7.setText("ACCOUNT NOT ACTIVATED")
                return None
            print "deploy.exe "+str(type)+" "+str(name2)
            subprocess.Popen("deploy.exe "+str(type)+" "+str(name2))
            sys.exit()
        else:
            self.label_7.setText("Authendication Failed")
        con.close()
#     def reg(self):
#         r_name = str(self.lineEdit_3.text())
#         r_id = str(self.lineEdit_4.text())
#         r_username = str(self.lineEdit_5.text())
#         r_password = str(self.lineEdit_6.text())
#         re_password = str(self.lineEdit_7.text())
#         if len(r_name)<1:
#             self.label_6.setText("Enter Name")
#             return None
#         if len(r_id)<1:
#             self.label_6.setText("Enter id")
#             return None
#         if len(r_username)<1:
#             self.label_6.setText("Enter username")
#             return None
#         if r_password != re_password:
#             self.label_6.setText("Password Not matched")
#             return None
#         if len(r_password)<1:
#             self.label_6.setText("Enter password")
#             return None
#         else:
#             con = sqlite3.connect('auth.db')
#             cursor = con.cursor()
#             cursor.execute("select * from auth where username="+'\''+r_username+'\'')
#             chk_usr = cursor.fetchall()
#             if len(chk_usr)<1:
#                 con.close()
#                 con = sqlite3.connect('auth.db')
#                 cursor = con.cursor()
#                 cursor.execute('''INSERT INTO auth(username, password, id, name)VALUES(?,?,?,?)''', (r_username,r_password,r_id,r_name))
#                 sta = con.commit()
#                 con.close()
#                 self.label_6.setText("Registration Success")
# 
#             else:
#                 con.close()
#                 self.label_6.setText("Username Already exist")
#                 return None
                
if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        window = MyApp()
        window.show()
        sys.exit(app.exec_())