Data.GI.Base.GVariant

Description

This module contains some helper functions for dealing with GVariant values. The simplest way of dealing with them is by using the IsGVariant typeclass:

str <- fromGVariant variant :: IO (Maybe Text)

assuming that the variant is expected to contain a string in UTF8 encoding. The code becomes even shorter if the type checker can determine the return type for you:

readStringVariant :: GVariant -> IO Text
fromGVariant variant >>= \case
Nothing  -> error "Variant was not a string"
Just str -> return str

Alternatively, you can use manually the gvariantFrom* and gvariantTo* family of functions.

Synopsis

# Documentation

class IsGVariant a where Source #

The typeclass for types that can be automatically marshalled into GVariant using toGVariant and fromGVariant.

Minimal complete definition

Methods

toGVariant :: a -> IO GVariant Source #

Convert a value of the given type into a GVariant.

Try to decode a GVariant into a target type. If the conversion fails we return Nothing. The type that was expected can be obtained by calling toGVariantFormatString, and the actual type as understood by the GVariant code can be obtained by calling gvariantToTypeString.

The expected format string for this type (the argument is ignored).

Instances

 Source # Methods Source # Methods Source # Methods Source # Methods Source # Methods Source # Methods Source # Methods Source # Methods Source # Methods Source # The empty tuple GVariant, mostly useful for type checking. MethodstoGVariant :: () -> IO GVariant Source # Source # Methods Source # Methods Source # Methods Source # Methods Source # Methods Source # Methods IsGVariant a => IsGVariant [a] Source # MethodstoGVariant :: [a] -> IO GVariant Source #fromGVariant :: GVariant -> IO (Maybe [a]) Source #toGVariantFormatString :: [a] -> Text Source # IsGVariant a => IsGVariant (Maybe a) Source # Methods Source # One element tuples. Methods (IsGVariant a, IsGVariant b) => IsGVariant (a, b) Source # MethodstoGVariant :: (a, b) -> IO GVariant Source #fromGVariant :: GVariant -> IO (Maybe (a, b)) Source #toGVariantFormatString :: (a, b) -> Text Source # (IsGVariant a, IsGVariantBasicType a, IsGVariant b) => IsGVariant (Map a b) Source # MethodstoGVariant :: Map a b -> IO GVariant Source #fromGVariant :: GVariant -> IO (Maybe (Map a b)) Source # Source # Methods (IsGVariant a, IsGVariant b, IsGVariant c) => IsGVariant (a, b, c) Source # MethodstoGVariant :: (a, b, c) -> IO GVariant Source #fromGVariant :: GVariant -> IO (Maybe (a, b, c)) Source #toGVariantFormatString :: (a, b, c) -> Text Source # (IsGVariant a, IsGVariant b, IsGVariant c, IsGVariant d) => IsGVariant (a, b, c, d) Source # MethodstoGVariant :: (a, b, c, d) -> IO GVariant Source #fromGVariant :: GVariant -> IO (Maybe (a, b, c, d)) Source #toGVariantFormatString :: (a, b, c, d) -> Text Source # (IsGVariant a, IsGVariant b, IsGVariant c, IsGVariant d, IsGVariant e) => IsGVariant (a, b, c, d, e) Source # MethodstoGVariant :: (a, b, c, d, e) -> IO GVariant Source #fromGVariant :: GVariant -> IO (Maybe (a, b, c, d, e)) Source #toGVariantFormatString :: (a, b, c, d, e) -> Text Source #

class Ord a => IsGVariantBasicType a Source #

The typeclass for basic type GVariant types, i.e. those that are not containers.

An alias for Nothing :: Maybe GVariant to save some typing.

Get the expected type of a GVariant, in GVariant notation. See https://developer.gnome.org/glib/stable/glib-GVariantType.html for the meaning of the resulting format string.

# Type wrappers

Some GVariant types are isomorphic to Haskell types, but they carry some extra information. For example, there is a tuple singlet type, which is isomorphic to a single Haskell value with the added bit of information that it is wrapped in a tuple container. In order to use these values you can use the following wrappers, which allow the IsGVariant instance to disambiguate the requested type properly.

newtype GVariantSinglet a Source #

Haskell has no notion of one element tuples, but GVariants do, so the following allows for marshalling one element tuples properly using fromGVariant and toGVariant. For instance, to construct a single element tuple containing a string, you could do

toGVariant (GVariantSinglet "Test")

Constructors

 GVariantSinglet a

Instances

 Eq a => Eq (GVariantSinglet a) Source # Methods(==) :: GVariantSinglet a -> GVariantSinglet a -> Bool #(/=) :: GVariantSinglet a -> GVariantSinglet a -> Bool # Show a => Show (GVariantSinglet a) Source # MethodsshowList :: [GVariantSinglet a] -> ShowS # Source # One element tuples. Methods

data GVariantDictEntry key value Source #

A DictEntry GVariant is isomorphic to a two-tuple. Wrapping the values into a GVariantDictentry allows the IsGVariant instance to do the right thing.

Constructors

 GVariantDictEntry key value

