text-viewer-application.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* text-viewer-application.c
  2. *
  3. * Copyright 2025 Thomas Arnoux
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * SPDX-License-Identifier: GPL-3.0-or-later
  19. */
  20. #include "config.h"
  21. #include <glib/gi18n.h>
  22. #include "text-viewer-application.h"
  23. #include "text-viewer-window.h"
  24. struct _TextViewerApplication
  25. {
  26. AdwApplication parent_instance;
  27. };
  28. G_DEFINE_FINAL_TYPE (TextViewerApplication, text_viewer_application, ADW_TYPE_APPLICATION)
  29. TextViewerApplication *
  30. text_viewer_application_new (const char *application_id,
  31. GApplicationFlags flags)
  32. {
  33. g_return_val_if_fail (application_id != NULL, NULL);
  34. return g_object_new (TEXT_VIEWER_TYPE_APPLICATION,
  35. "application-id", application_id,
  36. "flags", flags,
  37. "resource-base-path", "/com/example/TextViewer",
  38. NULL);
  39. }
  40. static void
  41. text_viewer_application_activate (GApplication *app)
  42. {
  43. GtkWindow *window;
  44. g_assert (TEXT_VIEWER_IS_APPLICATION (app));
  45. window = gtk_application_get_active_window (GTK_APPLICATION (app));
  46. if (window == NULL)
  47. window = g_object_new (TEXT_VIEWER_TYPE_WINDOW,
  48. "application", app,
  49. NULL);
  50. gtk_window_present (window);
  51. }
  52. static void
  53. text_viewer_application_class_init (TextViewerApplicationClass *klass)
  54. {
  55. GApplicationClass *app_class = G_APPLICATION_CLASS (klass);
  56. app_class->activate = text_viewer_application_activate;
  57. }
  58. static void
  59. text_viewer_application_about_action (GSimpleAction *action,
  60. GVariant *parameter,
  61. gpointer user_data)
  62. {
  63. static const char *developers[] = {"Thomas Arnoux", NULL};
  64. TextViewerApplication *self = user_data;
  65. GtkWindow *window = NULL;
  66. g_assert (TEXT_VIEWER_IS_APPLICATION (self));
  67. window = gtk_application_get_active_window (GTK_APPLICATION (self));
  68. adw_show_about_dialog (GTK_WIDGET (window),
  69. "application-name", "text-viewer",
  70. "application-icon", "com.example.TextViewer",
  71. "developer-name", "Thomas Arnoux",
  72. "translator-credits", _("translator-credits"),
  73. "version", "0.1.0",
  74. "developers", developers,
  75. "copyright", "© 2025 Thomas Arnoux",
  76. NULL);
  77. }
  78. static void
  79. text_viewer_application_quit_action (GSimpleAction *action,
  80. GVariant *parameter,
  81. gpointer user_data)
  82. {
  83. TextViewerApplication *self = user_data;
  84. g_assert (TEXT_VIEWER_IS_APPLICATION (self));
  85. g_application_quit (G_APPLICATION (self));
  86. }
  87. static const GActionEntry app_actions[] = {
  88. { "quit", text_viewer_application_quit_action },
  89. { "about", text_viewer_application_about_action },
  90. };
  91. static void
  92. text_viewer_application_init (TextViewerApplication *self)
  93. {
  94. g_action_map_add_action_entries (G_ACTION_MAP (self),
  95. app_actions,
  96. G_N_ELEMENTS (app_actions),
  97. self);
  98. gtk_application_set_accels_for_action (GTK_APPLICATION (self),
  99. "app.quit",
  100. (const char *[]) { "<control>q", NULL });
  101. }