Esempio n. 1
0
 def on_act_SaveAs_triggered(self):
     if len(self.ui.mdi.subWindowList()) > 0:  # 如果有打开的MDI窗口,获取活动窗口
         self.formDoc = self.ui.mdi.activeSubWindow().widget()
     curPath = QDir.currentPath()  # 获取系统当前目录
     title = "另存为一个文件"  # 对话框标题
     filt = "Python程序(*.py);; C++程序(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)"  # 文件过滤器
     fileName, flt = QFileDialog.getSaveFileName(self, title, curPath, filt)
     print(fileName)
     if fileName == "":
         return
     try:
         fileDevice = QFile(fileName)
         if not fileDevice.open(QIODevice.WriteOnly | QIODevice.Text):
             return False
         try:
             text = self.formDoc.tE.toPlainText()  # 返回str类型
             strBytes = text.encode("utf-8")  # str转换为bytes类型
             fileDevice.write(strBytes)  # 写入文件
         finally:
             fileDevice.close()
     except Exception as e:
         print(e)
         QMessageBox.critical(self, "错误", "保存文件失败")
     else:
         print("保存成功")
         self.formDoc.loadFromFile(fileName)
Esempio n. 2
0
    def saveFile(self):
        fileName, _ = QFileDialog.getSaveFileName(self, "Save file as", '',
                '*.cht')

        if fileName:
            f = QFile(fileName)

            if f.open(QFile.WriteOnly | QFile.Text):
                for row in range(self.model.rowCount(QModelIndex())):
                    pieces = []

                    pieces.append(
                            self.model.data(
                                    self.model.index(row, 0, QModelIndex()),
                                    Qt.DisplayRole))
                    pieces.append(
                            '%g' % self.model.data(
                                    self.model.index(row, 1, QModelIndex()),
                                    Qt.DisplayRole))
                    pieces.append(
                            self.model.data(
                                    self.model.index(row, 0, QModelIndex()),
                                    Qt.DecorationRole).name())

                    f.write(b','.join([p.encode('utf-8') for p in pieces]))
                    f.write(b'\n')

            f.close()
            self.statusBar().showMessage("Saved %s" % fileName, 2000)
Esempio n. 3
0
 def received_file(self):
     """ 接收到文件 """
     reply = self.sender()
     # 从路径中解析要保存的位置
     request_url = reply.request().url().url()
     split_ = request_url.split("/{}/".format(self.sys_bit))
     save_file_path = os.path.join(BASE_DIR, split_[1])
     # 文件夹不存在创建
     save_dir = os.path.split(save_file_path)[0]
     if not os.path.exists(save_dir):
         os.makedirs(save_dir)
     file_data = reply.readAll()
     if reply.error():
         # print("更新错误", reply.error())
         self.update_error = True
     file_obj = QFile(save_file_path)
     is_open = file_obj.open(QFile.WriteOnly)
     if is_open:
         file_obj.write(file_data)
         file_obj.close()
     else:
         self.update_error = True
     self.current_count += 1
     self.update_process_bar.setValue(self.current_count)
     reply.deleteLater()
     if self.current_count >= self.update_count:
         self.update_finished_restart_app()
Esempio n. 4
0
    def on_act_Save_triggered(self):  # 如果当前路径没有这个标题的文件就调出getfiledialog
        if len(self.ui.mdi.subWindowList()) > 0:  # 如果有打开的MDI窗口,获取活动窗口
            self.formDoc = self.ui.mdi.activeSubWindow().widget()

        # 从这里开始取文件路径 然后就是setcurrentpath 我觉得可以搞一个绝对路径
        # 先取出这个子窗口的 文件绝对路径 然后追加
        # 当然也要判断这个 文件绝对路径是否存在啊 因为是有这个无标题存在的
        if len(self.formDoc.currentFilePath):
            print(self.formDoc.currentFilePath)

        cursubwidget = self.formDoc.currentFilePath
        if QFile.exists(cursubwidget):  # 存在就直接保存
            print(cursubwidget)
            fileDevice = QFile(cursubwidget)
            if not fileDevice.open(QIODevice.WriteOnly | QIODevice.Text):
                return False
            try:
                text = self.formDoc.tE.toPlainText()  # 返回str类型
                strBytes = text.encode("utf-8")  # str转换为bytes类型
                fileDevice.write(strBytes)  # 写入文件
            finally:
                fileDevice.close()
            return True
        else:
            self.on_act_SaveAs_triggered()  # 如果当前路径不存在
Esempio n. 5
0
 def rank_source_file_reply(self):
     """ 获取日排名请求返回 """
     reply = self.sender()
     request_url = reply.request().url().url()
     # 解析出请求的品种
     request_filename = request_url.rsplit("/", 1)[1]
     request_variety = request_filename.split("_")[0]
     if reply.error():
         reply.deleteLater()
         self.spider_finished.emit(
             "获取{}排名数据文件。\n失败:{}".format(request_variety[:2],
                                         str(reply.error())), True)
         logger.error("获取{}排名数据文件失败了!".format(request_url[:2]))
         return
     save_path = os.path.join(
         LOCAL_SPIDER_SRC,
         'cffex/rank/{}_{}.csv'.format(request_variety,
                                       self.date.strftime("%Y-%m-%d")))
     file_data = reply.readAll()
     file_obj = QFile(save_path)
     is_open = file_obj.open(QFile.WriteOnly)
     if is_open:
         file_obj.write(file_data)
         file_obj.close()
     reply.deleteLater()
     tip = "获取中金所{}_{}日持仓排名数据保存到文件成功!".format(
         request_variety, self.date.strftime("%Y-%m-%d"))
     if request_variety == "T":
         tip = "获取中金所{}日所有品种持仓排名数据保存到文件成功!".format(
             self.date.strftime("%Y-%m-%d"))
     self.spider_finished.emit(tip, True)
     self.event_loop.quit()
Esempio n. 6
0
    def saveFile(self):
        fileName, _ = QFileDialog.getSaveFileName(self, "Save file as", "",
                                                  "*.cht")

        if fileName:
            f = QFile(fileName)

            if f.open(QFile.WriteOnly | QFile.Text):
                for row in range(self.model.rowCount(QModelIndex())):
                    pieces = []

                    pieces.append(
                        self.model.data(
                            self.model.index(row, 0, QModelIndex()),
                            Qt.DisplayRole))
                    pieces.append("%g" % self.model.data(
                        self.model.index(row, 1, QModelIndex()),
                        Qt.DisplayRole))
                    pieces.append(
                        self.model.data(
                            self.model.index(row, 0, QModelIndex()),
                            Qt.DecorationRole).name())

                    f.write(b",".join([p.encode("utf-8") for p in pieces]))
                    f.write(b"\n")

            f.close()
            self.statusBar().showMessage("Saved %s" % fileName, 2000)
Esempio n. 7
0
    def save(self, content, path=None):
        """
        Write a temporary file with .tnj extension and copy it over the
        original one.
        .nsf = Ninja Swap File
        # FIXME: Where to locate addExtension, does not fit here
        """
        new_path = False
        if path:
            self.attach_to_path(path)
            new_path = True

        save_path = self._file_path

        if not save_path:
            raise NinjaNoFileNameException("I am asked to write a "
                                           "file but no one told me where")
        swap_save_path = "%s.nsp" % save_path

        # If we have a file system watcher, remove the file path
        # from its watch list until we are done making changes.
        if self.__watcher is not None:
            self.__watcher.removePath(save_path)

        flags = QIODevice.WriteOnly | QIODevice.Truncate
        f = QFile(swap_save_path)
        if settings.use_platform_specific_eol():
            flags |= QIODevice.Text

        if not f.open(flags):
            raise NinjaIOException(f.errorString())

        stream = QTextStream(f)
        encoding = get_file_encoding(content)
        if encoding:
            stream.setCodec(encoding)

        encoded_stream = stream.codec().fromUnicode(content)
        f.write(encoded_stream)
        f.flush()
        f.close()
        # SIGNAL: Will save (temp, definitive) to warn folder to do something
        self.willSave.emit(swap_save_path, save_path)
        self.__mtime = os.path.getmtime(swap_save_path)
        shutil.move(swap_save_path, save_path)
        self.reset_state()

        # If we have a file system watcher, add the saved path back
        # to its watch list, otherwise create a watcher and start
        # watching
        if self.__watcher is not None:
            if new_path:
                # FIXME: what?
                # self.__watcher.removePath(self.__watcher.files()[0])
                self.__watcher.addPath(self._file_path)
            else:
                self.__watcher.addPath(save_path)
        else:
            self.start_watching()
        return self
Esempio n. 8
0
    def saveFile(self):
        fileName, _ = QFileDialog.getSaveFileName(self, "Save file as", '',
                                                  '*.cht')

        if fileName:
            f = QFile(fileName)

            if f.open(QFile.WriteOnly | QFile.Text):
                for row in range(self.model.rowCount(QModelIndex())):
                    pieces = []

                    pieces.append(
                        self.model.data(
                            self.model.index(row, 0, QModelIndex()),
                            Qt.DisplayRole))
                    pieces.append(
                        str(
                            self.model.data(
                                self.model.index(row, 1, QModelIndex()),
                                Qt.DisplayRole)))
                    pieces.append(
                        self.model.data(
                            self.model.index(row, 0, QModelIndex()),
                            Qt.DecorationRole).name())

                    f.write(QByteArray(','.join(pieces)))
                    f.write('\n')

            f.close()
            self.statusBar().showMessage("Saved %s" % fileName, 2000)
Esempio n. 9
0
    def write(self):
        data = json.dumps(self.json_object, sort_keys=True, indent=4)
        out = QFile(self.path)

        if out.open(QIODevice.WriteOnly | QIODevice.Text):
            out.write(bytes(data, encoding="utf-8"))
            out.close()
Esempio n. 10
0
    def save(self, content, path=None):
        """
        Write a temporary file with .tnj extension and copy it over the
        original one.
        .nsf = Ninja Swap File
        # FIXME: Where to locate addExtension, does not fit here
        """
        new_path = False
        if path:
            self.attach_to_path(path)
            new_path = True

        save_path = self._file_path

        if not save_path:
            raise NinjaNoFileNameException("I am asked to write a "
                                           "file but no one told me where")
        swap_save_path = "%s.nsp" % save_path

        # If we have a file system watcher, remove the file path
        # from its watch list until we are done making changes.
        if self.__watcher is not None:
            self.__watcher.removePath(save_path)

        flags = QIODevice.WriteOnly | QIODevice.Truncate
        f = QFile(swap_save_path)
        if settings.use_platform_specific_eol():
            flags |= QIODevice.Text

        if not f.open(flags):
            raise NinjaIOException(f.errorString())

        stream = QTextStream(f)
        encoding = get_file_encoding(content)
        if encoding:
            stream.setCodec(encoding)

        encoded_stream = stream.codec().fromUnicode(content)
        f.write(encoded_stream)
        f.flush()
        f.close()
        # SIGNAL: Will save (temp, definitive) to warn folder to do something
        self.willSave.emit(swap_save_path, save_path)
        self.__mtime = os.path.getmtime(swap_save_path)
        shutil.move(swap_save_path, save_path)
        self.reset_state()

        # If we have a file system watcher, add the saved path back
        # to its watch list, otherwise create a watcher and start
        # watching
        if self.__watcher is not None:
            if new_path:
                # self.__watcher.removePath(self.__watcher.files()[0])
                self.__watcher.addPath(self._file_path)
            else:
                self.__watcher.addPath(save_path)
        else:
            self.start_watching()
        return self
Esempio n. 11
0
 def save(self, data):
     f = QFile(self.savePath)
     f.open(QIODevice.WriteOnly)
     data = data.readAll()
     f.write(data)
     f.flush()
     f.close()
     self.downloaded.emit(self.savePath)
Esempio n. 12
0
    def _fileSave(self):
        file = QFile(self.file_path)
        if file.open(QIODevice.WriteOnly | QIODevice.Text):
            file.write(bytes(self.text(), encoding="utf-8"))
            file.flush()
            file.close()

        self.saveState = True
