From 8c02ec7f117d4168587380d0bebcbd77aa700e9d Mon Sep 17 00:00:00 2001 From: ketmar Date: Sat, 14 Sep 2013 10:26:21 +0300 Subject: [PATCH] preview.*: cosmetix --- src/preview.c | 237 ++++++++++++++++++++++++---------------------------------- src/preview.h | 17 +++-- 2 files changed, 108 insertions(+), 146 deletions(-) diff --git a/src/preview.c b/src/preview.c index 9fd7c46..80efe00 100644 --- a/src/preview.c +++ b/src/preview.c @@ -4,190 +4,162 @@ * vim: tabstop=4 shiftwidth=4 noexpandtab : */ #ifdef HAVE_GTKHTML330 - #include "gtk-all.h" #include "util-gtk.h" + #include -#include -#include +#include #include #include +#include +#include -#include "jam.h" #include "icons.h" +#include "inline.h" +#include "jam.h" #include "preview.h" #include "spawn.h" -#include "inline.h" -#define RESPONSE_UPDATE 1 +#define RESPONSE_UPDATE (1) + +#define HTMLPREVIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), html_preview_get_type(), HTMLPreview)) + +static GType html_preview_get_type (void); -#define HTMLPREVIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), html_preview_get_type(), HTMLPreview)) -static GType html_preview_get_type(void); +static void url_requested (GtkHTML *html, const char *url, GtkHTMLStream *handle); +static void link_clicked (GtkHTML *html, const char *url, gpointer data); -static void url_requested(GtkHTML *html, const char *url, GtkHTMLStream *handle); -static void link_clicked(GtkHTML *html, const char *url, gpointer data); -static void -html_preview_init(HTMLPreview *hp) { - gtk_html_construct((gpointer)hp); - g_signal_connect(G_OBJECT(hp), "url_requested", - G_CALLBACK(url_requested), NULL); - g_signal_connect(G_OBJECT(hp), "link_clicked", - G_CALLBACK(link_clicked), NULL); +static void html_preview_init (HTMLPreview *hp) { + gtk_html_construct((gpointer) hp); + g_signal_connect(G_OBJECT(hp), "url_requested", G_CALLBACK(url_requested), NULL); + g_signal_connect(G_OBJECT(hp), "link_clicked", G_CALLBACK(link_clicked), NULL); } -GtkWidget* -html_preview_new(GetEntryFunc get_entry_f, gpointer get_entry_data) { + +GtkWidget *html_preview_new (GetEntryFunc get_entry_f, gpointer get_entry_data) { HTMLPreview *hp = HTMLPREVIEW(g_object_new(html_preview_get_type(), NULL)); - hp->get_entry = get_entry_f; + hp->get_entry = get_entry_f; hp->get_entry_data = get_entry_data; return GTK_WIDGET(hp); } -GType -html_preview_get_type(void) { + +GType html_preview_get_type (void) { static GType hp_type = 0; if (!hp_type) { const GTypeInfo hp_info = { - sizeof (GtkHTMLClass), + sizeof(GtkHTMLClass), NULL, NULL, NULL, NULL, NULL, - sizeof (HTMLPreview), + sizeof(HTMLPreview), 0, (GInstanceInitFunc) html_preview_init, }; - hp_type = g_type_register_static(GTK_TYPE_HTML, - "HTMLPreview", &hp_info, 0); + hp_type = g_type_register_static(GTK_TYPE_HTML, "HTMLPreview", &hp_info, 0); } return hp_type; } -static void -link_clicked(GtkHTML *html, const char *url, gpointer data) { + +static void link_clicked (GtkHTML *html, const char *url, gpointer data) { spawn_url(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(html))), url); } -static void -url_requested(GtkHTML *html, const char *url, GtkHTMLStream *handle) { -#define PROVIDE_IMAGE(name) \ - if (g_ascii_strcasecmp(url, "logjam:" #name) == 0) { \ - gtk_html_write(html, handle, \ - (char*)logjam_##name##_png, sizeof(logjam_##name##_png)); } +#define PROVIDE_IMAGE(name) if (g_ascii_strcasecmp(url, "logjam:" #name) == 0) { gtk_html_write(html, handle, (const char *)logjam_##name##_png, sizeof(logjam_##name##_png)); } else + +static void url_requested (GtkHTML *html, const char *url, GtkHTMLStream *handle) { PROVIDE_IMAGE(ljuser) - else PROVIDE_IMAGE(ljcomm) - else PROVIDE_IMAGE(protected) - else PROVIDE_IMAGE(private) - else { + { gtk_html_end(html, handle, GTK_HTML_STREAM_ERROR); return; } gtk_html_end(html, handle, GTK_HTML_STREAM_OK); } -/* XXX not utf8 safe, but i think we're ok because we depend on ASCII - * characters in HTML and usernames. */ -static char* -parse_ljtag(char *src, GString *dst) { - gboolean isuser; - char *start, *end; +#undef PROVIDE_IMAGE + - char *p = src + 3; - while (*p == ' ') p++; +/* XXX not utf8 safe, but i think we're ok because we depend on ASCII characters in HTML and usernames. */ +static const char *parse_ljtag (const char *src, GString *dst) { + gboolean isuser; + const char *start, *end; + const char *p = src + 3; + while (*p && isspace(*p)) ++p; - if (*p == 'u') - isuser = TRUE; - else if (*p == 'c') - isuser = FALSE; - else - return src; + if (*p == 'u') isuser = TRUE; + else if (*p == 'c') isuser = FALSE; + else return src; /* skip forward to equals sign. */ while (*p != '=') { if (*p == 0) return src; - p++; + ++p; } - p++; - while (*p == ' ') p++; /* skip spaces. */ - if (*p == '\'' || *p == '"') /* optional quote. */ - p++; + ++p; + while (*p && isspace(*p)) ++p; /* skip spaces. */ + if (*p == '\'' || *p == '"') ++p; /* optional quote. */ start = p; while (*p != '\'' && *p != '"' && *p != '>' && *p != ' ' && *p != '/') { if (*p == 0) return src; - p++; + ++p; } end = p; - if (*p == '\'' || *p == '"') p++; - while (*p == ' ') p++; - if (*p == '/') p++; - while (*p == ' ') p++; - if (*p != '>') - return src; - p++; - - g_string_append_printf(dst, "" - "" - "", isuser ? "ljuser" : "ljcomm"); - g_string_append_len(dst, start, end-start); + if (*p == '\'' || *p == '"') ++p; + while (*p && isspace(*p)) ++p; + if (*p == '/') ++p; + while (*p && isspace(*p)) ++p; + if (*p != '>') return src; + ++p; + + g_string_append_printf(dst, "" "" "", (isuser ? "ljuser" : "ljcomm")); + g_string_append_len(dst, start, end - start); g_string_append(dst, ""); return p; } -static GString* -entry_prepare_preview(LJEntry *entry) { + +static GString *entry_prepare_preview (LJEntry *entry) { GString *str = g_string_new(NULL); - gchar *event; + const gchar *event; gboolean has_time, has_security; - if (!entry) - return str; + if (!entry) return str; - has_time = entry->time.tm_year > 0; - has_security = entry->security.type != LJ_SECURITY_PUBLIC; + has_time = (entry->time.tm_year > 0); + has_security = (entry->security.type != LJ_SECURITY_PUBLIC); if (has_security || entry->subject || has_time) { g_string_append(str, ""); if (has_security || entry->subject) { g_string_append(str, ""); } - if (has_time) { - g_string_append_printf(str, - "", asctime(&entry->time)); - } + if (has_time) g_string_append_printf(str, "", asctime(&entry->time)); g_string_append(str, "
"); if (has_security) { - char *img; - if (entry->security.type == LJ_SECURITY_PRIVATE) - img = "private"; - else - img = "protected"; - g_string_append_printf(str, - "", img); + char *img = (entry->security.type == LJ_SECURITY_PRIVATE ? "private" : "protected"); + g_string_append_printf(str, "", img); } - if (entry->subject) - g_string_append_printf(str, "%s", entry->subject); + if (entry->subject) g_string_append_printf(str, "%s", entry->subject); g_string_append(str, "%s%s


