void KrecipesView::wizard( bool force )
{
	KConfigGroup config = KGlobal::config()->group( "Wizard" );
	bool setupDone = config.readEntry( "SystemSetup", false );

	QString setupVersion = config.readEntry( "Version", "0.3" );  // By default assume it's 0.3. This parameter didn't exist in that version yet.

	if ( !setupDone || ( setupVersion.toDouble() < 0.5 ) || force )  // The config structure changed in version 0.4 to have DBType and Config Structure version
	{

		bool setupUser, initData, doUSDAImport, adminEnabled;
		QString adminUser, adminPass, user, pass, host, client, dbName;
		int port;
		bool isRemote;

		QPointer<SetupAssistant> setupAssistant = new SetupAssistant( this );
		if ( setupAssistant->exec() == QDialog::Accepted )
		{
			config.sync();
			config = KGlobal::config()->group( "DBType" );
			dbType = config.readEntry( "Type", "SQLite" );

			kDebug() << "Setting up" ;
			setupAssistant->getOptions( setupUser, initData, doUSDAImport );
			kDebug()<<" setupUser :"<<setupUser<<" initData :"<<initData<<" doUSDAImport :"<<doUSDAImport;
			// Setup user if necessary
			if ( ( dbType == "MySQL" || dbType == "PostgreSQL" ) && setupUser )  // Don't setup user if checkbox of existing user... was set
			{
				kDebug() << "Setting up user\n";
				setupAssistant->getAdminInfo( adminEnabled, adminUser, adminPass, dbType );
				setupAssistant->getServerInfo( isRemote, host, client, dbName, user, pass, port );

				if ( !adminEnabled )  // Use root without password
				{
					kDebug() << "Using default admin\n";
					if ( dbType == "MySQL" )
						adminUser = "root";
					else if ( dbType == "PostgreSQL" )
						adminUser = "postgres";
					adminPass.clear();
				}
				if ( !isRemote )  // Use localhost
				{
					kDebug() << "Using localhost\n";
					host = "localhost";
					client = "localhost";
				}

				setupUserPermissions( host, client, dbName, user, pass, adminUser, adminPass, port );
			}

			// Initialize database with data if requested
			if ( initData ) {
				kDebug();
				setupAssistant->getServerInfo( isRemote, host, client, dbName, user, pass, port );
				initializeData( host, dbName, user, pass, port ); // Populate data as normal user
			}

			if ( doUSDAImport ) {
				kDebug()<<" import USDA";
				// Open the DB first
				setupAssistant->getServerInfo( isRemote, host, client, dbName, user, pass, port ); //only used if needed by backend
				kDebug()<<" dbName :"<<dbName<<" user :"<<user<<" pass :"<<pass<<" port :"<<port;
				RecipeDB *db = RecipeDB::createDatabase( dbType, host, user, pass, dbName, port, dbName );
				kDebug()<<" database created :"<<db;
				// Import the data
				if ( db ) {
					kDebug()<<" try to connect";
					db->connect();

					if ( db->ok() ) {
						ProgressInterface pi(this);
						pi.listenOn(db);
						db->importUSDADatabase();
					}
					kDebug()<<" close";
					//close the database whether ok() or not
					delete db;
				}
			}

			//we can do a faster usda import if this is done after it
			if ( initData ) {
				kDebug()<<" initData :"<<initData;
				RecipeDB *db = RecipeDB::createDatabase( dbType, host, user, pass, dbName, port, dbName );
				if ( db ) {
					kDebug()<<" dbName :"<<dbName<<" user :"<<user<<" pass :"<<pass<<" port :"<<port;
					db->connect();

					if ( db->ok() ) {
						kDebug()<<" import sample";
						db->importSamples();
					}

					//close the database whether ok() or not
					kDebug()<<" close db";
					delete db;
				}
			}

		}
		delete setupAssistant;
	}
}
void KrecipesView::wizard( bool force )
{
	KConfigGroup config = KGlobal::config()->group( "Wizard" );
	bool setupDone = config.readEntry( "SystemSetup", false );

	QString setupVersion = config.readEntry( "Version", "0.3" );   

	if ( !setupDone || ( setupVersion.toDouble() < 0.5 ) || force )   
	{

		bool setupUser, initData, doUSDAImport, adminEnabled;
		QString adminUser, adminPass, user, pass, host, client, dbName;
		int port;
		bool isRemote;

		QPointer<SetupAssistant> setupAssistant = new SetupAssistant( this );
		if ( setupAssistant->exec() == QDialog::Accepted )
		{
			config.sync();
			config = KGlobal::config()->group( "DBType" );
			dbType = config.readEntry( "Type", "SQLite" );

			kDebug() << "Setting up" ;
			setupAssistant->getOptions( setupUser, initData, doUSDAImport );
			kDebug()<<" setupUser :"<<setupUser<<" initData :"<<initData<<" doUSDAImport :"<<doUSDAImport;
			 
			if ( ( dbType == "MySQL" || dbType == "PostgreSQL" ) && setupUser )   
			{
				kDebug() << "Setting up user\n";
				setupAssistant->getAdminInfo( adminEnabled, adminUser, adminPass, dbType );
				setupAssistant->getServerInfo( isRemote, host, client, dbName, user, pass, port );

				if ( !adminEnabled )   
				{
					kDebug() << "Using default admin\n";
					if ( dbType == "MySQL" )
						adminUser = "root";
					else if ( dbType == "PostgreSQL" )
						adminUser = "postgres";
					adminPass.clear();
				}
				if ( !isRemote )   
				{
					kDebug() << "Using localhost\n";
					host = "localhost";
					client = "localhost";
				}

				setupUserPermissions( host, client, dbName, user, pass, adminUser, adminPass, port );
			}

			 
			if ( initData ) {
				kDebug();
				setupAssistant->getServerInfo( isRemote, host, client, dbName, user, pass, port );
				initializeData( host, dbName, user, pass, port );  
			}

			if ( doUSDAImport ) {
				kDebug()<<" import USDA";
				 
				setupAssistant->getServerInfo( isRemote, host, client, dbName, user, pass, port );  
				kDebug()<<" dbName :"<<dbName<<" user :"<<user<<" pass :"<<pass<<" port :"<<port;
				RecipeDB *db = RecipeDB::createDatabase( dbType, host, user, pass, dbName, port, dbName );
				kDebug()<<" database created :"<<db;
				 
				if ( db ) {
					kDebug()<<" try to connect";
					db->connect();

					if ( db->ok() ) {
						ProgressInterface pi(this);
						pi.listenOn(db);
						db->importUSDADatabase();
					}
					kDebug()<<" close";
					 
					delete db;
				}
			}

			 
			if ( initData ) {
				kDebug()<<" initData :"<<initData;
				RecipeDB *db = RecipeDB::createDatabase( dbType, host, user, pass, dbName, port, dbName );
				if ( db ) {
					kDebug()<<" dbName :"<<dbName<<" user :"<<user<<" pass :"<<pass<<" port :"<<port;
					db->connect();

					if ( db->ok() ) {
						kDebug()<<" import sample";
						db->importSamples();
					}

					 
					kDebug()<<" close db";
					delete db;
				}
			}

		}
		delete setupAssistant;
	}
}