Subversion Repositories tpanel

Rev

Rev 3 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 21
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (C) 2020 by Andreas Theofilu <andreas@theosys.at>
2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
3
 *
3
 *
4
 * This program is free software; you can redistribute it and/or modify
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
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
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
7
 * (at your option) any later version.
Line 24... Line 24...
24
#include "tsettings.h"
24
#include "tsettings.h"
25
#include "tpagelist.h"
25
#include "tpagelist.h"
26
#include "tpage.h"
26
#include "tpage.h"
27
#include "tsubpage.h"
27
#include "tsubpage.h"
28
#include "tpagemanager.h"
28
#include "tpagemanager.h"
29
 
-
 
30
#ifdef QT5_LINUX
-
 
31
#include "tqtmain.h"
29
#include "tqtmain.h"
32
#endif
-
 
33
 
30
 
34
using std::string;
31
using std::string;
35
using std::find;
32
using std::find;
36
using std::vector;
33
using std::vector;
-
 
34
using std::cout;
-
 
35
using std::endl;
37
 
36
 
-
 
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
 */
38
class InputParser
44
class InputParser
39
{
45
{
40
    public:
46
    public:
-
 
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
         */
41
        InputParser(int *argc, char **argv)
60
        InputParser(int *argc, char **argv)
42
        {
61
        {
43
            int i;
62
            int i;
44
 
63
 
45
            for (i = 1; i < *argc; ++i)
64
            for (i = 1; i < *argc; ++i)
Line 58... Line 77...
58
            {
77
            {
59
                *argc = *argc + 1;
78
                *argc = *argc + 1;
60
                *(argv + i - 1) = *argv;
79
                *(argv + i - 1) = *argv;
61
            }
80
            }
62
        }
81
        }
-
 
82
        /**
-
 
83
         * @brief getCmdOption searches for the command line option \p option.
63
        /// @author iain
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
         */
64
        const string& getCmdOption(const string &option) const
101
        const string& getCmdOption(const string &option) const
65
        {
102
        {
66
            vector<string>::const_iterator itr;
103
            vector<string>::const_iterator itr;
67
            itr = find(this->tokens.begin(), this->tokens.end(), option);
104
            itr = find(this->tokens.begin(), this->tokens.end(), option);
68
 
105
 
Line 70... Line 107...
70
                return *itr;
107
                return *itr;
71
 
108
 
72
            static const string empty_string("");
109
            static const string empty_string("");
73
            return empty_string;
110
            return empty_string;
74
        }
111
        }
-
 
112
 
-
 
113
        /**
-
 
114
         * @brief cmdOptionExists tests for an existing option.
75
        /// @author iain
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
         */
76
        bool cmdOptionExists(const string &option) const
128
        bool cmdOptionExists(const string &option) const
77
        {
129
        {
78
            return find(this->tokens.begin(), this->tokens.end(), option) != this->tokens.end();
130
            return find(this->tokens.begin(), this->tokens.end(), option) != this->tokens.end();
79
        }
131
        }
80
 
132
 
81
    private:
133
    private:
82
        vector <string> tokens;
134
        vector <string> tokens;
83
};
135
};
84
 
136
 
-
 
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
 */
85
void usage()
146
void usage()
86
{
147
{
-
 
148
#ifndef __ANDROID__
87
    std::cout << TConfig::getProgName() << " version " <<  V_MAJOR << "."  << V_MINOR << "." << V_PATCH << std::endl << std::endl;
149
    cout << TConfig::getProgName() << " version " <<  V_MAJOR << "."  << V_MINOR << "." << V_PATCH << endl << endl;
88
    std::cout << "Usage: tpanel [-c <config file>]" << std::endl;
150
    cout << "Usage: tpanel [-c <config file>]" << endl;
89
    std::cout << "-c | --config-file <file> The path and name of the configuration file." << std::endl;
151
    cout << "-c | --config-file <file> The path and name of the configuration file." << endl;
90
    std::cout << "                          This parameter is optional. If it is omitted," << std::endl;
152
    cout << "                          This parameter is optional. If it is omitted," << endl;
91
    std::cout << "                          The standard path is searched for the" << std::endl;
153
    cout << "                          The standard path is searched for the" << endl;
92
    std::cout << "                          configuration file." << std::endl << std::endl;
154
    cout << "                          configuration file." << endl << endl;
93
    std::cout << "-h | --help               This help." << std::endl << std::endl;
155
    cout << "-h | --help               This help." << endl << endl;
-
 
156
#endif
94
}
157
}
95
 
