def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ self.server_model = ServerModel() self.platform_model = PlatformModel() self.solution_model = SolutionModel() self.upload_thread = UploadThread() self.upload_thread.finished.connect(self.on_upload_thread_finished) self.upload_thread.information.connect( self.on_upload_thread_information) super(MainWindow, self).__init__(parent) self.setupUi(self) self.initUi() self.initData() self.createContextMenu()
def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ self.server_model = ServerModel() self.platform_model = PlatformModel() self.solution_model = SolutionModel() self.upload_thread = UploadThread() self.upload_thread.finished.connect(self.on_upload_thread_finished) self.upload_thread.information.connect(self.on_upload_thread_information) super(MainWindow,self).__init__(parent) self.setupUi(self) self.initUi() self.initData() self.createContextMenu()
def echo(): global LAST_UPDATE_ID # Request updates from last updated_id for update in bot.getUpdates(offset=LAST_UPDATE_ID): if LAST_UPDATE_ID < update.update_id: # chat_id is required to reply any message chat_id = update.message.chat_id message = update.message.text if (message): if '/start' in message or '/help' in message or '/list' in message or '/commands' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.sendMessage(chat_id=chat_id, text=help.encode('utf8'), disable_web_page_preview=True) ##################----- PublicPlugins -----################## #Require API keys '''Youtube search''' if '/yt' in message or '/youtube' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/youtube': '', '/yt': ''} search_term = replace_all(message, replacer) if len(search_term) < 1: bot.sendMessage( chat_id=chat_id, text= 'Youtube API calls are costly. Use it like /yt keywords; Ex, /yt Iron Maiden' ) else: bot.sendMessage( chat_id=chat_id, text=youtube(search_term).encode('utf8')) '''Twitter latest tweets of user''' if '/twitter' in message or '/tw' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/twitter': '', '/tw': ''} username = replace_all(message, replacer) if len(username) < 1: bot.sendMessage( chat_id=chat_id, text='Use it like: /tw username; Ex, /tw pytacular' ) else: bot.sendMessage(chat_id=chat_id, text=twitter(username).encode('utf8')) '''Gets twitter trends by country /tt countryname, ex /tt India ''' if '/tt' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/tt ': ''} place = replace_all(message, replacer) bot.sendMessage(chat_id=chat_id, text=twittertrends(place).encode('utf8')) '''Search twitter for top 4 related tweets. /ts #Privacy''' if '/ts' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/ts ': ''} search_term = replace_all(message, replacer) bot.sendMessage( chat_id=chat_id, text=twittersearch(search_term).encode('utf8')) '''Instagram latest posts of user''' if '/insta' in message or '/instagram' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.sendMessage( chat_id=chat_id, text= 'Instagram has restricted API access. This will not work anymore. Sorry :(' ) '''Game "Hot or Not" ''' if '/hon' in message or '/hotornot' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) custom_keyboard = [[ telegram.Emoji.THUMBS_UP_SIGN, telegram.Emoji.THUMBS_DOWN_SIGN ]] reply_markup = telegram.ReplyKeyboardMarkup( custom_keyboard, resize_keyboard=True, one_time_keyboard=True) image = imgur_hon() bot.sendMessage( chat_id=chat_id, text='Fetched from Imgur Subreddit r/models : ' + image, reply_markup=reply_markup) '''Bing Image Search''' if '/image' in message or '/img' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/image': '', '/img': ''} search_term = replace_all(message, replacer) pic_link = bingsearch(search_term, 'Image') bot.sendMessage(chat_id=chat_id, text=pic_link) '''Microsoft translator''' if '/translate' in message: message = message.replace('/translate', '').encode('utf8') message_broken = shlex.split(message) error = 'Not enough parameters. Use, /translate en hi "Hello world" or /translate help to know more' if not len(message_broken) < 1: if message_broken[0] == 'help': help_string = """ Example, /translate en hi "Hello world" ar-Arabic | bs-Latn-Bosnian (Latin) | bg-Bulgarian | ca-Catalan | zh-CHS-Chinese Simplified | zh-CHT-Chinese Traditional|hr-Croatian | cs-Czech | da-Danish | nl-Dutch |en-English | cy-Welsh | et-Estonian | fi-Finnish | fr-French | de-German | el-Greek | ht-Haitian Creole | he-Hebrew | hi-Hindi | mww-Hmong Daw | hu-Hungarian | id-Indonesian | it-Italian | ja-Japanese | tlh-Klingon | tlh - Qaak-Klingon (pIqaD) | ko-Korean | lv-Latvian | lt-Lithuanian | ms-Malay | mt-Maltese | no-Norwegian | fa-Persian | pl-Polish | pt-Portuguese | otq-Querétaro Otomi | ro-Romanian | ru-Russian | sr-Cyrl-Serbian (Cyrillic) | sr-Latn-Serbian (Latin) | sk-Slovak | sl-Slovenian | es-Spanish | sv-Swedish | th-Thai | tr-Turkish | uk-Ukrainian | ur-Urdu | vi-Vietnamese | """ bot.sendMessage(chat_id=chat_id, text=help_string) else: if len(message_broken) < 3: bot.sendMessage(chat_id=chat_id, text=error) else: lang_from = message_broken[0] lang_to = message_broken[1] lang_text = message_broken[2] print(lang_from + lang_to + lang_text) bot.sendMessage(chat_id=chat_id, text=btranslate( lang_text, lang_from, lang_to)) else: bot.sendMessage(chat_id=chat_id, text=error) '''Random cat pic''' if '/cats' in message: bot.sendMessage( chat_id=chat_id, text='Hold on, digging out a random cat pic!') url = "http://thecatapi.com/api/images/get?api_key=" + cat_API_key + "&format=xml" xml_src = urllib.request.urlopen(url) data = xml_src.read() xml_src.close() data = xmltodict.parse(data) piclink = data['response']['data']['images']['image'][ 'url'] source_url = data['response']['data']['images']['image'][ 'source_url'] threadobjcats = UploadThread(bot, chat_id, piclink, caption=source_url) threadobjcats.setName('catsthread') threadobjcats.start() # Don't need an API key '''Google search''' if '/google' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) search_term = message.replace('/google', '') if len(search_term) < 1: bot.sendMessage( chat_id=chat_id, text='Use it like: /google what is a bot') else: bot.sendMessage(chat_id=chat_id, text=google(search_term)) '''Wikipedia search''' if '/wiki' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) search_term = message.replace('/wiki ', '') if len(search_term) < 1: bot.sendMessage(chat_id=chat_id, text='Use it like: /wiki Anaconda') else: reply = wiki(search_term) bot.sendMessage(chat_id=chat_id, text=reply) if ("Cannot acces link!" in reply): reply = "No wikipedia article on that but got some google results for you \n" + google( message) bot.sendMessage(chat_id=chat_id, text=reply) '''Weather by city,state''' if '/weather' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) reply = weather(message) bot.sendMessage(chat_id=chat_id, text=reply) '''Github public feed of any user''' if '/github' in message or '/gh' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/github': '', '/gh': ''} username = replace_all(message, replacer) if len(username) < 1: bot.sendMessage( chat_id=chat_id, text= 'Use it like: /github username Ex, /github bhavyanshu or /gh bhavyanshu' ) else: bot.sendMessage(chat_id=chat_id, text=gitfeed(username)) '''Giphy to search for gif by keyword''' if '/giphy' in message or '/gif' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/giphy': '', '/gif': ''} search_term = replace_all(message, replacer) if len(search_term) < 1: bot.sendMessage( chat_id=chat_id, text= 'Use it like: /giphy keyword ; Ex, /giphy cats or /gif cats' ) else: img = translate(search_term) print(img.fixed_height.downsampled.url) bot.sendMessage( chat_id=chat_id, text= 'Hang in there. Fetching gif..-Powered by GIPHY!') bot.sendChatAction( chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO) threadobjgiphy = UploadThread( bot, chat_id, img.fixed_height.downsampled.url.encode('utf-8')) threadobjgiphy.setName('giphythread') threadobjgiphy.start() '''Basic calculator''' if '/calc' in message: head, sep, tail = message.partition('/') input_nums = tail.replace('calc', '') input_nums = input_nums.replace('\'', '') finalexp = shlex.split(input_nums) exp = finalexp[0] bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) error = 'You think I can compute apple+mongo? Don\'t add alphabet in between please. Use like, /calc 2+2-5(4+8)' if not exp: bot.sendMessage( chat_id=chat_id, text='Y u no type math expression? >.<') elif re.search('[a-zA-Z]', exp): bot.sendMessage(chat_id=chat_id, text=error) else: bot.sendMessage(chat_id=chat_id, text=calculate(exp)) # Updates global offset to get the new updates LAST_UPDATE_ID = update.update_id
def echo(): global LAST_UPDATE_ID # Request updates from last updated_id for update in bot.getUpdates(offset=LAST_UPDATE_ID): if LAST_UPDATE_ID < update.update_id: # chat_id is required to reply any message chat_id = update.message.chat_id message = update.message.text if (message): if '/start' in message or '/help' in message or '/list' in message or '/commands' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.sendMessage(chat_id=chat_id,text=help.encode('utf8'),disable_web_page_preview=True) ##################----- PublicPlugins -----################## #Require API keys '''Youtube search''' if '/yt' in message or '/youtube' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/youtube':'','/yt':''} search_term = replace_all(message,replacer) if len(search_term)<1: bot.sendMessage(chat_id=chat_id,text='Youtube API calls are costly. Use it like /yt keywords; Ex, /yt Iron Maiden') else: bot.sendMessage(chat_id=chat_id,text=youtube(search_term).encode('utf8')) '''Twitter latest tweets of user''' if '/twitter' in message or '/tw' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/twitter':'','/tw':''} username = replace_all(message,replacer) if len(username)<1: bot.sendMessage(chat_id=chat_id,text='Use it like: /tw username; Ex, /tw pytacular') else: bot.sendMessage(chat_id=chat_id,text=twitter(username).encode('utf8')) '''Gets twitter trends by country /tt countryname, ex /tt India ''' if '/tt' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/tt ':''} place = replace_all(message,replacer) bot.sendMessage(chat_id=chat_id,text=twittertrends(place).encode('utf8')) '''Search twitter for top 4 related tweets. /ts #Privacy''' if '/ts' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/ts ':''} search_term = replace_all(message,replacer) bot.sendMessage(chat_id=chat_id,text=twittersearch(search_term).encode('utf8')) '''Instagram latest posts of user''' if '/insta' in message or '/instagram' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.sendMessage(chat_id=chat_id,text='Instagram has restricted API access. This will not work anymore. Sorry :(') '''Game "Hot or Not" ''' if '/hon' in message or '/hotornot' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) custom_keyboard = [[ telegram.Emoji.THUMBS_UP_SIGN, telegram.Emoji.THUMBS_DOWN_SIGN ]] reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard,resize_keyboard=True,one_time_keyboard=True) image = imgur_hon() bot.sendMessage(chat_id=chat_id,text='Fetched from Imgur Subreddit r/models : '+image, reply_markup=reply_markup) '''Bing Image Search''' if '/image' in message or '/img' in message: bot.sendChatAction(chat_id=chat_id,action=telegram.ChatAction.TYPING) replacer = {'/image':'','/img':''} search_term = replace_all(message,replacer) pic_link = bingsearch(search_term,'Image') bot.sendMessage(chat_id=chat_id,text=pic_link) '''Microsoft translator''' if '/translate' in message: message = message.replace('/translate','').encode('utf8') message_broken = shlex.split(message) error = 'Not enough parameters. Use, /translate en hi "Hello world" or /translate help to know more' if not len(message_broken)<1: if message_broken[0] == 'help': help_string = """ Example, /translate en hi "Hello world" ar-Arabic | bs-Latn-Bosnian (Latin) | bg-Bulgarian | ca-Catalan | zh-CHS-Chinese Simplified | zh-CHT-Chinese Traditional|hr-Croatian | cs-Czech | da-Danish | nl-Dutch |en-English | cy-Welsh | et-Estonian | fi-Finnish | fr-French | de-German | el-Greek | ht-Haitian Creole | he-Hebrew | hi-Hindi | mww-Hmong Daw | hu-Hungarian | id-Indonesian | it-Italian | ja-Japanese | tlh-Klingon | tlh - Qaak-Klingon (pIqaD) | ko-Korean | lv-Latvian | lt-Lithuanian | ms-Malay | mt-Maltese | no-Norwegian | fa-Persian | pl-Polish | pt-Portuguese | otq-Querétaro Otomi | ro-Romanian | ru-Russian | sr-Cyrl-Serbian (Cyrillic) | sr-Latn-Serbian (Latin) | sk-Slovak | sl-Slovenian | es-Spanish | sv-Swedish | th-Thai | tr-Turkish | uk-Ukrainian | ur-Urdu | vi-Vietnamese | """ bot.sendMessage(chat_id=chat_id,text=help_string) else: if len(message_broken)<3: bot.sendMessage(chat_id=chat_id,text=error) else: lang_from = message_broken[0] lang_to = message_broken[1] lang_text = message_broken[2] print lang_from+lang_to+lang_text bot.sendMessage(chat_id=chat_id,text=btranslate(lang_text,lang_from,lang_to)) else: bot.sendMessage(chat_id=chat_id,text=error) '''Random cat pic''' if '/cats' in message: bot.sendMessage(chat_id=chat_id,text='Hold on, digging out a random cat pic!') url = "http://thecatapi.com/api/images/get?api_key="+cat_API_key+"&format=xml" xml_src = urllib2.urlopen(url) data = xml_src.read() xml_src.close() data = xmltodict.parse(data) piclink = data['response']['data']['images']['image']['url'] source_url = data['response']['data']['images']['image']['source_url'] threadobjcats = UploadThread(bot,chat_id,piclink,caption=source_url) threadobjcats.setName('catsthread') threadobjcats.start() # Don't need an API key '''Google search''' if '/google' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) search_term = message.replace('/google','') if len(search_term)<1: bot.sendMessage(chat_id=chat_id,text='Use it like: /google what is a bot') else: bot.sendMessage(chat_id=chat_id,text=google(search_term)) '''Wikipedia search''' if '/wiki' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) search_term = message.replace('/wiki ','') if len(search_term)<1: bot.sendMessage(chat_id=chat_id,text='Use it like: /wiki Anaconda') else: reply=wiki(search_term) bot.sendMessage(chat_id=chat_id,text=reply) if ("Cannot acces link!" in reply): reply="No wikipedia article on that but got some google results for you \n"+google(message) bot.sendMessage(chat_id=chat_id,text=reply) '''Weather by city,state''' if '/weather' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) reply=weather(message) bot.sendMessage(chat_id=chat_id,text=reply) '''Github public feed of any user''' if '/github' in message or '/gh' in message: bot.sendChatAction(chat_id=chat_id,action=telegram.ChatAction.TYPING) replacer = {'/github':'','/gh':''} username = replace_all(message,replacer) if len(username)<1: bot.sendMessage(chat_id=chat_id,text='Use it like: /github username Ex, /github bhavyanshu or /gh bhavyanshu') else: bot.sendMessage(chat_id=chat_id,text=gitfeed(username)) '''Giphy to search for gif by keyword''' if '/giphy' in message or '/gif' in message: bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) replacer = {'/giphy':'','/gif':''} search_term = replace_all(message,replacer) if len(search_term)<1: bot.sendMessage(chat_id=chat_id,text='Use it like: /giphy keyword ; Ex, /giphy cats or /gif cats') else: img = translate(search_term) print img.fixed_height.downsampled.url bot.sendMessage(chat_id=chat_id,text='Hang in there. Fetching gif..-Powered by GIPHY!') bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO) threadobjgiphy = UploadThread(bot,chat_id,img.fixed_height.downsampled.url.encode('utf-8')) threadobjgiphy.setName('giphythread') threadobjgiphy.start() '''Basic calculator''' if '/calc' in message: head, sep, tail = message.partition('/') input_nums = tail.replace('calc','') input_nums = input_nums.replace('\'','') finalexp = shlex.split(input_nums) exp = finalexp[0] bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING) error = 'You think I can compute apple+mongo? Don\'t add alphabet in between please. Use like, /calc 2+2-5(4+8)' if not exp: bot.sendMessage(chat_id=chat_id,text='Y u no type math expression? >.<') elif re.search('[a-zA-Z]', exp): bot.sendMessage(chat_id=chat_id,text=error) else: bot.sendMessage(chat_id=chat_id,text=calculate(exp)) # Updates global offset to get the new updates LAST_UPDATE_ID = update.update_id
class MainWindow(QMainWindow, Ui_MainWindow): """ Class documentation goes here. """ def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ self.server_model = ServerModel() self.platform_model = PlatformModel() self.solution_model = SolutionModel() self.upload_thread = UploadThread() self.upload_thread.finished.connect(self.on_upload_thread_finished) self.upload_thread.information.connect( self.on_upload_thread_information) super(MainWindow, self).__init__(parent) self.setupUi(self) self.initUi() self.initData() self.createContextMenu() def initUi(self): self.file_list.setColumnWidth(0, 400) self.file_list.setColumnWidth(2, 150) self.selected_servers.setColumnWidth(2, 80) self.server_list.setColumnWidth(2, 80) self.server_list.hideColumn(0) self.selected_servers.hideColumn(0) self.server_list.keyPressEvent = self.on_server_list_keyPressEvent self.selected_servers.keyPressEvent = self.on_selected_servers_keyPressEvent self.statusBar.showMessage(u"状态") def initData(self): self.platform.addItems(self.platform_model.list()) self.upload_solution.addItems(self.solution_model.list()) def createContextMenu(self): self.server_list_contextmenu = QtGui.QMenu(self) self.server_list_contextmenu.addAction(self.action_move2left) self.server_list_contextmenu.addSeparator() self.server_list_contextmenu.addAction(self.action_add_server) self.server_list_contextmenu.addAction(self.action_edit_server) self.server_list_contextmenu.addAction(self.action_copy_server) self.server_list_contextmenu.addSeparator() self.server_list_contextmenu.addAction(self.action_delete_server) @pyqtSlot(QPoint) def on_server_list_customContextMenuRequested(self, point): """ Slot documentation goes here. """ point.setY(point.y() + 25) if self.server_list.rowCount(): point.setX(point.x() + 15) self.server_list_contextmenu.exec_(self.server_list.mapToGlobal(point)) @pyqtSlot() def on_start_upload_clicked(self): """ Slot documentation goes here. """ if self.file_list.rowCount() < 1: QtGui.QMessageBox.information(self, u"提示", u"请先添加要上传的文件!") return if self.selected_servers.rowCount() < 1: QtGui.QMessageBox.information(self, u"提示", u"请先添加服务器到上传列表中!") return answer = QtGui.QMessageBox.warning(self, u"提示", u"上传将覆盖服务器设置路径下的同名文件,确定上传吗?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.No: return if self.upload_thread.isRunning() == False: files = [ self.file_list.item(i, 0).text() for i in range(self.file_list.rowCount()) ] server_ids = [ str(self.selected_servers.item(i, 0).text()) for i in range(self.selected_servers.rowCount()) ] servers = self.server_model.findByIds(",".join(server_ids)) self.upload_thread.setData(files, servers) self.upload_thread.start() self.stop_upload.setEnabled(True) self.start_upload.setEnabled(False) @pyqtSlot() def on_stop_upload_clicked(self): """ Slot documentation goes here. """ if self.upload_thread.isRunning() == True: answer = QtGui.QMessageBox.warning(self, u"提示", u"确定终止当前上传任务?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.No: return self.start_upload.setEnabled(True) self.stop_upload.setEnabled(False) self.upload_thread.terminate() def on_upload_thread_finished(self): self.start_upload.setEnabled(True) self.stop_upload.setEnabled(False) @pyqtSlot(str) def on_upload_thread_information(self, info): time = QTime(QTime.currentTime()) self.textBrowser.append(u"[%s] %s" % (time.toString(), info)) @pyqtSlot(str) def on_upload_solution_currentIndexChanged(self, p0): """ Slot documentation goes here. """ if self.upload_solution.currentIndex() > 0: self.setSelectedServersData(self.solution_model.load(p0)) self.remove_solution.setEnabled(True) self.refresh_server_list.clicked.emit(True) else: self.setSelectedServersData([]) self.remove_solution.setEnabled(False) @pyqtSlot(str) def on_platform_currentIndexChanged(self, p0): """ Slot documentation goes here. """ self.setServerListData(self.server_model.findByPlatform(p0)) @pyqtSlot() def on_move2left_clicked(self): """ Slot documentation goes here. """ rows = list( set([index.row() for index in self.server_list.selectedIndexes()])) for i in range(len(rows)): row = rows[i] - i index = self.selected_servers.rowCount() self.selected_servers.insertRow(index) self.selected_servers.setItem( index, 0, self.server_list.item(row, 0).clone()) self.selected_servers.setItem( index, 1, self.server_list.item(row, 1).clone()) self.selected_servers.setItem( index, 2, self.server_list.item(row, 2).clone()) self.selected_servers.setItem( index, 3, self.server_list.item(row, 3).clone()) self.server_list.removeRow(row) @pyqtSlot() def on_move2right_clicked(self): """ Slot documentation goes here. """ rows = list( set([ index.row() for index in self.selected_servers.selectedIndexes() ])) for i in range(len(rows)): row = rows[i] - i self.selected_servers.removeRow(row) self.refresh_server_list.clicked.emit(True) @pyqtSlot() def on_menu_about_triggered(self): """ Slot documentation goes here. """ QtGui.QMessageBox.about(self, u"FTP Client", u"Powered by ZLH!") @pyqtSlot() def on_menu_clear_log_triggered(self): self.textBrowser.clear() @pyqtSlot() def on_add_solution_clicked(self): """ Slot documentation goes here. """ if self.selected_servers.rowCount() < 1: QtGui.QMessageBox.information(self, u"提示", u"请先添加服务器到上传列表中!") return text, ok = QtGui.QInputDialog.getText(self, u"添加", u"上传方案名称:") if ok == True: if self.solution_model.check(text) > 0: QtGui.QMessageBox.critical(self, u"提示", u"上传方案名称已存在!") return servers = [ str(self.selected_servers.item(i, 0).text()) for i in range(self.selected_servers.rowCount()) ] if self.solution_model.add(text, ",".join(servers)): self.upload_solution.addItem(text) self.upload_solution.setCurrentIndex( self.upload_solution.count() - 1) QtGui.QMessageBox.information(self, u"提示", u"操作成功!") else: QtGui.QMessageBox.critical(self, u"提示", u"操作失败!") @pyqtSlot() def on_remove_solution_clicked(self): """ Slot documentation goes here. """ if self.upload_solution.currentIndex() == 0: QtGui.QMessageBox.information(self, u"FTP Client", u"请选择要删除的上传方案!") return answer = QtGui.QMessageBox.question(self, u"提示", u"确定删除当前选中上传方案?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.Yes: name = self.upload_solution.currentText() if self.solution_model.delete(name): self.upload_solution.removeItem( self.upload_solution.currentIndex()) self.selected_servers.setRowCount(0) self.refresh_server_list.clicked.emit(True) QtGui.QMessageBox.information(self, u"提示", u"操作成功!") @pyqtSlot() def on_refresh_server_list_clicked(self): """ Slot documentation goes here. """ platform = self.platform.currentText() self.setServerListData(self.server_model.findByPlatform(platform)) @pyqtSlot() def on_server_list_itemSelectionChanged(self): rows = self.server_list.selectedIndexes() if (rows): self.move2left.setEnabled(True) self.action_move2left.setEnabled(True) self.action_edit_server.setEnabled(True) self.action_copy_server.setEnabled(True) self.action_delete_server.setEnabled(True) else: self.move2left.setEnabled(False) self.action_move2left.setEnabled(False) self.action_edit_server.setEnabled(False) self.action_copy_server.setEnabled(False) self.action_delete_server.setEnabled(False) @pyqtSlot() def on_selected_servers_itemSelectionChanged(self): rows = self.selected_servers.selectedIndexes() if rows: self.move2right.setEnabled(True) else: self.move2right.setEnabled(False) def on_server_list_keyPressEvent(self, event): if event.key() == Qt.Key_Escape: self.server_list.clearSelection() elif event.key() == Qt.Key_Left: self.move2left.clicked.emit(True) def on_selected_servers_keyPressEvent(self, event): if event.key() == Qt.Key_Escape: self.selected_servers.clearSelection() elif event.key() == Qt.Key_Delete or event.key() == Qt.Key_Right: self.move2right.clicked.emit(True) @pyqtSlot() def on_action_add_server_triggered(self): """ Slot documentation goes here. """ self.server = Server() self.server.saved.connect(self.on_server_saved) self.server.show() @pyqtSlot() def on_action_edit_server_triggered(self): """ Slot documentation goes here. """ server_id = self.server_list.item(self.server_list.currentRow(), 0).text() self.server = Server() self.server.saved.connect(self.on_server_saved) self.server.setData(self.server_model.item(server_id)) self.server.show() @pyqtSlot() def on_action_copy_server_triggered(self): """ Slot documentation goes here. """ server_id = self.server_list.item(self.server_list.currentRow(), 0).text() server_data = self.server_model.item(server_id) server_data["id"] = None self.server = Server() self.server.saved.connect(self.on_server_saved) self.server.setData(server_data) self.server.show() @pyqtSlot() def on_action_delete_server_triggered(self): """ Slot documentation goes here. """ index = self.server_list.currentRow() platform = self.server_list.item(index, 1).text() server = self.server_list.item(index, 2).text() answer = QtGui.QMessageBox.question( self, u"提示", u"确定删除 %s %s?" % (platform, server), QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.Yes: server_id = self.server_list.item(index, 0).text() if self.server_model.delete(server_id): self.server_list.removeRow(index) QtGui.QMessageBox.information(self, u"提示", u"操作成功!") else: QtGui.QMessageBox.critical(self, u"提示", u"操作失败!") def on_server_saved(self): self.refresh_server_list.clicked.emit(True) @pyqtSlot() def on_action_move2left_triggered(self): self.move2left.clicked.emit(True) def setServerListData(self, data): self.server_list.setRowCount(0) for row in data: if self.inSelectedServers(row["id"]) == False: index = self.server_list.rowCount() self.server_list.insertRow(index) self.server_list.setItem(index, 0, QTableWidgetItem(row["id"])) self.server_list.setItem(index, 1, QTableWidgetItem(row["platform"])) self.server_list.setItem(index, 2, QTableWidgetItem(row["server_name"])) self.server_list.setItem(index, 3, QTableWidgetItem(row["path"])) def setSelectedServersData(self, data): self.selected_servers.setRowCount(0) for row in data: index = self.selected_servers.rowCount() self.selected_servers.insertRow(index) self.selected_servers.setItem(index, 0, QTableWidgetItem(row["id"])) self.selected_servers.setItem(index, 1, QTableWidgetItem(row["platform"])) self.selected_servers.setItem(index, 2, QTableWidgetItem(row["server_name"])) self.selected_servers.setItem(index, 3, QTableWidgetItem(row["path"])) self.refresh_server_list.clicked.emit(True) def inSelectedServers(self, server_id): for i in range(self.selected_servers.rowCount()): if self.selected_servers.item(i, 0).text() == server_id: return True return False
class MainWindow(QMainWindow, Ui_MainWindow): """ Class documentation goes here. """ def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ self.server_model = ServerModel() self.platform_model = PlatformModel() self.solution_model = SolutionModel() self.upload_thread = UploadThread() self.upload_thread.finished.connect(self.on_upload_thread_finished) self.upload_thread.information.connect(self.on_upload_thread_information) super(MainWindow,self).__init__(parent) self.setupUi(self) self.initUi() self.initData() self.createContextMenu() def initUi(self): self.file_list.setColumnWidth(0, 400) self.file_list.setColumnWidth(2, 150) self.selected_servers.setColumnWidth(2, 80) self.server_list.setColumnWidth(2, 80) self.server_list.hideColumn(0) self.selected_servers.hideColumn(0) self.server_list.keyPressEvent = self.on_server_list_keyPressEvent self.selected_servers.keyPressEvent = self.on_selected_servers_keyPressEvent self.statusBar.showMessage(u"状态") def initData(self): self.platform.addItems(self.platform_model.list()) self.upload_solution.addItems(self.solution_model.list()) def createContextMenu(self): self.server_list_contextmenu = QtGui.QMenu(self) self.server_list_contextmenu.addAction(self.action_move2left) self.server_list_contextmenu.addSeparator() self.server_list_contextmenu.addAction(self.action_add_server) self.server_list_contextmenu.addAction(self.action_edit_server) self.server_list_contextmenu.addAction(self.action_copy_server) self.server_list_contextmenu.addSeparator() self.server_list_contextmenu.addAction(self.action_delete_server) @pyqtSlot(QPoint) def on_server_list_customContextMenuRequested(self, point): """ Slot documentation goes here. """ point.setY(point.y() + 25) if self.server_list.rowCount(): point.setX(point.x() + 15) self.server_list_contextmenu.exec_(self.server_list.mapToGlobal(point)) @pyqtSlot() def on_start_upload_clicked(self): """ Slot documentation goes here. """ if self.file_list.rowCount() < 1: QtGui.QMessageBox.information(self, u"提示", u"请先添加要上传的文件!") return if self.selected_servers.rowCount() < 1: QtGui.QMessageBox.information(self, u"提示", u"请先添加服务器到上传列表中!") return answer = QtGui.QMessageBox.warning(self, u"提示", u"上传将覆盖服务器设置路径下的同名文件,确定上传吗?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.No: return if self.upload_thread.isRunning() == False: files = [self.file_list.item(i, 0).text() for i in range(self.file_list.rowCount())] server_ids = [str(self.selected_servers.item(i, 0).text()) for i in range(self.selected_servers.rowCount())] servers = self.server_model.findByIds(",".join(server_ids)) self.upload_thread.setData(files, servers) self.upload_thread.start(); self.stop_upload.setEnabled(True) self.start_upload.setEnabled(False) @pyqtSlot() def on_stop_upload_clicked(self): """ Slot documentation goes here. """ if self.upload_thread.isRunning() == True: answer = QtGui.QMessageBox.warning(self, u"提示", u"确定终止当前上传任务?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.No: return self.start_upload.setEnabled(True) self.stop_upload.setEnabled(False) self.upload_thread.terminate(); def on_upload_thread_finished(self): self.start_upload.setEnabled(True) self.stop_upload.setEnabled(False) @pyqtSlot(str) def on_upload_thread_information(self, info): time = QTime(QTime.currentTime()) self.textBrowser.append(u"[%s] %s" % (time.toString(), info)) @pyqtSlot(str) def on_upload_solution_currentIndexChanged(self, p0): """ Slot documentation goes here. """ if self.upload_solution.currentIndex() > 0: self.setSelectedServersData(self.solution_model.load(p0)) self.remove_solution.setEnabled(True) self.refresh_server_list.clicked.emit(True) else: self.setSelectedServersData([]) self.remove_solution.setEnabled(False) @pyqtSlot(str) def on_platform_currentIndexChanged(self, p0): """ Slot documentation goes here. """ self.setServerListData(self.server_model.findByPlatform(p0)) @pyqtSlot() def on_move2left_clicked(self): """ Slot documentation goes here. """ rows = list(set([index.row() for index in self.server_list.selectedIndexes()])) for i in range(len(rows)): row = rows[i]-i index = self.selected_servers.rowCount() self.selected_servers.insertRow(index) self.selected_servers.setItem(index, 0, self.server_list.item(row, 0).clone()) self.selected_servers.setItem(index, 1, self.server_list.item(row, 1).clone()) self.selected_servers.setItem(index, 2, self.server_list.item(row, 2).clone()) self.selected_servers.setItem(index, 3, self.server_list.item(row, 3).clone()) self.server_list.removeRow(row) @pyqtSlot() def on_move2right_clicked(self): """ Slot documentation goes here. """ rows = list(set([index.row() for index in self.selected_servers.selectedIndexes()])) for i in range(len(rows)): row = rows[i]-i self.selected_servers.removeRow(row) self.refresh_server_list.clicked.emit(True) @pyqtSlot() def on_menu_about_triggered(self): """ Slot documentation goes here. """ QtGui.QMessageBox.about(self, u"FTP Client", u"Powered by ZLH!") @pyqtSlot() def on_menu_clear_log_triggered(self): self.textBrowser.clear() @pyqtSlot() def on_add_solution_clicked(self): """ Slot documentation goes here. """ if self.selected_servers.rowCount() < 1: QtGui.QMessageBox.information(self, u"提示", u"请先添加服务器到上传列表中!") return text, ok = QtGui.QInputDialog.getText(self, u"添加", u"上传方案名称:") if ok == True: if self.solution_model.check(text) > 0: QtGui.QMessageBox.critical(self, u"提示", u"上传方案名称已存在!") return servers = [str(self.selected_servers.item(i, 0).text()) for i in range(self.selected_servers.rowCount())] if self.solution_model.add(text, ",".join(servers)): self.upload_solution.addItem(text) self.upload_solution.setCurrentIndex(self.upload_solution.count()-1) QtGui.QMessageBox.information(self, u"提示", u"操作成功!") else: QtGui.QMessageBox.critical(self, u"提示", u"操作失败!") @pyqtSlot() def on_remove_solution_clicked(self): """ Slot documentation goes here. """ if self.upload_solution.currentIndex() == 0: QtGui.QMessageBox.information(self, u"FTP Client", u"请选择要删除的上传方案!") return answer = QtGui.QMessageBox.question(self, u"提示", u"确定删除当前选中上传方案?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.Yes: name = self.upload_solution.currentText() if self.solution_model.delete(name): self.upload_solution.removeItem(self.upload_solution.currentIndex()) self.selected_servers.setRowCount(0) self.refresh_server_list.clicked.emit(True) QtGui.QMessageBox.information(self, u"提示", u"操作成功!") @pyqtSlot() def on_refresh_server_list_clicked(self): """ Slot documentation goes here. """ platform = self.platform.currentText() self.setServerListData(self.server_model.findByPlatform(platform)) @pyqtSlot() def on_server_list_itemSelectionChanged(self): rows = self.server_list.selectedIndexes() if(rows): self.move2left.setEnabled(True) self.action_move2left.setEnabled(True) self.action_edit_server.setEnabled(True) self.action_copy_server.setEnabled(True) self.action_delete_server.setEnabled(True) else: self.move2left.setEnabled(False) self.action_move2left.setEnabled(False) self.action_edit_server.setEnabled(False) self.action_copy_server.setEnabled(False) self.action_delete_server.setEnabled(False) @pyqtSlot() def on_selected_servers_itemSelectionChanged(self): rows = self.selected_servers.selectedIndexes() if rows: self.move2right.setEnabled(True) else: self.move2right.setEnabled(False) def on_server_list_keyPressEvent(self, event): if event.key() == Qt.Key_Escape: self.server_list.clearSelection() elif event.key() == Qt.Key_Left: self.move2left.clicked.emit(True) def on_selected_servers_keyPressEvent(self, event): if event.key() == Qt.Key_Escape: self.selected_servers.clearSelection() elif event.key() == Qt.Key_Delete or event.key() == Qt.Key_Right: self.move2right.clicked.emit(True) @pyqtSlot() def on_action_add_server_triggered(self): """ Slot documentation goes here. """ self.server = Server() self.server.saved.connect(self.on_server_saved) self.server.show() @pyqtSlot() def on_action_edit_server_triggered(self): """ Slot documentation goes here. """ server_id = self.server_list.item(self.server_list.currentRow(), 0).text() self.server = Server() self.server.saved.connect(self.on_server_saved) self.server.setData(self.server_model.item(server_id)) self.server.show() @pyqtSlot() def on_action_copy_server_triggered(self): """ Slot documentation goes here. """ server_id = self.server_list.item(self.server_list.currentRow(), 0).text() server_data = self.server_model.item(server_id) server_data["id"] = None self.server = Server() self.server.saved.connect(self.on_server_saved) self.server.setData(server_data) self.server.show() @pyqtSlot() def on_action_delete_server_triggered(self): """ Slot documentation goes here. """ index = self.server_list.currentRow() platform = self.server_list.item(index, 1).text() server = self.server_list.item(index, 2).text() answer = QtGui.QMessageBox.question(self, u"提示", u"确定删除 %s %s?" % (platform, server), QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if answer == QtGui.QMessageBox.Yes: server_id = self.server_list.item(index, 0).text() if self.server_model.delete(server_id): self.server_list.removeRow(index) QtGui.QMessageBox.information(self, u"提示", u"操作成功!") else: QtGui.QMessageBox.critical(self, u"提示", u"操作失败!") def on_server_saved(self): self.refresh_server_list.clicked.emit(True) @pyqtSlot() def on_action_move2left_triggered(self): self.move2left.clicked.emit(True) def setServerListData(self, data): self.server_list.setRowCount(0) for row in data: if self.inSelectedServers(row["id"]) == False: index = self.server_list.rowCount() self.server_list.insertRow(index) self.server_list.setItem(index, 0, QTableWidgetItem(row["id"])) self.server_list.setItem(index, 1, QTableWidgetItem(row["platform"])) self.server_list.setItem(index, 2, QTableWidgetItem(row["server_name"])) self.server_list.setItem(index, 3, QTableWidgetItem(row["path"])) def setSelectedServersData(self, data): self.selected_servers.setRowCount(0) for row in data: index = self.selected_servers.rowCount() self.selected_servers.insertRow(index) self.selected_servers.setItem(index, 0, QTableWidgetItem(row["id"])) self.selected_servers.setItem(index, 1, QTableWidgetItem(row["platform"])) self.selected_servers.setItem(index, 2, QTableWidgetItem(row["server_name"])) self.selected_servers.setItem(index, 3, QTableWidgetItem(row["path"])) self.refresh_server_list.clicked.emit(True) def inSelectedServers(self, server_id): for i in range(self.selected_servers.rowCount()): if self.selected_servers.item(i, 0).text() == server_id: return True return False