2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
24 #include <glib/gi18n.h>
30 const GOptionEntry option_entries
[] =
34 "check-mode-offline", 'o', 0, G_OPTION_ARG_NONE
,
35 &opts
.core
.check_mode_offline
, NULL
, NULL
38 "print-format", 'p', 0, G_OPTION_ARG_STRING
, &opts
.core
.print_format
,
42 "print-subtitles", 'B', 0, G_OPTION_ARG_NONE
, &opts
.core
.print_subtitles
,
46 "print-streams", 'S', 0, G_OPTION_ARG_NONE
, &opts
.core
.print_streams
,
50 "subtitle-export-format", 'L', 0, G_OPTION_ARG_STRING
,
51 &opts
.core
.subtitle_export_format
, NULL
, NULL
54 "subtitle-language", 'l', 0, G_OPTION_ARG_STRING
,
55 &opts
.core
.subtitle_language
, NULL
, NULL
58 "stream", 's', 0, G_OPTION_ARG_STRING
, &opts
.core
.stream
,
62 "verbosity", 'b', 0, G_OPTION_ARG_STRING
, &opts
.core
.verbosity
,
67 "query-metainfo", 'q', 0, G_OPTION_ARG_NONE
, &opts
.dump
.query_metainfo
,
72 "exec-enable-stderr", 'E', 0, G_OPTION_ARG_NONE
, &opts
.exec
.enable_stderr
,
76 "exec-enable-stdout", 'O', 0, G_OPTION_ARG_NONE
, &opts
.exec
.enable_stdout
,
80 "exec-dump-argv", 'A', 0, G_OPTION_ARG_NONE
, &opts
.exec
.dump_argv
,
84 "exec", 'e', 0, G_OPTION_ARG_STRING_ARRAY
, &opts
.exec
.external
,
89 "output-regex", 'g', 0, G_OPTION_ARG_STRING_ARRAY
, &opts
.get
.output_regex
,
93 "output-file", 'f', 0, G_OPTION_ARG_STRING
, &opts
.get
.output_file
,
97 "output-name", 'n', 0, G_OPTION_ARG_STRING
, &opts
.get
.output_name
,
101 "output-dir", 'i', 0, G_OPTION_ARG_STRING
, &opts
.get
.output_dir
,
105 "overwrite", 'w', 0, G_OPTION_ARG_NONE
, &opts
.get
.overwrite
,
109 "skip-transfer", 'k', 0, G_OPTION_ARG_NONE
, &opts
.get
.skip_transfer
,
113 "resume-from", 'r', 0, G_OPTION_ARG_DOUBLE
, &opts
.get
.resume_from
,
117 "throttle", 't', 0, G_OPTION_ARG_INT
, &opts
.get
.throttle
,
122 "user-agent", 'u', 0, G_OPTION_ARG_STRING
, &opts
.http
.user_agent
,
127 G_OPTION_REMAINING
, 0, 0, G_OPTION_ARG_STRING_ARRAY
, &opts
.rargs
,
130 {NULL
, 0, 0, 0, NULL
, NULL
, NULL
}
133 static const gchar
*dumpformat_possible_values
[] =
136 "rfc2483", /* playlist specific */
137 #ifdef HAVE_JSON_GLIB
146 static gint
cb_chk_str(const gchar
*fpath
, const gchar
*opt_name
,
147 const gchar
*opt_val
, const gchar
**ok_strv
)
150 if (lopts_chk_str(opt_val
, ok_strv
, &inv_val
) != EXIT_SUCCESS
)
151 return (lopts_invalid_value(opt_name
, fpath
, inv_val
, ok_strv
));
152 return (EXIT_SUCCESS
);
156 static gint
cb_chk_strv(const gchar
*fpath
, const gchar
*opt_name
,
157 const gchar
**opt_val
, const gchar
**ok_strv
)
160 if (lopts_chk_strv(opt_val
, ok_strv
, &inv_val
) != EXIT_SUCCESS
)
161 return (lopts_invalid_value(opt_name
, fpath
, inv_val
, ok_strv
));
162 return (EXIT_SUCCESS
);
166 static gint
cb_chk_re(const gchar
*fpath
, const gchar
*opt_name
,
167 const gchar
**opt_val
)
170 if (lopts_chk_re(opt_val
, &inv_val
) != EXIT_SUCCESS
)
171 return (lopts_invalid_value(opt_name
, fpath
, inv_val
, NULL
));
172 return (EXIT_SUCCESS
);
175 static gint
cb_chk_throttle(const gchar
*fpath
,
176 const gchar
*opt_name
,
179 gint r
= EXIT_SUCCESS
;
182 gchar
*s
= g_strdup_printf("%d", opt_val
);
183 lopts_invalid_value(opt_name
, fpath
, s
, NULL
);
190 static const gchar g_core
[] = "core";
191 static const gchar g_dump
[] = "dump";
192 static const gchar g_exec
[] = "exec";
193 static const gchar g_http
[] = "http";
194 static const gchar g_get
[] = "get";
196 void cb_parse_keyfile_values(GKeyFile
*kf
, const gchar
*fpath
)
200 lopts_keyfile_get_bool(kf
, fpath
, g_core
,
201 "check-mode-offline", &opts
.core
.check_mode_offline
);
203 lopts_keyfile_get_str(kf
, cb_chk_str
, fpath
, g_core
,
204 dumpformat_possible_values
,
205 "print-format", &opts
.core
.print_format
);
207 lopts_keyfile_get_str(kf
, NULL
, fpath
, g_core
, NULL
,
208 "subtitle-export-format",
209 &opts
.core
.subtitle_export_format
);
211 lopts_keyfile_get_str(kf
, NULL
, fpath
, g_core
, NULL
,
212 "subtitle-language", &opts
.core
.subtitle_language
);
214 lopts_keyfile_get_str(kf
, NULL
, fpath
, g_core
, NULL
,
215 "stream", &opts
.core
.stream
);
217 lopts_keyfile_get_str(kf
, cb_chk_str
, fpath
, g_core
,
218 lutil_verbosity_possible_values
,
219 "verbosity", &opts
.core
.verbosity
);
223 lopts_keyfile_get_bool(kf
, fpath
, g_dump
,
224 "query-metainfo", &opts
.dump
.query_metainfo
);
228 lopts_keyfile_get_bool(kf
, fpath
, g_exec
,
229 "dump-argv", &opts
.exec
.dump_argv
);
231 lopts_keyfile_get_bool(kf
, fpath
, g_exec
,
232 "enable-stderr", &opts
.exec
.enable_stderr
);
234 lopts_keyfile_get_bool(kf
, fpath
, g_exec
,
235 "enable-stdout", &opts
.exec
.enable_stdout
);
237 lopts_keyfile_get_strv(kf
, NULL
, fpath
, g_exec
, NULL
,
238 "external", &opts
.exec
.external
);
242 lopts_keyfile_get_str(kf
, NULL
, fpath
, g_get
, NULL
,
243 "output-dir", &opts
.get
.output_dir
);
245 lopts_keyfile_get_str(kf
, NULL
, fpath
, g_get
, NULL
,
246 "output-name", &opts
.get
.output_name
);
248 lopts_keyfile_get_re(kf
, cb_chk_re
, fpath
, g_get
, NULL
,
249 "output-regex", &opts
.get
.output_regex
);
251 lopts_keyfile_get_double(kf
, NULL
, fpath
, g_get
,
252 "resume-from", &opts
.get
.resume_from
);
254 lopts_keyfile_get_bool(kf
, fpath
, g_get
,
255 "skip-transfer", &opts
.get
.skip_transfer
);
257 lopts_keyfile_get_int(kf
, cb_chk_throttle
, fpath
, g_get
,
258 "throttle", &opts
.get
.throttle
);
262 lopts_keyfile_get_str(kf
, NULL
, fpath
, g_http
, NULL
,
263 "user-agent", &opts
.http
.user_agent
);
268 if (r != EXIT_SUCCESS)\
272 gint
cb_cmdline_validate_values()
278 r
= cb_chk_str(NULL
, "print-format", opts
.core
.print_format
,
279 dumpformat_possible_values
);
282 r
= cb_chk_str(NULL
, "verbosity", opts
.core
.verbosity
,
283 lutil_verbosity_possible_values
);
286 r
= cb_chk_re(NULL
, "output-regex", (const gchar
**) opts
.get
.output_regex
);
291 r
= cb_chk_throttle(NULL
, "throttle", opts
.get
.throttle
);
294 return (EXIT_SUCCESS
);
299 void cb_set_post_parse_defaults()
303 if (opts
.core
.subtitle_export_format
== NULL
)
304 opts
.core
.subtitle_export_format
= g_strdup("srt");
306 if (opts
.core
.verbosity
== NULL
)
307 opts
.core
.verbosity
= g_strdup("verbose");
311 if (opts
.get
.output_regex
== NULL
)
321 opts
.get
.output_regex
= g_strdupv(sv
);
324 if (opts
.get
.output_name
== NULL
)
325 opts
.get
.output_name
= g_strdup("%t.%e");
329 if (opts
.http
.user_agent
== NULL
)
330 opts
.http
.user_agent
= g_strdup("Mozilla/5.0");
333 gchar
*cb_get_config_fpath()
335 const gchar
*s
= g_getenv("QUVI_CONFIG");
336 if (s
!= NULL
&& strlen(s
) >0)
337 return (g_strdup(s
));
341 /* vim: set ts=2 sw=2 tw=72 expandtab: */