<commit_before>#include <QtXml>
#include <QtScript>
#include <QMessageBox>
#include <QFileDialog>
#include <QSound>
#include <QShortcut>
#include <QtNetwork>
#include <QDesktopServices>
#include <QCloseEvent>
#include <QScrollBar>
#include <QtSql/QSqlDatabase>
#if defined(Q_OS_WIN)
	#include "windows.h"
	#include <float.h>
#endif
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_tag-tab.h"
#include "ui_pool-tab.h"
#include "settings/optionswindow.h"
#include "settings/startwindow.h"
#include "favoritewindow.h"
#include "batch/addgroupwindow.h"
#include "batch/adduniquewindow.h"
#include "batch/batchwindow.h"
#include "aboutwindow.h"
#include "functions.h"
#include "json.h"
#include "commands/commands.h"
#include "utils/blacklist-fix/blacklist-fix-1.h"
#include "utils/rename-existing/rename-existing-1.h"
#include "utils/empty-dirs-fix/empty-dirs-fix-1.h"
#include "utils/md5-fix/md5-fix.h"
#include "models/filename.h"

#define DONE()	logUpdate(" Done")



mainWindow::mainWindow(Profile *profile, QString program, QStringList tags, QMap<QString,QString> params)
	: ui(new Ui::mainWindow), m_profile(profile), m_favorites(m_profile->getFavorites()), m_downloads(0), m_loaded(false), m_getAll(false), m_program(program), m_tags(tags), m_batchAutomaticRetries(0), m_showLog(true)
{ }
void mainWindow::init()
{
	m_settings = m_profile->getSettings();
	bool crashed = m_settings->value("crashed", false).toBool();

	m_settings->setValue("crashed", true);
	m_settings->sync();

	loadLanguage(m_settings->value("language", "English").toString(), true);
	ui->setupUi(this);

	m_showLog = m_settings->value("Log/show", true).toBool();
	if (!m_showLog)
	{ ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabLog)); }

	log("New session started.");
	log(QString("Software version: %1.").arg(VERSION));
	log(QString("Path: %1").arg(qApp->applicationDirPath()));
	log(QString("Loading preferences from <a href=\"file:///%1\">%1</a>").arg(m_settings->fileName()));

	tabifyDockWidget(ui->dock_internet, ui->dock_wiki);
	tabifyDockWidget(ui->dock_wiki, ui->dock_kfl);
	tabifyDockWidget(ui->dock_kfl, ui->dock_favorites);
	ui->dock_internet->raise();

	ui->menuView->addAction(ui->dock_internet->toggleViewAction());
	ui->menuView->addAction(ui->dock_wiki->toggleViewAction());
	ui->menuView->addAction(ui->dock_kfl->toggleViewAction());
	ui->menuView->addAction(ui->dock_favorites->toggleViewAction());
	ui->menuView->addAction(ui->dockOptions->toggleViewAction());

	m_favorites = m_profile->getFavorites();

	if (m_settings->value("Proxy/use", false).toBool())
	{
		QNetworkProxy::ProxyType type = m_settings->value("Proxy/type", "http").toString() == "http" ? QNetworkProxy::HttpProxy : QNetworkProxy::Socks5Proxy;
		QNetworkProxy proxy(type, m_settings->value("Proxy/hostName").toString(), m_settings->value("Proxy/port").toInt());
		QNetworkProxy::setApplicationProxy(proxy);
		log(QString("Activation of the general proxy on the host \"%1\" and port %2.").arg(m_settings->value("Proxy/hostName").toString()).arg(m_settings->value("Proxy/port").toInt()));
	}

	m_progressdialog = nullptr;

	ui->tableBatchGroups->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
	ui->tableBatchUniques->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

	log("Loading sources");
	loadSites();

	if (m_sites.size() == 0)
	{
		QMessageBox::critical(this, tr("No source found"), tr("No source found. Do you have a configuration problem? Try to reinstall the program."));
		qApp->quit();
		this->deleteLater();
		return;
	}
	else
	{
		QString srsc = "";
		for (int i = 0; i < m_sites.size(); ++i)
		{ srsc += (i != 0 ? ", " : "") + m_sites.keys().at(i) + " (" + m_sites.values().at(i)->type() + ")"; }
		log(QString("%n source(s) found: %1").arg(srsc));
	}

	ui->actionClosetab->setShortcut(QKeySequence::Close);
	QShortcut *actionCloseTabW = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
	connect(actionCloseTabW, &QShortcut::activated, ui->actionClosetab, &QAction::trigger);

	QShortcut *actionFocusSearch = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_L), this);
	connect(actionFocusSearch, &QShortcut::activated, this, &mainWindow::focusSearch);

	ui->actionAddtab->setShortcut(QKeySequence::AddTab);
	ui->actionQuit->setShortcut(QKeySequence::Quit);
	ui->actionFolder->setShortcut(QKeySequence::Open);

	loadLanguage(m_settings->value("language", "English").toString());

	connect(ui->actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
	connect(ui->actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

	// Action on first load
	if (m_settings->value("firstload", true).toBool())
	{
		this->onFirstLoad();
		m_settings->setValue("firstload", false);
	}

	// Crash restoration
	m_restore = m_settings->value("start", "none").toString() == "restore";
	if (crashed)
	{
		log("It seems that Imgbrd-Grabber hasn't shut down properly last time.");

		QString msg;
		if (m_restore)
			msg = tr("It seems that the application was not properly closed for its last use. Do you want to start without restoring your last session?");
		else
			msg = tr("It seems that the application was not properly closed for its last use. Do you want to restore your last session?");

		QMessageBox dlg(QMessageBox::Question, "Grabber", msg, QMessageBox::Yes | QMessageBox::No);
		dlg.setWindowIcon(windowIcon());
		int response = dlg.exec();
		if (response == QMessageBox::Yes)
			m_restore = !m_restore;
	}

	// Loading last window state, size and position from the settings file
	restoreGeometry(m_settings->value("geometry").toByteArray());
	restoreState(m_settings->value("state").toByteArray());

	// Tab add button
	QPushButton *add = new QPushButton(QIcon(":/images/add.png"), "", this);
		add->setFlat(true);<commit_msg>Replace session message pair to always ask to restore (fix #726)<commit_after>#include <QtXml>
#include <QtScript>
#include <QMessageBox>
#include <QFileDialog>
#include <QSound>
#include <QShortcut>
#include <QtNetwork>
#include <QDesktopServices>
#include <QCloseEvent>
#include <QScrollBar>
#include <QtSql/QSqlDatabase>
#if defined(Q_OS_WIN)
	#include "windows.h"
	#include <float.h>
#endif
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_tag-tab.h"
#include "ui_pool-tab.h"
#include "settings/optionswindow.h"
#include "settings/startwindow.h"
#include "favoritewindow.h"
#include "batch/addgroupwindow.h"
#include "batch/adduniquewindow.h"
#include "batch/batchwindow.h"
#include "aboutwindow.h"
#include "functions.h"
#include "json.h"
#include "commands/commands.h"
#include "utils/blacklist-fix/blacklist-fix-1.h"
#include "utils/rename-existing/rename-existing-1.h"
#include "utils/empty-dirs-fix/empty-dirs-fix-1.h"
#include "utils/md5-fix/md5-fix.h"
#include "models/filename.h"

#define DONE()	logUpdate(" Done")



mainWindow::mainWindow(Profile *profile, QString program, QStringList tags, QMap<QString,QString> params)
	: ui(new Ui::mainWindow), m_profile(profile), m_favorites(m_profile->getFavorites()), m_downloads(0), m_loaded(false), m_getAll(false), m_program(program), m_tags(tags), m_batchAutomaticRetries(0), m_showLog(true)
{ }
void mainWindow::init()
{
	m_settings = m_profile->getSettings();
	bool crashed = m_settings->value("crashed", false).toBool();

	m_settings->setValue("crashed", true);
	m_settings->sync();

	loadLanguage(m_settings->value("language", "English").toString(), true);
	ui->setupUi(this);

	m_showLog = m_settings->value("Log/show", true).toBool();
	if (!m_showLog)
	{ ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabLog)); }

	log("New session started.");
	log(QString("Software version: %1.").arg(VERSION));
	log(QString("Path: %1").arg(qApp->applicationDirPath()));
	log(QString("Loading preferences from <a href=\"file:///%1\">%1</a>").arg(m_settings->fileName()));

	tabifyDockWidget(ui->dock_internet, ui->dock_wiki);
	tabifyDockWidget(ui->dock_wiki, ui->dock_kfl);
	tabifyDockWidget(ui->dock_kfl, ui->dock_favorites);
	ui->dock_internet->raise();

	ui->menuView->addAction(ui->dock_internet->toggleViewAction());
	ui->menuView->addAction(ui->dock_wiki->toggleViewAction());
	ui->menuView->addAction(ui->dock_kfl->toggleViewAction());
	ui->menuView->addAction(ui->dock_favorites->toggleViewAction());
	ui->menuView->addAction(ui->dockOptions->toggleViewAction());

	m_favorites = m_profile->getFavorites();

	if (m_settings->value("Proxy/use", false).toBool())
	{
		QNetworkProxy::ProxyType type = m_settings->value("Proxy/type", "http").toString() == "http" ? QNetworkProxy::HttpProxy : QNetworkProxy::Socks5Proxy;
		QNetworkProxy proxy(type, m_settings->value("Proxy/hostName").toString(), m_settings->value("Proxy/port").toInt());
		QNetworkProxy::setApplicationProxy(proxy);
		log(QString("Activation of the general proxy on the host \"%1\" and port %2.").arg(m_settings->value("Proxy/hostName").toString()).arg(m_settings->value("Proxy/port").toInt()));
	}

	m_progressdialog = nullptr;

	ui->tableBatchGroups->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
	ui->tableBatchUniques->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

	log("Loading sources");
	loadSites();

	if (m_sites.size() == 0)
	{
		QMessageBox::critical(this, tr("No source found"), tr("No source found. Do you have a configuration problem? Try to reinstall the program."));
		qApp->quit();
		this->deleteLater();
		return;
	}
	else
	{
		QString srsc = "";
		for (int i = 0; i < m_sites.size(); ++i)
		{ srsc += (i != 0 ? ", " : "") + m_sites.keys().at(i) + " (" + m_sites.values().at(i)->type() + ")"; }
		log(QString("%n source(s) found: %1").arg(srsc));
	}

	ui->actionClosetab->setShortcut(QKeySequence::Close);
	QShortcut *actionCloseTabW = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
	connect(actionCloseTabW, &QShortcut::activated, ui->actionClosetab, &QAction::trigger);

	QShortcut *actionFocusSearch = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_L), this);
	connect(actionFocusSearch, &QShortcut::activated, this, &mainWindow::focusSearch);

	ui->actionAddtab->setShortcut(QKeySequence::AddTab);
	ui->actionQuit->setShortcut(QKeySequence::Quit);
	ui->actionFolder->setShortcut(QKeySequence::Open);

	loadLanguage(m_settings->value("language", "English").toString());

	connect(ui->actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
	connect(ui->actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

	// Action on first load
	if (m_settings->value("firstload", true).toBool())
	{
		this->onFirstLoad();
		m_settings->setValue("firstload", false);
	}

	// Crash restoration
	m_restore = m_settings->value("start", "none").toString() == "restore";
	if (crashed)
	{
		log("It seems that Imgbrd-Grabber hasn't shut down properly last time.");

		QString msg = tr("It seems that the application was not properly closed for its last use. Do you want to restore your last session?");
		QMessageBox dlg(QMessageBox::Question, "Grabber", msg, QMessageBox::Yes | QMessageBox::No);
		dlg.setWindowIcon(windowIcon());
		dlg.setDefaultButton(QMessageBox::Yes);

		int response = dlg.exec();
		m_restore = response == QMessageBox::Yes;
	}

	// Loading last window state, size and position from the settings file
	restoreGeometry(m_settings->value("geometry").toByteArray());
	restoreState(m_settings->value("state").toByteArray());

	// Tab add button
	QPushButton *add = new QPushButton(QIcon(":/images/add.png"), "", this);
		add->setFlat(true);
{'fraction_non_alphanumeric': 0.17933263452131376, 'fraction_numerical': 0.0034067085953878406, 'mean_word_length': 6.602257636122178, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 189, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1956298', 'n_tokens_mistral': 4071, 'n_tokens_neox': 3919, 'n_words': 710}