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
 
31
using std::string;
32
using std::find;
33
using std::vector;
21 andreas 34
using std::cout;
35
using std::endl;
2 andreas 36
 
21 andreas 37
/**
38
 * @class InputParser
39
 * @brief The InputParser class parses the command line.
40
 *
41
 * This class takes the command line arguments and parses them. It creates an
42
 * internal vector array to hold the parameters.
43
 */
2 andreas 44
class InputParser
45
{
3 andreas 46
    public:
21 andreas 47
        /**
48
         * @brief InputParser is the constructor.
49
         *
50
         * The constructor requires the command line parameters. It immediately
51
         * starts to parse each parameter. It it finds the string `--` it stops
52
         * and ignores the rest of parameters. \p argc contains the rest of
53
         * the parameters after `--`, if there are any.
54
         *
55
         * @param argc  A pointer to the numbers of command line parameters.
56
         *              This parameter must not be `NULL`.
57
         * @param argv  The 2 dimensional array of command line parameters.
58
         *              This parameter must not be `NULL`.
59
         */
3 andreas 60
        InputParser(int *argc, char **argv)
61
        {
62
            int i;
2 andreas 63
 
3 andreas 64
            for (i = 1; i < *argc; ++i)
65
            {
66
                if (string(argv[i]).compare("--") == 0)
67
                    break;
2 andreas 68
 
3 andreas 69
                this->tokens.push_back(string(argv[i]));
70
            }
2 andreas 71
 
3 andreas 72
            *argc -= i;
2 andreas 73
 
3 andreas 74
            if (*argc <= 1)
75
                *argc = 1;
76
            else
77
            {
78
                *argc = *argc + 1;
79
                *(argv + i - 1) = *argv;
80
            }
81
        }
21 andreas 82
        /**
83
         * @brief getCmdOption searches for the command line option \p option.
84
         * @author iain
85
         *
86
         * The method searches for the command line option \p option and
87
         * returns the parameter after the option, if there is one.
88
         *
89
         * @param option    The name of a command line option. This is any
90
         *                  name starting with 1 or 2 dash (-). The name in
91
         *                  the parameter must include the dash(es) in front
92
         *                  of the name.\n
93
         *                  If the option was found and the next parameter on
94
         *                  the command line doesn't start with a dash, the
95
         *                  parameter is returned.
96
         *
97
         * @return Tf the option was found and the parameter on the command
98
         * line following the option doesn't start with a dash, it is returned.
99
         * Otherwise an empty string is returned.
100
         */
3 andreas 101
        const string& getCmdOption(const string &option) const
102
        {
103
            vector<string>::const_iterator itr;
104
            itr = find(this->tokens.begin(), this->tokens.end(), option);
2 andreas 105
 
3 andreas 106
            if (itr != this->tokens.end() && ++itr != this->tokens.end())
107
                return *itr;
2 andreas 108
 
3 andreas 109
            static const string empty_string("");
110
            return empty_string;
111
        }
21 andreas 112
 
113
        /**
114
         * @brief cmdOptionExists tests for an existing option.
115
         * @author iain
116
         *
117
         * This function tests whether the option \p option exists or not. If
118
         * the option was found, it returnes `true`.
119
         *
120
         * @param option    The name of a command line option. This is any
121
         *                  name starting with 1 or 2 dash (-). The name in
122
         *                  the parameter must include the dash(es) in front
123
         *                  of the name.\n
124
         *
125
         * @return If the command line option was found in the internal vector
126
         * array `true` is returned. Otherwise it returnes `false`.
127
         */
3 andreas 128
        bool cmdOptionExists(const string &option) const
129
        {
130
            return find(this->tokens.begin(), this->tokens.end(), option) != this->tokens.end();
131
        }
2 andreas 132
 
3 andreas 133
    private:
134
        vector <string> tokens;
2 andreas 135
};
136
 
21 andreas 137
/**
138
 * @brief usage displays on the standard output a small help.
139
 *
140
 * This function shows a short help with all available parameters and a brief
141
 * description of them.
142
 * \verbatim
143
 * NOTE: This function is not available on Android systems.
144
 * \endverbatim
145
 */
