File: Utils.hs

package info (click to toggle)
hpodder 1.1.5.0%2Bnmu2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 352 kB
  • ctags: 1
  • sloc: haskell: 1,799; makefile: 70; sh: 62
file content (125 lines) | stat: -rw-r--r-- 4,314 bytes parent folder | download | duplicates (2)
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
{- hpodder component
Copyright (C) 2006-2007 John Goerzen <jgoerzen@complete.org>

This program 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 2 of the License, or
(at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

{- |
   Module     : Utils
   Copyright  : Copyright (C) 2006-2007 John Goerzen
   License    : GNU GPL, version 2 or above

   Maintainer : John Goerzen <jgoerzen@complete.org>
   Stability  : provisional
   Portability: portable

Written by John Goerzen, jgoerzen\@complete.org

-}

module Utils where
import System.Console.GetOpt.Utils
import System.Console.GetOpt
import Types
import System.Exit
import Config
import System.Directory
import Database.HDBC
import Data.List.Utils
import System.Time
import System.Time.Utils
import System.IO
import System.Posix.IO
import Control.Exception(finally)

simpleCmd :: IConnection conn => 
          String -> String -> String -> [OptDescr (String, String)] 
          -> (GlobalInfo conn -> ([(String, String)], [String]) -> IO ()) 
          -> (String, Command conn)
simpleCmd name descrip helptext optionsinp func =
    (name, Command {cmdname = name, cmddescrip = descrip,
                    execcmd = worker})
    where options =
              optionsinp ++ [Option "" ["help"] (NoArg ("help", "")) "Display this help"]
          worker argv gi =
              case getOpt RequireOrder options argv of
                (o, n, []) -> 
                    if (lookup "help" o == Just "") 
                       then usageerror []
                       else func gi (o, n)
                (_, _, errors) -> usageerror (concat errors)
          usageerror errormsg =
              do putStrLn $ "Error processing arguments for command " ++ 
                          name ++ ":"
                 putStrLn errormsg
                 putStrLn (usageInfo header options)
                 putStrLn helptext
                 exitFailure
          header = "Available command-options for " ++ name ++ " are:\n"
                                                               

initDirs = 
    do appdir <- getAppDir
       mapM_ mkdir [appdir, appdir ++ "/feedxfer", appdir ++ "/enclosurexfer"]
       where mkdir = createDirectoryIfMissing True

lock func =
    do appdir <- getAppDir
       lockh <- openFile (appdir ++ "/.lock") WriteMode
       lockfd <- handleToFd lockh
       catch (placelock lockfd) errorhandler
       r <- finally func (releaselock lockfd)
       return r

    where placelock lockfd = setLock lockfd (WriteLock, AbsoluteSeek, 0, 0)
          releaselock lockfd = do
               setLock lockfd (Unlock, AbsoluteSeek, 0, 0)
               closeFd lockfd
          errorhandler _ =
              do putStrLn "Aborting because another hpodder is already running"
                 exitFailure


sanitize_basic inp =
    case filter (\c -> not (c `elem` "\n\r\t\0")) inp of
      ('-':x) -> ('_':x)          -- Strip leading hyphen
      x -> x

sanitize_fn inp =
    case map worker . sanitize_basic $ inp of
      [] -> "UNKNOWN"
      x -> x
    where worker x = if x `elem` ";/|!`~ *?%^&(){}[]\\'\"<>:" 
                         then '_'
                         else x

genericIdHelp =
 "You can optionally specify one or more podcast IDs.  If given,\n\
  \only those IDs will be selected for processing.\n\n\
  \The special id \"all\" will select all podcast IDs.\n"

now :: IO Integer
now = do ct <- getClockTime
         return (clockTimeToEpoch ct)

filter_disabled = filter (\x -> pcenabled x == PCEnabled)

-- | Delete files in a given directory, but not the directory itself
emptyDir :: FilePath -> IO ()
emptyDir fp =
    do dircontents <- getDirectoryContents fp
       mapM_ (\f -> catch (removeFile $ fp ++ "/" ++ f) (\_ -> return ()))
                    dircontents