Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
2 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
#include <iostream>
19
#include <iomanip>
20
#include <string>
21
#include <vector>
22
#include <algorithm>
23
#include "tconfig.h"
24
#include "tsettings.h"
3 andreas 25
#include "tpagelist.h"
26
#include "tpage.h"
27
#include "tsubpage.h"
28
#include "tpagemanager.h"
2 andreas 29
#include "tqtmain.h"
30
 
22 andreas 31
#ifdef Q_OS_ANDROID
32
#include <QStandardPaths>
33
#endif
34
#ifdef __ANDROID__
35
#include <android/log.h>
36
#endif
37
 
2 andreas 38
using std::string;
39
using std::find;
40
using std::vector;
21 andreas 41
using std::cout;
42
using std::endl;
2 andreas 43
 
21 andreas 44
/**
45
 * @class InputParser
46
 * @brief The InputParser class parses the command line.
47
 *
48
 * This class takes the command line arguments and parses them. It creates an
49
 * internal vector array to hold the parameters.
50
 */
2 andreas 51
class InputParser
52
{
3 andreas 53
    public:
21 andreas 54
        /**
55
         * @brief InputParser is the constructor.
56
         *
57
         * The constructor requires the command line parameters. It immediately
58
         * starts to parse each parameter. It it finds the string `--` it stops
59
         * and ignores the rest of parameters. \p argc contains the rest of
60
         * the parameters after `--`, if there are any.
61
         *
62
         * @param argc  A pointer to the numbers of command line parameters.
63
         *              This parameter must not be `NULL`.
64
         * @param argv  The 2 dimensional array of command line parameters.
65
         *              This parameter must not be `NULL`.
66
         */
3 andreas 67
        InputParser(int *argc, char **argv)
68
        {
69
            int i;
2 andreas 70
 
3 andreas 71
            for (i = 1; i < *argc; ++i)
72
            {
73
                if (string(argv[i]).compare("--") == 0)
74
                    break;
2 andreas 75
 
3 andreas 76
                this->tokens.push_back(string(argv[i]));
77
            }
2 andreas 78
 
3 andreas 79
            *argc -= i;
2 andreas 80
 
3 andreas 81
            if (*argc <= 1)
82
                *argc = 1;
83
            else
84
            {
85
                *argc = *argc + 1;
86
                *(argv + i - 1) = *argv;
87
            }
88
        }
21 andreas 89
        /**
90
         * @brief getCmdOption searches for the command line option \p option.
91
         * @author iain
92
         *
93
         * The method searches for the command line option \p option and
94
         * returns the parameter after the option, if there is one.
95
         *
96
         * @param option    The name of a command line option. This is any
97
         *                  name starting with 1 or 2 dash (-). The name in
98
         *                  the parameter must include the dash(es) in front
99
         *                  of the name.\n
100
         *                  If the option was found and the next parameter on
101
         *                  the command line doesn't start with a dash, the
102
         *                  parameter is returned.
103
         *
104
         * @return Tf the option was found and the parameter on the command
105
         * line following the option doesn't start with a dash, it is returned.
106
         * Otherwise an empty string is returned.
107
         */
3 andreas 108
        const string& getCmdOption(const string &option) const
109
        {
110
            vector<string>::const_iterator itr;
111
            itr = find(this->tokens.begin(), this->tokens.end(), option);
2 andreas 112
 
3 andreas 113
            if (itr != this->tokens.end() && ++itr != this->tokens.end())
114
                return *itr;
2 andreas 115
 
3 andreas 116
            static const string empty_string("");
117
            return empty_string;
118
        }
21 andreas 119
 
120
        /**
121
         * @brief cmdOptionExists tests for an existing option.
122
         * @author iain
123
         *
124
         * This function tests whether the option \p option exists or not. If
125
         * the option was found, it returnes `true`.
126
         *
127
         * @param option    The name of a command line option. This is any
128
         *                  name starting with 1 or 2 dash (-). The name in
129
         *                  the parameter must include the dash(es) in front
130
         *                  of the name.\n
131
         *
132
         * @return If the command line option was found in the internal vector
133
         * array `true` is returned. Otherwise it returnes `false`.
134
         */
3 andreas 135
        bool cmdOptionExists(const string &option) const
136
        {
137
            return find(this->tokens.begin(), this->tokens.end(), option) != this->tokens.end();
138
        }
2 andreas 139
 
3 andreas 140
    private:
141
        vector <string> tokens;
2 andreas 142
};
143
 
21 andreas 144
/**
145
 * @brief usage displays on the standard output a small help.
146
 *
147
 * This function shows a short help with all available parameters and a brief
148
 * description of them.
149
 * \verbatim
150
 * NOTE: This function is not available on Android systems.
151
 * \endverbatim
152
 */
