将状态设置为与 dbus 共情时出错

Posted

技术标签:

【中文标题】将状态设置为与 dbus 共情时出错【英文标题】:Error setting status to empathy with dbus 【发布时间】:2011-12-02 01:46:39 【问题描述】:

当我尝试使用 python 将状态设置为对 dbus 的同情时出现错误, 这是我从不同来源获得的代码

## getting status from clementine music player

import dbus

# Clementine lives on the Session bus
session_bus = dbus.SessionBus()

# Get Clementine's player object, and then get an interface from that object,
# otherwise we'd have to type out the full interface name on every method call.
player = session_bus.get_object('org.mpris.clementine', '/Player')
iface = dbus.Interface(player, dbus_interface='org.freedesktop.MediaPlayer')

# Call a method on the interface
metadata = iface.GetMetadata()
print metadata["title"]+' - '+metadata["artist"]
status = metadata["title"]+' - '+metadata["artist"]

## the below code is from https://github.com/engla/kupfer/blob/master/kupfer/plugin/empathy.py
import os
import subprocess
import sys
import time

import pynotify as pn

# it takes a long time before empathy is willing to accept statuses
EMPATHY_STARTUP_SECONDS = 20

def show_usage():
    print "\nUsage:"
    print sys.argv[0], "|".join(_STATUSES.keys())

def set_status(status):
    try:

        activate(status)
        notify_set_status(status)
    except IndexError:
        print "Missing required parameter."
        show_usage()
    except ValueError as err:
        print err
        show_usage()

def notify_set_status(status):
    success = pn.init("icon-summary-body")
    if not success:
        raise Error()

    # I like this icon, even if it's not relevant
    icon = 'notification-keyboard-brightness-low'
    pn.Notification("Empathy", "Tried to set status to "+ status, icon).show()

def main():# give empathy some time to start up
    set_status(status)


def _(text):
    return text

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# All code below was derived from https://github.com/engla/kupfer/blob/master/kupfer/plugin/empathy.py
ACCOUNTMANAGER_PATH = "/org/freedesktop/Telepathy/AccountManager"
ACCOUNTMANAGER_IFACE = "org.freedesktop.Telepathy.AccountManager"
ACCOUNT_IFACE = "org.freedesktop.Telepathy.Account"
CHANNEL_GROUP_IFACE = "org.freedesktop.Telepathy.Channel.Interface.Group"
CONTACT_IFACE = "org.freedesktop.Telepathy.Connection.Interface.Contacts"
SIMPLE_PRESENCE_IFACE = "org.freedesktop.Telepathy.Connection.Interface.SimplePresence"
DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties"
CHANNELDISPATCHER_IFACE = "org.freedesktop.Telepathy.ChannelDispatcher"
CHANNELDISPATCHER_PATH = "/org/freedesktop/Telepathy/ChannelDispatcher"
CHANNEL_TYPE = "org.freedesktop.Telepathy.Channel.ChannelType"
CHANNEL_TYPE_TEXT = "org.freedesktop.Telepathy.Channel.Type.Text"
CHANNEL_TARGETHANDLE = "org.freedesktop.Telepathy.Channel.TargetHandle"
CHANNEL_TARGETHANDLETYPE = "org.freedesktop.Telepathy.Channel.TargetHandleType"
EMPATHY_CLIENT_IFACE = "org.freedesktop.Telepathy.Client.Empathy"

EMPATHY_ACCOUNT_KEY = "EMPATHY_ACCOUNT"
EMPATHY_CONTACT_ID = "EMPATHY_CONTACT_ID"


_ATTRIBUTES = 
    'alias': 'org.freedesktop.Telepathy.Connection.Interface.Aliasing/alias',
    'presence': 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence/presence',
    'contact_caps': 'org.freedesktop.Telepathy.Connection.Interface.ContactCapabilities.DRAFT/caps',
    'jid': 'org.freedesktop.Telepathy.Connection/contact-id',
    'caps': 'org.freedesktop.Telepathy.Connection.Interface.Capabilities/caps',

