fixed some clang warnings
[k8lowj.git] / src / network-gtk.c
blob363c55088263f2ca569345898ffcb71fb08e64d8
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #ifdef HAVE_GTK
6 #include "gtk-all.h"
8 #include "conf.h"
9 #include "network.h"
10 #include "network-internal.h"
11 #include "progress.h"
12 #include "throbber.h"
15 static void statuscb (NetStatusType status, gpointer statusdata, gpointer data) {
16 ProgressWindow *pw = PROGRESS_WINDOW(data);
17 NetStatusProgress *progress;
18 float fraction = 0;
19 switch (status) {
20 case NET_STATUS_BEGIN:
21 progress_window_set_cancel_cb(pw, net_mainloop_cancel, statusdata);
22 progress_window_set_progress(pw, 0);
23 break;
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);
30 break;
31 case NET_STATUS_DONE:
32 progress_window_set_cancel_cb(pw, NULL, NULL);
33 progress_window_set_progress(pw, -1);
34 break;
35 default:
36 g_warning("unhandled network status %d\n", status);
41 typedef struct {
42 NetContext ctx;
43 GtkWindow *parent;
44 GtkWidget *pw;
45 } NetContextGtk;
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);
88 return ctx;
92 void net_ctx_gtk_free (NetContext *ctx) {
93 NetContextGtk *gtkctx = (NetContextGtk *)ctx;
94 gtk_widget_destroy(gtkctx->pw);
95 g_free(ctx);
99 gboolean net_window_run_verb (ProgressWindow *pw, LJVerb *verb) {
100 GError *err = NULL;
101 gboolean ret;
102 gtk_widget_show(GTK_WIDGET(pw));
103 ret = net_verb_run_internal(verb, statuscb, pw, &err);
104 if (!ret) {
105 if (!g_error_matches(err, NET_ERROR, NET_ERROR_CANCELLED)) progress_window_show_error(pw, "%s", err->message);
106 g_error_free(err);
107 return FALSE;
109 return TRUE;
113 gboolean net_window_run_post (ProgressWindow *pw, const char *url, GSList *headers, GString *request, GString *response, GError **err) {
114 GString *res;
115 gboolean ret;
116 gtk_widget_show(GTK_WIDGET(pw));
117 res = net_post_mainloop(url, headers, request, statuscb, pw, err);
118 if (res) {
119 g_string_append_len(response, res->str, res->len);
120 ret = TRUE;
121 } else {
122 ret = FALSE;
124 g_string_free(res, TRUE);
125 return ret;
129 gboolean net_verb_run (LJVerb *verb, const char *title, GtkWindow *parent) {
130 GtkWidget *pw;
131 gboolean ret;
132 pw = progress_window_new(parent, title);
133 ret = net_window_run_verb(PROGRESS_WINDOW(pw), verb);
134 gtk_widget_destroy(pw);
135 return ret;
139 GString *net_get_run (const char *title, GtkWindow *parent, const char *url) {
140 GtkWidget *pw;
141 GString *ret;
142 GError *err = NULL;
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);
146 if (!ret) {
147 if (!g_error_matches(err, NET_ERROR, NET_ERROR_CANCELLED)) progress_window_show_error(PROGRESS_WINDOW(pw), "%s", err->message);
148 g_error_free(err);
150 gtk_widget_destroy(pw);
151 return ret;
154 #endif