bool pEnvironmentVariablesManager::mergeNewVariables( pEnvironmentVariablesModel::Variables& variables ) const
{
	const pEnvironmentVariablesModel::Variables newVariables = pEnvironmentVariablesModel::stringListToVariables( QProcess::systemEnvironment() );
	bool modified = false;
	
	foreach ( const QString& name, newVariables.keys() )
	{
		if ( variables.contains( name ) )
		{
			continue;
		}
		
		modified = true;
		variables[ name ] = newVariables[ name ];
	}
	
	return modified;
}
bool pEnvironmentVariablesManager::removeUnmodifiedVariables( pEnvironmentVariablesModel::Variables& variables ) const
{
	const pEnvironmentVariablesModel::Variables sysVariables = pEnvironmentVariablesModel::stringListToVariables( QProcess::systemEnvironment() );
	bool modified = false;
	
	foreach ( const pEnvironmentVariablesModel::Variable& variable, variables )
	{
		if ( !variable.enabled )
		{
			continue;
		}
		
		if ( sysVariables.contains( variable.name ) && variable.value == sysVariables[ variable.name ].value )
		{
			variables.remove( variable.name );
			modified = true;
		}
	}
	
	return modified;
}
bool pEnvironmentVariablesManager::writeVariables( const pEnvironmentVariablesModel::Variables& variables ) const
{
	pSettings settings( qApp, "EnvironmentVariables", "1.0.0" );
	
	settings.remove( mSettingsKey );
	
	settings.beginWriteArray( mSettingsKey );
	
	for ( int i = 0; i < variables.count(); i++ )
	{
		settings.setArrayIndex( i );
		const pEnvironmentVariablesModel::Variable& variable = (variables.begin() +i).value();
		
		settings.setValue( "Name", variable.name );
		settings.setValue( "Value", variable.value );
		settings.setValue( "Enabled", variable.enabled );
	}
	
	settings.endArray();
	
	return true;
}
QStringList pEnvironmentVariablesModel::variablesToStringList( const pEnvironmentVariablesModel::Variables& variables, bool keepDisabled )
{
    QStringList items;

    foreach ( const pEnvironmentVariablesModel::Variable& variable, variables.values() ) {
        if ( !variable.enabled && !keepDisabled ) {
            continue;
        }

        items << QString( QSL( "%1=%2" ) ).arg( variable.name ).arg( variable.value );
    }

    return items;
}
void pEnvironmentVariablesModel::setVariables( const pEnvironmentVariablesModel::Variables& variables, bool setDefault )
{
    emit layoutAboutToBeChanged();

    int count = rowCount();

    if ( count > 0 ) {
        beginRemoveRows( QModelIndex(), 0, count -1 );
    }

    mRowCount = 0;
    mOrder.clear();
    mVariables.clear();

    if ( setDefault ) {
        mDefaultVariables.clear();
    }

    if ( count > 0 ) {
        endRemoveRows();
    }

    count = variables.count();

    if ( count > 0 ) {
        beginInsertRows( QModelIndex(), 0, count -1 );
    }

    mVariables = variables;
    mRowCount = count;

    if ( setDefault ) {
        setDefaultVariables( mVariables );
    }

    QStringList keys = mVariables.keys();
    qSort( keys );

    foreach ( const QString& key, keys ) {
        mOrder << &mVariables[ key ];
    }

    if ( count > 0 ) {
        endInsertRows();
    }

    emit layoutChanged();
}