Instances

 (Eq key, Eq value) => Eq (GVariantDictEntry key value) Source # Methods(==) :: GVariantDictEntry key value -> GVariantDictEntry key value -> Bool #(/=) :: GVariantDictEntry key value -> GVariantDictEntry key value -> Bool # (Show key, Show value) => Show (GVariantDictEntry key value) Source # MethodsshowsPrec :: Int -> GVariantDictEntry key value -> ShowS #show :: GVariantDictEntry key value -> String #showList :: [GVariantDictEntry key value] -> ShowS # Source # Methods

newtype GVariantHandle Source #

Constructors

 GVariantHandle Int32

Instances

 Source # Methods Source # Methods Source # MethodsshowList :: [GVariantHandle] -> ShowS # Source # Source # Methods

An object representing a DBus object path, which is a particular type of GVariant too. (Just a string with some specific requirements.) In order to construct/deconstruct a GVariantObjectPath one can use newGVariantObjectPath and gvariantObjectPathToText.

Instances

 Source # Methods Source # Methods Source # MethodsshowList :: [GVariantObjectPath] -> ShowS # Source # Source # Methods

Try to construct a DBus object path. If the passed string is not a valid object path Nothing will be returned.

Return the Text representation of a GVariantObjectPath.

An object representing a DBus signature, which is a particular type of GVariant too. (Just a string with some specific requirements.) In order to construct/deconstruct a GVariantSignature one can use newGVariantSignature and gvariantSignatureToText.

Instances

 Source # Methods Source # Methods Source # MethodsshowList :: [GVariantSignature] -> ShowS # Source # Source # Methods

Try to construct a DBus object path. If the passed string is not a valid DBus signature Nothing will be returned.

Return the Text representation of a GVariantSignature.

# Manual memory management

Take ownership of a passed in Ptr (typically created just for us, so if it is floating we sink it).

Construct a Haskell wrapper for the given GVariant, without assuming ownership.

Remove a reference to the given GVariant.

Disown a GVariant, i.e. do not unref the underlying object when the Haskell object is garbage collected.

# Manual conversions

## Basic types

The use of these should be fairly self-explanatory. If you want to convert a Haskell type into a GVariant, use gvariantTo*. If you want to convert a GVariant into a Haskell type, use gvariantFrom*. The conversion can fail if the GVariant is not of the expected type (if you want to convert a GVariant containing a Int16 into a Text value, say), in which case Nothing will be returned.

Extract the DBus handle (an Int32) inside a GVariant.

Convert a DBus handle (an Int32) into a GVariant.

Decode an UTF-8 encoded string GVariant into Text.

Encode a Text into an UTF-8 encoded string GVariant.

Extract a GVariantObjectPath from a GVariant, represented as its underlying Text representation.

Construct a GVariant containing an object path. In order to build a GVariantObjectPath value see newGVariantObjectPath.

Extract a GVariantSignature from a GVariant, represented as Text.

Construct a GVariant containing an DBus signature. In order to build a GVariantSignature value see newGVariantSignature.

## Container type conversions

Unbox a GVariant contained inside another GVariant.

Box a GVariant inside another GVariant.

Extract a zero terminated list of bytes into a ByteString.

Encode a ByteString into a list of bytes GVariant.

gvariantFromMaybe :: forall a. IsGVariant a => Maybe a -> IO GVariant Source #

Convert a Maybe value into a corresponding GVariant of maybe type.

gvariantToMaybe :: forall a. IsGVariant a => GVariant -> IO (Maybe (Maybe a)) Source #

Try to decode a maybe GVariant into the corresponding Maybe type. If the conversion is successful this returns Just x, where x itself is of Maybe type. So, in particular, Just Nothing indicates a successful call, and means that the GVariant of maybe type was empty.

gvariantFromDictEntry :: (IsGVariant key, IsGVariantBasicType key, IsGVariant value) => key -> value -> IO GVariant Source #

Construct a GVariant of type DictEntry from the given key and value. The key must be a basic GVariant type, i.e. not a container. This is determined by whether it belongs to the IsGVariantBasicType typeclass. On the other hand value is an arbitrary GVariant, and in particular it can be a container type.

gvariantToDictEntry :: forall key value. (IsGVariant key, IsGVariantBasicType key, IsGVariant value) => GVariant -> IO (Maybe (key, value)) Source #

Unpack a DictEntry variant into key and value, which are returned as a two element tuple in case of success.

gvariantFromMap :: (IsGVariant key, IsGVariantBasicType key, IsGVariant value) => Map key value -> IO GVariant Source #

Pack a Map into a GVariant for dictionary type, which is just an array of GVariantDictEntry.

gvariantToMap :: forall key value. (IsGVariant key, IsGVariantBasicType key, IsGVariant value) => GVariant -> IO (Maybe (Map key value)) Source #

Unpack a GVariant into a Map. Notice that this assumes that all the elements in the GVariant array of GVariantDictEntry are of the same type, which is not necessary for a generic GVariant, so this is somewhat restrictive. For the general case it is necessary to use gvariantToList plus gvariantToDictEntry directly.

gvariantFromList :: forall a. IsGVariant a => [a] -> IO GVariant Source #

Given a list of elements construct a GVariant array containing them.

gvariantToList :: forall a. IsGVariant a => GVariant -> IO (Maybe [a]) Source #

Unpack a GVariant array into its elements.

Given a list of GVariant, construct a GVariant tuple containing the elements in the list.

Extract the children of a GVariant tuple into a list.