source: variable.c @ 353719a

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