property pLoadActiveList

on new me
  set pLoadActiveList = []
  
  return me
end new


on mCheckState me
  if count(pLoadActiveList) > 0 then
    repeat with x = 1 to count(pLoadActiveList)
      set myPara = getAt(pLoadActiveList,x)
      set myID   = getAt(myPara,1)
      set myURL  = getAt(myPara,2)
      set myPath = getAt(myPara,3)
      set myType = getAt(myPara,4)
      set myStat = getAt(myPara,5)
      set myDel  = FALSE
      
      case myStat of
        1 :
          mSetStatus(me,"Preloading")
          case mCheckLoadDone(me,myID) of
            1 :
              setAt(myPara,5,2)
              setAt(pLoadActiveList,x,myPara)
            2 :
              netAbort(myID)
              setAt(myPara,5,0)
              set myDel = TRUE
          end case
        2 :
          mSetStatus(me,"Downloading")
          mDownLoadNet(me,myURL,myPath)
          set myID = getLatestNetID()
          setAt(myPara,1,myID)
          setAt(myPara,5,3)
          setAt(pLoadActiveList,x,myPara)
        3 :
          case mCheckLoadDone(me,myID) of
            1 :
              setAt(myPara,5,4)
              setAt(pLoadActiveList,x,myPara)
            2 :
              netAbort(myID)
              setAt(myPara,5,0)
              set myDel = TRUE
          end case
        4 :
          case myType of
            1 :
              -- Image
              mSetLoadFile(me,myPath)
            2 :
              -- Preferences
              mSetLoadPref(me,myPath)
            3 :
              -- Infotext
              mSetLoadInfo(me,myPath)
            4 :
              -- Header
              mSetLoadHead(me,myPath)
          end case
          setAt(myPara,5,0)
          set myDel = TRUE
      end case
    end repeat
    
    if myDel then
      set x = 1
      repeat while myDel
        set myStat = getAt(getAt(pLoadActiveList,x),5)
        if myStat = 0 then
          deleteAt(pLoadActiveList,x)
        else
          set x = x + 1
        end if
        if x > count(pLoadActiveList) then
          set myDel = FALSE
        end if
      end repeat
    end if
    
    if count(pLoadActiveList) < 1 AND NOT(the text of field "status" contains "Error") then
      mSetStatus(me,"-")
    end if
  end if
end mCheckState


on mSetStatus me,newMsg
  if word 1 of the text of field "status" <> word 1 of newMsg then
    set the text of field "status" to newMsg
  end if
end mSetStatus


