1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
12 #include "network-internal.h"
17 statuscb(NetStatusType status
, gpointer statusdata
, gpointer data
) {
18 ProgressWindow
*pw
= PROGRESS_WINDOW(data
);
19 NetStatusProgress
*progress
;
23 case NET_STATUS_BEGIN
:
24 progress_window_set_cancel_cb(pw
, net_mainloop_cancel
, statusdata
);
25 progress_window_set_progress(pw
, 0);
27 case NET_STATUS_PROGRESS
:
28 progress
= (NetStatusProgress
*)statusdata
;
29 if (progress
->current
> 0 && progress
->total
> 0) {
30 fraction
= (float)progress
->current
/ progress
->total
;
31 progress_window_set_progress(pw
, fraction
);
35 progress_window_set_cancel_cb(pw
, NULL
, NULL
);
36 progress_window_set_progress(pw
, -1);
39 g_warning("unhandled network status %d\n", status
);
50 statuscb_ctx(NetStatusType status
, gpointer statusdata
, gpointer data
) {
51 NetContextGtk
*ctx
= data
;
52 statuscb(status
, statusdata
, ctx
->pw
);
56 ctx_gtk_title(NetContext
*ctx
, const char *title
) {
57 ProgressWindow
*pw
= PROGRESS_WINDOW(((NetContextGtk
*)ctx
)->pw
);
58 progress_window_set_title(pw
, title
);
61 ctx_gtk_progress_str(NetContext
*ctx
, const char *str
) {
62 ProgressWindow
*pw
= PROGRESS_WINDOW(((NetContextGtk
*)ctx
)->pw
);
63 progress_window_set_text(pw
, str
);
67 ctx_gtk_error(NetContext
*ctx
, GError
*err
) {
68 ProgressWindow
*pw
= PROGRESS_WINDOW(((NetContextGtk
*)ctx
)->pw
);
69 if (!g_error_matches(err
, NET_ERROR
, NET_ERROR_CANCELLED
))
70 progress_window_show_error(pw
, err
->message
);
74 ctx_gtk_http(NetContext
*ctx
, const char *url
, GSList
*headers
,
75 GString
*post
, GError
**err
) {
76 if (conf
.options
.nofork
)
77 return net_post_blocking(url
, NULL
, post
, statuscb_ctx
, ctx
, err
);
79 return net_post_mainloop(url
, NULL
, post
, statuscb_ctx
, ctx
, err
);
83 net_ctx_gtk_new(GtkWindow
*parent
, const char *title
) {
84 NetContext
*ctx
= (NetContext
*)g_new0(NetContextGtk
, 1);
85 NetContextGtk
*gtkctx
= (NetContextGtk
*)ctx
;
86 ctx
->title
= ctx_gtk_title
;
87 ctx
->progress_str
= ctx_gtk_progress_str
;
88 //XXX ctx->progress_int = ctx_cmdline_print_string;
89 ctx
->http
= ctx_gtk_http
;
90 ctx
->error
= ctx_gtk_error
;
91 gtkctx
->pw
= progress_window_new(parent
, title
);
92 gtk_widget_show(gtkctx
->pw
);
97 net_ctx_gtk_free(NetContext
*ctx
) {
98 NetContextGtk
*gtkctx
= (NetContextGtk
*)ctx
;
99 gtk_widget_destroy(gtkctx
->pw
);
104 net_window_run_verb(ProgressWindow
*pw
, LJVerb
*verb
) {
108 gtk_widget_show(GTK_WIDGET(pw
));
109 ret
= net_verb_run_internal(verb
, statuscb
, pw
, &err
);
112 if (!g_error_matches(err
, NET_ERROR
, NET_ERROR_CANCELLED
))
113 progress_window_show_error(pw
, err
->message
);
122 net_window_run_post(ProgressWindow
*pw
, const char *url
, GSList
*headers
,
123 GString
*request
, GString
*response
,
128 gtk_widget_show(GTK_WIDGET(pw
));
129 res
= net_post_mainloop(url
, headers
, request
, statuscb
, pw
, err
);
131 g_string_append_len(response
, res
->str
, res
->len
);
136 g_string_free(res
, TRUE
);
142 net_verb_run(LJVerb
*verb
, const char *title
, GtkWindow
*parent
) {
146 pw
= progress_window_new(parent
, title
);
147 ret
= net_window_run_verb(PROGRESS_WINDOW(pw
), verb
);
148 gtk_widget_destroy(pw
);
154 net_get_run(const char *title
, GtkWindow
*parent
,
160 pw
= progress_window_new(parent
, title
);
161 gtk_widget_show(GTK_WIDGET(pw
));
163 ret
= net_post_mainloop(url
, NULL
, NULL
, statuscb
, pw
, &err
);
165 if (!g_error_matches(err
, NET_ERROR
, NET_ERROR_CANCELLED
))
166 progress_window_show_error(PROGRESS_WINDOW(pw
), err
->message
);
169 gtk_widget_destroy(pw
);