/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *         (C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#ifndef __PKGUPGRADEENGINE_H
#define __PKGUPGRADEENGINE_H

#include <qptrlist.h>
#include <qstringlist.h>

#include <package.h>

class UnixProcess;

/**
  * @brief Upgrade or install packages or ports.
  *
  * This class creates a QProcess that applies
  * specified actions on a given list of packages.
  *
  * The portupgrade suite must be installed for this.
  * Commands like [-PP] indicate the corresponding
  * portupgrade(1) switch.
  */
class PkgUpgradeEngine{
public:
	enum PackageUse {
		UsePorts = 0,			/**< Compile every package from source.	[default]	*/
		UsePackages = 1,		/**< Try to use packages when available.	[-P]			*/
		UsePackagesOnly = 2	/**< Only use packages, even if outdated.	[-PP]			*/
	};

	enum Recursion {
		NoRecursion = 0,			/**< [default]															*/

		Recursive = 0x2,			/**< "Act on all those packages depending on the given
												packages as well." [-r]										*/

		UpwardRecursive = 0x4	/**< "Act on all those packages required by the given
												packages as well." [-R]										*/
	};

   PkgUpgradeEngine();

	UnixProcess * getProcess();


	void  loadSettings();


	/** @param value TRUE if you wish to install a specified
	  *              package if it isn't already installed.
	  *              FALSE [default]: Ignore such packages.
	  *
	  * portupgrade switch: [-N]
	  *
	  * Please note: portupgrade(1) upgrades all packages required
	  *              by such a package prior to it's installation.
	  */
	void	setInstallNewPkgs(bool value)	{ installNewPkgs = value;	}



	/** Sets the packages that should be processed.
	  * This doesn't keep copies of the given objects, but
	  * only of pointers, so don't these objects!
	  */
	void	setPackages(QPtrList<Package>  pkgs) { packages = pkgs;	}



	/** Specify how packages are used in the upgrade/installation process.
	  *
	  * @see PackageUse
	  */
	void	setPackageUse(PackageUse pkgUse) { packageUse = pkgUse;	}



	/** Adds arguments to make(1) [-m] */
	void	addMakeArgs(QStringList args) { makeArgs += args;			}
	
	
	/** Add key=value environment variable pairs for make(1) [-M] */
	void	addMakeEnv(QStringList env) { makeEnv += env;				}


	/** This function sets if and which types of
	  * recursion should be used.
	  *
	  * @param recMask Values from enum Recursion Or'ed together,
	  *                e.g. NoRecursion, Recursive|UpwardRecursive
	  *
	  * @see Recursion
	  */
	void  setRecursion(int  recMask) { recursion = recMask;			}



	/** "Force the upgrade of a package even if some of the requisite
	  * packages have failed to upgrade in advance." [-k]
	  */
	void	setKeepGoing(bool  kgoing) { keepGoing = kgoing;			}


	/**
	  * Returns the system's default package repository, e.g.
	  * "6.0-release", "6-stable", "current"
	  *
	  */
	static QString readDefaultRepository();

protected:
	static QStringList getEnvironment();

private:
	int         recursion;
	bool			installNewPkgs;
	bool			keepGoing;
	PackageUse	packageUse;
	QPtrList<Package>  packages;
	QStringList	makeArgs;
	QStringList	makeEnv;
};

#endif
