1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
9 #include <gtk/gtklist.h>
11 #include "liblj/login.h"
21 #define RESPONSE_UPDATE (1)
26 GtkWidget
*mhost
, *eusername
, *epassword
;
28 GtkWidget
*cruser
, *crpassword
;
35 static void populate_host_list (login_dlg
*ldlg
);
36 static void populate_user_list (login_dlg
*ldlg
);
39 static void load_account (login_dlg
*ldlg
, JamAccount
*acc
) {
41 const char *password
= jam_account_get_password(acc
);
42 gtk_entry_set_text(GTK_ENTRY(ldlg
->epassword
), (password
? password
: ""));
43 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ldlg
->cruser
), TRUE
);
44 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ldlg
->crpassword
), jam_account_get_remember_password(acc
));
46 gtk_entry_set_text(GTK_ENTRY(ldlg
->epassword
), "");
47 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ldlg
->cruser
), FALSE
);
48 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ldlg
->crpassword
), FALSE
);
53 static void username_changed (GtkWidget
*w
, login_dlg
*ldlg
) {
54 JamAccount
*acc
= jam_host_get_account_by_username(ldlg
->curhost
, gtk_entry_get_text(GTK_ENTRY(ldlg
->eusername
)), FALSE
);
55 load_account(ldlg
, acc
);
59 static JamAccount
*get_selected_account (login_dlg
*ldlg
) {
60 char *username
, *password
;
63 username
= gtk_editable_get_chars(GTK_EDITABLE(ldlg
->eusername
), 0, -1);
64 if (!username
|| !username
[0]) {
69 acc
= jam_host_get_account_by_username(ldlg
->curhost
, username
, TRUE
);
71 password
= gtk_editable_get_chars(GTK_EDITABLE(ldlg
->epassword
), 0, -1);
72 jam_account_set_password(acc
, password
);
77 jam_account_set_remember(acc
,
78 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ldlg
->cruser
)),
79 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ldlg
->crpassword
)));
85 gboolean
login_run (GtkWindow
*parent
, JamAccountLJ
*acc
) {
86 LJUser
*u
= jam_account_lj_get_user(acc
);
90 login
= lj_login_new(u
, "GTK2-LogJam/" PACKAGE_VERSION
);
92 ctx
= net_ctx_gtk_new(parent
, _("Logging In"));
93 if (!net_run_verb_ctx((LJVerb
*) login
, ctx
, NULL
)) {
95 net_ctx_gtk_free(ctx
);
99 if (login
->message
) jam_messagebox(parent
, _("LiveJournal Message"), login
->message
);
101 lj_login_free(login
);
102 net_ctx_gtk_free(ctx
);
104 acc
->lastupdate
= time(NULL
);
110 static void host_changed_cb (GtkOptionMenu
*menu
, login_dlg
*ldlg
) {
111 GSList
*l
= g_slist_nth(conf
.hosts
, gtk_option_menu_get_history(menu
));
112 if (!l
|| !l
->data
) return;
113 ldlg
->curhost
= l
->data
;
114 populate_user_list(ldlg
);
118 static void addedit_host_cb (GtkWidget
*w
, login_dlg
*ldlg
) {
119 void manager_dialog(GtkWindow
*parent
);
120 manager_dialog(GTK_WINDOW(ldlg
->win
));
121 ldlg
->curhost
= NULL
;
122 populate_host_list(ldlg
);
126 static void populate_host_list (login_dlg
*ldlg
) {
127 GtkWidget
*menu
, *item
, *box
, *icon
, *label
;
130 int i
= 0, history
= -1;
131 menu
= gtk_menu_new();
132 for (l
= conf
.hosts
; l
!= NULL
; l
= l
->next
) {
134 item
= gtk_menu_item_new();
135 box
= gtk_hbox_new(FALSE
, 3);
136 icon
= gtk_image_new_from_stock(jam_host_get_stock_icon(h
), GTK_ICON_SIZE_MENU
);
137 label
= gtk_label_new(h
->name
);
138 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
139 gtk_box_pack_start(GTK_BOX(box
), icon
, FALSE
, FALSE
, 0);
140 gtk_box_pack_start(GTK_BOX(box
), label
, TRUE
, TRUE
, 0);
141 gtk_container_add(GTK_CONTAINER(item
), box
);
142 gtk_widget_show_all(item
);
143 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
144 if (ldlg
->curhost
&& (g_ascii_strcasecmp(h
->name
, ldlg
->curhost
->name
) == 0)) history
= i
;
148 item
= gtk_separator_menu_item_new();
149 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
151 item
= gtk_menu_item_new_with_label(_("Add/Edit Servers..."));
152 g_signal_connect(G_OBJECT(item
), "activate", G_CALLBACK(addedit_host_cb
), ldlg
);
153 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
155 gtk_widget_show_all(menu
);
156 gtk_option_menu_set_menu(GTK_OPTION_MENU(ldlg
->mhost
), menu
);
158 if (history
>= 0) gtk_option_menu_set_history(GTK_OPTION_MENU(ldlg
->mhost
), history
);
162 static void populate_user_list (login_dlg
*ldlg
) {
164 GList
*strings
= NULL
;
165 JamHost
*host
= ldlg
->curhost
;
168 gtk_widget_set_sensitive(ldlg
->bupdate
, JAM_HOST_IS_LJ(ldlg
->curhost
));
170 for (l
= host
->accounts
; l
!= NULL
; l
= l
->next
) {
171 strings
= g_list_append(strings
, (char *)jam_account_get_username(l
->data
));
173 if (strings
) gtk_combo_set_popdown_strings(GTK_COMBO(ldlg
->cusername
), strings
);
174 else gtk_list_clear_items(GTK_LIST(GTK_COMBO(ldlg
->cusername
)->list
), 0, -1);
176 if (host
->lastaccount
) acc
= host
->lastaccount
;
177 else if (host
->accounts
) acc
= host
->accounts
->data
;
180 gtk_entry_set_text(GTK_ENTRY(ldlg
->eusername
), (acc
? jam_account_get_username(acc
) : ""));
181 load_account(ldlg
, acc
);
185 static void update_cb (GtkWidget
*w
, login_dlg
*ldlg
) {
187 acc
= get_selected_account(ldlg
);
188 /* we know the account is an lj account because the update button is disabled for non-lj. */
189 if (acc
) login_run(GTK_WINDOW(ldlg
->win
), JAM_ACCOUNT_LJ(acc
));
193 static GtkWidget
*make_login_table (login_dlg
*ldlg
) {
194 GtkWidget
*vbox
, *hbox
, *cvbox
, *img
;
197 sg
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
199 vbox
= gtk_vbox_new(FALSE
, 6);
201 ldlg
->mhost
= gtk_option_menu_new();
202 gtk_widget_set_size_request(ldlg
->mhost
, 50, -1);
203 populate_host_list(ldlg
);
204 g_signal_connect(G_OBJECT(ldlg
->mhost
), "changed", G_CALLBACK(host_changed_cb
), ldlg
);
205 gtk_box_pack_start(GTK_BOX(vbox
), labelled_box_new_sg(_("_Server:"), ldlg
->mhost
, sg
), FALSE
, FALSE
, 0);
207 ldlg
->cusername
= gtk_combo_new();
208 gtk_widget_set_size_request(ldlg
->cusername
, 50, -1);
209 ldlg
->eusername
= GTK_COMBO(ldlg
->cusername
)->entry
;
210 gtk_combo_disable_activate(GTK_COMBO(ldlg
->cusername
));
211 gtk_entry_set_activates_default(GTK_ENTRY(ldlg
->eusername
), TRUE
);
212 gtk_box_pack_start(GTK_BOX(vbox
), labelled_box_new_all(_("_User Name:"), ldlg
->cusername
, TRUE
, sg
, ldlg
->eusername
), FALSE
, FALSE
, 0);
214 ldlg
->epassword
= gtk_entry_new();
215 gtk_entry_set_activates_default(GTK_ENTRY(ldlg
->epassword
), TRUE
);
216 gtk_widget_set_size_request(ldlg
->epassword
, 100, -1);
217 gtk_entry_set_visibility(GTK_ENTRY(ldlg
->epassword
), FALSE
);
218 gtk_box_pack_start(GTK_BOX(vbox
), labelled_box_new_sg(_("_Password:"), ldlg
->epassword
, sg
), FALSE
, FALSE
, 0);
220 ldlg
->cruser
= gtk_check_button_new_with_mnemonic(_("R_emember user"));
221 ldlg
->crpassword
= gtk_check_button_new_with_mnemonic(_("Re_member password"));
222 ldlg
->bupdate
= gtk_button_new_with_mnemonic(_("Update _Information"));
223 g_signal_connect(G_OBJECT(ldlg
->bupdate
), "clicked", G_CALLBACK(update_cb
), ldlg
);
225 cvbox
= gtk_vbox_new(FALSE
, 6);
226 gtk_box_pack_start(GTK_BOX(cvbox
), ldlg
->cruser
, FALSE
, FALSE
, 0);
227 gtk_box_pack_start(GTK_BOX(cvbox
), ldlg
->crpassword
, FALSE
, FALSE
, 0);
228 gtk_box_pack_start(GTK_BOX(cvbox
), ldlg
->bupdate
, FALSE
, FALSE
, 0);
230 img
= gtk_image_new_from_stock("logjam-goat", GTK_ICON_SIZE_DIALOG
), gtk_size_group_add_widget(sg
, img
);
232 hbox
= gtk_hbox_new(FALSE
, 12);
233 gtk_box_pack_start(GTK_BOX(hbox
), img
, FALSE
, FALSE
, 0);
234 gtk_box_pack_start(GTK_BOX(hbox
), cvbox
, TRUE
, TRUE
, 0);
236 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
238 populate_user_list(ldlg
);
240 g_signal_connect(G_OBJECT(ldlg
->eusername
), "changed", G_CALLBACK(username_changed
), ldlg
);
246 static void login_shown (login_dlg
*ldlg
) {
247 gtk_widget_grab_focus(ldlg
->eusername
);
251 gboolean
login_check_lastupdate (GtkWindow
*parent
, JamAccountLJ
*acclj
) {
256 JamAccount
*acc
= JAM_ACCOUNT(acclj
);
258 deltat
= time(NULL
) - acclj
->lastupdate
;
259 if (deltat
< 1*7*24*60*60) return TRUE
; /* one week */
261 if (acclj
->lastupdate
== 0) {
262 msg
= g_strdup_printf(_("Account '%s' hasn't ever logged in. " "Log in now?"), jam_account_get_username(acc
));
264 msg
= g_strdup_printf(_("Account '%s' hasn't logged in "
265 "in at least %lu days. Log in now?"), jam_account_get_username(acc
), (unsigned long)deltat
/(24*60*60));
267 dlg
= gtk_message_dialog_new(parent
, GTK_DIALOG_DESTROY_WITH_PARENT
, GTK_MESSAGE_QUESTION
, GTK_BUTTONS_YES_NO
, "%s", msg
);
269 if (gtk_dialog_run(GTK_DIALOG(dlg
)) == GTK_RESPONSE_YES
) ret
= login_run(GTK_WINDOW(dlg
), acclj
);
270 gtk_widget_destroy(dlg
);
271 /* no matter what they answered, we shouldn't bug them again
272 * next time if they said no. the only case to bug them again
273 * is if login failed. */
274 if (ret
) acclj
->lastupdate
= time(NULL
);
280 JamAccount
*login_dlg_run (GtkWindow
*parent
, JamHost
*host
, JamAccount
*acc
) {
281 login_dlg ldlg_actual
= { 0 }, *ldlg
= &ldlg_actual
;
282 GtkWidget
*dlg
, *align
, *vbox
;
284 ldlg
->win
= dlg
= gtk_dialog_new_with_buttons(_("Select User"),
285 parent
, GTK_DIALOG_MODAL
,
286 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, _("Selec_t"), GTK_RESPONSE_OK
, NULL
);
288 if (acc
) ldlg
->curhost
= jam_account_get_host(acc
);
289 else if (host
) ldlg
->curhost
= host
;
290 else ldlg
->curhost
= conf
.hosts
->data
;
292 gtk_widget_realize(ldlg
->win
);
294 vbox
= gtk_vbox_new(FALSE
, 5);
296 gtk_box_pack_start(GTK_BOX(vbox
), make_login_table(ldlg
), TRUE
, TRUE
, 0);
297 align
= gtk_alignment_new(.5, .5, 1, 0);
298 gtk_container_add(GTK_CONTAINER(align
), vbox
);
300 jam_dialog_set_contents(GTK_DIALOG(dlg
), align
);
302 gtk_dialog_set_default_response(GTK_DIALOG(dlg
), GTK_RESPONSE_OK
);
304 g_signal_connect_swapped(G_OBJECT(ldlg
->win
), "show", G_CALLBACK(login_shown
), ldlg
);
306 if (acc
) gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(ldlg
->cusername
)->entry
), jam_account_get_username(acc
));
309 acc
= (gtk_dialog_run(GTK_DIALOG(ldlg
->win
)) == GTK_RESPONSE_OK
? get_selected_account(ldlg
) : NULL
);
311 conf
.lasthost
= jam_account_get_host(acc
);
312 conf
.lasthost
->lastaccount
= acc
;
313 if (JAM_ACCOUNT_IS_LJ(acc
)) {
314 if (!login_check_lastupdate(GTK_WINDOW(ldlg
->win
), JAM_ACCOUNT_LJ(acc
)))
319 gtk_widget_destroy(dlg
);