[go: up one dir, main page]

Menu

[r303]: / main.cpp  Maximize  Restore  History

Download this file

470 lines (414 with data), 13.6 kB

  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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* Marcion
Copyright (C) 2009 - 2011 Milan Konvicka
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
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program*/
#include <QtGui/QApplication>
#include <QObject>
#include <QSplashScreen>
#include <QBitmap>
#include <QtGlobal>
#include <QTextStream>
#include <QTranslator>
#include "mainwnd.h"
#include "outstr.h"
#include "settings.h"
#include <mysql.h>
#include "cmysql.h"
QString host("127.0.0.0");
QString user=("marcion");
QString pwd=("marcion");
QString db=("marcion");
unsigned int port(0);
bool tests=false;
bool globvar=false,sessvar=false;
QString like_v("%");
bool extra_server_options=false;
int extra_sopts_count=0;
char ** extra_sopts=0;
static char * server_options[] = \
{ (char*)"mysql",(char*)"--defaults-file=./mysql/my.cnf",(char*)"--basedir=./mysql",(char*)"--skip-innodb"
};
int num_elements = 4;
static char * server_groups[] = { (char*)"embedded", 0 };
int getMainArgs(int argc, char *argv[]);
void showGlobVars(),showSessVars();
void printVersion(),printHelp();
void finishMysql(bool);
void removeConfigFile();
bool standalone=false;
int main(int argc, char *argv[])
{
int err=getMainArgs(argc,argv);
OSTREAM.flush();
switch(err)
{
case 2 :
return 0;
case 0 :
break;
default:
return err;
break;
}
QApplication a(argc, argv);
a.setApplicationName("Marcion");
a.setApplicationVersion("1.5");
a.setOrganizationName("Vagrant");
a.setOrganizationDomain("Vagrant");
a.setWindowIcon(QIcon(":/new/icons/icons/main.png"));
CSettings::marcDir=QDir::toNativeSeparators(QCoreApplication::applicationDirPath());
if(!QDir::setCurrent(CSettings::marcDir))
{
OSTREAM << "cannot change current directory to " << CSettings::marcDir << "\n";
OSTREAM.flush();
return 13;
}
else
{
OSTREAM << "current directory changed to " << CSettings::marcDir << "\n";
OSTREAM.flush();
}
QTranslator qtTranslator;
if(!tests)
{
a.setQuitOnLastWindowClosed(true);
QFont appf;
int lang;
bool interrupted(false);
bool raf(CSettings::readAppFont(&qtTranslator,lang,appf,interrupted));
if(interrupted)
{
OSTREAM << "interrupted by user during basic configuration\n";
OSTREAM.flush();
return 0;
}
if(raf)
{
a.setFont(appf);
OSTREAM << "application font " << appf.family() << " size " << appf.pointSize() << " used\n";
OSTREAM.flush();
}
else
{
OSTREAM << "extra application font is turned off\n";
OSTREAM.flush();
}
if(!CSettings::wiz)
{
if(lang!=0)
{
QString trname(CSettings::marcDir+QDir::separator()+"marcion_"),lapp;
switch(lang)
{
case CSettings::Czech :
lapp="cs";
break;
case CSettings::Greek :
lapp="el";
break;
case CSettings::German :
lapp="de";
break;
}
trname.append(lapp);
if(!qtTranslator.load(trname))
{
OSTREAM << "Warning: cannot initialize "+lapp+" language (file '"+trname+"')\n";
OSTREAM.flush();
//return 14;
}
else
{
OSTREAM << lapp+QObject::tr(" language initialized (file '")+trname+"')\n";
OSTREAM.flush();
a.installTranslator(&qtTranslator);
}
}
else
{
OSTREAM << "native language used (english)\n";
OSTREAM.flush();
}
}
}
QSplashScreen splash;
if(!tests)
{
SET_BUSY_CURSOR
QPixmap splp(":/new/icons/icons/marcion_logo.png");
splash.setPixmap(splp);
splash.setMask(splp.mask());
splash.show();
}
OSTREAM << "mysql_library_init() ...\n";
int mlir;
if(!standalone)
{
if(!extra_server_options)
mlir=mysql_library_init(num_elements, server_options, server_groups);
else
mlir=mysql_library_init(extra_sopts_count, extra_sopts, 0);
}
else
mlir=mysql_library_init(-1, 0, 0);
if(mlir!=0)
{
OSTREAM << QObject::tr("mysql_library_init failed: errorcode ") << QString::number(mlir) << "\n";
OSTREAM.flush();
return 10;
}
else
OSTREAM << "OK\n";
OSTREAM.flush();
OSTREAM << "MySql client: " << QString(mysql_get_client_info()) << "\n";
OSTREAM.flush();
OSTREAM << "mysql_init() ";
CMySql::m_init = mysql_init(0);
if(!CMySql::m_init)
{
OSTREAM << QObject::tr("failed (out of memory)\n");
finishMysql(false);
}
else
{
OSTREAM << "OK\n";
}
OSTREAM.flush();
if(!standalone)
{
mysql_options(CMySql::m_init, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_options(CMySql::m_init, MYSQL_OPT_USE_EMBEDDED_CONNECTION, 0);
}
else
mysql_options(CMySql::m_init, MYSQL_OPT_USE_REMOTE_CONNECTION, 0);
if(!CMySql::connect_local(host,user,pwd,db,port))
{
OSTREAM << CMySql::last_Error();
OSTREAM.flush();
finishMysql(true);
return 11;
}
else
{
if(globvar)
showGlobVars();
if(sessvar)
showSessVars();
}
int r=0;
if(!tests)
{
MainWnd w;
w.show();
splash.finish(&w);
r=a.exec();
}
finishMysql(true);
if(r==0)
OSTREAM << QObject::tr("Success!\n");
OSTREAM.flush();
return r;
}
int getMainArgs(int argc, char *argv[])
{
for(int x=1;x<argc;x++)
{
QString arg(argv[x]);
if(arg=="--rm-config"||arg=="-r")
{
removeConfigFile();
}
else if(arg=="--standalone-server"||arg=="-s")
{
OSTREAM << QObject::tr("standalone server requested\n");
standalone=true;
extra_server_options=false;
//return 0;
}
else if(arg=="--mysql-server-opts"||arg=="-e")
{
OSTREAM << QObject::tr("extra server options received\n");
extra_server_options=true;
standalone=false;
extra_sopts_count=argc-x-1;
extra_sopts=&argv[x+1];
for(int y=0;y<extra_sopts_count;y++)
OSTREAM << QObject::tr("option ") << y+1 << QString(extra_sopts[y]) << "\n";
return 0;
}
else if(arg=="--version"||arg=="-V")
{
printVersion();
return 2;
}
else if(arg=="--help"||arg=="-h")
{
printHelp();
return 2;
}
else if(arg=="--test"||arg=="-t")
{
printf("%s\n","test:");
tests=true;
}
else if(arg.startsWith("--connect"))
{
QString a(arg);
a.remove("--connect=");
QStringList al(a.split(","));
if(al.count()!=5)
{
OSTREAM << QObject::tr("incorrect use of --connect\n");
return 3;
}
host=al[0];
user=al[1];
pwd=al[2];
db=al[3];
port=al[4].toUInt();
}
else if(arg=="--show-global-vars"||arg=="-g")
{
globvar=true;
}
else if(arg=="--show-session-vars"||arg=="-i")
{
sessvar=true;
}
else if(arg.startsWith("--like"))
{
QString a(arg);
like_v=a.remove("--like=");
}
else
{
OSTREAM << QObject::tr("unknown option: ") << QString(argv[x]) << "\n";
return 1;
}
}
return 0;
}
void finishMysql(bool close)
{
if(close)
{
mysql_close(CMySql::m_init);
OSTREAM << "mysql_close()\n";
}
mysql_library_end();
OSTREAM << "mysql_library_end()\n";
OSTREAM.flush();
}
void showGlobVars()
{
OSTREAM << QObject::tr("global variables: filter: ") << like_v << "\n";
CMySql q(QString(QObject::tr("show global variables like \"")+like_v+"\""));
if(q.exec())
{
while(q.next())
{
QString v1(q.value(0));
QString v2(q.value(1));
OSTREAM << QObject::tr("name: ") << v1 << QObject::tr("\nvalue: ") << v2 << "\n";
}
OSTREAM << QObject::tr("global variables: end\n");
}
else
{
OSTREAM << QObject::tr("cannot obtain informations about variables\n");
OSTREAM << q.lastError() << "\n";
}
OSTREAM.flush();
}
void showSessVars()
{
OSTREAM << QObject::tr("session variables: filter: ") << like_v << "\n";
CMySql q(QString(QObject::tr("show session variables like \"")+like_v+"\""));
if(q.exec())
{
while(q.next())
{
QString v1(q.value(0));
QString v2(q.value(1));
OSTREAM << QObject::tr("name: ") << v1 << QObject::tr("\nvalue: ") << v2 << "\n";
}
OSTREAM << QObject::tr("session variables: end\n");
}
else
{
OSTREAM << QObject::tr("cannot obtain informations about variables\n");
OSTREAM << q.lastError() << "\n";
}
OSTREAM.flush();
}
void removeConfigFile()
{
OSTREAM << QObject::tr("removing file 'config.txt' ... ");
OSTREAM.flush();
if(QFile::exists("config.txt"))
{
if(QFile::remove("config.txt"))
OSTREAM << QObject::tr("Ok.\n");
else
OSTREAM << QObject::tr("Failed.\n");
}
else
OSTREAM << QObject::tr("cannot remove 'config.txt', file don't exist\n");
OSTREAM.flush();
OSTREAM << QObject::tr("removing file 'config-defaults.txt' ... ");
OSTREAM.flush();
if(QFile::exists("config-defaults.txt"))
{
if(QFile::remove("config-defaults.txt"))
OSTREAM << QObject::tr("Ok.\n");
else
OSTREAM << QObject::tr("Failed.\n");
}
else
OSTREAM << QObject::tr("cannot remove 'config-defaults.txt', file don't exist\n");
OSTREAM.flush();
}
void printVersion()
{
OSTREAM << QCoreApplication::applicationName() << " " << QCoreApplication::applicationVersion() << "\n\n";
OSTREAM << "Qt version: " << QString(QT_VERSION_STR);
OSTREAM << "\nMySql client: " << QString(mysql_get_client_info()) << "\n\n";
OSTREAM.flush();
}
void printHelp()
{
//printVersion();
OSTREAM << QObject::tr("Copyright (c) 2009-2011 Milan Konvicka. All rights reserved.\n") << QObject::tr("This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license.\n\n");
OSTREAM << QObject::tr("usage: marcion [OPTIONS]\n\n");
OSTREAM << QObject::tr("options:\n\n");
OSTREAM << "--standalone-server, -s\n";
OSTREAM << QObject::tr(" connected is standalone mysql server (local or remote) instead embedded\n\n");
OSTREAM << "--mysql-server-opts, -e\n";
OSTREAM << QObject::tr(" following arguments [arg1] [arg2] [arg3] ... [argN] are passed into embedded mysql server. using of it clears all internal options of server. (this argument should be used as last of all others arguments)\n\n");
OSTREAM << QObject::tr("--connect=comma separated list of values\n");
OSTREAM << QObject::tr(" comma separated list of values used for connection to server. (order: host,user,password,database,port)\n\n");
OSTREAM << "--test, -t\n";
OSTREAM << QObject::tr(" test of server and connection\n\n");
OSTREAM << "--show-global-vars, -g\n";
OSTREAM << QObject::tr(" shows global variables obtained from running MySql server\n\n");
OSTREAM << "--show-session-vars, -i\n";
OSTREAM << QObject::tr(" shows session variables obtained from running MySql server\n\n");
OSTREAM << "--like=string\n";
OSTREAM << QObject::tr(" sets filter for list of global and/or session variables invoked by -g and/or -i\n\n");
OSTREAM << "--rm-config, -r\n";
OSTREAM << QObject::tr(" removes configuration files 'config.txt' and 'config-defaults.txt' (if exists) before start\n\n");
OSTREAM << "--version, -V\n";
OSTREAM << QObject::tr(" version\n\n");
OSTREAM << "--help, -h\n";
OSTREAM << QObject::tr(" this help\n\n");
OSTREAM << QObject::tr("Notes:\n -> options -s and -e cannot be used together <-\n -> if you are using -e, then use it as last parameter. all parameters following -e are passed to mysql server <-\n -> -t can be used together with -s, -e, --connect <-\n\n");
OSTREAM << QObject::tr("for more informations look at http://marcion.sourceforge.net/doc.html\n\n");
OSTREAM.flush();
}