from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import findNetPcs as lan
import networkFileTransferB as nft
import os.path
import thread
from mainwindow import Ui_MainWindow
var_cancelar = False
class window(QMainWindow, Ui_MainWindow):
model = QDirModel()
pcModel = QStandardItemModel()
contas = [ ('', ''), ('', ''), ('', ''), ('', '') ]
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.setupUi(self)
self.lsPastas.setModel(self.model)
self.lsComputadores.setModel(self.pcModel)
self.connect(self.btCopiar, SIGNAL("clicked()"), self.transferir)
self.connect(self.btRefresh, SIGNAL('clicked()'), self.getPcs)
self.connect(self.btCancelar, SIGNAL('clicked()'), self.cancelar)
self.getPcs()
def printa(self, msg='', cor=QColor(0,0,0)):
self.txtSaida.setTextColor(cor)
self.txtSaida.append(msg)
self.txtSaida.moveCursor(QTextCursor.NextRow)
self.txtSaida.moveCursor(QTextCursor.StartOfLine)
def lockui(self, lock=False):
self.btCancelar.setEnabled(lock)
self.btCancelar.setFlat(not lock)
if lock:
self.btCancelar.setText('Cancelar')
else:
self.btCancelar.setText('')
self.frame.setEnabled(not lock)
self.btFechar.setEnabled(not lock)
QApplication.processEvents()
def getPcs(self):
self.pcModel.clear()
count = 0
for i in lan.getPcs():
sItem = QStandardItem()
sItem.setText(i)
sItem.setEditable(False)
sItem.setCheckable(True)
sItem.setCheckState(Qt.Checked)
self.pcModel.insertRow(count, sItem)
ds = lan.diskEnum()
for j in range(len(ds)):
sItem2 = QStandardItem()
sItem2.setText(ds[j])
sItem2.setCheckable(True)
sItem2.setEditable(False)
if ds[j]=='C:':
sItem2.setCheckState(Qt.Checked)
sItem.insertRow(j, sItem2)
count += 1
def getSource(self):
return self.model.filePath(self.lsPastas.currentIndex())
def getDestinos(self):
hosts=[]
for i in range(self.pcModel.rowCount()):
item = self.pcModel.item(i,0)
if item.checkState():
host=str(self.pcModel.item(i,0).text())
drives=[]
for j in range(item.rowCount()):
if item.child(j,0).checkState():
drives.append( str(item.child(j,0).text()[0])+':' )
if len(drives) <= 0:
drives.append('C:')
hosts.append((host,drives))
return hosts
def copiar(self, host, drives=[]):
source = os.path.abspath(str(self.getSource()) )
for drive in drives:
if os.path.isdir(source):
tree = os.walk(source)
for dir in tree:
self.printa('copiando arquivos da pasta '+ dir[0])
for file in dir[2]:
global var_cancelar
if var_cancelar:
self.printa('OPERACAO CANCELADA PELO USUARIO',QColor(255,0,0))
return
pathSource = dir[0]+'\\'+file
pathDest = str(drive) + os.path.splitdrive(pathSource)[1]
self.printa('copiando arquivo '+pathSource+' --> '+host + '\\' + pathDest)
try:
nft.netcopy(host, pathSource, pathDest)
except Exception, err:
self.printa(err, QColor(0255,0,0))
elif os.path.isfile(source):
pathDest = str(drive) + os.path.splitdrive(source)[1]
self.printa( 'copiando arquivo '+ source+ ' --> '+ host + '\\' + pathDest)
nft.netcopy(host, source, pathDest)
else:
self.printa('ERROR: INVALID SOURCE PATH', QColor(255,0,0))
def transferir(self):
thread.start_new_thread(self.transferir_thread, ('thread1',1))
def transferir_thread(self, threadName='', id=1):
global var_cancelar
var_cancelar = False
self.lockui(True)
for host, drives in self.getDestinos():
self.printa('LOGIN.... '+ host, QColor(0,255,0))
for conta in self.contas:
user, passwd = conta
try:
nft.wnet_connect(host, user, passwd)
except Exception, err:
if isinstance(err, nft.win32wnet.error) and err[0] == 1326:
if conta == self.contas[len(self.contas)-1]:
self.printa( 'ERROR: FALHA DE AUTENTICACAO NO COMPUTADOR '+ host, QColor(255,0,0))
#else:
# sys.stderr.write('ERROR: '+ err)
else:
self.printa('log com sucesso em ' + host, QColor(0,255,0))
try:
self.copiar(host, drives)
except Exception, err:
self.printa('ERROR: FALHA NA TRANSFERENCIA DO ARQUIVO', QColor(255,0,0))
self.printa(str(err), QColor(255,0,0))
break
self.lockui(False)
self.printa()
self.printa('CONCLUIDO!!!', QColor(0,0,255))
QMessageBox.information(self,'Concluido', 'COPIAS COMPLETAS', QMessageBox.Ok)
def cancelar(self):
global var_cancelar
var_cancelar = True
self.txtSaida.append('')
self.printa('CANCELANDO.... ', QColor(255,0,0))
self.lockui(False)
if __name__ == '__main__':
a = QApplication(sys.argv)
q = window()
q.show()
sys.exit(a.exec_())