def AddPasswordBooks(self):
     dialog = AddBookDialog('添加密码本', self.PswBooksManageVer)
     # 如果是正常结束,ok返回1,cancel返回o,关闭返回0
     # print dialog.exec_()
     if dialog.exec_():
         temp = dialog.getData()
         # 密码本管理文件的存储格式为dict, 密码本名:[MD5(password), MD5(密码本名,用于在PswBooksDb
         # 中命名密码本存储名)]
         # print temp
         self.PswBooksManageVer[temp[0]] = [
             funclibs.getMD5(temp[1]),
             funclibs.getMD5(temp[0])
         ]
         # 建立对应的PswFile
         self.filemodel.setPswBook({}, funclibs.getMD5(temp[0]))
         # 说明文件没有创建成功
         if self.filemodel.getPswBook(funclibs.getMD5(temp[0])) is None:
             QtWidgets.QMessageBox.information(self, u'错误', u'添加密码本错误')
             # 从Manage 管理变量中讲新添加的删除
             self.PswBooksManageVer.pop(temp[0])
         else:
             QtWidgets.QMessageBox.information(self, u'提示', u'添加密码本成功')
         # 将内容存入文件
         self.filemodel.setPswBooksManage(self.PswBooksManageVer)
         # 设置PswBooksList
         self.func.setPswBooksList()
         # 获取最新的PswBooksManageVer
         self.PswBooksManageVer = self.filemodel.getPswBooksManage()
     pass
     dialog.destroy()
 def accept(self):
     pswOldVal = self.lePswOld.text()
     pswNewVal = self.lePswNew.text()
     rePswNewVal = self.leRePswNew.text()
     if self.pswBookdata.has_key(self.bookName):
         # 获得原始密码校验值
         checkPsw = self.pswBookdata[self.bookName][0]
     else:
         checkPsw = ''
     # 获得输入原始密码的校验值
     pswOldValMd5 = funclibs.getMD5(pswOldVal)
     if pswOldVal == '':
         QtWidgets.QMessageBox.information(self, u'错误', u'请输入原始密码')
         self.lePswOld.setText('')
         self.lePswOld.setFocus()
     elif pswNewVal == '':
         QtWidgets.QMessageBox.information(self, u'错误', u'请输入新密码')
         self.lePswNew.setText('')
         self.leRePswNew.setText('')
         self.lePswNew.setFocus()
     elif checkPsw != pswOldValMd5:
         QtWidgets.QMessageBox.information(self, u'错误', u'请输入正确的原始密码')
         self.lePswOld.setText('')
         self.lePswOld.setFocus()
     elif pswNewVal != rePswNewVal:
         QtWidgets.QMessageBox.information(self, u'错误', u'输入两次新密码不一致')
         self.lePswNew.setText('')
         self.leRePswNew.setText('')
         self.lePswNew.setFocus()
     else:
         super(EditBookPswDialog, self).accept()
     pass
 def DeletePasswordBooks(self):
     delList = self.func.getCheckedItems(self.PswBooksList)
     # 记录待删除的文件个数
     delLen = len(delList)
     # 记录PswBooksManage的备份
     manageTemp = self.PswBooksManageVer.copy()
     # 标记位 ,flag=0表示删除成功,flag=1表示删除失败
     flag = 0
     # backupList 从dict中删除成功但是,删除文件时出错
     backupList = []
     for dit in delList:
         # 若有的单词本没有被删除,修改标记为1,不存在dict未删除且文件未删除的情况
         if self.PswBooksManageVer.pop(dit) is None:
             flag = 1
             backupList.append(dit)
         else:
             # 删除文件出错
             state = self.filemodel.removePswFile(funclibs.getMD5(dit))
             if state == 0:
                 flag = 1
                 backupList.append(dit)
     # 记录未删除的文件个数
     backupLen = len(backupList)
     # 说明有的单词本未被删除,需要回滚
     if flag == 1 and backupLen != 0:
         for bk in backupList:
             val = manageTemp[bk]
             self.PswBooksManageVer[bk] = val
     # 将当前的PSWBOOKSMANAGE状态进行写入文件
     self.filemodel.setPswBooksManage(self.PswBooksManageVer)
     self.func.setPswBooksList()
     self.PswBooksManageVer = self.filemodel.getPswBooksManage()
     QtWidgets.QMessageBox.information(
         self, u'提示', u'批量删除' + str(delLen - backupLen) + u'个密码本成功')
     pass
 def OpenPswBooksListItem(self):
     item = self.PswBooksList.currentItem()
     pswBooksName = item.text()
     if self.CurOpenPswBookName != '':
         self.CloseCurPasswordBooks()
     if self.filemodel.pswFileExist(funclibs.getMD5(pswBooksName)):
         dialog = QtWidgets.QInputDialog()
         text, ok = dialog.getText(self, u'输入密码',
                                   u'请输入(' + pswBooksName + u')的开启密码',
                                   QtWidgets.QLineEdit.Password)
         if ok:
             if funclibs.getMD5(
                     text) != self.PswBooksManageVer[pswBooksName][0]:
                 QtWidgets.QMessageBox.information(
                     self, u'错误', u'输入(' + pswBooksName + u')的密码错误')
             else:
                 pswData = self.filemodel.getPswBook(
                     funclibs.getMD5(pswBooksName))
                 if pswData is not None:
                     # QtWidgets.QMessageBox.information(self, u'正确', u'输入(' + pswBooksName + u')的密码正确')
                     self.PasswordDictVer = self.filemodel.getPswBook(
                         funclibs.getMD5(pswBooksName))
                     self.CurOpenPswBookName = pswBooksName
                     self.CurOpenPswBookNameMD5 = funclibs.getMD5(
                         pswBooksName)
                     if self.PasswordDictVer is not None:
                         self.PswBooksCloseBtn.setEnabled(True)
                         self.AddPswBtn.setEnabled(True)
                         self.PswSelectAllBtn.setEnabled(True)
                         self.PswClearAllBtn.setEnabled(True)
                         self.DeletePswBtn.setEnabled(True)
                         self.func.setPswListByData(self.PasswordDictVer)
                         # QtWidgets.QMessageBox.information(self, u'打开', u'打开密码本(' + pswBooksName + u')成功')
                     else:
                         self.PasswordDictVer = {}
                         QtWidgets.QMessageBox.information(
                             self, u'错误', u'打开密码本(' + pswBooksName + u')错误')
                 else:
                     QtWidgets.QMessageBox.information(
                         self, u'错误', u'密码本(' + pswBooksName + u')的密码文件不存在')
     else:
         QtWidgets.QMessageBox.information(
             self, u'错误', u'密码本(' + pswBooksName + u')的密码文件不存在')
     pass
 def ExportExcel(self):
     # exportList存储待导出的密码本名称
     exportList = self.func.getCheckedItems(self.PswBooksList)
     exportLen = len(exportList)
     # 设置编码格式为utf8,防止中文存储报错
     excelObj = xlwt.Workbook(encoding='utf-8')
     if len(exportList) > 0:
         filedialog = QtWidgets.QFileDialog()
         filepath = filedialog.getExistingDirectory(self, u'选择文件夹',
                                                    self.filemodel.CUR_DIR)
         for eL in exportList:
             if self.filemodel.pswFileExist(funclibs.getMD5(eL)):
                 dialog = QtWidgets.QInputDialog()
                 text, ok = dialog.getText(self, u'输入密码',
                                           u'请输入(' + eL + u')的开启密码',
                                           QtWidgets.QLineEdit.Password)
                 if ok:
                     if funclibs.getMD5(
                             text) != self.PswBooksManageVer[eL][0]:
                         QtWidgets.QMessageBox.information(
                             self, u'错误', u'输入(' + eL + u')的密码错误')
                         return
                     else:
                         pswBookName = self.PswBooksManageVer[eL][1]
                         pswBookDict = self.filemodel.getPswBook(
                             pswBookName)
                         sheet = excelObj.add_sheet(eL,
                                                    cell_overwrite_ok=True)
                         miyao = self.PswBooksManageVer[eL][0]
                         self.__writeSheet(sheet, pswBookDict, miyao)
                         QtWidgets.QMessageBox.information(
                             self, u'提示', eL + u'密码导出成功')
                 else:
                     QtWidgets.QMessageBox.information(
                         self, u'提示', u'密码本不存在')
             pass
         excelObj.save(
             os.path.join(
                 filepath, u'密码本-' +
                 str(datetime.today().strftime('%Y%m%d%H%M%S')) + '.xls'))
     else:
         QtWidgets.QMessageBox.information(self, u'提示', u'至少选择一个导出的密码本')
     pass
    def EditPswBooksListItemName(self):
        item = self.PswBooksList.currentItem()
        dialog = QtWidgets.QInputDialog()
        pswBooksNameOld = item.text()
        pswBooksNameNew, ok = dialog.getText(
            self, u'修改密码本名称', u'请输入密码本(' + pswBooksNameOld + u')的新名称')
        if ok:
            if self.PswBooksManageVer.has_key(pswBooksNameNew):
                QtWidgets.QMessageBox.information(
                    self, u'错误', u'该密码本(' + pswBooksNameNew + u')已存在,请重新命名')
            else:
                pswBooksNameOldMD5 = funclibs.getMD5(pswBooksNameOld)
                pswBooksNameNewMD5 = funclibs.getMD5(pswBooksNameNew)
                state = self.filemodel.renamePswFile(pswBooksNameOldMD5,
                                                     pswBooksNameNewMD5)
                manageTemp = self.PswBooksManageVer.copy()
                if state == -2:
                    QtWidgets.QMessageBox.information(
                        self, u'错误',
                        u'该密码本(' + pswBooksNameNew + u')已存在,请重新命名')
                elif state == -1:
                    QtWidgets.QMessageBox.information(
                        self, u'错误', u'原始密码本(' + pswBooksNameOld +
                        u')文件不存在。\n请删除后重新添加该密码本!')
                elif state == 0:
                    QtWidgets.QMessageBox.information(self, u'错误', u'重命名密码本失败')
                elif state == 1:
                    # 获取原来的名字对应值
                    newVal = self.PswBooksManageVer[pswBooksNameOld]
                    # 将新的密码本名的MD5值进行更改
                    newVal[1] = pswBooksNameNewMD5
                    # 将新修改的值加入manage中
                    self.PswBooksManageVer[pswBooksNameNew] = newVal
                    # 删除掉原来的名字对应的信息
                    self.PswBooksManageVer.pop(pswBooksNameOld)

                    self.filemodel.setPswBooksManage(self.PswBooksManageVer)
                    self.func.setPswBooksList()
                    self.PswBooksManageVer = self.filemodel.getPswBooksManage()
                    QtWidgets.QMessageBox.information(self, u'提示', u'密码本重命名成功')
        pass
 def EditPswBooksListItemPsw(self):
     item = self.PswBooksList.currentItem()
     bkName = item.text()
     dialog = EditBookPswDialog(u'修改开启密码', self.PswBooksManageVer, bkName)
     if dialog.exec_():
         pswNew = dialog.getData()
         self.PswBooksManageVer[bkName][0] = funclibs.getMD5(pswNew)
         self.filemodel.setPswBooksManage(self.PswBooksManageVer)
         self.PswBooksManageVer = self.filemodel.getPswBooksManage()
         self.func.setPswBooksList()
         QtWidgets.QMessageBox.information(self, u'提示',
                                           u'修改(' + bkName + u')的开启密码成功')
     dialog.destroy()
     pass
 def DeletePswBooksListItem(self):
     item = self.PswBooksList.currentItem()
     booksName = item.text()
     bookmanageTemp = self.PswBooksManageVer.copy()
     state = self.filemodel.removePswFile(funclibs.getMD5(booksName))
     if self.PswBooksManageVer.pop(booksName) is not None and state != 0:
         QtWidgets.QMessageBox.information(self, u'提示',
                                           booksName + u' 删除成功')
         self.filemodel.setPswBooksManage(self.PswBooksManageVer)
         self.func.setPswBooksList()
         self.PswBooksManageVer = self.filemodel.getPswBooksManage()
     else:
         QtWidgets.QMessageBox.information(self, u'提示',
                                           booksName + u' 删除失败')
         self.filemodel.setPswBooksManage(bookmanageTemp)
         self.func.setPswBooksList()
         self.PswBooksManageVer = bookmanageTemp.copy()
     pass
 def ImportExcelMenuFunc(self):
     if self.CurOpenPswBookName != '':
         self.CloseCurPasswordBooks()
     QtWidgets.QMessageBox.information(
         self, u'提示',
         u'确保导入的电子表格文件仅存在三列内容,且按照应用名,用户名,密码的顺序排列(超出三列部分将不进行存储)')
     filedialog = QtWidgets.QFileDialog()
     filepath, filetype = filedialog.getOpenFileName(
         self, u'打开待导入文件', os.path.curdir,
         'Excel 97-2003 工作簿(*.xls);;Excel 工作簿(*.xlsx)')
     if filepath != '':
         pswBooks = self.__readExcel(filepath)
         index = 0
         if pswBooks is not None:
             # 导入如果密码本存在,不弹出新建窗口,直接和之前的密码本合并,覆盖原始资料
             # 导入密码本如果不存在,弹出新建窗口,添加密码本
             for bookname in pswBooks.keys():
                 # 导入密码本存在
                 if self.PswBooksManageVer.has_key(bookname):
                     bookNameMd5 = self.PswBooksManageVer[bookname][1]
                     key = self.PswBooksManageVer[bookname][0]
                     dictTemp = funclibs.pswDictCipher(
                         pswBooks[bookname], key)
                     bookDictOri = self.filemodel.getPswBook(bookNameMd5)
                     bookDictOri.update(dictTemp)
                     self.filemodel.setPswBook(bookDictOri, bookNameMd5)
                     index += 1
                     pass
                 # 导入密码本不存在
                 else:
                     dialog = AddExortBookPswDialog(u'添加密码本',
                                                    self.PswBooksManageVer,
                                                    bookname)
                     # print pswBooks[bookname]
                     if dialog.exec_():
                         temp = dialog.getData()
                         # print temp
                         # print self.PswBooksManageVer
                         self.PswBooksManageVer[bookname] = [
                             funclibs.getMD5(temp[1]),
                             funclibs.getMD5(temp[0])
                         ]
                         self.filemodel.setPswBooksManage(
                             self.PswBooksManageVer)
                         self.PswBooksManageVer = self.filemodel.getPswBooksManage(
                         )
                         self.func.setPswBooksList()
                         # print self.PswBooksManageVer
                         key = funclibs.getMD5(temp[1])
                         dictTemp = funclibs.pswDictCipher(
                             pswBooks[bookname], key)  # 加密后的密码本dict
                         self.filemodel.setPswBook(dictTemp,
                                                   funclibs.getMD5(temp[0]))
                         # print dictTemp
                         index += 1
                         pass
                     dialog.destroy()
             QtWidgets.QMessageBox.information(
                 self, u'提示', u'成功导入' + str(index) + u'个密码本')
         else:
             QtWidgets.QMessageBox.information(self, u'提示',
                                               u'读入excel文件内容为空')
     else:
         QtWidgets.QMessageBox.information(self, u'提示', u'导入0个密码本')
         pass