source: variable.c @ 69f74c2

release-1.10
Last change on this file since 69f74c2 was 69f74c2, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
And now... the moment you've all been waiting for... Expose OWL_VARIABLE_OTHER variables to perl. Rewrite all perl variable entry points to use that one. From now on, C only thinks of these variables as strings. (This has one regression to be fixed in a later commit: you can't set/unset with no arguments since we don't know how to set from boolean.)
  • Property mode set to 100644
File size: 41.4 KB
Line 
1#include "owl.h"
2#include <stdio.h>
3#include "gmarshal_funcs.h"
4
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);
21
22#define OWLVAR_BOOL(name,default,summary,description) \
23        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \
24            NULL, NULL, NULL, NULL, NULL }
25
26#define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \
27        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \
28            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
29
30#define OWLVAR_INT(name,default,summary,description) \
31        { name, OWL_VARIABLE_INT, NULL, default, "<int>", summary, description, NULL, \
32            NULL, NULL, NULL, NULL, NULL }
33
34#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
35        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
36            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
37
38#define OWLVAR_PATH(name,default,summary,description) \
39        { name, OWL_VARIABLE_STRING, default, 0, "<path>", summary, description,  NULL, \
40            NULL, NULL, NULL, NULL, NULL }
41
42#define OWLVAR_STRING(name,default,summary,description) \
43        { name, OWL_VARIABLE_STRING, default, 0, "<string>", summary, description, NULL, \
44            NULL, NULL, NULL, NULL, NULL }
45
46#define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \
47        { name, OWL_VARIABLE_STRING, default, 0, validset, summary, description, NULL, \
48            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
49
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. */
53#define OWLVAR_ENUM(name,default,summary,description,validset) \
54        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
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),           \
58            }
59
60#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
61        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
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), \
65            }
66
67int owl_variable_add_defaults(owl_vardict *vd)
68{
69  owl_variable_init_params variables_to_init[] = {
70
71  OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off",
72                 "ring the terminal bell when personal messages are received",
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"),
75
76  OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1,
77               "enable / disable the terminal bell", "" ),
78
79  OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG,
80                    "whether debugging is enabled",
81                    "If set to 'on', debugging messages are logged to the\n"
82                    "file specified by the debugfile variable.\n",
83                    NULL, owl_variable_debug_set, NULL),
84
85  OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1,
86               "send a login message when BarnOwl starts", "" ),
87
88  OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1,
89               "send a logout message when BarnOwl exits", "" ),
90
91  OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0,
92               "display received pings", "" ),
93
94  OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1,
95               "send pings", "" ),
96
97  OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0,
98               "disable printing information in the separator bar", "" ),
99
100  OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1,
101               "strip kerberos instance for reply", ""),
102
103  OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1,
104               "strip leading and trailing newlines", ""),
105
106  OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1,
107               "display outgoing messages", "" ),
108
109  OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1,
110               "load logins from .anyone on startup", "" ),
111
112  OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0,
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" ),
118
119  OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0,
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" ),
126
127  OWLVAR_ENUM( "loggingdirection" /* %OwlVarStub */, OWL_LOGGING_DIRECTION_BOTH,
128               "specifies which kind of messages should be logged",
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
136  OWLVAR_BOOL_FULL( "colorztext" /* %OwlVarStub */, 1,
137                    "allow @color() in zephyrs to change color",
138                    NULL, NULL, owl_variable_colorztext_set, NULL),
139
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
146  OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1,
147               "Do automatic zcrypt processing",
148               "" ),
149
150  OWLVAR_BOOL_FULL( "pseudologins" /* %OwlVarStub */, 0,
151                    "Enable zephyr pseudo logins",
152                    "When this is enabled, BarnOwl will periodically check the zephyr\n"
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"
155                    "logout message, a pseudo login or logout message will be created\n",
156                    NULL, owl_variable_pseudologins_set, NULL),
157
158  OWLVAR_BOOL( "ignorelogins" /* %OwlVarStub */, 0,
159               "Enable printing of login notifications",
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"
162               "login or logout notifications.\n"),
163
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"
168                 "This is a completely separate mechanism from the other logging\n"
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
173  OWLVAR_BOOL( "loglogins" /* %OwlVarStub */, 0,
174               "Enable logging of login notifications",
175               "When this is enabled, BarnOwl will log login and logout notifications\n"
176               "for AIM, zephyr, or other protocols.  If disabled BarnOwl will not print\n"
177               "login or logout notifications.\n"),
178
179  OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1,
180                    "don't send zephyrs on C-d",
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"
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",
189                    NULL, owl_variable_disable_ctrl_d_set, NULL),
190
191  OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people",
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"),
195
196  OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class",
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"),
200
201  OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE,
202               "path for logging debug messages when debugging is enabled",
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."),
205 
206  OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL,
207               "name of a program to run that will generate zsigs",
208               "This program should produce a zsig on stdout when run.\n"
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               ),
213
214  OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL,
215               "name of a program to run when new messages are present",
216               "The named program will be run when BarnOwl receives new\n"
217               "messages.  It will not be run again until the first\n"
218               "instance exits"),
219
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
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"
231                 "described in the documentation for the 'zsig' variable.\n"
232                 "See also BarnOwl::random_zephyr_signature().\n"),
233
234  OWLVAR_STRING( "zsig" /* %OwlVarStub */, "",
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"),
240
241  OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "",
242                 "string to append to the end of the sepbar",
243                 "The sepbar is the bar separating the top and bottom\n"
244                 "of the BarnOwl screen.  Any string specified here will\n"
245                 "be displayed on the right of the sepbar\n"),
246
247  OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0,
248               "turn zaway on or off", "" ),
249
250  OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 
251                 OWL_DEFAULT_ZAWAYMSG,
252                 "zaway msg for responding to zephyrs when away", "" ),
253
254  OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 
255                 OWL_DEFAULT_ZAWAYMSG,
256                 "default zaway message", "" ),
257
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
271  OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all",
272                 "home view to switch to after 'X' and 'V'", 
273                 "SEE ALSO: view, filter\n" ),
274
275  OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none",
276                 "filter on which to trigger alert actions",
277                 "" ),
278
279  OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop",
280                 "BarnOwl command to execute for alert actions",
281                 "" ),
282
283  OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "<string>", "tty name for zephyr location", "",
284                      NULL, owl_variable_tty_set, NULL),
285
286  OWLVAR_STRING( "default_style" /* %OwlVarStub */, "default",
287                 "name of the default formatting style",
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"
291                 "   default  - the default BarnOwl formatting\n"
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
297
298  OWLVAR_INT(    "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70,
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"),
303
304  OWLVAR_INT(    "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70,
305                 "maximum number of columns for line-wrapping",
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"),
312
313  OWLVAR_INT( "aim_ignorelogin_timer" /* %OwlVarStub */, 15,
314              "number of seconds after AIM login to ignore login messages",
315              "This specifies the number of seconds to wait after an\n"
316              "AIM login before allowing the receipt of AIM login notifications.\n"
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."),
319
320             
321  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
322                   OWL_TYPWIN_SIZE,
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",
328                   owl_variable_int_validate_gt0,
329                   owl_variable_typewinsize_set,
330                   NULL /* use default for get */
331                   ),
332
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
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"
348               "   normal      - This is the BarnOwl default.  Scrolling happens\n"
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
372  OWLVAR_BOOL( "narrow-related" /* %OwlVarStub:narrow_related */, 1,
373               "Make smartnarrow use broader filters",
374               "Causes smartfilter to narrow to messages \"related\" to \n"
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" ),
379
380  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
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" ),
386
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
452  /* This MUST be last... */
453  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
454    NULL, NULL, NULL, NULL, NULL }
455
456  };
457
458  return owl_variable_dict_add_from_list(vd, variables_to_init);
459}
460
461/**************************************************************************/
462/*********************** SPECIFIC TO VARIABLES ****************************/
463/**************************************************************************/
464
465
466/* commonly useful */
467
468int owl_variable_int_validate_gt0(const owl_variable *v, int newval)
469{
470  return !(newval < 1);
471}
472
473int owl_variable_int_validate_positive(const owl_variable *v, int newval)
474{
475  return !(newval < 0);
476}
477
478/* typewinsize */
479int owl_variable_typewinsize_set(owl_variable *v, int newval)
480{
481  int rv;
482  rv = owl_variable_int_set_default(v, newval);
483  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
484  return(rv);
485}
486
487/* debug (cache value in g->debug) */
488int owl_variable_debug_set(owl_variable *v, int newval)
489{
490  if (newval == 1 || newval == 0) {
491    g.debug = newval;
492  }
493  return owl_variable_bool_set_default(v, newval);
494}
495
496/* When 'aaway' is changed, need to notify the AIM server */
497int owl_variable_aaway_set(owl_variable *v, gboolean newval)
498{
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("");
503  }
504  return owl_variable_bool_set_default(v, newval);
505}
506
507int owl_variable_colorztext_set(owl_variable *v, const void *newval)
508{
509  int ret = owl_variable_bool_set_default(v, newval);
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
520int owl_variable_pseudologins_set(owl_variable *v, int newval)
521{
522  static guint timer = 0;
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;
532    }
533  }
534  return owl_variable_bool_set_default(v, newval);
535}
536
537/* note that changing the value of this will clobber
538 * any user setting of this */
539int owl_variable_disable_ctrl_d_set(owl_variable *v, int newval)
540{
541  if (!owl_context_is_startup(owl_global_get_context(&g))) {
542    if (newval == 2) {
543      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
544    } else if (newval == 1) {
545      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
546    } else {
547      owl_function_command_norv("bindkey editmulti C-d command edit:done");
548    }
549  } 
550  return owl_variable_int_set_default(v, newval);
551}
552
553int owl_variable_tty_set(owl_variable *v, const char *newval)
554{
555  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
556  return owl_variable_string_set_default(v, newval);
557}
558
559int owl_variable_default_exposure_set(owl_variable *v, const char *newval)
560{
561  return owl_zephyr_set_default_exposure(newval);
562}
563
564const char *owl_variable_default_exposure_get(const owl_variable *v)
565{
566  return owl_zephyr_get_default_exposure();
567}
568
569int owl_variable_exposure_set(owl_variable *v, const char *newval)
570{
571  int ret = owl_zephyr_set_exposure(newval);
572  if (ret != 0)
573    return ret;
574  return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval));
575}
576
577/**************************************************************************/
578/****************************** GENERAL ***********************************/
579/**************************************************************************/
580
581int owl_variable_dict_setup(owl_vardict *vd) {
582  owl_dict_create(vd);
583  return owl_variable_add_defaults(vd);
584}
585
586CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v,
587                                               GCallback fn,
588                                               GClosureMarshal marshal) {
589  GClosure *closure = g_cclosure_new_swap(fn, v, NULL);
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)
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);
613
614int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable_init_params *variables_to_init)
615{
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;
621    /* strdup all the strings so we can delete them consistently. */
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 = g_strdup(init_params->validsettings);
626    GCallback fn = NULL;
627    switch (init_params->type) {
628    case OWL_VARIABLE_STRING:
629      SET_CBS(string);
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
637      g_value_init(&newvar->val, G_TYPE_STRING);
638      owl_variable_set_string(newvar, init_params->pval_default);
639      break;
640    case OWL_VARIABLE_BOOL:
641      SET_CBS(bool);
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),
647                              g_cclosure_user_marshal_STRING__VOID, fn);
648
649      g_value_init(&newvar->val, G_TYPE_BOOLEAN);
650      owl_variable_set_bool(newvar, !!(init_params->ival_default));
651      break;
652    case OWL_VARIABLE_INT:
653      SET_CBS(int);
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),
659                              g_cclosure_user_marshal_STRING__VOID, fn);
660
661      g_value_init(&newvar->val, G_TYPE_INT);
662      owl_variable_set_int(newvar, init_params->ival_default);
663      break;
664    default:
665      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
666      return(-2);
667    }
668
669    /* record the initial value as a string */
670    newvar->default_str = owl_variable_get_tostring(newvar);
671
672    owl_dict_insert_element(vd, newvar->name, newvar, NULL);
673  }
674  return 0;
675}
676
677void owl_variable_dict_add_variable(owl_vardict * vardict,
678                                    owl_variable * var) {
679  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
680}
681
682void owl_variable_dict_newvar_other(owl_vardict *vd, const char *name, const char *summary, const char *description, const char *validsettings, GClosure *get_tostring_fn, GClosure *set_fromstring_fn)
683{
684  owl_variable *var = g_new0(owl_variable, 1);
685  var->name = g_strdup(name);
686  var->summary = g_strdup(summary);
687  var->description = g_strdup(description);
688  var->validsettings = g_strdup(validsettings);
689
690  var->get_tostring_fn = g_closure_ref(get_tostring_fn);
691  g_closure_sink(get_tostring_fn);
692
693  var->set_fromstring_fn = g_closure_ref(set_fromstring_fn);
694  g_closure_sink(set_fromstring_fn);
695
696  var->default_str = owl_variable_get_tostring(var);
697
698  /* Note: this'll overwrite any existing variable of that name, even a C one,
699     but it's consistent with previous behavior and commands. */
700  owl_variable_dict_add_variable(vd, var);
701}
702
703void owl_variable_dict_cleanup(owl_vardict *d)
704{
705  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
706}
707
708CALLER_OWN GPtrArray *owl_variable_dict_get_names(const owl_vardict *d) {
709  return owl_dict_get_keys(d);
710}
711
712void owl_variable_cleanup(owl_variable *v)
713{
714  g_free(v->name);
715  g_free(v->summary);
716  g_free(v->description);
717  g_free(v->default_str);
718  g_free(v->validsettings);
719  g_value_unset(&(v->val));
720  g_closure_unref(v->get_tostring_fn);
721  g_closure_unref(v->set_fromstring_fn);
722}
723
724void owl_variable_delete(owl_variable *v)
725{
726  owl_variable_cleanup(v);
727  g_free(v);
728}
729
730
731const char *owl_variable_get_name(const owl_variable *v)
732{
733  return v->name;
734}
735
736const char *owl_variable_get_description(const owl_variable *v) {
737  return v->description;
738}
739
740const char *owl_variable_get_summary(const owl_variable *v) {
741  return v->summary;
742}
743
744const char *owl_variable_get_validsettings(const owl_variable *v) {
745  return v->validsettings;
746}
747
748int owl_variable_get_type(const owl_variable *v)
749{
750  return v->type;
751}
752
753/* returns 0 on success, prints a status msg if msg is true */
754int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
755  char *tostring;
756  GValue values[] = {G_VALUE_INIT, G_VALUE_INIT};
757  GValue *value_box = &values[1];
758  GValue return_box = G_VALUE_INIT;
759  int set_successfully = -1;
760  if (!v->set_fromstring_fn) {
761    if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v));
762    return -1;   
763  }
764  g_value_init(&values[0], G_TYPE_POINTER);
765  g_value_set_pointer(&values[0], NULL);
766  g_value_init(value_box, G_TYPE_STRING);
767  g_value_set_static_string(value_box, value);
768  g_value_init(&return_box, G_TYPE_INT);
769  g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL);
770  set_successfully = g_value_get_int(&return_box);
771  if (0 != set_successfully) {
772    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
773                                owl_variable_get_validsettings(v));
774  } else if (msg) {
775    tostring = owl_variable_get_tostring(v);
776    if (tostring) {
777      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
778    } else {
779      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
780    }
781    g_free(tostring);
782  }
783  g_value_unset(value_box);
784  return set_successfully;
785}
786 
787int owl_variable_set_string(owl_variable *v, const char *newval)
788{
789  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1);
790
791  set_string_t cb = (set_string_t) v->set_fn;
792  return cb(v, newval);
793}
794
795int owl_variable_set_int(owl_variable *v, int newval)
796{
797  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1);
798
799  set_int_t cb = (set_int_t) v->set_fn;
800  return cb(v, newval);
801}
802
803int owl_variable_set_bool(owl_variable *v, gboolean newval) {
804  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1);
805
806  set_bool_t cb = (set_bool_t) v->set_fn;
807  return cb(v, newval);
808}
809
810int owl_variable_set_bool_on(owl_variable *v)
811{
812  if (v->type != OWL_VARIABLE_BOOL) return -1;
813  return owl_variable_set_bool(v, true);
814}
815
816int owl_variable_set_bool_off(owl_variable *v)
817{
818  if (v->type != OWL_VARIABLE_BOOL) return -1;
819  return owl_variable_set_bool(v, false);
820}
821
822CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
823{
824  GValue instance = G_VALUE_INIT;
825  GValue tostring_box = G_VALUE_INIT;
826  char *ret = NULL;
827
828  g_value_init(&instance, G_TYPE_POINTER);
829  g_value_set_pointer(&instance, NULL);
830  g_value_init(&tostring_box, G_TYPE_STRING);
831  g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL);
832
833  ret = g_value_dup_string(&tostring_box);
834  g_value_unset(&tostring_box);
835
836  return ret;
837}
838
839const char *owl_variable_get_default_tostring(const owl_variable *v)
840{
841  return v->default_str;
842}
843
844owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
845{
846  return owl_dict_find_element(d, name);
847}
848
849const char *owl_variable_get_string(const owl_variable *v)
850{
851  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL);
852
853  get_string_t cb = (get_string_t) v->get_fn;
854  return cb(v);
855}
856
857int owl_variable_get_int(const owl_variable *v)
858{
859  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0);
860
861  get_int_t cb = (get_int_t) v->get_fn;
862  return cb(v);
863}
864
865gboolean owl_variable_get_bool(const owl_variable *v)
866{
867  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE);
868
869  get_bool_t cb = (get_bool_t) v->get_fn;
870  return cb(v);
871}
872
873void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
874{
875  const char *default_str = owl_variable_get_default_tostring(v);
876  char *default_buf;
877
878  if (default_str)
879    default_buf = g_strdup_printf("'%s'", default_str);
880  else
881    default_buf = g_strdup("<null>");
882  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
883                            owl_variable_get_name(v),
884                            owl_variable_get_summary(v), default_buf);
885  g_free(default_buf);
886}
887
888void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
889  char *tostring;
890  const char *default_str;
891
892  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
893  owl_fmtext_append_normal(fm, OWL_TABSTR);
894  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
895  owl_fmtext_append_normal(fm, " - ");
896  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
897  owl_fmtext_append_normal(fm, "\n\n");
898
899  owl_fmtext_append_normal(fm, "Current:        ");
900  tostring = owl_variable_get_tostring(v);
901  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
902  g_free(tostring);
903  owl_fmtext_append_normal(fm, "\n\n");
904
905  default_str = owl_variable_get_default_tostring(v);
906  owl_fmtext_append_normal(fm, "Default:        ");
907  owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>"));
908  owl_fmtext_append_normal(fm, "\n\n");
909
910  owl_fmtext_append_normal(fm, "Valid Settings: ");
911  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
912  owl_fmtext_append_normal(fm, "\n\n");
913
914  if (v->description && *v->description) {
915    owl_fmtext_append_normal(fm, "Description:\n");
916    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
917    owl_fmtext_append_normal(fm, "\n\n");
918  }
919}
920
921
922
923
924/**************************************************************************/
925/*********************** GENERAL TYPE-SPECIFIC ****************************/
926/**************************************************************************/
927
928/* default common functions */
929
930const char *owl_variable_string_get_default(const owl_variable *v) {
931  return g_value_get_string(&(v->val));
932}
933
934int owl_variable_int_get_default(const owl_variable *v) {
935  return g_value_get_int(&(v->val));
936}
937
938gboolean owl_variable_bool_get_default(const owl_variable *v) {
939  return g_value_get_boolean(&(v->val));
940}
941
942/* default functions for booleans */
943
944int owl_variable_bool_validate_default(const owl_variable *v, gboolean newval) {
945  return (newval == 1) || (newval == 0);
946}
947
948int owl_variable_bool_set_default(owl_variable *v, bool newval) {
949  if (!((validate_bool_t)v->validate_fn)(v, newval))
950    return -1;
951
952  g_value_set_boolean(&(v->val), newval);
953  return(0);
954}
955
956int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
957  gboolean i;
958  if (!strcmp(newval, "on")) {
959    i = TRUE;
960  } else if (!strcmp(newval, "off")) {
961    i = FALSE;
962  } else {
963    return(-1);
964  }
965
966  return owl_variable_set_bool(v, i);
967}
968
969CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy)
970{
971  bool val = owl_variable_get_bool(v);
972  if (val == 0) {
973    return g_strdup("off");
974  } else if (val == 1) {
975    return g_strdup("on");
976  } else {
977    return g_strdup("<invalid>");
978  }
979}
980
981/* default functions for integers */
982
983int owl_variable_int_validate_default(const owl_variable *v, int newval)
984{
985  return (1);
986}
987
988int owl_variable_int_set_default(owl_variable *v, int newval) {
989  if (!((validate_int_t)v->validate_fn)(v, newval))
990    return -1;
991
992  g_value_set_int(&(v->val), newval);
993  return(0);
994}
995
996int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
997  int i;
998  char *ep;
999  i = strtol(newval, &ep, 10);
1000  if (*ep || ep==newval) return(-1);
1001  return owl_variable_set_int(v, i);
1002}
1003
1004CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy)
1005{
1006  return g_strdup_printf("%d", owl_variable_get_int(v));
1007}
1008
1009/* default functions for enums (a variant of integers) */
1010
1011int owl_variable_enum_validate(const owl_variable *v, int newval) {
1012  char **enums;
1013  int nenums, val;
1014  enums = g_strsplit_set(v->validsettings, ",", 0);
1015  nenums = g_strv_length(enums);
1016  g_strfreev(enums);
1017  val = newval;
1018  if (val < 0 || val >= nenums) {
1019    return(0);
1020  }
1021  return(1);
1022}
1023
1024int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval, void *dummy) {
1025  char **enums;
1026  int i, val=-1;
1027  if (newval == NULL) return(-1);
1028  enums = g_strsplit_set(v->validsettings, ",", 0);
1029  for (i = 0; enums[i] != NULL; i++) {
1030    if (0==strcmp(newval, enums[i])) {
1031      val = i;
1032    }
1033  }
1034  g_strfreev(enums);
1035  if (val == -1) return(-1);
1036  return owl_variable_set_int(v, val);
1037}
1038
1039CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy)
1040{
1041  char **enums;
1042  int nenums, i;
1043  char *tostring;
1044
1045  enums = g_strsplit_set(v->validsettings, ",", 0);
1046  nenums = g_strv_length(enums);
1047  i = owl_variable_get_int(v);
1048  if (i<0 || i>=nenums) {
1049    g_strfreev(enums);
1050    return g_strdup_printf("<invalid:%d>", i);
1051  }
1052  tostring = g_strdup(enums[i]);
1053  g_strfreev(enums);
1054  return tostring;
1055}
1056
1057/* default functions for stringeans */
1058
1059int owl_variable_string_validate_default(const owl_variable *v, const char *newval) {
1060  if (newval == NULL) return(0);
1061  else return (1);
1062}
1063
1064int owl_variable_string_set_default(owl_variable *v, const char *newval) {
1065  if (!((validate_string_t)v->validate_fn)(v, newval))
1066    return -1;
1067
1068  g_value_set_string(&(v->val), newval);
1069  return(0);
1070}
1071
1072int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 
1073{
1074  return owl_variable_set_string(v, newval);
1075}
1076
1077CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy)
1078{
1079  return g_strdup(owl_variable_get_string(v));
1080}
1081
Note: See TracBrowser for help on using the repository browser.