Index: libs/libmyth/mythcontext.cpp =================================================================== RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/mythcontext.cpp,v retrieving revision 1.134 diff -u -r1.134 mythcontext.cpp --- libs/libmyth/mythcontext.cpp 11 Sep 2004 06:27:29 -0000 1.134 +++ libs/libmyth/mythcontext.cpp 23 Sep 2004 00:46:35 -0000 @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -42,6 +44,12 @@ void LoadLogSettings(void); + bool FixSettingsFile(void); + bool EditSettings(QFile *f, QString *message); + bool BootstrapSettings(QFile *f); + bool MakeSettingsFile(QFile *f, + QString host, QString user, QString pass, QString name); + MythContext *parent; Settings *m_settings; @@ -132,6 +140,9 @@ if (!parent->LoadSettingsFiles("mysql.txt")) cerr << "Unable to read configuration file mysql.txt" << endl; + if (gui) + FixSettingsFile(); + m_localhostname = m_settings->GetSetting("LocalHostName", NULL); if (m_localhostname == NULL) { @@ -211,6 +222,116 @@ m_logprintlevel = parent->GetNumSetting("LogPrintLevel", LP_ERROR); } +// oldsettings.cpp has not been able to load the settings. +// Either the file has errors and needs editing, +// or it doesn't exist (a bootstrapping scenario) + +bool MythContextPrivate::FixSettingsFile(void) +{ + QString path = QDir::homeDirPath() + "/.mythtv/mysql.txt"; + QFile * f = new QFile(path); + + bool reload; + + if (f->exists()) + reload = EditSettings(f, new QString("Please correct configuration.")); + else + reload = BootstrapSettings(f); + + if (reload) + return parent->LoadSettingsFiles("mysql.txt"); + else + return false; +} + +// Let the user edit the settings file. +// Note that this is currently never called, because if the file exists, +// parent->LoadSettingsFiles() will have succeeded +// (even if it has errors or missing data in it) + +bool MythContextPrivate::EditSettings(QFile * f, QString * message) +{ + (void)f; + (void)message; + return false; +} + +// Try to bootstrap the settings by displaying a dialog +// for the user to enter the backend's IP address, et c., +// and then creating a settings file from that data + +bool MythContextPrivate::BootstrapSettings(QFile * f) +{ + // A simple dialog with some text fields and two buttons + QDialog * d = new QDialog(0, "", true); + QVBox * v = new QVBox(d); + v->setMargin(5); + v->setSpacing(3); + + // Create a box with the four editable fields, each of which have labels + QGroupBox * g = new QGroupBox(4, Qt::Vertical, + "MythTV backend configuration", v, ""); + new QLabel("DNS name or IP address:", g); + new QLabel("Database user name:", g); + new QLabel("Database user password:", g); + new QLabel("Database name:", g); + QLineEdit * host = new QLineEdit("localhost", g); + QLineEdit * user = new QLineEdit("mythtv", g); + QLineEdit * pass = new QLineEdit("mythtv", g); + QLineEdit * name = new QLineEdit("mythconverg", g); + g->adjustSize(); + + // Nicely arrange the two buttons + QHBox * h = new QHBox(v); + QPushButton * canc = new QPushButton("Cancel", h); + QPushButton * save = new QPushButton("Save", h); + QObject::connect(canc, SIGNAL(clicked()), d, SLOT(reject())); + QObject::connect(canc, SIGNAL(released()), d, SLOT(reject())); + QObject::connect(save, SIGNAL(clicked()), d, SLOT(accept())); + QObject::connect(save, SIGNAL(released()), d, SLOT(accept())); + save->setDefault(true); + h->adjustSize(); + + // Fix sizes, display, and process results + v->adjustSize(); + d->adjustSize(); + d->setCaption("Please enter details"); + + if (d->exec() == QDialog::Rejected) + return false; + + return MakeSettingsFile(f, + host->text(), user->text(), + pass->text(), name->text()); +} + +bool MythContextPrivate::MakeSettingsFile(QFile * f, + QString host, QString user, + QString pass, QString name) +{ + if (f->exists()) + { + cerr << "Logic error. Configuration file " << f->name() << "exists.\n"; + cerr << "MakeSettingsFile() should not have been called!\n"; + return false; + } + + // Note that this seems to create the directory if it doesn't exist + if (f->open(IO_WriteOnly)) + { + QTextStream s(f); + s << "DBHostName=" << host << endl; + s << "DBUserName=" << user << endl; + s << "DBPassword=" << pass << endl; + s << "DBName=" << name << endl; + f->close(); + return true; + } + + cerr << "Could not create configuration file " << f->name() << endl; + return false; +} + MythContext::MythContext(const QString &binversion, bool gui, bool lcd) : QObject() {