1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
10 #include "network-internal.h"
15 static void statuscb (NetStatusType status
, gpointer statusdata
, gpointer data
) {
16 ProgressWindow
*pw
= PROGRESS_WINDOW(data
);
17 NetStatusProgress
*progress
;
20 case NET_STATUS_BEGIN
:
21 progress_window_set_cancel_cb(pw
, net_mainloop_cancel
, statusdata
);
22 progress_window_set_progress(pw
, 0);
24 case NET_STATUS_PROGRESS
:
25 progress
= (NetStatusProgress
*) statusdata
;
26 if (progress
->current
> 0 && progress
->total
> 0) {
27 fraction
= (float)progress
->current
/progress
->total
;
28 progress_window_set_progress(pw
, fraction
);
32 progress_window_set_cancel_cb(pw
, NULL
, NULL
);
33 progress_window_set_progress(pw
, -1);
36 g_warning("unhandled network status %d\n", status
);
48 static void statuscb_ctx (NetStatusType status
, gpointer statusdata
, gpointer data
) {
49 NetContextGtk
*ctx
= data
;
50 statuscb(status
, statusdata
, ctx
->pw
);
54 static void ctx_gtk_title (NetContext
*ctx
, const char *title
) {
55 ProgressWindow
*pw
= PROGRESS_WINDOW(((NetContextGtk
*)ctx
)->pw
);
56 progress_window_set_title(pw
, title
);
60 static void ctx_gtk_progress_str (NetContext
*ctx
, const char *str
) {
61 ProgressWindow
*pw
= PROGRESS_WINDOW(((NetContextGtk
*)ctx
)->pw
);
62 progress_window_set_text(pw
, str
);
66 static void ctx_gtk_error (NetContext
*ctx
, GError
*err
) {
67 ProgressWindow
*pw
= PROGRESS_WINDOW(((NetContextGtk
*)ctx
)->pw
);
68 if (!g_error_matches(err
, NET_ERROR
, NET_ERROR_CANCELLED
)) progress_window_show_error(pw
, "%s", err
->message
);
72 static GString
*ctx_gtk_http (NetContext
*ctx
, const char *url
, GSList
*headers
, GString
*post
, GError
**err
) {
73 if (conf
.options
.nofork
) return net_post_blocking(url
, NULL
, post
, statuscb_ctx
, ctx
, err
);
74 return net_post_mainloop(url
, NULL
, post
, statuscb_ctx
, ctx
, err
);
78 NetContext
*net_ctx_gtk_new (GtkWindow
*parent
, const char *title
) {
79 NetContext
*ctx
= (NetContext
*)g_new0(NetContextGtk
, 1);
80 NetContextGtk
*gtkctx
= (NetContextGtk
*)ctx
;
81 ctx
->title
= ctx_gtk_title
;
82 ctx
->progress_str
= ctx_gtk_progress_str
;
83 //XXX ctx->progress_int = ctx_cmdline_print_string;
84 ctx
->http
= ctx_gtk_http
;
85 ctx
->error
= ctx_gtk_error
;
86 gtkctx
->pw
= progress_window_new(parent
, title
);
87 gtk_widget_show(gtkctx
->pw
);
92 void net_ctx_gtk_free (NetContext
*ctx
) {
93 NetContextGtk
*gtkctx
= (NetContextGtk
*)ctx
;
94 gtk_widget_destroy(gtkctx
->pw
);
99 gboolean
net_window_run_verb (ProgressWindow
*pw
, LJVerb
*verb
) {
102 gtk_widget_show(GTK_WIDGET(pw
));
103 ret
= net_verb_run_internal(verb
, statuscb
, pw
, &err
);
105 if (!g_error_matches(err
, NET_ERROR
, NET_ERROR_CANCELLED
)) progress_window_show_error(pw
, "%s", err
->message
);
113 gboolean
net_window_run_post (ProgressWindow
*pw
, const char *url
, GSList
*headers
, GString
*request
, GString
*response
, GError
**err
) {
116 gtk_widget_show(GTK_WIDGET(pw
));
117 res
= net_post_mainloop(url
, headers
, request
, statuscb
, pw
, err
);
119 g_string_append_len(response
, res
->str
, res
->len
);
124 g_string_free(res
, TRUE
);
129 gboolean
net_verb_run (LJVerb
*verb
, const char *title
, GtkWindow
*parent
) {
132 pw
= progress_window_new(parent
, title
);
133 ret
= net_window_run_verb(PROGRESS_WINDOW(pw
), verb
);
134 gtk_widget_destroy(pw
);
139 GString
*net_get_run (const char *title
, GtkWindow
*parent
, const char *url
) {
143 pw
= progress_window_new(parent
, title
);
144 gtk_widget_show(GTK_WIDGET(pw
));
145 ret
= net_post_mainloop(url
, NULL
, NULL
, statuscb
, pw
, &err
);
147 if (!g_error_matches(err
, NET_ERROR
, NET_ERROR_CANCELLED
)) progress_window_show_error(PROGRESS_WINDOW(pw
), "%s", err
->message
);
150 gtk_widget_destroy(pw
);