1 /*
2  * e-html-editor-link-dialog.h
3  *
4  * Copyright (C) 2012 Dan Vrátil <dvratil@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) version 3.
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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with the program; if not, see <http://www.gnu.org/licenses/>
18  *
19  */
20 
21 #include "evolution-config.h"
22 
23 #include <glib/gi18n-lib.h>
24 
25 #include "e-misc-utils.h"
26 #include "e-url-entry.h"
27 
28 #include "e-html-editor-link-dialog.h"
29 
30 #define E_HTML_EDITOR_LINK_DIALOG_GET_PRIVATE(obj) \
31 	(G_TYPE_INSTANCE_GET_PRIVATE \
32 	((obj), E_TYPE_HTML_EDITOR_LINK_DIALOG, EHTMLEditorLinkDialogPrivate))
33 
34 G_DEFINE_TYPE (
35 	EHTMLEditorLinkDialog,
36 	e_html_editor_link_dialog,
37 	E_TYPE_HTML_EDITOR_DIALOG);
38 
39 struct _EHTMLEditorLinkDialogPrivate {
40 	GtkWidget *url_edit;
41 	GtkWidget *label_edit;
42 
43 	GtkWidget *remove_link_button;
44 	GtkWidget *ok_button;
45 
46 	gboolean label_autofill;
47 };
48 
49 static void
html_editor_link_dialog_url_changed(EHTMLEditorLinkDialog * dialog)50 html_editor_link_dialog_url_changed (EHTMLEditorLinkDialog *dialog)
51 {
52 	if (dialog->priv->label_autofill &&
53 	    gtk_widget_is_sensitive (dialog->priv->label_edit)) {
54 		const gchar *text;
55 
56 		text = gtk_entry_get_text (
57 			GTK_ENTRY (dialog->priv->url_edit));
58 		gtk_entry_set_text (
59 			GTK_ENTRY (dialog->priv->label_edit), text);
60 	}
61 }
62 
63 static gboolean
html_editor_link_dialog_description_changed(EHTMLEditorLinkDialog * dialog)64 html_editor_link_dialog_description_changed (EHTMLEditorLinkDialog *dialog)
65 {
66 	const gchar *text;
67 
68 	text = gtk_entry_get_text (GTK_ENTRY (dialog->priv->label_edit));
69 	dialog->priv->label_autofill = (*text == '\0');
70 
71 	return FALSE;
72 }
73 
74 static void
html_editor_link_dialog_remove_link(EHTMLEditorLinkDialog * dialog)75 html_editor_link_dialog_remove_link (EHTMLEditorLinkDialog *dialog)
76 {
77 	EHTMLEditor *editor;
78 	EContentEditor *cnt_editor;
79 
80 	editor = e_html_editor_dialog_get_editor (E_HTML_EDITOR_DIALOG (dialog));
81 	cnt_editor = e_html_editor_get_content_editor (editor);
82 
83 	e_content_editor_selection_unlink (cnt_editor);
84 
85 	gtk_widget_hide (GTK_WIDGET (dialog));
86 }
87 
88 static void
html_editor_link_dialog_ok(EHTMLEditorLinkDialog * dialog)89 html_editor_link_dialog_ok (EHTMLEditorLinkDialog *dialog)
90 {
91 	EHTMLEditor *editor;
92 	EContentEditor *cnt_editor;
93 
94 	editor = e_html_editor_dialog_get_editor (E_HTML_EDITOR_DIALOG (dialog));
95 	cnt_editor = e_html_editor_get_content_editor (editor);
96 
97 	e_content_editor_link_set_properties (
98 		cnt_editor,
99 		gtk_entry_get_text (GTK_ENTRY (dialog->priv->url_edit)),
100 		gtk_entry_get_text (GTK_ENTRY (dialog->priv->label_edit)));
101 
102 	gtk_widget_hide (GTK_WIDGET (dialog));
103 }
104 
105 static gboolean
html_editor_link_dialog_entry_key_pressed(EHTMLEditorLinkDialog * dialog,GdkEventKey * event)106 html_editor_link_dialog_entry_key_pressed (EHTMLEditorLinkDialog *dialog,
107                                            GdkEventKey *event)
108 {
109 	/* We can't do things in key_released, because then you could not open
110 	 * this dialog from main menu by pressing enter on Insert->Link action */
111 	if (event->keyval == GDK_KEY_Return) {
112 		html_editor_link_dialog_ok (dialog);
113 		return TRUE;
114 	}
115 
116 	return FALSE;
117 }
118 
119 static void
html_editor_link_dialog_hide(GtkWidget * widget)120 html_editor_link_dialog_hide (GtkWidget *widget)
121 {
122 	EHTMLEditor *editor;
123 	EHTMLEditorLinkDialog *dialog;
124 	EContentEditor *cnt_editor;
125 
126 	dialog = E_HTML_EDITOR_LINK_DIALOG (widget);
127 	editor = e_html_editor_dialog_get_editor (E_HTML_EDITOR_DIALOG (dialog));
128 	cnt_editor = e_html_editor_get_content_editor (editor);
129 
130 	e_content_editor_on_dialog_close (cnt_editor, E_CONTENT_EDITOR_DIALOG_LINK);
131 
132 	/* Chain up to parent implementation */
133 	GTK_WIDGET_CLASS (e_html_editor_link_dialog_parent_class)->hide (widget);
134 }
135 
136 static void
html_editor_link_dialog_show(GtkWidget * widget)137 html_editor_link_dialog_show (GtkWidget *widget)
138 {
139 	EHTMLEditor *editor;
140 	EHTMLEditorLinkDialog *dialog;
141 	EContentEditor *cnt_editor;
142 	gchar *href = NULL, *text = NULL;
143 
144 	dialog = E_HTML_EDITOR_LINK_DIALOG (widget);
145 	editor = e_html_editor_dialog_get_editor (E_HTML_EDITOR_DIALOG (dialog));
146 	cnt_editor = e_html_editor_get_content_editor (editor);
147 
148 	/* Reset to default values */
149 	gtk_entry_set_text (GTK_ENTRY (dialog->priv->url_edit), "https://");
150 	gtk_entry_set_text (GTK_ENTRY (dialog->priv->label_edit), "");
151 	gtk_widget_set_sensitive (dialog->priv->label_edit, TRUE);
152 	gtk_widget_set_sensitive (dialog->priv->remove_link_button, TRUE);
153 
154 	dialog->priv->label_autofill = TRUE;
155 
156 	e_content_editor_on_dialog_open (cnt_editor, E_CONTENT_EDITOR_DIALOG_LINK);
157 
158 	e_content_editor_link_get_properties (cnt_editor, &href, &text);
159 	if (href && *href)
160 		gtk_entry_set_text (GTK_ENTRY (dialog->priv->url_edit), href);
161 	else
162 		gtk_widget_set_sensitive (dialog->priv->remove_link_button, FALSE);
163 
164 	g_free (href);
165 
166 	if (text && *text) {
167 		gtk_entry_set_text (GTK_ENTRY (dialog->priv->label_edit), text);
168 		dialog->priv->label_autofill = FALSE;
169 	}
170 	g_free (text);
171 
172 	/* Chain up to parent implementation */
173 	GTK_WIDGET_CLASS (e_html_editor_link_dialog_parent_class)->show (widget);
174 }
175 
176 static void
e_html_editor_link_dialog_class_init(EHTMLEditorLinkDialogClass * class)177 e_html_editor_link_dialog_class_init (EHTMLEditorLinkDialogClass *class)
178 {
179 	GtkWidgetClass *widget_class;
180 
181 	g_type_class_add_private (class, sizeof (EHTMLEditorLinkDialogPrivate));
182 
183 	widget_class = GTK_WIDGET_CLASS (class);
184 	widget_class->show = html_editor_link_dialog_show;
185 	widget_class->hide = html_editor_link_dialog_hide;
186 }
187 
188 static void
e_html_editor_link_dialog_init(EHTMLEditorLinkDialog * dialog)189 e_html_editor_link_dialog_init (EHTMLEditorLinkDialog *dialog)
190 {
191 	GtkGrid *main_layout;
192 	GtkBox *button_box;
193 	GtkWidget *widget;
194 
195 	dialog->priv = E_HTML_EDITOR_LINK_DIALOG_GET_PRIVATE (dialog);
196 
197 	main_layout = e_html_editor_dialog_get_container (E_HTML_EDITOR_DIALOG (dialog));
198 
199 	widget = e_url_entry_new ();
200 	gtk_widget_set_hexpand (widget, TRUE);
201 	gtk_grid_attach (main_layout, widget, 1, 0, 1, 1);
202 	g_signal_connect_swapped (
203 		widget, "notify::text",
204 		G_CALLBACK (html_editor_link_dialog_url_changed), dialog);
205 	g_signal_connect_swapped (
206 		widget, "key-press-event",
207 		G_CALLBACK (html_editor_link_dialog_entry_key_pressed), dialog);
208 	dialog->priv->url_edit = widget;
209 
210 	widget = gtk_label_new_with_mnemonic (_("_URL:"));
211 	gtk_label_set_justify (GTK_LABEL (widget), GTK_JUSTIFY_RIGHT);
212 	gtk_label_set_mnemonic_widget (GTK_LABEL (widget), dialog->priv->url_edit);
213 	gtk_grid_attach (main_layout, widget, 0, 0, 1, 1);
214 
215 	widget = gtk_entry_new ();
216 	gtk_widget_set_hexpand (widget, TRUE);
217 	gtk_grid_attach (main_layout, widget, 1, 1, 1, 1);
218 	g_signal_connect_swapped (
219 		widget, "key-release-event",
220 		G_CALLBACK (html_editor_link_dialog_description_changed), dialog);
221 	g_signal_connect_swapped (
222 		widget, "key-press-event",
223 		G_CALLBACK (html_editor_link_dialog_entry_key_pressed), dialog);
224 	dialog->priv->label_edit = widget;
225 
226 	widget = gtk_label_new_with_mnemonic (_("_Description:"));
227 	gtk_label_set_justify (GTK_LABEL (widget), GTK_JUSTIFY_RIGHT);
228 	gtk_label_set_mnemonic_widget (GTK_LABEL (widget), dialog->priv->label_edit);
229 	gtk_grid_attach (main_layout, widget, 0, 1, 1, 1);
230 
231 	button_box = e_html_editor_dialog_get_button_box (E_HTML_EDITOR_DIALOG (dialog));
232 
233 	widget = gtk_button_new_with_mnemonic (_("_Remove Link"));
234 	g_signal_connect_swapped (
235 		widget, "clicked",
236 		G_CALLBACK (html_editor_link_dialog_remove_link), dialog);
237 	gtk_box_pack_start (button_box, widget, FALSE, FALSE, 5);
238 	dialog->priv->remove_link_button = widget;
239 
240 	widget = gtk_button_new_with_mnemonic (_("_OK"));
241 	g_signal_connect_swapped (
242 		widget, "clicked",
243 		G_CALLBACK (html_editor_link_dialog_ok), dialog);
244 	gtk_box_pack_end (button_box, widget, FALSE, FALSE, 5);
245 	dialog->priv->ok_button = widget;
246 
247 	gtk_widget_show_all (GTK_WIDGET (main_layout));
248 }
249 
250 GtkWidget *
e_html_editor_link_dialog_new(EHTMLEditor * editor)251 e_html_editor_link_dialog_new (EHTMLEditor *editor)
252 {
253 	return GTK_WIDGET (
254 		g_object_new (
255 			E_TYPE_HTML_EDITOR_LINK_DIALOG,
256 			"editor", editor,
257 			"icon-name", "insert-link",
258 			"title", _("Link Properties"),
259 			NULL));
260 }
261