1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2006 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #ifndef D_OPTION_HANDLER_IMPL_H
36 #define D_OPTION_HANDLER_IMPL_H
37 
38 #include "OptionHandler.h"
39 
40 #include <vector>
41 
42 #include "AbstractOptionHandler.h"
43 #include "A2STR.h"
44 
45 namespace aria2 {
46 
47 class Option;
48 struct Pref;
49 
50 class BooleanOptionHandler : public AbstractOptionHandler {
51 public:
52   BooleanOptionHandler(PrefPtr pref, const char* description = NO_DESCRIPTION,
53                        const std::string& defaultValue = NO_DEFAULT_VALUE,
54                        OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
55                        char shortName = 0);
56   virtual ~BooleanOptionHandler();
57   virtual void parseArg(Option& option,
58                         const std::string& optarg) const CXX11_OVERRIDE;
59   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
60 };
61 
62 class IntegerRangeOptionHandler : public AbstractOptionHandler {
63 private:
64   int32_t min_;
65   int32_t max_;
66 
67 public:
68   IntegerRangeOptionHandler(PrefPtr pref, const char* description,
69                             const std::string& defaultValue, int32_t min,
70                             int32_t max, char shortName = 0);
71   virtual ~IntegerRangeOptionHandler();
72   virtual void parseArg(Option& option,
73                         const std::string& optarg) const CXX11_OVERRIDE;
74   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
75 };
76 
77 class NumberOptionHandler : public AbstractOptionHandler {
78 private:
79   int64_t min_;
80   int64_t max_;
81 
82 public:
83   NumberOptionHandler(PrefPtr pref, const char* description = NO_DESCRIPTION,
84                       const std::string& defaultValue = NO_DEFAULT_VALUE,
85                       int64_t min = -1, int64_t max = -1, char shortName = 0);
86   virtual ~NumberOptionHandler();
87 
88   virtual void parseArg(Option& option,
89                         const std::string& optarg) const CXX11_OVERRIDE;
90   void parseArg(Option& option, int64_t number) const;
91   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
92 };
93 
94 class UnitNumberOptionHandler : public NumberOptionHandler {
95 public:
96   UnitNumberOptionHandler(PrefPtr pref,
97                           const char* description = NO_DESCRIPTION,
98                           const std::string& defaultValue = NO_DEFAULT_VALUE,
99                           int64_t min = -1, int64_t max = -1,
100                           char shortName = 0);
101   virtual ~UnitNumberOptionHandler();
102   virtual void parseArg(Option& option,
103                         const std::string& optarg) const CXX11_OVERRIDE;
104 };
105 
106 class FloatNumberOptionHandler : public AbstractOptionHandler {
107 private:
108   double min_;
109   double max_;
110 
111 public:
112   FloatNumberOptionHandler(PrefPtr pref,
113                            const char* description = NO_DESCRIPTION,
114                            const std::string& defaultValue = NO_DEFAULT_VALUE,
115                            double min = -1, double max = -1,
116                            char shortName = 0);
117   virtual ~FloatNumberOptionHandler();
118   virtual void parseArg(Option& option,
119                         const std::string& optarg) const CXX11_OVERRIDE;
120   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
121 };
122 
123 class DefaultOptionHandler : public AbstractOptionHandler {
124 private:
125   std::string possibleValuesString_;
126   bool allowEmpty_;
127 
128 public:
129   DefaultOptionHandler(PrefPtr pref, const char* description = NO_DESCRIPTION,
130                        const std::string& defaultValue = NO_DEFAULT_VALUE,
131                        const std::string& possibleValuesString = A2STR::NIL,
132                        OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
133                        char shortName = 0);
134   virtual ~DefaultOptionHandler();
135   virtual void parseArg(Option& option,
136                         const std::string& optarg) const CXX11_OVERRIDE;
137   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
138   void setAllowEmpty(bool allow);
139 };
140 
141 class CumulativeOptionHandler : public AbstractOptionHandler {
142 private:
143   std::string delim_;
144   std::string possibleValuesString_;
145 
146 public:
147   CumulativeOptionHandler(
148       PrefPtr pref, const char* description, const std::string& defaultValue,
149       const std::string& delim,
150       const std::string& possibleValuesString = A2STR::NIL,
151       OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
152       char shortName = 0);
153   virtual ~CumulativeOptionHandler();
154   virtual void parseArg(Option& option,
155                         const std::string& optarg) const CXX11_OVERRIDE;
156   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
157 };
158 
159 class IndexOutOptionHandler : public AbstractOptionHandler {
160 public:
161   IndexOutOptionHandler(PrefPtr pref, const char* description,
162                         char shortName = 0);
163   virtual ~IndexOutOptionHandler();
164   virtual void parseArg(Option& option,
165                         const std::string& optarg) const CXX11_OVERRIDE;
166   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
167 };
168 
169 class ChecksumOptionHandler : public AbstractOptionHandler {
170 public:
171   ChecksumOptionHandler(PrefPtr pref, const char* description,
172                         char shortName = 0);
173   ChecksumOptionHandler(PrefPtr pref, const char* description,
174                         std::vector<std::string> acceptableTypes,
175                         char shortName = 0);
176   virtual ~ChecksumOptionHandler();
177   virtual void parseArg(Option& option,
178                         const std::string& optarg) const CXX11_OVERRIDE;
179   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
180 
181 private:
182   // message digest type acceptable for this option.  Empty means that
183   // it accepts all supported types.
184   std::vector<std::string> acceptableTypes_;
185 };
186 
187 class ParameterOptionHandler : public AbstractOptionHandler {
188 private:
189   std::vector<std::string> validParamValues_;
190 
191 public:
192   ParameterOptionHandler(PrefPtr pref, const char* description,
193                          const std::string& defaultValue,
194                          std::vector<std::string> validParamValues,
195                          char shortName = 0);
196   virtual ~ParameterOptionHandler();
197   virtual void parseArg(Option& option,
198                         const std::string& optarg) const CXX11_OVERRIDE;
199   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
200 };
201 
202 class HostPortOptionHandler : public AbstractOptionHandler {
203 private:
204   PrefPtr hostOptionName_;
205   PrefPtr portOptionName_;
206 
207 public:
208   HostPortOptionHandler(PrefPtr pref, const char* description,
209                         const std::string& defaultValue, PrefPtr hostOptionName,
210                         PrefPtr portOptionName, char shortName = 0);
211   virtual ~HostPortOptionHandler();
212   virtual void parseArg(Option& option,
213                         const std::string& optarg) const CXX11_OVERRIDE;
214   void setHostAndPort(Option& option, const std::string& hostname,
215                       uint16_t port) const;
216   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
217 };
218 
219 class HttpProxyOptionHandler : public AbstractOptionHandler {
220 private:
221   PrefPtr proxyUserPref_;
222   PrefPtr proxyPasswdPref_;
223 
224 public:
225   HttpProxyOptionHandler(PrefPtr pref, const char* description,
226                          const std::string& defaultValue, char shortName = 0);
227   virtual ~HttpProxyOptionHandler();
228   virtual void parseArg(Option& option,
229                         const std::string& optarg) const CXX11_OVERRIDE;
230   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
231 };
232 
233 class LocalFilePathOptionHandler : public AbstractOptionHandler {
234 private:
235   std::string possibleValuesString_;
236   bool acceptStdin_;
237   bool mustExist_;
238 
239 public:
240   LocalFilePathOptionHandler(PrefPtr pref,
241                              const char* description = NO_DESCRIPTION,
242                              const std::string& defaultValue = NO_DEFAULT_VALUE,
243                              bool acceptStdin = false, char shortName = 0,
244                              bool mustExist = true,
245                              const std::string& possibleValuesString = "");
246   virtual void parseArg(Option& option,
247                         const std::string& optarg) const CXX11_OVERRIDE;
248   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
249 };
250 
251 class PrioritizePieceOptionHandler : public AbstractOptionHandler {
252 public:
253   PrioritizePieceOptionHandler(
254       PrefPtr pref, const char* description = NO_DESCRIPTION,
255       const std::string& defaultValue = NO_DEFAULT_VALUE, char shortName = 0);
256   virtual void parseArg(Option& option,
257                         const std::string& optarg) const CXX11_OVERRIDE;
258   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
259 };
260 
261 class OptimizeConcurrentDownloadsOptionHandler : public AbstractOptionHandler {
262 public:
263   OptimizeConcurrentDownloadsOptionHandler(
264       PrefPtr pref, const char* description = NO_DESCRIPTION,
265       const std::string& defaultValue = NO_DEFAULT_VALUE, char shortName = 0);
266   virtual void parseArg(Option& option,
267                         const std::string& optarg) const CXX11_OVERRIDE;
268   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
269 };
270 
271 // This class is used to deprecate option and optionally handle its
272 // option value using replacing option.
273 class DeprecatedOptionHandler : public OptionHandler {
274 private:
275   OptionHandler* depOptHandler_;
276   const OptionHandler* repOptHandler_;
277   bool stillWork_;
278   std::string additionalMessage_;
279 
280 public:
281   // depOptHandler is deprecated option and repOptHandler is replacing
282   // new option. If there is no replacing option, specify nullptr.  If
283   // there is no replacing option, but the option still lives, give
284   // true to stillWork. Set additional message to additionalMessage.
285   DeprecatedOptionHandler(OptionHandler* depOptHandler,
286                           const OptionHandler* repOptHandler = nullptr,
287                           bool stillWork = false,
288                           std::string additionalMessage = "");
289   virtual ~DeprecatedOptionHandler();
290   virtual void parse(Option& option,
291                      const std::string& arg) const CXX11_OVERRIDE;
292   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
293   virtual bool hasTag(uint32_t tag) const CXX11_OVERRIDE;
294   virtual void addTag(uint32_t tag) CXX11_OVERRIDE;
295   virtual std::string toTagString() const CXX11_OVERRIDE;
296   virtual const char* getName() const CXX11_OVERRIDE;
297   virtual const char* getDescription() const CXX11_OVERRIDE;
298   virtual const std::string& getDefaultValue() const CXX11_OVERRIDE;
299   virtual bool isHidden() const CXX11_OVERRIDE;
300   virtual void hide() CXX11_OVERRIDE;
301   virtual PrefPtr getPref() const CXX11_OVERRIDE;
302   virtual ARG_TYPE getArgType() const CXX11_OVERRIDE;
303   virtual char getShortName() const CXX11_OVERRIDE;
304   virtual bool getEraseAfterParse() const CXX11_OVERRIDE;
305   virtual void setEraseAfterParse(bool eraseAfterParse) CXX11_OVERRIDE;
306   virtual bool getInitialOption() const CXX11_OVERRIDE;
307   virtual void setInitialOption(bool f) CXX11_OVERRIDE;
308   virtual bool getChangeOption() const CXX11_OVERRIDE;
309   virtual void setChangeOption(bool f) CXX11_OVERRIDE;
310   virtual bool getChangeOptionForReserved() const CXX11_OVERRIDE;
311   virtual void setChangeOptionForReserved(bool f) CXX11_OVERRIDE;
312   virtual bool getChangeGlobalOption() const CXX11_OVERRIDE;
313   virtual void setChangeGlobalOption(bool f) CXX11_OVERRIDE;
314   virtual bool getCumulative() const CXX11_OVERRIDE;
315   virtual void setCumulative(bool f) CXX11_OVERRIDE;
316 };
317 
318 } // namespace aria2
319 
320 #endif // D_OPTION_HANDLER_IMPL_H
321