"); } if (entry->mood || entry->music || entry->location || entry->taglist) { - if (entry->mood) - g_string_append_printf(str, "%s: %s
", _("Current Mood"), entry->mood); - if (entry->music) - g_string_append_printf(str, "%s: %s
", _("Current Music"), entry->music); - if (entry->location) - g_string_append_printf(str, "%s: %s
", _("Current Location"), entry->location); - if (entry->taglist) - g_string_append_printf(str, "%s: %s
", _("Tags"), entry->taglist); + if (entry->mood) g_string_append_printf(str, "%s: %s
", _("Current Mood"), entry->mood); + if (entry->music) g_string_append_printf(str, "%s: %s
", _("Current Music"), entry->music); + if (entry->location) g_string_append_printf(str, "%s: %s
", _("Current Location"), entry->location); + if (entry->taglist) g_string_append_printf(str, "%s: %s
", _("Tags"), entry->taglist); g_string_append(str, "
"); } @@ -196,11 +168,11 @@ entry_prepare_preview(LJEntry *entry) { if (*event == '\n' && !entry->preformatted) { g_string_append_len(str, "
", 5); } else if (event[0] == '<' && - /* check for lj user / lj comm. */ - /* this is not good html parsing, but it should be enough. - * besides, the whole tag is yucky html. :P */ - event[1] == 'l' && event[2] == 'j' && event[3] == ' ') { - char *p = parse_ljtag(event, str); + /* check for lj user / lj comm. */ + /* this is not good html parsing, but it should be enough. + * besides, the whole tag is yucky html. :P */ + event[1] == 'l' && event[2] == 'j' && (event[3] == ' ' || (event[3] == 'r' && event[4] == ' '))) { + const char *p = parse_ljtag(event, str); if (p != event) { event = p-1; continue; @@ -213,64 +185,50 @@ entry_prepare_preview(LJEntry *entry) { return str; } -void -preview_update(HTMLPreview *hp) { - LJEntry *entry; - GString *str; - entry = hp->get_entry(hp->get_entry_data); - if (!entry) - return; +void preview_update (HTMLPreview *hp) { + GString *str; + LJEntry *entry = hp->get_entry(hp->get_entry_data); + if (!entry) return; str = entry_prepare_preview(entry); /* gtkhtml whines if you give it an empty string. */ - if (str->str[0] == '\0') - g_string_append_c(str, ' '); - + if (str->str[0] == '\0') g_string_append_c(str, ' '); gtk_html_load_from_string(GTK_HTML(hp), str->str, -1); - g_string_free(str, TRUE); lj_entry_free(entry); } -static LJEntry* -get_entry_from_jw(JamWin *jw) { + +static LJEntry *get_entry_from_jw (JamWin *jw) { return jam_doc_get_entry(jw->doc); } -static void -response_cb(GtkWidget *dlg, gint response, PreviewUI *pui) { - if (response == RESPONSE_UPDATE) - preview_update(pui->html); - else - gtk_widget_destroy(dlg); + +static void response_cb (GtkWidget *dlg, gint response, PreviewUI *pui) { + if (response == RESPONSE_UPDATE) preview_update(pui->html); else gtk_widget_destroy(dlg); } -static void -make_window(PreviewUI *pui) { + +static void make_window (PreviewUI *pui) { pui->win = gtk_dialog_new_with_buttons(_("HTML Preview"), - GTK_WINDOW(pui->jw), - GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_NO_SEPARATOR, - _("_Update"), RESPONSE_UPDATE, - GTK_STOCK_CLOSE, GTK_RESPONSE_OK, - NULL); + GTK_WINDOW(pui->jw), + GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR, + _("_Update"), RESPONSE_UPDATE, GTK_STOCK_CLOSE, GTK_RESPONSE_OK, NULL); gtk_window_set_default_size(GTK_WINDOW(pui->win), 500, 400); geometry_tie(GTK_WIDGET(pui->win), GEOM_PREVIEW); /* this reportedly breaks some wms. * gtk_window_set_type_hint(GTK_WINDOW(pui->win), - GDK_WINDOW_TYPE_HINT_UTILITY);*/ - g_signal_connect(G_OBJECT(pui->win), "response", - G_CALLBACK(response_cb), pui); + GDK_WINDOW_TYPE_HINT_UTILITY);*/ + g_signal_connect(G_OBJECT(pui->win), "response", G_CALLBACK(response_cb), pui); - pui->html = HTMLPREVIEW(html_preview_new( - (GetEntryFunc)get_entry_from_jw, pui->jw)); + pui->html = HTMLPREVIEW(html_preview_new((GetEntryFunc) get_entry_from_jw, pui->jw)); preview_update(pui->html); - jam_dialog_set_contents(GTK_DIALOG(pui->win), - scroll_wrap(GTK_WIDGET(pui->html))); + jam_dialog_set_contents(GTK_DIALOG(pui->win), scroll_wrap(GTK_WIDGET(pui->html))); } -void -preview_ui_show(JamWin *jw) { + +void preview_ui_show (JamWin *jw) { PreviewUI *pui; if (jw->preview) { @@ -283,8 +241,7 @@ preview_ui_show(JamWin *jw) { jw->preview = pui = g_new0(PreviewUI, 1); pui->jw = jw; make_window(pui); - g_signal_connect_swapped(G_OBJECT(pui->win), "destroy", - G_CALLBACK(g_free), pui); + g_signal_connect_swapped(G_OBJECT(pui->win), "destroy", G_CALLBACK(g_free), pui); gtk_widget_show_all(pui->win); g_object_add_weak_pointer(G_OBJECT(pui->win), &jw->preview); diff --git a/src/preview.h b/src/preview.h index 68104f8..a8396b2 100644 --- a/src/preview.h +++ b/src/preview.h @@ -5,14 +5,17 @@ #define __LOGJAM_PREVIEW_H__ #include + #include "liblj/livejournal.h" + #include "jam.h" typedef struct _HTMLPreview HTMLPreview; -typedef LJEntry *(*GetEntryFunc) (HTMLPreview * hp); + +typedef LJEntry *(*GetEntryFunc) (HTMLPreview *hp); struct _HTMLPreview { - GtkHTML html; /* parent */ + GtkHTML html; /* parent */ GetEntryFunc get_entry; gpointer get_entry_data; }; @@ -24,8 +27,10 @@ struct _PreviewUI { JamWin *jw; }; -GtkWidget *html_preview_new(GetEntryFunc get_entry, gpointer get_entry_data); -void preview_ui_show(JamWin * jw); -void preview_update(HTMLPreview * hp); -#endif /* __html_preview_h__ */ +extern GtkWidget *html_preview_new (GetEntryFunc get_entry, gpointer get_entry_data); +extern void preview_ui_show (JamWin *jw); +extern void preview_update (HTMLPreview *hp); + + +#endif -- 2.11.4.GIT