source: variable.c @ bd482c3

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