<commit_before>{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
-- |
-- Module      : System.Taffybar.MPRIS2
-- Copyright   : (c) <NAME>
-- License     : BSD3-style (see LICENSE)
--
-- Maintainer  : <NAME>
-- Stability   : unstable
-- Portability : unportable
--
-- This is a "Now Playing" widget that listens for MPRIS events on DBus. Various
-- media players implement this. This widget works with version 2 of the MPRIS
-- protocol (https://specifications.freedesktop.org/mpris-spec/latest/).
-----------------------------------------------------------------------------
module System.Taffybar.MPRIS2 ( mpris2New ) where

import           Control.Monad.Trans
import           Control.Monad.Trans.Reader
import           DBus.Client hiding ( getProperty )
import qualified DBus.TH as DBus
import           Data.List
import           Graphics.UI.Gtk hiding ( Signal, Variant )
import           System.Information.MPRIS2
import           System.Taffybar.Context
import           Text.Printf

mpris2New :: TaffyIO Widget
mpris2New = ask >>= \context -> lift $ do
  ebox <- eventBoxNew
  label <- labelNew (Nothing :: Maybe String)
  containerAdd ebox label
  _ <- on label realize $ runReaderT (initLabel label) context
  widgetShowAll ebox
  return (toWidget ebox)

initLabel :: Label -> TaffyIO ()
initLabel label = do
  client <- asks dbusClient
  let doUpdate =
        getNowPlayingInfo client >>= setLabelText label
      signalCallback _ _ _ _ = doUpdate
      propMatcher =
        matchAny
        { matchPath = Just "/org/mpris/MediaPlayer2"
        }
  lift $ do
    DBus.registerForPropertiesChanged client propMatcher signalCallback
    doUpdate

setLabelText :: Label -> [NowPlaying] -> IO ()
setLabelText label playingInfos =
  let mfirstPlaying = find ((== "Playing") . npStatus) playingInfos
      playingText :: NowPlaying -> String
      playingText NowPlaying { npArtists = artists, npTitle = title } =
        let textPortion :: String<commit_msg>MPRIS2: Remove signal handler when label is destoryed<commit_after>{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
-- |
-- Module      : System.Taffybar.MPRIS2
-- Copyright   : (c) <NAME>
-- License     : BSD3-style (see LICENSE)
--
-- Maintainer  : <NAME>
-- Stability   : unstable
-- Portability : unportable
--
-- This is a "Now Playing" widget that listens for MPRIS events on DBus. Various
-- media players implement this. This widget works with version 2 of the MPRIS
-- protocol (https://specifications.freedesktop.org/mpris-spec/latest/).
-----------------------------------------------------------------------------
module System.Taffybar.MPRIS2 ( mpris2New ) where

import           Control.Monad.Trans
import           Control.Monad.Trans.Reader
import           DBus.Client hiding ( getProperty )
import qualified DBus.TH as DBus
import           Data.List
import           Graphics.UI.Gtk hiding ( Signal, Variant )
import           System.Information.MPRIS2
import           System.Taffybar.Context
import           Text.Printf

mpris2New :: TaffyIO Widget
mpris2New = ask >>= \context -> lift $ do
  ebox <- eventBoxNew
  label <- labelNew (Nothing :: Maybe String)
  containerAdd ebox label
  _ <- on label realize $ runReaderT (initLabel label) context
  widgetShowAll ebox
  return (toWidget ebox)

initLabel :: Label -> TaffyIO ()
initLabel label = do
  client <- asks dbusClient
  let doUpdate =
        getNowPlayingInfo client >>= setLabelText label
      signalCallback _ _ _ _ = doUpdate
      propMatcher =
        matchAny
        { matchPath = Just "/org/mpris/MediaPlayer2"
        }
  lift $ do
    handler <- DBus.registerForPropertiesChanged client propMatcher signalCallback
    _ <- on label unrealize (removeMatch client handler)
    doUpdate

setLabelText :: Label -> [NowPlaying] -> IO ()
setLabelText label playingInfos =
  let mfirstPlaying = find ((== "Playing") . npStatus) playingInfos
      playingText :: NowPlaying -> String
      playingText NowPlaying { npArtists = artists, npTitle = title } =
        let textPortion :: String
{'fraction_non_alphanumeric': 0.15899280575539568, 'fraction_numerical': 0.004556354916067146, 'mean_word_length': 2.1598484848484847, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7124008', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1139, 'n_words': 335}