Beispiel #1
0
	def start(self):
		# 1)start node service
		NodeService.start(self)
		# 2)init params
		self.initParams()
		# 3)init gui
		self.initUI()

		# 4)user timer to update gui list every 3 seconds
		self.update_timer = UpdateGUIListTimer('Thread-Update GUI Timer',self.update_interval,self.updateList)
		self.update_timer.start()
Beispiel #2
0
class GuiClient(NodeService,QtGui.QMainWindow):
	"""
	a simple client with gui
	"""
	def __init__(self,url,dirname,ipsfile,update_interval):
		NodeService.__init__(self,url,dirname,ipsfile)
		QtGui.QMainWindow.__init__(self)
		# update_interval for update timer
		self.update_interval = update_interval
		# start gui client
		self.start()

	def start(self):
		# 1)start node service
		NodeService.start(self)
		# 2)init params
		self.initParams()
		# 3)init gui
		self.initUI()

		# 4)user timer to update gui list every 3 seconds
		self.update_timer = UpdateGUIListTimer('Thread-Update GUI Timer',self.update_interval,self.updateList)
		self.update_timer.start()
	
	def stop(self):	
		# 1) stop update timer
		self.update_timer.stop()
		# 2) stop node service
		NodeService.stop(self)

	def initParams(self):
		self.localurl = NodeService.get_url(self)
		self.local_files = []
		self.remote_files = {}

	def initUI(self):
		mylogger.info("[initUI]...")
		# menus toolbars statusbar
		# actions
		self.fetchAction = QtGui.QAction(QtGui.QIcon(ICON_FETCH), '&Fetch', self)
		self.fetchAction.setShortcut('Ctrl+F')
		self.fetchAction.setStatusTip('Fetch file')
		self.fetchAction.triggered.connect(self.onFetchHandler)

		self.stopAction = QtGui.QAction(QtGui.QIcon(ICON_QUIT), '&Quit', self)
		self.stopAction.setShortcut('Ctrl+Q')
		self.stopAction.setStatusTip('Quit application')
		self.stopAction.triggered.connect(self.close)

		menubar = self.menuBar()
		fileMenu = menubar.addMenu('&File')
		fileMenu.addAction(self.fetchAction)
		fileMenu.addAction(self.stopAction)
		
		toolbar = self.addToolBar('tool')
		toolbar.addAction(self.fetchAction)
		toolbar.addAction(self.stopAction)
	
		self.statusbar = self.statusBar()
		# GuiWidget 
		self.main_widget = GuiWidget(self)
		self.main_widget.le.textChanged[str].connect(self.onTextChanged)
		self.main_widget.btn_fetch.clicked.connect(self.onFetchHandler)
		self.main_widget.btn_update.clicked.connect(self.onUpdateHandler)
		self.main_widget.list_remote.itemClicked.connect(self.onListItemClicked)
		# set central widget for main window
		self.setCentralWidget(self.main_widget)

		# set control states
		self.setFetchEnabled(False)
		# set list files
		self.setLocal()
		self.setRemote()

		# settings for window
		self.resize(WIN_WIDTH,WIN_HEIGHT)
		#self.move(200,200)
		self.center()
		self.setWindowTitle('File Sharing Client')
		self.setWindowIcon(QtGui.QIcon(ICON_APP))
		self.show()

	def closeEvent(self,event):
		mylogger.info("[closeEvent]")
		# If we close the QtGui.QWidget, the QtGui.QCloseEvent is generated and closeEvent is called.
        	reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure to exit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.Yes) 
        	if reply == QtGui.QMessageBox.Yes:
			msg = ('###[closeEvent]: program is going to exit... ')
			mylogger.info(msg)
			print(msg)
			self.stop()
            		event.accept()
       		else:
            		event.ignore()        

	def onUpdateHandler(self,value):
		mylogger.info("[onUpdateHandler]...")
		self.updateList()

	def _setLocalFiles(self):
		self.local_files = NodeService.get_local_files(self)

	def _setRemoteFiles(self):
		self.remote_files = NodeService.get_remote_files(self)

	def _setLocalLabel(self):
		#mylogger.info("[_setLocalLabel]...")
		str_local = "local@{0} [total {1} files]".format(self.localurl,len(self.local_files))
		self.main_widget.label_local.setText(str_local)

	def _getRemoteFileCount(self,remotefiles):
		count = 0
		for url,ls in remotefiles.iteritems():
			count +=len(ls)
		return count

	def _setRemoteLabel(self):
		#mylogger.info("[_setRemoteLabel]...")
		nodeCount = len(self.remote_files)
		fileCount = self._getRemoteFileCount(self.remote_files)
		str_remote = "remote [{0} nodes,total {1} files]".format(nodeCount,fileCount)
		self.main_widget.label_remote.setText(str_remote)

	def _setLocalList(self):
		#mylogger.info("[_setLocalList]...")
		self.main_widget.list_local.clear()
		for f in self.local_files:
			self.main_widget.list_local.addItem(f)

	def _setRemoteList(self):
		#mylogger.info("[_setRemoteList]...")
		self.main_widget.list_remote.clear()
		for url,lt in self.remote_files.iteritems():
			for f in lt:
				self.main_widget.list_remote.addItem(f)

	def setLocal(self):
		if NodeService.is_local_updated(self):
			mylogger.info('[setLocal]...')
			self._setLocalFiles()
			self._setLocalLabel()
			self._setLocalList()
			mylogger.info('[setLocal] finished')
			# after set local list,clear local update
			NodeService.clear_local_update(self) # set to false
		else:
			mylogger.info('*********NO GUI UPDATE FOR local list*********')
	
	def setRemote(self):
		if NodeService.is_remote_updated(self):
			mylogger.info('[setRemote]...')
			self._setRemoteFiles()
			self._setRemoteLabel()
			self._setRemoteList()
			mylogger.info('[setRemote] finished')
			# after set remote list,clear remote update
			NodeService.clear_remote_update(self) # set to false
		else:
			mylogger.info('*********NO GUI UPDATE FOR remote list*********')

	def updateList(self):
		mylogger.info('-'*50)
		mylogger.info("[updateList]...")
		# update local and remote files
		NodeService.update_local_list(self)
		NodeService.update_remote_list(self)
		self.setLocal()
		self.setRemote()
		mylogger.info("[updateList] finished")
		mylogger.info('-'*50)

	def setFetchEnabled(self,enabled):
		self.fetchAction.setEnabled(enabled)
		self.main_widget.btn_fetch.setEnabled(enabled)

	def center(self):
		mbr = self.frameGeometry()
        	cen = QtGui.QDesktopWidget().availableGeometry().center()
        	mbr.moveCenter(cen)
        	self.move(mbr.topLeft())

	def keyPressEvent(self,event):	
		mylogger.info("[keyPressEvent]")
		if event.key() == QtCore.Qt.Key_Escape:
			self.close()
		elif event.key() == QtCore.Qt.Key_Enter:
			self.onFetchHandler(False)
		else: pass
	

	def onTextChanged(self,value):
		if value.isEmpty():
			# set control states
			self.setFetchEnabled(False)
		else:
			# set control states
			self.setFetchEnabled(True)
		
	def onFetchHandler(self,value):
		mylogger.info("[onFetchHandler]")
		# by default ,for a button value is False
		arg = str(self.main_widget.le.text())
		if not arg.strip():
			msg = 'Please enter query file'
			self.statusbar.showMessage(msg)
			return
		# add statusbar messge for fetching file
		msg = "Fetching [{0}].......".format(arg)
		mylogger.info(msg)
		self.statusbar.showMessage(msg)
		# use NodeService
		code = NodeService.fetch(self,arg)
		if code == SUCCESS:
			msg ="Fetch successfully for [{0}]".format(arg)
			self._onFetchSuccessfully(arg)
		elif code == ACCESS_DENIED:
			msg ="Access denied for [{0}]".format(arg)
		elif code == NOT_EXIST:
			msg ="Not exist for [{0}]".format(arg)
		else:
			msg = "Already exist for [{0}]".format(arg)
		mylogger.info(msg)
		self.statusbar.showMessage(msg)
	
	def _onFetchSuccessfully(self,arg):
		# when fetch successfully, need to update local list
		NodeService.update_local_list(self)
		self.setLocal()

	def onListItemClicked(self,value):
		self.main_widget.le.setText(value.text())
		self.statusbar.showMessage('')