-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
273 lines (251 loc) · 8.54 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <QCoreApplication>
#include <QDebug>
#include <QDateTime>
#include <QSettings>
#include <QString>
#include <QDir>
#include <QUrl>
#include <readresources.h>
#include <server.h>
#include <dxclient.h>
#include <priyom.h>
#include <cleaner.h>
QStringList marker;
QStringList spot;
QStringList priyom;
void noDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QString current_time=QDateTime::currentDateTime().toString("ddMMyyyy-hh:mm:ss");
switch (type) {
case QtDebugMsg:
// fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtInfoMsg:
// fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
fprintf(stderr,"%s Info: %s \n",current_time.toStdString().c_str(),localMsg.constData());
break;
case QtWarningMsg:
// fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "%s Critical: %s (%s:%u, %s)\n",current_time.toStdString().c_str(), localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "%s Fatal: %s (%s:%u, %s)\n",current_time.toStdString().c_str(), localMsg.constData(), context.file, context.line, context.function);
abort();
}
}
void DebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QString current_time=QDateTime::currentDateTime().toString("ddMMyyyy-hh:mm:ss");
switch (type) {
case QtDebugMsg:
fprintf(stderr, "%s Debug: %s (%s:%u, %s)\n",current_time.toStdString().c_str(),localMsg.constData(), context.file, context.line, context.function);
break;
case QtInfoMsg:
fprintf(stderr, "%s Info: %s (%s:%u, %s)\n",current_time.toStdString().c_str(),localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "%s Warning: %s (%s:%u, %s)\n",current_time.toStdString().c_str(),localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "%s Critical: %s (%s:%u, %s)\n",current_time.toStdString().c_str(), localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "%s Fatal: %s (%s:%u, %s)\n",current_time.toStdString().c_str(), localMsg.constData(), context.file, context.line, context.function);
abort();
}
}
void ask_port()
{
QSettings settings;
QTextStream cin(stdin);
qInfo().nospace()<<"A free TCP port to listen on: ";
int tcpport=cin.readLine().toInt();
if (tcpport<1024||tcpport>65535)
{
qInfo()<<"TCP port must be between 1024 and 65535";
ask_port();
}
else
settings.setValue("tcpport",tcpport);
}
void ask_callsign()
{
QSettings settings;
QTextStream cin(stdin);
qInfo().nospace()<<"Your callsign: ";
QString callsign=cin.readLine();
if (callsign.isEmpty())
{
qInfo()<<"Callsign must not be empty!";
ask_callsign();
}
else
settings.setValue("callsign",callsign);
}
void ask_dxcluster()
{
QSettings settings;
QTextStream cin(stdin);
qInfo().nospace()<<"Public DX cluster to connect to: ";
QString dxcluster=cin.readLine();
if(dxcluster.isEmpty())
{
qInfo()<<"Public DX cluster must not be empty!";
ask_dxcluster();
}
else
{
QUrl url(dxcluster);
if (!url.isValid())
{
qInfo()<<"Public DX cluster invalid!";
ask_dxcluster();
}
else
settings.setValue("dxcluster",dxcluster);
}
}
void ask_wrkdir()
{
if (QSysInfo::kernelType()=="winnt")
{
QSettings settings;
QTextStream cin(stdin);
qInfo()<<"Directory with resources: ";
qInfo()<<"(press enter for default C:\\Shiva2\\)";
QString wrkdir=cin.readLine();
if (wrkdir.isEmpty())
wrkdir="C:\\Shiva2\\\\";
else
wrkdir.append("\\");
QDir dir(wrkdir);
if (!dir.exists())
{
qInfo()<<"Directory not found! Create it? [y/N]";
QString reply=cin.readLine();
if (reply=="y")
{
dir.mkpath(wrkdir);
settings.setValue("wrkdir",wrkdir);
}
else
ask_wrkdir();
}
else
settings.setValue("wrkdir",wrkdir);
}
else if (QSysInfo::kernelType()=="linux")
{
QSettings settings;
QTextStream cin(stdin);
qInfo()<<"Directory with resources: ";
qInfo()<<"(press enter for default /etc/shiva2/)";
QString wrkdir=cin.readLine();
if (wrkdir.isEmpty())
wrkdir="//etc//shiva2//";
else
{
wrkdir.append("/");
wrkdir.replace("/","//");
}
qInfo()<<"Directory"<<wrkdir;
QDir dir(wrkdir);
if (!dir.exists())
{
qInfo()<<"Directory"<<wrkdir<<"not found!";
ask_wrkdir();
}
else
settings.setValue("wrkdir",wrkdir);
}
}
void ask_interval()
{
QSettings settings;
QTextStream cin(stdin);
qInfo().nospace()<<"Sending interval in seconds (press enter for default = 30 secs) : ";
int interval=cin.readLine().toInt();
if (interval==0)
interval=30;
if (interval<5||interval>1800)
{
qInfo()<<"Interval must be between 5 (5 seconds) and 1800 (30 mins)";
ask_interval();
}
else
settings.setValue("interval",interval*1000);
}
void ask_timeout()
{
QSettings settings;
QTextStream cin(stdin);
qInfo().nospace()<<"Timeout in seconds (press enter for default = 2 seconds) : ";
int timeout=cin.readLine().toInt();
if (timeout==0)
timeout=2;
if (timeout<2||timeout>60)
{
qInfo()<<"Timeout must be between 2 (2 seconds) and 60 (1 min)";
ask_timeout();
}
else
settings.setValue("timeout",timeout*1000);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if ( ! a.arguments().contains(QLatin1String("-d"))) {
qInstallMessageHandler(noDebugOutput);
}
else
qInstallMessageHandler(DebugOutput);
QCoreApplication::setOrganizationName("IU2MEH");
QCoreApplication::setOrganizationDomain("-");
QCoreApplication::setApplicationName("Shiva2");
QSettings settings;
QTextStream cin(stdin);
if (! a.arguments().contains(QLatin1String("-b")))
{
if (!settings.value("tcpport").isNull()&&!settings.value("callsign").isNull()&&!settings.value("dxcluster").isNull())
{
qInfo()<<"Settings found:";
qInfo()<<"TCP port: "+settings.value("tcpport").toString();
qInfo()<<"Callsign: "+settings.value("callsign").toString();
qInfo()<<"Public DX cluster: "+settings.value("dxcluster").toString();
qInfo()<<"Directory with resources: "+settings.value("wrkdir").toString();
int interval=settings.value("interval").toInt()/1000;
qInfo()<<"Interval: "+QString::number(interval)+" seconds";
int timeout=settings.value("timeout").toInt()/1000;
qInfo()<<"Timeout: "+QString::number(timeout)+" seconds";
qInfo().noquote()<<"Do you want to change them? [y/N] ";
QString reply=cin.readLine();
if (reply=="y")
settings.clear();
}
if (settings.value("tcpport").isNull()||settings.value("callsign").isNull()||settings.value("dxcluster").isNull())
{
settings.clear();
qInfo()<<"Please fill settings:";
ask_port();
ask_callsign();
ask_dxcluster();
ask_wrkdir();
ask_interval();
ask_timeout();
}
}
qInfo()<<"Starting...";
marker.clear();
spot.clear();
priyom.clear();
Server myserver;
ReadResources readresources;
DXClient dxclient;
Priyom priyom;
Cleaner cleaner;
return a.exec();
}