#ifndef _KPILOT_PILOTDAEMON_H
#define _KPILOT_PILOTDAEMON_H
/* pilotDaemon.h			KPilot
**
** Copyright (C) 1998-2001 by Dan Pilone
** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
**
** See the .cc file for an explanation of what this file is for.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public 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.
**
** You should have received a copy of the GNU General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/


#include <ksystemtray.h>

#include "kpilotlink.h"
#include "syncAction.h"
#include "pilotDaemonDCOP.h"

class TQPixmap;
class TQTimer;
class KAboutApplication;
class TQPopupMenu;

class PilotDaemon;
class ActionQueue;
class FileInstaller;
class LoggerDCOP_stub;
class KPilotDCOP_stub;
class LogFile;

class KPilotLink;
class KPilotDeviceLink;

class PilotDaemonTray : public KSystemTray
{
	Q_OBJECT
	TQ_OBJECT

friend class PilotDaemon;

public:
	PilotDaemonTray(PilotDaemon *p);

	typedef enum { Normal, Busy, NotListening } IconShape ;
	void changeIcon(IconShape);

	void enableRunKPilot(bool);

	virtual void dragEnterEvent(TQDragEnterEvent *);
	virtual void dropEvent(TQDropEvent *);


protected:
	void setupWidget();
	/**
	* Menu of sync types.
	*/
	TQPopupMenu *fSyncTypeMenu;

protected slots:
	void slotShowAbout();
	void slotShowBusy();
	void slotShowNormal();
	void slotShowNotListening();
	void slotBusyTimer();

	// "Regular" QT actions
	//
	//
	virtual void mousePressEvent(TQMouseEvent* e);
	virtual void closeEvent(TQCloseEvent *e);

protected:
	void startHotSync();
	void endHotSync();

private:
	TQPixmap icons[((int) NotListening) + 1];
	IconShape fCurrentIcon;
	PilotDaemon *daemon;

	/**
	* Remember which item in the context menu
	* is "Run KPilot" so we can enable / disable
	* it as necessary.
	*/
	int menuKPilotItem;

	/**
	* Remember which item in the context menu
	* is "Configure Conduits" so we can enable / disable
	* it as necessary.
	*/
	int menuConfigureConduitsItem;

	/**
	* Window for the "About KPilot" information.
	*/
	KAboutApplication *kap;

	/**
	* Timer for blinking.
	*/
	TQTimer *fBlinkTimer;

} ;

class PilotDaemon : public TQObject, virtual public PilotDaemonDCOP
{
  Q_OBJECT
  TQ_OBJECT

// The tray must be our friend so that we can let it stop the daemon.
friend class PilotDaemonTray;


public:
	PilotDaemon();
	~PilotDaemon();

	enum DaemonStatus
  	{
		HOTSYNC_START,    // Hotsync is running
		HOTSYNC_END,      // Hotsync is cleaning up
		FILE_INSTALL_REQ, // A file is being saved for installation
		ERROR,
		READY,            // Connected to device and ready for Sync
		INIT,
		NOT_LISTENING
	};

	DaemonStatus status() const { return fDaemonStatus; } ;
	/* DCOP */ virtual TQString statusString();
	/* DCOP */ virtual TQString shorStatusString();

	/**
	* Display the daemon's system tray icon
	* (if there is one, depending on the DockDaemon
	* setting in the config file)
	*/
	void showTray();
	virtual void addInstallFiles(const TQStringList &);

	// The next few functions are the DCOP interface.
	// Some are also slots.
	//
public slots:
// MOC_SKIP_BEGIN
#ifdef Q_MOC_RUN
	virtual void requestSync(int);
#else // Q_MOC_RUN
// MOC_SKIP_END
	virtual ASYNC requestSync(int);
// MOC_SKIP_BEGIN
#endif // Q_MOC_RUN
// MOC_SKIP_END
public:
	virtual ASYNC requestSyncType(TQString);
	virtual ASYNC requestRegularSyncNext();
	virtual int nextSyncType() const;
	virtual ASYNC requestSyncOptions(bool,bool);

	virtual ASYNC quitNow();
	virtual ASYNC reloadSettings();
	virtual ASYNC setTempDevice(TQString d);

	virtual void stopListening();
	virtual void startListening();
	virtual bool isListening() { return fIsListening; }
	/**
	* Functions reporting same status data, e.g. for the kontact plugin.
	*/
	virtual TQDateTime lastSyncDate();
	virtual TQStringList configuredConduitList();
	virtual TQString logFileName();
	virtual TQString userName();
	virtual TQString pilotDevice();
	virtual bool killDaemonOnExit();

protected:
	DaemonStatus fDaemonStatus;

	enum postSyncActions {
		None=0,
		ReloadSettings = 1,
		Quit = 2
		} ;
	int fPostSyncAction;

protected slots:
	void startHotSync( KPilotLink* lnk );
	void endHotSync();

	void logMessage(const TQString &);
	void logError(const TQString &);
	void logProgress(const TQString &,int);

private:
	int getPilotSpeed();

	/**
	* Check whether we should do a backup.  This is based on the
	* KPilotSettings::backupFrequency and uses
	* SyncAction::BackupFrequency.  This will be expanded, hopefully,
	* to provide backup scheduling at some point.
	*/
	bool shouldBackup();

	bool setupPilotLink();

	KPilotDeviceLink &getPilotLink() { return *fPilotLink; }
	KPilotDeviceLink *fPilotLink;

	SyncAction::SyncMode fNextSyncType;

	ActionQueue *fSyncStack;

	/**
	* This is a pointer to the (optional) docked
	* system tray icon for the daemon.
	*/
	PilotDaemonTray *fTray;

	/**
	* Set or change the tooltip displayed by the tray icon.
	*/
	void updateTrayStatus(const TQString &s=TQString());

	FileInstaller *fInstaller;

protected slots:
	/**
	* Called after a file has been installed to notify any observers, like
	* KPilot, that files have been installed. [Here that means: copied
	* to the pending_install directory and thus *waiting* for
	* installation on the Palm]
	*/
	void slotFilesChanged();

	/**
	* Start up KPilot.
	*/
	void slotRunKPilot();

	/**
	* Run conduit configuration using "kpilot -c"
	*/
	void slotRunConfig();

	/**
	* Provide access to KPilot's DCOP interface through a stub.
	*/
protected:
	LoggerDCOP_stub &getLogger() { return *fLogStub; } ;
	LoggerDCOP_stub &getFileLogger() { return *fLogFileStub; } ;
	KPilotDCOP_stub &getKPilot() { return *fKPilotStub; } ;

	LogFile *fLogFile;
	bool fIsListening;

private:
	LoggerDCOP_stub *fLogStub;
	LoggerDCOP_stub *fLogFileStub;
	KPilotDCOP_stub *fKPilotStub;
	TQString fTempDevice;
};


#endif
