Permalink
Browse files

Fix some potential crashes in updateSongInfo (MPRIS2)

I was seeing crashes if I started this widget with no song
playing (also when stopping a song).  I removed the irrefutable
pattern matches in updateSongInfo and allowed it to fail gracefully if
the metadata fields were not populated.
  • Loading branch information...
1 parent 4cad2af commit cb19305bb83d0e3ea9ca3af7ade6448c7b02d516 @travitch committed Nov 23, 2012
Showing with 13 additions and 7 deletions.
  1. +13 −7 src/System/Taffybar/MPRIS2.hs
@@ -6,6 +6,7 @@
--
module System.Taffybar.MPRIS2 ( mpris2New ) where
+import Data.Maybe ( listToMaybe )
import DBus
import DBus.Client
import Data.List (isPrefixOf)
@@ -93,12 +94,18 @@ updatePlaybackStatus w items = do
return ()
updateSongInfo :: Label -> [(Variant, Variant)] -> IO ()
-updateSongInfo w items = do
- let Just aLookup = lookup (toVariant ("xesam:artist" :: String)) items
- artists = (unpack . unpack) aLookup :: [String]
- Just tLookup = lookup (toVariant ("xesam:title" :: String)) items
- title = (unpack . unpack) tLookup :: String
- setSongInfo w (artists !! 0) title
+updateSongInfo w items =
+ case parseArtistAndTitle of
+ Just (artist, title) -> setSongInfo w artist title
+ Nothing -> return ()
+ where
+ parseArtistAndTitle = do
+ aLookup <- lookup (toVariant ("xesam:artist" :: String)) items
+ tLookup <- lookup (toVariant ("xesam:title" :: String)) items
+ let artists = (unpack . unpack) aLookup :: [String]
+ title = (unpack . unpack) tLookup :: String
+ artist <- listToMaybe artists
+ return (artist, title)
updateMetadata :: Label -> [(Variant, Variant)] -> IO ()
updateMetadata w items = do
@@ -107,4 +114,3 @@ updateMetadata w items = do
let metaItems = dictionaryItems $ (unpack . unpack) meta
updateSongInfo w metaItems
Nothing -> return ()
-

0 comments on commit cb19305

Please sign in to comment.