当前位置: 首页>>代码示例>>Python>>正文


Python QDBusConnection.connect方法代码示例

本文整理汇总了Python中PyQt5.QtDBus.QDBusConnection.connect方法的典型用法代码示例。如果您正苦于以下问题:Python QDBusConnection.connect方法的具体用法?Python QDBusConnection.connect怎么用?Python QDBusConnection.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.QtDBus.QDBusConnection的用法示例。


在下文中一共展示了QDBusConnection.connect方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Notifier

# 需要导入模块: from PyQt5.QtDBus import QDBusConnection [as 别名]
# 或者: from PyQt5.QtDBus.QDBusConnection import connect [as 别名]
class Notifier(QObject):
    _conn = None
    _interface = None
    _notifications = None  # a dict of notifyId: taskDict

    _completedTasksStat = None

    def __init__(self, parent):
        super().__init__(parent)
        self._conn = QDBusConnection("Xware Desktop").sessionBus()

        self._interface = QDBusInterface(_DBUS_NOTIFY_SERVICE,
                                         _DBUS_NOTIFY_PATH,
                                         _DBUS_NOTIFY_INTERFACE,
                                         self._conn)

        self._notifications = {}
        self._completedTasksStat = app.etmpy.completedTasksStat
        self._completedTasksStat.sigTaskCompleted.connect(self.notifyTask)

        successful = self._conn.connect(_DBUS_NOTIFY_SERVICE,
                                        _DBUS_NOTIFY_PATH,
                                        _DBUS_NOTIFY_INTERFACE,
                                        "ActionInvoked", self.slotActionInvoked)
        if not successful:
            logging.error("ActionInvoked connect failed.")

        self._qSound_complete = QSound(":/sound/download-complete.wav", self)

    @property
    def isConnected(self):
        return self._conn.isConnected()

    def notifyTask(self, taskId):
        task = self._completedTasksStat.getTask(taskId)

        if task.get("state", None) == 11:  # see definitions in class TaskStatistic.
            if app.settings.getbool("frontend", "notifybysound"):
                self._qSound_complete.play()
            self._dbus_notify(task)
        else:
            # TODO: Also notify if errors occur
            pass

    def _dbus_notify(self, task):
        if not app.settings.getbool("frontend", "popnotifications"):
            return

        qdBusMsg = self._interface.call(
            "Notify",
            QDBusArgument("Xware Desktop", QMetaType.QString),  # app_name
            QDBusArgument(0, QMetaType.UInt),  # replace_id
            QDBusArgument("/opt/xware_desktop/frontend/thunder.ico", QMetaType.QString),  # app_icon
            QDBusArgument("下载完成", QMetaType.QString),  # summary
            QDBusArgument(task["name"], QMetaType.QString),  # body
            QDBusArgument(["open", "打开", "openDir", "打开文件夹"], QMetaType.QStringList),  # actions,
            {
                "category": "transfer.complete",
            },  # hints
            QDBusArgument(5000, QMetaType.Int),  # timeout
        )

        if qdBusMsg.errorName():
            logging.error("DBus, notifyTask {}: {}".format(qdBusMsg.errorName(),
                                                           qdBusMsg.errorMessage()))
        else:
            # add it to the dict
            self._notifications[qdBusMsg.arguments()[0]] = task

    @pyqtSlot(QDBusMessage)
    def slotActionInvoked(self, msg):
        notifyId, action = msg.arguments()
        task = self._notifications.get(notifyId, None)
        if not task:
            # other applications' notifications
            return
        name = task["name"]  # filename
        path = task["path"]  # location

        if action == "open":
            openPath = os.path.join(path, name)
        elif action == "openDir":
            openPath = path
        else:
            raise Exception("Unknown action from slotActionInvoked.")

        nativeOpenPath = app.mountsFaker.convertToNativePath(openPath)
        qUrl = QUrl.fromLocalFile(nativeOpenPath)
        QDesktopServices().openUrl(qUrl)
开发者ID:0rient,项目名称:XwareDesktop,代码行数:91,代码来源:__init__.py

示例2: Notifier

