gnunet-qr: Add into Makefile.am and pofiles,
[oweals/gnunet.git] / src / util / gnunet-qr.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013-2019 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 #include <stdio.h>
22 #include <zbar.h>
23 #include <stdbool.h>
24 #include <getopt.h>
25 #include "gnunet-qr-utils.h"
26
27 static const char *usage_note =
28   "gnunet-qr\n"
29   "Scan a QR code using a video device and import\n"
30   "\n"
31   "Arguments mandatory for long options are also mandatory for short options.\n"
32   "  -c, --config FILENAME      use configuration file FILENAME\n"
33   "  -d, --device DEVICE        use device DEVICE\n"
34   "  -s, --silent               do not show preview windows\n"
35   "  -h, --help                 print this help\n"
36   "  -v, --verbose              be verbose\n"
37   "Report bugs to gnunet-developers@gnu.org.\n"
38   "\n"
39   "GNUnet home page: https://gnunet.org/\n"
40   "General help using GNU software: https://www.gnu.org/gethelp/\n";
41
42 #define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
43
44 int main (int argc, char **argv)
45 {
46   const char* configuration = NULL;
47   const char* device = "/dev/video0";
48   static bool verbose = false;
49   static bool silent = false;
50
51   static struct option long_options[] = {
52       {"verbose", no_argument,       0, 'v'},
53       {"silent",  no_argument,       0, 's'},
54       {"help",    no_argument,       0, 'h'},
55       {"config",  required_argument, 0, 'c'},
56       {"device",  required_argument, 0, 'd'},
57       {0, 0, 0, 0}
58     };
59   while (1) {
60     int opt;
61     opt = getopt_long (argc, argv, "c:hd:sv",
62                      long_options, NULL);
63     if (opt == -1)
64       break;
65
66     switch (opt) {
67     case 'h':
68       printf(usage_note);
69       return 0;
70     case 'c':
71       configuration = optarg;
72       break;
73     case 'd':
74       device = optarg;
75       break;
76     case 's':
77       silent = true;
78       break;
79     case 'v':
80       verbose = true;
81       break;
82     default:
83       printf(usage_note);
84       return 1;
85     }
86   }
87
88   /* create a Processor */
89   LOG("Initializing\n");
90   zbar_processor_t *proc = zbar_processor_create(1);
91
92   // FIXME: Wrap all this into a function which returns an error on
93   // failure. And here ensure the processor is destroyed at the end.
94
95   /* configure the Processor */
96   zbar_processor_parse_config(proc, "enable");
97
98   /* initialize the Processor */
99   LOG("Opening video device %s\n", device);
100   // FIXME: error handling
101   zbar_processor_init(proc, device, 1);
102
103   /* enable the preview window */
104   zbar_processor_set_visible(proc, 1);
105   zbar_processor_set_active(proc, 1);
106
107   /* keep scanning until user provides key/mouse input */
108   //zbar_processor_user_wait(proc, -1);
109
110   // read at least one barcode (or until window closed)
111   LOG("Capturing\n");
112   int n;
113   n = zbar_process_one(proc, -1);
114   LOG("Got %i images\n", n);
115   // FIXME: Error handling (n = -1)
116
117   // hide the preview window
118   zbar_processor_set_active(proc, 0);
119   zbar_processor_set_visible(proc, 0);
120
121   // extract results
122   int rc = 1;
123
124   const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc);
125   const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols);
126
127   if (symbol != NULL) {
128     const char* data = zbar_symbol_get_data(symbol);
129     LOG("Found %s \"%s\"\n",
130         zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data);
131
132     if (configuration == NULL) {
133       char* command_args[] = {"gnunet-uri", data, NULL };
134       LOG("Running `gnunet-uri %s`\n", data);
135       rc = fork_and_exec("gnunet-uri", command_args);
136     } else {
137       char* command_args[] = {"gnunet-uri", "-c", configuration, data, NULL };
138       LOG("Running `gnunet-uri -c '%s' %s`\n", configuration, data);
139       rc = fork_and_exec("gnunet-uri", command_args);
140     };
141
142     if (rc != 0) {
143       printf("Failed to add URI %s\n", data);
144     } else {
145       printf("Added URI %s\n", data);
146     }
147   }
148
149   /* clean up */
150   zbar_processor_destroy(proc);
151
152   return(rc);
153 }