Restart properly when home changes

After fixing up the trainDB problem we can now restart
the mainwindows safely when the athlete directory changes.
This commit is contained in:
Mark Liversedge
2013-11-16 09:38:17 +00:00
parent 2ae36fb563
commit ab8aaee088
2 changed files with 128 additions and 108 deletions

View File

@@ -30,6 +30,8 @@
#include "AddDeviceWizard.h"
#include "MainWindow.h"
extern bool restarting; //its actually in main.cpp
ConfigDialog::ConfigDialog(QDir _home, Zones *_zones, Context *context) :
home(_home), zones(_zones), context(context)
{
@@ -184,18 +186,21 @@ void ConfigDialog::saveClicked()
// if so we will restart, if not I'll revert to current directory
QMessageBox msgBox;
msgBox.setText("You changed the location of the athlete library");
msgBox.setInformativeText("You have moved the location of the athlete library "
"This is where ALL new athletes and their activity files "
msgBox.setInformativeText("This is where all new athletes and their activity files "
"will now be stored.\n\nCurrent athlete data will no longer be "
"available and you will need to relaunch GoldenCheetah.\n\nDo you want to apply and close GoldenCheetah?");
"available and GoldenCheetah will need to restart for the change to take effect."
"\n\nDo you want to apply and restart GoldenCheetah?");
// we want our own buttons...
msgBox.addButton(tr("No, Keep current"), QMessageBox::RejectRole);
msgBox.addButton(tr("Yes, Apply and Close"), QMessageBox::AcceptRole);
msgBox.addButton(tr("Yes, Apply and Restart"), QMessageBox::AcceptRole);
msgBox.setDefaultButton(QMessageBox::Abort);
if (msgBox.exec() == 1) { // accept!
// lets restart
restarting = true;
// close all the mainwindows
foreach(MainWindow *m, mainwindows) m->byebye();

View File

@@ -28,10 +28,13 @@
#endif
QApplication *application;
bool restarting = false;
int
main(int argc, char *argv[])
{
int ret=2; // return code from qapplication, default to error
#ifdef Q_OS_X11
XInitThreads();
#endif
@@ -51,136 +54,148 @@ main(int argc, char *argv[])
}
#endif
// create the application -- only ever ONE regardless of restarts
application = new QApplication(argc, argv);
// set defaultfont
QFont font;
font.fromString(appsettings->value(NULL, GC_FONT_DEFAULT, QFont().toString()).toString());
font.setPointSize(appsettings->value(NULL, GC_FONT_DEFAULT_SIZE, 12).toInt());
application->setFont(font); // set default font
//this is the path within the current directory where GC will look for
//files to allow USB stick support
QString localLibraryPath="Library/GoldenCheetah";
//this is the path that used to be used for all platforms
//now different platforms will use their own path
//this path is checked first to make things easier for long-time users
QString oldLibraryPath=QDir::home().path()+"/Library/GoldenCheetah";
// creating that first mainwindow
do {
//these are the new platform-dependent library paths
// lets not restart endlessly
restarting = false;
//this is the path within the current directory where GC will look for
//files to allow USB stick support
QString localLibraryPath="Library/GoldenCheetah";
//this is the path that used to be used for all platforms
//now different platforms will use their own path
//this path is checked first to make things easier for long-time users
QString oldLibraryPath=QDir::home().path()+"/Library/GoldenCheetah";
//these are the new platform-dependent library paths
#if defined(Q_OS_MACX)
QString libraryPath="Library/GoldenCheetah";
QString libraryPath="Library/GoldenCheetah";
#elif defined(Q_OS_WIN)
QString libraryPath=QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/GoldenCheetah";
QString libraryPath=QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/GoldenCheetah";
#else
// Q_OS_LINUX et al
QString libraryPath=".goldencheetah";
// Q_OS_LINUX et al
QString libraryPath=".goldencheetah";
#endif
QString sh;
if ((sh=appsettings->value(NULL, GC_HOMEDIR).toString()) != "") localLibraryPath = sh;
// or did we override in settings?
QString sh;
if ((sh=appsettings->value(NULL, GC_HOMEDIR).toString()) != "") localLibraryPath = sh;
// lets try the local library we've worked out...
QDir home = QDir();
if(home.exists(localLibraryPath)) {
home.cd(localLibraryPath);
} else {
// YIKES !! The directory we should be using doesn't exist!
home = QDir::home();
if (home.exists(oldLibraryPath)) { // there is an old style path, lets fo there
home.cd(oldLibraryPath);
} else {
if (!home.exists(libraryPath)) {
if (!home.mkpath(libraryPath)) {
//First check to see if the Library folder exists where the executable is (for USB sticks)
QDir home = QDir();
//if it does, create an ini file for settings and cd into the library
if(home.exists(localLibraryPath))
{
home.cd(localLibraryPath);
}
//if it does not exist, let QSettings handle storing settings
//also cd to the home directory and look for libraries
else
{
home = QDir::home();
//check if this users previously stored files in the old library path
//if they did, use the existing library
if (home.exists(oldLibraryPath))
{
home.cd(oldLibraryPath);
}
//otherwise use the new library path
else
{
//first create the path if it does not exist
if (!home.exists(libraryPath))
{
if (!home.mkpath(libraryPath))
{
qDebug()<<"Failed to create library path\n";
exit(0);
}
}
home.cd(libraryPath);
}
}
// install QT Translator to enable QT Dialogs translation
// we may have restarted JUST to get this!
QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
application->installTranslator(&qtTranslator);
// Language setting (default to system locale)
QVariant lang = appsettings->value(NULL, GC_LANG, QLocale::system().name());
// Load specific translation
QTranslator gcTranslator;
gcTranslator.load(":translations/gc_" + lang.toString() + ".qm");
application->installTranslator(&gcTranslator);
// Initialize metrics once the translator is installed
RideMetricFactory::instance().initialize();
// Initialize global registry once the translator is installed
GcWindowRegistry::initialize();
// initialise the trainDB
trainDB = new TrainDB(home);
QStringList args(application->arguments());
// lets reopen last athlete
QVariant lastOpened;
if(args.size() > 1) {
lastOpened = args.at(1);
} else {
lastOpened = appsettings->value(NULL, GC_SETTINGS_LAST);
}
bool anyOpened = false;
if (lastOpened != QVariant()) {
QStringList list = lastOpened.toStringList();
QStringListIterator i(list);
while (i.hasNext()) {
QString cyclist = i.next();
if (home.cd(cyclist)) {
MainWindow *mainWindow = new MainWindow(home);
mainWindow->show();
home.cdUp();
anyOpened = true;
}
}
home.cd(libraryPath);
}
}
// install QT Translator to enable QT Dialogs translation
QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(),
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
application->installTranslator(&qtTranslator);
// need to ask the user
if (!anyOpened) {
ChooseCyclistDialog d(home, true);
d.setModal(true);
// Language setting (default to system locale)
QVariant lang = appsettings->value(NULL, GC_LANG, QLocale::system().name());
// Load specific translation
QTranslator gcTranslator;
gcTranslator.load(":translations/gc_" + lang.toString() + ".qm");
application->installTranslator(&gcTranslator);
// Initialize metrics once the translator is installed
RideMetricFactory::instance().initialize();
// Initialize global registry once the translator is installed
GcWindowRegistry::initialize();
// initialise the trainDB
trainDB = new TrainDB(home);
QStringList args( application->arguments() );
QVariant lastOpened;
if( args.size() > 1 ){
lastOpened = args.at(1);
} else {
lastOpened = appsettings->value(NULL, GC_SETTINGS_LAST);
}
double crankLength = appsettings->value(NULL, GC_CRANKLENGTH).toDouble();
if(crankLength<=0) {
appsettings->setValue(GC_CRANKLENGTH,172.5);
}
bool anyOpened = false;
if (lastOpened != QVariant()) {
QStringList list = lastOpened.toStringList();
QStringListIterator i(list);
while (i.hasNext()) {
QString cyclist = i.next();
if (home.cd(cyclist)) {
MainWindow *mainWindow = new MainWindow(home);
mainWindow->show();
home.cdUp();
anyOpened = true;
// choose cancel?
if ((ret=d.exec()) != QDialog::Accepted) {
delete trainDB;
return ret;
}
// chosen, so lets get the choice..
home.cd(d.choice());
if (!home.exists()) {
delete trainDB;
exit(0);
}
// .. and open a mainwindow
MainWindow *mainWindow = new MainWindow(home);
mainWindow->show();
}
}
if (!anyOpened) {
ChooseCyclistDialog d(home, true);
d.setModal(true);
if (d.exec() != QDialog::Accepted)
return 0;
home.cd(d.choice());
if (!home.exists())
exit(0);
MainWindow *mainWindow = new MainWindow(home);
mainWindow->show();
}
int ret =application->exec();
ret=application->exec();
// close trainDB
delete trainDB;
// close trainDB
delete trainDB;
} while (restarting);
return ret;
}