source: variable.c @ 06e04a9

Last change on this file since 06e04a9 was 06e04a9, checked in by David Benjamin <davidben@mit.edu>, 8 years ago
perlvariables - iteration N Later commits reworked a lot of this, but the use of GValue over void* was kept.
  • Property mode set to 100644
File size: 53.8 KB
Line 
1#include "owl.h"
2#include <stdio.h>
3#include "gmarshal_funcs.h"
4
5static const GType owl_variable_gtype_map[] = {G_TYPE_POINTER, 
6                                               G_TYPE_INT, G_TYPE_BOOLEAN,
7                                               G_TYPE_STRING };
8
9#define OWLVAR_BOOL(name,default,summary,description) \
10        { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \
11            NULL, NULL, NULL, NULL, NULL, NULL, NULL }
12
13#define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \
14        { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \
15            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL, NULL, NULL }
16
17#define OWLVAR_INT(name,default,summary,description) \
18        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, "<int>", g_strdup(summary), g_strdup(description), NULL, \
19            NULL, NULL, NULL, NULL, NULL, NULL, NULL }
20
21#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
22        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
23            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL, NULL, NULL }
24
25#define OWLVAR_PATH(name,default,summary,description) \
26        { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<path>", g_strdup(summary), g_strdup(description),  NULL, \
27            NULL, NULL, NULL, NULL, NULL, NULL, NULL }
28
29#define OWLVAR_STRING(name,default,summary,description) \
30        { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<string>", g_strdup(summary), g_strdup(description), NULL, \
31            NULL, NULL, NULL, NULL, NULL, NULL, NULL }
32
33#define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \
34        { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, validset, g_strdup(summary), g_strdup(description), NULL, \
35            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL, NULL, NULL }
36
37/* enums are really integers, but where validset is a comma-separated
38 * list of strings which can be specified.  The tokens, starting at 0,
39 * correspond to the values that may be specified. */
40#define OWLVAR_ENUM(name,default,summary,description,validset) \
41        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
42            G_CALLBACK(owl_variable_enum_validate),                     \
43            NULL, G_CALLBACK(owl_variable_enum_set_fromstring),         \
44            NULL, G_CALLBACK(owl_variable_enum_get_tostring),           \
45            NULL, NULL }
46
47#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
48        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
49            G_CALLBACK(validate),                                       \
50            G_CALLBACK(set), G_CALLBACK(owl_variable_enum_set_fromstring), \
51            G_CALLBACK(get), G_CALLBACK(owl_variable_enum_get_tostring), \
52            NULL, NULL }
53
54int owl_variable_add_defaults(owl_vardict *vd)
55{
56  owl_variable_init_params variables_to_init[] = {
57
58  OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off",
59                 "ring the terminal bell when personal messages are received",
60                 "Can be set to 'on', 'off', or the name of a filter which\n"
61                 "messages need to match in order to ring the bell"),
62
63  OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1,
64               "enable / disable the terminal bell", "" ),
65
66  OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG,
67                    "whether debugging is enabled",
68                    "If set to 'on', debugging messages are logged to the\n"
69                    "file specified by the debugfile variable.\n",
70                    NULL, owl_variable_debug_set, NULL),
71
72  OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1,
73               "send a login message when BarnOwl starts", "" ),
74
75  OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1,
76               "send a logout message when BarnOwl exits", "" ),
77
78  OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0,
79               "display received pings", "" ),
80
81  OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1,
82               "send pings", "" ),
83
84  OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0,
85               "disable printing information in the separator bar", "" ),
86
87  OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1,
88               "strip kerberos instance for reply", ""),
89
90  OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1,
91               "strip leading and trailing newlines", ""),
92
93  OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1,
94               "display outgoing messages", "" ),
95
96  OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1,
97               "load logins from .anyone on startup", "" ),
98
99  OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0,
100               "turn personal logging on or off", 
101               "If this is set to on, personal messages are\n"
102               "logged in the directory specified\n"
103               "by the 'logpath' variable.  The filename in that\n"
104               "directory is derived from the sender of the message.\n" ),
105
106  OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0,
107               "turn class logging on or off",
108               "If this is set to on, class messages are\n"
109               "logged in the directory specified\n"
110               "by the 'classlogpath' variable.\n" 
111               "The filename in that directory is derived from\n"
112               "the name of the class to which the message was sent.\n" ),
113
114  OWLVAR_ENUM( "loggingdirection" /* %OwlVarStub */, OWL_LOGGING_DIRECTION_BOTH,
115               "specifies which kind of messages should be logged",
116               "Can be one of 'both', 'in', or 'out'.  If 'in' is\n"
117               "selected, only incoming messages are logged, if 'out'\n"
118               "is selected only outgoing messages are logged.  If 'both'\n"
119               "is selected both incoming and outgoing messages are\n"
120               "logged.",
121               "both,in,out"),
122
123  OWLVAR_BOOL_FULL( "colorztext" /* %OwlVarStub */, 1,
124                    "allow @color() in zephyrs to change color",
125                    NULL, NULL, owl_variable_colorztext_set, NULL),
126
127  OWLVAR_BOOL( "fancylines" /* %OwlVarStub */, 1,
128               "Use 'nice' line drawing on the terminal.",
129               "If turned off, dashes, pipes and pluses will be used\n"
130               "to draw lines on the screen.  Useful when the terminal\n"
131               "is causing problems" ),
132
133  OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1,
134               "Do automatic zcrypt processing",
135               "" ),
136
137  OWLVAR_BOOL_FULL( "pseudologins" /* %OwlVarStub */, 0,
138                    "Enable zephyr pseudo logins",
139                    "When this is enabled, BarnOwl will periodically check the zephyr\n"
140                    "location of users in your .anyone file.  If a user is present\n"
141                    "but sent no login message, or a user is not present that sent no\n"
142                    "logout message, a pseudo login or logout message will be created\n",
143                    NULL, owl_variable_pseudologins_set, NULL),
144
145  OWLVAR_BOOL( "ignorelogins" /* %OwlVarStub */, 0,
146               "Enable printing of login notifications",
147               "When this is enabled, BarnOwl will print login and logout notifications\n"
148               "for AIM, zephyr, or other protocols.  If disabled BarnOwl will not print\n"
149               "login or logout notifications.\n"),
150
151  OWLVAR_STRING( "logfilter" /* %OwlVarStub */, "",
152                 "name of a filter controlling which messages to log",
153
154                 "If non empty, any messages matching the given filter will be logged.\n"
155                 "This is a completely separate mechanism from the other logging\n"
156                 "variables like logging, classlogging, loglogins, loggingdirection,\n"
157                 "etc.  If you want this variable to control all logging, make sure\n"
158                 "all other logging variables are in their default state.\n"),
159
160  OWLVAR_BOOL( "loglogins" /* %OwlVarStub */, 0,
161               "Enable logging of login notifications",
162               "When this is enabled, BarnOwl will log login and logout notifications\n"
163               "for AIM, zephyr, or other protocols.  If disabled BarnOwl will not print\n"
164               "login or logout notifications.\n"),
165
166  OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1,
167                    "don't send zephyrs on C-d",
168                    "If set to 'on', C-d won't send a zephyr from the edit\n"
169                    "window.  If set to 'off', C-d will always send a zephyr\n"
170                    "being composed in the edit window.  If set to 'middle',\n"
171                    "C-d will only ever send a zephyr if the cursor is at\n"
172                    "the end of the message being composed.\n\n"
173                    "Note that this works by changing the C-d keybinding\n"
174                    "in the editmulti keymap.\n",
175                    "off,middle,on",
176                    NULL, owl_variable_disable_ctrl_d_set, NULL),
177
178  OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people",
179               "path for logging personal zephyrs", 
180               "Specifies a directory which must exist.\n"
181               "Files will be created in the directory for each sender.\n"),
182
183  OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class",
184               "path for logging class zephyrs",
185               "Specifies a directory which must exist.\n"
186               "Files will be created in the directory for each class.\n"),
187
188  OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE,
189               "path for logging debug messages when debugging is enabled",
190               "This file will be logged to if 'debug' is set to 'on'.\n"
191               "BarnOwl will append a dot and the current process's pid to the filename."),
192 
193  OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL,
194               "name of a program to run that will generate zsigs",
195               "This program should produce a zsig on stdout when run.\n"
196               "Note that it is important that this program not block.\n\n"
197               "See the documentation for 'zsig' for more information about\n"
198               "how the outgoing zsig is chosen."
199               ),
200
201  OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL,
202               "name of a program to run when new messages are present",
203               "The named program will be run when BarnOwl receives new\n"
204               "messages.  It will not be run again until the first\n"
205               "instance exits"),
206
207  OWLVAR_STRING( "zsender" /* %OwlVarStub */, "",
208             "zephyr sender name",
209         "Allows you to customize the outgoing username in\n"
210         "zephyrs.  If this is unset, it will use your Kerberos\n"
211         "principal. Note that customizing the sender name will\n"
212         "cause your zephyrs to be sent unauthenticated."),
213
214  OWLVAR_STRING( "zsigfunc" /* %OwlVarStub */, "BarnOwl::default_zephyr_signature()",
215                 "zsig perl function",
216                 "Called every time you start a zephyrgram without an\n"
217                 "explicit zsig.  The default setting implements the policy\n"
218                 "described in the documentation for the 'zsig' variable.\n"
219                 "See also BarnOwl::random_zephyr_signature().\n"),
220
221  OWLVAR_STRING( "zsig" /* %OwlVarStub */, "",
222                 "zephyr signature",
223                 "The zsig to get on outgoing messages. If this variable is\n"
224                 "unset, 'zsigproc' will be run to generate a zsig. If that is\n"
225                 "also unset, the 'zwrite-signature' zephyr variable will be\n"
226                 "used instead.\n"),
227
228  OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "",
229                 "string to append to the end of the sepbar",
230                 "The sepbar is the bar separating the top and bottom\n"
231                 "of the BarnOwl screen.  Any string specified here will\n"
232                 "be displayed on the right of the sepbar\n"),
233
234  OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0,
235               "turn zaway on or off", "" ),
236
237  OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 
238                 OWL_DEFAULT_ZAWAYMSG,
239                 "zaway msg for responding to zephyrs when away", "" ),
240
241  OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 
242                 OWL_DEFAULT_ZAWAYMSG,
243                 "default zaway message", "" ),
244
245  OWLVAR_BOOL_FULL( "aaway" /* %OwlVarStub */, 0,
246                    "Set AIM away status",
247                    "",
248                    NULL, owl_variable_aaway_set, NULL),
249
250  OWLVAR_STRING( "aaway_msg" /* %OwlVarStub */, 
251                 OWL_DEFAULT_AAWAYMSG,
252                 "AIM away msg for responding when away", "" ),
253
254  OWLVAR_STRING( "aaway_msg_default" /* %OwlVarStub */, 
255                 OWL_DEFAULT_AAWAYMSG,
256                 "default AIM away message", "" ),
257
258  OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all",
259                 "home view to switch to after 'X' and 'V'", 
260                 "SEE ALSO: view, filter\n" ),
261
262  OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none",
263                 "filter on which to trigger alert actions",
264                 "" ),
265
266  OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop",
267                 "BarnOwl command to execute for alert actions",
268                 "" ),
269
270  OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "<string>", "tty name for zephyr location", "",
271                      NULL, owl_variable_tty_set, NULL),
272
273  OWLVAR_STRING( "default_style" /* %OwlVarStub */, "default",
274                 "name of the default formatting style",
275                 "This sets the default message formatting style.\n"
276                 "Styles may be created with the 'style' command.\n"
277                 "Some built-in styles include:\n"
278                 "   default  - the default BarnOwl formatting\n"
279                 "   oneline  - one line per-message\n"
280                 "   perl     - legacy perl interface\n"
281                 "\nSEE ALSO: style, show styles, view -s <style>\n"
282                 ),
283
284
285  OWLVAR_INT(    "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70,
286                 "maximum number of columns for M-q (edit:fill-paragraph) to fill text to",
287                 "This specifies the maximum number of columns for M-q to fill text\n"
288                 "to.  If set to 0, M-q will wrap to the width of the window, and\n"
289                 "values less than 0 disable M-q entirely.\n"),
290
291  OWLVAR_INT(    "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70,
292                 "maximum number of columns for line-wrapping",
293                 "This specifies the maximum number of columns for\n"
294                 "auto-line-wrapping.  If set to 0, text will be wrapped at the\n"
295                 "window width. Values less than 0 disable automatic wrapping.\n"
296                 "\n"
297                 "As a courtesy to recipients, it is recommended that outgoing\n"
298                 "Zephyr messages be no wider than 70 columns.\n"),
299
300  OWLVAR_INT( "aim_ignorelogin_timer" /* %OwlVarStub */, 15,
301              "number of seconds after AIM login to ignore login messages",
302              "This specifies the number of seconds to wait after an\n"
303              "AIM login before allowing the receipt of AIM login notifications.\n"
304              "By default this is set to 15.  If you would like to view login\n"
305              "notifications of buddies as soon as you login, set it to 0 instead."),
306
307             
308  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
309                   OWL_TYPWIN_SIZE,
310                  "number of lines in the typing window", 
311                   "This specifies the height of the window at the\n"
312                   "bottom of the screen where commands are entered\n"
313                   "and where messages are composed.\n",
314                   "int > 0",
315                   owl_variable_int_validate_gt0,
316                   owl_variable_typewinsize_set,
317                   NULL /* use default for get */
318                   ),
319
320  OWLVAR_INT( "typewindelta" /* %OwlVarStub */, 0,
321                  "number of lines to add to the typing window when in use",
322                   "On small screens you may want the typing window to\n"
323                   "auto-hide when not entering a command or message.\n"
324                   "This variable is the number of lines to add to the\n"
325           "typing window when it is in use; you can then set\n"
326           "typewinsize to 1.\n\n"
327           "This works a lot better with a non-default scrollmode;\n"
328           "try :set scrollmode pagedcenter.\n"),
329
330  OWLVAR_ENUM( "scrollmode" /* %OwlVarStub */, OWL_SCROLLMODE_NORMAL,
331               "how to scroll up and down",
332               "This controls how the screen is scrolled as the\n"
333               "cursor moves between messages being displayed.\n"
334               "The following modes are supported:\n\n"
335               "   normal      - This is the BarnOwl default.  Scrolling happens\n"
336               "                 when it needs to, and an attempt is made to\n"
337               "                 keep the current message roughly near\n"
338               "                 the middle of the screen.\n"
339               "   top         - The current message will always be the\n"
340               "                 the top message displayed.\n"
341               "   neartop     - The current message will be one down\n"
342               "                 from the top message displayed,\n"
343               "                 where possible.\n"
344               "   center      - An attempt is made to keep the current\n"
345               "                 message near the center of the screen.\n"
346               "   paged       - The top message displayed only changes\n"
347               "                 when user moves the cursor to the top\n"
348               "                 or bottom of the screen.  When it moves,\n"
349               "                 the screen will be paged up or down and\n"
350               "                 the cursor will be near the top or\n"
351               "                 the bottom.\n"
352               "   pagedcenter - The top message displayed only changes\n"
353               "                 when user moves the cursor to the top\n"
354               "                 or bottom of the screen.  When it moves,\n"
355               "                 the screen will be paged up or down and\n"
356               "                 the cursor will be near the center.\n",
357               "normal,top,neartop,center,paged,pagedcenter" ),
358
359  OWLVAR_BOOL( "narrow-related" /* %OwlVarStub:narrow_related */, 1,
360               "Make smartnarrow use broader filters",
361               "Causes smartfilter to narrow to messages \"related\" to \n"
362               "the current message, as well as ones to the same place.\n\n"
363               "for Zephyr, this controls whether to narrow to e.g. class-help or\n"
364               "class-help.d alone, or to related-class-help, which includes\n"
365               "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" ),
366
367  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
368               "enable automatic following of the last zephyr",
369               "If the cursor is at the last message, it will\n"
370               "continue to follow the last message if this is set.\n"
371               "Note that this is currently risky as you might accidentally\n"
372               "delete a message right as it came in.\n" ),
373
374  OWLVAR_STRING_FULL( "default_exposure" /* %OwlVarStub */, "",
375                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
376                      "controls the persistent value for exposure",
377                      "The default exposure level corresponds to the Zephyr exposure value\n"
378                      "in ~/.zephyr.vars.  Defaults to realm-visible if there is no value in\n"
379                      "~/.zephyr.vars.\n"
380                      "See the description of exposure for the values this can be.",
381                      NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get ),
382
383  OWLVAR_STRING_FULL( "exposure" /* %OwlVarStub */, "",
384                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
385                      "controls who can zlocate you",
386                      "The exposure level, defaulting to the value of default_exposure,\n"
387                      "can be one of the following (from least exposure to widest exposure,\n"
388                      "as listed in zctl(1)):\n"
389                      "\n"
390                      "   none            - This completely disables Zephyr for the user. \n"
391                      "                     The user is not registered with Zephyr.  No user\n"
392                      "                     location information is retained by Zephyr.  No\n"
393                      "                     login or logout announcements will be sent.  No\n"
394                      "                     subscriptions will be entered for the user, and\n"
395                      "                     no notices will be displayed by zwgc(1).\n"
396                      "   opstaff         - The user is registered with Zephyr.  No login or\n"
397                      "                     logout announcements will be sent, and location\n"
398                      "                     information will only be visible to Operations\n"
399                      "                     staff.  Default subscriptions and any additional\n"
400                      "                     personal subscriptions will be entered for the\n"
401                      "                     user.\n"
402                      "   realm-visible   - The user is registered with Zephyr.  User\n"
403                      "                     location information is retained by Zephyr and\n"
404                      "                     made available only to users within the user’s\n"
405                      "                     Kerberos realm.  No login or logout\n"
406                      "                     announcements will be sent.  This is the system\n"
407                      "                     default.  Default subscriptions and any\n"
408                      "                     additional personal subscriptions will be\n"
409                      "                     entered for the user.\n"
410                      "   realm-announced - The user is registered with Zephyr.  User\n"
411                      "                     location information is retained by Zephyr and\n"
412                      "                     made available only to users authenticated\n"
413                      "                     within the user’s Kerberos realm.  Login and\n"
414                      "                     logout announcements will be sent, but only to\n"
415                      "                     users within the user’s Kerberos realm who have\n"
416                      "                     explicitly requested such via subscriptions. \n"
417                      "                     Default subscriptions and any additional\n"
418                      "                     personal subscriptions will be entered for the\n"
419                      "                     user.\n"
420                      "   net-visible     - The user is registered with Zephyr.  User\n"
421                      "                     location information is retained by Zephyr and\n"
422                      "                     made available to any authenticated user who\n"
423                      "                     requests such.  Login and logout announcements\n"
424                      "                     will be sent only to users within the user’s\n"
425                      "                     Kerberos realm who have explicitly requested\n"
426                      "                     such via subscriptions.  Default subscriptions\n"
427                      "                     and any additional personal subscriptions will\n"
428                      "                     be entered for the user.\n"
429                      "   net-announced   - The user is registered with Zephyr.  User\n"
430                      "                     location information is retained by Zephyr and\n"
431                      "                     made available to any authenticated user who\n"
432                      "                     requests such.  Login and logout announcements\n"
433                      "                     will be sent to any user has requested such. \n"
434                      "                     Default subscriptions and any additional\n"
435                      "                     personal subscriptions will be entered for the\n"
436                      "                     user.\n",
437                      NULL, owl_variable_exposure_set, NULL /* use default for get */ ),
438
439  /* This MUST be last... */
440  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
441    NULL, NULL, NULL, NULL, NULL, NULL }
442
443  };
444
445  int ret = owl_variable_dict_add_from_list(vd, variables_to_init);
446  owl_variable_init_params *var;
447  for (var = variables_to_init; var->name != NULL; var++)
448    owl_variable_cleanup_initializer(var);
449  return ret;
450}
451
452/**************************************************************************/
453/*********************** SPECIFIC TO VARIABLES ****************************/
454/**************************************************************************/
455
456
457/* commonly useful */
458
459int owl_variable_int_validate_gt0(const owl_variable *v, const int newval, void *dummy)
460{
461  return !(newval < 1);
462}
463
464int owl_variable_int_validate_positive(const owl_variable *v, const int newval, void *dummy)
465{
466  return !(newval < 0);
467}
468
469/* typewinsize */
470int owl_variable_typewinsize_set(owl_variable *v, const int newval, void *dummy)
471{
472  int rv;
473  rv = owl_variable_int_set_default(v, newval, dummy);
474  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
475  return(rv);
476}
477
478/* debug (cache value in g->debug) */
479int owl_variable_debug_set(owl_variable *v, const int newval, void *dummy)
480{
481  if (newval == 1 || newval == 0) {
482    g.debug = newval;
483  }
484  return owl_variable_bool_set_default(v, newval, dummy);
485}
486
487/* When 'aaway' is changed, need to notify the AIM server */
488int owl_variable_aaway_set(owl_variable *v, const gboolean newval, void *dummy)
489{
490  if (newval == 1) {
491    owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g));
492  } else if (newval == 0) {
493    owl_aim_set_awaymsg("");
494  }
495  return owl_variable_bool_set_default(v, newval, dummy);
496}
497
498int owl_variable_colorztext_set(owl_variable *v, const void *newval, void *dummy)
499{
500  int ret = owl_variable_bool_set_default(v, newval, dummy);
501  /* flush the format cache so that we see the update, but only if we're done initializing BarnOwl */
502  if (owl_global_get_msglist(&g) != NULL)
503    owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
504  if (owl_global_get_mainwin(&g) != NULL) {
505    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
506    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
507  }
508  return ret;
509}
510
511int owl_variable_pseudologins_set(owl_variable *v, const int newval, void *dummy)
512{
513  static guint timer = 0;
514  if (newval == 1) {
515    owl_function_zephyr_buddy_check(0);
516    if (timer == 0) {
517      timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL);
518    }
519  } else {
520    if (timer != 0) {
521      g_source_remove(timer);
522      timer = 0;
523    }
524  }
525  return owl_variable_bool_set_default(v, newval, dummy);
526}
527
528/* note that changing the value of this will clobber
529 * any user setting of this */
530int owl_variable_disable_ctrl_d_set(owl_variable *v, const int newval, void *dummy)
531{
532  if (!owl_context_is_startup(owl_global_get_context(&g))) {
533    if (newval == 2) {
534      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
535    } else if (newval == 1) {
536      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
537    } else {
538      owl_function_command_norv("bindkey editmulti C-d command edit:done");
539    }
540  } 
541  return owl_variable_int_set_default(v, newval, dummy); 
542}
543
544int owl_variable_tty_set(owl_variable *v, const char *newval, void *dummy)
545{
546  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
547  return(owl_variable_string_set_default(v, newval, dummy));
548}
549
550int owl_variable_default_exposure_set(owl_variable *v, const char *newval, void *dummy)
551{
552  return owl_zephyr_set_default_exposure(newval);
553}
554
555const char *owl_variable_default_exposure_get(const owl_variable *v, void *dummy)
556{
557  return owl_zephyr_get_default_exposure();
558}
559
560int owl_variable_exposure_set(owl_variable *v, const char *newval, void *dummy)
561{
562  int ret = owl_zephyr_set_exposure(newval);
563  if (ret != 0)
564    return ret;
565  return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval), dummy);
566}
567
568/**************************************************************************/
569/****************************** GENERAL ***********************************/
570/**************************************************************************/
571
572int owl_variable_dict_setup(owl_vardict *vd) {
573  owl_dict_create(vd);
574  return owl_variable_add_defaults(vd);
575}
576
577CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v,
578                                                      GCallback fn,
579                                                      GClosureMarshal marshal) {
580  GClosure *closure = g_cclosure_new(fn, NULL, NULL);
581  g_closure_set_marshal(closure,marshal);
582  g_closure_ref(closure);
583  g_closure_sink(closure);
584  return closure;
585}
586
587#define OWL_VARIABLE_SETUP_FUNC(variable, initializer, func_name, default_func, marshal_func, temp) do { \
588  if(initializer->func_name) { \
589    temp = initializer->func_name; \
590  } else { \
591    temp = default_func; \
592  } \
593  variable->func_name = owl_variable_make_closure(variable, G_CALLBACK(temp), \
594                                                  marshal_func);        \
595  } while(0)
596 
597
598int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable_init_params *variables_to_init)
599{
600  owl_variable *newvar = NULL;
601  owl_variable_init_params *init_params = NULL;
602  for (init_params = variables_to_init; init_params->name; init_params++) {
603    newvar = g_new0(owl_variable, 1);
604    newvar->type = init_params->type;
605    /* strdup all the strings so we can delete them consistently. */
606    newvar->name = g_strdup(init_params->name);
607    newvar->summary = g_strdup(init_params->summary);
608    newvar->description = g_strdup(init_params->description);
609    newvar->validsettings = init_params->validsettings;
610    GValue values[] = {{0}, {0}};
611    GValue *value = values+1;
612    GValue ret = {0};
613    GCallback fn = NULL;
614    switch (init_params->type) {
615    case OWL_VARIABLE_STRING:
616      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn, 
617                              G_CALLBACK(owl_variable_string_get_default), 
618                              g_cclosure_user_marshal_STRING__VOID, fn);
619      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn, 
620                              G_CALLBACK(owl_variable_string_set_default), 
621                              g_cclosure_user_marshal_INT__STRING, fn);
622      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn,
623                              G_CALLBACK(owl_variable_string_validate_default),
624                              g_cclosure_user_marshal_INT__STRING, fn);
625      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
626                              G_CALLBACK(owl_variable_string_set_fromstring_default),
627                              g_cclosure_user_marshal_INT__STRING, fn);
628      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
629                              G_CALLBACK(owl_variable_string_get_tostring_default),
630                              g_cclosure_user_marshal_STRING__STRING, fn);
631      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn, 
632                              G_CALLBACK(owl_variable_string_get_default_default), 
633                              g_cclosure_user_marshal_STRING__VOID, fn);
634
635      g_value_init(value,G_TYPE_STRING);
636      g_value_set_string(value, init_params->pval_default);
637      break;
638    case OWL_VARIABLE_BOOL:
639      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn, 
640                              G_CALLBACK(owl_variable_bool_get_default), 
641                              g_cclosure_user_marshal_BOOLEAN__VOID, fn);
642      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn, 
643                              G_CALLBACK(owl_variable_bool_set_default), 
644                              g_cclosure_user_marshal_INT__BOOLEAN, fn);
645      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn,
646                              G_CALLBACK(owl_variable_bool_validate_default),
647                              g_cclosure_user_marshal_INT__BOOLEAN, fn);
648      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
649                              G_CALLBACK(owl_variable_bool_set_fromstring_default),
650                              g_cclosure_user_marshal_INT__STRING, fn);
651      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
652                              G_CALLBACK(owl_variable_bool_get_tostring_default),
653                              g_cclosure_user_marshal_STRING__BOOLEAN, fn);
654      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn,
655                              G_CALLBACK(owl_variable_bool_get_default_default),
656                              g_cclosure_user_marshal_BOOLEAN__VOID, fn);
657
658      g_value_init(value,G_TYPE_BOOLEAN);
659      g_value_set_boolean(value, !!(init_params->ival_default));
660      break;
661    case OWL_VARIABLE_INT:
662      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn, 
663                              G_CALLBACK(owl_variable_int_get_default), 
664                              g_cclosure_user_marshal_INT__VOID, fn);
665      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn, 
666                              G_CALLBACK(owl_variable_int_set_default), 
667                              g_cclosure_user_marshal_INT__INT, fn);
668      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn,
669                              G_CALLBACK(owl_variable_int_validate_default),
670                              g_cclosure_user_marshal_INT__INT, fn);
671      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
672                              G_CALLBACK(owl_variable_int_set_fromstring_default),
673                              g_cclosure_user_marshal_INT__STRING, fn);
674      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
675                              G_CALLBACK(owl_variable_int_get_tostring_default),
676                              g_cclosure_user_marshal_STRING__INT, fn);
677      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn, 
678                              G_CALLBACK(owl_variable_int_get_default_default), 
679                              g_cclosure_user_marshal_INT__VOID, fn);
680
681      g_value_init(value,G_TYPE_INT);
682      g_value_set_int(value, init_params->ival_default);
683      break;
684    default:
685      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
686      return(-2);
687    }
688    OWL_VARIABLE_SETUP_FUNC(newvar, init_params, delete_fn, 
689                            G_CALLBACK(owl_variable_delete_default),
690                            g_cclosure_marshal_VOID__VOID, fn);
691   
692    g_value_init(&(newvar->gval_default), G_VALUE_TYPE(value));
693    g_value_init(&(newvar->val), G_VALUE_TYPE(value));
694    g_value_copy(value, &(newvar->gval_default));
695    /* we have the value boxed up already *anyway*, so... */
696    g_value_init(values, G_TYPE_POINTER);
697    g_value_set_pointer(values, newvar);
698    g_value_init(&ret, G_TYPE_INT);
699    g_closure_invoke(newvar->set_fn, &ret, 2, values, NULL);
700    g_value_unset(value);
701    owl_dict_insert_element(vd, newvar->name, newvar, NULL);
702  }
703  return 0;
704}
705
706void owl_variable_dict_add_variable(owl_vardict * vardict,
707                                    owl_variable * var) {
708  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
709}
710
711CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description)
712{
713  owl_variable *var = g_new0(owl_variable, 1);
714  var->name = g_strdup(name);
715  var->summary = g_strdup(summary);
716  var->description = g_strdup(description);
717  return var;
718}
719
720void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
721  g_free(var->summary);
722  var->summary = g_strdup(summary);
723  g_free(var->description);
724  var->description = g_strdup(desc);
725}
726
727#define OWL_VARIABLE_SETUP_DEFAULT_FUNCS(variable, type, gtype) do { \
728  variable->set_fn = owl_variable_make_closure(variable, \
729    G_CALLBACK(owl_variable_##type##_set_default), \
730    g_cclosure_user_marshal_INT__##gtype); \
731  variable->get_fn = owl_variable_make_closure(variable, \
732    G_CALLBACK(owl_variable_##type##_get_default), \
733    g_cclosure_user_marshal_##gtype##__##VOID); \
734  variable->validate_fn = owl_variable_make_closure(variable, \
735    G_CALLBACK(owl_variable_##type##_validate_default), \
736    g_cclosure_user_marshal_INT__##gtype); \
737  variable->set_fromstring_fn = owl_variable_make_closure(variable, \
738    G_CALLBACK(owl_variable_##type##_set_fromstring_default), \
739    g_cclosure_user_marshal_INT__STRING); \
740  variable->get_tostring_fn = owl_variable_make_closure(variable, \
741    G_CALLBACK(owl_variable_##type##_get_tostring_default), \
742    g_cclosure_user_marshal_STRING__##gtype); \
743  variable->get_default_fn = owl_variable_make_closure(variable, \
744    G_CALLBACK(owl_variable_##type##_get_default_default), \
745    g_cclosure_user_marshal_##gtype##__##VOID); \
746  } while(0);
747
748void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
749{
750  GValue default_gvals[] = {{0}, {0}};
751  GValue *default_gval = default_gvals+1;
752  GValue retval = {0}; 
753  g_value_init(default_gval, G_TYPE_STRING);
754  g_value_init(&retval, G_TYPE_INT);
755  g_value_set_static_string(default_gval, initval);
756  owl_variable *old = owl_variable_get_var(vd, name);
757  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
758    owl_variable_update(old, summ, desc);
759    g_value_copy(default_gval, &(old->gval_default));
760  } else {
761    owl_variable * var = owl_variable_newvar(name, summ, desc);
762    var->type = OWL_VARIABLE_STRING;
763    var->validsettings = "<string>";
764    g_value_init(&(var->val), G_TYPE_STRING);
765    g_value_init(default_gvals, G_TYPE_POINTER);
766    g_value_set_pointer(default_gvals, var);
767    g_value_init(&(var->gval_default), G_TYPE_STRING);
768    g_value_set_string(&(var->gval_default), initval);
769    OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, string, STRING);
770
771    g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL);
772    owl_variable_dict_add_variable(vd, var);
773  }
774}
775
776void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
777{
778  GValue default_gvals[] = {{0}, {0}};
779  GValue *default_gval = default_gvals+1;
780  GValue retval = {0}; 
781  g_value_init(default_gval, G_TYPE_INT);
782  g_value_init(&retval, G_TYPE_INT);
783  g_value_set_int(default_gval, initval);
784  owl_variable *old = owl_variable_get_var(vd, name);
785  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
786    owl_variable_update(old, summ, desc);
787    g_value_copy(default_gval, &(old->gval_default));
788  } else {
789    owl_variable * var = owl_variable_newvar(name, summ, desc);
790    var->type = OWL_VARIABLE_INT;
791    var->validsettings = "<int>";
792    g_value_init(&(var->val), G_TYPE_INT);
793    g_value_init(&(var->gval_default), G_TYPE_INT);
794    OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, int, INT);
795    g_value_init(default_gvals, G_TYPE_POINTER);
796    g_value_set_pointer(default_gvals, var);
797    g_value_set_int(&(var->gval_default), initval);
798    g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL);
799    /*    g_value_unset(&retval); */
800    owl_variable_dict_add_variable(vd, var);
801  }
802}
803
804void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, gboolean initval)
805{
806  GValue default_gvals[] = {{0}, {0}};
807  GValue *default_gval = default_gvals+1;
808  GValue retval = {0}; 
809  g_value_init(default_gval, G_TYPE_BOOLEAN);
810  g_value_init(&retval, G_TYPE_INT);
811  g_value_set_boolean(default_gval, initval);
812  owl_variable *old = owl_variable_get_var(vd, name);
813  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
814    owl_variable_update(old, summ, desc);
815    g_value_copy(default_gval, &(old->gval_default));
816  } else {
817    owl_variable * var = owl_variable_newvar(name, summ, desc);
818    var->type = OWL_VARIABLE_BOOL;
819    var->validsettings = "on,off";
820    g_value_init(&(var->val), G_TYPE_BOOLEAN);
821    g_value_init(&(var->gval_default), G_TYPE_BOOLEAN);
822    OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, bool, BOOLEAN);
823
824    g_value_init(default_gvals, G_TYPE_POINTER);
825    g_value_set_pointer(default_gvals, var);
826    g_value_set_int(&(var->gval_default), initval);
827    g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL); 
828    owl_variable_dict_add_variable(vd, var);
829  }
830}
831
832void owl_variable_dict_cleanup(owl_vardict *d)
833{
834  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
835}
836
837CALLER_OWN GPtrArray *owl_variable_dict_get_names(const owl_vardict *d) {
838  return owl_dict_get_keys(d);
839}
840
841void owl_variable_cleanup(owl_variable *v)
842{
843  GValue val = {0};
844  if (v->delete_fn) {
845    g_value_init(&val, G_TYPE_POINTER);
846    g_value_set_pointer(&val, v);
847    g_closure_invoke(v->delete_fn, NULL, 1, &val, NULL);
848  }
849  g_free(v->name);
850  g_free(v->summary);
851  g_free(v->description);
852  if (v->type == OWL_VARIABLE_STRING) {
853    g_value_unset(&v->gval_default);
854  }
855  g_closure_unref(v->get_fn);
856  g_closure_unref(v->set_fn);
857  g_closure_unref(v->validate_fn);
858  g_closure_unref(v->get_tostring_fn);
859  g_closure_unref(v->set_fromstring_fn);
860}
861 
862void owl_variable_cleanup_initializer(owl_variable_init_params *v) {
863  g_free(v->name);
864  g_free(v->summary);
865  g_free(v->description);
866  if (v->type == OWL_VARIABLE_STRING)
867    g_free(v->pval_default);
868 }
869
870void owl_variable_delete(owl_variable *v)
871{
872  owl_variable_cleanup(v);
873  g_free(v);
874}
875
876
877const char *owl_variable_get_name(const owl_variable *v)
878{
879  return v->name;
880}
881
882const char *owl_variable_get_description(const owl_variable *v) {
883  return v->description;
884}
885
886const char *owl_variable_get_summary(const owl_variable *v) {
887  return v->summary;
888}
889
890const char *owl_variable_get_validsettings(const owl_variable *v) {
891  return v->validsettings;
892}
893
894int owl_variable_get_type(const owl_variable *v)
895{
896  return v->type;
897}
898
899/* functions for getting and setting variable values */
900
901static CALLER_OWN  char *owl_variable_invoke_tostring(const owl_variable *v,
902                                                     const GValue *value) 
903{
904  GValue values[] = { {0}, {0}};
905  GValue *value_box = values+1;
906  GValue tostring_box = {0};
907  char *ret = NULL;
908  gboolean need_to_free = false;
909
910  g_value_init(values, G_TYPE_POINTER);
911  g_value_set_pointer(values, (gpointer)v);
912  g_value_init(&tostring_box, G_TYPE_STRING);
913  if(value) {
914    g_value_init(value_box, G_VALUE_TYPE(value));
915    g_value_copy(value, value_box);
916    need_to_free = true;
917  } else {
918    g_value_init(value_box, owl_variable_gtype_map[v->type]);
919    g_closure_invoke(v->get_fn, value_box, 1, values, NULL);
920  }
921  g_closure_invoke(v->get_tostring_fn,&tostring_box, 2, values, NULL);
922
923  ret = g_value_dup_string(&tostring_box);
924  g_value_unset(&tostring_box);
925  if(need_to_free) {
926    g_value_unset(value_box);
927  }
928
929  return ret;
930}
931
932/* returns 0 on success, prints a status msg if msg is true */
933int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
934  char *tostring;
935  GValue values[] = {{0},{0},{0}};
936  GValue *value_box = values+1;
937  GValue return_box = {0};
938  int set_successfully = -1;
939  if (!v->set_fromstring_fn) {
940    if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v));
941    return -1;   
942  }
943  g_value_init(value_box, G_TYPE_STRING);
944  g_value_init(values, G_TYPE_POINTER);
945  g_value_set_pointer(values, v);
946  g_value_init(&return_box, G_TYPE_INT);
947  g_value_set_static_string(value_box, value);
948  g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL);
949  set_successfully = g_value_get_int(&return_box);
950  if (0 != set_successfully) {
951    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
952                                owl_variable_get_validsettings(v));
953  }
954  if (msg && (0 != set_successfully)) {
955    tostring = owl_variable_invoke_tostring(v, NULL);
956    if (tostring) {
957      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
958    } else {
959      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
960    }
961    g_free(tostring);
962  }
963  g_value_unset(value_box);
964  return set_successfully;
965}
966
967static int owl_variable_invoke_setter(owl_variable *v, const GValue *value) {
968  GValue values[] = {{0},{0}};
969  GValue return_box = {0};
970  int ret = -1;
971  g_value_init(&return_box, G_TYPE_INT);
972  g_value_init(values, G_TYPE_POINTER);
973  g_value_set_pointer(values, v);
974  g_value_init(values+1, G_VALUE_TYPE(value));
975  g_value_copy(value, values+1);
976  g_closure_invoke(v->set_fn, &return_box, 2, values, NULL);
977  ret = g_value_get_int(&return_box);
978  /*  g_value_unset(&return_box); */
979  g_value_unset(values+1);
980  return ret;
981}
982 
983int owl_variable_set_string(owl_variable *v, const char *newval)
984{
985  int ret = -1;
986  GValue value_box = {0};
987  if (v->type != OWL_VARIABLE_STRING) return -1;
988  g_value_init(&value_box, G_TYPE_STRING);
989  g_value_set_static_string(&value_box, newval);
990  ret = owl_variable_invoke_setter(v, &value_box);
991  g_value_unset(&value_box);
992  return ret;
993}
994
995int owl_variable_set_int(owl_variable *v, int newval)
996{
997  int ret = -1;
998  GValue value_box = {0};
999  if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1;
1000  g_value_init(&value_box, G_TYPE_INT);
1001  g_value_set_int(&value_box, newval);
1002  ret = owl_variable_invoke_setter(v, &value_box);
1003  /* g_value_unset(&value_box); */
1004  return ret;
1005}
1006
1007int owl_variable_set_bool(owl_variable *v, gboolean newval) {
1008  int ret = -1;
1009  GValue value_box = {0};
1010  if (v->type != OWL_VARIABLE_BOOL) return -1;
1011  g_value_init(&value_box, G_TYPE_BOOLEAN);
1012  g_value_set_int(&value_box, newval);
1013  ret = owl_variable_invoke_setter(v, &value_box);
1014  /* g_value_unset(&value_box); */
1015  return ret;
1016}
1017
1018int owl_variable_set_bool_on(owl_variable *v)
1019{
1020  if (v->type != OWL_VARIABLE_BOOL) return -1;
1021  return owl_variable_set_bool(v, true);
1022}
1023
1024int owl_variable_set_bool_off(owl_variable *v)
1025{
1026  if (v->type != OWL_VARIABLE_BOOL) return -1;
1027  return owl_variable_set_bool(v, false);
1028}
1029
1030CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
1031{
1032  return owl_variable_invoke_tostring(v, NULL);
1033}
1034
1035CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v)
1036{
1037  char *ret = NULL;
1038  GValue default_value_box = {0};
1039  GValue variable_box = {0};
1040  g_value_init(&variable_box, G_TYPE_POINTER);
1041  g_value_init(&default_value_box, owl_variable_gtype_map[v->type]);
1042  g_value_set_pointer(&variable_box, (gpointer)v);
1043  g_closure_invoke(v->get_default_fn, &default_value_box, 1, 
1044                   &variable_box, NULL); 
1045  ret = owl_variable_invoke_tostring(v, &default_value_box);
1046  g_value_unset(&default_value_box);
1047  return ret;
1048}
1049
1050owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
1051{
1052  return owl_dict_find_element(d, name);
1053}
1054
1055const GValue *owl_variable_get(const owl_variable *v, GValue *val)
1056{
1057  GValue variable_box = {0};
1058  g_value_init(&variable_box, G_TYPE_POINTER);
1059  g_value_set_pointer(&variable_box,(gpointer)v);
1060  g_closure_invoke(v->get_fn, val, 1, &variable_box, NULL); 
1061  return val;
1062}
1063
1064const char *owl_variable_get_string(const owl_variable *v)
1065{
1066  GValue value = {0};
1067  if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) {
1068    owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v));
1069    return NULL;
1070  }
1071  g_value_init(&value, G_TYPE_STRING);
1072  /* not a leak, since we don't own the underlying string */
1073  return g_value_get_string(owl_variable_get(v,&value));
1074}
1075
1076int owl_variable_get_int(const owl_variable *v)
1077{
1078  GValue value = {0};
1079  if (owl_variable_get_type(v) != OWL_VARIABLE_INT) {
1080    owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v));
1081    return -1;
1082  }
1083  g_value_init(&value, G_TYPE_INT);
1084  return g_value_get_int(owl_variable_get(v,&value));
1085}
1086
1087int owl_variable_get_bool(const owl_variable *v)
1088{
1089  GValue value = {0};
1090  if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
1091    owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v));
1092    return -1;
1093  }
1094  g_value_init(&value, G_TYPE_BOOLEAN);
1095  return g_value_get_boolean(owl_variable_get(v,&value));
1096}
1097
1098void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
1099{
1100  char *tostring = owl_variable_get_default_tostring(v);
1101  char *default_buf;
1102
1103  if (tostring)
1104    default_buf = g_strdup_printf("'%s'", tostring);
1105  else
1106    default_buf = g_strdup("<null>");
1107  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
1108                            owl_variable_get_name(v),
1109                            owl_variable_get_summary(v), default_buf);
1110  g_free(default_buf);
1111  g_free(tostring);
1112}
1113
1114void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
1115  char *tostring;
1116
1117  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
1118  owl_fmtext_append_normal(fm, OWL_TABSTR);
1119  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
1120  owl_fmtext_append_normal(fm, " - ");
1121  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
1122  owl_fmtext_append_normal(fm, "\n\n");
1123
1124  owl_fmtext_append_normal(fm, "Current:        ");
1125  tostring = owl_variable_get_tostring(v);
1126  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
1127  g_free(tostring);
1128  owl_fmtext_append_normal(fm, "\n\n");
1129
1130  tostring = owl_variable_get_default_tostring(v);
1131  owl_fmtext_append_normal(fm, "Default:        ");
1132  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
1133  owl_fmtext_append_normal(fm, "\n\n");
1134  g_free(tostring);
1135
1136  owl_fmtext_append_normal(fm, "Valid Settings: ");
1137  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
1138  owl_fmtext_append_normal(fm, "\n\n");
1139
1140  if (v->description && *v->description) {
1141    owl_fmtext_append_normal(fm, "Description:\n");
1142    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
1143    owl_fmtext_append_normal(fm, "\n\n");
1144  }
1145}
1146
1147
1148
1149
1150/**************************************************************************/
1151/*********************** GENERAL TYPE-SPECIFIC ****************************/
1152/**************************************************************************/
1153
1154/* default common functions */
1155
1156const char *owl_variable_string_get_default(const owl_variable *v, void *dummy) {
1157  return g_value_get_string(&(v->val));
1158}
1159
1160const char *owl_variable_string_get_default_default(const owl_variable *v, void *dummy) {
1161  return g_value_get_string(&(v->gval_default));
1162}
1163
1164
1165const int owl_variable_int_get_default(const owl_variable *v, void *dummy) {
1166  return g_value_get_int(&(v->val));
1167}
1168
1169const int owl_variable_int_get_default_default(const owl_variable *v, void *dummy) {
1170  return g_value_get_int(&(v->gval_default));
1171}
1172
1173
1174const gboolean owl_variable_bool_get_default(const owl_variable *v, void *dummy) {
1175  return g_value_get_boolean(&(v->val));
1176}
1177
1178const gboolean owl_variable_bool_get_default_default(const owl_variable *v, void *dummy) {
1179  return g_value_get_boolean(&(v->gval_default));
1180}
1181
1182
1183void owl_variable_delete_default(owl_variable *v, void *dummy)
1184{
1185  g_value_unset(&(v->val));
1186}
1187
1188/* default functions for booleans */
1189
1190int owl_variable_bool_validate_default(const owl_variable *v, const gboolean newval, void *dummy) {
1191  return (newval == 1) || (newval == 0);
1192}
1193
1194static int owl_variable_invoke_validator(owl_variable *v, const GValue *newval)
1195{
1196  GValue values[] = {{0},{0}};
1197  GValue ret = {0};
1198  g_value_init(&ret, G_TYPE_INT);
1199  g_value_init(values, G_TYPE_POINTER);
1200  g_value_set_pointer(values, v);
1201  g_value_init(values+1, G_VALUE_TYPE(newval));
1202  g_value_copy(newval, values+1);
1203  g_closure_invoke(v->validate_fn, &ret, 2, values, NULL);
1204  g_value_unset(values+1);
1205  return g_value_get_int(&ret);
1206}
1207
1208int owl_variable_bool_set_default(owl_variable *v, const bool newval, void *dummy) {
1209  GValue value = {0};
1210  g_value_init(&value, G_TYPE_BOOLEAN);
1211  g_value_set_boolean(&value, newval);
1212  if (v->validate_fn) {
1213    if (!owl_variable_invoke_validator(v,&value)) {
1214      return(-1);
1215    }
1216  }
1217  g_value_set_boolean(&(v->val), newval);
1218  return(0);
1219}
1220
1221int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
1222  gboolean i;
1223  GValue value = {0};
1224  if (!strcmp(newval, "on")) {
1225    i=1;
1226  } else if (!strcmp(newval, "off")) {
1227    i=0;
1228  } else {
1229    return(-1);
1230  }
1231  g_value_init(&value, G_TYPE_BOOLEAN);
1232  g_value_set_boolean(&value, i);
1233  return owl_variable_invoke_setter(v,&value);
1234}
1235
1236CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, const bool val, void *dummy)
1237{
1238  if (val == 0) {
1239    return g_strdup("off");
1240  } else if (val == 1) {
1241    return g_strdup("on");
1242  } else {
1243    return g_strdup("<invalid>");
1244  }
1245}
1246
1247/* default functions for integers */
1248
1249int owl_variable_int_validate_default(const owl_variable *v, const int newval, void *dummy)
1250{
1251  return (1);
1252}
1253
1254int owl_variable_int_set_default(owl_variable *v, const int newval, void *dummy) {
1255  GValue value = {0};
1256  g_value_init(&value, G_TYPE_INT);
1257  g_value_set_int(&value, newval);
1258  if (v->validate_fn) {
1259    if (!owl_variable_invoke_validator(v,&value)) {
1260      return(-1);
1261    }
1262  }
1263  g_value_set_int(&(v->val), newval);
1264  return(0);
1265}
1266
1267int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
1268  int i;
1269  char *ep;
1270  GValue value = {0};
1271  i = strtol(newval, &ep, 10);
1272  if (*ep || ep==newval) return(-1);
1273  g_value_init(&value, G_TYPE_INT);
1274  g_value_set_int(&value, i);
1275  return owl_variable_invoke_setter(v, &value);
1276}
1277
1278CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, const int val, void *dummy)
1279{
1280  return g_strdup_printf("%d", val);
1281}
1282
1283/* default functions for enums (a variant of integers) */
1284
1285int owl_variable_enum_validate(const owl_variable *v, const int newval, void *dummy) { 
1286  char **enums;
1287  int nenums, val;
1288  enums = g_strsplit_set(v->validsettings, ",", 0);
1289  nenums = g_strv_length(enums);
1290  g_strfreev(enums);
1291  val = newval;
1292  if (val < 0 || val >= nenums) {
1293    return(0);
1294  }
1295  return(1);
1296}
1297
1298int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval, void *dummy) {
1299  char **enums;
1300  int i, val=-1;
1301  GValue box = {0};
1302  if (newval == NULL) return(-1);
1303  enums = g_strsplit_set(v->validsettings, ",", 0);
1304  for (i = 0; enums[i] != NULL; i++) {
1305    if (0==strcmp(newval, enums[i])) {
1306      val = i;
1307    }
1308  }
1309  g_strfreev(enums);
1310  if (val == -1) return(-1);
1311  g_value_init(&box, G_TYPE_INT);
1312  g_value_set_int(&box, val);
1313  return owl_variable_invoke_setter(v, &box);
1314}
1315
1316CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, const int val, void *dummy)
1317{
1318  char **enums;
1319  int nenums, i;
1320  char *tostring;
1321
1322  enums = g_strsplit_set(v->validsettings, ",", 0);
1323  nenums = g_strv_length(enums);
1324  i = val;
1325  if (i<0 || i>=nenums) {
1326    g_strfreev(enums);
1327    return g_strdup_printf("<invalid:%d>", i);
1328  }
1329  tostring = g_strdup(enums[i]);
1330  g_strfreev(enums);
1331  return tostring;
1332}
1333
1334/* default functions for stringeans */
1335
1336int owl_variable_string_validate_default(const struct _owl_variable *v, const char *newval, void *dummy) {
1337  if (newval == NULL) return(0);
1338  else return (1);
1339}
1340
1341int owl_variable_string_set_default(owl_variable *v, const char *newval, void *dummy) {
1342  GValue value = {0};
1343  g_value_init(&value, G_TYPE_STRING);
1344  g_value_set_static_string(&value, newval);
1345  if (v->validate_fn) {
1346    if (!owl_variable_invoke_validator(v,&value)) {
1347      return(-1);
1348    }
1349  }
1350
1351  /* set_string dups the string for us */
1352  g_value_set_string(&(v->val), newval);
1353  return(0);
1354}
1355
1356int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 
1357{
1358  GValue val = {0};
1359  int ret = -1;
1360  g_value_init(&val, G_TYPE_STRING);
1361  /* we don't need to dup the string because we don't own it and the setter function we invoke will DTRT */
1362  g_value_set_static_string(&val, newval);
1363  ret = owl_variable_invoke_setter(v, &val);
1364  g_value_unset(&val);
1365  return ret;
1366}
1367
1368CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, const char *val, void *dummy)
1369{
1370  if(val) {
1371    return g_strdup(val);
1372  } else {
1373    return g_strdup("");
1374  }
1375}
1376
Note: See TracBrowser for help on using the repository browser.