diff src/gui/window.cc @ 202:71832ffe425a

animia: re-add kvm fd source this is all being merged from my wildly out-of-date laptop. SORRY! in other news, I edited the CI file to install the wayland client as well, so the linux CI build might finally get wayland stuff.
author Paper <paper@paper.us.eu.org>
date Tue, 02 Jan 2024 06:05:06 -0500
parents 975a3f0965e2
children 84e0a3c4737a
line wrap: on
line diff
--- a/src/gui/window.cc	Sun Nov 19 19:13:28 2023 -0500
+++ b/src/gui/window.cc	Tue Jan 02 06:05:06 2024 -0500
@@ -36,31 +36,14 @@
 #include <QToolBar>
 #include <QToolButton>
 
+#include <iostream>
+
 #ifdef MACOSX
 #	include "sys/osx/dark_theme.h"
 #elif defined(WIN32)
 #	include "sys/win32/dark_theme.h"
 #endif
 
-Q_DECLARE_METATYPE(std::vector<std::string>);
-
-class Thread : public QThread {
-		Q_OBJECT
-
-	public:
-		Thread(QObject* object = nullptr) : QThread(object) {}
-
-	private:
-		void run() override {
-			std::vector<std::string> files;
-			Track::Media::GetCurrentlyPlaying(files);
-			emit Done(files);
-		}
-
-	signals:
-		void Done(const std::vector<std::string>& files);
-};
-
 enum class Pages {
 	NOW_PLAYING,
 
@@ -73,11 +56,17 @@
 	TORRENTS
 };
 
+void PlayingThread::run() {
+	std::vector<std::string> files;
+	Track::Media::GetCurrentlyPlaying(files);
+	emit Done(files);
+}
+
 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
 	setWindowIcon(QIcon(":/favicon.png"));
 
 	main_widget.reset(new QWidget(this));
-	/*QHBoxLayout* layout = */new QHBoxLayout(main_widget.get());
+	new QHBoxLayout(main_widget.get());
 
 	AddMainWidgets();
 
@@ -89,13 +78,19 @@
 
 	qRegisterMetaType<std::vector<std::string>>();
 
-	QTimer* timer = new QTimer;
-	timer->start(5000);
+	/* This thread will be destroyed on
+	 * close of the program OR on the destruction
+	 * of MainWindow
+	*/
+	thread.reset(new PlayingThread(this));
+
+	QTimer* timer = new QTimer(this);
 
 	connect(timer, &QTimer::timeout, this, [this, page] {
-		Thread* thread = new Thread(this);
-		connect(thread, &QThread::finished, thread, &QThread::deleteLater);
-		connect(thread, &Thread::Done, this, [page](const std::vector<std::string>& files) {
+		if (!thread.get() || thread->isRunning())
+			return;
+
+		connect(thread.get(), &PlayingThread::Done, this, [page](const std::vector<std::string>& files) {
 			for (const auto& file : files) {
 				anitomy::Anitomy anitomy;
 				anitomy.Parse(Strings::ToWstring(file));
@@ -112,6 +107,8 @@
 		});
 		thread->start();
 	});
+
+	timer->start(5000);
 }
 
 void MainWindow::AddMainWidgets() {
@@ -332,10 +329,10 @@
 
 			/* pain in my ass */
 			connect(sidebar.get(), &SideBar::CurrentItemChanged, this,
-			        [pages_group](int index) { pages_group->actions()[index]->setChecked(true); });
+					[pages_group](int index) { pages_group->actions()[index]->setChecked(true); });
 
 			connect(pages_group, &QActionGroup::triggered, this,
-			        [this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); });
+					[this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); });
 		}
 
 		menu->addSeparator();
@@ -380,7 +377,7 @@
 		/* Toolbar */
 		QToolBar* toolbar = new QToolBar(this);
 		toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"),
-		                   [this] { AsyncSynchronize(stack.get()); });
+						   [this] { AsyncSynchronize(stack.get()); });
 
 		toolbar->addSeparator();
 
@@ -482,8 +479,8 @@
 
 void MainWindow::closeEvent(QCloseEvent* event) {
 	session.config.Save();
+	Anime::db.SaveDatabaseToDisk();
 	event->accept();
 }
 
 #include "gui/moc_window.cpp"
-#include "gui/window.moc"