QTfrontend/HWApplication.cpp
branchwebgl
changeset 8833 c13ebed437cb
parent 8803 b3f37e7f9ff4
child 9163 67334acaaac7
--- a/QTfrontend/HWApplication.cpp	Wed Feb 20 02:21:58 2013 +0100
+++ b/QTfrontend/HWApplication.cpp	Tue Apr 02 21:00:57 2013 +0200
@@ -1,6 +1,6 @@
 /*
  * Hedgewars, a free turn based strategy game
- * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
+ * Copyright (c) 2012 Vittorio Giovara <vittorio.giovara@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,30 +18,84 @@
 
 #include "HWApplication.h"
 #include <QFileOpenEvent>
+#include <QEvent>
 
 #include "hwform.h"
+#include "MessageDialog.h"
 
-HWApplication::HWApplication(int &argc,  char **argv):
+#if !defined(Q_WS_WIN)
+#include "signal.h"
+#endif
+
+#if !defined(Q_WS_WIN)
+void terminateFrontend(int signal)
+{
+    Q_UNUSED(signal);
+    QCoreApplication::exit(0);
+}
+#endif
+
+HWApplication::HWApplication(int &argc, char **argv) :
     QApplication(argc, argv)
 {
+#if !defined(Q_WS_WIN)
+    signal(SIGINT, &terminateFrontend);
+#endif
+#if 0
+    qDebug("%s called with", argv[0]);
+    for (int i = 1; i < argc; i++)
+        qDebug("%d: %s", i, argv[i]);
+#endif
+    // on Windows, sending an event right away leads to a segfault
+    // so we use urlString to save the data and send the event just before the app.exec()
+    urlString = NULL;
+    if (argc > 1) {
+        urlString = new QString(argv[1]);
+        if (urlString->contains("//", Qt::CaseInsensitive) == false) {
+            delete urlString;
+            urlString = NULL;
+        }
+    }
+}
 
+void HWApplication::fakeEvent()
+{
+    QUrl parsedUrl(*urlString);
+    delete urlString;
+    urlString = NULL;
+    QFileOpenEvent *openEvent = new QFileOpenEvent(parsedUrl);
+    QCoreApplication::sendEvent(QCoreApplication::instance(), openEvent);
 }
 
 bool HWApplication::event(QEvent *event)
 {
     QFileOpenEvent *openEvent;
+    QString scheme, path, address;
 
-    switch (event->type())
-    {
-        case QEvent::FileOpen:
-            openEvent = (QFileOpenEvent *)event;
-            if (form) form->PlayDemoQuick(openEvent->file());
+    if (event->type() == QEvent::FileOpen) {
+        openEvent = (QFileOpenEvent *)event;
+        scheme = openEvent->url().scheme();
+        path = openEvent->url().path();
+        address = openEvent->url().host();
+
+        QFile file(path);
+        if (scheme == "file" && file.exists()) {
+            form->PlayDemoQuick(path);
             return true;
-            break;
-        default:
-            return QApplication::event(event);
-            break;
+        } else if (scheme == "hwplay") {
+            int port = openEvent->url().port(NETGAME_DEFAULT_PORT);
+            if (address == "")
+                address = NETGAME_DEFAULT_SERVER;
+            form->NetConnectQuick(address, (quint16) port);
+            return true;
+        } else {
+            const QString errmsg = tr("Scheme '%1' not supported").arg(scheme);
+            MessageDialog::ShowErrorMessage(errmsg, form);
+            return false;
+        }
     }
+
+    return QApplication::event(event);
 }