File: GameTracker.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,976 kB
  • ctags: 35,666
  • sloc: cpp: 213,139; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (106 lines) | stat: -rw-r--r-- 2,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <QDir>
#include <QDirIterator>
#include <QFile>

#include "DolphinQt2/Settings.h"
#include "DolphinQt2/GameList/GameTracker.h"

static const QStringList game_filters{
	QStringLiteral("*.gcm"),
	QStringLiteral("*.iso"),
	QStringLiteral("*.ciso"),
	QStringLiteral("*.gcz"),
	QStringLiteral("*.wbfs"),
	QStringLiteral("*.wad"),
	QStringLiteral("*.elf"),
	QStringLiteral("*.dol")
};

GameTracker::GameTracker(QObject* parent)
	: QFileSystemWatcher(parent)
{
	m_loader = new GameLoader;
	m_loader->moveToThread(&m_loader_thread);

	qRegisterMetaType<QSharedPointer<GameFile>>();
	connect(&m_loader_thread, &QThread::finished, m_loader, &QObject::deleteLater);
	connect(this, &QFileSystemWatcher::directoryChanged, this, &GameTracker::UpdateDirectory);
	connect(this, &QFileSystemWatcher::fileChanged, this, &GameTracker::UpdateFile);
	connect(this, &GameTracker::PathChanged, m_loader, &GameLoader::LoadGame);
	connect(m_loader, &GameLoader::GameLoaded, this, &GameTracker::GameLoaded);

	m_loader_thread.start();

	for (QString dir : Settings().GetPaths())
		AddDirectory(dir);
}

GameTracker::~GameTracker()
{
	m_loader_thread.quit();
	m_loader_thread.wait();
}

void GameTracker::AddDirectory(const QString& dir)
{
	if (!QFileInfo(dir).exists())
		return;
	addPath(dir);
	UpdateDirectory(dir);
}

void GameTracker::RemoveDirectory(const QString& dir)
{
	removePath(dir);
	QDirIterator it(dir, game_filters, QDir::NoFilter, QDirIterator::Subdirectories);
	while (it.hasNext())
	{
		QString path = QFileInfo(it.next()).canonicalFilePath();
		if (m_tracked_files.contains(path))
		{
			m_tracked_files[path]--;
			if (m_tracked_files[path] == 0)
			{
				removePath(path);
				m_tracked_files.remove(path);
				emit GameRemoved(path);
			}
		}
	}
}

void GameTracker::UpdateDirectory(const QString& dir)
{
	QDirIterator it(dir, game_filters, QDir::NoFilter, QDirIterator::Subdirectories);
	while (it.hasNext())
	{
		QString path = QFileInfo(it.next()).canonicalFilePath();
		if (m_tracked_files.contains(path))
		{
			m_tracked_files[path]++;
		}
		else
		{
			addPath(path);
			m_tracked_files[path] = 1;
			emit PathChanged(path);
		}
	}
}

void GameTracker::UpdateFile(const QString& file)
{
	if (QFileInfo(file).exists())
	{
		emit PathChanged(file);
	}
	else if (removePath(file))
	{
		m_tracked_files.remove(file);
		emit GameRemoved(file);
	}
}