aboutsummaryrefslogtreecommitdiffhomepage
path: root/Main.hs
blob: 5a5bb3fdffc0e9ed995c8dbfbbd92c8563c99d9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
{-

sscan --- text UI for scanning with SANE

Copyright (C) 2017  Sean Whitton

This file is part of sscan.

sscan is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

sscan is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with sscan.  If not, see <http://www.gnu.org/licenses/>.

-}

{-# LANGUAGE OverloadedStrings #-}

import           Control.Concurrent.Async (concurrently_)
import           Control.Monad            (unless, void, when)
import           Data.Time.Clock.POSIX    (getPOSIXTime, posixSecondsToUTCTime)
import           Data.Time.Format         (defaultTimeLocale, formatTime,
                                           iso8601DateFormat)
import           Lens.Micro               ((^.))
import           System.Directory         (getHomeDirectory, removeFile,
                                           renamePath, withCurrentDirectory)
import           System.Exit              (ExitCode (..))
import           System.FilePath          ((<.>), (</>))
import           System.IO                (IOMode (WriteMode), hClose,
                                           hGetContents, hPutStr, openFile,
                                           withBinaryFile, withFile)
import           System.IO.Temp           (withSystemTempDirectory)
import           System.Process

import           Types.State
import           UI

processScanSessDir :: St -> FilePath -> IO ()
processScanSessDir st dir = withCurrentDirectory dir $ do
    posix <- getPOSIXTime
    let stamp = show (round posix :: Int)
    logH <- openFile (logFile stamp) WriteMode -- TODO maybe AppendMode?
    void $ case st^.stOutFormat of
      PDF -> do
          -- 1. convert tiff->PDF
          void $ createProcessWait_ "convert"
              (proc "convert" (allPages ++ ["temp.pdf"]))
          -- 2. set metadata with pdftk
          renamePath (dir </> "temp.pdf") (dir </> "temp2.pdf")
          writeFile "metadata" (metadata posix)
          void $ createProcessWait_ "pdftk"
              (proc "pdftk"
                ["temp2.pdf", "update_info", "metadata", "output", "temp.pdf"])
              { std_in = NoStream
              , std_out = NoStream
              , std_err = UseHandle logH
              }
          -- 3. maybe ocrmypdf
          when (st^.stOCR) $ do
              renamePath (dir </> "temp.pdf") (dir </> "temp2.pdf")
              -- OCRmyPDF dies if stdout is not connected, so tell it
              -- to output to stdout
              withBinaryFile "temp.pdf" WriteMode $ \tempFile -> do
                (_, _, Just herr, p) <- createProcess_ "OCRmyPDF"
                -- we'd like to use --remove-background here, but then
                -- qpdf says that ocrmypdf's output is damaged.
                -- reported upstream
                  (proc "ocrmypdf" ["-c", "-i", "-r", "temp2.pdf", "-"])
                  { std_in = NoStream
                  , std_out = UseHandle tempFile
                  , std_err = CreatePipe
                  }
                -- OCRmyPDF does not yet have a --quiet option, so we
                -- emulate the chronic(1) utility
                exitCode <- waitForProcess p
                unless (exitCode == ExitSuccess) $
                  hGetContents herr >>= hPutStr logH
                hClose herr
          -- 4. qpdf (ocrmypdf invokes qpdf but it doesn't use
          -- --linearize, which shrinks the PDF, often substantially)
          void $ createProcessWait_ "qpdf"
              (proc "qpdf" ["--linearize", "temp.pdf", outFile stamp])
              { std_in = NoStream
              , std_out = NoStream
              , std_err = UseHandle logH
              }
      -- assume that only one page was scanned.  Not clear how we
      -- can avoid this assumption when producing a PNG
      PNG -> void $ createProcessWait_ "convert"
        (proc "convert" ["page1" <.> "tiff", outFile stamp])
          { std_in = NoStream
          , std_out = NoStream
          , std_err = UseHandle logH
          }
    hClose logH
    -- clean up the log file if it's empty
    finalLog <- readFile (logFile stamp)
    when (null finalLog) $ removeFile (logFile stamp)
  where
      logFile stamp = (st^.stOutdir) </> stamp <.> "log"
      outFile stamp = (st^.stOutdir) </> stamp <.> outExt
      outExt = case st^.stOutFormat of
        PDF -> "pdf"
        PNG -> "png"
      allPages = map (\n -> "page" ++ show n <.> "tiff") [1..(getLatestPage st)]
      metadata posix = let date = formatTime
                                  defaultTimeLocale
                                  (iso8601DateFormat Nothing)
                                  (posixSecondsToUTCTime posix)
                       in unlines [ "InfoBegin"
                                  , "InfoKey: Title"
                                  , "InfoValue: scan of " ++ date
                                  ]

makeInitialState :: IO St
makeInitialState = do
    home <- getHomeDirectory
    papersize <- init <$> readFile "/etc/papersize"
    let paper = if papersize == "letter" then Letter else A4
    return St
        { _stScanSess     = Nothing
        , _stOCR          = True
        , _stColour       = Greyscale
        , _stPaper        = paper
        , _stDefaultPaper = paper
        , _stDPI          = 300
        , _stOutFormat    = PDF
        , _stOutdir       = home </> "tmp"
        }

scanPage :: St -> FilePath -> IO Bool
scanPage st dir = withFile outF WriteMode $ \outH -> do
    exit <- createProcessWait_ "scanimage" (proc "scanimage" (scanimageArgs st))
        { std_in = NoStream
        , std_out = UseHandle outH
        , std_err = Inherit     -- let the user see progress bar
        }
    case exit of
      ExitSuccess -> return True
      ExitFailure c -> do
          putStrLn $ "scanimage exited with exit code " ++ show c ++ "!"
          putStrLn "This might because sscan tried to use an option that your"
          putStrLn "scanner does not support.  Please try another preset."
          putStrLn "Press any key to abort this scanning session..."
          void getChar
          return False
  where
      outF = dir </> "page" ++ (show $ getLatestPage st + 1) <.> "tiff"

scanimageArgs :: St -> [String]
scanimageArgs st =
    [ "-vp"
    , "--format=tiff"
    , "--resolution=" ++ show (st^.stDPI)
    , "--mode=" ++ case st^.stColour of
        Colour    -> "Color"
        Greyscale -> "Gray"
        Lineart   -> "Lineart"
    ] ++ case st^.stPaper of
           Auto -> ["--swcrop=yes"]
           _ -> [ "-x"
                , show $ case st^.stPaper of
                    A4     -> 210   :: Double
                    Letter -> 215.9 :: Double
                    Photo  -> 150   :: Double
                    Auto   -> 1     :: Double
                , "-y"
                , show $ case st^.stPaper of
                    A4     -> 297   :: Double
                    Letter -> 279.4 :: Double
                    Photo  -> 100   :: Double
                    Auto   -> 1     :: Double
                ]
-- other device-specific scanimage options the old script supported:
-- --swdespeck; --color-filter; --depth

-- TODO include usbreset.c as a cbit, and use FFI to optionally reset
-- the scanner's USB connection after a scan -- this is needed on some
-- scanners that I occasionally use

processCommand :: St -> IO ()
processCommand st = case st^.stScanSess of
  Nothing -> putStrLn "Waiting for OCR threads to complete..."
    >> return ()          -- quit sscan
  Just (ScanSess command _ maybeDir) -> case maybeDir of
    Nothing -> withSystemTempDirectory "sscan" $ \dir ->
      processCommand (setScanSessDir dir st)
    Just dir -> case command of
      Abort -> newSession
      NextPage -> scanPage st dir >>= \scanned ->
        if scanned
        then presentUI (incrementPages st)
        else newSession
      FinalPage -> scanPage st dir
                >> finaliseSession (incrementPages st) dir
      Finalise -> finaliseSession st dir
  where
    newSession = presentUI $ resetScanSess st
    finaliseSession st' dir =
        concurrently_ (processScanSessDir st' dir) newSession

presentUI    :: St -> IO ()
presentUI st = runTheApp st >>= processCommand

main :: IO ()
main = makeInitialState >>= presentUI

-- | Create a process, wait for it to finish, don't close any
-- handles used by the CreateProcess record
createProcessWait_ :: String -> CreateProcess -> IO ExitCode
createProcessWait_ s c = do
    (_, _, _, p) <- createProcess_ s c
    waitForProcess p