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
|
/*
* commandline.cpp
*
* (c) 2003,2008-2011 by Jeremy Bowman <jmbowman@alum.mit.edu>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*/
/** @file commandline.cpp
* Source file for CommandLine
*/
#include <QApplication>
#include <QFile>
#include <QFileInfo>
#include <QObject>
#include <QStringList>
#include "commandline.h"
#include "database.h"
#include "importutils.h"
#include "view.h"
#include "encryption/crypto.h"
/**
* Constructor.
*/
CommandLine::CommandLine()
{
}
/**
* Parse the command line arguments and perform the requested action.
*/
int CommandLine::process()
{
QStringList args = qApp->arguments();
QStringList importCommands;
QStringList exportCommands;
importCommands << "fromxml" << "fromcsv" << "frommobiledb";
exportCommands << "toxml" << "tocsv" << "tohtml";
if (importCommands.contains(args[1])) {
return fromOtherFormat(args);
}
else if (exportCommands.contains(args[1])) {
return toOtherFormat(args);
}
else if (args[1] == "-h" || args[1] == "--help") {
printUsage();
return 0;
}
else {
printUsage();
return 1;
}
}
/**
* Process a command line instruction to import data from another file format.
*
* @param args The command line arguments
* @return The value to return from program execution
*/
int CommandLine::fromOtherFormat(const QStringList &args)
{
int argc = args.count();
int numArgs = 4;
// Check for a password
int passIndex = args.indexOf("-p");
if (passIndex != -1) {
if (argc < passIndex + 4) {
printUsage();
return 1;
}
numArgs += 2;
}
// Check for an encoding specification
int encIndex = args.indexOf("-e");
if (encIndex != -1) {
if (argc < encIndex + 4) {
printUsage();
return 1;
}
numArgs += 2;
}
if (argc != numArgs) {
printUsage();
return 1;
}
QString sourceFile(args[numArgs - 2]);
QString pbFile(args[numArgs - 1]);
bool fromcsv = (args[1] == "fromcsv");
if (fromcsv) {
if (!QFile::exists(pbFile)) {
printf("Named PortaBase file doesn't exist\n");
return 1;
}
QFileInfo info(pbFile);
if (!info.isWritable()) {
printf("Named PortaBase file can't be written to");
return 1;
}
}
Database::OpenResult openResult;
bool encrypt = passIndex != -1 && !fromcsv;
Database *db = new Database(pbFile, &openResult, encrypt);
if (openResult == Database::NewerVersion || openResult == Database::Failure) {
if (fromcsv) {
printf("Unable to open PortaBase file\n");
}
else {
printf("Unable to create/initialize file\n");
}
return 1;
}
else if (openResult == Database::Encrypted || (passIndex != -1 && !fromcsv)) {
QString error = db->encryption()->setPassword(args[passIndex + 1], !fromcsv);
if (!error.isEmpty()) {
printf("%s", error.toLocal8Bit().data());
printf("\n");
return 1;
}
error = db->load();
if (!error.isEmpty()) {
printf("%s", error.toLocal8Bit().data());
printf("\n");
return 1;
}
}
else {
db->load();
}
QString error = "";
QString data = "";
ImportUtils utils;
if (args[1] == "fromxml") {
error = utils.importXML(sourceFile, db);
}
else if (fromcsv) {
QString encoding = "UTF-8";
if (encIndex != -1) {
encoding = args[encIndex + 1];
}
QStringList result = db->importFromCSV(sourceFile, encoding);
int count = result.count();
if (count > 0) {
error = result[0];
}
if (count > 1) {
data = result[1];
}
}
else {
error = utils.importMobileDB(sourceFile, db);
}
if (!error.isEmpty()) {
printf("%s", error.toLocal8Bit().data());
printf("\n");
if (!data.isEmpty()) {
printf("%s", (QObject::tr("Problematic row") + ":\n").toLocal8Bit().data());
printf("%s", data.toLocal8Bit().data());
printf("\n");
}
if (!fromcsv) {
QFile::remove(pbFile);
}
delete db;
return 1;
}
db->commit();
delete db;
return 0;
}
/**
* Process a command line instruction to export data to another file format.
*
* @param args The command line arguments
* @return The value to return from program execution
*/
int CommandLine::toOtherFormat(const QStringList &args)
{
int argc = args.count();
int numArgs = 4;
// Check for a password
int passIndex = args.indexOf("-p");
if (passIndex != -1) {
if (argc < passIndex + 4) {
printUsage();
return 1;
}
numArgs += 2;
}
// Check for a view
int viewIndex = args.indexOf("-v");
if (viewIndex != -1) {
if (argc < viewIndex + 4) {
printUsage();
return 1;
}
numArgs += 2;
}
// Check for a sorting
int sortIndex = args.indexOf("-s");
if (sortIndex != -1) {
if (argc < sortIndex + 4) {
printUsage();
return 1;
}
numArgs += 2;
}
// Check for a filter
int filterIndex = args.indexOf("-f");
if (filterIndex != -1) {
numArgs += 2;
if (argc < filterIndex + 4) {
printUsage();
return 1;
}
}
if (argc != numArgs) {
printUsage();
return 1;
}
QString pbFile(args[numArgs - 2]);
QString outputFile(args[numArgs - 1]);
Database::OpenResult openResult;
Database *db = new Database(pbFile, &openResult);
if (openResult == Database::NewerVersion || openResult == Database::Failure) {
printf("Error opening file: %s\n", args[numArgs - 2].toLocal8Bit().data());
return 1;
}
else if (openResult == Database::Encrypted) {
if (passIndex == -1) {
printf("Encrypted file, must provide password\n");
return 1;
}
QString error = db->encryption()->setPassword(args[passIndex + 1], false);
if (!error.isEmpty()) {
printf("%s", error.toLocal8Bit().data());
printf("\n");
return 1;
}
error = db->load();
if (!error.isEmpty()) {
printf("%s", error.toLocal8Bit().data());
printf("\n");
return 1;
}
}
else {
db->load();
}
QString viewName = db->currentView();
QString sortName = db->currentSorting();
QString filterName = db->currentFilter();
if (viewIndex != -1) {
viewName = args[viewIndex + 1];
if (!db->listViews().contains(viewName)) {
printf("Unknown view: %s\n", viewName.toLocal8Bit().data());
return 1;
}
}
if (sortIndex != -1) {
sortName = args[sortIndex + 1];
if (!db->listSortings().contains(sortName)) {
printf("Unknown sorting: %s\n", sortName.toLocal8Bit().data());
return 1;
}
}
if (filterIndex != -1) {
filterName = args[filterIndex + 1];
if (!db->listFilters().contains(filterName)) {
printf("Unknown filter: %s\n", filterName.toLocal8Bit().data());
return 1;
}
}
db->setGlobalInfo(viewName, sortName, filterName);
View *view = db->getView(viewName);
if (args[1] == "toxml") {
view->exportToXML(outputFile);
}
else if (args[1] == "tohtml") {
view->exportToHTML(outputFile);
}
else {
view->prepareData();
view->exportToCSV(outputFile);
}
// view is deleted by Database destructor
delete db;
return 0;
}
/**
* Print usage instructions to the console.
*/
void CommandLine::printUsage()
{
printf("Usage: portabase [-h | --help | file]\n");
printf(" portabase command [-p password] [options] fromfile tofile\n");
printf(" where command is fromxml, toxml, fromcsv, tocsv, tohtml, or frommobiledb\n");
printf(" Valid options for toxml, tocsv, and tohtml are:\n");
printf(" -v viewname (apply this view before exporting)\n");
printf(" -s sortname (apply this sorting before exporting)\n");
printf(" -f filtername (apply this filter before exporting)\n");
printf(" There is one option for fromcsv:\n");
printf(" -e encoding (UTF-8 or Latin-1; default is UTF-8)\n");
printf(" When using fromcsv, \"tofile\" must be an existing PortaBase file.\n");
printf(" Specify -h or --help to receive this message\n");
}
|