2014-01-27 20:14:02 +01:00
# include "settingspage.hpp"
2014-04-16 18:34:24 +02:00
# include <QFileDialog>
2014-04-16 16:54:55 +02:00
# include <QMessageBox>
2022-06-16 21:29:55 +02:00
# include <QDebug>
2014-04-16 18:34:24 +02:00
# include <QDir>
2014-03-30 23:57:30 +02:00
2014-04-16 16:54:55 +02:00
# include "utils/textinputdialog.hpp"
2015-02-06 12:24:06 +13:00
# include "datafilespage.hpp"
2014-04-16 16:54:55 +02:00
2014-03-30 23:57:30 +02:00
using namespace Process ;
2014-04-17 00:01:19 +02:00
Launcher : : SettingsPage : : SettingsPage ( Files : : ConfigurationManager & cfg ,
Config : : GameSettings & gameSettings ,
Config : : LauncherSettings & launcherSettings , MainDialog * parent )
2015-04-30 19:24:27 -05:00
: QWidget ( parent )
, mCfgMgr ( cfg )
2014-04-17 00:01:19 +02:00
, mGameSettings ( gameSettings )
, mLauncherSettings ( launcherSettings )
, mMain ( parent )
2014-01-27 20:14:02 +01:00
{
setupUi ( this ) ;
QStringList languages ;
2019-11-23 17:42:10 +01:00
languages < < tr ( " English " )
< < tr ( " French " )
< < tr ( " German " )
< < tr ( " Italian " )
< < tr ( " Polish " )
< < tr ( " Russian " )
< < tr ( " Spanish " ) ;
2014-01-27 20:14:02 +01:00
languageComboBox - > addItems ( languages ) ;
2014-04-16 16:54:55 +02:00
2014-04-18 13:17:37 +02:00
mWizardInvoker = new ProcessInvoker ( ) ;
mImporterInvoker = new ProcessInvoker ( ) ;
2015-02-22 08:46:12 +13:00
resetProgressBar ( ) ;
2014-04-16 16:54:55 +02:00
2022-08-23 14:35:54 -03:00
connect ( mWizardInvoker - > getProcess ( ) , & QProcess : : started ,
this , & SettingsPage : : wizardStarted ) ;
2014-04-16 16:54:55 +02:00
2022-08-23 14:35:54 -03:00
connect ( mWizardInvoker - > getProcess ( ) , qOverload < int , QProcess : : ExitStatus > ( & QProcess : : finished ) ,
this , & SettingsPage : : wizardFinished ) ;
2014-04-16 16:54:55 +02:00
2022-08-23 14:35:54 -03:00
connect ( mImporterInvoker - > getProcess ( ) , & QProcess : : started ,
this , & SettingsPage : : importerStarted ) ;
2014-04-16 16:54:55 +02:00
2022-08-23 14:35:54 -03:00
connect ( mImporterInvoker - > getProcess ( ) , qOverload < int , QProcess : : ExitStatus > ( & QProcess : : finished ) ,
this , & SettingsPage : : importerFinished ) ;
2014-04-16 16:54:55 +02:00
2015-01-10 15:04:45 +13:00
mProfileDialog = new TextInputDialog ( tr ( " New Content List " ) , tr ( " Content List name: " ) , this ) ;
2014-04-16 16:54:55 +02:00
2022-08-23 14:35:54 -03:00
connect ( mProfileDialog - > lineEdit ( ) , & LineEdit : : textChanged ,
this , & SettingsPage : : updateOkButton ) ;
2014-04-16 16:54:55 +02:00
2014-04-16 18:34:24 +02:00
// Detect Morrowind configuration files
QStringList iniPaths ;
2019-10-06 13:39:27 +02:00
for ( const QString & path : mGameSettings . getDataDirs ( ) )
{
2014-04-16 18:34:24 +02:00
QDir dir ( path ) ;
dir . setPath ( dir . canonicalPath ( ) ) ; // Resolve symlinks
if ( dir . exists ( QString ( " Morrowind.ini " ) ) )
iniPaths . append ( dir . absoluteFilePath ( QString ( " Morrowind.ini " ) ) ) ;
else
{
if ( ! dir . cdUp ( ) )
continue ; // Cannot move from Data Files
if ( dir . exists ( QString ( " Morrowind.ini " ) ) )
iniPaths . append ( dir . absoluteFilePath ( QString ( " Morrowind.ini " ) ) ) ;
}
}
if ( ! iniPaths . isEmpty ( ) ) {
settingsComboBox - > addItems ( iniPaths ) ;
importerButton - > setEnabled ( true ) ;
} else {
importerButton - > setEnabled ( false ) ;
}
2014-04-17 00:01:19 +02:00
loadSettings ( ) ;
2014-01-27 20:14:02 +01:00
}
2014-04-18 13:17:37 +02:00
Launcher : : SettingsPage : : ~ SettingsPage ( )
{
delete mWizardInvoker ;
delete mImporterInvoker ;
}
2014-03-30 23:57:30 +02:00
void Launcher : : SettingsPage : : on_wizardButton_clicked ( )
{
2015-02-24 20:14:18 +13:00
mMain - > writeSettings ( ) ;
2014-04-17 00:01:19 +02:00
2014-04-16 16:54:55 +02:00
if ( ! mWizardInvoker - > startProcess ( QLatin1String ( " openmw-wizard " ) , false ) )
return ;
2014-03-30 23:57:30 +02:00
}
void Launcher : : SettingsPage : : on_importerButton_clicked ( )
{
2015-02-24 20:14:18 +13:00
mMain - > writeSettings ( ) ;
2014-04-17 00:01:19 +02:00
// Create the file if it doesn't already exist, else the importer will fail
2022-06-19 13:28:33 +02:00
QString path ( QString : : fromUtf8 ( mCfgMgr . getUserConfigPath ( ) . string ( ) . c_str ( ) ) ) ; //TODO(Project579): This will probably break in windows with unicode paths
2014-04-17 00:01:19 +02:00
path . append ( QLatin1String ( " openmw.cfg " ) ) ;
QFile file ( path ) ;
if ( ! file . exists ( ) ) {
if ( ! file . open ( QIODevice : : ReadWrite ) ) {
// File cannot be created
QMessageBox msgBox ;
msgBox . setWindowTitle ( tr ( " Error writing OpenMW configuration file " ) ) ;
msgBox . setIcon ( QMessageBox : : Critical ) ;
msgBox . setStandardButtons ( QMessageBox : : Ok ) ;
msgBox . setText ( tr ( " <html><head/><body><p><b>Could not open or create %1 for writing </b></p> \
< p > Please make sure you have the right permissions \
and try again . < / p > < / body > < / html > " ).arg(file.fileName()));
msgBox . exec ( ) ;
return ;
}
file . close ( ) ;
}
// Construct the arguments to run the importer
QStringList arguments ;
if ( addonsCheckBox - > isChecked ( ) )
arguments . append ( QString ( " --game-files " ) ) ;
2022-08-13 15:22:53 +04:00
if ( fontsCheckBox - > isChecked ( ) )
arguments . append ( QString ( " --fonts " ) ) ;
2014-04-17 00:01:19 +02:00
arguments . append ( QString ( " --encoding " ) ) ;
arguments . append ( mGameSettings . value ( QString ( " encoding " ) , QString ( " win1252 " ) ) ) ;
arguments . append ( QString ( " --ini " ) ) ;
arguments . append ( settingsComboBox - > currentText ( ) ) ;
arguments . append ( QString ( " --cfg " ) ) ;
arguments . append ( path ) ;
qDebug ( ) < < " arguments " < < arguments ;
2015-02-22 08:46:12 +13:00
// start the progress bar as a "bouncing ball"
progressBar - > setMaximum ( 0 ) ;
progressBar - > setValue ( 0 ) ;
2015-01-30 00:07:18 +01:00
if ( ! mImporterInvoker - > startProcess ( QLatin1String ( " openmw-iniimporter " ) , arguments , false ) )
2015-02-22 08:46:12 +13:00
{
resetProgressBar ( ) ;
}
2014-04-16 16:54:55 +02:00
}
2014-04-16 18:34:24 +02:00
void Launcher : : SettingsPage : : on_browseButton_clicked ( )
{
QString iniFile = QFileDialog : : getOpenFileName (
this ,
QObject : : tr ( " Select configuration file " ) ,
QDir : : currentPath ( ) ,
QString ( tr ( " Morrowind configuration file (*.ini) " ) ) ) ;
if ( iniFile . isEmpty ( ) )
return ;
QFileInfo info ( iniFile ) ;
if ( ! info . exists ( ) | | ! info . isReadable ( ) )
return ;
const QString path ( QDir : : toNativeSeparators ( info . absoluteFilePath ( ) ) ) ;
if ( settingsComboBox - > findText ( path ) = = - 1 ) {
settingsComboBox - > addItem ( path ) ;
settingsComboBox - > setCurrentIndex ( settingsComboBox - > findText ( path ) ) ;
importerButton - > setEnabled ( true ) ;
}
}
2014-04-16 16:54:55 +02:00
void Launcher : : SettingsPage : : wizardStarted ( )
{
2014-05-29 17:57:41 +02:00
mMain - > hide ( ) ; // Hide the launcher
2014-04-16 16:54:55 +02:00
wizardButton - > setEnabled ( false ) ;
}
void Launcher : : SettingsPage : : wizardFinished ( int exitCode , QProcess : : ExitStatus exitStatus )
{
if ( exitCode ! = 0 | | exitStatus = = QProcess : : CrashExit )
2014-05-29 17:57:41 +02:00
return qApp - > quit ( ) ;
2014-04-16 16:54:55 +02:00
mMain - > reloadSettings ( ) ;
wizardButton - > setEnabled ( true ) ;
2014-05-29 17:57:41 +02:00
mMain - > show ( ) ; // Show the launcher again
2014-04-16 16:54:55 +02:00
}
void Launcher : : SettingsPage : : importerStarted ( )
{
importerButton - > setEnabled ( false ) ;
}
void Launcher : : SettingsPage : : importerFinished ( int exitCode , QProcess : : ExitStatus exitStatus )
{
if ( exitCode ! = 0 | | exitStatus = = QProcess : : CrashExit )
2014-04-16 18:34:24 +02:00
{
2015-02-22 08:46:12 +13:00
resetProgressBar ( ) ;
QMessageBox msgBox ;
msgBox . setWindowTitle ( tr ( " Importer finished " ) ) ;
msgBox . setStandardButtons ( QMessageBox : : Ok ) ;
msgBox . setIcon ( QMessageBox : : Warning ) ;
msgBox . setText ( tr ( " Failed to import settings from INI file. " ) ) ;
msgBox . exec ( ) ;
}
else
{
// indicate progress finished
progressBar - > setMaximum ( 1 ) ;
progressBar - > setValue ( 1 ) ;
// Importer may have changed settings, so refresh
mMain - > reloadSettings ( ) ;
2014-04-16 16:54:55 +02:00
}
importerButton - > setEnabled ( true ) ;
}
2015-02-22 08:46:12 +13:00
void Launcher : : SettingsPage : : resetProgressBar ( )
{
// set progress bar to 0 %
2015-02-23 19:30:46 +13:00
progressBar - > reset ( ) ;
2015-02-22 08:46:12 +13:00
}
2014-04-16 16:54:55 +02:00
void Launcher : : SettingsPage : : updateOkButton ( const QString & text )
{
2014-04-17 02:17:18 +02:00
// We do this here because we need to access the profiles
2014-04-16 16:54:55 +02:00
if ( text . isEmpty ( ) ) {
mProfileDialog - > setOkButtonEnabled ( false ) ;
return ;
}
2015-01-10 18:46:47 +13:00
const QStringList profiles ( mLauncherSettings . getContentLists ( ) ) ;
2014-03-30 23:57:30 +02:00
2014-04-16 18:34:24 +02:00
( profiles . contains ( text ) )
? mProfileDialog - > setOkButtonEnabled ( false )
: mProfileDialog - > setOkButtonEnabled ( true ) ;
2014-03-30 23:57:30 +02:00
}
2014-04-17 00:01:19 +02:00
void Launcher : : SettingsPage : : saveSettings ( )
{
QString language ( languageComboBox - > currentText ( ) ) ;
mLauncherSettings . setValue ( QLatin1String ( " Settings/language " ) , language ) ;
if ( language = = QLatin1String ( " Polish " ) ) {
mGameSettings . setValue ( QLatin1String ( " encoding " ) , QLatin1String ( " win1250 " ) ) ;
} else if ( language = = QLatin1String ( " Russian " ) ) {
mGameSettings . setValue ( QLatin1String ( " encoding " ) , QLatin1String ( " win1251 " ) ) ;
} else {
mGameSettings . setValue ( QLatin1String ( " encoding " ) , QLatin1String ( " win1252 " ) ) ;
}
}
bool Launcher : : SettingsPage : : loadSettings ( )
{
QString language ( mLauncherSettings . value ( QLatin1String ( " Settings/language " ) ) ) ;
int index = languageComboBox - > findText ( language ) ;
if ( index ! = - 1 )
languageComboBox - > setCurrentIndex ( index ) ;
return true ;
}