about.c: cosmetix
[k8lowj.git] / src / network-gtk.c
blob910829ee0acca84ad4a5b20beb1ebad127650a17
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
6 #ifdef HAVE_GTK
8 #include "gtk-all.h"
10 #include "conf.h"
11 #include "network.h"
12 #include "network-internal.h"
13 #include "throbber.h"
14 #include "progress.h"
16 static void
17 statuscb(NetStatusType status, gpointer statusdata, gpointer data) {
18 ProgressWindow *pw = PROGRESS_WINDOW(data);
19 NetStatusProgress *progress;
20 float fraction = 0;
22 switch (status) {
23 case NET_STATUS_BEGIN:
24 progress_window_set_cancel_cb(pw, net_mainloop_cancel, statusdata);
25 progress_window_set_progress(pw, 0);
26 break;
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);
33 break;
34 case NET_STATUS_DONE:
35 progress_window_set_cancel_cb(pw, NULL, NULL);
36 progress_window_set_progress(pw, -1);
37 break;
38 default:
39 g_warning("unhandled network status %d\n", status);
43 typedef struct {
44 NetContext ctx;
45 GtkWindow *parent;
46 GtkWidget *pw;
47 } NetContextGtk;
49 static void
50 statuscb_ctx(NetStatusType status, gpointer statusdata, gpointer data) {
51 NetContextGtk *ctx = data;
52 statuscb(status, statusdata, ctx->pw);
55 static void
56 ctx_gtk_title(NetContext *ctx, const char *title) {
57 ProgressWindow *pw = PROGRESS_WINDOW(((NetContextGtk*)ctx)->pw);
58 progress_window_set_title(pw, title);
60 static void
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);
66 static void
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);
73 static GString*
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);
78 else
79 return net_post_mainloop(url, NULL, post, statuscb_ctx, ctx, err);
82 NetContext*
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);
93 return ctx;
96 void
97 net_ctx_gtk_free(NetContext *ctx) {
98 NetContextGtk *gtkctx = (NetContextGtk*)ctx;
99 gtk_widget_destroy(gtkctx->pw);
100 g_free(ctx);
103 gboolean
104 net_window_run_verb(ProgressWindow *pw, LJVerb *verb) {
105 GError *err = NULL;
106 gboolean ret;
108 gtk_widget_show(GTK_WIDGET(pw));
109 ret = net_verb_run_internal(verb, statuscb, pw, &err);
111 if (!ret) {
112 if (!g_error_matches(err, NET_ERROR, NET_ERROR_CANCELLED))
113 progress_window_show_error(pw, err->message);
114 g_error_free(err);
115 return FALSE;
118 return TRUE;
121 gboolean
122 net_window_run_post(ProgressWindow *pw, const char *url, GSList *headers,
123 GString *request, GString *response,
124 GError **err) {
125 GString *res;
126 gboolean ret;
128 gtk_widget_show(GTK_WIDGET(pw));
129 res = net_post_mainloop(url, headers, request, statuscb, pw, err);
130 if (res) {
131 g_string_append_len(response, res->str, res->len);
132 ret = TRUE;
133 } else {
134 ret = FALSE;
136 g_string_free(res, TRUE);
138 return ret;
141 gboolean
142 net_verb_run(LJVerb *verb, const char *title, GtkWindow *parent) {
143 GtkWidget *pw;
144 gboolean ret;
146 pw = progress_window_new(parent, title);
147 ret = net_window_run_verb(PROGRESS_WINDOW(pw), verb);
148 gtk_widget_destroy(pw);
150 return ret;
153 GString*
154 net_get_run(const char *title, GtkWindow *parent,
155 const char *url) {
156 GtkWidget *pw;
157 GString *ret;
158 GError *err = NULL;
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);
164 if (!ret) {
165 if (!g_error_matches(err, NET_ERROR, NET_ERROR_CANCELLED))
166 progress_window_show_error(PROGRESS_WINDOW(pw), err->message);
167 g_error_free(err);
169 gtk_widget_destroy(pw);
171 return ret;
174 #endif