# 需要导入模块: from PyQt5.QtDBus import QDBusConnection [as 别名]
# 或者: from PyQt5.QtDBus.QDBusConnection import connect [as 别名]
class Notifier(QObject):
    def __init__(self, parent):
        super().__init__(parent)
        self._conn = QDBusConnection("Xware Desktop").sessionBus()

        self._interface = QDBusInterface(_DBUS_NOTIFY_SERVICE,
                                         _DBUS_NOTIFY_PATH,
                                         _DBUS_NOTIFY_INTERFACE,
                                         self._conn)

        self._notified = {}  # a dict of notifyId: taskDict
        app.taskModel.taskCompleted.connect(self.notifyTaskCompleted, Qt.DirectConnection)

        self._capabilities = self._getCapabilities()
        if "actions" in self._capabilities:
            successful = self._conn.connect(_DBUS_NOTIFY_SERVICE,
                                            _DBUS_NOTIFY_PATH,
                                            _DBUS_NOTIFY_INTERFACE,
                                            "ActionInvoked", self.slotActionInvoked)
            if not successful:
                logging.error("ActionInvoked connect failed.")

        self._qSound_complete = QSound(":/sound/download-complete.wav", self)

    @property
    def isConnected(self):
        return self._conn.isConnected()

    @pyqtSlot("QObject", result = "void")
    def notifyTaskCompleted(self, taskItem):
        if app.settings.getbool("frontend", "notifybysound"):
            self._qSound_complete.play()

        if not app.settings.getbool("frontend", "popnotifications"):
            return

        self._dbus_notifyCompleted(taskItem)

    def _getCapabilities(self):
        # get libnotify server caps and remember it.
        qdBusMsg = self._interface.call(
            "GetCapabilities"
        )
        if qdBusMsg.errorName():
            logging.error("cannot get org.freedesktop.Notifications.GetCapabilities")
            return []
        else:
            return qdBusMsg.arguments()[0]

    def _dbus_notifyCompleted(self, task: "TaskItem"):
        if "actions" in self._capabilities:
            actions = QDBusArgument(["open", "打开", "viewOneFile", "在文件夹中显示"], QMetaType.QStringList)
        else:
            actions = QDBusArgument([], QMetaType.QStringList)

        qdBusMsg = self._interface.call(
            "Notify",
            QDBusArgument("Xware Desktop", QMetaType.QString),  # app_name
            QDBusArgument(0, QMetaType.UInt),  # replace_id
            QDBusArgument("xware-desktop", QMetaType.QString),  # app_icon
            QDBusArgument("下载完成", QMetaType.QString),  # summary
            QDBusArgument(task.name, QMetaType.QString),  # body
            actions,
            {
                "category": "transfer.complete",
            },  # hints
            QDBusArgument(5000, QMetaType.Int),  # timeout
        )

        if qdBusMsg.errorName():
            logging.error("DBus, notifyTask {}: {}".format(qdBusMsg.errorName(),
                                                           qdBusMsg.errorMessage()))
        else:
            # add it to the dict
            notificationId = qdBusMsg.arguments()[0]
            self._notified[notificationId] = task.id

    @pyqtSlot(QDBusMessage)
    def slotActionInvoked(self, msg):
        notifyId, action = msg.arguments()
        taskId = self._notified.get(notifyId, None)
        if not taskId:
            # other applications' notifications
            return

        taskItem = app.taskModel.taskManager.get(taskId, None)
        if not taskItem:
            logging.debug("taskItem cannot be found anymore in TaskModel.")
            return

        fullpath = taskItem.fullpath  # path + name

        if action == "open":
            return systemOpen(fullpath)
        elif action == "viewOneFile":
            return viewOneFile(fullpath)
        elif action == "default":  # Unity's notify osd always have a default action.
            return
        else:
            raise Exception("Unknown action from slotActionInvoked: {}.".format(action))
开发者ID:CludeX,项目名称:XwareDesktop,代码行数:102,代码来源:_DBusNotifier.py


注:本文中的PyQt5.QtDBus.QDBusConnection.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。