nmtui-hostname.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
  2. /*
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as
  5. * published by the Free Software Foundation; either version 2 of the
  6. * License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Copyright 2013 Red Hat, Inc.
  17. */
  18. /**
  19. * SECTION:nmtui-hostname
  20. * @short_description: hostname-setting functionality
  21. *
  22. * nmtui-hostname implements the "set hostname" functionality
  23. */
  24. #include "config.h"
  25. #include <glib/gi18n-lib.h>
  26. #include "nmt-newt.h"
  27. #include "nmtui.h"
  28. #include "nmtui-hostname.h"
  29. #include "nmt-utils.h"
  30. static char *
  31. nmtui_hostname_run_dialog (void)
  32. {
  33. NmtNewtForm *form;
  34. NmtNewtWidget *widget, *ok, *cancel;
  35. NmtNewtGrid *grid;
  36. NmtNewtEntry *entry;
  37. NmtNewtButtonBox *bbox;
  38. char *hostname, *ret = NULL;
  39. form = g_object_new (NMT_TYPE_NEWT_FORM,
  40. "title", _("Set Hostname"),
  41. "escape-exits", TRUE,
  42. NULL);
  43. widget = nmt_newt_grid_new ();
  44. nmt_newt_form_set_content (form, widget);
  45. grid = NMT_NEWT_GRID (widget);
  46. widget = nmt_newt_label_new (_("Hostname"));
  47. nmt_newt_grid_add (grid, widget, 0, 0);
  48. widget = nmt_newt_entry_new (40, 0);
  49. nmt_newt_widget_set_exit_on_activate (widget, TRUE);
  50. nmt_newt_grid_add (grid, widget, 1, 0);
  51. nmt_newt_widget_set_padding (widget, 1, 0, 0, 0);
  52. entry = NMT_NEWT_ENTRY (widget);
  53. widget = nmt_newt_button_box_new (NMT_NEWT_BUTTON_BOX_HORIZONTAL);
  54. nmt_newt_grid_add (grid, widget, 1, 1);
  55. nmt_newt_widget_set_padding (widget, 0, 1, 0, 0);
  56. bbox = NMT_NEWT_BUTTON_BOX (widget);
  57. cancel = nmt_newt_button_box_add_end (bbox, _("Cancel"));
  58. nmt_newt_widget_set_exit_on_activate (cancel, TRUE);
  59. ok = nmt_newt_button_box_add_end (bbox, _("OK"));
  60. nmt_newt_widget_set_exit_on_activate (ok, TRUE);
  61. g_object_get (G_OBJECT (nm_settings),
  62. NM_REMOTE_SETTINGS_HOSTNAME, &hostname,
  63. NULL);
  64. nmt_newt_entry_set_text (entry, hostname);
  65. g_free (hostname);
  66. widget = nmt_newt_form_run_sync (form);
  67. if (widget == (NmtNewtWidget *)entry || widget == ok)
  68. ret = g_strdup (nmt_newt_entry_get_text (entry));
  69. g_object_unref (form);
  70. return ret;
  71. }
  72. static void
  73. hostname_set (NMRemoteSettings *settings,
  74. GError *error,
  75. gpointer op)
  76. {
  77. nmt_sync_op_complete_boolean (op, error == NULL, error);
  78. }
  79. NmtNewtForm *
  80. nmtui_hostname (int argc, char **argv)
  81. {
  82. const char *hostname;
  83. char *tmp = NULL;
  84. NmtSyncOp op;
  85. GError *error = NULL;
  86. if (argc == 2)
  87. hostname = argv[1];
  88. else
  89. hostname = tmp = nmtui_hostname_run_dialog ();
  90. if (hostname) {
  91. nmt_sync_op_init (&op);
  92. nm_remote_settings_save_hostname (nm_settings, hostname, hostname_set, &op);
  93. if (nmt_sync_op_wait_boolean (&op, &error)) {
  94. /* Translators: this indicates the result. ie, "I have set the hostname to ..." */
  95. nmt_newt_message_dialog (_("Set hostname to '%s'"), hostname);
  96. } else {
  97. nmt_newt_message_dialog (_("Unable to set hostname: %s"), error->message);
  98. g_error_free (error);
  99. }
  100. g_free (tmp);
  101. }
  102. return NULL;
  103. }