1 /*
2  * Hedgewars, a free turn based strategy game
3  * Copyright (c) 2012 Vittorio Giovara <vittorio.giovara@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include "HWApplication.h"
20 #include <QFileOpenEvent>
21 #include <QEvent>
22 
23 #include "MessageDialog.h"
24 
25 #if !defined(Q_OS_WIN)
26 #include "signal.h"
27 #endif
28 
29 #if !defined(Q_OS_WIN)
terminateFrontend(int signal)30 void terminateFrontend(int signal)
31 {
32     Q_UNUSED(signal);
33     QCoreApplication::exit(0);
34 }
35 #endif
36 
HWApplication(int & argc,char ** argv)37 HWApplication::HWApplication(int &argc, char **argv) :
38     QApplication(argc, argv)
39 {
40     form = 0;
41 
42 #if !defined(Q_OS_WIN)
43     signal(SIGINT, &terminateFrontend);
44 #endif
45 #if 0
46     qDebug("%s called with", argv[0]);
47     for (int i = 1; i < argc; i++)
48         qDebug("%d: %s", i, argv[i]);
49 #endif
50     // on Windows, sending an event right away leads to a segfault
51     // so we use urlString to save the data and send the event just before the app.exec()
52     urlString = NULL;
53     if (argc > 1) {
54         urlString = new QString(argv[1]);
55         if (urlString->contains("//", Qt::CaseInsensitive) == false) {
56             delete urlString;
57             urlString = NULL;
58         }
59     }
60 }
61 
fakeEvent()62 void HWApplication::fakeEvent()
63 {
64     QUrl parsedUrl(*urlString);
65     delete urlString;
66     urlString = NULL;
67     QFileOpenEvent *openEvent = new QFileOpenEvent(parsedUrl);
68     QCoreApplication::sendEvent(QCoreApplication::instance(), openEvent);
69 }
70 
event(QEvent * event)71 bool HWApplication::event(QEvent *event)
72 {
73     QFileOpenEvent *openEvent;
74     QString scheme, path, address;
75 
76     if (event->type() == QEvent::FileOpen) {
77         openEvent = (QFileOpenEvent *)event;
78         scheme = openEvent->url().scheme();
79         path = openEvent->url().path();
80         address = openEvent->url().host();
81 
82         QFile file(path);
83         if (scheme == "file" && file.exists()) {
84             form->PlayDemoQuick(path);
85             return true;
86         } else if (scheme == "hwplay") {
87             int port = openEvent->url().port(NETGAME_DEFAULT_PORT);
88             if (address == "")
89                 address = NETGAME_DEFAULT_SERVER;
90             form->NetConnectQuick(address, (quint16) port);
91             return true;
92         } else {
93             //: Here, “scheme” refers to the scheme of a Uniform Resource Identifier”
94             const QString errmsg = tr("Scheme '%1' not supported").arg(scheme);
95             MessageDialog::ShowErrorMessage(errmsg, form);
96             return false;
97         }
98     }
99 
100     return QApplication::event(event);
101 }
102 
103 
104