--------------------------------------------------------------------------------
--                                                                            --
-- Copyright (C) 2004, RISC OS Ada Library (RASCAL) developers.               --
--                                                                            --
-- This library is free software; you can redistribute it and/or              --
-- modify it under the terms of the GNU Lesser General Public                 --
-- License as published by the Free Software Foundation; either               --
-- version 2.1 of the License, or (at your option) any later version.         --
--                                                                            --
-- This library 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           --
-- Lesser General Public License for more details.                            --
--                                                                            --
-- You should have received a copy of the GNU Lesser General Public           --
-- License along with this library; if not, write to the Free Software        --
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    --
--                                                                            --
--------------------------------------------------------------------------------

-- $Author$
-- $Date$
-- $Revision$

with RASCAL.OS;
with RASCAL.Memory;           use RASCAL.Memory;

with Kernel;                  use Kernel;
with Interfaces.C;            use Interfaces.C;
with System.Storage_Elements; use System.Storage_Elements;


package body RASCAL.FilerAction is

   FilerAction_SendStartOperation    : constant Interfaces.C.unsigned := 16#40F82#;
   FilerAction_SendSelectedDirectory : constant Interfaces.C.unsigned := 16#40F80#;
   FilerAction_SendSelectedFile      : constant Interfaces.C.unsigned := 16#40F81#;

   --

   procedure Set_Selection_Directory (Block: in out Wimp_Block_Type;
                                      Path : in string ) is

      Null_Terminated_Path : String(Path'first..Path'last+1)
                           := Path & Character'Val(0);

      Wimp_SendMessage  : constant := 16#400E7#;
      Register          : aliased Kernel.swi_regs;
      Error             : OSError_Access;
   begin
      Block(0) := 20 + Path'Length + 1;
      Block(3) := 0;
      Block(4) := 403; -- FilerSelectionDirectory
      StringToMemory(Null_Terminated_Path,Block(5)'Address);

      Register.R(0) := int(17);
      Register.R(1) := int(To_Integer(Block'Address));
      Register.R(2) := int(0);
      Error := Kernel.swi ( Wimp_SendMessage, register'Access, register'Access );

      if Error /= null then
         pragma Debug(Report("FilerAction:Set_Selection_Directory: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Set_Selection_Directory;

   --

   procedure Send_Selected_Directory (Directory   : in string;
                                      Task_Handle : in integer) is

      Register        : aliased Kernel.swi_regs;
      Error           : OSError_Access;
   begin
      Register.R(0) := int(Task_Handle);
      Register.R(1) := int(To_Integer(Directory'Address));
      Error := Kernel.SWI ( FilerAction_SendSelectedDirectory, register'Access, register'Access );

      if Error /= null then
         pragma Debug(Report("FilerAction:Send_Selected_Directory: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Send_Selected_Directory;

   --

   procedure Send_Selected_File (Filename    : in string;
                                 Task_Handle : in integer) is

      Register        : aliased Kernel.swi_regs;
      Null_Filename   : string := Filename & Character'Val(0);
      Error           : OSError_Access;
   begin
      Register.R(0) := int(Task_Handle);
      Register.R(1) := int(To_Integer(Null_Filename'Address));
      Error := Kernel.SWI ( FilerAction_SendSelectedFile, register'Access, register'Access );

      if Error /= null then
         pragma Debug(Report("FilerAction:Send_Selected_File: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;

   end Send_Selected_File;

   --

   procedure Send_Start_Operation (Path        : in string;
                                   Operation   : in FilerAction_Operation_Type;
                                   Task_Handle : in integer;
                                   Options     : in FilerAction_Option_Type := Verbose) is

      Register  : aliased Kernel.swi_regs;
      Null_Path : string := Path & Character'Val(0);
      Error     : OSError_Access;
   begin
      Register.R(0) := int(Task_Handle);
      Register.R(1) := int(FilerAction_Operation_Type'Pos(Operation));
      Register.R(2) := 1;
      Register.R(3) := int(To_Integer(Null_Path'Address));
      Register.R(4) := Path'Length;
      Error := Kernel.SWI ( FilerAction_SendStartOperation, register'Access, register'Access );

      if Error /= null then
         pragma Debug(Report("FilerAction:Send_Start_Operation: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Send_Start_Operation;

   --
   
end RASCAL.FilerAction;
