/* 

                          Firewall Builder

                 Copyright (C) 2000 Vadim Kurland

  Author:  Vadim Kurland     vadim@vk.crocodile.org

  $Id: BackgroundOp.hh,v 1.8 2002/05/07 02:07:07 vkurland Exp $


  This program is free software which we release under the GNU General Public
  License. You may redistribute and/or modify this program under the terms
  of that 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.
 
  To get a copy of the GNU General Public License, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


#ifndef __BACKGROUNDOP_HH_FLAG__
#define __BACKGROUNDOP_HH_FLAG__

#include <vector>
#include <iostream>
#ifdef __MINGW32__ //win32 pthread ditrib doesn't have config.h
# ifdef HAVE_CONFIG_H
#  undef HAVE_CONFIG_H
#  include <pthread.h>
#  define HAVE_CONFIG_H
# endif
#else
# include <pthread.h>
#endif

#include <fwbuilder/FWException.hh>
#include <fwbuilder/Tools.hh>
#include <fwbuilder/ThreadTools.hh>
#include <fwbuilder/Pool.hh>
#include <fwbuilder/Logger.hh>

#include <sigc++/signal_system.h>

namespace libfwbuilder
{


/**
 *  Abstract class BackgroundOp represents operatioin executed in background
 */
class BackgroundOp : public SigC::Object
{
    friend void *background_thread(void *);

    private: 

    SyncFlag     stop_program ;
    SyncFlag     running      ;
    SyncFlag     connected    ;

    protected:

    FWException        *error      ;
    SyncFlag           *iamdead    ;

    /**
     * Implementation of the actual operation. Use logger to send output
     * text to the GUI.
     */
    virtual void  run_impl(Logger *) throw(FWException) = 0;

    /**
     * sets flag "running"
     */
    void     setRunning();

    /**
     * clears flag "running"
     */
    void     clearRunning();

    /**
     * checks "stop" flag and throws exception if it is set. Used
     * inside run_impl to check if background operation should be
     * immediately interrupted
     */
    virtual void check_stop() throw(FWException)
    {
        if( stop_program )
            throw FWException("Interrupted by user");
    }
    
    public:
    
    BackgroundOp();
    virtual ~BackgroundOp();

    /**
     * checks whether background operation is connected to GUI
     */
    bool isConnected();

    /**
     * disconnects background operation from the GUI
     */
    void disconnect();

    /**
     * Initiates background operation
     */
    virtual Logger* start_operation()  throw(FWException);

    /**
     * Stops background operation
     */
    virtual void stop_operation();

    /**
     * returns flag "running"
     */
    bool isRunning();

    /**
     * returns last error from the background operation. The meaning of
     * the error is determined by the operation
     */
    FWException *get_latest_error() { return error; }

};

}

#endif // __BACKGROUNDOP_HH_FLAG__