Esempio n. 13
0
    async def showImage(self, ipfsop, imgPath, fromBlock=False, timeout=10):
        try:
            if fromBlock is True:
                imgData = await ipfsop.waitFor(
                    ipfsop.client.block.get(imgPath), timeout)
            else:
                imgData = await ipfsop.waitFor(ipfsop.catObject(imgPath),
                                               timeout)

            if not imgData:
                raise Exception('Failed to load image')

            mimeType, statInfo = await self.app.rscAnalyzer(IPFSPath(imgPath))

            if mimeType is None:
                raise Exception('Failed to load image')

            stat = StatInfo(statInfo)

            if mimeType.isAnimation:
                fp = self.app.tempDir.filePath(stat.cid)
                tmpFile = QFile(fp)

                if not tmpFile.exists() and tmpFile.open(QFile.ReadWrite):
                    tmpFile.write(imgData)
                    tmpFile.close()

                self.clip.stop()
                self.clip.setFileName(fp)
                self.labelImage.setMovie(self.clip)
                self.clip.frameChanged.connect(self.onMovieFrameChanged)
                self.clip.error.connect(self.onMovieError)
                self.clip.start()
                self.labelImage.adjustSize()
            else:
                img = QImage()
                img.loadFromData(imgData)
                self.image = img

                self.labelImage.setPixmap(QPixmap.fromImage(self.image))
                self.labelImage.adjustSize()
                self.resizePixmap()

                if self.qrDecoder:
                    # See if we have any QR codes in the image
                    urls = self.qrDecoder.decode(imgData)
                    if urls:
                        self.qrCodesPresent.emit(urls)

            self.labelImage.adjustSize()
            self.currentImgPath = IPFSPath(imgPath)
            self.imageLoaded.emit(self.currentImgPath, mimeType)

        except Exception:
            logUser.debug('Failed to load image: {path}'.format(path=imgPath))
            messageBox(iImageCannotLoad(imgPath))
Esempio n. 14
0
 def __downloadRepositoryFileDone(self):
     """
     Private method called after the repository file was downloaded.
     """
     reply = self.sender()
     if reply in self.__replies:
         self.__replies.remove(reply)
     if reply.error() != QNetworkReply.NoError:
         E5MessageBox.warning(
             None,
             self.tr("Error downloading file"),
             self.tr(
                 """<p>Could not download the requested file"""
                 """ from {0}.</p><p>Error: {1}</p>"""
             ).format(Preferences.getUI("PluginRepositoryUrl6"),
                      reply.errorString())
         )
         return
     
     ioDevice = QFile(self.pluginRepositoryFile + ".tmp")
     ioDevice.open(QIODevice.WriteOnly)
     ioDevice.write(reply.readAll())
     ioDevice.close()
     if QFile.exists(self.pluginRepositoryFile):
         QFile.remove(self.pluginRepositoryFile)
     ioDevice.rename(self.pluginRepositoryFile)
     
     if os.path.exists(self.pluginRepositoryFile):
         f = QFile(self.pluginRepositoryFile)
         if f.open(QIODevice.ReadOnly):
             # save current URL
             url = Preferences.getUI("PluginRepositoryUrl6")
             
             # read the repository file
             from E5XML.PluginRepositoryReader import PluginRepositoryReader
             reader = PluginRepositoryReader(f, self.checkPluginEntry)
             reader.readXML()
             if url != Preferences.getUI("PluginRepositoryUrl6"):
                 # redo if it is a redirect
                 self.checkPluginUpdatesAvailable()
                 return
             
             if self.__updateAvailable:
                 res = E5MessageBox.information(
                     None,
                     self.tr("New plugin versions available"),
                     self.tr("<p>There are new plug-ins or plug-in"
                             " updates available. Use the plug-in"
                             " repository dialog to get them.</p>"),
                     E5MessageBox.StandardButtons(
                         E5MessageBox.Ignore |
                         E5MessageBox.Open),
                     E5MessageBox.Open)
                 if res == E5MessageBox.Open:
                     self.__ui.showPluginsAvailable()
Esempio n. 15
0
 def save_project_data(self) -> None:
     """Saves project configuration data to permanent storage"""
     try:
         file = QFile()
         file.setFileName(self.root_path + "/.manuwrite/project.toml")
         file.open(QFile.WriteOnly)
         file.write(toml.dumps(self.project_info).encode())
     except OSError:
         raise ProjectError("Failed to write project configuration data")
     finally:
         file.close()
Esempio n. 16
0
 def __saveByIODevice(self, fileName):
     fileDevice = QFile(fileName)
     if not fileDevice.open(QIODevice.WriteOnly | QIODevice.Text):
         return False
     try:
         text = self.ui.receiveText.toPlainText()
         strBytes = text.encode("utf-8")  # str转换为bytes类型
         fileDevice.write(strBytes)  #写入文件
     finally:
         fileDevice.close()
     return True
Esempio n. 17
0
 def _autosave(self):
     if self._neditable.editor.is_modified:
         flags = QIODevice.WriteOnly
         f = QFile(self.filename())
         if not f.open(flags):
             raise IOError(f.errorString())
         content = self._neditable.editor.text
         stream = QTextStream(f)
         encoded_stream = stream.codec().fromUnicode(content)
         f.write(encoded_stream)
         f.flush()
         f.close()
Esempio n. 18
0
    def saveToDisk(self, filename, data):
        fi = "%s/%s" % (self.dir_, filename)
        if not os.path.exists(self.dir_):
            os.makedirs(self.dir_)
        file = QFile(fi)
        if not file.open(QIODevice.WriteOnly):
            return False

        file.write(data.readAll())
        file.close()

        return True
Esempio n. 19
0
    def saveToDisk(self, filename, data):
        fi = "%s/%s" % (self.dir_, filename)
        if not os.path.exists(self.dir_):
            os.makedirs(self.dir_)
        file = QFile(fi)
        if not file.open(QIODevice.WriteOnly):
            return False

        file.write(data.readAll())
        file.close()

        return True
Esempio n. 20
0
 def _autosave(self):
     if self._neditable.editor.is_modified:
         flags = QIODevice.WriteOnly
         f = QFile(self.filename())
         if not f.open(flags):
             raise IOError(f.errorString())
         content = self._neditable.editor.text
         stream = QTextStream(f)
         encoded_stream = stream.codec().fromUnicode(content)
         f.write(encoded_stream)
         f.flush()
         f.close()
Esempio n. 21
0
 def __rulesDownloaded(self):
     """
     Private slot to deal with the downloaded rules.
     """
     reply = self.sender()
     
     response = reply.readAll()
     reply.close()
     self.__downloading = None
     
     if reply.error() != QNetworkReply.NoError:
         if not self.__defaultSubscription:
             # don't show error if we try to load the default
             E5MessageBox.warning(
                 None,
                 self.tr("Downloading subscription rules"),
                 self.tr(
                     """<p>Subscription rules could not be"""
                     """ downloaded.</p><p>Error: {0}</p>""")
                 .format(reply.errorString()))
         else:
             # reset after first download attempt
             self.__defaultSubscription = False
         return
     
     if response.isEmpty():
         E5MessageBox.warning(
             None,
             self.tr("Downloading subscription rules"),
             self.tr("""Got empty subscription rules."""))
         return
     
     fileName = self.rulesFileName()
     QFile.remove(fileName)
     f = QFile(fileName)
     if not f.open(QIODevice.ReadWrite):
         E5MessageBox.warning(
             None,
             self.tr("Downloading subscription rules"),
             self.tr(
                 """Unable to open adblock file '{0}' for writing.""")
             .file(fileName))
         return
     f.write(response)
     f.close()
     self.__lastUpdate = QDateTime.currentDateTime()
     if self.__validateCheckSum(fileName):
         self.__loadRules()
     else:
         QFile.remove(fileName)
     self.__downloading = None
     reply.deleteLater()
Esempio n. 22
0
 def __saveImage(self):
     """
     Private slot to save the selected image to disk.
     """
     act = self.sender()
     index = act.data()
     itm = self.imagesTree.topLevelItem(index)
     if itm is None:
         return
     
     imageUrl = QUrl(itm.text(1))
     if not imageUrl.host():
         imageUrl.setHost(QUrl(self.siteAddressLabel.text()).host())
         imageUrl.setScheme(QUrl(self.siteAddressLabel.text()).scheme())
     
     import Helpviewer.HelpWindow
     cache = Helpviewer.HelpWindow.HelpWindow.networkAccessManager().cache()
     if cache:
         cacheData = cache.data(imageUrl)
     else:
         cacheData = None
     if not cacheData:
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr("""This image is not available."""))
         return
     
     downloadDirectory = Helpviewer.HelpWindow.HelpWindow\
         .downloadManager().downloadDirectory()
     fn = os.path.join(downloadDirectory, os.path.basename(itm.text(1)))
     filename = E5FileDialog.getSaveFileName(
         self,
         self.tr("Save Image"),
         fn,
         self.tr("All Files (*)"),
         E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
     
     if not filename:
         return
     
     f = QFile(filename)
     if not f.open(QFile.WriteOnly):
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr(
                 """<p>Cannot write to file <b>{0}</b>.</p>""")
             .format(filename))
         return
     f.write(cacheData.readAll())
     f.close()
Esempio n. 23
0
 def __saveImage(self):
     """
     Private slot to save the selected image to disk.
     """
     act = self.sender()
     index = act.data()
     itm = self.imagesTree.topLevelItem(index)
     if itm is None:
         return
     
     imageUrl = QUrl(itm.text(1))
     if not imageUrl.host():
         imageUrl.setHost(QUrl(self.siteAddressLabel.text()).host())
         imageUrl.setScheme(QUrl(self.siteAddressLabel.text()).scheme())
     
     import Helpviewer.HelpWindow
     cache = Helpviewer.HelpWindow.HelpWindow.networkAccessManager().cache()
     if cache:
         cacheData = cache.data(imageUrl)
     else:
         cacheData = None
     if not cacheData:
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr("""This image is not available."""))
         return
     
     downloadDirectory = Helpviewer.HelpWindow.HelpWindow\
         .downloadManager().downloadDirectory()
     fn = os.path.join(downloadDirectory, os.path.basename(itm.text(1)))
     filename = E5FileDialog.getSaveFileName(
         self,
         self.tr("Save Image"),
         fn,
         self.tr("All Files (*)"),
         E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
     
     if not filename:
         return
     
     f = QFile(filename)
     if not f.open(QFile.WriteOnly):
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr(
                 """<p>Cannot write to file <b>{0}</b>.</p>""")
             .format(filename))
         return
     f.write(cacheData.readAll())
     f.close()
Esempio n. 24
0
    def __saveByIODevice(self, fileName):
        fileDevice = QFile(fileName)
        if not fileDevice.open(QIODevice.ReadOnly | QIODevice.Text):
            return False

        try:
            text = self.ui.textEdit.toPlainText()
            strBytes = text.encode("utf-8")
            fileDevice.write(strBytes)
        finally:
            fileDevice.close()

        return True
Esempio n. 25
0
    def save_as(self, filename):
        if not self.modified:
            return

        out_file = QFile(filename)
        out_file.open(QFile.WriteOnly)
        out_file.write(self.text.encode())
        out_file.close()

        self.modified = False
        self.filename = filename

        self._set_window_title(filename)
Esempio n. 26
0
    def __exportCertificate(self, name, cert):
        """
        Private slot to export a certificate.
        
        @param name default file name without extension (string)
        @param cert certificate to be exported (QSslCertificate)
        """
        if cert is not None:
            fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
                self,
                self.tr("Export Certificate"),
                name,
                self.tr("Certificate File (PEM) (*.pem);;" "Certificate File (DER) (*.der)"),
                None,
                E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite),
            )

            if fname:
                ext = QFileInfo(fname).suffix()
                if not ext or ext not in ["pem", "der"]:
                    ex = selectedFilter.split("(*")[1].split(")")[0]
                    if ex:
                        fname += ex
                if QFileInfo(fname).exists():
                    res = E5MessageBox.yesNo(
                        self,
                        self.tr("Export Certificate"),
                        self.tr("<p>The file <b>{0}</b> already exists." " Overwrite it?</p>").format(fname),
                        icon=E5MessageBox.Warning,
                    )
                    if not res:
                        return

                f = QFile(fname)
                if not f.open(QIODevice.WriteOnly):
                    E5MessageBox.critical(
                        self,
                        self.tr("Export Certificate"),
                        self.tr(
                            """<p>The certificate could not be written""" """ to file <b>{0}</b></p><p>Error: {1}</p>"""
                        ).format(fname, f.errorString()),
                    )
                    return

                if fname.endswith(".pem"):
                    crt = cert.toPem()
                else:
                    crt = cert.toDer()
                f.write(crt)
                f.close()