on mAddLoadNet me,myURL,myPath,myType,mustNew
  Global gLoadState,pLoadActiveList,gProxyIP,gProxyPort
  
  if count(pLoadActiveList) < 4 then 
    if FileExists(the pathName & myPath) = 0 AND NOT(mustNew) then
      if myType <> 2 then
        set the text of field "state_Net" to "L"
      end if
      set myPara = [0,myURL,myPath,myType,4]
      add(pLoadActiveList, myPara)
    else
      if myType <> 2 then
        set the text of field "state_Net" to "N"
      end if
      if gProxyIP <> "" then
        proxyserver(#http,gProxyIP,gProxyPort)
      end if
      mPreLoadNet(me,myURL)
      set myID = getLatestNetID()
      set myPara = [myID,myURL,myPath,myType,1]
      add(pLoadActiveList, myPara)
    end if
    
    return TRUE
  else
    return FALSE
  end if
end mAddLoadNet


on mPreLoadNet me,wichOne  
  
  set myError = preLoadNetThing(wichOne)
end mPreLoadNet


on mDownLoadNet me,wichURL,wichPath
  
  set myError = downLoadNetThing(wichURL,the pathName & wichPath)
end mDownLoadNet


on mSetLoadFile me,wichOne
  Global gGifObj
  
  set myError = FileExists(the pathName & wichOne)
  
  if myError = 0 then
    mChangePict(gGifObj,wichOne)
  else
    mFileUtilErr(me,myError)
  end if
end mSetLoadFile


on mSetLoadHead me,wichOne
  Global gGifObj
  
  set myError = FileExists(the pathName & wichOne)
  if myError = 0 then
    set the fileName of member "TheHead" to the pathName & wichOne
  else
    mFileUtilErr(me,myError)
  end if
end mSetLoadHead


on mSetLoadPref me,wichOne
  Global gContent,gFirstTextList,gGifObjPath,gLimit,gFontObj
  
  set myError = FileExists(the pathName & wichOne)
  
  if myError = 0 then
    set filePnt = new(xtra "fileio")
    openFile(filePnt,the pathName & wichOne,1)
    
    set myText = readFile(filePnt)
    closeFile(filePnt)
    set gContent = MakeListFromText(mChangeFontMap(gFontObj,KillUnixLF(myText)),"#",2,1,17)
    set myHead = GetStringTo(getAt(gFirstTextList,1),"#",TRUE)
    mAddLoadNet(me,gGifObjPath & myHead,"data" & gLimit & myHead,4,false)
    SetFieldValue()
  else
    mFileUtilErr(me,myError)
  end if
end mSetLoadPref


on mSetLoadInfo me,wichOne
  Global gContent,gFontObj
  
  set myError = FileExists(the pathName & wichOne)
  if myError = 0 then
    set filePnt = new(xtra "fileio")
    openFile(filePnt,the pathName & wichOne,1)
    
    set myText = mChangeFontMap(gFontObj,KillUnixLF(readFile(filePnt)))
    closeFile(filePnt)
    set the text of field "info_sta" to line 1 of myText
    set the text of field "info_url" to line 2 of myText
    set the text of field "info_text" to line 3 to the number of lines in myText of myText
    set filePnt = EMPTY
  else
    mFileUtilErr(me,myError)
  end if
end mSetLoadInfo


on mCheckPref me,wichOne
  if FileExists(the pathName & wichOne) = 0 then
    set filePnt = new(xtra "fileio")
    openFile(filePnt,the pathName & wichOne,1)
    setPosition(filePnt,0)
    set myText = readLine(filePnt)
    closeFile(filePnt)
    
    if GetStringFrom(myText,"#",TRUE) = "y" then
      mKillPref(me,wichOne)
    end if
    set filePnt = EMPTY
  end if
end mCheckPref


on mKillPref me,wichOne
  DeleteFile(the pathName & wichOne)
end mKillPref


on mCheckLoadDone me,theIP
  if netDone(theIP) then
    set myError = netError(theIP)
    if myError <> "OK" then
      mSetStatus(me,"Network Error" && myError)
    end if
    set myResult = 1
  else
    set myResult = 0
  end if
  
  return myResult
end mCheckLoadDone


on mFileUtilErr me,wichOne
  case wichOne of
    0: 
      set message to "successful completion"
    -1: 
      set message to "General error of unknown origin"
    -5: 
      set message to "File deletion failure"
    -6: 
      set message to "File rename failure"
    -7: 
      set message to "File not found"
    -8: 
      set message to "Specified file is actually a directory"
    -9: 
      set message to "File creation failure"
    -10: 
      set message to "File open failure"
    -11: 
      set message to "File write failure"
    -12: 
      set message to "File close failure"
    -13: 
      set message to "File read failure"
    -14: 
      set message to "Destination disk full"
    -15: 
      set message to "Directory not found"
    -16: 
      set message to "Specified directory is actually a file"
    -17: 
      set message to "Directory creation failure"
    -18: 
      set message to "Could not delete specified directory"
    -19: 
      set message to "Could not retrieve directory ID number"
    -40: 

      set message to "Could not allocate memory for file copy"
    -210: 
      set message to "New filename already exists or two paths are different"
    otherwise 
      set message to "unknown error code"
  end case
  
  if NOT(the text of field "status" contains "Error") then
    mSetStatus(me,message)
  end if
end mFileUtilErr
