diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-06-26 19:37:49 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-06-26 19:37:49 +0000 |
commit | eb1364131f6a46df1747651c2f9d30abce855733 (patch) | |
tree | 53458d288f61f01305b9bfe5849e221890556c3a | |
parent | 1fa58da15c41b9cac03ff91de7cdb2f190c697ad (diff) | |
download | gnunet-gtk-eb1364131f6a46df1747651c2f9d30abce855733.tar.gz gnunet-gtk-eb1364131f6a46df1747651c2f9d30abce855733.zip |
-qr code saveas and decoding via online service work
-rw-r--r-- | contrib/gnunet_setup_gtk_main_window.glade | 32 | ||||
-rw-r--r-- | src/setup/gnunet-setup-gns.c | 236 |
2 files changed, 148 insertions, 120 deletions
diff --git a/contrib/gnunet_setup_gtk_main_window.glade b/contrib/gnunet_setup_gtk_main_window.glade index ebf3f293..5a2e8bdd 100644 --- a/contrib/gnunet_setup_gtk_main_window.glade +++ b/contrib/gnunet_setup_gtk_main_window.glade | |||
@@ -16,6 +16,21 @@ | |||
16 | <property name="step_increment">1</property> | 16 | <property name="step_increment">1</property> |
17 | <property name="page_increment">10</property> | 17 | <property name="page_increment">10</property> |
18 | </object> | 18 | </object> |
19 | <object class="GtkAdjustment" id="GNUNET_setup_hostlist_server_port_adjustment"> | ||
20 | <property name="lower">1</property> | ||
21 | <property name="upper">65535</property> | ||
22 | <property name="value">8080</property> | ||
23 | <property name="step_increment">1</property> | ||
24 | <property name="page_increment">10</property> | ||
25 | </object> | ||
26 | <object class="GtkListStore" id="GNUNET_setup_hostlist_url_liststore"> | ||
27 | <columns> | ||
28 | <!-- column-name url --> | ||
29 | <column type="gchararray"/> | ||
30 | <!-- column-name editable --> | ||
31 | <column type="gboolean"/> | ||
32 | </columns> | ||
33 | </object> | ||
19 | <object class="GtkDialog" id="GNUNET_setup_dialog"> | 34 | <object class="GtkDialog" id="GNUNET_setup_dialog"> |
20 | <property name="can_focus">False</property> | 35 | <property name="can_focus">False</property> |
21 | <property name="border_width">5</property> | 36 | <property name="border_width">5</property> |
@@ -4492,6 +4507,8 @@ | |||
4492 | </child> | 4507 | </child> |
4493 | <child> | 4508 | <child> |
4494 | <object class="GtkImage" id="GNUNET_setup_gns_qr_image"> | 4509 | <object class="GtkImage" id="GNUNET_setup_gns_qr_image"> |
4510 | <property name="width_request">64</property> | ||
4511 | <property name="height_request">64</property> | ||
4495 | <property name="visible">True</property> | 4512 | <property name="visible">True</property> |
4496 | <property name="can_focus">False</property> | 4513 | <property name="can_focus">False</property> |
4497 | <property name="tooltip_text" translatable="yes">QR code for the selected zone</property> | 4514 | <property name="tooltip_text" translatable="yes">QR code for the selected zone</property> |
@@ -4931,21 +4948,6 @@ | |||
4931 | </row> | 4948 | </row> |
4932 | </data> | 4949 | </data> |
4933 | </object> | 4950 | </object> |
4934 | <object class="GtkAdjustment" id="GNUNET_setup_hostlist_server_port_adjustment"> | ||
4935 | <property name="lower">1</property> | ||
4936 | <property name="upper">65535</property> | ||
4937 | <property name="value">8080</property> | ||
4938 | <property name="step_increment">1</property> | ||
4939 | <property name="page_increment">10</property> | ||
4940 | </object> | ||
4941 | <object class="GtkListStore" id="GNUNET_setup_hostlist_url_liststore"> | ||
4942 | <columns> | ||
4943 | <!-- column-name url --> | ||
4944 | <column type="gchararray"/> | ||
4945 | <!-- column-name editable --> | ||
4946 | <column type="gboolean"/> | ||
4947 | </columns> | ||
4948 | </object> | ||
4949 | <object class="GtkAdjustment" id="GNUNET_setup_min_friends_adjustment"> | 4951 | <object class="GtkAdjustment" id="GNUNET_setup_min_friends_adjustment"> |
4950 | <property name="upper">9999</property> | 4952 | <property name="upper">9999</property> |
4951 | <property name="step_increment">1</property> | 4953 | <property name="step_increment">1</property> |
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. |