Esempio n. 27
0
    def speak(self, text):
        """
        Transform a text into audio

        Retrieve text and send a signal to :class:`surirobot.services.audioplayer.AudioPlayer`
        'LOCAL_VOICE' environment variable influences the behaviour of this function
        Parameters
        ----------
        text : str
            Well.. the text.

        Returns
        -------
        None
            This function only send a signal
        """
        if self.local_voice:
            # Play the audio directly
            # audio_file = self.TMP_DIR + format(uuid.uuid4()) + ".wav"
            # tts = gTTS(text=text, lang="fr", slow=False)
            # tts.save(data_audio)
            # self.play_sound.emit(audio_file)
            self.play_sound.emit(text, True)
        else:
            # Json request
            data = {'text': text, 'language': self.DEFAULT_LANGUAGE_EXT}
            data = json.dumps(data)
            r = requests.post(self.url + '/tts/speak',
                              data=data,
                              headers={'Content-Type': 'application/json'})
            # Receive response
            if r.status_code != 200:
                self.logger.error(
                    'HTTP {} error occurred while retrieving audio.\n{}'.
                    format(r.status_code, r.content))
                self.signal_indicator.emit("converse", "red")
            else:
                # Audio
                filename = self.TMP_DIR + str(uuid.uuid4()) + ".wav"
                file = QFile(filename)
                if not file.open(QIODevice.WriteOnly):
                    self.logger.error(
                        "Could not create file : {}".format(filename))
                    return
                file.write(r.content)
                self.logger.info(
                    "Sound file generated at : {}".format(filename))
                file.close()
                # Play the audio
                self.play_sound.emit(filename, False)
Esempio n. 28
0
	def saveFile(self, sourceModel, filename):
		saveResult = None
		search = SettingStruct.DeepSearchSettingTree(sourceModel)
		doc = Document()
		for childTreeEntryTupel in search.getElementGenerator():
			settings = childTreeEntryTupel[1].data(1).getDependSettings()
			for setting in settings:
				fileInfo = setting.fileInformation
				element = doc
				for path in fileInfo.xmlpath:
					found = False
					for node in reversed(element.childNodes):
						if node.nodeType == node.ELEMENT_NODE:
							if node.tagName == path:
								if not path is fileInfo.xmlpath[-1]:
									found = True
									element = node
								else:
									if fileInfo.attribute != "":
										if not node.hasAttribute(fileInfo.attribute):
											found = True
											element = node
									if len(node.childNodes) == 0:
										found = True
										element = node
								break
					if not found:
						newElement = doc.createElement(path)
						element.appendChild(newElement)
						element = newElement
				
				if fileInfo.attribute != "":
					element.setAttribute(fileInfo.attribute, setting.value)
				else:
					if setting.value != "":
						textNode = doc.createTextNode(setting.value)
						element.appendChild(textNode)
		prettyXML = doc.toprettyxml(indent="  ", newl='\n', encoding="UTF-8")
		try:
			file = QFile(filename)
			if file.open(QIODevice.WriteOnly):
				file.write(prettyXML)
				file.close()
			else:
				saveResult = ""
		except IOError as e:
			saveResult = str(e)
		finally:
			doc.unlink()
			return saveResult
 def __exportCertificate(self, name, cert):
     """
     Private slot to export a certificate.
     
     @param name default file name without extension (string)
     @param cert certificate to be exported (QSslCertificate)
     """
     if cert is not None:
         fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
             self,
             self.tr("Export Certificate"),
             name,
             self.tr("Certificate File (PEM) (*.pem);;"
                     "Certificate File (DER) (*.der)"),
             None,
             E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
         
         if fname:
             ext = QFileInfo(fname).suffix()
             if not ext or ext not in ["pem", "der"]:
                 ex = selectedFilter.split("(*")[1].split(")")[0]
                 if ex:
                     fname += ex
             if QFileInfo(fname).exists():
                 res = E5MessageBox.yesNo(
                     self,
                     self.tr("Export Certificate"),
                     self.tr("<p>The file <b>{0}</b> already exists."
                             " Overwrite it?</p>").format(fname),
                     icon=E5MessageBox.Warning)
                 if not res:
                     return
             
             f = QFile(fname)
             if not f.open(QIODevice.WriteOnly):
                 E5MessageBox.critical(
                     self,
                     self.tr("Export Certificate"),
                     self.tr(
                         """<p>The certificate could not be written"""
                         """ to file <b>{0}</b></p><p>Error: {1}</p>""")
                     .format(fname, f.errorString()))
                 return
             
             if fname.endswith(".pem"):
                 crt = cert.toPem()
             else:
                 crt = cert.toDer()
             f.write(crt)
             f.close()
Esempio n. 30
0
class StreamHandler(QObject):

    commandReceived = pyqtSignal(str)
    
    def __init__(self, parent = None):
    
        QObject.__init__(self, parent)
    
    def openInput(self):
    
        self.stdin = QFile()
        self.stdin.open(0, QFile.ReadOnly)
    
    def openOutput(self):
    
        self.stdout = QFile()
        self.stdout.open(1, QFile.WriteOnly)
    
    def handleInput(self):
    
        while self.stdin.isOpen():
        
            command = str(self.stdin.readLine(), "utf8")
            if not command:
                break
            
            self.commandReceived.emit(command.strip())
            
            # This shouldn't be needed, but QFile can get into a state where
            # readLine no longer blocks.
        
        print("Input stream closed.")
    
    @pyqtSlot(QByteArray)
    def handleOutput(self, data):
    
        while self.stdout.isOpen() and data.size() > 0:
        
            written = self.stdout.write(data)
            data = data.right(data.size() - written)
    
    @pyqtSlot(str)
    def handleOutput(self, data):
    
        data = QByteArray(bytes(data, "utf8"))
        while self.stdout.isOpen() and data.size() > 0:
        
            written = self.stdout.write(data)
            self.stdout.flush()
            data = data.right(data.size() - written)
Esempio n. 31
0
    def uninit(self):
        json = {}
        json["HighestScore"] = self.highestScore  #写入最高分
        json["HighestScorePlayer"] = self.highestScorePlayer  #写入最高分保持者
        json["ThemePath"] = self.themePath  #写入主题文件路径

        jsonDocument = QJsonDocument()
        jsonDocument.setObject(json)
        b = jsonDocument.toJson()

        #写入json
        f = QFile("config.ini")
        f.open(QFile.WriteOnly | QFile.Truncate)
        f.write(b)
        f.close()
Esempio n. 32
0
 def rank_source_file_reply(self):
     reply = self.sender()
     if reply.error():
         reply.deleteLater()
         self.spider_finished.emit("失败:" + str(reply.error()), True)
         return
     save_path = os.path.join(LOCAL_SPIDER_SRC, 'czce/rank/{}.xls'.format(self.date.strftime("%Y-%m-%d")))
     file_data = reply.readAll()
     file_obj = QFile(save_path)
     is_open = file_obj.open(QFile.WriteOnly)
     if is_open:
         file_obj.write(file_data)
         file_obj.close()
     reply.deleteLater()
     self.spider_finished.emit("获取郑商所{}日持仓排名数据源文件成功!".format(self.date.strftime("%Y-%m-%d")), True)
Esempio n. 33
0
	def writeConfiguration(self):
		if self.parent != None:
			try:
				lastfile = self.parent.lastFile
				windowRect = self.parent.geometry()
				windowpositionx = str(windowRect.x())
				windowpositiony = str(windowRect.y())
				windowsizex = str(windowRect.width())
				windowsizey = str(windowRect.height())
				
				configDOM = xml.dom.minidom.Document()
				configElement = configDOM.createElement("apeconfig")
				configDOM.appendChild(configElement)
				
				element = configDOM.createElement("lastfile")
				configElement.appendChild(element)
				configText = configDOM.createTextNode(lastfile)
				element.appendChild(configText)
				
				element = configDOM.createElement("windowpositionx")
				configElement.appendChild(element)
				configText = configDOM.createTextNode(windowpositionx)
				element.appendChild(configText)
				
				element = configDOM.createElement("windowpositiony")
				configElement.appendChild(element)
				configText = configDOM.createTextNode(windowpositiony)
				element.appendChild(configText)
				
				element = configDOM.createElement("windowsizex")
				configElement.appendChild(element)
				configText = configDOM.createTextNode(windowsizex)
				element.appendChild(configText)
				
				element = configDOM.createElement("windowsizey")
				configElement.appendChild(element)
				configText = configDOM.createTextNode(windowsizey)
				element.appendChild(configText)
				
				prettyXML = configDOM.toprettyxml(indent="  ", newl='\n', encoding="UTF-8")
				file = QFile(self.confFile)
				if file.open(QIODevice.WriteOnly):
					file.write(prettyXML)
					file.close()
				configDOM.unlink()
				
			except Exception:
				pass
Esempio n. 34
0
def copy_embedded_file(src_name, dst_name, macros={}):
    """ Copy an embedded source file to a destination file.  src_name is the
    name of the source file.  dst_name is the name of the destination file.
    macros is an optional dictionary of key/value string macros and instances
    of each key are replaced by the corresponding value.  A UserException is
    raised if there was an error.
    """

    contents = read_embedded_file(src_name)

    for key, value in macros.items():
        contents.replace(bytes(key, encoding='ascii'),
                         bytes(value, encoding='ascii'))

    dst_file = QFile(dst_name)

    if not dst_file.open(QIODevice.WriteOnly | QIODevice.Text):
        raise UserException(
            "Unable to create file {0}.".format(dst_file.fileName()),
            dst_file.errorString())

    if dst_file.write(contents) < 0:
        raise UserException(
            "Unable to write to file {0}.".format(dst_file.fileName()),
            dst_file.errorString())

    dst_file.close()
Esempio n. 35
0
def copy_embedded_file(src_name, dst_name, macros={}):
    """ Copy an embedded source file to a destination file.  src_name is the
    name of the source file.  dst_name is the name of the destination file.
    macros is an optional dictionary of key/value string macros and instances
    of each key are replaced by the corresponding value.  A UserException is
    raised if there was an error.
    """

    contents = read_embedded_file(src_name)

    for key, value in macros.items():
        contents.replace(bytes(key, encoding='ascii'),
                bytes(value, encoding='ascii'))

    dst_file = QFile(dst_name)

    if not dst_file.open(QIODevice.WriteOnly|QIODevice.Text):
        raise UserException(
                "Unable to create file {0}.".format(dst_file.fileName()),
                dst_file.errorString())

    if dst_file.write(contents) < 0:
        raise UserException(
                "Unable to write to file {0}.".format(dst_file.fileName()),
                dst_file.errorString())

    dst_file.close()
Esempio n. 36
0
    def __rulesDownloaded(self):
        """
        Private slot to deal with the downloaded rules.
        """
        reply = self.sender()

        response = reply.readAll()
        reply.close()
        self.__downloading = None

        if reply.error() != QNetworkReply.NoError:
            if not self.__defaultSubscription:
                # don't show error if we try to load the default
                E5MessageBox.warning(
                    None, self.tr("Downloading subscription rules"),
                    self.tr("""<p>Subscription rules could not be"""
                            """ downloaded.</p><p>Error: {0}</p>""").format(
                                reply.errorString()))
            else:
                # reset after first download attempt
                self.__defaultSubscription = False
            return

        if response.isEmpty():
            E5MessageBox.warning(None,
                                 self.tr("Downloading subscription rules"),
                                 self.tr("""Got empty subscription rules."""))
            return

        fileName = self.rulesFileName()
        QFile.remove(fileName)
        f = QFile(fileName)
        if not f.open(QIODevice.ReadWrite):
            E5MessageBox.warning(
                None, self.tr("Downloading subscription rules"),
                self.tr("""Unable to open adblock file '{0}' for writing.""").
                file(fileName))
            return
        f.write(response)
        f.close()
        self.__lastUpdate = QDateTime.currentDateTime()
        if self.__validateCheckSum(fileName):
            self.__loadRules()
        else:
            QFile.remove(fileName)
        self.__downloading = None
        reply.deleteLater()
