summaryrefslogtreecommitdiff
path: root/resourceqt-client/resourceqt-client.cpp
blob: 49fa3231da84073a240a4c3dfe17286fc0641e6a (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
#include <QtCore/QCoreApplication>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>
#include <getopt.h>
#include "client.h"

class CommandLineParser
{
public:
    CommandLineParser(int argc, char** argv) {
        exitCode	= 0;
        exitFlag 	= false;
        exeName = strdup(basename(argv[0]));

        resourcesAll 		= 0;
        resourcesOptional	= 0;
        autoRelease = false;
        alwaysReply = false;

        parseArguments(argc, argv);
    }

    ~CommandLineParser() {
        if (exeName != NULL) {
            delete exeName;
            exeName = NULL;
        }
    }

public:
    bool		exitFlag;
    int			exitCode;

    uint32_t	resourcesAll;
    uint32_t	resourcesOptional;
    QString		applicationClass;
    bool		autoRelease;
    bool		alwaysReply;

    void printMessage(const char *fmt, ...) {
        va_list  ap;
        char     fmtbuf[512];

        snprintf(fmtbuf, sizeof(fmtbuf), "%s\n", fmt);
        va_start(ap, fmt);
        vprintf(fmtbuf, ap);
        va_end(ap);
    }

private:
    char* 	exeName;

    void printError(const char *fmt, ...) {
        va_list  ap;
        char     fmtbuf[512];

        snprintf(fmtbuf, sizeof(fmtbuf), "%s\n", fmt);
        va_start(ap, fmt);
        vprintf(fmtbuf, ap);
        va_end(ap);

        exitCode = errno;
        exitFlag = true;
    }

    void parseArguments(int argc, char** argv) {
        int   option;

        while ((option = getopt(argc, argv, "hm:o:")) != -1) {
            switch (option) {
            case 'h':
                usage(0);
                return;
            case 'm':
                parseModeValues(optarg);
                break;
            case 'o':
                resourcesOptional = Client::parseResourceList(optarg);
                break;
            default:
                usage(EINVAL);
                return;
            }
        }

        if ((optind != argc - 2) && (optind != argc - 1)) {
            usage(EINVAL);
            return;
        }
        else {
            applicationClass = parseClassString(argv[optind]);
            if (argc > optind + 1)
                resourcesAll = Client::parseResourceList(argv[optind+1]);
            else
                resourcesAll = 0;
        }

        if (!resourcesAll) {
            printMessage("No resources found, use add command to add some ...");
        }

        if ((resourcesAll | resourcesOptional) != resourcesAll) {
            printError("optional resources are not subset of all resources");
        }
    }

    char* parseClassString(char *str) {
        if (strcmp(str, "call") &&
                strcmp(str, "camera") &&
                strcmp(str, "ringtone") &&
                strcmp(str, "alarm") &&
                strcmp(str, "navigator") &&
                strcmp(str, "game") &&
                strcmp(str, "player") &&
                strcmp(str, "event") &&
                strcmp(str, "background")) {
            printError("invalid class '%s'", str);
            return NULL;
        }

        return str;
    }

    void parseModeValues(QString modeListStr) {
        if (!modeListStr.isEmpty() && !modeListStr.isNull()) {
            QStringList modeList = modeListStr.split(",", QString::SkipEmptyParts);

            for (int i = 0; i < modeList.count(); i++) {
                if (modeList[i] == "AutoRelease") {
                    autoRelease = true;
                }
                else if (modeList[i] == "AlwaysReply") {
                    alwaysReply = true;
                }
                else {
                    const char* mode = qPrintable(modeList[i]);
                    printMessage("Ignoring unknown mode '%s'!", mode);
                }
            }
        }
    }

    void usage(int theExitCode) {
        printf("usage: %s [-h] [-t] [-v] [-m mode-values]"
               "[-o optional-resources] [-s shared-resources -m shared-mask] "
               "class all-resources\n",
               exeName);
        printf("\toptions:\n");
        printf("\t  h\tprint this help message and exit\n");
        printf("\t  m\tmode values. See 'modes' below for the "
               "\n\t\tsyntax of <mode-values>\n");
        printf("\t  o\toptional resources. See 'resources' below for the "
               "syntax of\n\t\t<optional-resources>\n");
        printf("\tclass:\n");
        printf("\t\tcall\t  - for native or 3rd party telephony\n");
        printf("\t\tcamera\t  - for photo applications\n");
        printf("\t\tringtone  - for ringtones\n");
        printf("\t\talarm\t  - for alarm clock\n");
        printf("\t\tnavigator - for mapping applications\n");
        printf("\t\tgame\t  - for gaming\n");
        printf("\t\tplayer\t  - for media playback/recording\n");
        printf("\t\tevent\t  - for messaging and other event notifications\n");
        printf("\t\tbackground - for thumbnailing etc\n");
        printf("\tresources:\n");
        printf("\t  comma separated list of the following resources\n");
        printf("\t\tAudioPlayback\n");
        printf("\t\tVideoPlayback\n");
        printf("\t\tAudioRecording\n");
        printf("\t\tVideoRecording\n");
        printf("\t\tVibra\n");
        printf("\t\tLeds\n");
        printf("\t\tBackLight\n");
        printf("\t\tSystemButton\n");
        printf("\t\tLockButton\n");
        printf("\t\tScaleButton\n");
        printf("\t\tSnapButton\n");
        printf("\t\tLensCover\n");
        printf("\t  no whitespace allowed in the resource list.\n");
        printf("\tmodes:\n");
        printf("\t  comma separated list of the following modes\n");
        printf("\t\tAutoRelease\n");
        printf("\t\tAlwaysReply\n");
        fflush(stdout);

        exitCode = theExitCode;
        exitFlag = true;
    }
};

int main(int argc, char *argv[])
{
    CommandLineParser parser(argc, argv);

    if (parser.exitFlag)
        return parser.exitCode;

    QCoreApplication app(argc, argv);

    Client client(parser.applicationClass);
    if (!client.initialize(parser.resourcesAll, parser.resourcesOptional, parser.alwaysReply, parser.autoRelease)) {
        parser.printMessage("initialization failed!");
        return EINVAL;
    }

    return app.exec();
}