fixed GTKHTML detection
[k8lowj.git] / src / friendedit.c
blobd1405edb1c8cd35fb8c8f237cd6b780777fafac4
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"
21 #include "util.h"
23 typedef struct {
24 GtkWidget *win;
25 GtkWidget *eusername, *ebgcolor, *efgcolor;
27 LJFriend *editfriend;
28 JamAccountLJ *account;
29 } FriendEditUI;
31 static void
32 update_preview(FriendEditUI *feui) {
33 GdkColor fg, bg;
35 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)), &fg);
36 gdk_color_parse(gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)), &bg);
38 gtk_widget_modify_text(feui->eusername, GTK_STATE_NORMAL, &fg);
39 gtk_widget_modify_base(feui->eusername, GTK_STATE_NORMAL, &bg);
42 static void
43 color_entry_changed(GtkEntry *e, FriendEditUI *feui) {
44 if (strlen(gtk_entry_get_text(e)) == 7)
45 update_preview(feui);
48 static gint
49 change_entry_color_dlg(FriendEditUI *feui, GtkWidget *toedit, const char *title) {
50 GtkWidget *dlg;
51 GtkColorSelection *csel;
52 const char *curcolor;
53 char new_hex[10];
54 GdkColor color;
56 dlg = gtk_color_selection_dialog_new(title);
57 gtk_window_set_transient_for(GTK_WINDOW(dlg), GTK_WINDOW(feui->win));
58 gtk_widget_hide(GTK_COLOR_SELECTION_DIALOG(dlg)->help_button);
60 csel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dlg)->colorsel);
62 curcolor = gtk_entry_get_text(GTK_ENTRY(toedit));
64 /* convert existing hex color to the color selection's color */
65 if (strlen(curcolor) == 7 && curcolor[0] == '#') {
66 gdk_color_parse(curcolor, &color);
67 gtk_color_selection_set_current_color(csel, &color);
70 if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
71 gtk_color_selection_get_current_color(csel, &color);
73 gdkcolor_to_hex(&color, new_hex);
75 gtk_entry_set_text(GTK_ENTRY(toedit), new_hex);
77 gtk_widget_destroy(dlg);
79 return 0;
82 static void
83 change_col_bg(GtkWidget *w, FriendEditUI *feui) {
84 change_entry_color_dlg(feui, feui->ebgcolor, _("Select Background Color"));
87 static void
88 change_col_fg(GtkWidget *w, FriendEditUI *feui) {
89 change_entry_color_dlg(feui, feui->efgcolor, _("Select Foreground Color"));
92 static gboolean
93 add_the_friend(FriendEditUI *feui) {
94 NetContext *ctx;
95 LJEditFriends *ef;
96 gchar *username, *name;
98 ctx = net_ctx_gtk_new(GTK_WINDOW(feui->win), _("Adding Friend"));
100 ef = lj_editfriends_new(jam_account_lj_get_user(feui->account));
101 lj_editfriends_add_friend(ef,
102 gtk_entry_get_text(GTK_ENTRY(feui->eusername)),
103 gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)),
104 gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)));
106 if (!net_run_verb_ctx((LJVerb*)ef, ctx, NULL) || ef->addcount != 1) {
107 lj_editfriends_free(ef);
108 net_ctx_gtk_free(ctx);
109 return FALSE;
112 name = ef->added[0].fullname;
113 username = ef->added[0].username;
115 if (feui->editfriend == NULL ||
116 strcmp(feui->editfriend->username, username) != 0) {
117 /* we must create a new friend */
118 feui->editfriend = lj_friend_new();
119 feui->editfriend->conn = LJ_FRIEND_CONN_MY;
122 string_replace(&feui->editfriend->username, g_strdup(username));
123 string_replace(&feui->editfriend->fullname, g_strdup(name));
124 feui->editfriend->foreground = lj_color_to_int(
125 gtk_entry_get_text(GTK_ENTRY(feui->efgcolor)));
126 feui->editfriend->background = lj_color_to_int(
127 gtk_entry_get_text(GTK_ENTRY(feui->ebgcolor)));
129 lj_editfriends_free(ef);
130 net_ctx_gtk_free(ctx);
131 return TRUE;
134 static void
135 entry_changed(GtkEntry *entry, FriendEditUI* feui) {
136 gtk_dialog_set_response_sensitive(GTK_DIALOG(feui->win),
137 GTK_RESPONSE_OK,
138 (strlen(gtk_entry_get_text(entry)) > 0));
141 LJFriend*
142 friend_edit_dlg_run(GtkWindow *parent, JamAccountLJ *acc, gboolean edit, LJFriend *f) {
143 FriendEditUI feui_actual = { 0 };
144 FriendEditUI *feui = &feui_actual;
145 GtkWidget *button;
146 GtkWidget *table;
148 feui->account = acc;
149 feui->editfriend = f;
151 feui->win = gtk_dialog_new_with_buttons(
152 edit ? _("Edit Friend") :
153 (f ? _("Add This Friend") : _("Add a Friend")),
154 parent, GTK_DIALOG_MODAL,
155 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
156 edit ? _("Change") : _("Add"), GTK_RESPONSE_OK,
157 NULL);
158 gtk_dialog_set_default_response(GTK_DIALOG(feui->win), GTK_RESPONSE_OK);
160 table = jam_table_new(3, 3);
162 /* make the labels/entries */
163 feui->eusername = gtk_entry_new();
164 gtk_entry_set_activates_default(GTK_ENTRY(feui->eusername), TRUE);
165 gtk_entry_set_max_length(GTK_ENTRY(feui->eusername), 18);
166 if (edit)
167 gtk_editable_set_editable(GTK_EDITABLE(feui->eusername), FALSE);
168 else
169 /* enable/disable the button based on name text */
170 g_signal_connect(G_OBJECT(feui->eusername), "changed",
171 G_CALLBACK(entry_changed), feui);
173 gtk_widget_set_size_request(feui->eusername, 100, -1);
174 jam_table_label_content(GTK_TABLE(table), 0,
175 _("Friend's _username:"), feui->eusername);
177 feui->efgcolor = gtk_entry_new();
178 gtk_entry_set_activates_default(GTK_ENTRY(feui->efgcolor), TRUE);
179 gtk_entry_set_max_length(GTK_ENTRY(feui->efgcolor), 7);
180 g_signal_connect(G_OBJECT(feui->efgcolor), "changed",
181 G_CALLBACK(color_entry_changed), feui);
182 gtk_widget_set_size_request(feui->efgcolor, 100, -1);
183 jam_table_label_content(GTK_TABLE(table), 1,
184 _("_Text color:"), feui->efgcolor);
186 feui->ebgcolor = gtk_entry_new();
187 gtk_entry_set_activates_default(GTK_ENTRY(feui->ebgcolor), TRUE);
188 gtk_entry_set_max_length(GTK_ENTRY(feui->ebgcolor), 7);
189 g_signal_connect(G_OBJECT(feui->ebgcolor), "changed",
190 G_CALLBACK(color_entry_changed), feui);
191 gtk_widget_set_size_request(feui->ebgcolor, 100, -1);
192 jam_table_label_content(GTK_TABLE(table), 2,
193 _("_Background color:"), feui->ebgcolor);
195 /* make the color selector buttons */
196 button = gtk_button_new_with_label(" ... ");
197 gtk_table_attach(GTK_TABLE(table), button, 2, 3, 1, 2, GTK_FILL, 0, 2, 2);
198 g_signal_connect(G_OBJECT(button), "clicked",
199 G_CALLBACK(change_col_fg), feui);
200 button = gtk_button_new_with_label(" ... ");
201 gtk_table_attach(GTK_TABLE(table), button, 2, 3, 2, 3, GTK_FILL, 0, 2, 2);
202 g_signal_connect(G_OBJECT(button), "clicked",
203 G_CALLBACK(change_col_bg), feui);
205 jam_dialog_set_contents(GTK_DIALOG(feui->win), table);
207 /* fill in default values. */
208 if (f) {
209 gtk_entry_set_text(GTK_ENTRY(feui->eusername), f->username);
210 } else {
211 /* emit the "changed" signal, in any case. */
212 g_signal_emit_by_name(G_OBJECT(feui->eusername), "changed");
215 if (!edit) {
216 gtk_entry_set_text(GTK_ENTRY(feui->efgcolor), "#000000");
217 gtk_entry_set_text(GTK_ENTRY(feui->ebgcolor), "#FFFFFF");
218 } else {
219 char color[10];
220 lj_int_to_color(f->foreground, color);
221 gtk_entry_set_text(GTK_ENTRY(feui->efgcolor), color);
222 lj_int_to_color(f->background, color);
223 gtk_entry_set_text(GTK_ENTRY(feui->ebgcolor), color);
226 while (gtk_dialog_run(GTK_DIALOG(feui->win)) == GTK_RESPONSE_OK) {
227 if (add_the_friend(feui)) {
228 gtk_widget_destroy(feui->win);
229 return feui->editfriend;
232 gtk_widget_destroy(feui->win);
233 return NULL;