diff options
Diffstat (limited to 'src/setup/gnunet-setup-gns.c')
-rw-r--r-- | src/setup/gnunet-setup-gns.c | 236 |
1 files changed, 131 insertions, 105 deletions
diff --git a/src/setup/gnunet-setup-gns.c b/src/setup/gnunet-setup-gns.c index f141d156..8e70f21e 100644 --- a/src/setup/gnunet-setup-gns.c +++ b/src/setup/gnunet-setup-gns.c | |||
@@ -61,7 +61,7 @@ | |||
61 | #define PSEU_EMPTY_STR gettext_noop ("<not set>") | 61 | #define PSEU_EMPTY_STR gettext_noop ("<not set>") |
62 | 62 | ||
63 | /** | 63 | /** |
64 | * Height and width of the QR code | 64 | * Height and width of the QR code we display |
65 | */ | 65 | */ |
66 | #define QRCODE_IMAGE_SIZE 64 | 66 | #define QRCODE_IMAGE_SIZE 64 |
67 | 67 | ||
@@ -219,6 +219,123 @@ static struct GNUNET_CRYPTO_ShortHashCode zone; | |||
219 | static int iteration; | 219 | static int iteration; |
220 | 220 | ||
221 | 221 | ||
222 | #if HAVE_QRENCODE_H | ||
223 | #include <qrencode.h> | ||
224 | #include <gdk-pixbuf/gdk-pixbuf.h> | ||
225 | |||
226 | /** | ||
227 | * Create the QR code image for our zone. | ||
228 | * | ||
229 | * @param size height and width of the image to create | ||
230 | * @return NULL on error | ||
231 | */ | ||
232 | static GdkPixbuf * | ||
233 | create_qrcode (unsigned int size) | ||
234 | { | ||
235 | QRinput * qri; | ||
236 | QRcode *qrc; | ||
237 | char *str; | ||
238 | const gchar * pseu; | ||
239 | GtkEntry * entry; | ||
240 | GdkPixbuf *pb; | ||
241 | unsigned int x; | ||
242 | unsigned int y; | ||
243 | unsigned int off; | ||
244 | guchar *pixels; | ||
245 | int n_channels; | ||
246 | int c; | ||
247 | const char *dir; | ||
248 | char *fn; | ||
249 | |||
250 | qri = QRinput_new2 (0, QR_ECLEVEL_Q); | ||
251 | if (NULL == qri) | ||
252 | { | ||
253 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "QRinput_new2"); | ||
254 | return NULL; | ||
255 | } | ||
256 | entry = GTK_ENTRY (GNUNET_SETUP_get_object ("GNUNET_setup_gns_pseu_entry")); | ||
257 | pseu = gtk_entry_get_text (GTK_ENTRY(entry)); | ||
258 | GNUNET_asprintf (&str, | ||
259 | "gnunet://gns/%s/%s\n", | ||
260 | zone_as_string, | ||
261 | pseu); | ||
262 | if (0 != QRinput_append (qri, | ||
263 | QR_MODE_8, | ||
264 | strlen (str), | ||
265 | (unsigned char*) str)) | ||
266 | { | ||
267 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "QRinput_append"); | ||
268 | GNUNET_free (str); | ||
269 | return NULL; | ||
270 | } | ||
271 | GNUNET_free (str); | ||
272 | qrc = QRcode_encodeInput (qri); | ||
273 | if (NULL == qrc) | ||
274 | { | ||
275 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "QRcode_encodeInput"); | ||
276 | QRinput_free (qri); | ||
277 | return NULL; | ||
278 | } | ||
279 | /* We use a trick to create a pixbuf in a way that works for both Gtk2 and Gtk3 | ||
280 | by loading a dummy file from disk; all other methods are not portable to both | ||
281 | Gtk2 and Gtk3. */ | ||
282 | dir = GNUNET_GTK_get_data_dir (); | ||
283 | GNUNET_asprintf (&fn, | ||
284 | "%s%s", | ||
285 | dir, | ||
286 | "qr_dummy.png"); | ||
287 | pb = gdk_pixbuf_new_from_file_at_size (fn, | ||
288 | size, size, | ||
289 | NULL); | ||
290 | GNUNET_free (fn); | ||
291 | if (NULL == pb) | ||
292 | { | ||
293 | QRinput_free (qri); | ||
294 | return NULL; | ||
295 | } | ||
296 | pixels = gdk_pixbuf_get_pixels (pb); | ||
297 | n_channels = gdk_pixbuf_get_n_channels (pb); | ||
298 | for (x=0;x<size;x++) | ||
299 | for (y=0;y<size;y++) | ||
300 | { | ||
301 | off = (x * qrc->width / size) + | ||
302 | (y * qrc->width / size) * qrc->width; | ||
303 | for (c = 0; c < n_channels; c++) | ||
304 | pixels[(y * size + x) * n_channels + c] = (0 == (qrc->data[off] & 1)) ? 0xFF : 0; | ||
305 | } | ||
306 | QRcode_free (qrc); | ||
307 | QRinput_free (qri); | ||
308 | return pb; | ||
309 | } | ||
310 | |||
311 | |||
312 | /** | ||
313 | * Create the QR code image for our zone. | ||
314 | */ | ||
315 | static void | ||
316 | setup_qrcode () | ||
317 | { | ||
318 | GdkPixbuf *pb; | ||
319 | GtkImage *image; | ||
320 | |||
321 | pb = create_qrcode (QRCODE_IMAGE_SIZE); | ||
322 | if (NULL == pb) | ||
323 | { | ||
324 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to initialize QR-code pixbuf")); | ||
325 | return; | ||
326 | } | ||
327 | image = GTK_IMAGE (GNUNET_SETUP_get_object ("GNUNET_setup_gns_qr_image")); | ||
328 | if (NULL == image) | ||
329 | { | ||
330 | GNUNET_break (0); | ||
331 | return; | ||
332 | } | ||
333 | gtk_image_set_from_pixbuf (image, pb); | ||
334 | g_object_unref (pb); | ||
335 | } | ||
336 | |||
337 | #endif | ||
338 | |||
222 | 339 | ||
223 | /** | 340 | /** |
224 | * Context we use for making changes to the namestore. | 341 | * Context we use for making changes to the namestore. |
@@ -1312,7 +1429,6 @@ GNUNET_setup_qr_save_as_dialog_response_cb (GtkDialog *dialog, | |||
1312 | gpointer user_data) | 1429 | gpointer user_data) |
1313 | { | 1430 | { |
1314 | GtkBuilder *builder = user_data; | 1431 | GtkBuilder *builder = user_data; |
1315 | GtkImage *image; | ||
1316 | GdkPixbuf *pb; | 1432 | GdkPixbuf *pb; |
1317 | char *filename; | 1433 | char *filename; |
1318 | 1434 | ||
@@ -1324,14 +1440,18 @@ GNUNET_setup_qr_save_as_dialog_response_cb (GtkDialog *dialog, | |||
1324 | } | 1440 | } |
1325 | filename = | 1441 | filename = |
1326 | GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (dialog)); | 1442 | GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (dialog)); |
1327 | image = GTK_IMAGE (GNUNET_SETUP_get_object ("GNUNET_setup_gns_qr_image")); | 1443 | pb = create_qrcode (512); |
1328 | pb = gtk_image_get_pixbuf (image); | 1444 | if (NULL == pb) |
1329 | 1445 | { | |
1446 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to initialize QR-code pixbuf")); | ||
1447 | return; | ||
1448 | } | ||
1330 | gdk_pixbuf_save (pb, | 1449 | gdk_pixbuf_save (pb, |
1331 | filename, | 1450 | filename, |
1332 | "png", | 1451 | "png", |
1333 | NULL, NULL); | 1452 | NULL, NULL); |
1334 | g_free (filename); | 1453 | g_free (filename); |
1454 | g_object_unref (pb); | ||
1335 | gtk_widget_destroy (GTK_WIDGET (dialog)); | 1455 | gtk_widget_destroy (GTK_WIDGET (dialog)); |
1336 | g_object_unref (G_OBJECT (builder)); | 1456 | g_object_unref (G_OBJECT (builder)); |
1337 | } | 1457 | } |
@@ -1411,7 +1531,9 @@ zone_iteration_proc (void *cls, | |||
1411 | iteration = GNUNET_NO; | 1531 | iteration = GNUNET_NO; |
1412 | GNUNET_free (zc_ctx->label); | 1532 | GNUNET_free (zc_ctx->label); |
1413 | GNUNET_free (zc_ctx); | 1533 | GNUNET_free (zc_ctx); |
1414 | 1534 | #if HAVE_QRENCODE_H | |
1535 | setup_qrcode (); | ||
1536 | #endif | ||
1415 | gtk_widget_hide (GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_gns_status_label"))); | 1537 | gtk_widget_hide (GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_gns_status_label"))); |
1416 | gtk_widget_show (GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_gns_main_scrolledwindow"))); | 1538 | gtk_widget_show (GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_gns_main_scrolledwindow"))); |
1417 | return; | 1539 | return; |
@@ -1573,7 +1695,9 @@ GNUNET_setup_gns_pseu_entry_changed_cb (GtkEditable *editable, | |||
1573 | { | 1695 | { |
1574 | gtk_entry_set_text (GTK_ENTRY(editable), PSEU_EMPTY_STR); | 1696 | gtk_entry_set_text (GTK_ENTRY(editable), PSEU_EMPTY_STR); |
1575 | } | 1697 | } |
1576 | 1698 | #if HAVE_QRENCODE_H | |
1699 | setup_qrcode (); | ||
1700 | #endif | ||
1577 | } | 1701 | } |
1578 | 1702 | ||
1579 | 1703 | ||
@@ -1595,104 +1719,6 @@ GNUNET_setup_gns_public_key_copy_button_clicked_cb (GtkButton *button, | |||
1595 | } | 1719 | } |
1596 | 1720 | ||
1597 | 1721 | ||
1598 | #if HAVE_QRENCODE_H | ||
1599 | #include <qrencode.h> | ||
1600 | #include <gdk-pixbuf/gdk-pixbuf.h> | ||
1601 | |||
1602 | /** | ||
1603 | * Create the QR code image for our zone. | ||
1604 | */ | ||
1605 | static void | ||
1606 | setup_qrcode () | ||
1607 | { | ||
1608 | QRinput * qri; | ||
1609 | QRcode *qrc; | ||
1610 | char *str; | ||
1611 | const gchar * pseu; | ||
1612 | GtkEntry * entry; | ||
1613 | GdkPixbuf *pb; | ||
1614 | GtkImage *image; | ||
1615 | unsigned int x; | ||
1616 | unsigned int y; | ||
1617 | unsigned int off; | ||
1618 | guchar *pixels; | ||
1619 | int n_channels; | ||
1620 | int c; | ||
1621 | const char *dir; | ||
1622 | char *fn; | ||
1623 | |||
1624 | qri = QRinput_new2 (0, QR_ECLEVEL_Q); | ||
1625 | if (NULL == qri) | ||
1626 | { | ||
1627 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "QRinput_new2"); | ||
1628 | return; | ||
1629 | } | ||
1630 | entry = GTK_ENTRY (GNUNET_SETUP_get_object ("GNUNET_setup_gns_pseu_entry")); | ||
1631 | pseu = gtk_entry_get_text (GTK_ENTRY(entry)); | ||
1632 | GNUNET_asprintf (&str, | ||
1633 | "gnunet://gns/%s/%s\n", | ||
1634 | zone_as_string, | ||
1635 | pseu); | ||
1636 | if (0 != QRinput_append (qri, | ||
1637 | QR_MODE_8, | ||
1638 | strlen (str), | ||
1639 | (unsigned char*) str)) | ||
1640 | { | ||
1641 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "QRinput_append"); | ||
1642 | GNUNET_free (str); | ||
1643 | return; | ||
1644 | } | ||
1645 | GNUNET_free (str); | ||
1646 | qrc = QRcode_encodeInput (qri); | ||
1647 | if (NULL == qrc) | ||
1648 | { | ||
1649 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "QRcode_encodeInput"); | ||
1650 | QRinput_free (qri); | ||
1651 | return; | ||
1652 | } | ||
1653 | /* We use a trick to create a pixbuf in a way that works for both Gtk2 and Gtk3 | ||
1654 | by loading a dummy file from disk; all other methods are not portable to both | ||
1655 | Gtk2 and Gtk3. */ | ||
1656 | dir = GNUNET_GTK_get_data_dir (); | ||
1657 | GNUNET_asprintf (&fn, | ||
1658 | "%s%s", | ||
1659 | dir, | ||
1660 | "qr_dummy.png"); | ||
1661 | pb = gdk_pixbuf_new_from_file_at_size (fn, | ||
1662 | QRCODE_IMAGE_SIZE, QRCODE_IMAGE_SIZE, | ||
1663 | NULL); | ||
1664 | GNUNET_free (fn); | ||
1665 | if (NULL == pb) | ||
1666 | { | ||
1667 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to initialize QR-code pixbuf")); | ||
1668 | QRinput_free (qri); | ||
1669 | return; | ||
1670 | } | ||
1671 | pixels = gdk_pixbuf_get_pixels (pb); | ||
1672 | n_channels = gdk_pixbuf_get_n_channels (pb); | ||
1673 | for (x=0;x<QRCODE_IMAGE_SIZE;x++) | ||
1674 | for (y=0;y<QRCODE_IMAGE_SIZE;y++) | ||
1675 | { | ||
1676 | off = (x * qrc->width / QRCODE_IMAGE_SIZE) + | ||
1677 | (y * qrc->width / QRCODE_IMAGE_SIZE) * qrc->width; | ||
1678 | for (c = 0; c < n_channels; c++) | ||
1679 | pixels[(y * QRCODE_IMAGE_SIZE + x) * n_channels + c] = (0 == (qrc->data[off] & 1)) ? 0 : 0xFF; | ||
1680 | } | ||
1681 | image = GTK_IMAGE (GNUNET_SETUP_get_object ("GNUNET_setup_gns_qr_image")); | ||
1682 | if (NULL == image) | ||
1683 | { | ||
1684 | GNUNET_break (0); | ||
1685 | QRcode_free (qrc); | ||
1686 | QRinput_free (qri); | ||
1687 | return; | ||
1688 | } | ||
1689 | gtk_image_set_from_pixbuf (image, pb); | ||
1690 | QRcode_free (qrc); | ||
1691 | QRinput_free (qri); | ||
1692 | } | ||
1693 | #endif | ||
1694 | |||
1695 | |||
1696 | /** | 1722 | /** |
1697 | * Connect to the namestore and initialize the main | 1723 | * Connect to the namestore and initialize the main |
1698 | * GNS tree view. | 1724 | * GNS tree view. |