158
 
-
 
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
 */
96
void banner(const string& pname)
170
void banner(const string& pname)
97
{
171
{
-
 
172
#ifdef __ANDROID__
-
 
173
    return;
-
 
174
#else
98
    if (!TConfig::showBanner())
175
    if (!TConfig::showBanner())
99
        return;
176
        return;
100
 
177
 
101
    std::cout << pname << " v" << V_MAJOR << "."  << V_MINOR << "." << V_PATCH << std::endl;
178
    cout << pname << " v" << V_MAJOR << "."  << V_MINOR << "." << V_PATCH << endl;
102
    std::cout << "(C) Andreas Theofilu <andreas@theosys.at>" << std::endl;
179
    cout << "(C) Andreas Theofilu <andreas@theosys.at>" << endl;
103
    std::cout << "This program is under the terms of GPL version 3" << std::endl << std::endl;
180
    cout << "This program is under the terms of GPL version 3" << endl << endl;
-
 
181
#endif
104
}
182
}
105
 
183
 
-
 
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
 */
106
int main(int argc, char *argv[])
196
int main(int argc, char *argv[])
107
{
197
{
108
    string configFile;
198
    string configFile;
109
 
199
 
110
    string pname = *argv;
200
    string pname = *argv;
111
    size_t pos = pname.find_last_of("/");
201
    size_t pos = pname.find_last_of("/");
112
 
202
 
113
    if (pos != string::npos)
203
    if (pos != string::npos)
114
        pname = pname.substr(pos + 1);
204
        pname = pname.substr(pos + 1);
115
 
205
 
116
    TConfig::setProgName(pname);
206
    TConfig::setProgName(pname);    // Remember the name of this application.
117
 
207
 
118
    int oldArgc = argc;
208
    int oldArgc = argc;
119
    InputParser input(&argc, argv);
209
    InputParser input(&argc, argv); // Parse the command line parameters.
120
 
210
 
-
 
211
    // Evaluate the command line parameters.
121
    if (input.cmdOptionExists("-h") || input.cmdOptionExists("--help"))
212
    if (input.cmdOptionExists("-h") || input.cmdOptionExists("--help"))
122
    {
213
    {
123
        banner(pname);
214
        banner(pname);
124
        usage();
215
        usage();
125
        return 0;
216
        return 0;
Line 139... Line 230...
139
            usage();
230
            usage();
140
            return 1;
231
            return 1;
141
        }
232
        }
142
    }
233
    }
143
 
234
 
144
    TError::clear();
235
    TError::clear();                    // Clear all errors (initialize)
145
    TConfig config(configFile);
236
    TConfig config(configFile);         // Read the configuration file.
146
 
237
 
147
    if (TError::isError())
238
    if (TError::isError())              // Exit if the previous command failed.
148
        return 1;
239
        return 1;
149
 
240
 
150
    banner(pname);
241
    banner(pname);
151
    TError::clear();
242
    TError::clear();
152
    // Read in the pages
243
    // Read in the pages
Line 156... Line 247...
156
        return 1;
247
        return 1;
157
 
248
 
158
    // Prepare command line stack
249
    // Prepare command line stack
159
    int pt = oldArgc - argc;
250
    int pt = oldArgc - argc;
160
    // Start the graphical environment
251
    // Start the graphical environment
161
#ifdef QT5_LINUX
-
 
-
 
252
 
162
    int ret = 0;
253
    int ret = 0;
163
 
254
 
164
    if ((ret = qtmain(argc, &argv[pt], &pageManager)) != 0)
255
    if ((ret = qtmain(argc, &argv[pt], &pageManager)) != 0)
165
        return ret;
256
        return ret;
166
 
257
 
167
#endif
-
 
168
    return 0;
258
    return 0;
169
}
259
}