module Darcs.Util.Download.Request
( UrlRequest(..)
, Cachable(..)
, UrlState(..)
, Q(..)
, readQ
, insertQ
, pushQ
, addUsingPriority
, deleteQ
, elemQ
, emptyQ
, nullQ
, Priority(..)
, ConnectionError(..)
) where
import Prelude ()
import Darcs.Prelude
import Data.List ( delete )
import Data.Map ( Map )
import Foreign.C.Types ( CInt )
data Priority = High
| Low
deriving Eq
data Cachable = Cachable
| Uncachable
| MaxAge !CInt
deriving (Show, Eq)
data UrlRequest = UrlRequest
{ url :: String
, file :: FilePath
, cachable :: Cachable
, priority :: Priority
}
type InProgressStatus = ( FilePath
, [FilePath]
, Cachable
)
data UrlState = UrlState
{ inProgress :: Map String InProgressStatus
, waitToStart :: Q String
, pipeLength :: Int
, randomJunk :: String
}
data Q a = Q [a] [a]
readQ :: Q a -> Maybe (a, Q a)
readQ (Q (x : xs) ys) = return (x, Q xs ys)
readQ (Q [] ys) = do
x : xs <- return $ reverse ys
return (x, Q xs [])
addUsingPriority :: Priority -> a -> Q a -> Q a
addUsingPriority High = pushQ
addUsingPriority Low = insertQ
insertQ :: a -> Q a -> Q a
insertQ y (Q xs ys) = Q xs (y:ys)
pushQ :: a -> Q a -> Q a
pushQ x (Q xs ys) = Q (x:xs) ys
deleteQ :: Eq a => a -> Q a -> Q a
deleteQ x (Q xs ys) = Q (delete x xs) (delete x ys)
elemQ :: Eq a => a -> Q a -> Bool
elemQ x (Q xs ys) = x `elem` xs || x `elem` ys
emptyQ :: Q a
emptyQ = Q [] []
nullQ :: Q a -> Bool
nullQ (Q [] []) = True
nullQ _ = False
data ConnectionError = CouldNotResolveHost
| CouldNotConnectToServer
| OperationTimeout
deriving (Eq, Read, Show)