def _create_dbus_connection():
        sbus = dbus.SessionBus()
        proxy_obj = sbus.get_object(ACCOUNTMANAGER_IFACE, ACCOUNTMANAGER_PATH)
        dbus_iface = dbus.Interface(proxy_obj, DBUS_PROPS_IFACE)
        return dbus_iface

def activate(status):

    bus = dbus.SessionBus()
    interface = _create_dbus_connection()
    for valid_account in interface.Get(ACCOUNTMANAGER_IFACE, "ValidAccounts"):
        account = bus.get_object(ACCOUNTMANAGER_IFACE, valid_account)
        connection_status = account.Get(ACCOUNT_IFACE, "ConnectionStatus")
        if connection_status != 0:
            continue

        connection_path = account.Get(ACCOUNT_IFACE, "Connection")
        connection_iface = connection_path.replace("/", ".")[1:]
        connection = bus.get_object(connection_iface, connection_path)
        simple_presence = dbus.Interface(connection, SIMPLE_PRESENCE_IFACE)
        try:
            simple_presence.SetPresence(status, _(status))
        except dbus.exceptions.DBusException:
            print(status + ' is not supported by ' + valid_account)
            print simple_presence

main()

当我运行这个脚本时, 我收到以下错误。

phanindra@phanindra:~$ python clementine.py 
onelove - Blue

(clementine.py:6142): Gtk-WARNING **: Unable to locate theme engine in module_path: "pixmap",
onelove - Blue is not supported by /org/freedesktop/Telepathy/Account/gabble/jabber/abcd_40gmail_2ecom0

我做错了什么?还是这些功能已弃用?

【问题讨论】:

【参考方案1】:

尝试使用 Clementine 中当前播放的曲目更新 Empathy 的状态:

import dbus
session_bus = dbus.SessionBus()
player = session_bus.get_object('org.mpris.clementine', '/Player')
iface = dbus.Interface(player, dbus_interface='org.freedesktop.MediaPlayer')
metadata = iface.GetMetadata()
status = "♫ ".decode('utf8')+metadata["title"]+' - '+metadata["album"]+" ♫".decode('utf8')
print status
from gi.repository import TelepathyGLib as Tp
from gi.repository import GObject
loop = GObject.MainLoop()
am = Tp.AccountManager.dup()
am.prepare_async(None, lambda *args: loop.quit(), None)
loop.run()
am.set_all_requested_presences(Tp.ConnectionPresenceType.AVAILABLE,
'available', status)

感谢您更新我原来帖子中的格式! - 哈沙

【讨论】:

【参考方案2】:

这里是 SimplePresence API 的 SetPresence 调用的 API 规范:http://telepathy.freedesktop.org/spec/Connection_Interface_Simple_Presence.html#Method:SetPresence

我认为问题在于您试图将存在设置为无效状态 - 您只允许将状态设置为连接管理器识别的状态 - 例如可用的。您可以为消息设置任何您喜欢的内容。

所以你的代码应该是这样的:

simple_presence.SetPresence("Available", status)

【讨论】:

我已经尝试过 simple_presence.SetPresence("Available", status) 但它也不起作用.. :| 从链接中我了解到不能使用 dbus 将状态设置为自定义状态,但我不知道为什么当我使用可用时它仍然无法正常工作。谢谢你的帮助

以上是关于将状态设置为与 dbus 共情时出错的主要内容,如果未能解决你的问题,请参考以下文章

使用 QDBusInterface 获取 DBus 接口属性时出错

使用 glib 和 dbus dbus_g_value_build_g_variant 构建 Maliit 时出错

Dbus - 没有收到回复

WebSocket 连接失败。 WebSocket握手期间出错-socketjs

FLash:尝试使用setChildIndex和TweenLite创建按钮rollOver效果时出错

将数组置于状态时出错 - React