forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cpp
127 lines (115 loc) · 4.45 KB
/
Application.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
#include <QFileOpenEvent>
#include <QTranslator>
#include <QTextCodec>
#include <QLibraryInfo>
#include <QLocale>
#include <QDebug>
#include "Application.h"
#include "MainWindow.h"
Application::Application(int& argc, char** argv) :
QApplication(argc, argv)
{
// Set organisation and application names
setOrganizationName("sqlitebrowser");
setApplicationName("SQLite Database Browser");
// Set character encoding to UTF8
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
#if QT_VERSION < 0x050000
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
#endif
// Load translations
bool ok;
QString name = QLocale::system().name();
// First of all try to load the application translation file.
m_translatorApp = new QTranslator(this);
ok = m_translatorApp->load("sqlb_" + name, "translations");
if (ok == true) {
installTranslator(m_translatorApp);
// The application translation file has been found and loaded.
// And now try to load a Qt translation file.
// Search path:
// 1) standard Qt translations directory;
// 2) the application translations directory.
m_translatorQt = new QTranslator(this);
ok = m_translatorQt->load("qt_" + name,
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
if (ok == false)
ok = m_translatorQt->load("qt_" + name, "translations");
if (ok == true)
installTranslator(m_translatorQt);
}
// Parse command line
QString fileToOpen;
QStringList sqlToExecute;
m_dontShowMainWindow = false;
for(int i=1;i<arguments().size();i++)
{
// Check next command line argument
if(arguments().at(i) == "-h" || arguments().at(i) == "--help")
{
// Help
qWarning() << qPrintable(tr("Usage: %1 [options] [db]\n").arg(argv[0]));
qWarning() << qPrintable(tr("Possible command line arguments:"));
qWarning() << qPrintable(tr(" -h, --help\t\tShow command line options"));
qWarning() << qPrintable(tr(" -s, --sql [file]\tExecute this SQL file after opening the DB"));
qWarning() << qPrintable(tr(" -q, --quit\t\tExit application after running scripts"));
qWarning() << qPrintable(tr(" [file]\t\tOpen this SQLite database"));
m_dontShowMainWindow = true;
} else if(arguments().at(i) == "-s" || arguments().at(i) == "--sql") {
// Run SQL file: If file exists add it to list of scripts to execute
if(++i >= arguments().size())
qWarning() << qPrintable(tr("The -s/--sql option requires an argument"));
else if(!QFile::exists(arguments().at(i)))
qWarning() << qPrintable(tr("The file %1 does not exist").arg(arguments().at(i)));
else
sqlToExecute.append(arguments().at(i));
} else if(arguments().at(i) == "-q" || arguments().at(i) == "--quit") {
m_dontShowMainWindow = true;
} else {
// Other: Check if it's a valid file name
if(QFile::exists(arguments().at(i)))
fileToOpen = arguments().at(i);
else
qWarning() << qPrintable(tr("Invalid option/non-existant file: %1").arg(arguments().at(i)));
}
}
// Show main window
m_mainWindow = new MainWindow();
m_mainWindow->show();
connect(this, SIGNAL(lastWindowClosed()), this, SLOT(quit()));
// Open database if one was specified
if(fileToOpen.size())
{
if(m_mainWindow->fileOpen(fileToOpen))
{
// If database could be opened run the SQL scripts
foreach(const QString& f, sqlToExecute)
{
QFile file(f);
if(file.open(QIODevice::ReadOnly))
{
m_mainWindow->getDb()->executeMultiSQL(file.readAll(), false, true);
file.close();
}
}
if(!sqlToExecute.isEmpty())
m_mainWindow->browseRefresh();
}
}
}
Application::~Application()
{
delete m_mainWindow;
}
bool Application::event(QEvent* event)
{
switch(event->type())
{
case QEvent::FileOpen:
m_mainWindow->fileOpen(static_cast<QFileOpenEvent*>(event)->file());
return true;
default:
return QApplication::event(event);
}
}