it builds! (note that some autogenerated files are hardcoded now, and you can't build...
[k8lowj.git] / src / friendedit.c
blob51ebfad8b733432c35f954a5a6d7f8df8fb34742
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 <stdlib.h>
10 #include <string.h>
12 #include "liblj/editfriends.h"
14 #include "friends.h"
15 #include "network.h"
16 #include "conf.h"
17 #include "util-gtk.h"
18 #include "account.h"
20 #include "friendedit.h"
22 typedef struct {
23 GtkWidget *win;
24 GtkWidget *eusername, *ebgcolor, *efgcolor;
26 LJFriend *editfriend;
27 JamAccountLJ *account;
28 } FriendEditUI;
30 static void
31 update_preview(FriendEditUI *feui) {
32 GdkColor fg, bg;
34 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)), &fg);
35 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)), &bg);
37 gtk_widget_modify_text(feui->eusername, GTK_STATE_NORMAL, &fg);
38 gtk_widget_modify_base(feui->eusername, GTK_STATE_NORMAL, &bg);
41 static void
42 color_entry_changed(GtkEntry *e, FriendEditUI *feui) {
43 if (strlen(gtk_entry_get_text(e)) == 7)
44 update_preview(feui);
47 static gint
48 change_entry_color_dlg(FriendEditUI *feui, GtkWidget *toedit, const char *title) {
49 GtkWidget *dlg;
50 GtkColorSelection *csel;
51 const char *curcolor;
52 char new_hex[10];
53 GdkColor color;
55 dlg = gtk_color_selection_dialog_new(title);
56 gtk_window_set_transient_for(GTK_WINDOW(dlg), GTK_WINDOW(feui->win));
57 gtk_widget_hide(GTK_COLOR_SELECTION_DIALOG(dlg)->help_button);
59 csel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dlg)->colorsel);
61 curcolor = gtk_entry_get_text(GTK_ENTRY(toedit));
63 /* convert existing hex color to the color selection's color */
64 if (strlen(curcolor) == 7 && curcolor[0] == '#') {
65 gdk_color_parse(curcolor, &color);
66 gtk_color_selection_set_current_color(csel, &color);
69 if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
70 gtk_color_selection_get_current_color(csel, &color);
72 gdkcolor_to_hex(&color, new_hex);
74 gtk_entry_set_text(GTK_ENTRY(toedit), new_hex);
76 gtk_widget_destroy(dlg);
78 return 0;
81 static void
82 change_col_bg(GtkWidget *w, FriendEditUI *feui) {
83 change_entry_color_dlg(feui, feui->ebgcolor, _("Select Background Color"));
86 static void
87 change_col_fg(GtkWidget *w, FriendEditUI *feui) {
88 change_entry_color_dlg(feui, feui->efgcolor, _("Select Foreground Color"));
91 static gboolean
92 add_the_friend(FriendEditUI *feui) {
93 NetContext *ctx;
94 LJEditFriends *ef;
95 gchar *username, *name;
97 ctx = net_ctx_gtk_new(GTK_WINDOW(feui->win), _("Adding Friend"));
99 ef = lj_editfriends_new(jam_account_lj_get_user(feui->account));
100 lj_editfriends_add_friend(ef,
101 gtk_entry_get_text(GTK_ENTRY(feui->eusername)),
102 gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)),
103 gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)));
105 if (!net_run_verb_ctx((LJVerb*)ef, ctx, NULL) || ef->addcount != 1) {
106 lj_editfriends_free(ef);
107 net_ctx_gtk_free(ctx);
108 return FALSE;
111 name = ef->added[0].fullname;
112 username = ef->added[0].username;
114 if (feui->editfriend == NULL ||
115 strcmp(feui->editfriend->username, username) != 0) {
116 /* we must create a new friend */
117 feui->editfriend = lj_friend_new();
118 feui->editfriend->conn = LJ_FRIEND_CONN_MY;
121 string_replace(&feui->editfriend->username, g_strdup(username));
122 string_replace(&feui->editfriend->fullname, g_strdup(name));
123 feui->editfriend->foreground = lj_color_to_int(
124 gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)));
125 feui->editfriend->background = lj_color_to_int(
126 gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)));
128 lj_editfriends_free(ef);
129 net_ctx_gtk_free(ctx);
130 return TRUE;
133 static void
134 entry_changed(GtkEntry *entry, FriendEditUI* feui) {
135 gtk_dialog_set_response_sensitive(GTK_DIALOG(feui->win),
136 GTK_RESPONSE_OK,
137 (strlen(gtk_entry_get_text(entry)) > 0));
140 LJFriend*
141 friend_edit_dlg_run(GtkWindow *parent, JamAccountLJ *acc, gboolean edit, LJFriend *f) {
142 FriendEditUI feui_actual = { 0 };
143 FriendEditUI *feui = &feui_actual;
144 GtkWidget *button;
145 GtkWidget *table;
147 feui->account = acc;
148 feui->editfriend = f;
150 feui->win = gtk_dialog_new_with_buttons(
151 edit ? _("Edit Friend") :
152 (f ? _("Add This Friend") : _("Add a Friend")),
153 parent, GTK_DIALOG_MODAL,
154 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
155 edit ? _("Change") : _("Add"), GTK_RESPONSE_OK,
156 NULL);
157 gtk_dialog_set_default_response(GTK_DIALOG(feui->win), GTK_RESPONSE_OK);
159 table = jam_table_new(3, 3);
161 /* make the labels/entries */
162 feui->eusername = gtk_entry_new();
163 gtk_entry_set_activates_default(GTK_ENTRY(feui->eusername), TRUE);
164 gtk_entry_set_max_length(GTK_ENTRY(feui->eusername), 18);
165 if (edit)
166 gtk_editable_set_editable(GTK_EDITABLE(feui->eusername), FALSE);
167 else
168 /* enable/disable the button based on name text */
169 g_signal_connect(G_OBJECT(feui->eusername), "changed",
170 G_CALLBACK(entry_changed), feui);
172 gtk_widget_set_size_request(feui->eusername, 100, -1);
173 jam_table_label_content(GTK_TABLE(table), 0,
174 _("Friend's _username:"), feui->eusername);
176 feui->efgcolor = gtk_entry_new();
177 gtk_entry_set_activates_default(GTK_ENTRY(feui->efgcolor), TRUE);
178 gtk_entry_set_max_length(GTK_ENTRY(feui->efgcolor), 7);
179 g_signal_connect(G_OBJECT(feui->efgcolor), "changed",
180 G_CALLBACK(color_entry_changed), feui);
181 gtk_widget_set_size_request(feui->efgcolor, 100, -1);
182 jam_table_label_content(GTK_TABLE(table), 1,
183 _("_Text color:"), feui->efgcolor);
185 feui->ebgcolor = gtk_entry_new();
186 gtk_entry_set_activates_default(GTK_ENTRY(feui->ebgcolor), TRUE);
187 gtk_entry_set_max_length(GTK_ENTRY(feui->ebgcolor), 7);
188 g_signal_connect(G_OBJECT(feui->ebgcolor), "changed",
189 G_CALLBACK(color_entry_changed), feui);
190 gtk_widget_set_size_request(feui->ebgcolor, 100, -1);
191 jam_table_label_content(GTK_TABLE(table), 2,
192 _("_Background color:"), feui->ebgcolor);
194 /* make the color selector buttons */
195 button = gtk_button_new_with_label(" ... ");
196 gtk_table_attach(GTK_TABLE(table), button, 2, 3, 1, 2, GTK_FILL, 0, 2, 2);
197 g_signal_connect(G_OBJECT(button), "clicked",
198 G_CALLBACK(change_col_fg), feui);
199 button = gtk_button_new_with_label(" ... ");
200 gtk_table_attach(GTK_TABLE(table), button, 2, 3, 2, 3, GTK_FILL, 0, 2, 2);
201 g_signal_connect(G_OBJECT(button), "clicked",
202 G_CALLBACK(change_col_bg), feui);
204 jam_dialog_set_contents(GTK_DIALOG(feui->win), table);
206 /* fill in default values. */
207 if (f) {
208 gtk_entry_set_text(GTK_ENTRY(feui->eusername), f->username);
209 } else {
210 /* emit the "changed" signal, in any case. */
211 g_signal_emit_by_name(G_OBJECT(feui->eusername), "changed");
214 if (!edit) {
215 gtk_entry_set_text(GTK_ENTRY(feui->efgcolor), "#000000");
216 gtk_entry_set_text(GTK_ENTRY(feui->ebgcolor), "#FFFFFF");
217 } else {
218 char color[10];
219 lj_int_to_color(f->foreground, color);
220 gtk_entry_set_text(GTK_ENTRY(feui->efgcolor), color);
221 lj_int_to_color(f->background, color);
222 gtk_entry_set_text(GTK_ENTRY(feui->ebgcolor), color);
225 while (gtk_dialog_run(GTK_DIALOG(feui->win)) == GTK_RESPONSE_OK) {
226 if (add_the_friend(feui)) {
227 gtk_widget_destroy(feui->win);
228 return feui->editfriend;
231 gtk_widget_destroy(feui->win);
232 return NULL;