summaryrefslogtreecommitdiff
path: root/resourceqt-client/commandlineparser.cpp
diff options
context:
space:
mode:
authorWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2010-08-23 12:15:15 +0300
committerWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2010-09-06 14:36:28 +0300
commit0bcbc969c7dfa362cfffad3f92d375329fee289f (patch)
tree7f1897a8e359091b112cacaea0c8fd4fbf3a4c5f /resourceqt-client/commandlineparser.cpp
parenta83adabf1dd858d3dab0cc141fc51fdcb9388d44 (diff)
downloadlibresourceqt-0bcbc969c7dfa362cfffad3f92d375329fee289f.tar.gz
* Moved CommandLineParser to its own files
* Removed broken timer, replaced it with a simpler timer made in C * Sanitized the use of resources (Use Qt types instead of custom bitmap) * Added a MessageHandler so we can later play with verbosity level
Diffstat (limited to 'resourceqt-client/commandlineparser.cpp')
-rw-r--r--resourceqt-client/commandlineparser.cpp223
1 files changed, 223 insertions, 0 deletions
diff --git a/resourceqt-client/commandlineparser.cpp b/resourceqt-client/commandlineparser.cpp
new file mode 100644
index 0000000..86e5d53
--- /dev/null
+++ b/resourceqt-client/commandlineparser.cpp
@@ -0,0 +1,223 @@
+#include "commandlineparser.h"
+#include <QDebug>
+
+QHash<QString, ResourcePolicy::ResourceType> CommandLineParser::resourceValues;
+
+CommandLineParser::CommandLineParser():
+ allResources(), optResources(), autoRelease(false), alwaysReply(false),
+ verbose(false), allowUnkownResourceClass(false), output(stdout)
+{
+ resourceValues["AudioPlayback"] = ResourcePolicy::AudioPlaybackType;
+ resourceValues["VideoPlayback"] = ResourcePolicy::VideoPlaybackType;
+ resourceValues["AudioRecording"] = ResourcePolicy::AudioRecorderType;
+ resourceValues["VideoRecording"] = ResourcePolicy::VideoRecorderType;
+ resourceValues["Vibra"] = ResourcePolicy::VibraType;
+ resourceValues["Leds"] = ResourcePolicy::LedsType;
+ resourceValues["Backlight"] = ResourcePolicy::BacklightType;
+ resourceValues["SystemButton"] = ResourcePolicy::SystemButtonType;
+ resourceValues["LockButton"] = ResourcePolicy::LockButtonType;
+ resourceValues["ScaleButton"] = ResourcePolicy::ScaleButtonType;
+ resourceValues["SnapButton"] = ResourcePolicy::SnapButtonType;
+ resourceValues["LensCover"] = ResourcePolicy::LensCoverType;
+ resourceValues["HeadsetButtons"] = ResourcePolicy::HeadsetButtonsType;
+}
+
+CommandLineParser::~CommandLineParser()
+{
+}
+
+bool CommandLineParser::parseArguments()
+{
+ QStringList args = QCoreApplication::arguments();
+ QStringList::const_iterator ci = args.constBegin();
+ // skip the first arg, which is program name
+ ++ci;
+ while (ci != args.constEnd()) {
+ if ((*ci).at(0) == QChar('-')) {
+ switch ((*ci).at(1).toAscii()) {
+ case 'd':
+ case 'm':
+ case 's':
+ case 't':
+ output << "The switch '" << (*ci).at(1) << "' is only present for compatibility reasons." << endl;
+ break;
+ case 'f':
+ if (!parseModeValues(*(++ci))) {
+ return false;
+ }
+ break;
+ case 'o':
+ if (!parseResourceList(*(++ci), optResources)) {
+ output << "Failed to parse resources: " << *ci << endl;
+ return false;
+ }
+ break;
+ case 'u':
+ allowUnkownResourceClass = true;
+ break;
+ case 'h':
+ default:
+ usage();
+ return false;
+ }
+ }
+ else {
+ //assume there are no more args
+ break;
+ }
+ ++ci;
+ }
+
+ if (ci == args.constEnd()) {
+ usage();
+ return false;
+ }
+ else {
+ if (!parseClassString(*ci)) {
+ return false;
+ }
+ ++ci;
+ if (ci != args.constEnd()) {
+ if (!parseResourceList(*ci, allResources)) {
+ output << "Error while parsing resource list" << endl;
+ }
+ }
+ }
+
+ if (!allResources.contains(optResources)) {
+ output << "optional resources are not subset of all resources" << endl;
+ return false;
+ }
+ return true;
+}
+
+bool CommandLineParser::parseClassString(const QString &str)
+{
+ if (!allowUnkownResourceClass &&
+ (str != "call") &&
+ (str != "camera") &&
+ (str != "ringtone") &&
+ (str != "alarm") &&
+ (str != "navigator") &&
+ (str != "game") &&
+ (str != "player") &&
+ (str != "event") &&
+ (str != "background") &&
+ (str != "videoeditor") )
+ {
+ output << "invalid class " << str;
+ return false;
+ }
+
+ applicationClass = str;
+ return true;
+}
+
+bool CommandLineParser::parseResourceList(const QString &resourceListStr,
+ QSet<ResourcePolicy::ResourceType> &resources)
+{
+ if (resourceListStr.isEmpty() || resourceListStr.isNull()) {
+ return false;
+ }
+ else {
+ QStringList resList = resourceListStr.split(",", QString::SkipEmptyParts);
+
+ foreach(QString res, resList) {
+ if (resourceValues.contains(res)) {
+ resources.insert(resourceValues[res]);
+ }
+ }
+ }
+
+ return true;
+}
+
+bool CommandLineParser::parseModeValues(const QString &modeListStr)
+{
+ if (modeListStr.isEmpty() || modeListStr.isNull()) {
+ return false;
+ }
+
+ QStringList modeList = modeListStr.split(",", QString::SkipEmptyParts);
+
+ foreach(QString mode, modeList) {
+ if (mode == "AutoRelease") {
+ autoRelease = true;
+ }
+ else if (mode == "AlwaysReply") {
+ alwaysReply = true;
+ }
+ else {
+ output << "Ignoring unknown mode '" << mode << "'!" << endl;
+ }
+ }
+ return true;
+}
+
+void CommandLineParser::usage()
+{
+ output << "usage: resourceqt-client [-h] [-m mode-values]" <<
+ "[-o optional-resources] [-s shared-resources -m shared-mask] " <<
+ "class all-resources" << endl;
+ output << "\toptions:" << endl;
+ output << "\t h\tprint this help message and exit" << endl;
+ output << "\t f\tmode values. See 'modes' below for the "
+ "\n\t\tsyntax of <mode-values>" << endl;
+ output << "\t o\toptional resources. See 'resources' below for the "
+ "syntax of\n\t\t<optional-resources>" << endl;
+ output << "\tclass:" << endl;
+ output << "\t\tcall\t - for native or 3rd party telephony" << endl;
+ output << "\t\tcamera\t - for photo applications" << endl;
+ output << "\t\tringtone - for ringtones" << endl;
+ output << "\t\talarm\t - for alarm clock" << endl;
+ output << "\t\tnavigator - for mapping applications" << endl;
+ output << "\t\tgame\t - for gaming" << endl;
+ output << "\t\tplayer\t - for media playback/recording" << endl;
+ output << "\t\tevent\t - for messaging and other event notifications" << endl;
+ output << "\t\tbackground - for thumbnailing etc" << endl;
+ output << "\t\tvideoeditor\t - for video editing/MMS" << endl;
+ output << "\tresources:" << endl;
+ output << "\t comma separated list of the following resources" << endl;
+
+ QHash<QString, ResourcePolicy::ResourceType>::const_iterator ci = resourceValues.constBegin();
+ while (ci != resourceValues.constEnd()) {
+ output << "\t\t" << ci.key() << endl;
+ ++ci;
+ }
+ output << "\t no whitespace allowed in the resource list." << endl;
+ output << "\tmodes:" << endl;
+ output << "\t comma separated list of the following modes" << endl;
+ output << "\t\tAutoRelease" << endl;
+ output << "\t\tAlwaysReply" << endl;
+}
+
+const QSet<ResourcePolicy::ResourceType>& CommandLineParser::resources() const
+{
+ return allResources;
+}
+
+const QSet<ResourcePolicy::ResourceType>& CommandLineParser::optionalResources() const
+{
+ return optResources;
+}
+
+QString CommandLineParser::resourceApplicationClass() const
+{
+ return applicationClass;
+}
+
+bool CommandLineParser::shouldAutoRelease() const
+{
+ return autoRelease;
+}
+
+bool CommandLineParser::shouldAlwaysReply() const
+{
+ return alwaysReply;
+}
+
+bool CommandLineParser::shouldBeVerbose() const
+{
+ return verbose;
+}
+