progress.*: cosmetix
[k8lowj.git] / src / friendedit.c
blob87a9d60c6858737a6504b9a89db3a95e99070f80
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #include "gtk-all.h"
5 #include "util-gtk.h"
7 #include <stdlib.h>
8 #include <string.h>
10 #include "liblj/editfriends.h"
12 #include "account.h"
13 #include "conf.h"
14 #include "friendedit.h"
15 #include "friends.h"
16 #include "network.h"
17 #include "util.h"
20 typedef struct {
21 GtkWidget *win;
22 GtkWidget *eusername, *ebgcolor, *efgcolor;
24 LJFriend *editfriend;
25 JamAccountLJ *account;
26 } FriendEditUI;
29 static void update_preview (FriendEditUI *feui) {
30 GdkColor fg, bg;
31 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)), &fg);
32 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)), &bg);
33 gtk_widget_modify_text(feui->eusername, GTK_STATE_NORMAL, &fg);
34 gtk_widget_modify_base(feui->eusername, GTK_STATE_NORMAL, &bg);
38 static void color_entry_changed (GtkEntry *e, FriendEditUI *feui) {
39 if (strlen(gtk_entry_get_text(e)) == 7) update_preview(feui);
43 static gint change_entry_color_dlg (FriendEditUI *feui, GtkWidget *toedit, const char *title) {
44 GtkWidget *dlg;
45 GtkColorSelection *csel;
46 const char *curcolor;
47 char new_hex[10];
48 GdkColor color;
50 dlg = gtk_color_selection_dialog_new(title);
51 gtk_window_set_transient_for(GTK_WINDOW(dlg), GTK_WINDOW(feui->win));
52 gtk_widget_hide(GTK_COLOR_SELECTION_DIALOG(dlg)->help_button);
54 csel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dlg)->colorsel);
56 curcolor = gtk_entry_get_text(GTK_ENTRY(toedit));
58 /* convert existing hex color to the color selection's color */
59 if (strlen(curcolor) == 7 && curcolor[0] == '#') {
60 gdk_color_parse(curcolor, &color);
61 gtk_color_selection_set_current_color(csel, &color);
64 if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
65 gtk_color_selection_get_current_color(csel, &color);
66 gdkcolor_to_hex(&color, new_hex);
67 gtk_entry_set_text(GTK_ENTRY(toedit), new_hex);
69 gtk_widget_destroy(dlg);
71 return 0;
75 static void change_col_bg (GtkWidget *w, FriendEditUI *feui) {
76 change_entry_color_dlg(feui, feui->ebgcolor, _("Select Background Color"));
80 static void change_col_fg (GtkWidget *w, FriendEditUI *feui) {
81 change_entry_color_dlg(feui, feui->efgcolor, _("Select Foreground Color"));
85 static gboolean add_the_friend (FriendEditUI *feui) {
86 NetContext *ctx;
87 LJEditFriends *ef;
88 gchar *username, *name;
90 ctx = net_ctx_gtk_new(GTK_WINDOW(feui->win), _("Adding Friend"));
92 ef = lj_editfriends_new(jam_account_lj_get_user(feui->account));
93 lj_editfriends_add_friend(ef,
94 gtk_entry_get_text(GTK_ENTRY(feui->eusername)),
95 gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)), gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)));
97 if (!net_run_verb_ctx((LJVerb *)ef, ctx, NULL) || ef->addcount != 1) {
98 lj_editfriends_free(ef);
99 net_ctx_gtk_free(ctx);
100 return FALSE;
103 name = ef->added[0].fullname;
104 username = ef->added[0].username;
106 if (feui->editfriend == NULL || strcmp(feui->editfriend->username, username) != 0) {
107 /* we must create a new friend */
108 feui->editfriend = lj_friend_new();
109 feui->editfriend->conn = LJ_FRIEND_CONN_MY;
112 string_replace(&feui->editfriend->username, g_strdup(username));
113 string_replace(&feui->editfriend->fullname, g_strdup(name));
114 feui->editfriend->foreground = lj_color_to_int(gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)));
115 feui->editfriend->background = lj_color_to_int(gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)));
117 lj_editfriends_free(ef);
118 net_ctx_gtk_free(ctx);
119 return TRUE;
123 static void entry_changed (GtkEntry *entry, FriendEditUI *feui) {
124 gtk_dialog_set_response_sensitive(GTK_DIALOG(feui->win), GTK_RESPONSE_OK, (strlen(gtk_entry_get_text(entry)) > 0));
128 LJFriend *friend_edit_dlg_run (GtkWindow *parent, JamAccountLJ *acc, gboolean edit, LJFriend *f) {
129 FriendEditUI feui_actual = { 0 };
130 FriendEditUI *feui = &feui_actual;
131 GtkWidget *button;
132 GtkWidget *table;
134 feui->account = acc;
135 feui->editfriend = f;
137 feui->win = gtk_dialog_new_with_buttons(edit ? _("Edit Friend") :
138 (f ? _("Add This Friend") : _("Add a Friend")),
139 parent, GTK_DIALOG_MODAL,
140 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, edit ? _("Change") : _("Add"), GTK_RESPONSE_OK, NULL);
141 gtk_dialog_set_default_response(GTK_DIALOG(feui->win), GTK_RESPONSE_OK);
143 table = jam_table_new(3, 3);
145 /* make the labels/entries */
146 feui->eusername = gtk_entry_new();
147 gtk_entry_set_activates_default(GTK_ENTRY(feui->eusername), TRUE);
148 gtk_entry_set_max_length(GTK_ENTRY(feui->eusername), 18);
149 if (edit) {
150 gtk_editable_set_editable(GTK_EDITABLE(feui->eusername), FALSE);
151 } else {
152 /* enable/disable the button based on name text */
153 g_signal_connect(G_OBJECT(feui->eusername), "changed", G_CALLBACK(entry_changed), feui);
156 gtk_widget_set_size_request(feui->eusername, 100, -1);
157 jam_table_label_content(GTK_TABLE(table), 0, _("Friend's _username:"), feui->eusername);
159 feui->efgcolor = gtk_entry_new();
160 gtk_entry_set_activates_default(GTK_ENTRY(feui->efgcolor), TRUE);
161 gtk_entry_set_max_length(GTK_ENTRY(feui->efgcolor), 7);
162 g_signal_connect(G_OBJECT(feui->efgcolor), "changed", G_CALLBACK(color_entry_changed), feui);
163 gtk_widget_set_size_request(feui->efgcolor, 100, -1);
164 jam_table_label_content(GTK_TABLE(table), 1, _("_Text color:"), feui->efgcolor);
166 feui->ebgcolor = gtk_entry_new();
167 gtk_entry_set_activates_default(GTK_ENTRY(feui->ebgcolor), TRUE);
168 gtk_entry_set_max_length(GTK_ENTRY(feui->ebgcolor), 7);
169 g_signal_connect(G_OBJECT(feui->ebgcolor), "changed", G_CALLBACK(color_entry_changed), feui);
170 gtk_widget_set_size_request(feui->ebgcolor, 100, -1);
171 jam_table_label_content(GTK_TABLE(table), 2, _("_Background color:"), feui->ebgcolor);
173 /* make the color selector buttons */
174 button = gtk_button_new_with_label(" ... ");
175 gtk_table_attach(GTK_TABLE(table), button, 2, 3, 1, 2, GTK_FILL, 0, 2, 2);
176 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(change_col_fg), feui);
177 button = gtk_button_new_with_label(" ... ");
178 gtk_table_attach(GTK_TABLE(table), button, 2, 3, 2, 3, GTK_FILL, 0, 2, 2);
179 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(change_col_bg), feui);
181 jam_dialog_set_contents(GTK_DIALOG(feui->win), table);
183 /* fill in default values. */
184 if (f) {
185 gtk_entry_set_text(GTK_ENTRY(feui->eusername), f->username);
186 } else {
187 /* emit the "changed" signal, in any case. */
188 g_signal_emit_by_name(G_OBJECT(feui->eusername), "changed");
191 if (!edit) {
192 gtk_entry_set_text(GTK_ENTRY(feui->efgcolor), "#000000");
193 gtk_entry_set_text(GTK_ENTRY(feui->ebgcolor), "#FFFFFF");
194 } else {
195 char color[10];
196 lj_int_to_color(f->foreground, color);
197 gtk_entry_set_text(GTK_ENTRY(feui->efgcolor), color);
198 lj_int_to_color(f->background, color);
199 gtk_entry_set_text(GTK_ENTRY(feui->ebgcolor), color);
202 while (gtk_dialog_run(GTK_DIALOG(feui->win)) == GTK_RESPONSE_OK) {
203 if (add_the_friend(feui)) {
204 gtk_widget_destroy(feui->win);
205 return feui->editfriend;
208 gtk_widget_destroy(feui->win);
209 return NULL;