 /**************************************************************************
 *   Copyright (C) 2006 by  Danny Kukawka                                  *
 *                            <dkukawka@suse.de>, <danny.kukawka@web.de>   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License     *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#ifndef _INACTIVITY_H_
#define _INACTIVITY_H_

// KDE Header
#include <kprocess.h>

// TQt Header
#include <tqregexp.h>
#include <tqstring.h>
#include <tqwidget.h>
#include <tqtimer.h>
#include <tqevent.h>

// X11 Header
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

/* needed for lXext C library linkage */
extern "C" {
	#include <X11/extensions/scrnsaver.h>
}

// from project
#include "tdepowersave_debug.h"
#include "screen.h"

/*! 
*  \file 	inactivity.h
*  \brief 	Headerfile for inactivity.cpp and the class \ref inactivity.
*/
/*! 
*  \class 	inactivity
*  \brief 	class for detect inactivity related funtionality
*  \author 	Danny Kukawka, <dkukawka@suse.de>, <danny.kukawka@web.de>
*  \date    	2006
*/

class inactivity : public TQWidget
{
	TQ_OBJECT
  
	
public:
	//! default constructor
	inactivity(screen *disp);
	//! default destructor
	~inactivity();
	
	//! to start the monitoring of the X-Server
	void start(int, TQStringList);
	//! to stop the monitoring of the X-Server
	void stop();
	
	//! to get the current user inactivity
	unsigned long getXInactivity();

signals:

	//! signal emited if the with \ref start() set time of inactivity is expired
	void inactivityTimeExpired();
	//! signal to emit error msg
	void displayErrorMsg( TQString );
	
private:

	//! pointer to the process to call pidof
	TDEProcess *proc;
	
	//! about the call result of pidof 
	/*!
	 * This boolean tells if call of the command pidof failed.
	 * \li true:  if failed
	 * \li false: if not
	 */
	bool pidof_call_failed;
	//! about the call of command pidof
	/*!
	 * This boolean tells if the call of the command pidof was started or if
	 * the command is running
	 * \li true:  if is started/running
	 * \li false: if not
	 */
	bool pidof_call_started;
	//! if pidof return value is recieved
	/*!
	 * This boolean tells if the call of the command pidof returned and the 
	 * returnvalue was parsed to set \ref blacklisted_running
	 * \li true:  if returned and parsed
	 * \li false: if not
	 */
	bool pidof_call_returned;
	//! if a blacklisted program/process is running
	/*! 
	 * This boolean tells if a blacklisted program/process is currently running.
	 * \li true:  if a blacklisted program/process is running
	 * \li false: if not
	 */
	bool blacklisted_running;

	//! TQStringList with blacklisted programs for autosuspend
	TQStringList blacklist;
	
	//! time which must expire before emit signal for autosuspend
	unsigned long timeToInactivity;
	//! time of inactivity from the last check 
	unsigned long idleTime;
	//! time of inactivity from the last check 
	unsigned long blacklisted_running_last;
	//! if the XServer-has XScreenSaverExtension
	int has_XSC_Extension;
	
	//! TQTimer intervall for the \ref checkInactivity Timer
	/*!
	 * The time intervall to check for the current status and time of
	 * userinactivity. The timeslice is currently 30 sec.
	 */
	static const int CHECK_for_INACTIVITY = 10000;

	//! TQTimer for check inactivity
	/*!
	 * This timer is used to check the currently status and time of
	 * user inactivity on the X-Server. The timer interval is defined through
	 * \ref CHECK_for_INACTIVITY .
	 */
	TQTimer *checkInactivity;

	screen *display;  // No ownership here!!
	bool prev_screensaver_enabled;
	unsigned long prev_idle_time;
	unsigned long correction_value;

	// -------- FUNCTIONS ------------
	
	//! to check the user-inactivity on the XServer
	void checkXInactivity();
	//! to check for running blacklisted programs
	void checkBlacklisted();
	//! to monitor the values
	void check( bool recheck ); 
	//! to workaround a strange behavior of the XScreenSaver extension
	unsigned long workaroundCreepyXServer(XScreenSaverInfo *_mitInfo);

private slots:

	//! to monitor the values
	void check();
	//! to monitor the values
	void recheck();
	//! to get the PIDs of blacklisted programs/processes
	void getPIDs(TDEProcess *, char *, int);
	//! to get the signal if the command call is exited
	void getPIDsExited(TDEProcess *);
};

#endif