Esempio n. 37
0
    def _copy_file(self, uid: str = "", source_path: str = "", dest_path: str = ""):
        self._current_uid = uid

        source_file = QFile(source_path)
        dest_file = QFile(dest_path)

        if not source_file.open(QFile.ReadOnly):
            self.copy_error.emit(uid, FileCopier.CannotOpenSourceFile)
            return

        dest_file_info = QFileInfo(dest_file)
        dest_dir = dest_file_info.absoluteDir()
        if not dest_dir.exists():
            if not dest_dir.mkpath(dest_dir.absolutePath()):
                self.copy_error.emit(uid, FileCopier.CannotCreateDestinationDirectory)
                return

        if not dest_file.open(QFile.WriteOnly):
            ic(dest_path, dest_file.errorString())
            self.copy_error.emit(uid, FileCopier.CannotOpenDestinationFile)
            return

        progress: int = 0
        total: int = source_file.size()
        error: int = FileCopier.NoError
        while True:
            if self._cancel_current:
                self._cancel_current = False
                self.copy_cancelled.emit(uid)
                break

            data: Union[bytes, int] = source_file.read(_COPY_BLOCK_SIZE)
            if isinstance(data, int):
                assert data == -1
                error = FileCopier.CannotReadSourceFile
                break

            data_len = len(data)
            if data_len == 0:
                self.copy_progress.emit(uid, progress, total)
                break

            if data_len != dest_file.write(data):
                error = FileCopier.CannotWriteDestinationFile
                break

            progress += data_len
            self.copy_progress.emit(uid, progress, total)

            qApp.processEvents()

        source_file.close()
        dest_file.close()
        if error != FileCopier.NoError:
            dest_file.remove()
            self.copy_error.emit(uid, error)
        else:
            dest_file.setPermissions(source_file.permissions())
            self.copy_complete.emit(uid)
Esempio n. 38
0
    def save_as(self, tab_index, filename):
        if not self.is_editor_modified(tab_index):
            return

        sci = self.get_editor(tab_index)
        sci.set_mode_by_filename(filename)

        out_file = QFile(filename)
        out_file.open(QFile.WriteOnly)
        out_file.write(sci.text.encode())
        out_file.close()

        sci.modified = False
        sci.filename = filename

        file_info = QFileInfo(out_file)
        self.main_tabs.setTabText(tab_index, file_info.fileName())
Esempio n. 39
0
    def saveFile(self, reply):
        disposition = reply.rawHeader(b"Content-Disposition").data().decode()
        disposition = disposition.replace(' ', '')
        filenames = [i.split('=')[-1] for i in disposition.split(';')
                     if i.startswith('filename=')]
        filename = ""
        if len(filenames) > 0:
            filename = filenames[0]
        filename = filename.replace('"', '')
        path, _ = QFileDialog.getSaveFileName(
            self.parent, QCoreApplication.translate("Downloader", "Save File"),
            filename)

        if len(path) != 0:
            file = QFile(path)
            if file.open(QIODevice.WriteOnly):
                file.write(reply.readAll())
                file.close()
Esempio n. 40
0
 def receipt_source_file_reply(self):
     """ 大商所的每日仓单html返回 """
     reply = self.sender()
     if reply.error():
         reply.deleteLater()
         self.spider_finished.emit("失败:" + str(reply.error()), True)
         return
     save_path = os.path.join(
         LOCAL_SPIDER_SRC,
         'dce/receipt/{}.html'.format(self.date.strftime("%Y-%m-%d")))
     file_data = reply.readAll()
     file_obj = QFile(save_path)
     is_open = file_obj.open(QFile.WriteOnly)
     if is_open:
         file_obj.write(file_data)
         file_obj.close()
     reply.deleteLater()
     self.spider_finished.emit(
         "获取大商所{}每日仓单数据源文件成功!".format(self.date.strftime("%Y-%m-%d")), True)
Esempio n. 41
0
 def daily_source_file_reply(self):
     """ 获取日统计数据请求返回 """
     reply = self.sender()
     if reply.error():
         reply.deleteLater()
         self.spider_finished.emit("失败:" + str(reply.error()), True)
         return
     save_path = os.path.join(
         LOCAL_SPIDER_SRC,
         'cffex/daily/{}.csv'.format(self.date.strftime("%Y-%m-%d")))
     file_data = reply.readAll()
     file_obj = QFile(save_path)
     is_open = file_obj.open(QFile.WriteOnly)
     if is_open:
         file_obj.write(file_data)
         file_obj.close()
     reply.deleteLater()
     self.spider_finished.emit(
         "获取中金所{}日交易数据源文件成功!".format(self.date.strftime("%Y-%m-%d")), True)
