QTfrontend/main.cpp
branchphysfslayer
changeset 8520 1dedcc37bfe8
parent 8508 f849b7b3af1d
child 8651 da384b09eb2b
equal deleted inserted replaced
8056:d5d5e1698554 8520:1dedcc37bfe8
    25 #include <QRegExp>
    25 #include <QRegExp>
    26 #include <QMap>
    26 #include <QMap>
    27 #include <QSettings>
    27 #include <QSettings>
    28 #include <QStringListModel>
    28 #include <QStringListModel>
    29 #include <QDate>
    29 #include <QDate>
       
    30 #include <QDesktopWidget>
       
    31 #include <QLabel>
    30 
    32 
    31 #include "hwform.h"
    33 #include "hwform.h"
    32 #include "hwconsts.h"
    34 #include "hwconsts.h"
    33 #include "newnetclient.h"
    35 #include "newnetclient.h"
    34 
    36 
    35 #include "DataManager.h"
    37 #include "DataManager.h"
    36 #include "FileEngine.h"
    38 #include "FileEngine.h"
    37 
    39 
    38 #ifdef _WIN32
    40 #ifdef _WIN32
    39 #include <Shlobj.h>
    41 #include <Shlobj.h>
    40 #endif
    42 #elif defined __APPLE__
       
    43 #include "CocoaInitializer.h"
       
    44 #endif
       
    45 #ifndef _WIN32
       
    46 #include <signal.h>
       
    47 #endif
       
    48 
       
    49 // Program resources
    41 #ifdef __APPLE__
    50 #ifdef __APPLE__
    42 #include "CocoaInitializer.h"
    51 static CocoaInitializer * cocoaInit = NULL;
    43 #endif
    52 #endif
    44 
    53 static FileEngineHandler * engine = NULL;
    45 
    54 
    46 //Determines the day of easter in year
    55 //Determines the day of easter in year
    47 //from http://aa.usno.navy.mil/faq/docs/easter.php,adapted to C/C++
    56 //from http://aa.usno.navy.mil/faq/docs/easter.php,adapted to C/C++
    48 QDate calculateEaster(long year)
    57 QDate calculateEaster(long year)
    49 {
    58 {
    83     else if (calculateEaster(date.year()) == date)
    92     else if (calculateEaster(date.year()) == date)
    84         season = SEASON_EASTER;
    93         season = SEASON_EASTER;
    85     else
    94     else
    86         season = SEASON_NONE;
    95         season = SEASON_NONE;
    87 }
    96 }
       
    97 #ifndef _WIN32
       
    98 void terminateFrontend(int signal)
       
    99 {
       
   100     Q_UNUSED(signal);
       
   101 
       
   102     QCoreApplication::exit(0);
       
   103 }
       
   104 #endif
    88 
   105 
    89 bool checkForDir(const QString & dir)
   106 bool checkForDir(const QString & dir)
    90 {
   107 {
    91     QDir tmpdir;
   108     QDir tmpdir(dir);
    92     if (!tmpdir.exists(dir))
   109     if (!tmpdir.exists())
    93         if (!tmpdir.mkdir(dir))
   110         if (!tmpdir.mkpath(dir))
    94         {
   111         {
    95             QMessageBox directoryMsg(QApplication::activeWindow());
   112             QMessageBox directoryMsg(QApplication::activeWindow());
    96             directoryMsg.setIcon(QMessageBox::Warning);
   113             directoryMsg.setIcon(QMessageBox::Warning);
    97             directoryMsg.setWindowTitle(QMessageBox::tr("Main - Error"));
   114             directoryMsg.setWindowTitle(QMessageBox::tr("Main - Error"));
    98             directoryMsg.setText(QMessageBox::tr("Cannot create directory %1").arg(dir));
   115             directoryMsg.setText(QMessageBox::tr("Cannot create directory %1").arg(dir));
   101             return false;
   118             return false;
   102         }
   119         }
   103     return true;
   120     return true;
   104 }
   121 }
   105 
   122 
       
   123 // Guaranteed to be the last thing ran in the application's life time.
       
   124 // Closes resources that need to exist as long as possible.
       
   125 void closeResources(void)
       
   126 {
       
   127 #ifdef __APPLE__
       
   128     if (cocoaInit != NULL)
       
   129     {
       
   130         delete cocoaInit;
       
   131         cocoaInit = NULL;
       
   132     }
       
   133 #endif
       
   134     if (engine != NULL)
       
   135     {
       
   136         delete engine;
       
   137         engine = NULL;
       
   138     }
       
   139 }
       
   140 
   106 int main(int argc, char *argv[])
   141 int main(int argc, char *argv[])
   107 {
   142 {
       
   143     // Since we're calling this first, closeResources() will be the last thing called after main() returns.
       
   144     atexit(closeResources);
       
   145 
       
   146 #ifdef __APPLE__
       
   147     cocoaInit = new CocoaInitializer(); // Creates the autoreleasepool preventing cocoa object leaks on OS X.
       
   148 #endif
       
   149 
       
   150 #ifndef _WIN32
       
   151     signal(SIGINT, &terminateFrontend);
       
   152 #endif
       
   153 
   108     HWApplication app(argc, argv);
   154     HWApplication app(argc, argv);
   109 
   155 
   110     FileEngineHandler engine(argv[0]);
   156     QLabel *splash = NULL;
       
   157 #if defined Q_WS_WIN
       
   158     QPixmap pixmap(":res/splash.png");
       
   159     splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
       
   160     splash->setAttribute(Qt::WA_TranslucentBackground);
       
   161     const QRect deskSize = QApplication::desktop()->screenGeometry(-1);
       
   162     QPoint splashCenter = QPoint( (deskSize.width() - pixmap.width())/2,
       
   163                                   (deskSize.height() - pixmap.height())/2 );
       
   164     splash->move(splashCenter);
       
   165     splash->setPixmap(pixmap);
       
   166     splash->show();
       
   167 #endif
       
   168 
       
   169     engine = new FileEngineHandler(argv[0]);
   111 
   170 
   112     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
   171     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
   113 
   172 
   114     QStringList arguments = app.arguments();
   173     QStringList arguments = app.arguments();
   115     QMap<QString, QString> parsedArgs;
   174     QMap<QString, QString> parsedArgs;
   144     }
   203     }
   145 
   204 
   146     if(parsedArgs.contains("config-dir"))
   205     if(parsedArgs.contains("config-dir"))
   147     {
   206     {
   148         QFileInfo f(parsedArgs["config-dir"]);
   207         QFileInfo f(parsedArgs["config-dir"]);
   149         *cConfigDir = f.absoluteFilePath();
   208         cfgdir->setPath(f.absoluteFilePath());
   150         custom_config = true;
   209         custom_config = true;
       
   210     }
       
   211     else
       
   212     {
       
   213         cfgdir->setPath(QDir::homePath());
       
   214         custom_config = false;
   151     }
   215     }
   152 
   216 
   153     app.setStyle(new QPlastiqueStyle());
   217     app.setStyle(new QPlastiqueStyle());
   154 
   218 
   155     QDateTime now = QDateTime::currentDateTime();
   219     QDateTime now = QDateTime::currentDateTime();
   158 
   222 
   159     Q_INIT_RESOURCE(hedgewars);
   223     Q_INIT_RESOURCE(hedgewars);
   160 
   224 
   161     qRegisterMetaType<HWTeam>("HWTeam");
   225     qRegisterMetaType<HWTeam>("HWTeam");
   162 
   226 
   163     bindir->cd("bin"); // workaround over NSIS installer
   227     bindir->cd(QCoreApplication::applicationDirPath());
   164 
   228 
   165     if(cConfigDir->length() == 0)
   229     if(custom_config == false)
   166         cfgdir->setPath(cfgdir->homePath());
       
   167     else
       
   168         cfgdir->setPath(*cConfigDir);
       
   169 
       
   170     if(cConfigDir->length() == 0)
       
   171     {
   230     {
   172 #ifdef __APPLE__
   231 #ifdef __APPLE__
   173         checkForDir(cfgdir->absolutePath() + "/Library/Application Support/Hedgewars");
   232         checkForDir(cfgdir->absolutePath() + "/Library/Application Support/Hedgewars");
   174         cfgdir->cd("Library/Application Support/Hedgewars");
   233         cfgdir->cd("Library/Application Support/Hedgewars");
   175 #elif defined _WIN32
   234 #elif defined _WIN32
   206         checkForDir(cfgdir->absolutePath() + "/VideoTemp");
   265         checkForDir(cfgdir->absolutePath() + "/VideoTemp");
   207     }
   266     }
   208 
   267 
   209     datadir->cd(bindir->absolutePath());
   268     datadir->cd(bindir->absolutePath());
   210     datadir->cd(*cDataDir);
   269     datadir->cd(*cDataDir);
   211     if(!datadir->cd("hedgewars/Data"))
   270     if(!datadir->cd("Data"))
   212     {
   271     {
   213         QMessageBox missingMsg(QApplication::activeWindow());
   272         QMessageBox missingMsg(QApplication::activeWindow());
   214         missingMsg.setIcon(QMessageBox::Critical);
   273         missingMsg.setIcon(QMessageBox::Critical);
   215         missingMsg.setWindowTitle(QMessageBox::tr("Main - Error"));
   274         missingMsg.setWindowTitle(QMessageBox::tr("Main - Error"));
   216         missingMsg.setText(QMessageBox::tr("Failed to open data directory:\n%1\n\n"
   275         missingMsg.setText(QMessageBox::tr("Failed to open data directory:\n%1\n\n"
   217                                            "Please check your installation!").
   276                                            "Please check your installation!").
   218                                             arg(datadir->absolutePath()+"/hedgewars/Data"));
   277                                             arg(datadir->absolutePath()+"/Data"));
   219         missingMsg.setWindowModality(Qt::WindowModal);
   278         missingMsg.setWindowModality(Qt::WindowModal);
   220         missingMsg.exec();
   279         missingMsg.exec();
   221         return 1;
   280         return 1;
   222     }
   281     }
   223 
   282 
   224     // setup PhysFS
   283     // setup PhysFS
   225     engine.mount(datadir->absolutePath());
   284     engine->mount(datadir->absolutePath());
   226     engine.mount(cfgdir->absolutePath() + "/Data");
   285     engine->mount(cfgdir->absolutePath() + "/Data");
   227     engine.mount(cfgdir->absolutePath(), "/config");
   286     engine->mount(cfgdir->absolutePath());
   228     engine.setWriteDir(cfgdir->absolutePath());
   287     engine->setWriteDir(cfgdir->absolutePath());
   229     engine.mountPacks();
   288     engine->mountPacks();
       
   289 
       
   290     DataManager::ensureFileExists("physfs://hedgewars.ini");
   230 
   291 
   231     QTranslator Translator;
   292     QTranslator Translator;
   232     {
   293     {
   233         QSettings settings("physfs://config/hedgewars.ini", QSettings::IniFormat);
   294         QSettings settings("physfs://hedgewars.ini", QSettings::IniFormat);
   234         QString cc = settings.value("misc/locale", QString()).toString();
   295         QString cc = settings.value("misc/locale", QString()).toString();
   235         if(cc.isEmpty())
   296         if(cc.isEmpty())
   236             cc = QLocale::system().name();
   297             cc = QLocale::system().name();
   237 
   298 
   238         // load locale file into translator
   299         // load locale file into translator
   247     {
   308     {
   248         QSettings registry_hklm("HKEY_LOCAL_MACHINE", QSettings::NativeFormat);
   309         QSettings registry_hklm("HKEY_LOCAL_MACHINE", QSettings::NativeFormat);
   249         registry_hklm.setValue("Software/Hedgewars/Frontend", bindir->absolutePath().replace("/", "\\") + "\\hedgewars.exe");
   310         registry_hklm.setValue("Software/Hedgewars/Frontend", bindir->absolutePath().replace("/", "\\") + "\\hedgewars.exe");
   250         registry_hklm.setValue("Software/Hedgewars/Path", bindir->absolutePath().replace("/", "\\"));
   311         registry_hklm.setValue("Software/Hedgewars/Path", bindir->absolutePath().replace("/", "\\"));
   251     }
   312     }
   252 #endif
       
   253 #ifdef __APPLE__
       
   254     // this creates the autoreleasepool that prevents leaking
       
   255     CocoaInitializer initializer;
       
   256 #endif
   313 #endif
   257 
   314 
   258     QString style = "";
   315     QString style = "";
   259     QString fname;
   316     QString fname;
   260 
   317 
   274         case SEASON_HWBDAY :
   331         case SEASON_HWBDAY :
   275             fname = "birthday.css";
   332             fname = "birthday.css";
   276             break;
   333             break;
   277         default :
   334         default :
   278             fname = "qt.css";
   335             fname = "qt.css";
       
   336             break;
   279     }
   337     }
   280 
   338 
   281     // load external stylesheet if there is any
   339     // load external stylesheet if there is any
   282     QFile extFile("physfs://css/" + fname);
   340     QFile extFile("physfs://css/" + fname);
   283 
   341 
   288     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   346     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   289         style.append(file.readAll());
   347         style.append(file.readAll());
   290 
   348 
   291     app.form = new HWForm(NULL, style);
   349     app.form = new HWForm(NULL, style);
   292     app.form->show();
   350     app.form->show();
       
   351     if(splash)
       
   352         splash->close();
   293     return app.exec();
   353     return app.exec();
   294 }
   354 }