kexi

simplecommandlineapp.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "simplecommandlineapp.h"
00021 
00022 #include <qfileinfo.h>
00023 
00024 #include <kcmdlineargs.h>
00025 #include <kdebug.h>
00026 
00027 #include <kexidb/connectiondata.h>
00028 #include <kexidb/drivermanager.h>
00029 
00030 using namespace KexiDB;
00031 
00032 static KCmdLineOptions predefinedOptions[] =
00033 {
00034     { "drv", 0, 0 },
00035     { "driver <name>", I18N_NOOP("Database driver name"), 0 },
00036     { "u", 0, 0 },
00037     { "user <name>", I18N_NOOP("Database user name"), 0 },
00038     { "p", 0, 0 },
00039     { "password", I18N_NOOP("Prompt for password"), 0 },
00040     { "h", 0, 0 },
00041     { "host <name>", I18N_NOOP("Host (server) name"), 0 },
00042     { "port <number>", I18N_NOOP("Server's port number"), 0 },
00043     { "s", 0, 0 },
00044     { "local-socket <filename>", I18N_NOOP("Server's local socket filename"), 0 },
00045     KCmdLineLastOption
00046 };
00047 
00048 //-----------------------------------------
00049 
00051 class SimpleCommandLineApp::Private
00052 {
00053 public:
00054     Private()
00055      : conn(0)
00056     {}
00057     ~Private()
00058     {
00059         if (conn) {
00060             conn->disconnect();
00061             delete (Connection*)conn;
00062         }
00063         delete instance;
00064 
00065         for (KCmdLineOptions *optionsPtr = allOptions; optionsPtr->name; optionsPtr++) {
00066             delete optionsPtr->name;
00067             delete optionsPtr->description;
00068             delete optionsPtr->def;
00069         }
00070         delete allOptions;
00071     }
00072 
00073     KexiDB::DriverManager manager;
00074     KCmdLineOptions *allOptions;
00075     KInstance* instance;
00076     ConnectionData connData;
00077     QGuardedPtr<Connection> conn;
00078 };
00079 
00080 //-----------------------------------------
00081 
00082 SimpleCommandLineApp::SimpleCommandLineApp(
00083     int argc, char** argv, KCmdLineOptions *options,
00084     const char *programName, const char *version, 
00085     const char *shortDescription, int licenseType, 
00086     const char *copyrightStatement, const char *text, 
00087     const char *homePageAddress, const char *bugsEmailAddress)
00088  : Object()
00089  , d( new Private() )
00090 {
00091     QFileInfo fi(argv[0]);
00092     QCString appName( fi.baseName().latin1() );
00093     KCmdLineArgs::init(argc, argv, 
00094         new KAboutData( appName, programName,
00095             version, shortDescription, licenseType, copyrightStatement, text, 
00096             homePageAddress, bugsEmailAddress));
00097 
00098     int predefinedOptionsCount = 0;
00099     for (KCmdLineOptions *optionsPtr = predefinedOptions; optionsPtr->name; optionsPtr++, predefinedOptionsCount++)
00100         ;
00101     int userOptionsCount = 0;
00102     for (KCmdLineOptions *optionsPtr = options; optionsPtr->name; optionsPtr++, userOptionsCount++)
00103         ;
00104 
00105     d->instance = new KInstance(appName);
00106 
00107     // join the predefined options and user options
00108     d->allOptions = new KCmdLineOptions[predefinedOptionsCount + userOptionsCount + 1];
00109     KCmdLineOptions *allOptionsPtr = d->allOptions;
00110     for (KCmdLineOptions *optionsPtr = predefinedOptions; optionsPtr->name; optionsPtr++, allOptionsPtr++) {
00111         allOptionsPtr->name = qstrdup(optionsPtr->name);
00112         allOptionsPtr->description = qstrdup(optionsPtr->description);
00113         if (optionsPtr == predefinedOptions) //first row == drv
00114             allOptionsPtr->def = qstrdup(KexiDB::Driver::defaultFileBasedDriverName().latin1());
00115         else
00116             allOptionsPtr->def = qstrdup(optionsPtr->def);
00117     }
00118     for (KCmdLineOptions *optionsPtr = options; optionsPtr->name; optionsPtr++, allOptionsPtr++) {
00119         allOptionsPtr->name = qstrdup(optionsPtr->name);
00120         allOptionsPtr->description = qstrdup(optionsPtr->description);
00121         allOptionsPtr->def = qstrdup(optionsPtr->def);
00122     }
00123     allOptionsPtr->name = 0; //end
00124     allOptionsPtr->description = 0;
00125     allOptionsPtr->def = 0;
00126     KCmdLineArgs::addCmdLineOptions( d->allOptions );
00127 
00128     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00129 
00130     d->connData.driverName = args->getOption("driver");
00131     d->connData.userName = args->getOption("user");
00132     d->connData.hostName = args->getOption("host");
00133     d->connData.localSocketFileName = args->getOption("local-socket");
00134     d->connData.port = args->getOption("port").toInt();
00135     d->connData.useLocalSocketFile = args->isSet("local-socket");
00136 
00137     if (args->isSet("password")) {
00138         QString userAtHost = d->connData.userName;
00139         if (!d->connData.userName.isEmpty())
00140             userAtHost += "@";
00141         userAtHost += (d->connData.hostName.isEmpty() ? "localhost" : d->connData.hostName);
00142         QTextStream cout(stdout,IO_WriteOnly);
00143         cout << i18n("Enter password for %1: ").arg(userAtHost);
00145         QTextStream cin(stdin,IO_ReadOnly);
00146         cin >> d->connData.password;
00147         KexiDBDbg << d->connData.password << endl;
00148     }
00149 }
00150 
00151 SimpleCommandLineApp::~SimpleCommandLineApp()
00152 {
00153     closeDatabase();
00154     delete d;
00155 }
00156 
00157 bool SimpleCommandLineApp::openDatabase(const QString& databaseName)
00158 {
00159     if (!d->conn) {
00160         if (d->manager.error()) {
00161             setError(&d->manager);
00162             return false;
00163         }
00164 
00165         //get the driver
00166         KexiDB::Driver *driver = d->manager.driver(d->connData.driverName);
00167         if (!driver || d->manager.error()) {
00168             setError(&d->manager);
00169             return false;
00170         }
00171 
00172         if (driver->isFileDriver())
00173             d->connData.setFileName( databaseName );
00174 
00175         d->conn = driver->createConnection(d->connData);
00176         if (!d->conn || driver->error()) {
00177             setError(driver);
00178             return false;
00179         }
00180     }
00181     if (d->conn->isConnected()) {
00182         // db already opened
00183         if (d->conn->isDatabaseUsed() && d->conn->currentDatabase()==databaseName) //the same: do nothing
00184             return true;
00185         if (!closeDatabase()) // differs: close the first
00186             return false;
00187     }
00188     if (!d->conn->connect()) {
00189         setError(d->conn);
00190         delete d->conn;
00191         d->conn = 0;
00192         return false;
00193     }
00194 
00195     if (!d->conn->useDatabase( databaseName )) {
00196         setError(d->conn);
00197         delete d->conn;
00198         d->conn = 0;
00199         return false;
00200     }
00201     return true;
00202 }
00203 
00204 bool SimpleCommandLineApp::closeDatabase()
00205 {
00206     if (!d->conn)
00207         return true;
00208     if (!d->conn->disconnect()) {
00209         setError(d->conn);
00210         return false;
00211     }
00212     return true;
00213 }
00214 
00215 KInstance* SimpleCommandLineApp::instance() const
00216 {
00217     return d->instance;
00218 }
00219 
00220 KexiDB::ConnectionData* SimpleCommandLineApp::connectionData() const
00221 {
00222     return &d->connData;
00223 }
00224 
00225 KexiDB::Connection* SimpleCommandLineApp::connection() const
00226 {
00227     return d->conn;
00228 }
KDE Home | KDE Accessibility Home | Description of Access Keys