1 /* |
1 /* |
2 * Hedgewars, a free turn based strategy game |
2 * Hedgewars, a free turn based strategy game |
3 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
3 * Copyright (c) 2012 Vittorio Giovara <vittorio.giovara@gmail.com> |
4 * |
4 * |
5 * This program is free software; you can redistribute it and/or modify |
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 |
6 * it under the terms of the GNU General Public License as published by |
7 * the Free Software Foundation; version 2 of the License |
7 * the Free Software Foundation; version 2 of the License |
8 * |
8 * |
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
17 */ |
17 */ |
18 |
18 |
19 #include "HWApplication.h" |
19 #include "HWApplication.h" |
20 #include <QFileOpenEvent> |
20 #include <QFileOpenEvent> |
|
21 #include <QEvent> |
21 |
22 |
22 #include "hwform.h" |
23 #include "hwform.h" |
|
24 #include "MessageDialog.h" |
23 |
25 |
24 HWApplication::HWApplication(int &argc, char **argv): |
26 #if !defined(Q_WS_WIN) |
|
27 #include "signal.h" |
|
28 #endif |
|
29 |
|
30 #if !defined(Q_WS_WIN) |
|
31 void terminateFrontend(int signal) |
|
32 { |
|
33 Q_UNUSED(signal); |
|
34 QCoreApplication::exit(0); |
|
35 } |
|
36 #endif |
|
37 |
|
38 HWApplication::HWApplication(int &argc, char **argv) : |
25 QApplication(argc, argv) |
39 QApplication(argc, argv) |
26 { |
40 { |
|
41 #if !defined(Q_WS_WIN) |
|
42 signal(SIGINT, &terminateFrontend); |
|
43 #endif |
|
44 #if 0 |
|
45 qDebug("%s called with", argv[0]); |
|
46 for (int i = 1; i < argc; i++) |
|
47 qDebug("%d: %s", i, argv[i]); |
|
48 #endif |
|
49 // on Windows, sending an event right away leads to a segfault |
|
50 // so we use urlString to save the data and send the event just before the app.exec() |
|
51 urlString = NULL; |
|
52 if (argc > 1) { |
|
53 urlString = new QString(argv[1]); |
|
54 if (urlString->contains("//", Qt::CaseInsensitive) == false) { |
|
55 delete urlString; |
|
56 urlString = NULL; |
|
57 } |
|
58 } |
|
59 } |
27 |
60 |
|
61 void HWApplication::fakeEvent() |
|
62 { |
|
63 QUrl parsedUrl(*urlString); |
|
64 delete urlString; |
|
65 urlString = NULL; |
|
66 QFileOpenEvent *openEvent = new QFileOpenEvent(parsedUrl); |
|
67 QCoreApplication::sendEvent(QCoreApplication::instance(), openEvent); |
28 } |
68 } |
29 |
69 |
30 bool HWApplication::event(QEvent *event) |
70 bool HWApplication::event(QEvent *event) |
31 { |
71 { |
32 QFileOpenEvent *openEvent; |
72 QFileOpenEvent *openEvent; |
|
73 QString scheme, path, address; |
33 |
74 |
34 switch (event->type()) |
75 if (event->type() == QEvent::FileOpen) { |
35 { |
76 openEvent = (QFileOpenEvent *)event; |
36 case QEvent::FileOpen: |
77 scheme = openEvent->url().scheme(); |
37 openEvent = (QFileOpenEvent *)event; |
78 path = openEvent->url().path(); |
38 if (form) form->PlayDemoQuick(openEvent->file()); |
79 address = openEvent->url().host(); |
|
80 |
|
81 QFile file(path); |
|
82 if (scheme == "file" && file.exists()) { |
|
83 form->PlayDemoQuick(path); |
39 return true; |
84 return true; |
40 break; |
85 } else if (scheme == "hwplay") { |
41 default: |
86 int port = openEvent->url().port(NETGAME_DEFAULT_PORT); |
42 return QApplication::event(event); |
87 if (address == "") |
43 break; |
88 address = NETGAME_DEFAULT_SERVER; |
|
89 form->NetConnectQuick(address, (quint16) port); |
|
90 return true; |
|
91 } else { |
|
92 const QString errmsg = tr("Scheme '%1' not supported").arg(scheme); |
|
93 MessageDialog::ShowErrorMessage(errmsg, form); |
|
94 return false; |
|
95 } |
44 } |
96 } |
|
97 |
|
98 return QApplication::event(event); |
45 } |
99 } |
46 |
100 |
47 |
101 |