"Fossies" - the Fresh Open Source Software Archive

Member "jpilot-2_0_1/prefs.c" (3 Apr 2021, 26549 Bytes) of package /linux/privat/jpilot-2_0_1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "prefs.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.8.2_vs_2_0_1.

    1 /*******************************************************************************
    2  * prefs.c
    3  * A module of J-Pilot http://jpilot.org
    4  *
    5  * Copyright (C) 1999-2014 by Judd Montgomery
    6  *
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation; version 2 of the License.
   10  *
   11  * This program is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19  ******************************************************************************/
   20 
   21 /********************************* Includes ***********************************/
   22 #include "config.h"
   23 #include "utils.h"
   24 #include <sys/types.h>
   25 #include <dirent.h>
   26 #include <stdio.h>
   27 #include <stdlib.h>
   28 #include <string.h>
   29 #ifdef HAVE_LANGINFO_H
   30 #  include <langinfo.h>
   31 #endif
   32 
   33 #include "i18n.h"
   34 #include "utils.h"
   35 #include "prefs.h"
   36 #include "log.h"
   37 #include "otherconv.h"
   38 
   39 /********************************* Constants **********************************/
   40 #define NUM_SHORTDATES 7
   41 #define NUM_LONGDATES 6
   42 #define NUM_TIMES 10
   43 #define NUM_TIMES_NO_AMPM 6
   44 #define NUM_RATES 11
   45 #define NUM_PAPER_SIZES 2
   46 
   47 /******************************* Global vars **********************************/
   48 static int t_fmt_ampm = TRUE;
   49 
   50 /* These are the default settings */
   51 /* name, usertype, filetype, ivalue, char *svalue, svalue_size; */
   52 static prefType glob_prefs[NUM_PREFS] = {
   53    {"jpilotcss", CHARTYPE, CHARTYPE, 0, "jpilotcss.default", 16},
   54    {"time", CHARTYPE, INTTYPE, 0, NULL, 0},
   55    {"sdate", CHARTYPE, INTTYPE, 0, NULL, 0},
   56    {"ldate", CHARTYPE, INTTYPE, 0, NULL, 0},
   57    {"xxx_internal1_xxx", CHARTYPE, INTTYPE, 0, NULL, 0},
   58    {"show_deleted", INTTYPE, INTTYPE, 0, NULL, 0},
   59    {"show_modified", INTTYPE, INTTYPE, 0, NULL, 0},
   60    {"todo_hide_completed", INTTYPE, INTTYPE, 0, NULL, 0},
   61    {"datebook_highlight_days", INTTYPE, INTTYPE, 1, NULL, 0},
   62    {"port", CHARTYPE, CHARTYPE, 0, NULL, 0},
   63    {"rate", CHARTYPE, INTTYPE, 8, NULL, 0},
   64    {"user", CHARTYPE, CHARTYPE, 0, NULL, 0},
   65    {"user_id", INTTYPE, INTTYPE, 0, NULL, 0},
   66    {"pc_id", INTTYPE, INTTYPE, 0, NULL, 0},
   67    {"num_backups", INTTYPE, INTTYPE, 2, NULL, 0},
   68    {"window_width", INTTYPE, INTTYPE, 640, NULL, 0},
   69    {"window_height", INTTYPE, INTTYPE, 480, NULL, 0},
   70    {"datebook_pane", INTTYPE, INTTYPE, 370, NULL, 0},
   71    {"address_pane", INTTYPE, INTTYPE, 340, NULL, 0},
   72    {"todo_pane", INTTYPE, INTTYPE, 370, NULL, 0},
   73    {"memo_pane", INTTYPE, INTTYPE, 370, NULL, 0},
   74    {"use_db3", INTTYPE, INTTYPE, 0, NULL, 0},
   75    {"last_app", INTTYPE, INTTYPE, DATEBOOK, NULL, 0},
   76    {"print_this_many", INTTYPE, INTTYPE, 3, NULL, 0},
   77    {"print_one_per_page", INTTYPE, INTTYPE, 0, NULL ,0},
   78    {"print_blank_lines", INTTYPE, INTTYPE, 1, NULL, 0},
   79    {"print_command", CHARTYPE, CHARTYPE, 0, NULL, 0},
   80    {"char_set", INTTYPE, INTTYPE, CHAR_SET_1252_UTF , NULL, 0},
   81    {"sync_datebook", INTTYPE, INTTYPE, 1, NULL, 0},
   82    {"sync_address", INTTYPE, INTTYPE, 1, NULL, 0},
   83    {"sync_todo", INTTYPE, INTTYPE, 1, NULL, 0},
   84    {"sync_memo", INTTYPE, INTTYPE, 1, NULL, 0},
   85    {"sync_memo32", INTTYPE, INTTYPE, 0, NULL, 0},
   86    {"address_notebook_page", INTTYPE, INTTYPE, 0, NULL, 0},
   87    {"output_height", INTTYPE, INTTYPE, 60, NULL, 0},
   88    {"open_alarm_windows", INTTYPE, INTTYPE, 1, NULL, 0},
   89    {"do_alarm_command", INTTYPE, INTTYPE, 0, NULL, 0},
   90    {"alarm_command", CHARTYPE, CHARTYPE, 0, NULL, 0},
   91    {"remind_in", INTTYPE, INTTYPE, 0, NULL, 0},
   92    {"remind_units", INTTYPE, INTTYPE, 0, NULL, 0},
   93    /* This is actually the password, but I wanted to name it something more discreet */
   94    {"session_id", CHARTYPE, CHARTYPE, 0, NULL, 0},
   95    {"memo32_mode", INTTYPE, INTTYPE, 0, NULL, 0},
   96    {"paper_size", INTTYPE, INTTYPE, 0, NULL, 0},
   97    {"datebook_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
   98    {"datebook_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
   99    {"address_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
  100    {"address_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
  101    {"todo_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
  102    {"todo_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
  103    {"memo_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
  104    {"memo_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
  105    {"manana_mode", INTTYPE, INTTYPE, 0, NULL, 0},
  106    {"sync_manana", INTTYPE, INTTYPE, 0, NULL, 0},
  107    {"use_jos", INTTYPE, INTTYPE, 0, NULL, 0},
  108    {"phone_prefix1", CHARTYPE, CHARTYPE, 0, NULL, 0},
  109    {"phone_prefix1_on", INTTYPE, INTTYPE, 0, NULL, 0},
  110    {"phone_prefix2", CHARTYPE, CHARTYPE, 0, NULL, 0},
  111    {"phone_prefix2_on", INTTYPE, INTTYPE, 0, NULL, 0},
  112    {"phone_prefix3", CHARTYPE, CHARTYPE, 0, NULL, 0},
  113    {"phone_prefix3_on", INTTYPE, INTTYPE, 0, NULL, 0},
  114    {"dial_command", CHARTYPE, CHARTYPE, 0, NULL, 0},
  115    {"datebook_todo_pane", INTTYPE, INTTYPE, 350, NULL, 0},
  116    {"datebook_todo_show", INTTYPE, INTTYPE, 0, NULL, 0},
  117    {"todo_hide_not_due", INTTYPE, INTTYPE, 0, NULL, 0},
  118    {"todo_completion_date", INTTYPE, INTTYPE, 0, NULL, 0},
  119    {"install_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
  120    {"monthview_width", INTTYPE, INTTYPE, 640, NULL, 0},
  121    {"monthview_height", INTTYPE, INTTYPE, 480, NULL, 0},
  122    {"weekview_width", INTTYPE, INTTYPE, 640, NULL, 0},
  123    {"weekview_height", INTTYPE, INTTYPE, 480, NULL, 0},
  124    {"last_date_category", INTTYPE, INTTYPE, CATEGORY_ALL, NULL, 0},
  125    {"last_addr_category", INTTYPE, INTTYPE, CATEGORY_ALL, NULL, 0},
  126    {"last_todo_category", INTTYPE, INTTYPE, CATEGORY_ALL, NULL, 0},
  127    {"last_memo_category", INTTYPE, INTTYPE, CATEGORY_ALL, NULL, 0},
  128    {"mail_command", CHARTYPE, CHARTYPE, 0, NULL, 0},
  129    {"version", CHARTYPE, CHARTYPE, 0, NULL, 0},
  130    {"utf_encoding", INTTYPE, INTTYPE, 0, NULL, 0},
  131    {"confirm_file_install", INTTYPE, INTTYPE, 1, NULL, 0},
  132    {"todo_days_due", INTTYPE, INTTYPE, 0, NULL, 0},
  133    {"todo_days_till_due", INTTYPE, INTTYPE, 7, NULL, 0},
  134    {"show_tooltips", INTTYPE, INTTYPE, 1, NULL, 0},
  135    {"datebook_note_pane", INTTYPE, INTTYPE, 75, NULL, 0},
  136    {"datebook_hi_today", INTTYPE, INTTYPE, 1, NULL, 0},
  137    {"datebook_anni_years", INTTYPE, INTTYPE, 0, NULL, 0},
  138    {"keyring_pane", INTTYPE, INTTYPE, 0, NULL, 0},
  139    {"expense_pane", INTTYPE, INTTYPE, 0, NULL, 0},
  140    {"datebook_version", INTTYPE, INTTYPE, 0, NULL, 0},
  141    {"address_version", INTTYPE, INTTYPE, 0, NULL, 0},
  142    {"todo_version", INTTYPE, INTTYPE, 0, NULL, 0},
  143    {"memo_version", INTTYPE, INTTYPE, 0, NULL, 0},
  144    {"contacts_photo_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
  145    {"todo_sort_column", INTTYPE, INTTYPE, 1, NULL, 0},
  146    {"todo_sort_order", INTTYPE, INTTYPE, 0, NULL, 0},
  147    {"addr_sort_order", INTTYPE, INTTYPE, 0, NULL, 0},
  148    {"addr_name_col_sz", INTTYPE, INTTYPE, 140, NULL, 0},
  149    {"todo_note_pane", INTTYPE, INTTYPE, 200, NULL, 0},
  150    {"expense_sort_column", INTTYPE, INTTYPE, 0, NULL, 0},
  151    {"expense_sort_order", INTTYPE, INTTYPE, 0, NULL, 0},
  152    {"keyr_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
  153    {"external_editor", CHARTYPE, CHARTYPE, 0, NULL, 0},
  154 };
  155 
  156 
  157 char *get_new_css_name(char *field2);
  158 
  159 gboolean using_old_rc_name_in_pref_file(const char *field1);
  160 
  161 /****************************** Main Code *************************************/
  162 void pref_init(void)
  163 {
  164    int i;
  165 
  166    /* Determine whether locale supports am/pm time formats */
  167 #  ifdef HAVE_LANGINFO_H
  168       t_fmt_ampm = strcmp(nl_langinfo(T_FMT_AMPM), "");
  169 #  endif
  170 
  171    for (i=0; i<NUM_PREFS; i++) {
  172       switch (i) {
  173        case PREF_PORT:
  174          glob_prefs[i].svalue=strdup("usb:");
  175          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  176          break;
  177        case PREF_RCFILE:
  178          glob_prefs[i].svalue=strdup(EPN"css.default");
  179          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  180          break;
  181        case PREF_PRINT_COMMAND:
  182          glob_prefs[i].svalue=strdup("lpr -h");
  183          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  184          break;
  185        case PREF_ALARM_COMMAND:
  186          glob_prefs[i].svalue=strdup("echo %t %d");
  187          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  188          break;
  189        case PREF_REMIND_IN:
  190          glob_prefs[i].svalue=strdup("5");
  191          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  192          break;
  193        case PREF_PASSWORD:
  194          glob_prefs[i].svalue=strdup("09021345070413440c08135a3215135dd217ead3b5df556322e9a14a994b0f88");
  195          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  196          break;
  197        case PREF_DIAL_COMMAND:
  198          glob_prefs[i].svalue=strdup("jpilot-dial --lv 0 --rv 50 %n");
  199          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  200          break;
  201        case PREF_MAIL_COMMAND:
  202          glob_prefs[i].svalue=strdup("mozilla-thunderbird -remote \"mailto(%s)\"");
  203          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  204          break;
  205        case PREF_EXTERNAL_EDITOR:
  206          glob_prefs[i].svalue=strdup("gvim -f");
  207          glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
  208          break;
  209        default:
  210          glob_prefs[i].svalue=strdup("");
  211          glob_prefs[i].svalue_size=1;
  212       }
  213    }
  214 }
  215 
  216 void jp_pref_init(prefType prefs[], int count)
  217 {
  218    int i;
  219 
  220    for (i=0; i<count; i++) {
  221       if (prefs[i].svalue) {
  222          prefs[i].svalue=strdup(prefs[i].svalue);
  223       } else {
  224          prefs[i].svalue=strdup("");
  225       }
  226       prefs[i].svalue_size=strlen(prefs[i].svalue)+1;
  227    }
  228 }
  229 
  230 void jp_free_prefs(prefType prefs[], int count)
  231 {
  232    int i;
  233 
  234    for (i=0; i<count; i++) {
  235       if (prefs[i].svalue) {
  236          free(prefs[i].svalue);
  237          prefs[i].svalue=NULL;
  238       }
  239    }
  240 }
  241 
  242 /* Get just the formatting for just the hour and am/pm if its set */
  243 /* datef needs to be preallocated */
  244 void get_pref_hour_ampm(char *datef)
  245 {
  246    const char *svalue;
  247 
  248    get_pref(PREF_TIME, NULL, &svalue);
  249    strncpy(datef, svalue, 2);
  250    datef[2]='\0';
  251    if (!strncasecmp(&(svalue[strlen(svalue)-2]), "%p", 2)) {
  252       strncpy(&(datef[2]), &(svalue[strlen(svalue)-2]), 2);
  253       datef[4]='\0';
  254    }
  255 }
  256 
  257 int get_pref_time_no_secs(char *datef)
  258 {
  259    /* "%I:%M:%S %p" */
  260    /* "%I:%M:%S" */
  261    /* "%I:%M" */
  262    /* "%I:%M%p" */
  263    const char *svalue;
  264    int i1, i2;
  265 
  266    get_pref(PREF_TIME, NULL, &svalue);
  267    if (!svalue) {
  268       return EXIT_FAILURE;
  269    }
  270    for (i1=0, i2=0; ; i1++, i2++) {
  271       if (svalue[i2]=='S') {
  272          i1-=2;
  273          i2++;
  274       }
  275       if (svalue[i2]==' ') {
  276          i1--;
  277          continue;
  278       }
  279       datef[i1]=svalue[i2];
  280       if (svalue[i2]=='\0') {
  281          break;
  282       }
  283    }
  284    return EXIT_SUCCESS;
  285 }
  286 
  287 int get_pref_time_no_secs_no_ampm(char *datef)
  288 {
  289    const char *svalue;
  290 
  291    get_pref(PREF_TIME, NULL, &svalue);
  292    if (!svalue) {
  293       return EXIT_FAILURE;
  294    }
  295    if (svalue) {
  296       strncpy(datef, svalue, 5);
  297       datef[5]='\0';
  298    } else {
  299       datef[0]='\0';
  300    }
  301 
  302    return EXIT_SUCCESS;
  303 }
  304 
  305 /* This function is used internally to free up any memory that prefs is using */
  306 /* I'm not using this function right now.
  307 void free_name_list(struct name_list **Plist)
  308 {
  309    struct name_list *temp_list, *next_list;
  310 
  311    for (temp_list=*Plist; temp_list; temp_list=next_list) {
  312       next_list=temp_list->next;
  313       if (temp_list->name) {
  314          free(temp_list->name);
  315       }
  316       free(temp_list);
  317    }
  318    *Plist=NULL;
  319 }
  320 */
  321 
  322 
  323 
  324 /* if n is out of range then this function will fail */
  325 int get_pref_possibility(int which, int n, char *pref_str)
  326 {
  327    const char *short_date_formats[] = {
  328       "%m/%d/%y",
  329       "%d/%m/%y",
  330       "%d.%m.%y",
  331       "%d-%m-%y",
  332       "%y/%m/%d",
  333       "%y.%m.%d",
  334       "%y-%m-%d"
  335    };
  336 
  337    const char *long_date_formats[] = {
  338       N_("%B %d, %Y"),
  339       N_("%d %B %Y"),
  340       N_("%d. %B %Y"),
  341       N_("%d %B, %Y"),
  342       N_("%Y. %B. %d"),
  343       N_("%Y %B %d")
  344    };
  345 
  346    const char *time_formats[] = {
  347       "%I:%M:%S %p",
  348       "%H:%M:%S",
  349       "%I.%M.%S %p",
  350       "%H.%M.%S",
  351       "%H,%M,%S",
  352       "%I:%M %p",
  353       "%H:%M",
  354       "%I.%M %p",
  355       "%H.%M",
  356       "%H,%M"
  357    };
  358 
  359    const char *time_formats_no_ampm[] = {
  360       "%H:%M:%S",
  361       "%H.%M.%S",
  362       "%H,%M,%S",
  363       "%H:%M",
  364       "%H.%M",
  365       "%H,%M"
  366    };
  367 
  368    static const char *days[] = {
  369       N_("Sunday"),
  370       N_("Monday")
  371    };
  372 
  373    static const char *rates[] = {
  374       "300",
  375       "1200",
  376       "2400",
  377       "4800",
  378       "9600",
  379       "19200",
  380       "38400",
  381       "57600",
  382       "115200",
  383       "H230400",
  384       "H460800"
  385    };
  386 
  387    static const char *char_sets[] = {
  388       "Latin1 / No conversion",
  389       "Japanese",
  390       "Host ISO-8859-2 <-> Palm Windows1250 (EE)",
  391       "Host Windows1251 <-> Palm KOI8-R",
  392       "Host KOI8-R <-> Palm Windows-1251",
  393       "Chinese(Big5)",
  394       "Korean",
  395       "UTF: Latin 2, Eastern Europe (CP1250)",
  396       "UTF: Latin 1, Western Europe (CP1252)",
  397       "UTF: Greek (CP1253)",
  398       "UTF: Latin 2, Eastern Europe (ISO8859-2)",
  399       "UTF: Cyrillic (KOI8-R)",
  400       "UTF: Cyrillic (CP1251)",
  401       "UTF: Simplified Chinese (GBK)",
  402       "UTF: Japanese (SJIS)",
  403       "UTF: Hebrew (CP1255)",
  404       "UTF: Traditional Chinese (BIG-5)",
  405       "UTF: Korean (CP949)",
  406    };
  407 
  408    static const char *paper_sizes[] = {
  409       "US Letter",
  410       "A4"
  411    };
  412 
  413    switch (which) {
  414 
  415     case PREF_RCFILE:
  416          return get_rcfile_name(n, pref_str);
  417       break;
  418 
  419     case PREF_TIME:
  420       if (t_fmt_ampm) {
  421          if ((n >= NUM_TIMES) || (n<0)) {
  422             pref_str[0]='\0';
  423             return EXIT_FAILURE;
  424          }
  425          strcpy(pref_str, time_formats[n]);
  426       } else {
  427          if ((n >= NUM_TIMES_NO_AMPM) || (n<0)) {
  428             pref_str[0]='\0';
  429             return EXIT_FAILURE;
  430          }
  431          strcpy(pref_str, time_formats_no_ampm[n]);
  432       }
  433          break;
  434 
  435     case PREF_SHORTDATE:
  436       if ((n >= NUM_SHORTDATES) || (n<0)) {
  437          pref_str[0]='\0';
  438          return EXIT_FAILURE;
  439       }
  440       strcpy(pref_str, short_date_formats[n]);
  441       break;
  442 
  443     case PREF_LONGDATE:
  444       if ((n >= NUM_LONGDATES) || (n<0)) {
  445          pref_str[0]='\0';
  446          return EXIT_FAILURE;
  447       }
  448       strcpy(pref_str, long_date_formats[n]);
  449       break;
  450 
  451     case PREF_FDOW:
  452       if ((n > 1) || (n<0)) {
  453          pref_str[0]='\0';
  454          return EXIT_FAILURE;
  455       }
  456       strcpy(pref_str, _(days[n]));
  457       break;
  458 
  459     case PREF_RATE:
  460       if ((n >= NUM_RATES) || (n<0)) {
  461          pref_str[0]='\0';
  462          return EXIT_FAILURE;
  463       }
  464       strcpy(pref_str, rates[n]);
  465       break;
  466 
  467     case PREF_CHAR_SET:
  468       if ((n >= NUM_CHAR_SETS) || (n<0)) {
  469          pref_str[0]='\0';
  470          return EXIT_FAILURE;
  471       }
  472       strcpy(pref_str, char_sets[n]);
  473       break;
  474 
  475     case PREF_PAPER_SIZE:
  476       if ((n >= NUM_PAPER_SIZES) || (n<0)) {
  477          pref_str[0]='\0';
  478          return EXIT_FAILURE;
  479       }
  480       strcpy(pref_str, paper_sizes[n]);
  481       break;
  482 
  483     default:
  484       pref_str[0]='\0';
  485       jp_logf(JP_LOG_DEBUG, "Unknown preference type\n");
  486       return EXIT_FAILURE;
  487    }
  488 
  489    return EXIT_SUCCESS;
  490 }
  491 
  492 /* Warning: which must be in the range of choices or this can seg fault */
  493 int jp_get_pref(prefType prefs[], int which, long *n, const char **string)
  494 {
  495    if (which < 0) {
  496       return EXIT_FAILURE;
  497    }
  498    if (n) {
  499       *n = prefs[which].ivalue;
  500    }
  501    if (string!=NULL) {
  502       if (prefs[which].usertype == CHARTYPE) {
  503          *string = prefs[which].svalue;
  504       } else {
  505          *string = NULL;
  506       }
  507    }
  508 
  509    return EXIT_SUCCESS;
  510 }
  511 
  512 int get_pref(int which, long *n, const char **string)
  513 {
  514    if (which >= NUM_PREFS) {
  515       return EXIT_FAILURE;
  516    }
  517    return jp_get_pref(glob_prefs, which, n, string);
  518 }
  519 
  520 /*
  521  * Get the preference value as integer. If failed to do so, return the
  522  * specified default (defval).
  523  */
  524 long get_pref_int_default(int which, long defval)
  525 {
  526    long val;
  527 
  528    if (get_pref(which, &val, NULL) == 0) {
  529       return val;
  530    }
  531    else {
  532       return defval;
  533    }
  534 }
  535 
  536 /*
  537  * Treats src==NULL as ""
  538  * Writes NULL at end of string
  539  */
  540 static char *pref_lstrncpy_realloc(char **dest, const char *src, 
  541                                    int *size, int max_size)
  542 {
  543    int new_size, len;
  544    const char null_str[]="";
  545    const char *Psrc;
  546 
  547    if (!src) {
  548       Psrc=null_str;
  549    } else {
  550       Psrc=src;
  551    }
  552    len=strlen(Psrc)+1;
  553    new_size=*size;
  554    if (len > *size) {
  555       new_size=len;
  556    }
  557    if (new_size > max_size) new_size=max_size;
  558 
  559    if (new_size > *size) {
  560       if (*size == 0) {
  561          *dest=malloc(new_size);
  562       } else {
  563          *dest=realloc(*dest, new_size);
  564       }
  565       if (!(*dest)) {
  566          return "";
  567       }
  568       *size=new_size;
  569    }
  570    g_strlcpy(*dest, Psrc, new_size);
  571 
  572    return *dest;
  573 }
  574 
  575 int jp_set_pref(prefType prefs[], int which, long n, const char *string)
  576 {
  577    const char null_str[]="";
  578    const char *Pstr;
  579 
  580    if (which < 0) {
  581       return EXIT_FAILURE;
  582    }
  583    prefs[which].ivalue = n;
  584    if (string == NULL) {
  585       Pstr=null_str;
  586    } else {
  587       Pstr=string;
  588    }
  589    if (prefs[which].usertype == CHARTYPE) {
  590       pref_lstrncpy_realloc(&(prefs[which].svalue), Pstr,
  591                             &(prefs[which].svalue_size), MAX_PREF_LEN);
  592    }
  593    return EXIT_SUCCESS;
  594 }
  595 
  596 int set_pref(int which, long n, const char *string, int save)
  597 {
  598    const char *str;
  599    int r;
  600 
  601    if (which >= NUM_PREFS) {
  602       return EXIT_FAILURE;
  603    }
  604    str=string;
  605    if ((which==PREF_RCFILE) ||
  606        (which==PREF_SHORTDATE) ||
  607        (which==PREF_LONGDATE) ||
  608        (which==PREF_TIME) ||
  609        (which==PREF_PAPER_SIZE)) {
  610       set_pref_possibility(which, n, FALSE);
  611       str=glob_prefs[which].svalue;
  612    }
  613    r = jp_set_pref(glob_prefs, which, n, str);
  614    if (save) {
  615       pref_write_rc_file();
  616    }
  617 
  618    return r;
  619 }
  620 
  621 int set_pref_possibility(int which, long n, int save)
  622 {
  623    char svalue[MAX_PREF_LEN];
  624    char *str=NULL;
  625    int r;
  626 
  627    if (which >= NUM_PREFS) {
  628       return EXIT_FAILURE;
  629    }
  630    if (glob_prefs[which].usertype == CHARTYPE) {
  631       get_pref_possibility(which, n, svalue);
  632       str=svalue;
  633    }
  634    r = jp_set_pref(glob_prefs, which, n, str);
  635    if (save) {
  636       pref_write_rc_file();
  637 
  638    }
  639 
  640    if (PREF_CHAR_SET == which)
  641       if (otherconv_init())
  642          printf("Error: could not set charset encoding\n");
  643 
  644    return r;
  645 }
  646 
  647 static int validate_glob_prefs(void)
  648 {
  649    int i, r;
  650    char svalue[MAX_PREF_LEN];
  651 
  652    if (t_fmt_ampm) {
  653       if (glob_prefs[PREF_TIME].ivalue >= NUM_TIMES) {
  654          glob_prefs[PREF_TIME].ivalue = NUM_TIMES - 1;
  655       }
  656    }
  657    else {
  658       if (glob_prefs[PREF_TIME].ivalue >= NUM_TIMES_NO_AMPM) {
  659          glob_prefs[PREF_TIME].ivalue = NUM_TIMES_NO_AMPM - 1;
  660       }
  661    }
  662    if (glob_prefs[PREF_TIME].ivalue < 0) {
  663       glob_prefs[PREF_TIME].ivalue = 0;
  664    }
  665 
  666    if (glob_prefs[PREF_SHORTDATE].ivalue >= NUM_SHORTDATES) {
  667       glob_prefs[PREF_SHORTDATE].ivalue = NUM_SHORTDATES - 1;
  668    }
  669    if (glob_prefs[PREF_SHORTDATE].ivalue < 0) {
  670       glob_prefs[PREF_SHORTDATE].ivalue = 0;
  671    }
  672 
  673    if (glob_prefs[PREF_LONGDATE].ivalue >= NUM_LONGDATES) {
  674       glob_prefs[PREF_LONGDATE].ivalue = NUM_LONGDATES - 1;
  675    }
  676    if (glob_prefs[PREF_LONGDATE].ivalue < 0) {
  677       glob_prefs[PREF_LONGDATE].ivalue = 0;
  678    }
  679 
  680    if (glob_prefs[PREF_FDOW].ivalue > 1) {
  681       glob_prefs[PREF_FDOW].ivalue = 1;
  682    }
  683    if (glob_prefs[PREF_FDOW].ivalue < 0) {
  684       glob_prefs[PREF_FDOW].ivalue = 0;
  685    }
  686 
  687    if (glob_prefs[PREF_SHOW_DELETED].ivalue > 1) {
  688       glob_prefs[PREF_SHOW_DELETED].ivalue = 1;
  689    }
  690    if (glob_prefs[PREF_SHOW_DELETED].ivalue < 0) {
  691       glob_prefs[PREF_SHOW_DELETED].ivalue = 0;
  692    }
  693 
  694    if (glob_prefs[PREF_SHOW_MODIFIED].ivalue > 1) {
  695       glob_prefs[PREF_SHOW_MODIFIED].ivalue = 1;
  696    }
  697    if (glob_prefs[PREF_SHOW_MODIFIED].ivalue < 0) {
  698       glob_prefs[PREF_SHOW_MODIFIED].ivalue = 0;
  699    }
  700 
  701    if (glob_prefs[PREF_TODO_HIDE_COMPLETED].ivalue > 1) {
  702       glob_prefs[PREF_TODO_HIDE_COMPLETED].ivalue = 1;
  703    }
  704    if (glob_prefs[PREF_TODO_HIDE_COMPLETED].ivalue < 0) {
  705       glob_prefs[PREF_TODO_HIDE_COMPLETED].ivalue = 0;
  706    }
  707 
  708    if (glob_prefs[PREF_DATEBOOK_HIGHLIGHT_DAYS].ivalue > 1) {
  709       glob_prefs[PREF_DATEBOOK_HIGHLIGHT_DAYS].ivalue = 1;
  710    }
  711    if (glob_prefs[PREF_DATEBOOK_HIGHLIGHT_DAYS].ivalue < 0) {
  712       glob_prefs[PREF_DATEBOOK_HIGHLIGHT_DAYS].ivalue = 0;
  713    }
  714 
  715    if (glob_prefs[PREF_RATE].ivalue >= NUM_RATES) {
  716       glob_prefs[PREF_RATE].ivalue = NUM_RATES - 1;
  717    }
  718    if (glob_prefs[PREF_RATE].ivalue < 0) {
  719       glob_prefs[PREF_RATE].ivalue = 0;
  720    }
  721 
  722    if (glob_prefs[PREF_CHAR_SET].ivalue >= NUM_CHAR_SETS) {
  723       glob_prefs[PREF_CHAR_SET].ivalue = NUM_CHAR_SETS - 1;
  724    }
  725    if (glob_prefs[PREF_CHAR_SET].ivalue < 0) {
  726       glob_prefs[PREF_CHAR_SET].ivalue = 0;
  727    }
  728 
  729    if (glob_prefs[PREF_PAPER_SIZE].ivalue >= NUM_PAPER_SIZES) {
  730       glob_prefs[PREF_PAPER_SIZE].ivalue = NUM_PAPER_SIZES - 1;
  731    }
  732    if (glob_prefs[PREF_PAPER_SIZE].ivalue < 0) {
  733       glob_prefs[PREF_PAPER_SIZE].ivalue = 0;
  734    }
  735 
  736    if (glob_prefs[PREF_NUM_BACKUPS].ivalue >= MAX_PREF_NUM_BACKUPS) {
  737       glob_prefs[PREF_NUM_BACKUPS].ivalue = MAX_PREF_NUM_BACKUPS;
  738    }
  739    if (glob_prefs[PREF_NUM_BACKUPS].ivalue < 1) {
  740       glob_prefs[PREF_NUM_BACKUPS].ivalue = 1;
  741    }
  742 
  743    /* Backwards compatability with MEMO32 preference */
  744    if (glob_prefs[PREF_MEMO32_MODE].ivalue) {
  745       glob_prefs[PREF_MEMO_VERSION].ivalue = 2;
  746       glob_prefs[PREF_MEMO32_MODE].ivalue = 0;
  747    }
  748    if (glob_prefs[PREF_SYNC_MEMO32].ivalue) {
  749       glob_prefs[PREF_SYNC_MEMO].ivalue = 1;
  750       glob_prefs[PREF_SYNC_MEMO32].ivalue = 0;
  751    }
  752 
  753    get_pref_possibility(PREF_TIME, glob_prefs[PREF_TIME].ivalue, svalue);
  754    pref_lstrncpy_realloc(&(glob_prefs[PREF_TIME].svalue), svalue,
  755                          &(glob_prefs[PREF_TIME].svalue_size), MAX_PREF_LEN);
  756 
  757    get_pref_possibility(PREF_SHORTDATE, glob_prefs[PREF_SHORTDATE].ivalue, svalue);
  758    pref_lstrncpy_realloc(&(glob_prefs[PREF_SHORTDATE].svalue), svalue,
  759                          &(glob_prefs[PREF_SHORTDATE].svalue_size), MAX_PREF_LEN);
  760 
  761 
  762    get_pref_possibility(PREF_LONGDATE, glob_prefs[PREF_LONGDATE].ivalue, svalue);
  763    pref_lstrncpy_realloc(&(glob_prefs[PREF_LONGDATE].svalue), svalue,
  764                          &(glob_prefs[PREF_LONGDATE].svalue_size), MAX_PREF_LEN);
  765 
  766    get_pref_possibility(PREF_FDOW, glob_prefs[PREF_FDOW].ivalue, svalue);
  767    pref_lstrncpy_realloc(&(glob_prefs[PREF_FDOW].svalue), svalue,
  768                          &(glob_prefs[PREF_FDOW].svalue_size), MAX_PREF_LEN);
  769 
  770    get_pref_possibility(PREF_RATE, glob_prefs[PREF_RATE].ivalue, svalue);
  771    pref_lstrncpy_realloc(&(glob_prefs[PREF_RATE].svalue), svalue,
  772                          &(glob_prefs[PREF_RATE].svalue_size), MAX_PREF_LEN);
  773 
  774    get_pref_possibility(PREF_PAPER_SIZE, glob_prefs[PREF_PAPER_SIZE].ivalue, svalue);
  775    pref_lstrncpy_realloc(&(glob_prefs[PREF_PAPER_SIZE].svalue), svalue,
  776                          &(glob_prefs[PREF_PAPER_SIZE].svalue_size), MAX_PREF_LEN);
  777 
  778    for (i=0; i<MAX_NUM_PREFS; i++) {
  779       r = get_pref_possibility(PREF_RCFILE, i, svalue);
  780       if (r) break;
  781       if (!strcmp(svalue, glob_prefs[PREF_RCFILE].svalue)) {
  782          glob_prefs[PREF_RCFILE].ivalue = i;
  783          break;
  784       }
  785    }
  786 
  787    return EXIT_SUCCESS;
  788 }
  789 
  790 int jp_pref_read_rc_file(char *filename, prefType prefs[], int num_prefs)
  791 {
  792    int i;
  793    FILE *in;
  794    char line[1024];
  795    char *field1, *field2;
  796    char *Pc;
  797 
  798    in=jp_open_home_file(filename, "r");
  799    if (!in) {
  800       return EXIT_FAILURE;
  801    }
  802 
  803    while (!feof(in)) {
  804       if (fgets(line, sizeof(line), in) == NULL) {
  805          ; /* Keep compiler from complaining */
  806       }
  807       if (feof(in)) break;
  808       line[sizeof(line)-2] = ' ';
  809       line[sizeof(line)-1] = '\0';
  810       field1 = strtok(line, " ");
  811       field2 = (field1 != NULL) ? strtok(NULL, "\n") : NULL;
  812       if ((field1 == NULL) || (field2 == NULL)) {
  813          continue;
  814       }
  815       if ((Pc = (char *)index(field2, '\n'))) {
  816          Pc[0]='\0';
  817       }
  818       for(i=0; i<num_prefs; i++) {
  819           //attempt to convert to new css file if
  820           // pref file has old rc name in it.
  821          if(using_old_rc_name_in_pref_file(field1)){
  822              field1 = "jpilotcss";
  823              field2 = get_new_css_name(field2);
  824          }
  825          if (!strcmp(prefs[i].name, field1)) {
  826             if (prefs[i].filetype == INTTYPE) {
  827                prefs[i].ivalue = atoi(field2);
  828             }
  829             if (prefs[i].filetype == CHARTYPE) {
  830                if (pref_lstrncpy_realloc(&(prefs[i].svalue), field2,
  831                                         &(prefs[i].svalue_size),
  832                                         MAX_PREF_LEN)==NULL) {
  833                   jp_logf(JP_LOG_WARN, "read_rc_file(): %s\n", _("Out of memory"));
  834                   continue;
  835                }
  836             }
  837          }
  838       }
  839    }
  840    fclose(in);
  841 
  842    return EXIT_SUCCESS;
  843 }
  844 
  845 gboolean using_old_rc_name_in_pref_file(const char *field1) {
  846     return (gboolean) (field1 != NULL && strcmp("jpilotrc", field1) == 0);
  847 }
  848 
  849 char *get_new_css_name(char *field2) {
  850     if(field2 != NULL){
  851         if(strcmp(field2, "jpilotrc.green") == 0){
  852             field2 = "jpilotcss.green";
  853         }else if(strcmp(field2,"jpilotrc.blue") == 0){
  854            field2 = "jpilotcss.blue";
  855         }else if(strcmp(field2,"jpilotrc.purple") == 0){
  856             field2 = "jpilotcss.purple";
  857         }else if(strcmp(field2,"jpilotrc.steel") == 0){
  858             field2 = "jpilotcss.steel";
  859         }else {
  860             // not using one of the old default rc files,
  861             // so revert to default.
  862             field2 = "jpilotcss.default";
  863         }
  864     }
  865     return field2;
  866 }
  867 
  868 int pref_read_rc_file(void)
  869 {
  870    int r;
  871 
  872    r = jp_pref_read_rc_file(EPN".rc", glob_prefs, NUM_PREFS);
  873 
  874    validate_glob_prefs();
  875 
  876    return r;
  877 }
  878 
  879 int jp_pref_write_rc_file(char *filename, prefType prefs[], int num_prefs)
  880 {
  881    int i;
  882    FILE *out;
  883 
  884    jp_logf(JP_LOG_DEBUG, "jp_pref_write_rc_file()\n");
  885 
  886    out=jp_open_home_file(filename,"w" );
  887    if (!out) {
  888       return EXIT_FAILURE;
  889    }
  890 
  891    for(i=0; i<num_prefs; i++) {
  892 
  893       if (prefs[i].filetype == INTTYPE) {
  894          fprintf(out, "%s %ld\n", prefs[i].name, prefs[i].ivalue);
  895       }
  896 
  897       if (prefs[i].filetype == CHARTYPE) {
  898          fprintf(out, "%s %s\n", prefs[i].name, prefs[i].svalue);
  899       }
  900    }
  901    jp_close_home_file(out);
  902 
  903    return EXIT_SUCCESS;
  904 }
  905 
  906 int pref_write_rc_file(void)
  907 {
  908    return jp_pref_write_rc_file(EPN".rc", glob_prefs, NUM_PREFS);
  909 }
  910