removed alot of warnings
[k8lowj.git] / src / imagelink.c
blob26c44b6cbcf1c7d31945663e73c5105289889882
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2004 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
7 #include "gtk-all.h"
8 #include "util-gtk.h"
9 #include "conf.h"
10 #include "jamdoc.h"
11 #include "util.h"
13 typedef struct {
14 GtkWidget *dlg;
15 GtkWidget *url, *get, *width, *height;
16 } ImageDlg;
18 static void
19 url_changed_cb(ImageDlg *idlg) {
20 const char *text = gtk_entry_get_text(GTK_ENTRY(idlg->url));
21 gtk_widget_set_sensitive(idlg->get, text && text[0]);
22 gtk_dialog_set_response_sensitive(GTK_DIALOG(idlg->dlg),
23 GTK_RESPONSE_OK, text && text[0]);
26 static void
27 size_cb(ImageDlg *idlg, gint width, gint height) {
28 char *text;
29 text = g_strdup_printf("%d", width);
30 gtk_entry_set_text(GTK_ENTRY(idlg->width), text);
31 g_free(text);
32 text = g_strdup_printf("%d", height);
33 gtk_entry_set_text(GTK_ENTRY(idlg->height), text);
34 g_free(text);
37 static void
38 get_url_cb(ImageDlg *idlg) {
39 GdkPixbufLoader *loader;
40 const char *url;
41 GString *img;
42 GError *err = NULL;
44 /* XXX suboptimal: we retrieve the entire image
45 * when we only need a few bytes. */
46 url = gtk_entry_get_text(GTK_ENTRY(idlg->url));
47 img = net_get_run(_("Retrieving Image..."), GTK_WINDOW(idlg->dlg), url);
49 if (!img)
50 return;
52 loader = gdk_pixbuf_loader_new();
53 g_signal_connect_swapped(G_OBJECT(loader), "size-prepared",
54 G_CALLBACK(size_cb), idlg);
55 if (!gdk_pixbuf_loader_write(loader, (unsigned char*)img->str, img->len, &err)) {
56 jam_warning(GTK_WINDOW(idlg->dlg),
57 _("Error loading image: %s."), err->message);
58 g_error_free(err);
60 gdk_pixbuf_loader_close(loader, NULL);
61 g_object_unref(G_OBJECT(loader));
64 static void
65 make_dialog(ImageDlg *idlg, GtkWindow *win) {
66 GtkWidget *vbox, *hbox, *dimbox;
67 GtkSizeGroup *sg;
69 idlg->dlg = gtk_dialog_new_with_buttons(_("Insert Image"),
70 win, GTK_DIALOG_MODAL,
71 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
72 GTK_STOCK_OK, GTK_RESPONSE_OK,
73 NULL);
74 gtk_dialog_set_default_response(GTK_DIALOG(idlg->dlg),
75 GTK_RESPONSE_OK);
76 g_signal_connect_swapped(G_OBJECT(idlg->dlg), "show",
77 G_CALLBACK(url_changed_cb), idlg);
79 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
81 vbox = gtk_vbox_new(FALSE, 12);
83 idlg->url = gtk_entry_new();
84 g_signal_connect_swapped(G_OBJECT(idlg->url), "changed",
85 G_CALLBACK(url_changed_cb), idlg);
86 gtk_box_pack_start(GTK_BOX(vbox),
87 labelled_box_new_sg(_("_URL:"), idlg->url, sg),
88 FALSE, FALSE, 0);
90 hbox = gtk_hbox_new(FALSE, 6);
92 dimbox = gtk_vbox_new(FALSE, 6);
93 idlg->width = gtk_entry_new();
94 gtk_entry_set_width_chars(GTK_ENTRY(idlg->width), 6);
95 gtk_box_pack_start(GTK_BOX(dimbox),
96 labelled_box_new_sg(_("_Width:"), idlg->width, sg),
97 FALSE, FALSE, 0);
99 idlg->height = gtk_entry_new();
100 gtk_entry_set_width_chars(GTK_ENTRY(idlg->height), 6);
101 gtk_box_pack_start(GTK_BOX(dimbox),
102 labelled_box_new_sg(_("_Height:"), idlg->height, sg),
103 FALSE, FALSE, 0);
105 gtk_box_pack_start(GTK_BOX(hbox), dimbox, FALSE, FALSE, 0);
107 dimbox = gtk_vbox_new(FALSE, 6);
108 idlg->get = gtk_button_new_with_mnemonic(_("_Retrieve Dimensions from Network"));
109 g_signal_connect_swapped(G_OBJECT(idlg->get), "clicked",
110 G_CALLBACK(get_url_cb), idlg);
111 gtk_box_pack_start(GTK_BOX(dimbox), idlg->get, TRUE, FALSE, 0);
113 gtk_box_pack_end(GTK_BOX(hbox), dimbox, FALSE, FALSE, 0);
115 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
117 jam_dialog_set_contents(GTK_DIALOG(idlg->dlg), vbox);
120 static gboolean
121 is_image_link(const char *str) {
122 if (!str)
123 return FALSE;
124 return (g_str_has_prefix(str, "http://") ||
125 g_str_has_prefix(str, "ftp://"));
128 void
129 image_dialog_run(GtkWindow *win, JamDoc *doc) {
130 STACK(ImageDlg, idlg);
131 GtkTextBuffer *buffer;
132 GtkTextIter start, end;
133 char *sel = NULL, *url = NULL;
134 const char *data;
135 int width = -1, height = -1;
137 make_dialog(idlg, win);
139 buffer = jam_doc_get_text_buffer(doc);
140 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) {
141 sel = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
142 if (is_image_link(sel))
143 gtk_entry_set_text(GTK_ENTRY(idlg->url), sel);
144 } else {
145 char *clip;
146 clip = jam_clipboard_wait_for_text_timeout(
147 gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
148 50 /* ms */);
149 if (is_image_link(clip))
150 gtk_entry_set_text(GTK_ENTRY(idlg->url), clip);
151 g_free(clip);
154 if (gtk_dialog_run(GTK_DIALOG(idlg->dlg)) == GTK_RESPONSE_OK) {
155 if (sel)
156 gtk_text_buffer_delete(buffer, &start, &end);
157 else
158 gtk_text_buffer_get_iter_at_mark(buffer, &start,
159 gtk_text_buffer_get_insert(buffer));
161 url = g_strdup(gtk_entry_get_text(GTK_ENTRY(idlg->url)));
162 xml_escape(&url);
163 gtk_text_buffer_insert(buffer, &start, "<img src='", -1);
164 gtk_text_buffer_insert(buffer, &start, url, -1);
165 gtk_text_buffer_insert(buffer, &start, "'", -1);
166 g_free(url);
168 data = gtk_entry_get_text(GTK_ENTRY(idlg->width));
169 width = atoi(data);
171 data = gtk_entry_get_text(GTK_ENTRY(idlg->height));
172 height = atoi(data);
174 if (width > 0) {
175 char *data = g_strdup_printf(" width='%d'", width);
176 gtk_text_buffer_insert(buffer, &start, data, -1);
177 g_free(data);
179 if (height > 0) {
180 char *data = g_strdup_printf(" height='%d'", height);
181 gtk_text_buffer_insert(buffer, &start, data, -1);
182 g_free(data);
184 gtk_text_buffer_insert(buffer, &start, " />", -1);
187 g_free(sel);
189 gtk_widget_destroy(idlg->dlg);