2 andreas 153
void usage()
154
{
21 andreas 155
#ifndef __ANDROID__
156
    cout << TConfig::getProgName() << " version " <<  V_MAJOR << "."  << V_MINOR << "." << V_PATCH << endl << endl;
157
    cout << "Usage: tpanel [-c <config file>]" << endl;
158
    cout << "-c | --config-file <file> The path and name of the configuration file." << endl;
159
    cout << "                          This parameter is optional. If it is omitted," << endl;
160
    cout << "                          The standard path is searched for the" << endl;
161
    cout << "                          configuration file." << endl << endl;
162
    cout << "-h | --help               This help." << endl << endl;
163
#endif
2 andreas 164
}
165
 
22 andreas 166
#ifndef __ANDROID__
21 andreas 167
/**
168
 * @brief banner displays a shor banner with informations about this application.
169
 *
170
 * This function shows a short information about this application. It prints
171
 * this on the standard output.
172
 * \verbatim
173
 * NOTE: This function is not available on Android systems.
174
 * \endverbatim
175
 *
176
 * @param pname The name of this application.
177
 */
2 andreas 178
void banner(const string& pname)
179
{
21 andreas 180
    return;
3 andreas 181
    if (!TConfig::showBanner())
182
        return;
2 andreas 183
 
21 andreas 184
    cout << pname << " v" << V_MAJOR << "."  << V_MINOR << "." << V_PATCH << endl;
185
    cout << "(C) Andreas Theofilu <andreas@theosys.at>" << endl;
186
    cout << "This program is under the terms of GPL version 3" << endl << endl;
22 andreas 187
}
21 andreas 188
#endif
2 andreas 189
 
21 andreas 190
/**
191
 * @brief main is the main entry function.
192
 *
193
 * This is where the program starts.
194
 *
195
 * @param argc  The number of command line arguments.
196
 * @param argv  A pointer to a 2 dimensional array containing the command line
197
 *              parameters.
198
 *
199
 * @return 0 on success. This means that no errors occured.\n
200
 * In case of an error a number grater than 0 is returned.
201
 */
2 andreas 202
int main(int argc, char *argv[])
203
{
3 andreas 204
    string configFile;
22 andreas 205
#ifndef __ANDROID__
3 andreas 206
    string pname = *argv;
207
    size_t pos = pname.find_last_of("/");
2 andreas 208
 
3 andreas 209
    if (pos != string::npos)
210
        pname = pname.substr(pos + 1);
22 andreas 211
#else
212
    string pname = "tpanel";
213
#endif
21 andreas 214
    TConfig::setProgName(pname);    // Remember the name of this application.
22 andreas 215
#ifndef Q_OS_ANDROID
3 andreas 216
    int oldArgc = argc;
21 andreas 217
    InputParser input(&argc, argv); // Parse the command line parameters.
2 andreas 218
 
21 andreas 219
    // Evaluate the command line parameters.
3 andreas 220
    if (input.cmdOptionExists("-h") || input.cmdOptionExists("--help"))
221
    {
222
        banner(pname);
223
        usage();
224
        return 0;
225
    }
2 andreas 226
 
3 andreas 227
    if (input.cmdOptionExists("-c") || input.cmdOptionExists("--config-file"))
228
    {
229
        configFile = input.getCmdOption("-c");
2 andreas 230
 
3 andreas 231
        if (configFile.empty())
232
            configFile = input.getCmdOption("--config-file");
2 andreas 233
 
3 andreas 234
        if (configFile.empty())
235
        {
236
            banner(pname);
237
            std::cerr << "Missing the path and name of the configuration file!" << std::endl;
238
            usage();
239
            return 1;
240
        }
241
    }
22 andreas 242
#endif
21 andreas 243
    TError::clear();                    // Clear all errors (initialize)
244
    TConfig config(configFile);         // Read the configuration file.
2 andreas 245
 
21 andreas 246
    if (TError::isError())              // Exit if the previous command failed.
22 andreas 247
    {
248
        TError::displayMessage(TError::getErrorMsg());
3 andreas 249
        return 1;
22 andreas 250
    }
251
#ifndef __ANDROID__
3 andreas 252
    banner(pname);
22 andreas 253
#endif
3 andreas 254
    TError::clear();
255
    // Read in the pages
256
    TPageManager pageManager;
2 andreas 257
 
3 andreas 258
    if (TError::isError())
259
        return 1;
2 andreas 260
 
3 andreas 261
    // Prepare command line stack
22 andreas 262
#ifndef __ANDROID__
3 andreas 263
    int pt = oldArgc - argc;
22 andreas 264
#else
265
    int pt = 0;
266
#endif
3 andreas 267
    // Start the graphical environment
21 andreas 268
 
3 andreas 269
    int ret = 0;
2 andreas 270
 
3 andreas 271
    if ((ret = qtmain(argc, &argv[pt], &pageManager)) != 0)
272
        return ret;
273
 
274
    return 0;
2 andreas 275
}