Commit 76c0acee authored by Lisa's avatar Lisa

Astyle used over this branch

parent 384b7dc1
......@@ -23,11 +23,11 @@
#include <QTimer>
OperationManager::OperationManager(APM::OperationType type, APM::OperationName operation, QHash<APM::OptionName, AkabeiOption> options, QStringList args, QObject * parent)
: QObject(parent),
m_type(type),
m_operation(operation),
m_options(options),
m_args(args)
: QObject(parent),
m_type(type),
m_operation(operation),
m_options(options),
m_args(args)
{
//QTimer::singleShot(0, AkabeiClient::Backend::instance(), SLOT(initialize()));
//QCoreApplication::instance()->quit();
......@@ -36,7 +36,7 @@ OperationManager::OperationManager(APM::OperationType type, APM::OperationName o
QString root;
QString cache;
QString dbs;
if (options.contains(APM::RootDir)) {
root = options[APM::RootDir].args[0];
}
......@@ -46,7 +46,7 @@ OperationManager::OperationManager(APM::OperationType type, APM::OperationName o
if (options.contains(APM::DBPath)) {
dbs = options[APM::DBPath].args[0];
}
AkabeiClient::Backend::instance()->initialize(root, cache, dbs);
}
......@@ -64,28 +64,28 @@ void OperationManager::statusChanged(Akabei::Backend::Status status)
void OperationManager::start()
{
switch (m_type) {
case APM::QueryOperationType: {
QueryOperation * query = new QueryOperation(this);
query->start(m_operation, m_options, m_args);
break;
}
case APM::RemoveOperationType: {
RemoveOperation * remove = new RemoveOperation(this);
remove->start(m_operation, m_options, m_args);
break;
}
case APM::SyncOperationType: {
SyncOperation * sync = new SyncOperation(this);
sync->start(m_operation, m_options, m_args);
break;
}
case APM::UpgradeOperationType: {
UpgradeOperation * upgrade = new UpgradeOperation(this);
upgrade->start(m_operation, m_options, m_args);
break;
}
default:
break;
case APM::QueryOperationType: {
QueryOperation * query = new QueryOperation(this);
query->start(m_operation, m_options, m_args);
break;
}
case APM::RemoveOperationType: {
RemoveOperation * remove = new RemoveOperation(this);
remove->start(m_operation, m_options, m_args);
break;
}
case APM::SyncOperationType: {
SyncOperation * sync = new SyncOperation(this);
sync->start(m_operation, m_options, m_args);
break;
}
case APM::UpgradeOperationType: {
UpgradeOperation * upgrade = new UpgradeOperation(this);
upgrade->start(m_operation, m_options, m_args);
break;
}
default:
break;
}
}
......
......@@ -19,15 +19,15 @@
class OperationManager : public QObject
{
Q_OBJECT
Q_OBJECT
public:
explicit OperationManager(APM::OperationType type, APM::OperationName operation, QHash<APM::OptionName, AkabeiOption> options, QStringList args, QObject* parent = 0);
virtual ~OperationManager();
private slots:
void statusChanged(Akabei::Backend::Status);
void start();
private:
APM::OperationType m_type;
APM::OperationName m_operation;
......
......@@ -17,14 +17,14 @@
* Build our database of accepted operations and options with the right properties and associations
*/
CmdLine::CmdLine()
: parsed(false)
: parsed(false)
{
typechars.insert("D", APM::DatabaseOperationType);
typechars.insert("Q", APM::QueryOperationType);
typechars.insert("R", APM::RemoveOperationType);
typechars.insert("S", APM::SyncOperationType);
typechars.insert("U", APM::UpgradeOperationType);
/*
* The argument list is only meaningful after reading the command line, so for now it stays empty
*/
......@@ -45,67 +45,67 @@ CmdLine::CmdLine()
AkabeiOption rootdir(APM::RootDir, "", "root", "Set a different root dir", true);
AkabeiOption cachedir(APM::CacheDir, "", "cachedir", "Set a different cache dir", true);
AkabeiOption dbpath(APM::DBPath, "b", "dbpath", "Set a different database dir", false);
acceptedOptions << querypkgfile << showlocal << showless << installasdeps << installasexplicit << skipdependencycheck;
acceptedOptions << force << downloadonly << onlyneeded << ignore << ignoregroup << databaseonly << removeconfig << recursive;
acceptedOptions << rootdir << cachedir << dbpath;
AkabeiOperation database(APM::DatabaseOperationType, APM::DatabaseOperation, "D", "", "Database operation", false);
AkabeiOperation changelog(APM::QueryOperationType, APM::ShowChangelog, "c", "changelog", "Shows changelog of the package", true);
changelog.addOption(querypkgfile);
AkabeiOperation installedasdeps(APM::QueryOperationType, APM::ShowInstalledAsDeps, "d", "deps", "Shows packages which got installed as dependencies", false);
installedasdeps.addOption(querypkgfile);
installedasdeps.addOption(ignore);
installedasdeps.addOption(ignoregroup);
AkabeiOperation installedexp(APM::QueryOperationType, APM::ShowInstalledExplicitely, "e", "explicit", "Shows packages which got installed explicitely", false);
installedexp.addOption(querypkgfile);
installedexp.addOption(ignore);
installedexp.addOption(ignoregroup);
AkabeiOperation checkfiles(APM::QueryOperationType, APM::CheckFiles, "k", "check", "Check if the files of the package are available", true);
checkfiles.addOption(querypkgfile);
checkfiles.addOption(showlocal);
AkabeiOperation showowner(APM::QueryOperationType, APM::ShowOwner, "o", "owns", "Returns the package which contains the file", true);
showowner.addOption(querypkgfile);
showowner.addOption(showlocal);
AkabeiOperation notrequired(APM::QueryOperationType, APM::ShowNotRequired, "t", "unrequired", "Returns packages which are not required by others", false);
notrequired.addOption(querypkgfile);
notrequired.addOption(ignore);
notrequired.addOption(ignoregroup);
AkabeiOperation upgradeable(APM::QueryOperationType, APM::ShowUpgradeable, "u", "upgrades", "Returns upgradeable packages", false);
upgradeable.addOption(querypkgfile);
upgradeable.addOption(ignore);
upgradeable.addOption(ignoregroup);
AkabeiOperation groupsl(APM::QueryOperationType, APM::ShowPackagesOfGroupLocal, "g", "groups", "Shows all packages in a package group", true);
groupsl.addOption(ignore);
AkabeiOperation infol(APM::QueryOperationType, APM::ShowInformationLocal, "i", "info", "Shows package information", true);
infol.addOption(showless);
AkabeiOperation searchl(APM::QueryOperationType, APM::SearchLocal, "s", "search", "Searches repositories for a word", true);
searchl.addOption(ignore);
searchl.addOption(ignoregroup);
AkabeiOperation remove(APM::RemoveOperationType, APM::Remove, "R", "remove", "Remove packages", true);
remove.addOption(skipdependencycheck);
remove.addOption(force);
remove.addOption(databaseonly);
remove.addOption(removeconfig);
remove.addOption(recursive);
AkabeiOperation cascade(APM::RemoveOperationType, APM::Cascade, "c", "cascade", "Remove packages with their dependencies", true);
AkabeiOperation unneeded(APM::RemoveOperationType, APM::Unneeded, "u", "unneeded", "Remove not needed packages", false);
unneeded.addOption(ignore);
unneeded.addOption(ignoregroup);
AkabeiOperation install(APM::SyncOperationType, APM::Install, "S", "install", "Install packages", true);
install.addOption(installasdeps);
install.addOption(installasexplicit);
......@@ -115,65 +115,65 @@ CmdLine::CmdLine()
install.addOption(onlyneeded);
AkabeiOperation removeold(APM::SyncOperationType, APM::RemoveOldFromCache, "c", "clean", "Remove old packages from cache", false);
AkabeiOperation removeallcache(APM::SyncOperationType, APM::RemoveAllCache, "cc", "", "Remove all packages from cache", false);
AkabeiOperation groups(APM::SyncOperationType, APM::ShowPackagesOfGroup, "g", "groups", "Shows all packages in a package group", true);
groups.addOption(ignore);
AkabeiOperation info(APM::SyncOperationType, APM::ShowInformation, "i", "info", "Shows package information", true);
info.addOption(showless);
AkabeiOperation search(APM::SyncOperationType, APM::Search, "s", "search", "Searches repositories for a word", true);
search.addOption(ignore);
search.addOption(ignoregroup);
AkabeiOperation listrepo(APM::SyncOperationType, APM::ListRepo, "l", "list", "List all packages in a repository", true);
listrepo.addOption(querypkgfile);
listrepo.addOption(showlocal);
listrepo.addOption(ignore);
listrepo.addOption(ignoregroup);
AkabeiOperation updatedb(APM::SyncOperationType, APM::UpdateDatabases, "y", "refresh", "Refresh database", false);
AkabeiOperation updatesys(APM::SyncOperationType, APM::UpdateSystem, "u", "sysupgrade", "Update system", false);
updatesys.addOption(downloadonly);
updatesys.addOption(ignore);
updatesys.addOption(ignoregroup);
AkabeiOperation updatedbandsys(APM::SyncOperationType, APM::UpdateDatabaseAndSystem, "", "", "Refresh database and update system", false);
updatedbandsys.addOption(downloadonly);
updatedbandsys.addOption(ignore);
updatedbandsys.addOption(ignoregroup);
AkabeiOperation upgrade(APM::UpgradeOperationType, APM::Upgrade, "U", "", "Upgrade operation", true);
upgrade.addOption(downloadonly);
upgrade.addOption(ignore);
upgrade.addOption(ignoregroup);
AkabeiOperation help(APM::NoType, APM::Help, "h", "help", "Shows the help message", false);
AkabeiOperation version(APM::NoType, APM::Version, "v", "version", "Shows Akabei version", false);
QList<AkabeiOperation *> tmp;
tmp << &removeallcache << &help << &version << &install << &remove << &database << &changelog << &installedasdeps << &installedexp;
tmp << &checkfiles << &showowner << &notrequired << &upgradeable << &cascade;
tmp << &removeold << &groups << &info << &listrepo << &groupsl << &searchl << &infol << &unneeded;
tmp << &search << &updatedb << &updatesys << &updatedbandsys << &upgrade;
/*
* It has to be pointers otherwise the changes in this foreach
* aren't reflected in specialOperations below
*/
foreach (AkabeiOperation *op, tmp) {
foreach(AkabeiOperation * op, tmp) {
/* These options are associated to every operation */
op->addOption(rootdir);
op->addOption(cachedir);
op->addOption(dbpath);
acceptedOperations.insertMulti(op->type, *op);
acceptedOperations.insertMulti(op->type, *op);
}
/* These are the only operation we need to access directly later, so better speed up searches a bit */
specialOperations << removeallcache << help << version;
}
......@@ -229,7 +229,7 @@ void AkabeiOperation::set(AkabeiOperation& other)
if ((this->type == APM::SyncOperationType && other.name == APM::Install) || (this->type == APM::RemoveOperationType && other.name == APM::Remove)) {
return;
}
if (this->name == APM::None || this->name == APM::Remove || this->name == APM::Install || (this->commandShort == "c" && other.name == APM::RemoveAllCache)) {
this->name = other.name;
this->type = other.type;
......@@ -278,20 +278,20 @@ void CmdLine::parse(int argc, char** argv)
KAboutData::License_GPL,
ki18n("(C) 2011 Lukas Appelhans"),
ki18n("<a href=\"mailto:boom1992@chakra-project.org\">boom1992@chakra-project.org</a>"));
aboutData.addAuthor(ki18n("Lukas Appelhans"), ki18n("Maintainer"), "boom1992@chakra-project.org");
KCmdLineArgs::init(argc, argv, &aboutData);
KCmdLineOptions koptions;
/*
* Adds the only type which is not an operation
*/
koptions.add("Q", ki18n("Query operation"));
/*
* Adds all the operations
*/
foreach (const AkabeiOperation& op, acceptedOperations.values()) {
foreach(const AkabeiOperation & op, acceptedOperations.values()) {
if (!op.commandShort.isEmpty()) {
if (!op.commandLong.isEmpty()) {
koptions.add(op.commandLong);
......@@ -299,12 +299,12 @@ void CmdLine::parse(int argc, char** argv)
koptions.add(op.commandShort, op.description);
}
}
/*
* Adds all the option with <argument> if they require one
* TODO: make <argument> more explicative of its nature (package, group, filename, ...)
*/
foreach (const AkabeiOption& opt, acceptedOptions) {
foreach(const AkabeiOption & opt, acceptedOptions) {
QByteArray newcomlong = opt.commandLong;
if (opt.hasArg) {
newcomlong += " <argument>";
......@@ -314,22 +314,22 @@ void CmdLine::parse(int argc, char** argv)
}
koptions.add(newcomlong, opt.description);
}
/*
* Free arguments at the end
*/
koptions.add("+[arg1]", ki18n("An optional argument 'arg1'"));
/*
* Parsing!
*/
KCmdLineArgs::addCmdLineOptions(koptions);
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
APM::OperationType type = APM::NoType;
m_operation.name = APM::None;
QList<QByteArray> found;
/*
* These are considered separately and when found, they surpass any other operation that may be present.
*/
......@@ -339,24 +339,23 @@ void CmdLine::parse(int argc, char** argv)
} else if (args->isSet("v")) {
AkabeiOperation version = specialOperations[2];
m_operation.set(version);
}
else {
} else {
for (QHash<QByteArray, APM::OperationType>::iterator it = typechars.begin(); it != typechars.end(); it++) {
if (args->isSet(it.key())) {
if (type != APM::NoType) {
throw CmdLineException("multiple types present.");
}
type = it.value();
if (type == APM::QueryOperationType) {
found << "Q";
}
/*
* We look only for the acceptable operations
* all the others, if present, will be spotted later
*/
foreach (AkabeiOperation op, acceptedOperations.values(type)) {
foreach(AkabeiOperation op, acceptedOperations.values(type)) {
/*
* This eliminates the only "dummy" operation with no commands at all
* every other operation has got at least the short command
......@@ -364,7 +363,7 @@ void CmdLine::parse(int argc, char** argv)
if (op.commandShort.isEmpty()) {
continue;
}
/*
* Saves up the commands for later inspection
*/
......@@ -376,7 +375,7 @@ void CmdLine::parse(int argc, char** argv)
}
}
}
/*
* This kind of command is unfortunately completely ignored (-S -cc is fine)
* so we have to check it separately. Again, malformed versions will be spotted later
......@@ -389,35 +388,35 @@ void CmdLine::parse(int argc, char** argv)
}
}
}
if (m_operation.name == APM::None) {
throw CmdLineException("no operation specified.");
}
foreach (AkabeiOption opt, acceptedOptions) {
foreach(AkabeiOption opt, acceptedOptions) {
QByteArray command = (opt.commandShort.isEmpty()) ? opt.commandLong : opt.commandShort;
bool condition = !found.contains(command); /* if found contains the command, we already decided it's an operation */
/* This is a special case for cause of the "nosave" option */
if (opt.name == APM::RemoveConfig) {
condition &= !args->isSet("save");
} else {
condition &= args->isSet(command); /* normal check */
}
if (condition) {
if (condition) {
if (!m_operation.isOptionSupported(opt.name)) {
throw CmdLineException("invalid option for this operation");
}
if (!opt.commandShort.isEmpty()) {
found << opt.commandShort;
}
if (!opt.commandLong.isEmpty()) {
found << opt.commandLong;
}
/*
* Gets arguments associated with the option
*/
......@@ -431,7 +430,7 @@ void CmdLine::parse(int argc, char** argv)
QString message = "the option \"" + command + "\" must not have an argument.";
throw CmdLineException(message.toUtf8().data());
}
/*
* In this case, we're sure :)
*/
......@@ -439,21 +438,21 @@ void CmdLine::parse(int argc, char** argv)
QString message = "missing argument for the option \"" + command + "\"";
throw CmdLineException(message.toUtf8().data());
}
opt.args = optargs;
m_options.insert(opt.name, opt);
}
}
secondParse(KCmdLineArgs::allArguments(), found);
/*
* Gets all the other arguments around
*/
for (int i = 0; i < args->count(); i++) {
m_freeArgs << args->arg(i);
}
if (!m_freeArgs.isEmpty() && !m_operation.hasFreeArgs) {
throw CmdLineException("unexpected free arguments.");
}
......@@ -461,7 +460,7 @@ void CmdLine::parse(int argc, char** argv)
if (m_freeArgs.isEmpty() && m_operation.hasFreeArgs) {
throw CmdLineException("free arguments required but not present.");
}
args->clear(); /* frees up memory */
parsed = true;
}
......@@ -475,31 +474,31 @@ void CmdLine::parse(int argc, char** argv)
void CmdLine::secondParse(QStringList args, QList<QByteArray>& foundCmds)
{
QList<QByteArray> cmdline;
foreach (QString part, args) {
foreach(QString part, args) {
if (part.size() < 2) {
continue;
}
/* Long command */
if (part.at(0) == '-' && part.at(1) == '-') {
part.remove(0, 2);
int sep = part.indexOf("=");
if (sep != -1) {
part.truncate(sep);
}
cmdline << part.toUtf8();
/* Short command: takes each letter separately */
/* Short command: takes each letter separately */
} else if (part.at(0) == '-') {
part.remove(0, 1);
int sep = part.indexOf("=");
if (sep != -1) {
part.truncate(sep);
}
/* The only short commands with two letters is treated separately to avoid problems */
if (part.contains("cc")) {
if (part.count("cc") > 1) {
......@@ -509,25 +508,25 @@ void CmdLine::secondParse(QStringList args, QList<QByteArray>& foundCmds)
cmdline << "cc";
}
}
for (int ch = 0; ch < part.length(); ch++) {
QString tmp(part[ch]);
cmdline << tmp.toUtf8();
}
}
}
/*
* If the command line contains something else other than what we have already parsed
* there's something wrong around
*/
foreach (const QByteArray& com, cmdline) {
foreach(const QByteArray & com, cmdline) {
if (!foundCmds.contains(com)) {
QString msg = "out of context operation: " + com;
throw CmdLineException(msg.toUtf8().data());
}
}
/*
* Counts the occurrence of the operation
*/
......@@ -535,17 +534,17 @@ void CmdLine::secondParse(QStringList args, QList<QByteArray>& foundCmds)
if (occurrences > 1) {
throw CmdLineException("can't accept linked operations.");
}
/*
* Counts the occurrence of all the options we previously found
*/
foreach (const AkabeiOption& opt, m_options.values()) {
foreach(const AkabeiOption & opt, m_options.values()) {
/* The only options allowed to be repeated several times */
if (opt.name == APM::Ignore || opt.name == APM::IgnoreGroup) {
continue;
}
occurrences = cmdline.count(opt.commandLong) + cmdline.count(opt.commandShort);
if (occurrences > 1) {
throw CmdLineException("can't repeat this option twice.");
......@@ -567,7 +566,7 @@ APM::OperationName CmdLine::operation()
if (!parsed) {
throw CmdLineException("Command line not parsed.");
}
return m_operation.name;
}
......@@ -576,7 +575,7 @@ QStringList CmdLine::freeArgs()
if (!parsed) {
throw CmdLineException("Command line not parsed.");
}
return m_freeArgs;
}
......@@ -585,6 +584,6 @@ QHash< APM::OptionName, AkabeiOption > CmdLine::options()
if (!parsed) {
throw CmdLineException("Command line not parsed.");
}
return m_options;
}
......@@ -39,7 +39,7 @@ enum OperationName {
None,
ShowChangelog, ShowInstalledAsDeps, ShowInstalledExplicitely, ShowPackagesOfGroup, ShowPackagesOfGroupLocal, ShowInformationLocal,
ShowInformation, CheckFiles, ShowOwner, ShowNotRequired, ShowUpgradeable,
Install, RemoveOldFromCache, RemoveAllCache, ListRepo, Search, UpdateDatabases, UpdateSystem, UpdateDatabaseAndSystem,
Install, RemoveOldFromCache, RemoveAllCache, ListRepo, Search, UpdateDatabases, UpdateSystem, UpdateDatabaseAndSystem,
Remove, Cascade, Unneeded, SearchLocal,
DatabaseOperation, Upgrade, Help, Version
};
......@@ -53,19 +53,19 @@ typedef enum OperationName OperationName;
class AkabeiOption
{
private:
void init(APM::OptionName, const char *, const char *, const char *, bool, QStringList);
void init(APM::OptionName, const char *, const char *, const char *, bool, QStringList);
public:
APM::OptionName name;
QByteArray commandShort, commandLong;
KLocalizedString description;
bool hasArg;
/*
* Args are required by some options like --ignore or --ignoregroup
*/
QStringList args;
AkabeiOption(APM::OptionName, const char *, const char *, const char *, bool);
AkabeiOption(APM::OptionName, const char *, const char *, const char *, bool, QStringList);
AkabeiOption() {}
......@@ -80,7 +80,7 @@ class AkabeiOperation
{
private:
void init(APM::OperationType, APM::OperationName, const char *, const char *, const char *, bool, QStringList);
public:
APM::OperationType type;
APM::OperationName name;
......@@ -88,22 +88,22 @@ public:
KLocalizedString description;
QHash<APM::OptionName, AkabeiOption> options; /* options that can be associated to this operation */
bool hasFreeArgs;
/*
* An example of free args is the list of packages following an install command
*/
QStringList freeArgs;
AkabeiOperation();
AkabeiOperation(APM::OperationType, APM::OperationName, const char *, const char *, const char *, bool);
AkabeiOperation(APM::OperationType, APM::OperationName, const char *, const char *, const char *, bool, QStringList);
/*
* Checks, with special rules, than no more than one operation is specified on the command line
* see cmdline.cpp for a more detailed explanation
*/
void set(AkabeiOperation &);
/*
* Options checking
*/
......@@ -119,23 +119,23 @@ private:
AkabeiOperation m_operation;
QHash<APM::OptionName, AkabeiOption> m_options;
QStringList m_freeArgs;
/* Database */
QMultiHash<APM::OperationType, AkabeiOperation> acceptedOperations;
AkabeiOptionList acceptedOptions;
QHash<QByteArray, APM::OperationType> typechars;
/* This is for the few operations that we need to access quickly later */
AkabeiOperationList specialOperations;
bool parsed;
void secondParse(QStringList, QList<QByteArray> &);
public:
CmdLine();
void parse(int argc, char **argv);
APM::OperationType type();
APM::OperationName operation();
QHash<APM::OptionName, AkabeiOption> options();
......
......@@ -14,12 +14,13 @@
#include <kdebug.h>
#include "apm.h"
#include "cmdline.h"
#include <akabeilog.h>
int main(int argc, char** argv)
{
{
QCoreApplication app(argc, argv);
CmdLine cmd;
try {
cmd.parse(argc, argv);
} catch (CmdLineException& ex) {
......@@ -27,7 +28,8 @@ int main(int argc, char** argv)
err << "Fatal error parsing command line: " << ex.what() << endl;
return -1;
}
Akabei::logInit("akabei.log");
OperationManager apm(cmd.type(), cmd.operation(), cmd.options(), cmd.freeArgs());
return app.exec();
}