2 andreas 146
void usage()
147
{
21 andreas 148
#ifndef __ANDROID__
149
    cout << TConfig::getProgName() << " version " <<  V_MAJOR << "."  << V_MINOR << "." << V_PATCH << endl << endl;
150
    cout << "Usage: tpanel [-c <config file>]" << endl;
151
    cout << "-c | --config-file <file> The path and name of the configuration file." << endl;
152
    cout << "                          This parameter is optional. If it is omitted," << endl;
153
    cout << "                          The standard path is searched for the" << endl;
154
    cout << "                          configuration file." << endl << endl;
155
    cout << "-h | --help               This help." << endl << endl;
156
#endif
2 andreas 157
}
158
 
21 andreas 159
/**
160
 * @brief banner displays a shor banner with informations about this application.
161
 *
162
 * This function shows a short information about this application. It prints
163
 * this on the standard output.
164
 * \verbatim
165
 * NOTE: This function is not available on Android systems.
166
 * \endverbatim
167
 *
168
 * @param pname The name of this application.
169
 */
2 andreas 170
void banner(const string& pname)
171
{
21 andreas 172
#ifdef __ANDROID__
173
    return;
174
#else
3 andreas 175
    if (!TConfig::showBanner())
176
        return;
2 andreas 177
 
21 andreas 178
    cout << pname << " v" << V_MAJOR << "."  << V_MINOR << "." << V_PATCH << endl;
179
    cout << "(C) Andreas Theofilu <andreas@theosys.at>" << endl;
180
    cout << "This program is under the terms of GPL version 3" << endl << endl;
181
#endif
2 andreas 182
}
183
 
21 andreas 184
/**
185
 * @brief main is the main entry function.
186
 *
187
 * This is where the program starts.
188
 *
189
 * @param argc  The number of command line arguments.
190
 * @param argv  A pointer to a 2 dimensional array containing the command line
191
 *              parameters.
192
 *
193
 * @return 0 on success. This means that no errors occured.\n
194
 * In case of an error a number grater than 0 is returned.
195
 */
2 andreas 196
int main(int argc, char *argv[])
197
{
3 andreas 198
    string configFile;
2 andreas 199
 
3 andreas 200
    string pname = *argv;
201
    size_t pos = pname.find_last_of("/");
2 andreas 202
 
3 andreas 203
    if (pos != string::npos)
204
        pname = pname.substr(pos + 1);
2 andreas 205
 
21 andreas 206
    TConfig::setProgName(pname);    // Remember the name of this application.
2 andreas 207
 
3 andreas 208
    int oldArgc = argc;
21 andreas 209
    InputParser input(&argc, argv); // Parse the command line parameters.
2 andreas 210
 
21 andreas 211
    // Evaluate the command line parameters.
3 andreas 212
    if (input.cmdOptionExists("-h") || input.cmdOptionExists("--help"))
213
    {
214
        banner(pname);
215
        usage();
216
        return 0;
217
    }
2 andreas 218
 
3 andreas 219
    if (input.cmdOptionExists("-c") || input.cmdOptionExists("--config-file"))
220
    {
221
        configFile = input.getCmdOption("-c");
2 andreas 222
 
3 andreas 223
        if (configFile.empty())
224
            configFile = input.getCmdOption("--config-file");
2 andreas 225
 
3 andreas 226
        if (configFile.empty())
227
        {
228
            banner(pname);
229
            std::cerr << "Missing the path and name of the configuration file!" << std::endl;
230
            usage();
231
            return 1;
232
        }
233
    }
2 andreas 234
 
21 andreas 235
    TError::clear();                    // Clear all errors (initialize)
236
    TConfig config(configFile);         // Read the configuration file.
2 andreas 237
 
21 andreas 238
    if (TError::isError())              // Exit if the previous command failed.
3 andreas 239
        return 1;
2 andreas 240
 
3 andreas 241
    banner(pname);
242
    TError::clear();
243
    // Read in the pages
244
    TPageManager pageManager;
2 andreas 245
 
3 andreas 246
    if (TError::isError())
247
        return 1;
2 andreas 248
 
3 andreas 249
    // Prepare command line stack
250
    int pt = oldArgc - argc;
251
    // Start the graphical environment
21 andreas 252
 
3 andreas 253
    int ret = 0;
2 andreas 254
 
3 andreas 255
    if ((ret = qtmain(argc, &argv[pt], &pageManager)) != 0)
256
        return ret;
257
 
258
    return 0;
2 andreas 259
}