bool VirtualBoxBridge::deleteVM(IMachine *m)
{
	uint32_t medias_size;
	IMedium ** medias;
	nsresult rc;

	NS_CHECK_AND_DEBUG_ERROR(m, Unregister(CleanupMode::Full, &medias_size, &medias), rc);
	if(NS_FAILED(rc))
		return false;
	
	std::cout << "medias_size: " << medias_size << std::endl;

	bool succeeded = true;
	for(int i = 0; i < medias_size; i++)
	{
		nsXPIDLString media_name;
		nsXPIDLString media_location;
		medias[i]->GetName(getter_Copies(media_name));
		medias[i]->GetLocation(getter_Copies(media_location));

		std::cout << "Deleting media " << returnQStringValue(media_name).toStdString()
		<< " (" << returnQStringValue(media_location).toStdString() << ")" << std::endl;
		
		IProgress *progress;
		int32_t resultCode;
		medias[i]->DeleteStorage(&progress);
		progress->WaitForCompletion(-1);
		progress->GetResultCode(&resultCode);
		
		if(resultCode != 0)
		{
			succeeded= false;
			std::cerr << "Error while deleting media " << returnQStringValue(media_name).toStdString()
				<< " (" << returnQStringValue(media_location).toStdString() << ")" << std::endl;
		}
		
		if(resultCode == 0)
		{
			QFile media_file(returnQStringValue(media_location));
			if(media_file.exists())
				media_file.remove();
		}
	}
	
	return succeeded;
}
IMachine *VirtualBoxBridge::cloneVM(QString qName, bool reInitIfaces, IMachine *m)
{
	nsXPIDLString name; name.AssignWithConversion(qName.toStdString().c_str());
	nsXPIDLString osTypeId;
	nsresult rc;

	m->GetOSTypeId(getter_Copies(osTypeId));

	IMachine *new_machine;
	IProgress *progress;

	NS_CHECK_AND_DEBUG_ERROR(virtualBox, FindMachine(name, &new_machine), rc);
	if(rc != VBOX_E_OBJECT_NOT_FOUND)
	{
		std::cout << "machine: " << &new_machine << std::endl;
		return NULL;
	}

	nsXPIDLString settingsFile;
	virtualBox->ComposeMachineFilename(name, NULL, NULL, NULL, getter_Copies(settingsFile));
	std::cout << "Predicted settings file name: " << returnQStringValue(settingsFile).toStdString() << std::endl;

	QFile file(returnQStringValue(settingsFile));
	QFile file_prev(returnQStringValue(settingsFile).append("-prev"));

	if(file.exists())
	{
		if(file.remove())
			std::cerr  << "Deleted old settings file: " << file.fileName().toStdString() << std::endl;
		else
			std::cerr  << "Error while deleting old settings file: " << file.fileName().toStdString() << std::endl;
	}
	if(file_prev.exists())
	{
		if(file_prev.remove())
			std::cerr  << "Deleted old backup settings file: " << file_prev.fileName().toStdString() << std::endl;
		else
			std::cerr  << "Error while deleting old backup settings file: " << file_prev.fileName().toStdString() << std::endl;
	}
	
	QString label = QString::fromUtf8("Creazione macchina \"").append(qName).append("\"...");
	ProgressDialog p(label);
	p.ui->progressBar->setValue(0);
	p.ui->label->setText(label);
	p.open();
	
	NS_CHECK_AND_DEBUG_ERROR(virtualBox, CreateMachine(NULL, name, 0, NULL, osTypeId, NULL, &new_machine), rc);
	if(NS_FAILED(rc))
		return NULL;

	std::cout << "Machine " << qName.toStdString() << " created" << std::endl;

	if(!reInitIfaces)
	{
		uint32_t clone_options[1];
		clone_options[0] = CloneOptions::KeepAllMACs;
		NS_CHECK_AND_DEBUG_ERROR(m, CloneTo(new_machine, CloneMode::MachineState, 1, clone_options, &progress), rc);
	}
	else
		NS_CHECK_AND_DEBUG_ERROR(m, CloneTo(new_machine, CloneMode::MachineState, 0, NULL, &progress), rc);

	if(NS_FAILED(rc))
		return NULL;

	int32_t resultCode;
	PRBool progress_completed;
	do
	{
		uint32_t percent;
		progress->GetCompleted(&progress_completed);
		progress->GetPercent(&percent);
		p.ui->progressBar->setValue(percent);
		p.refresh();
		usleep(750000);
	} while(!progress_completed);

	progress->GetResultCode(&resultCode);

	if (resultCode != 0) // check success
	{
		std::cout << "Error during clone process: " << resultCode << std::endl;
		return NULL;
	}

	std::cout << "Machine " << qName.toStdString() << " cloned" << std::endl;

	p.ui->label->setText(QString::fromUtf8("Registrazione macchina \"").append(qName).append("\"..."));
	NS_CHECK_AND_DEBUG_ERROR(virtualBox, RegisterMachine(new_machine), rc);
	if(NS_FAILED(rc))
		return NULL;
	
	std::cout << "Machine " << qName.toStdString() << " registered" << std::endl;
	return new_machine;
}