it builds! (note that some autogenerated files are hardcoded now, and you can't build...
[k8lowj.git] / src / console.c
blob3ae9e14bf794b0e84062b07ff5ddf7f6fa7d376f
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 */
7 #include "gtk-all.h"
9 #include "liblj/consolecommand.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include "util-gtk.h"
15 #include "network.h"
16 #include "console.h"
17 #include "conf.h"
19 typedef struct {
20 GtkWidget *win;
21 GtkWidget *entry, *display;
22 GtkTextMark *mark_end;
23 JamAccountLJ *account;
24 } ConsoleUI;
26 static char *
27 name_from_type(LJConsoleLineType type) {
28 switch (type) {
29 case LJ_CONSOLE_LINE_TYPE_INFO:
30 return "info";
31 case LJ_CONSOLE_LINE_TYPE_ERROR:
32 return "error";
33 case LJ_CONSOLE_LINE_TYPE_UNKNOWN:
34 default:
35 return NULL;
39 void
40 console_print(ConsoleUI *cui, char *type, const char *text, ...) {
41 char buf[4096];
42 va_list ap;
43 GtkTextBuffer *buffer;
44 GtkTextIter iter;
46 va_start(ap, text);
47 g_vsnprintf(buf, 4096, text, ap);
48 va_end(ap);
50 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(cui->display));
51 gtk_text_buffer_get_end_iter(buffer, &iter);
52 if (type) {
53 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
54 buf, -1, type, NULL);
55 } else {
56 gtk_text_buffer_insert(buffer, &iter, buf, -1);
58 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(cui->display),
59 cui->mark_end, 0.0, FALSE, 0, 0);
62 gboolean
63 run_console_command(ConsoleUI *cui, const char *command) {
64 NetContext *ctx;
65 LJConsoleCommand *cc;
66 int i;
68 console_print(cui, NULL, _("Running command: '"));
69 console_print(cui, "command", command);
70 console_print(cui, NULL, "'.\n");
72 cc = lj_consolecommand_new(jam_account_lj_get_user(cui->account), command);
74 ctx = net_ctx_gtk_new(GTK_WINDOW(cui->win), _("Running Command"));
75 if (!net_run_verb_ctx((LJVerb*)cc, ctx, NULL)) {
76 console_print(cui, "error", _("Error running command.\n"));
77 lj_consolecommand_free(cc);
78 net_ctx_gtk_free(ctx);
79 return FALSE;
82 for (i = 0; i < cc->linecount; i++) {
83 char *typename = name_from_type(cc->lines[i].type);
84 console_print(cui, typename, cc->lines[i].text);
85 console_print(cui, typename, "\n");
88 lj_consolecommand_free(cc);
89 net_ctx_gtk_free(ctx);
91 return TRUE;
94 static void
95 submit_cb(GtkWidget *w, ConsoleUI *cui) {
96 char *command;
98 command = gtk_editable_get_chars(GTK_EDITABLE(cui->entry), 0, -1);
99 if (run_console_command(cui, command))
100 gtk_editable_delete_text(GTK_EDITABLE(cui->entry), 0, -1);
101 g_free(command);
104 static void
105 console_help(GtkWidget *w, ConsoleUI *cui) {
106 run_console_command(cui, "help");
109 void
110 console_dialog_run(GtkWindow *parent, JamAccountLJ *acc) {
111 ConsoleUI *cui;
112 GtkWidget *vbox, *hbox, *button;
113 GtkTextBuffer *buffer;
114 GtkTextIter end;
116 cui = g_new0(ConsoleUI, 1);
117 cui->account = acc;
118 cui->win = gtk_dialog_new_with_buttons(_("LiveJournal Console"),
119 parent, GTK_DIALOG_DESTROY_WITH_PARENT,
120 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
121 NULL);
122 jam_win_set_size(GTK_WINDOW(cui->win), 500, 400);
123 g_signal_connect_swapped(G_OBJECT(cui->win), "response",
124 G_CALLBACK(gtk_widget_destroy), cui->win);
125 g_signal_connect_swapped(G_OBJECT(cui->win), "destroy",
126 G_CALLBACK(g_free), cui);
127 geometry_tie(cui->win, GEOM_CONSOLE);
129 vbox = gtk_vbox_new(FALSE, 5);
131 hbox = gtk_hbox_new(FALSE, 5);
132 cui->entry = gtk_entry_new();
133 gtk_box_pack_start(GTK_BOX(hbox), cui->entry, TRUE, TRUE, 0);
135 button = gtk_button_new_with_mnemonic(_(" _Submit "));
136 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
138 g_signal_connect(G_OBJECT(cui->entry), "activate",
139 G_CALLBACK(submit_cb), cui);
140 g_signal_connect(G_OBJECT(button), "clicked",
141 G_CALLBACK(submit_cb), cui);
143 button = gtk_button_new_from_stock(GTK_STOCK_HELP);
144 g_signal_connect(G_OBJECT(button), "clicked",
145 G_CALLBACK(console_help), cui);
146 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
148 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
150 cui->display = gtk_text_view_new();
151 gtk_text_view_set_editable(GTK_TEXT_VIEW(cui->display), FALSE);
152 gtk_box_pack_start(GTK_BOX(vbox), scroll_wrap(cui->display),
153 TRUE, TRUE, 0);
155 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(cui->display));
156 gtk_text_buffer_get_end_iter(buffer, &end);
157 cui->mark_end = gtk_text_buffer_create_mark(buffer, NULL, &end, FALSE);
158 gtk_text_buffer_create_tag(buffer, "command",
159 "family", "monospace",
160 NULL);
161 gtk_text_buffer_create_tag(buffer, "info",
162 "family", "monospace",
163 "foreground", "darkgreen",
164 NULL);
165 gtk_text_buffer_create_tag(buffer, "error",
166 "family", "monospace",
167 "weight", PANGO_WEIGHT_BOLD,
168 "foreground", "red",
169 NULL);
171 jam_dialog_set_contents(GTK_DIALOG(cui->win), vbox);
173 console_print(cui, NULL, _("LiveJournal console ready.\n"));
175 gtk_widget_show(cui->win);