summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/simplecommandlineapp.cpp
blob: 5b3732a5f6d00fdbdc5095805d873163e89f54b5 (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "simplecommandlineapp.h"

#include <tqfileinfo.h>

#include <kcmdlineargs.h>
#include <kdebug.h>

#include <kexidb/connectiondata.h>
#include <kexidb/drivermanager.h>

using namespace KexiDB;

static KCmdLineOptions predefinedOptions[] =
{
	{ "drv", 0, 0 },
	{ "driver <name>", I18N_NOOP("Database driver name"), 0 },
	{ "u", 0, 0 },
	{ "user <name>", I18N_NOOP("Database user name"), 0 },
	{ "p", 0, 0 },
	{ "password", I18N_NOOP("Prompt for password"), 0 },
	{ "h", 0, 0 },
	{ "host <name>", I18N_NOOP("Host (server) name"), 0 },
	{ "port <number>", I18N_NOOP("Server's port number"), 0 },
	{ "s", 0, 0 },
	{ "local-socket <filename>", I18N_NOOP("Server's local socket filename"), 0 },
	KCmdLineLastOption
};

//-----------------------------------------

//! @internal used for SimpleCommandLineApp
class SimpleCommandLineApp::Private
{
public:
	Private()
	 : conn(0)
	{}
	~Private()
	{
		if (conn) {
			conn->disconnect();
			delete (Connection*)conn;
		}
		delete instance;

		for (KCmdLineOptions *optionsPtr = allOptions; optionsPtr->name; optionsPtr++) {
			delete optionsPtr->name;
			delete optionsPtr->description;
			delete optionsPtr->def;
		}
		delete allOptions;
	}

	KexiDB::DriverManager manager;
	KCmdLineOptions *allOptions;
	TDEInstance* instance;
	ConnectionData connData;
	TQGuardedPtr<Connection> conn;
};

//-----------------------------------------

SimpleCommandLineApp::SimpleCommandLineApp(
	int argc, char** argv, KCmdLineOptions *options,
	const char *programName, const char *version, 
	const char *shortDescription, int licenseType, 
	const char *copyrightStatement, const char *text, 
	const char *homePageAddress, const char *bugsEmailAddress)
 : Object()
 , d( new Private() )
{
	TQFileInfo fi(argv[0]);
	TQCString appName( fi.baseName().latin1() );
	TDECmdLineArgs::init(argc, argv, 
		new TDEAboutData( appName, programName,
			version, shortDescription, licenseType, copyrightStatement, text, 
			homePageAddress, bugsEmailAddress));

	int predefinedOptionsCount = 0;
	for (KCmdLineOptions *optionsPtr = predefinedOptions; optionsPtr->name; optionsPtr++, predefinedOptionsCount++)
		;
	int userOptionsCount = 0;
	for (KCmdLineOptions *optionsPtr = options; optionsPtr->name; optionsPtr++, userOptionsCount++)
		;

	d->instance = new TDEInstance(appName);

	// join the predefined options and user options
	d->allOptions = new KCmdLineOptions[predefinedOptionsCount + userOptionsCount + 1];
	KCmdLineOptions *allOptionsPtr = d->allOptions;
	for (KCmdLineOptions *optionsPtr = predefinedOptions; optionsPtr->name; optionsPtr++, allOptionsPtr++) {
		allOptionsPtr->name = tqstrdup(optionsPtr->name);
		allOptionsPtr->description = tqstrdup(optionsPtr->description);
		if (optionsPtr == predefinedOptions) //first row == drv
			allOptionsPtr->def = tqstrdup(KexiDB::Driver::defaultFileBasedDriverName().latin1());
		else
			allOptionsPtr->def = tqstrdup(optionsPtr->def);
	}
	for (KCmdLineOptions *optionsPtr = options; optionsPtr->name; optionsPtr++, allOptionsPtr++) {
		allOptionsPtr->name = tqstrdup(optionsPtr->name);
		allOptionsPtr->description = tqstrdup(optionsPtr->description);
		allOptionsPtr->def = tqstrdup(optionsPtr->def);
	}
	allOptionsPtr->name = 0; //end
	allOptionsPtr->description = 0;
	allOptionsPtr->def = 0;
	TDECmdLineArgs::addCmdLineOptions( d->allOptions );

	TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

	d->connData.driverName = args->getOption("driver");
	d->connData.userName = args->getOption("user");
	d->connData.hostName = args->getOption("host");
	d->connData.localSocketFileName = args->getOption("local-socket");
	d->connData.port = args->getOption("port").toInt();
	d->connData.useLocalSocketFile = args->isSet("local-socket");

	if (args->isSet("password")) {
		TQString userAtHost = d->connData.userName;
		if (!d->connData.userName.isEmpty())
			userAtHost += "@";
		userAtHost += (d->connData.hostName.isEmpty() ? "localhost" : d->connData.hostName);
		TQTextStream cout(stdout,IO_WriteOnly);
		cout << i18n("Enter password for %1: ").arg(userAtHost);
//! @todo make use of pty/tty here! (and care about portability)
		TQTextStream cin(stdin,IO_ReadOnly);
		cin >> d->connData.password;
		KexiDBDbg << d->connData.password << endl;
	}
}

SimpleCommandLineApp::~SimpleCommandLineApp()
{
	closeDatabase();
	delete d;
}

bool SimpleCommandLineApp::openDatabase(const TQString& databaseName)
{
	if (!d->conn) {
		if (d->manager.error()) {
			setError(&d->manager);
			return false;
		}

		//get the driver
		KexiDB::Driver *driver = d->manager.driver(d->connData.driverName);
		if (!driver || d->manager.error()) {
			setError(&d->manager);
			return false;
		}

		if (driver->isFileDriver())
			d->connData.setFileName( databaseName );

		d->conn = driver->createConnection(d->connData);
		if (!d->conn || driver->error()) {
			setError(driver);
			return false;
		}
	}
	if (d->conn->isConnected()) {
		// db already opened
		if (d->conn->isDatabaseUsed() && d->conn->currentDatabase()==databaseName) //the same: do nothing
			return true;
		if (!closeDatabase()) // differs: close the first
			return false;
	}
	if (!d->conn->connect()) {
		setError(d->conn);
		delete d->conn;
		d->conn = 0;
		return false;
	}

	if (!d->conn->useDatabase( databaseName )) {
		setError(d->conn);
		delete d->conn;
		d->conn = 0;
		return false;
	}
	return true;
}

bool SimpleCommandLineApp::closeDatabase()
{
	if (!d->conn)
		return true;
	if (!d->conn->disconnect()) {
		setError(d->conn);
		return false;
	}
	return true;
}

TDEInstance* SimpleCommandLineApp::instance() const
{
	return d->instance;
}

KexiDB::ConnectionData* SimpleCommandLineApp::connectionData() const
{
	return &d->connData;
}

KexiDB::Connection* SimpleCommandLineApp::connection() const
{
	return d->conn;
}