module Quipper.Utils.Preview where
import Control.Exception
import System.Environment
import System.Exit
import System.Info
import System.IO
import System.IO.Error
import System.Process
try_several_IO :: [IO a] -> IO a
try_several_IO [] = error "try_several_IO"
try_several_IO [h] = do
catchIOError h (\e -> throw e)
try_several_IO (h:t) = do
catchIOError h (\e -> try_several_IO t)
system_pdf_viewer :: Double -> String -> IO ()
system_pdf_viewer zoom pdffile = do
envList <- getEnvironment
if (elem ("OS", "Windows_NT") envList) then
windows_pdf_viewer zoom pdffile
else if (os == "darwin") then
macos_pdf_viewer zoom pdffile
else
linux_pdf_viewer zoom pdffile
windows_pdf_viewer :: Double -> String -> IO ()
windows_pdf_viewer zoom pdffile = catchIOError body handler
where
body = do
r <- system ("start /WAIT AcroRd32 " ++ pdffile)
case r of
ExitSuccess -> return ()
ExitFailure n -> acrord32_error n
handler e = do
name <- getProgName
hPutStrLn stderr $ name ++ ": unable to preview PDF file. Please ensure that Acrobat Reader is installed."
exitFailure
acrord32_error n = do
name <- getProgName
hPutStrLn stderr $ name ++ ": unable to preview PDF file. Please ensure that Acrobat Reader is installed."
exitFailure
macos_pdf_viewer :: Double -> String -> IO ()
macos_pdf_viewer zoom pdffile = catchIOError body handler
where
body = do
rawSystem "open" [pdffile]
rawSystem "sleep" ["1"]
return ()
handler e = do
name <- getProgName
hPutStrLn stderr $ name ++ ": unable to preview PDF file. There seems to be a problem with Preview."
exitFailure
linux_pdf_viewer :: Double -> String -> IO ()
linux_pdf_viewer zoom pdffile = catchIOError body handler
where
body = do
try_several_IO [
rawSystem "acroread" ["/a", "zoom=" ++ show zoom, pdffile],
rawSystem "xpdf" ["-z", show zoom, pdffile]
]
return ()
handler e = do
name <- getProgName
hPutStrLn stderr $ name ++ ": unable to preview PDF file. Please ensure that either Acrobat Reader or Xpdf are installed."
exitFailure