Esempio n. 1
0
 def __init__(self, url, password, mainWindow, filenameArg, useRSA, script):
     QsciScintilla.__init__(self)
     self.url = url
     self.password = password
     self.useRSA = useRSA
     self.mainWindow = mainWindow
     self.filenameArg = filenameArg
     self.script = script
     if self.script == 'JSP':
         self.fileConetent = jspCore.downloadFile(url, password,
                                                  filenameArg)
     else:
         self.fileConetent = phpCore.downloadFile(url, password,
                                                  filenameArg, useRSA)
     self.tabWidget = mainWindow.tabWidget
    def fileTableDoubleClicked(self, data):
        url = data[0]
        password = data[1]
        treeWidget = data[2]
        fileTableWidget = data[3]
        rdata = data[4]
        useRSA = data[6]
        script = data[7]
        if treeWidget.currentItem() is None:
            item = data[5]
        else:
            item = treeWidget.currentItem()
        # print(item.text(0))

        # 计算当前行数
        row_num = -1
        for i in fileTableWidget.selectionModel().selection().indexes():
            row_num = i.row()

        if fileTableWidget.item(row_num, 0).text().endswith('/'):
            temp = fileTableWidget.item(row_num, 0).text()[:-1]
            for i in range(item.childCount()):
                if item.child(i).text(0) == temp:
                    self.updateTree([
                        url, password, treeWidget, fileTableWidget, rdata,
                        useRSA, script, 1,
                        item.child(i)
                    ])
        else:
            try:
                dir = self.parsePath(item, rdata)
                filename = fileTableWidget.item(row_num, 0).text()
                filesize = fileTableWidget.item(row_num, 2).text()
                if script == 'JSP':
                    fileConetent = jspCore.readFile(url, password,
                                                    dir + filename)
                else:
                    fileConetent = phpCore.readFile(url, password,
                                                    dir + filename, useRSA)

                # 如果为二进制文件或者超过10M就下载
                if maxFileSize(filesize) or '\0' in fileConetent:
                    try:
                        # TODO 后台下载
                        file = QtWidgets.QFileDialog.getSaveFileName(
                            self.mainWindow, '保存路径', filename)
                        if file[0] != '':

                            if script == 'JSP':
                                buffer = jspCore.downloadFile(
                                    url, password, dir + filename)
                            else:
                                buffer = phpCore.downloadFile(
                                    url, password, dir + filename, useRSA)
                            with open(file[0], 'w', encoding='utf-8') as f:
                                f.write(buffer)
                            QtWidgets.QMessageBox.about(
                                self.mainWindow, "下载成功!", '文件已保存')
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "下载失败!",
                                                    str(Exception(e)))
                else:
                    try:

                        editor = setEditor(url, password, self.mainWindow,
                                           dir + filename, useRSA, script)
                        editor.setText(fileConetent)
                        editor.set()

                        self.mainWindow.tabMaxIndex += 1
                        # tb是TabIndex中的元素
                        tb = self.mainWindow.tabMaxIndex
                        self.mainWindow.tabIndex.append(tb)

                        xbutton = QtWidgets.QPushButton("x")
                        xbutton.setFixedSize(16, 16)
                        xbutton.clicked.connect(
                            lambda: self.delEditorTab([tb, editor]))
                        # 用index方法找到标签页的相对位置
                        self.mainWindow.tabWidget.tabBar().setTabButton(
                            self.mainWindow.tabIndex.index(tb),
                            self.mainWindow.tabWidget.tabBar().RightSide,
                            xbutton)
                        self.mainWindow.tabWidget.setCurrentIndex(
                            self.mainWindow.tabIndex.index(tb))

                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "文件打开失败",
                                                    str(Exception(e)))

            except Exception as e:
                QtWidgets.QMessageBox.about(self.mainWindow, "存在异常",
                                            str(Exception(e)))
    def generateFileListMenu(self, data, pos):
        url = data[0]
        password = data[1]
        treeWidget = data[2]
        fileTableWidget = data[3]
        rdata = data[4]
        useRSA = data[6]
        script = data[7]

        if treeWidget.currentItem() is None:
            item = data[5]
        else:
            item = treeWidget.currentItem()
        # 当前选择文件的目录
        dir = self.parsePath(item, rdata)

        menu = QtWidgets.QMenu()
        # 计算当前行数
        self.mainWindow.row_num = -1
        for i in fileTableWidget.selectionModel().selection().indexes():
            self.mainWindow.row_num = i.row()

        if self.mainWindow.row_num != -1:
            if fileTableWidget.item(self.mainWindow.row_num,
                                    0).text().endswith('/'):
                item0 = menu.addAction('上传文件')
                item0.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/upload_easyicon.svg'))
                item1 = menu.addAction('重命名')
                item1.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/filename_easyicon.svg'))
                item2 = menu.addAction('删除文件')
                item2.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/delete_easyicon.svg'))
                item3 = menu.addAction('更改权限')
                item3.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/management_easyicon.svg'))
                item4 = menu.addAction('刷新目录')
                item4.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/refresh_easyicon.svg'))
                action = menu.exec_(fileTableWidget.mapToGlobal(pos))

                if action == item0:
                    try:
                        filePath = QtWidgets.QFileDialog.getOpenFileName(
                            self.mainWindow, '选择文件')
                        if filePath[0] != '':
                            with open(filePath[0], encoding='utf-8') as f:
                                buffer = f.read()
                            if script == 'JSP':
                                r = jspCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]))
                            else:
                                r = phpCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]),
                                    useRSA)

                            if r == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传成功!", '文件已上传')
                                # 更新文件目录
                                if script == 'JSP':
                                    files = jspCore.scanDir(
                                        url, password, dir).split('\n')
                                else:
                                    files = phpCore.scanDir(
                                        url, password, dir, useRSA).split('\n')
                                files = list(filter(None, files))
                                self.updataTable(files, fileTableWidget)
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传失败!", '可能没有权限')

                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "上传失败!",
                                                    str(Exception(e)))
                elif action == item1:
                    try:
                        rfilename = fileTableWidget.item(
                            self.mainWindow.row_num, 0).text()
                        dfilename, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '重命名', '更改后的文件名:', text=rfilename)
                        if ok:
                            if script == 'JSP':
                                reResult = jspCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename)
                            else:
                                reResult = phpCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename, useRSA)

                            if reResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名成功!", '文件已重命名')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "重命名失败!",
                                                    str(Exception(e)))
                elif action == item2:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        reply = QtWidgets.QMessageBox.question(
                            self.mainWindow, '删除文件', "确定要删除该文件吗?",
                            QtWidgets.QMessageBox.Yes
                            | QtWidgets.QMessageBox.No,
                            QtWidgets.QMessageBox.Yes)
                        if reply == QtWidgets.QMessageBox.Yes:
                            if script == 'JSP':
                                deResult = jspCore.deleteFile(
                                    url, password, dir + file)
                            else:
                                deResult = phpCore.deleteFile(
                                    url, password, dir + file, useRSA)
                            if deResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除成功!", '文件已删除')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')

                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "删除文件失败!",
                                                    str(Exception(e)))
                elif action == item3:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        rmode = fileTableWidget.item(self.mainWindow.row_num,
                                                     3).text()
                        nmode, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '更改权限', '更改为:', text=rmode)
                        if ok:
                            searchObj = re.search('^0[0-7][0-7][0-7]$', nmode)
                            if searchObj is None:
                                raise Exception('输入不合法')
                            if script == 'JSP':
                                chmodResult = jspCore.chmodFile(
                                    url, password, dir + file, nmode)
                            else:
                                chmodResult = phpCore.chmodFile(
                                    url, password, dir + file, nmode, useRSA)

                            if chmodResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改成功!", '权限已经更改')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改失败!", '更改权限失败!')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            self.mainWindow.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "更改权限失败!",
                                                    str(Exception(e)))
                elif action == item4:
                    # 更新文件目录
                    if script == 'JSP':
                        files = jspCore.scanDir(url, password, dir).split('\n')
                    else:
                        files = phpCore.scanDir(url, password, dir,
                                                useRSA).split('\n')
                    files = list(filter(None, files))
                    self.updataTable(files, fileTableWidget)
            else:
                # 文件
                item0 = menu.addAction('上传文件')
                item0.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/upload_easyicon.svg'))
                item1 = menu.addAction('下载文件')
                item1.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/download_easyicon.svg'))
                item2 = menu.addAction('重命名')
                item2.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/filename_easyicon.svg'))
                item3 = menu.addAction('删除文件')
                item3.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/delete_easyicon.svg'))
                item4 = menu.addAction('更改权限')
                item4.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/management_easyicon.svg'))
                item5 = menu.addAction('刷新目录')
                item5.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/refresh_easyicon.svg'))
                action = menu.exec_(fileTableWidget.mapToGlobal(pos))

                if action == item0:
                    try:
                        filePath = QtWidgets.QFileDialog.getOpenFileName(
                            self.mainWindow, '选择文件')
                        if filePath[0] != '':
                            with open(filePath[0], encoding='utf-8') as f:
                                buffer = f.read()
                            if script == 'JSP':
                                upResult = jspCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]))
                            else:
                                upResult = phpCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]),
                                    useRSA)
                            if upResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传成功!", '文件已上传')
                                # 更新文件目录
                                if script == 'JSP':
                                    files = jspCore.scanDir(
                                        url, password, dir).split('\n')
                                else:
                                    files = phpCore.scanDir(
                                        url, password, dir, useRSA).split('\n')
                                files = list(filter(None, files))
                                self.updataTable(files, fileTableWidget)
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传失败!", '可能没有权限')

                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "上传失败!",
                                                    str(Exception(e)))
                elif action == item1:
                    try:
                        # TODO 后台下载
                        filename = fileTableWidget.item(
                            self.mainWindow.row_num, 0).text()
                        file = QtWidgets.QFileDialog.getSaveFileName(
                            self.mainWindow, '保存路径', filename)
                        if file[0] != '':
                            if script == 'JSP':
                                buffer = jspCore.downloadFile(
                                    url, password, dir + filename)
                            else:
                                buffer = phpCore.downloadFile(
                                    url, password, dir + filename, useRSA)
                            with open(file[0], 'w', encoding='utf-8') as f:
                                f.write(buffer)
                            QtWidgets.QMessageBox.about(
                                self.mainWindow, "下载成功!", '文件已保存')
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "下载失败!",
                                                    str(Exception(e)))
                elif action == item2:
                    try:
                        rfilename = fileTableWidget.item(
                            self.mainWindow.row_num, 0).text()
                        dfilename, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '重命名', '更改后的文件名:', text=rfilename)
                        if ok:
                            if script == 'JSP':
                                reResult = jspCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename)
                            else:
                                reResult = phpCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename, useRSA)
                            if reResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名成功!", '文件已重命名')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "重命名失败!",
                                                    str(Exception(e)))
                elif action == item3:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        reply = QtWidgets.QMessageBox.question(
                            self.mainWindow, '删除文件', "确定要删除该文件吗?",
                            QtWidgets.QMessageBox.Yes
                            | QtWidgets.QMessageBox.No,
                            QtWidgets.QMessageBox.Yes)
                        if reply == QtWidgets.QMessageBox.Yes:
                            if script == 'JSP':
                                deResult = jspCore.deleteFile(
                                    url, password, dir + file)
                            else:
                                deResult = phpCore.deleteFile(
                                    url, password, dir + file, useRSA)
                            if deResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除成功!", '文件已删除')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "删除文件失败!",
                                                    str(Exception(e)))
                elif action == item4:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        rmode = fileTableWidget.item(self.mainWindow.row_num,
                                                     3).text()
                        nmode, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '更改权限', '更改为:', text=rmode)
                        if ok:
                            searchObj = re.search('^0[0-7][0-7][0-7]$', nmode)
                            if searchObj is None:
                                raise Exception('输入不合法')
                            if script == 'JSP':
                                chmodResult = jspCore.chmodFile(
                                    url, password, dir + file, nmode)
                            else:
                                chmodResult = phpCore.chmodFile(
                                    url, password, dir + file, nmode, useRSA)
                            if chmodResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改成功!", '权限已经更改')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改失败!", '更改权限失败!')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "更改权限失败!",
                                                    str(Exception(e)))
                elif action == item5:
                    # 更新文件目录
                    if script == 'JSP':
                        files = jspCore.scanDir(url, password, dir).split('\n')
                    else:
                        files = phpCore.scanDir(url, password, dir,
                                                useRSA).split('\n')
                    files = list(filter(None, files))
                    self.updataTable(files, fileTableWidget)
        else:
            item1 = menu.addAction('上传文件')
            item1.setIcon(
                QIcon(
                    os.path.dirname(os.path.realpath(sys.argv[0])) +
                    '/Icons/upload_easyicon.svg'))
            item2 = menu.addAction('刷新目录')
            item2.setIcon(
                QIcon(
                    os.path.dirname(os.path.realpath(sys.argv[0])) +
                    '/Icons/refresh_easyicon.svg'))
            action = menu.exec_(fileTableWidget.mapToGlobal(pos))
            if action == item1:
                try:
                    filePath = QtWidgets.QFileDialog.getOpenFileName(
                        self.mainWindow, '选择文件')
                    if filePath[0] != '':
                        with open(filePath[0], encoding='utf-8') as f:
                            buffer = f.read()
                        if script == 'JSP':
                            r = jspCore.uploadFile(
                                url, password, buffer,
                                dir + os.path.basename(filePath[0]))
                        else:
                            r = phpCore.uploadFile(
                                url, password, buffer,
                                dir + os.path.basename(filePath[0]), useRSA)
                        if r == '1':
                            QtWidgets.QMessageBox.about(
                                self.mainWindow, "上传成功!", '文件已上传')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                        else:
                            QtWidgets.QMessageBox.about(
                                self.mainWindow, "上传失败!", '可能没有权限')

                except Exception as e:
                    QtWidgets.QMessageBox.about(self.mainWindow, "上传失败!",
                                                str(Exception(e)))
            elif action == item2:
                # 更新文件目录
                if script == 'JSP':
                    files = jspCore.scanDir(url, password, dir).split('\n')
                else:
                    files = phpCore.scanDir(url, password, dir,
                                            useRSA).split('\n')
                files = list(filter(None, files))
                self.updataTable(files, fileTableWidget)