如何创建 a{sv} 类型的值以通过 DBus 调用 org.freedesktop.Notifications.Notify?

问题描述 投票:0回答:1

我正在尝试使用 DBus,试图理解 桌面通知规范

在后一个链接中,我读到该方法

Notify
具有此签名

UINT32 org.freedesktop.Notifications.Notify ( app_name,    
                                              replaces_id,     
                                              app_icon,    
                                              summary,     
                                              body,    
                                              actions,     
                                              hints,   
                                              expire_timeout); 
STRING app_name;
UINT32 replaces_id;
STRING app_icon;
STRING summary;
STRING body;
as actions;
a{sv} hints;
INT32 expire_timeout;

其中参数

hints
的类型为
a{sv}
,注释如下:

可以从客户端程序传递到服务器的可选提示。尽管客户端和服务器永远不应该假定对方支持任何特定的提示,但它们可以用于传递服务器可以利用的信息,例如进程 PID 或窗口 ID。请参阅提示。可以为空。

所以我尝试编写一个小小的 haskell 程序,该程序进行调用并提供一些虚拟参数,认为“可选提示”意味着

Maybe

{-# LANGUAGE OverloadedStrings #-}

import Data.Int
import Data.Word
import Data.List (sort)
import DBus
import DBus.Client
import DBus.Internal.Types
import GHC.Generics

main = do
    client <- connectSession

    call_ client (methodCall "/org/freedesktop/Notifications" "org.freedesktop.Notifications" "Notify")
        { methodCallDestination = Just "org.freedesktop.Notifications"
        , methodCallBody = [ toVariant ("" :: String)
                           , toVariant (1 :: Word32)
                           , toVariant ("" :: String)
                           , toVariant ("" :: String)
                           , toVariant ("" :: String)
                           , toVariant ([""] :: [String])
                           , toVariant (Nothing :: Maybe [(String, Variant)]) -- compilation error
                           , toVariant (2000 :: Int32) ]
        }

但是上面无法编译,因为有

No instance for (IsVariant (Maybe [(String, Variant)]))

早期的尝试是做

                           , toVariant ([] :: [(String, Variant)]) -- compilation error

但是编译时,出现错误

mynot: ClientError {clientErrorMessage = "Call failed: Type of message, \8220(susssasa(sv)i)\8221, does not match expected type \8220(susssasa{sv}i)\8221", clientErrorFatal = False}

其中类型

[(String, Variant)]
似乎映射到
a(sv)
,但我需要一个
a{sv}

haskell notifications dbus freedesktop.org
1个回答
0
投票

a{sv}
是D-Bus中的字典类型。在 Haskell 方面,字典对应于
Map
,它有一个
IsVariant
实例。既然如此,使用
import qualified Data.Map.Strict as Map
,您可以为提示提供一个空地图:
toVariant (Map.empty :: Map.Map String Variant)

© www.soinside.com 2019 - 2024. All rights reserved.