1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
9 #include "liblj/consolecommand.h"
21 GtkWidget
*entry
, *display
;
22 GtkTextMark
*mark_end
;
23 JamAccountLJ
*account
;
27 name_from_type(LJConsoleLineType type
) {
29 case LJ_CONSOLE_LINE_TYPE_INFO
:
31 case LJ_CONSOLE_LINE_TYPE_ERROR
:
33 case LJ_CONSOLE_LINE_TYPE_UNKNOWN
:
40 console_print(ConsoleUI
*cui
, char *type
, const char *text
, ...) {
43 GtkTextBuffer
*buffer
;
47 g_vsnprintf(buf
, 4096, text
, ap
);
50 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(cui
->display
));
51 gtk_text_buffer_get_end_iter(buffer
, &iter
);
53 gtk_text_buffer_insert_with_tags_by_name(buffer
, &iter
,
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);
63 run_console_command(ConsoleUI
*cui
, const char *command
) {
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
);
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
);
95 submit_cb(GtkWidget
*w
, ConsoleUI
*cui
) {
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);
105 console_help(GtkWidget
*w
, ConsoleUI
*cui
) {
106 run_console_command(cui
, "help");
110 console_dialog_run(GtkWindow
*parent
, JamAccountLJ
*acc
) {
112 GtkWidget
*vbox
, *hbox
, *button
;
113 GtkTextBuffer
*buffer
;
116 cui
= g_new0(ConsoleUI
, 1);
118 cui
->win
= gtk_dialog_new_with_buttons(_("LiveJournal Console"),
119 parent
, GTK_DIALOG_DESTROY_WITH_PARENT
,
120 GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
,
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
),
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",
161 gtk_text_buffer_create_tag(buffer
, "info",
162 "family", "monospace",
163 "foreground", "darkgreen",
165 gtk_text_buffer_create_tag(buffer
, "error",
166 "family", "monospace",
167 "weight", PANGO_WEIGHT_BOLD
,
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
);