QTfrontend/main.cpp
branchwebgl
changeset 8444 75db7bb8dce8
parent 8406 8a834943609d
child 8508 f849b7b3af1d
equal deleted inserted replaced
8340:46a9fde631f4 8444:75db7bb8dce8
    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(dir);
   108     QDir tmpdir(dir);
    92     if (!tmpdir.exists())
   109     if (!tmpdir.exists())
   110         return tmpfile.open(QFile::WriteOnly);
   127         return tmpfile.open(QFile::WriteOnly);
   111     else
   128     else
   112         return true;
   129         return true;
   113 }
   130 }
   114 
   131 
       
   132 // Guaranteed to be the last thing ran in the application's life time.
       
   133 // Closes resources that need to exist as long as possible.
       
   134 void closeResources(void)
       
   135 {
   115 #ifdef __APPLE__
   136 #ifdef __APPLE__
   116 static CocoaInitializer *cocoaInit = NULL;
       
   117 // Function to be called at end of program's termination on OS X to release
       
   118 // the NSAutoReleasePool contained within the CocoaInitializer.
       
   119 void releaseCocoaPool(void)
       
   120 {
       
   121     if (cocoaInit != NULL)
   137     if (cocoaInit != NULL)
   122     {
   138     {
   123         delete cocoaInit;
   139         delete cocoaInit;
   124         cocoaInit = NULL;
   140         cocoaInit = NULL;
   125     }
   141     }
   126 }
   142 #endif
   127 #endif
   143     if (engine != NULL)
       
   144     {
       
   145         delete engine;
       
   146         engine = NULL;
       
   147     }
       
   148 }
   128 
   149 
   129 int main(int argc, char *argv[])
   150 int main(int argc, char *argv[])
   130 {
   151 {
       
   152     // Since we're calling this first, closeResources() will be the last thing called after main() returns.
       
   153     atexit(closeResources);
       
   154 
   131 #ifdef __APPLE__
   155 #ifdef __APPLE__
   132     // This creates the autoreleasepool that prevents leaking, and destroys it only on exit
   156     cocoaInit = new CocoaInitializer(); // Creates the autoreleasepool preventing cocoa object leaks on OS X.
   133     cocoaInit = new CocoaInitializer();
   157 #endif
   134     atexit(releaseCocoaPool);
   158 
       
   159 #ifndef _WIN32
       
   160     signal(SIGINT, &terminateFrontend);
   135 #endif
   161 #endif
   136 
   162 
   137     HWApplication app(argc, argv);
   163     HWApplication app(argc, argv);
   138 
   164 
   139     FileEngineHandler engine(argv[0]);
   165     QLabel *splash = NULL;
       
   166 #if defined Q_WS_WIN
       
   167     QPixmap pixmap(":res/splash.png");
       
   168     splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
       
   169     splash->setAttribute(Qt::WA_TranslucentBackground);
       
   170     const QRect deskSize = QApplication::desktop()->screenGeometry(-1);
       
   171     QPoint splashCenter = QPoint( (deskSize.width() - pixmap.width())/2,
       
   172                                   (deskSize.height() - pixmap.height())/2 );
       
   173     splash->move(splashCenter);
       
   174     splash->setPixmap(pixmap);
       
   175     splash->show();
       
   176 #endif
       
   177 
       
   178     engine = new FileEngineHandler(argv[0]);
   140 
   179 
   141     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
   180     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
   142 
   181 
   143     QStringList arguments = app.arguments();
   182     QStringList arguments = app.arguments();
   144     QMap<QString, QString> parsedArgs;
   183     QMap<QString, QString> parsedArgs;
   192 
   231 
   193     Q_INIT_RESOURCE(hedgewars);
   232     Q_INIT_RESOURCE(hedgewars);
   194 
   233 
   195     qRegisterMetaType<HWTeam>("HWTeam");
   234     qRegisterMetaType<HWTeam>("HWTeam");
   196 
   235 
   197     // workaround over NSIS installer which modifies the install path
       
   198     //bindir->cd("./");
       
   199     bindir->cd(QCoreApplication::applicationDirPath());
   236     bindir->cd(QCoreApplication::applicationDirPath());
   200 
   237 
   201     if(custom_config == false)
   238     if(custom_config == false)
   202     {
   239     {
   203 #ifdef __APPLE__
   240 #ifdef __APPLE__
   251         missingMsg.exec();
   288         missingMsg.exec();
   252         return 1;
   289         return 1;
   253     }
   290     }
   254 
   291 
   255     // setup PhysFS
   292     // setup PhysFS
   256     engine.mount(datadir->absolutePath());
   293     engine->mount(datadir->absolutePath());
   257     engine.mount(cfgdir->absolutePath() + "/Data");
   294     engine->mount(cfgdir->absolutePath() + "/Data");
   258     engine.mount(cfgdir->absolutePath());
   295     engine->mount(cfgdir->absolutePath());
   259     engine.setWriteDir(cfgdir->absolutePath());
   296     engine->setWriteDir(cfgdir->absolutePath());
   260     engine.mountPacks();
   297     engine->mountPacks();
   261 
   298 
   262     checkForFile("physfs://hedgewars.ini");
   299     checkForFile("physfs://hedgewars.ini");
   263 
   300 
   264     QTranslator Translator;
   301     QTranslator Translator;
   265     {
   302     {
   303         case SEASON_HWBDAY :
   340         case SEASON_HWBDAY :
   304             fname = "birthday.css";
   341             fname = "birthday.css";
   305             break;
   342             break;
   306         default :
   343         default :
   307             fname = "qt.css";
   344             fname = "qt.css";
       
   345             break;
   308     }
   346     }
   309 
   347 
   310     // load external stylesheet if there is any
   348     // load external stylesheet if there is any
   311     QFile extFile("physfs://css/" + fname);
   349     QFile extFile("physfs://css/" + fname);
   312 
   350 
   317     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   355     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   318         style.append(file.readAll());
   356         style.append(file.readAll());
   319 
   357 
   320     app.form = new HWForm(NULL, style);
   358     app.form = new HWForm(NULL, style);
   321     app.form->show();
   359     app.form->show();
       
   360     if(splash)
       
   361         splash->close();
   322     return app.exec();
   362     return app.exec();
   323 }
   363 }