------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2009 Vadim Godunko <vgodunko@gmail.com>                      --
--                                                                          --
-- QtAda is free software;  you can  redistribute it and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion. QtAda is distributed in the hope that it will be useful, but WITH- --
-- OUT 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 distributed with QtAda;  see file COPYING.  If not, write --
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
-- Boston, MA 02110-1301, USA.                                              --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3670 $ $Date: 2009-12-21 09:36:28 +0300 (Mon, 21 Dec 2009) $
------------------------------------------------------------------------------
with Qt_Ada.Object_Unchecked_Deallocation;

with Emitters;

package body Jobs is

   ---------
   -- Job --
   ---------

   task body Job is

      use type Qt4.Q_Integer;

      procedure Free is
        new Qt_Ada.Object_Unchecked_Deallocation
             (Emitters.Emitter, Emitters.Emitter_Access);

      E : Emitters.Emitter_Access;
      K : Qt4.Q_Integer := 0;

   begin
      accept Initialize (Emitter : out Qt4.Objects.Q_Object_Pointer) do
         E := Emitters.Create;
         Emitter.Set (Qt4.Objects.Q_Object_Access (E));
      end Initialize;

      for J in Qt4.Q_Integer'(1) .. 100 loop
         select
            accept Stop;

            K := J;

            exit;

         or
            delay 0.1;
         end select;

         E.Set_Progress_Value (J);
      end loop;

      if K /= 0 then
         for J in reverse 1 .. K loop
            delay 0.01;
            E.Set_Progress_Value (J);
         end loop;
      end if;

      E.Set_Completed;

      Free (E);
   end Job;

end Jobs;
