GuiGamelistOptions::~GuiGamelistOptions()
{
	// save and apply sort
	SystemData* system = getGamelist()->getCursor()->getSystem();
	FileData* root = system->getRootFolder();
	system->sortId = mListSort->getSelectedId(); // this will break if mListSort isn't in the same order as FileSorts:typesArr
	root->sort(*mListSort->getSelected()); // will also recursively sort children

	// notify that the root folder was sorted
	getGamelist()->onFileChanged(root, FILE_SORTED);
}
void GuiFastSelect::updateGameListSort()
{
	const FileData::SortType& sort = FileSorts::SortTypes.at(mSortId);

	SystemData* system = mGameList->getCursor()->getSystem();
	FileData* root = system->getRootFolder();

	system->sortId = mSortId; // update system sort setting

	root->sort(sort); // will also recursively sort children

	// notify that the root folder was sorted
	mGameList->onFileChanged(root, FILE_SORTED);
}
// this updates all collection files related to the argument file
void CollectionSystemManager::updateCollectionSystems(FileData* file)
{
	// collection files use the full path as key, to avoid clashes
	std::string key = file->getFullPath();
	// find games in collection systems
	for(auto sysIt = SystemData::sSystemVector.begin(); sysIt != SystemData::sSystemVector.end(); sysIt++)
	{
		if ((*sysIt)->isCollection()) {
			const std::unordered_map<std::string, FileData*>& children = (*sysIt)->getRootFolder()->getChildrenByFilename();
			bool found = children.find(key) != children.end();
			FileData* rootFolder = (*sysIt)->getRootFolder();
			FileFilterIndex* fileIndex = (*sysIt)->getIndex();
			std::string name = (*sysIt)->getName();
			if (found) {
				// if we found it, we need to update it
				FileData* collectionEntry = children.at(key);
				// remove from index, so we can re-index metadata after refreshing
				fileIndex->removeFromIndex(collectionEntry);
				collectionEntry->refreshMetadata();
				if (name == "favorites" && file->metadata.get("favorite") == "false") {
					// need to check if still marked as favorite, if not remove
					ViewController::get()->getGameListView((*sysIt)).get()->remove(collectionEntry, false);
					ViewController::get()->onFileChanged((*sysIt)->getRootFolder(), FILE_REMOVED);
				}
				else
				{
					// re-index with new metadata
					fileIndex->addToIndex(collectionEntry);
					ViewController::get()->onFileChanged(collectionEntry, FILE_METADATA_CHANGED);
				}
			}
			else
			{
				// we didn't find it here - we need to check if we should add it
				if (name == "recent" && file->metadata.get("playcount") > "0" ||
					name == "favorites" && file->metadata.get("favorite") == "true") {
					CollectionFileData* newGame = new CollectionFileData(file, (*sysIt));
					rootFolder->addChild(newGame);
					fileIndex->addToIndex(newGame);
					ViewController::get()->onFileChanged(file, FILE_METADATA_CHANGED);
					ViewController::get()->getGameListView((*sysIt))->onFileChanged(newGame, FILE_METADATA_CHANGED);
				}
			}
			rootFolder->sort(getSortType(mCollectionSystemDecls[name].defaultSort));
			ViewController::get()->onFileChanged(rootFolder, FILE_SORTED);
		}
	}
}
void CollectionSystemManager::loadAutoCollectionSystems()
{
	for(std::map<std::string, CollectionSystemDecl>::iterator it = mCollectionSystemDecls.begin() ; it != mCollectionSystemDecls.end() ; it++ )
	{
		CollectionSystemDecl sysDecl = it->second;
		if (!sysDecl.isCustom && !findCollectionSystem(sysDecl.name))
		{
			SystemData* newSys = new SystemData(sysDecl.name, sysDecl.longName, mCollectionEnvData, sysDecl.themeFolder, true);

			FileData* rootFolder = newSys->getRootFolder();
			FileFilterIndex* index = newSys->getIndex();
			for(auto sysIt = SystemData::sSystemVector.begin(); sysIt != SystemData::sSystemVector.end(); sysIt++)
			{
				if ((*sysIt)->isGameSystem()) {
					std::vector<FileData*> files = (*sysIt)->getRootFolder()->getFilesRecursive(GAME);
					for(auto gameIt = files.begin(); gameIt != files.end(); gameIt++)
					{
						bool include = includeFileInAutoCollections((*gameIt));
						switch(sysDecl.type) {
							case AUTO_LAST_PLAYED:
								include = include && (*gameIt)->metadata.get("playcount") > "0";
								break;
							case AUTO_FAVORITES:
								// we may still want to add files we don't want in auto collections in "favorites"
								include = (*gameIt)->metadata.get("favorite") == "true";
								break;
						}

						if (include) {
							CollectionFileData* newGame = new CollectionFileData(*gameIt, newSys);
							rootFolder->addChild(newGame);
							index->addToIndex(newGame);
						}
					}
				}
			}
			rootFolder->sort(getSortType(sysDecl.defaultSort));
			mAutoCollectionSystems.push_back(newSys);

			CollectionSystemData newCollectionData;
			newCollectionData.system = newSys;
			newCollectionData.decl = sysDecl;
			newCollectionData.isEnabled = false;
			mAllCollectionSystems[sysDecl.name] = newCollectionData;
		}
	}
}
GuiGamelistOptions::~GuiGamelistOptions()
{
	LOG(LogDebug) << "GUIGamelistOptions::~GuiGamelistOptions()";
	// apply sort
	FileData* root = getGamelist()->getCursor()->getSystem()->getRootFolder();
	root->sort(*mListSort->getSelected()); // will also recursively sort children
	
	// notify that the root folder was sorted
	
	getGamelist()->onFileChanged(root, FILE_SORTED);				
	if (mFavoriteStateChanged)
	{
		LOG(LogDebug) << "  GUIGamelistOptions::~GuiGamelistOptions(): FavoriteStateChanged, reloading GameList";
		ViewController::get()->setAllInvalidGamesList(getGamelist()->getCursor()->getSystem());
		ViewController::get()->reloadGameListView(getGamelist()->getCursor()->getSystem());
	}
}