Debian Codegraph @

src/av-cp/didl-dialog.c

raw
1 /*
2 * Copyright (C) 2016 Jens Georg <[email protected]>
3 *
4 * Authors: Jens Georg <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <config.h>
22
23 #include "didl-dialog.h"
24 #include "pretty-print.h"
25
26 #include <gtk/gtk.h>
27
28 #ifdef HAVE_GTK_SOURCEVIEW
29 #include <gtksourceview/gtksource.h>
30 #endif
31
32 #define DIALOG_RESOURCE_PATH "/org/gupnp/Tools/AV-CP/didl-lite-dialog.ui"
33
34 struct _AVCPDidlDialog {
35 GtkDialog parent;
36 };
37
38 struct _AVCPDidlDialogClass {
39 GtkDialogClass parent_class;
40 };
41
42 struct _AVCPDidlDialogPrivate {
43 GtkWidget *didl_textview;
44 };
45 typedef struct _AVCPDidlDialogPrivate AVCPDidlDialogPrivate;
46
47 G_DEFINE_TYPE_WITH_PRIVATE (AVCPDidlDialog, av_cp_didl_dialog, GTK_TYPE_DIALOG)
48
49 /* GObject overrides */
50 static void av_cp_didl_dialog_constructed (GObject *object);
51
52 static void
53 av_cp_didl_dialog_class_init (AVCPDidlDialogClass *klass)
54 {
55 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
56 GObjectClass *object_class = G_OBJECT_CLASS (klass);
57 gtk_widget_class_set_template_from_resource (widget_class,
58 DIALOG_RESOURCE_PATH);
59
60 gtk_widget_class_bind_template_child_private (widget_class,
61 AVCPDidlDialog,
62 didl_textview);
63
64 object_class->constructed = av_cp_didl_dialog_constructed;
65 }
66
67 static void
68 av_cp_didl_dialog_init (AVCPDidlDialog *self)
69 {
70 gtk_widget_init_template (GTK_WIDGET (self));
71 }
72
73 static void
74 av_cp_didl_dialog_constructed (GObject *object)
75 {
76 GObjectClass *parent_class = NULL;
77
78 parent_class = G_OBJECT_CLASS (av_cp_didl_dialog_parent_class);
79 if (parent_class->constructed != NULL) {
80 parent_class->constructed (object);
81 }
82
83 g_signal_connect (object, "delete-event",
84 G_CALLBACK (gtk_widget_hide_on_delete), NULL);
85
86 #ifdef HAVE_GTK_SOURCEVIEW
87 {
88 AVCPDidlDialog *self = AV_CP_DIDL_DIALOG (object);
89 AVCPDidlDialogPrivate *priv;
90
91 priv = av_cp_didl_dialog_get_instance_private (self);
92
93 GtkSourceLanguageManager *manager =
94 gtk_source_language_manager_get_default ();
95 GtkSourceLanguage *language =
96 gtk_source_language_manager_guess_language (manager,
97 NULL,
98 "text/xml");
99
100 GtkSourceBuffer *buffer = gtk_source_buffer_new_with_language (language);
101 gtk_source_buffer_set_highlight_syntax (buffer, TRUE);
102 gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW (priv->didl_textview),
103 TRUE);
104
105 gtk_text_view_set_buffer (GTK_TEXT_VIEW (priv->didl_textview),
106 GTK_TEXT_BUFFER (buffer));
107 }
108 #endif
109 }
110
111 AVCPDidlDialog *
112 av_cp_didl_dialog_new (void)
113 {
114 GtkSettings *settings = gtk_settings_get_default ();
115 int use_header;
116
117 g_object_get (G_OBJECT (settings),
118 "gtk-dialogs-use-header",
119 &use_header,
120 NULL);
121
122 return g_object_new (AV_CP_TYPE_DIDL_DIALOG,
123 "use-header-bar", use_header == 1 ? TRUE : FALSE,
124 NULL);
125 }
126
127 void
128 av_cp_didl_dialog_set_xml (AVCPDidlDialog *self, const char *xml)
129 {
130 GtkTextBuffer *buffer;
131 char *formatted;
132 AVCPDidlDialogPrivate *priv;
133
134 priv = av_cp_didl_dialog_get_instance_private (self);
135
136 formatted = pretty_print_xml (xml);
137
138 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->didl_textview));
139 gtk_text_buffer_set_text (buffer, formatted, -1);
140 g_free (formatted);
141 }