Esempio n. 42
0
class PluginRepositoryWidget(QWidget, Ui_PluginRepositoryDialog):
    """
    Class implementing a dialog showing the available plugins.
    
    @signal closeAndInstall() emitted when the Close & Install button is
        pressed
    """
    closeAndInstall = pyqtSignal()
    
    DescrRole = Qt.UserRole
    UrlRole = Qt.UserRole + 1
    FilenameRole = Qt.UserRole + 2
    AuthorRole = Qt.UserRole + 3

    PluginStatusUpToDate = 0
    PluginStatusNew = 1
    PluginStatusLocalUpdate = 2
    PluginStatusRemoteUpdate = 3
    
    def __init__(self, parent=None, external=False):
        """
        Constructor
        
        @param parent parent of this dialog (QWidget)
        @param external flag indicating an instatiation as a main
            window (boolean)
        """
        super(PluginRepositoryWidget, self).__init__(parent)
        self.setupUi(self)
        
        self.__updateButton = self.buttonBox.addButton(
            self.tr("Update"), QDialogButtonBox.ActionRole)
        self.__downloadButton = self.buttonBox.addButton(
            self.tr("Download"), QDialogButtonBox.ActionRole)
        self.__downloadButton.setEnabled(False)
        self.__downloadInstallButton = self.buttonBox.addButton(
            self.tr("Download && Install"),
            QDialogButtonBox.ActionRole)
        self.__downloadInstallButton.setEnabled(False)
        self.__downloadCancelButton = self.buttonBox.addButton(
            self.tr("Cancel"), QDialogButtonBox.ActionRole)
        self.__installButton = \
            self.buttonBox.addButton(self.tr("Close && Install"),
                                     QDialogButtonBox.ActionRole)
        self.__downloadCancelButton.setEnabled(False)
        self.__installButton.setEnabled(False)
        
        self.repositoryUrlEdit.setText(
            Preferences.getUI("PluginRepositoryUrl6"))
        
        self.repositoryList.headerItem().setText(
            self.repositoryList.columnCount(), "")
        self.repositoryList.header().setSortIndicator(0, Qt.AscendingOrder)
        
        self.__pluginContextMenu = QMenu(self)
        self.__hideAct = self.__pluginContextMenu.addAction(
            self.tr("Hide"), self.__hidePlugin)
        self.__hideSelectedAct = self.__pluginContextMenu.addAction(
            self.tr("Hide Selected"), self.__hideSelectedPlugins)
        self.__pluginContextMenu.addSeparator()
        self.__showAllAct = self.__pluginContextMenu.addAction(
            self.tr("Show All"), self.__showAllPlugins)
        self.__pluginContextMenu.addSeparator()
        self.__pluginContextMenu.addAction(
            self.tr("Cleanup Downloads"), self.__cleanupDownloads)
        
        self.pluginRepositoryFile = \
            os.path.join(Utilities.getConfigDir(), "PluginRepository")
        
        self.__external = external
        
        # attributes for the network objects
        self.__networkManager = QNetworkAccessManager(self)
        self.__networkManager.proxyAuthenticationRequired.connect(
            proxyAuthenticationRequired)
        if SSL_AVAILABLE:
            self.__sslErrorHandler = E5SslErrorHandler(self)
            self.__networkManager.sslErrors.connect(self.__sslErrors)
        self.__replies = []
        
        self.__networkConfigurationManager = QNetworkConfigurationManager(self)
        self.__onlineStateChanged(
            self.__networkConfigurationManager.isOnline())
        
        self.__networkConfigurationManager.onlineStateChanged.connect(
            self.__onlineStateChanged)
        
        self.__doneMethod = None
        self.__inDownload = False
        self.__pluginsToDownload = []
        self.__pluginsDownloaded = []
        self.__isDownloadInstall = False
        self.__allDownloadedOk = False
        
        self.__hiddenPlugins = Preferences.getPluginManager("HiddenPlugins")
        
        self.__populateList()
    
    @pyqtSlot(bool)
    def __onlineStateChanged(self, online):
        """
        Private slot handling online state changes.
        
        @param online flag indicating the online status
        @type bool
        """
        self.__updateButton.setEnabled(online)
        self.on_repositoryList_itemSelectionChanged()
        if online:
            msg = self.tr("Network Status: online")
        else:
            msg = self.tr("Network Status: offline")
        self.statusLabel.setText(msg)
    
    @pyqtSlot(QAbstractButton)
    def on_buttonBox_clicked(self, button):
        """
        Private slot to handle the click of a button of the button box.
        
        @param button reference to the button pressed (QAbstractButton)
        """
        if button == self.__updateButton:
            self.__updateList()
        elif button == self.__downloadButton:
            self.__isDownloadInstall = False
            self.__downloadPlugins()
        elif button == self.__downloadInstallButton:
            self.__isDownloadInstall = True
            self.__allDownloadedOk = True
            self.__downloadPlugins()
        elif button == self.__downloadCancelButton:
            self.__downloadCancel()
        elif button == self.__installButton:
            self.__closeAndInstall()
    
    def __formatDescription(self, lines):
        """
        Private method to format the description.
        
        @param lines lines of the description (list of strings)
        @return formatted description (string)
        """
        # remove empty line at start and end
        newlines = lines[:]
        if len(newlines) and newlines[0] == '':
            del newlines[0]
        if len(newlines) and newlines[-1] == '':
            del newlines[-1]
        
        # replace empty lines by newline character
        index = 0
        while index < len(newlines):
            if newlines[index] == '':
                newlines[index] = '\n'
            index += 1
        
        # join lines by a blank
        return ' '.join(newlines)
    
    @pyqtSlot(QPoint)
    def on_repositoryList_customContextMenuRequested(self, pos):
        """
        Private slot to show the context menu.
        
        @param pos position to show the menu (QPoint)
        """
        self.__hideAct.setEnabled(
            self.repositoryList.currentItem() is not None and
            len(self.__selectedItems()) == 1)
        self.__hideSelectedAct.setEnabled(
            len(self.__selectedItems()) > 1)
        self.__showAllAct.setEnabled(bool(self.__hasHiddenPlugins()))
        self.__pluginContextMenu.popup(self.repositoryList.mapToGlobal(pos))
    
    @pyqtSlot(QTreeWidgetItem, QTreeWidgetItem)
    def on_repositoryList_currentItemChanged(self, current, previous):
        """
        Private slot to handle the change of the current item.
        
        @param current reference to the new current item (QTreeWidgetItem)
        @param previous reference to the old current item (QTreeWidgetItem)
        """
        if self.__repositoryMissing or current is None:
            return
        
        self.urlEdit.setText(
            current.data(0, PluginRepositoryWidget.UrlRole) or "")
        self.descriptionEdit.setPlainText(
            current.data(0, PluginRepositoryWidget.DescrRole) and
            self.__formatDescription(
                current.data(0, PluginRepositoryWidget.DescrRole)) or "")
        self.authorEdit.setText(
            current.data(0, PluginRepositoryWidget.AuthorRole) or "")
    
    def __selectedItems(self):
        """
        Private method to get all selected items without the toplevel ones.
        
        @return list of selected items (list)
        """
        ql = self.repositoryList.selectedItems()
        for index in range(self.repositoryList.topLevelItemCount()):
            ti = self.repositoryList.topLevelItem(index)
            if ti in ql:
                ql.remove(ti)
        return ql
    
    @pyqtSlot()
    def on_repositoryList_itemSelectionChanged(self):
        """
        Private slot to handle a change of the selection.
        """
        self.__downloadButton.setEnabled(
            len(self.__selectedItems()) and
            self.__networkConfigurationManager.isOnline())
        self.__downloadInstallButton.setEnabled(
            len(self.__selectedItems()) and
            self.__networkConfigurationManager.isOnline())
        self.__installButton.setEnabled(len(self.__selectedItems()))
    
    def __updateList(self):
        """
        Private slot to download a new list and display the contents.
        """
        url = self.repositoryUrlEdit.text()
        self.__downloadFile(url,
                            self.pluginRepositoryFile,
                            self.__downloadRepositoryFileDone)
    
    def __downloadRepositoryFileDone(self, status, filename):
        """
        Private method called after the repository file was downloaded.
        
        @param status flaging indicating a successful download (boolean)
        @param filename full path of the downloaded file (string)
        """
        self.__populateList()
    
    def __downloadPluginDone(self, status, filename):
        """
        Private method called, when the download of a plugin is finished.
        
        @param status flag indicating a successful download (boolean)
        @param filename full path of the downloaded file (string)
        """
        if status:
            self.__pluginsDownloaded.append(filename)
        if self.__isDownloadInstall:
            self.__allDownloadedOk &= status
        
        del self.__pluginsToDownload[0]
        if len(self.__pluginsToDownload):
            self.__downloadPlugin()
        else:
            self.__downloadPluginsDone()
    
    def __downloadPlugin(self):
        """
        Private method to download the next plugin.
        """
        self.__downloadFile(self.__pluginsToDownload[0][0],
                            self.__pluginsToDownload[0][1],
                            self.__downloadPluginDone)
    
    def __downloadPlugins(self):
        """
        Private slot to download the selected plugins.
        """
        self.__pluginsDownloaded = []
        self.__pluginsToDownload = []
        self.__downloadButton.setEnabled(False)
        self.__downloadInstallButton.setEnabled(False)
        self.__installButton.setEnabled(False)
        for itm in self.repositoryList.selectedItems():
            if itm not in [self.__stableItem, self.__unstableItem,
                           self.__unknownItem]:
                url = itm.data(0, PluginRepositoryWidget.UrlRole)
                filename = os.path.join(
                    Preferences.getPluginManager("DownloadPath"),
                    itm.data(0, PluginRepositoryWidget.FilenameRole))
                self.__pluginsToDownload.append((url, filename))
        self.__downloadPlugin()
    
    def __downloadPluginsDone(self):
        """
        Private method called, when the download of the plugins is finished.
        """
        self.__downloadButton.setEnabled(len(self.__selectedItems()))
        self.__downloadInstallButton.setEnabled(len(self.__selectedItems()))
        self.__installButton.setEnabled(True)
        self.__doneMethod = None
        if not self.__external:
            ui = e5App().getObject("UserInterface")
        else:
            ui = None
        if ui and ui.notificationsEnabled():
            ui.showNotification(
                UI.PixmapCache.getPixmap("plugin48.png"),
                self.tr("Download Plugin Files"),
                self.tr("""The requested plugins were downloaded."""))
        
        if self.__isDownloadInstall:
            self.closeAndInstall.emit()
        else:
            if ui is None or not ui.notificationsEnabled():
                E5MessageBox.information(
                    self,
                    self.tr("Download Plugin Files"),
                    self.tr("""The requested plugins were downloaded."""))
            self.downloadProgress.setValue(0)
            
            # repopulate the list to update the refresh icons
            self.__populateList()
    
    def __resortRepositoryList(self):
        """
        Private method to resort the tree.
        """
        self.repositoryList.sortItems(
            self.repositoryList.sortColumn(),
            self.repositoryList.header().sortIndicatorOrder())
    
    def __populateList(self):
        """
        Private method to populate the list of available plugins.
        """
        self.repositoryList.clear()
        self.__stableItem = None
        self.__unstableItem = None
        self.__unknownItem = None
        
        self.downloadProgress.setValue(0)
        self.__doneMethod = None
        
        if os.path.exists(self.pluginRepositoryFile):
            self.__repositoryMissing = False
            f = QFile(self.pluginRepositoryFile)
            if f.open(QIODevice.ReadOnly):
                from E5XML.PluginRepositoryReader import PluginRepositoryReader
                reader = PluginRepositoryReader(f, self.addEntry)
                reader.readXML()
                self.repositoryList.resizeColumnToContents(0)
                self.repositoryList.resizeColumnToContents(1)
                self.repositoryList.resizeColumnToContents(2)
                self.__resortRepositoryList()
                url = Preferences.getUI("PluginRepositoryUrl6")
                if url != self.repositoryUrlEdit.text():
                    self.repositoryUrlEdit.setText(url)
                    E5MessageBox.warning(
                        self,
                        self.tr("Plugins Repository URL Changed"),
                        self.tr(
                            """The URL of the Plugins Repository has"""
                            """ changed. Select the "Update" button to get"""
                            """ the new repository file."""))
            else:
                E5MessageBox.critical(
                    self,
                    self.tr("Read plugins repository file"),
                    self.tr("<p>The plugins repository file <b>{0}</b> "
                            "could not be read. Select Update</p>")
                    .format(self.pluginRepositoryFile))
        else:
            self.__repositoryMissing = True
            QTreeWidgetItem(
                self.repositoryList,
                ["", self.tr(
                    "No plugin repository file available.\nSelect Update.")
                 ])
            self.repositoryList.resizeColumnToContents(1)
    
    def __downloadFile(self, url, filename, doneMethod=None):
        """
        Private slot to download the given file.
        
        @param url URL for the download (string)
        @param filename local name of the file (string)
        @param doneMethod method to be called when done
        """
        if self.__networkConfigurationManager.isOnline():
            self.__updateButton.setEnabled(False)
            self.__downloadButton.setEnabled(False)
            self.__downloadInstallButton.setEnabled(False)
            self.__downloadCancelButton.setEnabled(True)
            
            self.statusLabel.setText(url)
            
            self.__doneMethod = doneMethod
            self.__downloadURL = url
            self.__downloadFileName = filename
            self.__downloadIODevice = QFile(self.__downloadFileName + ".tmp")
            self.__downloadCancelled = False
            
            request = QNetworkRequest(QUrl(url))
            request.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
                                 QNetworkRequest.AlwaysNetwork)
            reply = self.__networkManager.get(request)
            reply.finished.connect(self.__downloadFileDone)
            reply.downloadProgress.connect(self.__downloadProgress)
            self.__replies.append(reply)
        else:
            E5MessageBox.warning(
                self,
                self.tr("Error downloading file"),
                self.tr(
                    """<p>Could not download the requested file"""
                    """ from {0}.</p><p>Error: {1}</p>"""
                ).format(url, self.tr("Computer is offline.")))
    
    def __downloadFileDone(self):
        """
        Private method called, after the file has been downloaded
        from the internet.
        """
        self.__updateButton.setEnabled(True)
        self.__downloadCancelButton.setEnabled(False)
        self.__onlineStateChanged(
            self.__networkConfigurationManager.isOnline())
        
        ok = True
        reply = self.sender()
        if reply in self.__replies:
            self.__replies.remove(reply)
        if reply.error() != QNetworkReply.NoError:
            ok = False
            if not self.__downloadCancelled:
                E5MessageBox.warning(
                    self,
                    self.tr("Error downloading file"),
                    self.tr(
                        """<p>Could not download the requested file"""
                        """ from {0}.</p><p>Error: {1}</p>"""
                    ).format(self.__downloadURL, reply.errorString())
                )
            self.downloadProgress.setValue(0)
            self.__downloadURL = None
            self.__downloadIODevice.remove()
            self.__downloadIODevice = None
            if self.repositoryList.topLevelItemCount():
                if self.repositoryList.currentItem() is None:
                    self.repositoryList.setCurrentItem(
                        self.repositoryList.topLevelItem(0))
                else:
                    self.__downloadButton.setEnabled(
                        len(self.__selectedItems()))
                    self.__downloadInstallButton.setEnabled(
                        len(self.__selectedItems()))
            reply.deleteLater()
            return
        
        self.__downloadIODevice.open(QIODevice.WriteOnly)
        self.__downloadIODevice.write(reply.readAll())
        self.__downloadIODevice.close()
        if QFile.exists(self.__downloadFileName):
            QFile.remove(self.__downloadFileName)
        self.__downloadIODevice.rename(self.__downloadFileName)
        self.__downloadIODevice = None
        self.__downloadURL = None
        reply.deleteLater()
        
        if self.__doneMethod is not None:
            self.__doneMethod(ok, self.__downloadFileName)
    
    def __downloadCancel(self):
        """
        Private slot to cancel the current download.
        """
        if self.__replies:
            reply = self.__replies[0]
            self.__downloadCancelled = True
            self.__pluginsToDownload = []
            reply.abort()
    
    def __downloadProgress(self, done, total):
        """
        Private slot to show the download progress.
        
        @param done number of bytes downloaded so far (integer)
        @param total total bytes to be downloaded (integer)
        """
        if total:
            self.downloadProgress.setMaximum(total)
            self.downloadProgress.setValue(done)
    
    def addEntry(self, name, short, description, url, author, version,
                 filename, status):
        """
        Public method to add an entry to the list.
        
        @param name data for the name field (string)
        @param short data for the short field (string)
        @param description data for the description field (list of strings)
        @param url data for the url field (string)
        @param author data for the author field (string)
        @param version data for the version field (string)
        @param filename data for the filename field (string)
        @param status status of the plugin (string [stable, unstable, unknown])
        """
        pluginName = filename.rsplit("-", 1)[0]
        if pluginName in self.__hiddenPlugins:
            return
        
        if status == "stable":
            if self.__stableItem is None:
                self.__stableItem = \
                    QTreeWidgetItem(self.repositoryList,
                                    [self.tr("Stable")])
                self.__stableItem.setExpanded(True)
            parent = self.__stableItem
        elif status == "unstable":
            if self.__unstableItem is None:
                self.__unstableItem = \
                    QTreeWidgetItem(self.repositoryList,
                                    [self.tr("Unstable")])
                self.__unstableItem.setExpanded(True)
            parent = self.__unstableItem
        else:
            if self.__unknownItem is None:
                self.__unknownItem = \
                    QTreeWidgetItem(self.repositoryList,
                                    [self.tr("Unknown")])
                self.__unknownItem.setExpanded(True)
            parent = self.__unknownItem
        itm = QTreeWidgetItem(parent, [name, version, short])
        
        itm.setData(0, PluginRepositoryWidget.UrlRole, url)
        itm.setData(0, PluginRepositoryWidget.FilenameRole, filename)
        itm.setData(0, PluginRepositoryWidget.AuthorRole, author)
        itm.setData(0, PluginRepositoryWidget.DescrRole, description)
        
        updateStatus = self.__updateStatus(filename, version)
        if updateStatus == PluginRepositoryWidget.PluginStatusUpToDate:
            itm.setIcon(1, UI.PixmapCache.getIcon("empty.png"))
            itm.setToolTip(1, self.tr("up-to-date"))
        elif updateStatus == PluginRepositoryWidget.PluginStatusNew:
            itm.setIcon(1, UI.PixmapCache.getIcon("download.png"))
            itm.setToolTip(1, self.tr("new download available"))
        elif updateStatus == PluginRepositoryWidget.PluginStatusLocalUpdate:
            itm.setIcon(1, UI.PixmapCache.getIcon("updateLocal.png"))
            itm.setToolTip(1, self.tr("update installable"))
        elif updateStatus == PluginRepositoryWidget.PluginStatusRemoteUpdate:
            itm.setIcon(1, UI.PixmapCache.getIcon("updateRemote.png"))
            itm.setToolTip(1, self.tr("updated download available"))
    
    def __updateStatus(self, filename, version):
        """
        Private method to check, if the given archive update status.
        
        @param filename data for the filename field (string)
        @param version data for the version field (string)
        @return plug-in update status (integer, one of PluginStatusNew,
            PluginStatusUpToDate, PluginStatusLocalUpdate,
            PluginStatusRemoteUpdate)
        """
        archive = os.path.join(Preferences.getPluginManager("DownloadPath"),
                               filename)
        
        # check, if it is an update (i.e. we already have archives
        # with the same pattern)
        archivesPattern = archive.rsplit('-', 1)[0] + "-*.zip"
        if len(glob.glob(archivesPattern)) == 0:
            return PluginRepositoryWidget.PluginStatusNew
        
        # check, if the archive exists
        if not os.path.exists(archive):
            return PluginRepositoryWidget.PluginStatusRemoteUpdate
        
        # check, if the archive is a valid zip file
        if not zipfile.is_zipfile(archive):
            return PluginRepositoryWidget.PluginStatusRemoteUpdate
        
        zip = zipfile.ZipFile(archive, "r")
        try:
            aversion = zip.read("VERSION").decode("utf-8")
        except KeyError:
            aversion = ""
        zip.close()
        
        if aversion == version:
            if not self.__external:
                # Check against installed/loaded plug-ins
                pluginManager = e5App().getObject("PluginManager")
                pluginName = filename.rsplit('-', 1)[0]
                pluginDetails = pluginManager.getPluginDetails(pluginName)
                if pluginDetails is None or pluginDetails["version"] < version:
                    return PluginRepositoryWidget.PluginStatusLocalUpdate
            
            return PluginRepositoryWidget.PluginStatusUpToDate
        else:
            return PluginRepositoryWidget.PluginStatusRemoteUpdate
    
    def __sslErrors(self, reply, errors):
        """
        Private slot to handle SSL errors.
        
        @param reply reference to the reply object (QNetworkReply)
        @param errors list of SSL errors (list of QSslError)
        """
        ignored = self.__sslErrorHandler.sslErrorsReply(reply, errors)[0]
        if ignored == E5SslErrorHandler.NotIgnored:
            self.__downloadCancelled = True
    
    def getDownloadedPlugins(self):
        """
        Public method to get the list of recently downloaded plugin files.
        
        @return list of plugin filenames (list of strings)
        """
        return self.__pluginsDownloaded
    
    @pyqtSlot(bool)
    def on_repositoryUrlEditButton_toggled(self, checked):
        """
        Private slot to set the read only status of the repository URL line
        edit.
        
        @param checked state of the push button (boolean)
        """
        self.repositoryUrlEdit.setReadOnly(not checked)
    
    def __closeAndInstall(self):
        """
        Private method to close the dialog and invoke the install dialog.
        """
        if not self.__pluginsDownloaded and self.__selectedItems():
            for itm in self.__selectedItems():
                filename = os.path.join(
                    Preferences.getPluginManager("DownloadPath"),
                    itm.data(0, PluginRepositoryWidget.FilenameRole))
                self.__pluginsDownloaded.append(filename)
        self.closeAndInstall.emit()
    
    def __hidePlugin(self):
        """
        Private slot to hide the current plug-in.
        """
        itm = self.__selectedItems()[0]
        pluginName = (itm.data(0, PluginRepositoryWidget.FilenameRole)
                      .rsplit("-", 1)[0])
        self.__updateHiddenPluginsList([pluginName])
    
    def __hideSelectedPlugins(self):
        """
        Private slot to hide all selected plug-ins.
        """
        hideList = []
        for itm in self.__selectedItems():
            pluginName = (itm.data(0, PluginRepositoryWidget.FilenameRole)
                          .rsplit("-", 1)[0])
            hideList.append(pluginName)
        self.__updateHiddenPluginsList(hideList)
    
    def __showAllPlugins(self):
        """
        Private slot to show all plug-ins.
        """
        self.__hiddenPlugins = []
        self.__updateHiddenPluginsList([])
    
    def __hasHiddenPlugins(self):
        """
        Private method to check, if there are any hidden plug-ins.
        
        @return flag indicating the presence of hidden plug-ins (boolean)
        """
        return bool(self.__hiddenPlugins)
    
    def __updateHiddenPluginsList(self, hideList):
        """
        Private method to store the list of hidden plug-ins to the settings.
        
        @param hideList list of plug-ins to add to the list of hidden ones
            (list of string)
        """
        if hideList:
            self.__hiddenPlugins.extend(
                [p for p in hideList if p not in self.__hiddenPlugins])
        Preferences.setPluginManager("HiddenPlugins", self.__hiddenPlugins)
        self.__populateList()
    
    def __cleanupDownloads(self):
        """
        Private slot to cleanup the plug-in downloads area.
        """
        downloadPath = Preferences.getPluginManager("DownloadPath")
        downloads = {}  # plug-in name as key, file name as value
        
        # step 1: extract plug-ins and downloaded files
        for pluginFile in os.listdir(downloadPath):
            if not os.path.isfile(os.path.join(downloadPath, pluginFile)):
                continue
            
            pluginName = pluginFile.rsplit("-", 1)[0]
            if pluginName not in downloads:
                downloads[pluginName] = []
            downloads[pluginName].append(pluginFile)
        
        # step 2: delete old entries
        for pluginName in downloads:
            downloads[pluginName].sort()
        
            if pluginName in self.__hiddenPlugins and \
                    not Preferences.getPluginManager("KeepHidden"):
                removeFiles = downloads[pluginName]
            else:
                removeFiles = downloads[pluginName][
                    :-Preferences.getPluginManager("KeepGenerations")]
            for removeFile in removeFiles:
                try:
                    os.remove(os.path.join(downloadPath, removeFile))
                except (IOError, OSError) as err:
                    E5MessageBox.critical(
                        self,
                        self.tr("Cleanup of Plugin Downloads"),
                        self.tr("""<p>The plugin download <b>{0}</b> could"""
                                """ not be deleted.</p><p>Reason: {1}</p>""")
                        .format(removeFile, str(err)))
