1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
8 #include <gdk/gdkkeysyms.h>
9 #include <gdk/gdktypes.h>
18 GtkWidget
*etext
, *ruser
, *euser
, *rurl
, *eurl
;
20 gint sel_type
, clip_type
;
21 gchar
*sel_input
, *clip_input
;
25 select_toggled(GtkToggleButton
*tb
, GtkWidget
*focus
) {
26 if (gtk_toggle_button_get_active(tb
))
27 gtk_widget_grab_focus(focus
);
31 entry_focus_cb(GtkWidget
*w
, GdkEventFocus
*e
, GtkToggleButton
*radio
) {
32 gtk_toggle_button_set_active(radio
, TRUE
);
36 radio_option(GSList
*g
, GtkWidget
**radio
, GtkWidget
**entry
,
37 const char *caption
, const char *label
, const char *initial
) {
40 vbox
= gtk_vbox_new(FALSE
, 0);
41 *radio
= gtk_radio_button_new_with_mnemonic(g
, caption
);
42 gtk_box_pack_start(GTK_BOX(vbox
), *radio
, FALSE
, FALSE
, 0);
44 *entry
= gtk_entry_new();
45 gtk_entry_set_activates_default(GTK_ENTRY(*entry
), TRUE
);
46 g_signal_connect(G_OBJECT(*entry
), "focus-in-event",
47 G_CALLBACK(entry_focus_cb
), *radio
);
49 gtk_entry_set_text(GTK_ENTRY(*entry
), initial
);
50 gtk_widget_set_size_request(*entry
, 75, -1);
51 g_signal_connect(G_OBJECT(*radio
), "toggled",
52 G_CALLBACK(select_toggled
), *entry
);
53 g_signal_connect(G_OBJECT(*radio
), "activate",
54 G_CALLBACK(select_toggled
), *entry
);
57 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 0);
58 gtk_box_pack_start(GTK_BOX(hbox
),
61 gtk_box_pack_start(GTK_BOX(hbox
), *entry
, TRUE
, TRUE
, 0);
62 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
64 gtk_box_pack_start(GTK_BOX(vbox
), *entry
, TRUE
, TRUE
, 0);
70 #if !GLIB_CHECK_VERSION(2,2,0)
72 g_str_has_prefix(const gchar
*str
, const gchar
*prefix
) {
73 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
79 link_magic(LinkRequest
*lr
) {
81 if (! ((lr
->clip_type
| lr
->sel_type
) & JAM_SELECTION_HAS_URL
))
84 xml_escape(&lr
->clip_input
);
86 /* boo for no list primitives in c */
87 if (lr
->clip_type
& JAM_SELECTION_HAS_URL
) {
88 a
= lr
->clip_input
; b
= lr
->sel_input
;
90 b
= lr
->clip_input
; a
= lr
->sel_input
;
93 return g_strdup_printf("<a href=\"%s\">%s</a>", a
, b
);
98 make_link_dialog(LinkDialog
*ld
, GtkWindow
*win
, gboolean livejournal
) {
103 ld
->dlg
= gtk_dialog_new_with_buttons(_("Make Link"),
104 win
, GTK_DIALOG_MODAL
,
105 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
106 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
108 gtk_dialog_set_default_response(GTK_DIALOG(ld
->dlg
), GTK_RESPONSE_OK
);
110 vbox
= gtk_vbox_new(FALSE
, 10);
112 ld
->etext
= gtk_entry_new();
113 gtk_entry_set_activates_default(GTK_ENTRY(ld
->etext
), TRUE
);
114 subbox
= labelled_box_new(_("Link _Text:"), ld
->etext
);
115 gtk_box_pack_start(GTK_BOX(vbox
), subbox
, FALSE
, FALSE
, 0);
117 gtk_box_pack_start(GTK_BOX(vbox
),
118 radio_option(NULL
, &ld
->rurl
, &ld
->eurl
, _("_URL:"), NULL
, ""),
121 rgroup
= gtk_radio_button_get_group(GTK_RADIO_BUTTON(ld
->rurl
));
124 gtk_box_pack_start(GTK_BOX(vbox
),
125 radio_option(rgroup
, &ld
->ruser
, &ld
->euser
,
126 _("_LiveJournal User:"), ".../users/", ""),
130 jam_dialog_set_contents(GTK_DIALOG(ld
->dlg
), vbox
);
135 if (!buf
|| buf
[0] == 0) return FALSE
;
136 /* just test a few of the obvious ones... */
137 if (g_str_has_prefix(buf
, "http://") ||
138 g_str_has_prefix(buf
, "https://") ||
139 g_str_has_prefix(buf
, "ftp://") ||
140 g_str_has_suffix(buf
, ".com") ||
141 g_str_has_suffix(buf
, ".net") ||
142 g_str_has_suffix(buf
, ".org"))
148 prepopulate_fields(LinkDialog
*ld
, char *bufsel
) {
149 GtkClipboard
*clipboard
;
150 char *clipsel
, *url
= NULL
;
152 clipboard
= gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
);
153 clipsel
= gtk_clipboard_wait_for_text(clipboard
);
155 if (bufsel
&& has_url(bufsel
)) {
157 } else if (clipsel
&& has_url(clipsel
)) {
162 gtk_entry_set_text(GTK_ENTRY(ld
->etext
), bufsel
);
165 if (g_str_has_prefix(url
, "http://") ||
166 g_str_has_prefix(url
, "https://") ||
167 g_str_has_prefix(url
, "ftp://")) {
170 niceurl
= g_strdup_printf("http://%s", url
);
172 gtk_entry_set_text(GTK_ENTRY(ld
->eurl
), niceurl
);
181 get_link(LinkDialog
*ld
, JamAccount
*acc
) {
182 char *url
, *user
, *text
;
185 url
= gtk_editable_get_chars(GTK_EDITABLE(ld
->eurl
), 0, -1);
187 user
= gtk_editable_get_chars(GTK_EDITABLE(ld
->euser
), 0, -1);
189 text
= gtk_editable_get_chars(GTK_EDITABLE(ld
->etext
), 0, -1);
192 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ld
->rurl
))) {
193 /* build a "url" link. */
194 link
= g_strdup_printf("<noindex><a href=\"%s\" rel=\"nofollow\" rel=\"noreferrer\" style=\"text-decoration:overline\">%s</a></noindex>", url
, text
);
195 } else if (ld
->ruser
&& gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ld
->ruser
))) {
196 /* build a "friends" link. */
197 LJServer
*server
= jam_account_lj_get_server(JAM_ACCOUNT_LJ(acc
));
199 url
= g_strdup(server
->url
);
201 link
= g_strdup_printf("<noindex><a href=\"%s/users/%s\" rel=\"nofollow\" rel=\"noreferrer\" style=\"text-decoration:overline\">%s</a></noindex>",
212 link_dialog_run(GtkWindow
*win
, JamDoc
*doc
) {
213 STACK(LinkDialog
, ld
);
214 GtkTextBuffer
*buffer
;
215 GtkTextIter start
, end
;
219 JamAccount
*acc
= jam_doc_get_account(doc
);
220 make_link_dialog(ld
, win
, JAM_ACCOUNT_IS_LJ(acc
));
222 buffer
= jam_doc_get_text_buffer(doc
);
223 if (gtk_text_buffer_get_selection_bounds(buffer
, &start
, &end
))
224 sel
= gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
225 prepopulate_fields(ld
, sel
);
228 if (gtk_dialog_run(GTK_DIALOG(ld
->dlg
)) != GTK_RESPONSE_OK
) {
229 gtk_widget_destroy(ld
->dlg
);
233 link
= get_link(ld
, acc
);
235 gtk_widget_destroy(ld
->dlg
);
238 gtk_text_buffer_begin_user_action(buffer
);
239 gtk_text_buffer_delete(buffer
, &start
, &end
);
240 gtk_text_buffer_insert(buffer
, &start
, link
, -1);
241 gtk_text_buffer_end_user_action(buffer
);