Esempio n. 43
0
class HttpWindow(QDialog):
    def __init__(self, parent=None):
        super(HttpWindow, self).__init__(parent)

        self.url = QUrl()
        self.qnam = QNetworkAccessManager()
        self.reply = None
        self.outFile = None
        self.httpGetId = 0
        self.httpRequestAborted = False

        self.urlLineEdit = QLineEdit('https://www.qt.io')

        urlLabel = QLabel("&URL:")
        urlLabel.setBuddy(self.urlLineEdit)
        self.statusLabel = QLabel(
                "Please enter the URL of a file you want to download.")
        self.statusLabel.setWordWrap(True)

        self.downloadButton = QPushButton("Download")
        self.downloadButton.setDefault(True)
        self.quitButton = QPushButton("Quit")
        self.quitButton.setAutoDefault(False)

        buttonBox = QDialogButtonBox()
        buttonBox.addButton(self.downloadButton, QDialogButtonBox.ActionRole)
        buttonBox.addButton(self.quitButton, QDialogButtonBox.RejectRole)

        self.progressDialog = QProgressDialog(self)

        self.urlLineEdit.textChanged.connect(self.enableDownloadButton)
        self.qnam.authenticationRequired.connect(
                self.slotAuthenticationRequired)
        self.qnam.sslErrors.connect(self.sslErrors)
        self.progressDialog.canceled.connect(self.cancelDownload)
        self.downloadButton.clicked.connect(self.downloadFile)
        self.quitButton.clicked.connect(self.close)

        topLayout = QHBoxLayout()
        topLayout.addWidget(urlLabel)
        topLayout.addWidget(self.urlLineEdit)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(topLayout)
        mainLayout.addWidget(self.statusLabel)
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)

        self.setWindowTitle("HTTP")
        self.urlLineEdit.setFocus()

    def startRequest(self, url):
        self.reply = self.qnam.get(QNetworkRequest(url))
        self.reply.finished.connect(self.httpFinished)
        self.reply.readyRead.connect(self.httpReadyRead)
        self.reply.downloadProgress.connect(self.updateDataReadProgress)

    def downloadFile(self):
        self.url = QUrl(self.urlLineEdit.text())
        fileInfo = QFileInfo(self.url.path())
        fileName = fileInfo.fileName()

        if not fileName:
            fileName = 'index.html'

        if QFile.exists(fileName):
            ret = QMessageBox.question(self, "HTTP",
                    "There already exists a file called %s in the current "
                    "directory. Overwrite?" % fileName,
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

            if ret == QMessageBox.No:
                return

            QFile.remove(fileName)

        self.outFile = QFile(fileName)
        if not self.outFile.open(QIODevice.WriteOnly):
            QMessageBox.information(self, "HTTP",
                    "Unable to save the file %s: %s." % (fileName, self.outFile.errorString()))
            self.outFile = None
            return

        self.progressDialog.setWindowTitle("HTTP")
        self.progressDialog.setLabelText("Downloading %s." % fileName)
        self.downloadButton.setEnabled(False)

        self.httpRequestAborted = False
        self.startRequest(self.url)

    def cancelDownload(self):
        self.statusLabel.setText("Download canceled.")
        self.httpRequestAborted = True
        if self.reply is not None:
            self.reply.abort()
        self.downloadButton.setEnabled(True)

    def httpFinished(self):
        if self.httpRequestAborted:
            if self.outFile is not None:
                self.outFile.close()
                self.outFile.remove()
                self.outFile = None

            self.reply.deleteLater()
            self.reply = None
            self.progressDialog.hide()
            return

        self.progressDialog.hide()
        self.outFile.flush()
        self.outFile.close()

        redirectionTarget = self.reply.attribute(QNetworkRequest.RedirectionTargetAttribute)

        if self.reply.error():
            self.outFile.remove()
            QMessageBox.information(self, "HTTP",
                    "Download failed: %s." % self.reply.errorString())
            self.downloadButton.setEnabled(True)
        elif redirectionTarget is not None:
            newUrl = self.url.resolved(redirectionTarget)

            ret = QMessageBox.question(self, "HTTP",
                    "Redirect to %s?" % newUrl.toString(),
                    QMessageBox.Yes | QMessageBox.No)

            if ret == QMessageBox.Yes:
                self.url = newUrl
                self.reply.deleteLater()
                self.reply = None
                self.outFile.open(QIODevice.WriteOnly)
                self.outFile.resize(0)
                self.startRequest(self.url)
                return
        else:
            fileName = QFileInfo(QUrl(self.urlLineEdit.text()).path()).fileName()
            self.statusLabel.setText("Downloaded %s to %s." % (fileName, QDir.currentPath()))

            self.downloadButton.setEnabled(True)

        self.reply.deleteLater()
        self.reply = None
        self.outFile = None

    def httpReadyRead(self):
        if self.outFile is not None:
            self.outFile.write(self.reply.readAll())

    def updateDataReadProgress(self, bytesRead, totalBytes):
        if self.httpRequestAborted:
            return

        self.progressDialog.setMaximum(totalBytes)
        self.progressDialog.setValue(bytesRead)

    def enableDownloadButton(self):
        self.downloadButton.setEnabled(self.urlLineEdit.text() != '')

    def slotAuthenticationRequired(self, authenticator):
        import os
        from PyQt5 import uic

        ui = os.path.join(os.path.dirname(__file__), 'authenticationdialog.ui')
        dlg = uic.loadUi(ui)
        dlg.adjustSize()
        dlg.siteDescription.setText("%s at %s" % (authenticator.realm(), self.url.host()))

        dlg.userEdit.setText(self.url.userName())
        dlg.passwordEdit.setText(self.url.password())

        if dlg.exec_() == QDialog.Accepted:
            authenticator.setUser(dlg.userEdit.text())
            authenticator.setPassword(dlg.passwordEdit.text())

    def sslErrors(self, reply, errors):
        errorString = ", ".join([str(error.errorString()) for error in errors])

        ret = QMessageBox.warning(self, "HTTP Example",
                "One or more SSL errors has occurred: %s" % errorString,
                QMessageBox.Ignore | QMessageBox.Abort)

        if ret == QMessageBox.Ignore:
            self.reply.ignoreSslErrors()
Esempio n. 44
0
    def accept(self):
        className = self.field('className')
        baseClass = self.field('baseClass')
        macroName = self.field('macroName')
        baseInclude = self.field('baseInclude')

        outputDir = self.field('outputDir')
        header = self.field('header')
        implementation = self.field('implementation')

        block = ''

        if self.field('comment'):
            block += '/*\n'
            block += '    ' + header + '\n'
            block += '*/\n'
            block += '\n'

        if self.field('protect'):
            block += '#ifndef ' + macroName + '\n'
            block += '#define ' + macroName + '\n'
            block += '\n'

        if self.field('includeBase'):
            block += '#include ' + baseInclude + '\n'
            block += '\n'

        block += 'class ' + className
        if baseClass:
            block += ' : public ' + baseClass

        block += '\n'
        block += '{\n'

        if self.field('qobjectMacro'):
            block += '    Q_OBJECT\n'
            block += '\n'

        block += 'public:\n'

        if self.field('qobjectCtor'):
            block += '    ' + className + '(QObject *parent = 0);\n'
        elif self.field('qwidgetCtor'):
            block += '    ' + className + '(QWidget *parent = 0);\n'
        elif self.field('defaultCtor'):
            block += '    ' + className + '();\n'

            if self.field('copyCtor'):
                block += '    ' + className + '(const ' + className + ' &other);\n'
                block += '\n'
                block += '    ' + className + ' &operator=' + '(const ' + className + ' &other);\n'

        block += '};\n'

        if self.field('protect'):
            block += '\n'
            block += '#endif\n'

        headerFile = QFile(outputDir + '/' + header)

        if not headerFile.open(QFile.WriteOnly | QFile.Text):
            QMessageBox.warning(None, "Class Wizard",
                    "Cannot write file %s:\n%s" % (headerFile.fileName(), headerFile.errorString()))
            return

        headerFile.write(block)

        block = ''

        if self.field('comment'):
            block += '/*\n'
            block += '    ' + implementation + '\n'
            block += '*/\n'
            block += '\n'

        block += '#include "' + header + '"\n'
        block += '\n'

        if self.field('qobjectCtor'):
            block += className + '::' + className + '(QObject *parent)\n'
            block += '    : ' + baseClass + '(parent)\n'
            block += '{\n'
            block += '}\n'
        elif self.field('qwidgetCtor'):
            block += className + '::' + className + '(QWidget *parent)\n'
            block += '    : ' + baseClass + '(parent)\n'
            block += '{\n'
            block += '}\n'
        elif self.field('defaultCtor'):
            block += className + '::' + className + '()\n'
            block += '{\n'
            block += '    // missing code\n'
            block += '}\n'

            if self.field('copyCtor'):
                block += '\n'
                block += className + '::' + className + '(const ' + className + ' &other)\n'
                block += '{\n'
                block += '    *this = other;\n'
                block += '}\n'
                block += '\n'
                block += className + ' &' + className + '::operator=(const ' + className + ' &other)\n'
                block += '{\n'

                if baseClass:
                    block += '    ' + baseClass + '::operator=(other);\n'

                block += '    // missing code\n'
                block += '    return *this;\n'
                block += '}\n'

        implementationFile = QFile(outputDir + '/' + implementation)

        if not implementationFile.open(QFile.WriteOnly | QFile.Text):
            QMessageBox.warning(None, "Class Wizard",
                    "Cannot write file %s:\n%s" % (implementationFile.fileName(), implementationFile.errorString()))
            return

        implementationFile.write(block)

        super(ClassWizard, self).accept()
Esempio n. 45
0
class DownloadItem(QWidget, Ui_DownloadItem):
    """
    Class implementing a widget controlling a download.
    
    @signal statusChanged() emitted upon a status change of a download
    @signal downloadFinished() emitted when a download finished
    @signal progress(int, int) emitted to signal the download progress
    """
    statusChanged = pyqtSignal()
    downloadFinished = pyqtSignal()
    progress = pyqtSignal(int, int)
    
    Downloading = 0
    DownloadSuccessful = 1
    DownloadCancelled = 2
    
    def __init__(self, reply=None, requestFilename=False, webPage=None,
                 download=False, parent=None, mainWindow=None):
        """
        Constructor
        
        @keyparam reply reference to the network reply object (QNetworkReply)
        @keyparam requestFilename flag indicating to ask the user for a
            filename (boolean)
        @keyparam webPage reference to the web page object the download
            originated from (QWebPage)
        @keyparam download flag indicating a download operation (boolean)
        @keyparam parent reference to the parent widget (QWidget)
        @keyparam mainWindow reference to the main window (HelpWindow)
        """
        super(DownloadItem, self).__init__(parent)
        self.setupUi(self)
        
        p = self.infoLabel.palette()
        p.setColor(QPalette.Text, Qt.darkGray)
        self.infoLabel.setPalette(p)
        
        self.progressBar.setMaximum(0)
        
        self.__isFtpDownload = reply is not None and \
            reply.url().scheme() == "ftp"
        
        self.tryAgainButton.setIcon(UI.PixmapCache.getIcon("restart.png"))
        self.tryAgainButton.setEnabled(False)
        self.tryAgainButton.setVisible(False)
        self.stopButton.setIcon(UI.PixmapCache.getIcon("stopLoading.png"))
        self.pauseButton.setIcon(UI.PixmapCache.getIcon("pause.png"))
        self.openButton.setIcon(UI.PixmapCache.getIcon("open.png"))
        self.openButton.setEnabled(False)
        self.openButton.setVisible(False)
        if self.__isFtpDownload:
            self.stopButton.setEnabled(False)
            self.stopButton.setVisible(False)
            self.pauseButton.setEnabled(False)
            self.pauseButton.setVisible(False)
        
        self.__state = DownloadItem.Downloading
        
        icon = self.style().standardIcon(QStyle.SP_FileIcon)
        self.fileIcon.setPixmap(icon.pixmap(48, 48))
        
        self.__mainWindow = mainWindow
        self.__reply = reply
        self.__requestFilename = requestFilename
        self.__page = webPage
        self.__pageUrl = webPage and webPage.mainFrame().url() or QUrl()
        self.__toDownload = download
        self.__bytesReceived = 0
        self.__bytesTotal = -1
        self.__downloadTime = QTime()
        self.__output = QFile()
        self.__fileName = ""
        self.__originalFileName = ""
        self.__startedSaving = False
        self.__finishedDownloading = False
        self.__gettingFileName = False
        self.__canceledFileSelect = False
        self.__autoOpen = False
        
        self.__sha1Hash = QCryptographicHash(QCryptographicHash.Sha1)
        self.__md5Hash = QCryptographicHash(QCryptographicHash.Md5)
        
        if not requestFilename:
            self.__requestFilename = \
                Preferences.getUI("RequestDownloadFilename")
        
        self.__initialize()
    
    def __initialize(self, tryAgain=False):
        """
        Private method to (re)initialize the widget.
        
        @param tryAgain flag indicating a retry (boolean)
        """
        if self.__reply is None:
            return
        
        self.__startedSaving = False
        self.__finishedDownloading = False
        self.__bytesReceived = 0
        self.__bytesTotal = -1
        
        self.__sha1Hash.reset()
        self.__md5Hash.reset()
        
        # start timer for the download estimation
        self.__downloadTime.start()
        
        # attach to the reply object
        self.__url = self.__reply.url()
        self.__reply.setParent(self)
        self.__reply.setReadBufferSize(16 * 1024 * 1024)
        self.__reply.readyRead.connect(self.__readyRead)
        self.__reply.error.connect(self.__networkError)
        self.__reply.downloadProgress.connect(self.__downloadProgress)
        self.__reply.metaDataChanged.connect(self.__metaDataChanged)
        self.__reply.finished.connect(self.__finished)
        
        # reset info
        self.infoLabel.clear()
        self.progressBar.setValue(0)
        self.__getFileName()
        
        if self.__reply.error() != QNetworkReply.NoError:
            self.__networkError()
            self.__finished()
    
    def __getFileName(self):
        """
        Private method to get the file name to save to from the user.
        """
        if self.__gettingFileName:
            return
        
        import Helpviewer.HelpWindow
        downloadDirectory = Helpviewer.HelpWindow.HelpWindow\
            .downloadManager().downloadDirectory()
        
        if self.__fileName:
            fileName = self.__fileName
            originalFileName = self.__originalFileName
            self.__toDownload = True
            ask = False
        else:
            defaultFileName, originalFileName = \
                self.__saveFileName(downloadDirectory)
            fileName = defaultFileName
            self.__originalFileName = originalFileName
            ask = True
        self.__autoOpen = False
        if not self.__toDownload:
            from .DownloadAskActionDialog import DownloadAskActionDialog
            url = self.__reply.url()
            dlg = DownloadAskActionDialog(
                QFileInfo(originalFileName).fileName(),
                self.__reply.header(QNetworkRequest.ContentTypeHeader),
                "{0}://{1}".format(url.scheme(), url.authority()),
                self)
            if dlg.exec_() == QDialog.Rejected or dlg.getAction() == "cancel":
                self.progressBar.setVisible(False)
                self.__reply.close()
                self.on_stopButton_clicked()
                self.filenameLabel.setText(
                    self.tr("Download canceled: {0}").format(
                        QFileInfo(defaultFileName).fileName()))
                self.__canceledFileSelect = True
                return
            
            if dlg.getAction() == "scan":
                self.__mainWindow.requestVirusTotalScan(url)
                
                self.progressBar.setVisible(False)
                self.__reply.close()
                self.on_stopButton_clicked()
                self.filenameLabel.setText(
                    self.tr("VirusTotal scan scheduled: {0}").format(
                        QFileInfo(defaultFileName).fileName()))
                self.__canceledFileSelect = True
                return
            
            self.__autoOpen = dlg.getAction() == "open"
            if PYQT_VERSION_STR >= "5.0.0":
                from PyQt5.QtCore import QStandardPaths
                tempLocation = QStandardPaths.standardLocations(
                    QStandardPaths.TempLocation)[0]
            else:
                from PyQt5.QtGui import QDesktopServices
                tempLocation = QDesktopServices.storageLocation(
                    QDesktopServices.TempLocation)
            fileName = tempLocation + '/' + \
                QFileInfo(fileName).completeBaseName()
        
        if ask and not self.__autoOpen and self.__requestFilename:
            self.__gettingFileName = True
            fileName = E5FileDialog.getSaveFileName(
                None,
                self.tr("Save File"),
                defaultFileName,
                "")
            self.__gettingFileName = False
            if not fileName:
                self.progressBar.setVisible(False)
                self.__reply.close()
                self.on_stopButton_clicked()
                self.filenameLabel.setText(
                    self.tr("Download canceled: {0}")
                        .format(QFileInfo(defaultFileName).fileName()))
                self.__canceledFileSelect = True
                return
        
        fileInfo = QFileInfo(fileName)
        Helpviewer.HelpWindow.HelpWindow.downloadManager()\
            .setDownloadDirectory(fileInfo.absoluteDir().absolutePath())
        self.filenameLabel.setText(fileInfo.fileName())
        
        self.__output.setFileName(fileName + ".part")
        self.__fileName = fileName
        
        # check file path for saving
        saveDirPath = QFileInfo(self.__fileName).dir()
        if not saveDirPath.exists():
            if not saveDirPath.mkpath(saveDirPath.absolutePath()):
                self.progressBar.setVisible(False)
                self.on_stopButton_clicked()
                self.infoLabel.setText(self.tr(
                    "Download directory ({0}) couldn't be created.")
                    .format(saveDirPath.absolutePath()))
                return
        
        self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
        if self.__requestFilename:
            self.__readyRead()
    
    def __saveFileName(self, directory):
        """
        Private method to calculate a name for the file to download.
        
        @param directory name of the directory to store the file into (string)
        @return proposed filename and original filename (string, string)
        """
        path = parseContentDisposition(self.__reply)
        info = QFileInfo(path)
        baseName = info.completeBaseName()
        endName = info.suffix()
        
        origName = baseName
        if endName:
            origName += '.' + endName
        
        name = directory + baseName
        if endName:
            name += '.' + endName
            if not self.__requestFilename:
                # do not overwrite, if the user is not being asked
                i = 1
                while QFile.exists(name):
                    # file exists already, don't overwrite
                    name = directory + baseName + ('-{0:d}'.format(i))
                    if endName:
                        name += '.' + endName
                    i += 1
        return name, origName
    
    def __open(self):
        """
        Private slot to open the downloaded file.
        """
        info = QFileInfo(self.__output)
        url = QUrl.fromLocalFile(info.absoluteFilePath())
        QDesktopServices.openUrl(url)
    
    @pyqtSlot()
    def on_tryAgainButton_clicked(self):
        """
        Private slot to retry the download.
        """
        self.retry()
    
    def retry(self):
        """
        Public slot to retry the download.
        """
        if not self.tryAgainButton.isEnabled():
            return
        
        self.tryAgainButton.setEnabled(False)
        self.tryAgainButton.setVisible(False)
        self.openButton.setEnabled(False)
        self.openButton.setVisible(False)
        if not self.__isFtpDownload:
            self.stopButton.setEnabled(True)
            self.stopButton.setVisible(True)
            self.pauseButton.setEnabled(True)
            self.pauseButton.setVisible(True)
        self.progressBar.setVisible(True)
        
        if self.__page:
            nam = self.__page.networkAccessManager()
        else:
            import Helpviewer.HelpWindow
            nam = Helpviewer.HelpWindow.HelpWindow.networkAccessManager()
        reply = nam.get(QNetworkRequest(self.__url))
        if self.__output.exists():
            self.__output.remove()
        self.__output = QFile()
        self.__reply = reply
        self.__initialize(tryAgain=True)
        self.__state = DownloadItem.Downloading
        self.statusChanged.emit()
    
    @pyqtSlot(bool)
    def on_pauseButton_clicked(self, checked):
        """
        Private slot to pause the download.
        
        @param checked flag indicating the state of the button (boolean)
        """
        if checked:
            self.__reply.readyRead.disconnect(self.__readyRead)
            self.__reply.setReadBufferSize(16 * 1024)
        else:
            self.__reply.readyRead.connect(self.__readyRead)
            self.__reply.setReadBufferSize(16 * 1024 * 1024)
            self.__readyRead()
    
    @pyqtSlot()
    def on_stopButton_clicked(self):
        """
        Private slot to stop the download.
        """
        self.cancelDownload()
    
    def cancelDownload(self):
        """
        Public slot to stop the download.
        """
        self.setUpdatesEnabled(False)
        if not self.__isFtpDownload:
            self.stopButton.setEnabled(False)
            self.stopButton.setVisible(False)
            self.pauseButton.setEnabled(False)
            self.pauseButton.setVisible(False)
        self.tryAgainButton.setEnabled(True)
        self.tryAgainButton.setVisible(True)
        self.openButton.setEnabled(False)
        self.openButton.setVisible(False)
        self.setUpdatesEnabled(True)
        self.__state = DownloadItem.DownloadCancelled
        self.__reply.abort()
        self.downloadFinished.emit()
    
    @pyqtSlot()
    def on_openButton_clicked(self):
        """
        Private slot to open the downloaded file.
        """
        self.openFile()
    
    def openFile(self):
        """
        Public slot to open the downloaded file.
        """
        info = QFileInfo(self.__fileName)
        url = QUrl.fromLocalFile(info.absoluteFilePath())
        QDesktopServices.openUrl(url)
    
    def openFolder(self):
        """
        Public slot to open the folder containing the downloaded file.
        """
        info = QFileInfo(self.__fileName)
        url = QUrl.fromLocalFile(info.absolutePath())
        QDesktopServices.openUrl(url)
    
    def __readyRead(self):
        """
        Private slot to read the available data.
        """
        if self.__requestFilename and not self.__output.fileName():
            return
        
        if not self.__output.isOpen():
            # in case someone else has already put a file there
            if not self.__requestFilename:
                self.__getFileName()
            if not self.__output.open(QIODevice.WriteOnly):
                self.infoLabel.setText(
                    self.tr("Error opening save file: {0}")
                    .format(self.__output.errorString()))
                self.on_stopButton_clicked()
                self.statusChanged.emit()
                return
            self.statusChanged.emit()
        
        buffer = self.__reply.readAll()
        self.__sha1Hash.addData(buffer)
        self.__md5Hash.addData(buffer)
        bytesWritten = self.__output.write(buffer)
        if bytesWritten == -1:
            self.infoLabel.setText(
                self.tr("Error saving: {0}")
                    .format(self.__output.errorString()))
            self.on_stopButton_clicked()
        else:
            self.__startedSaving = True
            if self.__finishedDownloading:
                self.__finished()
    
    def __networkError(self):
        """
        Private slot to handle a network error.
        """
        self.infoLabel.setText(
            self.tr("Network Error: {0}")
                .format(self.__reply.errorString()))
        self.tryAgainButton.setEnabled(True)
        self.tryAgainButton.setVisible(True)
        self.downloadFinished.emit()
    
    def __metaDataChanged(self):
        """
        Private slot to handle a change of the meta data.
        """
        locationHeader = self.__reply.header(QNetworkRequest.LocationHeader)
        if locationHeader and locationHeader.isValid():
            self.__url = QUrl(locationHeader)
            import Helpviewer.HelpWindow
            self.__reply = Helpviewer.HelpWindow.HelpWindow\
                .networkAccessManager().get(QNetworkRequest(self.__url))
            self.__initialize()
    
    def __downloadProgress(self, bytesReceived, bytesTotal):
        """
        Private method to show the download progress.
        
        @param bytesReceived number of bytes received (integer)
        @param bytesTotal number of total bytes (integer)
        """
        self.__bytesReceived = bytesReceived
        self.__bytesTotal = bytesTotal
        currentValue = 0
        totalValue = 0
        if bytesTotal > 0:
            currentValue = bytesReceived * 100 / bytesTotal
            totalValue = 100
        self.progressBar.setValue(currentValue)
        self.progressBar.setMaximum(totalValue)
        
        self.progress.emit(currentValue, totalValue)
        self.__updateInfoLabel()
    
    def bytesTotal(self):
        """
        Public method to get the total number of bytes of the download.
        
        @return total number of bytes (integer)
        """
        if self.__bytesTotal == -1:
            self.__bytesTotal = self.__reply.header(
                QNetworkRequest.ContentLengthHeader)
            if self.__bytesTotal is None:
                self.__bytesTotal = -1
        return self.__bytesTotal
    
    def bytesReceived(self):
        """
        Public method to get the number of bytes received.
        
        @return number of bytes received (integer)
        """
        return self.__bytesReceived
    
    def remainingTime(self):
        """
        Public method to get an estimation for the remaining time.
        
        @return estimation for the remaining time (float)
        """
        if not self.downloading():
            return -1.0
        
        if self.bytesTotal() == -1:
            return -1.0
        
        cSpeed = self.currentSpeed()
        if cSpeed != 0:
            timeRemaining = (self.bytesTotal() - self.bytesReceived()) / cSpeed
        else:
            timeRemaining = 1
        
        # ETA should never be 0
        if timeRemaining == 0:
            timeRemaining = 1
        
        return timeRemaining
    
    def currentSpeed(self):
        """
        Public method to get an estimation for the download speed.
        
        @return estimation for the download speed (float)
        """
        if not self.downloading():
            return -1.0
        
        return self.__bytesReceived * 1000.0 / self.__downloadTime.elapsed()
    
    def __updateInfoLabel(self):
        """
        Private method to update the info label.
        """
        if self.__reply.error() != QNetworkReply.NoError:
            return
        
        bytesTotal = self.bytesTotal()
        running = not self.downloadedSuccessfully()
        
        speed = self.currentSpeed()
        timeRemaining = self.remainingTime()
        
        info = ""
        if running:
            remaining = ""
            
            if bytesTotal > 0:
                remaining = timeString(timeRemaining)
            
            info = self.tr("{0} of {1} ({2}/sec)\n{3}")\
                .format(
                    dataString(self.__bytesReceived),
                    bytesTotal == -1 and self.tr("?")
                    or dataString(bytesTotal),
                    dataString(int(speed)),
                    remaining)
        else:
            if self.__bytesReceived == bytesTotal or bytesTotal == -1:
                info = self.tr("{0} downloaded\nSHA1: {1}\nMD5: {2}")\
                    .format(dataString(self.__output.size()),
                            str(self.__sha1Hash.result().toHex(),
                                encoding="ascii"),
                            str(self.__md5Hash.result().toHex(),
                                encoding="ascii")
                            )
            else:
                info = self.tr("{0} of {1} - Stopped")\
                    .format(dataString(self.__bytesReceived),
                            dataString(bytesTotal))
        self.infoLabel.setText(info)
    
    def downloading(self):
        """
        Public method to determine, if a download is in progress.
        
        @return flag indicating a download is in progress (boolean)
        """
        return self.__state == DownloadItem.Downloading
    
    def downloadedSuccessfully(self):
        """
        Public method to check for a successful download.
        
        @return flag indicating a successful download (boolean)
        """
        return self.__state == DownloadItem.DownloadSuccessful
    
    def downloadCanceled(self):
        """
        Public method to check, if the download was cancelled.
        
        @return flag indicating a canceled download (boolean)
        """
        return self.__state == DownloadItem.DownloadCancelled
    
    def __finished(self):
        """
        Private slot to handle the download finished.
        """
        self.__finishedDownloading = True
        if not self.__startedSaving:
            return
        
        noError = self.__reply.error() == QNetworkReply.NoError
        
        self.progressBar.setVisible(False)
        if not self.__isFtpDownload:
            self.stopButton.setEnabled(False)
            self.stopButton.setVisible(False)
            self.pauseButton.setEnabled(False)
            self.pauseButton.setVisible(False)
        self.openButton.setEnabled(noError)
        self.openButton.setVisible(noError)
        self.__output.close()
        if QFile.exists(self.__fileName):
            QFile.remove(self.__fileName)
        self.__output.rename(self.__fileName)
        self.__updateInfoLabel()
        self.__state = DownloadItem.DownloadSuccessful
        self.statusChanged.emit()
        self.downloadFinished.emit()
        
        if self.__autoOpen:
            self.__open()
    
    def canceledFileSelect(self):
        """
        Public method to check, if the user canceled the file selection.
        
        @return flag indicating cancellation (boolean)
        """
        return self.__canceledFileSelect
    
    def setIcon(self, icon):
        """
        Public method to set the download icon.
        
        @param icon reference to the icon to be set (QIcon)
        """
        self.fileIcon.setPixmap(icon.pixmap(48, 48))
    
    def fileName(self):
        """
        Public method to get the name of the output file.
        
        @return name of the output file (string)
        """
        return self.__fileName
    
    def absoluteFilePath(self):
        """
        Public method to get the absolute path of the output file.
        
        @return absolute path of the output file (string)
        """
        return QFileInfo(self.__fileName).absoluteFilePath()
    
    def getData(self):
        """
        Public method to get the relevant download data.
        
        @return tuple of URL, save location, flag and the
            URL of the related web page (QUrl, string, boolean,QUrl)
        """
        return (self.__url, QFileInfo(self.__fileName).filePath(),
                self.downloadedSuccessfully(), self.__pageUrl)
    
    def setData(self, data):
        """
        Public method to set the relevant download data.
        
        @param data tuple of URL, save location, flag and the
            URL of the related web page (QUrl, string, boolean, QUrl)
        """
        self.__url = data[0]
        self.__fileName = data[1]
        self.__pageUrl = data[3]
        self.__isFtpDownload = self.__url.scheme() == "ftp"
        
        self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
        self.infoLabel.setText(self.__fileName)
        
        self.stopButton.setEnabled(False)
        self.stopButton.setVisible(False)
        self.pauseButton.setEnabled(False)
        self.pauseButton.setVisible(False)
        self.openButton.setEnabled(data[2])
        self.openButton.setVisible(data[2])
        self.tryAgainButton.setEnabled(not data[2])
        self.tryAgainButton.setVisible(not data[2])
        if data[2]:
            self.__state = DownloadItem.DownloadSuccessful
        else:
            self.__state = DownloadItem.DownloadCancelled
        self.progressBar.setVisible(False)
    
    def getInfoData(self):
        """
        Public method to get the text of the info label.
        
        @return text of the info label (string)
        """
        return self.infoLabel.text()
    
    def getPageUrl(self):
        """
        Public method to get the URL of the download page.
        
        @return URL of the download page (QUrl)
        """
        return self.__pageUrl