1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
|
/* prefs.c
* A module of J-Pilot http://jpilot.org
*
* Copyright (C) 1999-2001 by Judd Montgomery
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "i18n.h"
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "prefs.h"
#include "log.h"
/*These are the default settings */
/*name, usertype, filetype, ivalue, char *svalue, svalue_size; */
static prefType glob_prefs[NUM_PREFS] = {
{"jpilotrc", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"time", CHARTYPE, INTTYPE, 0, NULL, 0},
{"sdate", CHARTYPE, INTTYPE, 0, NULL, 0},
{"ldate", CHARTYPE, INTTYPE, 0, NULL, 0},
{"fdow", CHARTYPE, INTTYPE, 0, NULL, 0},
{"show_deleted", INTTYPE, INTTYPE, 0, NULL, 0},
{"show_modified", INTTYPE, INTTYPE, 0, NULL, 0},
{"hide_completed", INTTYPE, INTTYPE, 0, NULL, 0},
{"highlight_days", INTTYPE, INTTYPE, 1, NULL, 0},
{"port", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"rate", CHARTYPE, INTTYPE, 4, NULL, 0},
{"user", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"user_id", INTTYPE, INTTYPE, 0, NULL, 0},
{"pc_id", INTTYPE, INTTYPE, 0, NULL, 0},
{"num_backups", INTTYPE, INTTYPE, 2, NULL, 0},
{"window_width", INTTYPE, INTTYPE, 770, NULL, 0},
{"window_height", INTTYPE, INTTYPE, 500, NULL, 0},
{"datebook_pane", INTTYPE, INTTYPE, 370, NULL, 0},
{"address_pane", INTTYPE, INTTYPE, 340, NULL, 0},
{"todo_pane", INTTYPE, INTTYPE, 370, NULL, 0},
{"memo_pane", INTTYPE, INTTYPE, 370, NULL, 0},
{"use_db3", INTTYPE, INTTYPE, 0, NULL, 0},
{"last_app", INTTYPE, INTTYPE, DATEBOOK, NULL, 0},
{"print_this_many", INTTYPE, INTTYPE, 3, NULL, 0},
{"print_one_per_page", INTTYPE, INTTYPE, 0, NULL ,0},
{"print_blank_lines", INTTYPE, INTTYPE, 1, NULL, 0},
{"print_command", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"char_set", INTTYPE, INTTYPE, CHAR_SET_ENGLISH, NULL, 0},
{"sync_datebook", INTTYPE, INTTYPE, 1, NULL, 0},
{"sync_address", INTTYPE, INTTYPE, 1, NULL, 0},
{"sync_todo", INTTYPE, INTTYPE, 1, NULL, 0},
{"sync_memo", INTTYPE, INTTYPE, 1, NULL, 0},
{"sync_memo32", INTTYPE, INTTYPE, 0, NULL, 0},
{"address_page", INTTYPE, INTTYPE, 0, NULL, 0},
{"output_height", INTTYPE, INTTYPE, 40, NULL, 0},
{"open_alarm_windows", INTTYPE, INTTYPE, 1, NULL, 0},
{"do_alarm_command", INTTYPE, INTTYPE, 0, NULL, 0},
{"alarm_command", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"remind_in", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"remind_units", INTTYPE, INTTYPE, 0, NULL, 0},
/* This is actually the password, but I wanted to name it something more discreet */
{"session_id", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"memo32_mode", INTTYPE, INTTYPE, 0, NULL, 0},
{"paper_size", INTTYPE, INTTYPE, 0, NULL, 0},
{"datebook_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"datebook_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"address_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"address_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"todo_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"todo_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"memo_export_filename", CHARTYPE, CHARTYPE, 0, NULL, 0},
{"memo_import_path", CHARTYPE, CHARTYPE, 0, NULL, 0}
};
struct name_list {
char *name;
struct name_list *next;
};
static struct name_list *dir_list=NULL;
void pref_init()
{
int i;
for (i=0; i<NUM_PREFS; i++) {
switch (i) {
case PREF_RCFILE:
glob_prefs[i].svalue=strdup("jpilotrc.default");
glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
break;
case PREF_PRINT_COMMAND:
glob_prefs[i].svalue=strdup("lpr -h");
glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
break;
case PREF_ALARM_COMMAND:
glob_prefs[i].svalue=strdup("echo %t %d");
glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
break;
case PREF_REMIND_IN:
glob_prefs[i].svalue=strdup("5");
glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
break;
case PREF_PASSWORD:
glob_prefs[i].svalue=strdup("09021345070413440c08135a3215135dd217ead3b5df556322e9a14a994b0f88");
glob_prefs[i].svalue_size=strlen(glob_prefs[i].svalue)+1;
break;
default:
glob_prefs[i].svalue=strdup("");
glob_prefs[i].svalue_size=1;
}
}
}
void jp_pref_init(prefType prefs[], int count)
{
int i;
for (i=0; i<count; i++) {
if (prefs[i].svalue) {
prefs[i].svalue=strdup(prefs[i].svalue);
} else {
prefs[i].svalue=strdup("");
}
prefs[i].svalue_size=strlen(prefs[i].svalue)+1;
}
}
void jp_free_prefs(prefType prefs[], int count)
{
int i;
for (i=0; i<count; i++) {
if (prefs[i].svalue) {
free(prefs[i].svalue);
prefs[i].svalue=NULL;
}
}
}
int get_pref_time_no_secs(char *datef)
{
/* "%I:%M:%S %p" */
/* "%I:%M:%S" */
/* "%I:%M" */
/* "%I:%M%p" */
long ivalue;
const char *svalue;
int i1, i2;
get_pref(PREF_TIME, &ivalue, &svalue);
if (!svalue) {
return -1;
}
for (i1=0, i2=0; ; i1++, i2++) {
if (svalue[i2]=='S') {
i1-=2;
i2++;
}
if (svalue[i2]==' ') {
i1--;
continue;
}
datef[i1]=svalue[i2];
if (svalue[i2]=='\0') {
break;
}
}
return 0;
}
int get_pref_time_no_secs_no_ampm(char *datef)
{
long ivalue;
const char *svalue;
get_pref(PREF_TIME, &ivalue, &svalue);
if (!svalue) {
return -1;
}
if (svalue) {
strncpy(datef, svalue, 5);
datef[5]='\0';
} else {
datef[0]='\0';
}
return 0;
}
int get_pref_dmy_order()
{
long n;
get_pref(PREF_SHORTDATE, &n, NULL);
if (n<1) {
return PREF_MDY;
}
if ((n>0) && (n<4)) {
return PREF_DMY;
}
if ((n>3)) {
return PREF_YMD;
}
return 0;
}
/*This function is used internally to free up any memory that prefs is using */
/* I'm not using this function right now.
void free_name_list(struct name_list **Plist)
{
struct name_list *temp_list, *next_list;
for (temp_list=*Plist; temp_list; temp_list=next_list) {
next_list=temp_list->next;
if (temp_list->name) {
free(temp_list->name);
}
free(temp_list);
}
*Plist=NULL;
}
*/
static int get_rcfile_name(int n, char *rc_copy)
{
DIR *dir;
struct dirent *dirent;
char full_name[256];
int i;
char filename[256];
int found, count;
struct name_list *temp_list, *new_entry;
if (dir_list == NULL) {
i = found = count = 0;
sprintf(filename, "%s/%s/%s/", BASE_DIR, "share", EPN);
jpilot_logf(LOG_DEBUG, "opening dir %s\n", filename);
dir = opendir(filename);
if (dir) {
for(i=0; (dirent = readdir(dir)); i++) {
sprintf(filename, "%s%s", EPN, "rc");
if (strncmp(filename, dirent->d_name, strlen(filename))) {
continue;
} else {
jpilot_logf(LOG_DEBUG, "found %s\n", dirent->d_name);
new_entry = malloc(sizeof(struct name_list));
if (!new_entry) {
jpilot_logf(LOG_FATAL, "get_rcfile_name(): Out of memory\n");
return -1;
}
new_entry->name = strdup(dirent->d_name);
new_entry->next = dir_list;
dir_list = new_entry;
}
}
}
if (dir) {
closedir(dir);
}
get_home_file_name("", full_name, 255);
jpilot_logf(LOG_DEBUG, "opening dir %s\n", full_name);
dir = opendir(full_name);
if (dir) {
for(; (dirent = readdir(dir)); i++) {
sprintf(filename, "%s%s", EPN, "rc");
if (strncmp(filename, dirent->d_name, strlen(filename))) {
continue;
} else {
jpilot_logf(LOG_DEBUG, "found %s\n", dirent->d_name);
new_entry = malloc(sizeof(struct name_list));
if (!new_entry) {
jpilot_logf(LOG_FATAL, "get_rcfile_name(): Out of memory 2\n");
return -1;
}
new_entry->name = strdup(dirent->d_name);
new_entry->next = dir_list;
dir_list = new_entry;
}
}
}
if (dir) {
closedir(dir);
}
}
found = 0;
for (i=0, temp_list=dir_list; temp_list; temp_list=temp_list->next, i++) {
if (i == n) {
strncpy(rc_copy, temp_list->name, MAX_PREF_VALUE);
rc_copy[MAX_PREF_VALUE-1]='\0';
found=1;
break;
}
}
if (found) {
return 0;
} else {
rc_copy[0]='\0';
return -1;
}
}
#define NUM_SHORTDATES 7
#define NUM_LONGDATES 6
#define NUM_TIMES 10
#define NUM_RATES 11
#define NUM_PAPER_SIZES 2
/*if n is out of range then this function will fail */
int get_pref_possibility(int which, int n, char *pref_str)
{
const char *short_date_formats[] = {
"%m/%d/%y",
"%d/%m/%y",
"%d.%m.%y",
"%d-%m-%y",
"%y/%m/%d",
"%y.%m.%d",
"%y-%m-%d"
};
const char *long_date_formats[] = {
"%B %d, %Y",
"%d %B %Y",
"%d. %B %Y",
"%d %B, %Y",
"%Y. %B. %d",
"%Y %B %d"
};
const char *time_formats[] = {
"%I:%M:%S %p",
"%H:%M:%S",
"%I.%M.%S %p",
"%H.%M.%S",
"%H,%M,%S",
"%I:%M %p",
"%H:%M",
"%I.%M %p",
"%H.%M",
"%H,%M"
};
static char *days[2];
static const char *rates[] = {
"300",
"1200",
"2400",
"4800",
"9600",
"19200",
"38400",
"57600",
"115200",
"H230400",
"H460800"
};
static const char *char_sets[] = {
"English",
"Japanese",
"Windows1250 (EE)",
"Host Windows1251 <-> Palm KOI8-R",
"Host KOI8-R <-> Palm Windows-1251",
"Chinese(Big5)",
"Korean"
};
static const char *paper_sizes[] = {
"US Letter",
"A4"
};
days[0] = _("Sunday");
days[1] = _("Monday");
switch(which) {
case PREF_RCFILE:
return get_rcfile_name(n, pref_str);
break;
case PREF_TIME:
if ((n >= NUM_TIMES) || (n<0)) {
pref_str[0]='\0';
return -1;
}
strcpy(pref_str, time_formats[n]);
break;
case PREF_SHORTDATE:
if ((n >= NUM_SHORTDATES) || (n<0)) {
pref_str[0]='\0';
return -1;
}
strcpy(pref_str, short_date_formats[n]);
break;
case PREF_LONGDATE:
if ((n >= NUM_LONGDATES) || (n<0)) {
pref_str[0]='\0';
return -1;
}
strcpy(pref_str, long_date_formats[n]);
break;
case PREF_FDOW:
if ((n > 1) || (n<0)) {
pref_str[0]='\0';
return -1;
}
strcpy(pref_str, days[n]);
break;
case PREF_RATE:
if ((n >= NUM_RATES) || (n<0)) {
pref_str[0]='\0';
return -1;
}
strcpy(pref_str, rates[n]);
break;
case PREF_CHAR_SET:
if ((n >= NUM_CHAR_SETS) || (n<0)) {
pref_str[0]='\0';
return -1;
}
strcpy(pref_str, char_sets[n]);
break;
case PREF_PAPER_SIZE:
if ((n >= NUM_PAPER_SIZES) || (n<0)) {
pref_str[0]='\0';
return -1;
}
strcpy(pref_str, paper_sizes[n]);
break;
default:
pref_str[0]='\0';
jpilot_logf(LOG_DEBUG, "Unknown preference type\n");
return -1;
}
return 0;
}
/*
* WARNING
* Caller must ensure that which is not out of range!
*/
int jp_get_pref(prefType prefs[], int which, long *n, const char **string)
{
if (which < 0) {
return -1;
}
if (n) {
*n = prefs[which].ivalue;
}
if (prefs[which].usertype == CHARTYPE) {
if (string!=NULL) {
*string = prefs[which].svalue;
}
} else {
if (string!=NULL) {
*string = NULL;
}
}
return 0;
}
int get_pref(int which, long *n, const char **string)
{
if (which > NUM_PREFS) {
return -1;
}
return jp_get_pref(glob_prefs, which, n, string);
}
/*
* Get the preference value as integer. If failed to do so, return the
* specified default (defval).
*/
long get_pref_int_default(int which, long defval)
{
long val;
if (get_pref(which, &val, NULL) == 0) {
return val;
}
else {
return defval;
}
}
/*
* Treats src==NULL as ""
* Writes NULL at end of string
*/
char *pref_lstrncpy_realloc(char **dest, const char *src, int *size, int max_size)
{
int new_size, len;
const char null_str[]="";
const char *Psrc;
if (!src) {
Psrc=null_str;
} else {
Psrc=src;
}
len=strlen(Psrc)+1;
new_size=*size;
if (len > *size) {
new_size=len;
}
if (new_size > max_size) new_size=max_size;
if (new_size > *size) {
if (*size == 0) {
*dest=malloc(new_size);
} else {
*dest=realloc(*dest, new_size);
}
if (!(*dest)) {
return 0;
}
*size=new_size;
}
strncpy(*dest, Psrc, new_size);
(*dest)[new_size-1]='\0';
return *dest;
}
int jp_set_pref(prefType prefs[], int which, long n, const char *string)
{
const char null_str[]="";
const char *Pstr;
if (which < 0) {
return -1;
}
prefs[which].ivalue = n;
if (string == NULL) {
Pstr=null_str;
} else {
Pstr=string;
}
if (prefs[which].usertype == CHARTYPE) {
pref_lstrncpy_realloc(&(prefs[which].svalue), Pstr,
&(prefs[which].svalue_size), MAX_PREF_VALUE);
}
/* #ifdef PROMETHEON */
/* Some people like to just kill the window manager */
/* Prometheon kills us, always be prepared for death */
pref_write_rc_file();
/* #endif */
return 0;
}
int set_pref(int which, long n, const char *string)
{
const char *str;
if (which > NUM_PREFS) {
return -1;
}
str=string;
if ((which==PREF_RCFILE) ||
(which==PREF_SHORTDATE) ||
(which==PREF_LONGDATE) ||
(which==PREF_TIME) ||
(which==PREF_PAPER_SIZE)) {
set_pref_possibility(which, n);
str=glob_prefs[which].svalue;
}
return jp_set_pref(glob_prefs, which, n, str);
}
int set_pref_possibility(int which, long n)
{
char svalue[MAX_PREF_VALUE];
char *str=NULL;
if (which > NUM_PREFS) {
return -1;
}
if (glob_prefs[which].usertype == CHARTYPE) {
get_pref_possibility(which, n, svalue);
str=svalue;
}
return jp_set_pref(glob_prefs, which, n, str);
}
static int validate_glob_prefs()
{
int i, r;
char svalue[MAX_PREF_VALUE];
if (glob_prefs[PREF_TIME].ivalue >= NUM_TIMES) {
glob_prefs[PREF_TIME].ivalue = NUM_TIMES - 1;
}
if (glob_prefs[PREF_TIME].ivalue < 0) {
glob_prefs[PREF_TIME].ivalue = 0;
}
if (glob_prefs[PREF_SHORTDATE].ivalue >= NUM_SHORTDATES) {
glob_prefs[PREF_SHORTDATE].ivalue = NUM_SHORTDATES - 1;
}
if (glob_prefs[PREF_SHORTDATE].ivalue < 0) {
glob_prefs[PREF_SHORTDATE].ivalue = 0;
}
if (glob_prefs[PREF_LONGDATE].ivalue >= NUM_LONGDATES) {
glob_prefs[PREF_LONGDATE].ivalue = NUM_LONGDATES - 1;
}
if (glob_prefs[PREF_LONGDATE].ivalue < 0) {
glob_prefs[PREF_LONGDATE].ivalue = 0;
}
if (glob_prefs[PREF_FDOW].ivalue > 1) {
glob_prefs[PREF_FDOW].ivalue = 1;
}
if (glob_prefs[PREF_FDOW].ivalue < 0) {
glob_prefs[PREF_FDOW].ivalue = 0;
}
if (glob_prefs[PREF_SHOW_DELETED].ivalue > 1) {
glob_prefs[PREF_SHOW_DELETED].ivalue = 1;
}
if (glob_prefs[PREF_SHOW_DELETED].ivalue < 0) {
glob_prefs[PREF_SHOW_DELETED].ivalue = 0;
}
if (glob_prefs[PREF_SHOW_MODIFIED].ivalue > 1) {
glob_prefs[PREF_SHOW_MODIFIED].ivalue = 1;
}
if (glob_prefs[PREF_SHOW_MODIFIED].ivalue < 0) {
glob_prefs[PREF_SHOW_MODIFIED].ivalue = 0;
}
if (glob_prefs[PREF_HIDE_COMPLETED].ivalue > 1) {
glob_prefs[PREF_HIDE_COMPLETED].ivalue = 1;
}
if (glob_prefs[PREF_HIDE_COMPLETED].ivalue < 0) {
glob_prefs[PREF_HIDE_COMPLETED].ivalue = 0;
}
if (glob_prefs[PREF_HIGHLIGHT].ivalue > 1) {
glob_prefs[PREF_HIGHLIGHT].ivalue = 1;
}
if (glob_prefs[PREF_HIGHLIGHT].ivalue < 0) {
glob_prefs[PREF_HIGHLIGHT].ivalue = 0;
}
if (glob_prefs[PREF_RATE].ivalue >= NUM_RATES) {
glob_prefs[PREF_RATE].ivalue = NUM_RATES - 1;
}
if (glob_prefs[PREF_RATE].ivalue < 0) {
glob_prefs[PREF_RATE].ivalue = 0;
}
if (glob_prefs[PREF_CHAR_SET].ivalue >= NUM_CHAR_SETS) {
glob_prefs[PREF_CHAR_SET].ivalue = NUM_CHAR_SETS - 1;
}
if (glob_prefs[PREF_CHAR_SET].ivalue < 0) {
glob_prefs[PREF_CHAR_SET].ivalue = 0;
}
if (glob_prefs[PREF_PAPER_SIZE].ivalue >= NUM_PAPER_SIZES) {
glob_prefs[PREF_PAPER_SIZE].ivalue = NUM_PAPER_SIZES - 1;
}
if (glob_prefs[PREF_PAPER_SIZE].ivalue < 0) {
glob_prefs[PREF_PAPER_SIZE].ivalue = 0;
}
if (glob_prefs[PREF_NUM_BACKUPS].ivalue >= MAX_PREF_NUM_BACKUPS) {
glob_prefs[PREF_NUM_BACKUPS].ivalue = MAX_PREF_NUM_BACKUPS;
}
if (glob_prefs[PREF_NUM_BACKUPS].ivalue < 1) {
glob_prefs[PREF_NUM_BACKUPS].ivalue = 1;
}
get_pref_possibility(PREF_TIME, glob_prefs[PREF_TIME].ivalue, svalue);
pref_lstrncpy_realloc(&(glob_prefs[PREF_TIME].svalue), svalue,
&(glob_prefs[PREF_TIME].svalue_size), MAX_PREF_VALUE);
get_pref_possibility(PREF_SHORTDATE, glob_prefs[PREF_SHORTDATE].ivalue, svalue);
pref_lstrncpy_realloc(&(glob_prefs[PREF_SHORTDATE].svalue), svalue,
&(glob_prefs[PREF_SHORTDATE].svalue_size), MAX_PREF_VALUE);
get_pref_possibility(PREF_LONGDATE, glob_prefs[PREF_LONGDATE].ivalue, svalue);
pref_lstrncpy_realloc(&(glob_prefs[PREF_LONGDATE].svalue), svalue,
&(glob_prefs[PREF_LONGDATE].svalue_size), MAX_PREF_VALUE);
get_pref_possibility(PREF_FDOW, glob_prefs[PREF_FDOW].ivalue, svalue);
pref_lstrncpy_realloc(&(glob_prefs[PREF_FDOW].svalue), svalue,
&(glob_prefs[PREF_FDOW].svalue_size), MAX_PREF_VALUE);
get_pref_possibility(PREF_RATE, glob_prefs[PREF_RATE].ivalue, svalue);
pref_lstrncpy_realloc(&(glob_prefs[PREF_RATE].svalue), svalue,
&(glob_prefs[PREF_RATE].svalue_size), MAX_PREF_VALUE);
get_pref_possibility(PREF_PAPER_SIZE, glob_prefs[PREF_PAPER_SIZE].ivalue, svalue);
pref_lstrncpy_realloc(&(glob_prefs[PREF_PAPER_SIZE].svalue), svalue,
&(glob_prefs[PREF_PAPER_SIZE].svalue_size), MAX_PREF_VALUE);
for (i=0; i<1000; i++) {
r = get_pref_possibility(PREF_RCFILE, i, svalue);
if (r) break;
if (!strcmp(svalue, glob_prefs[PREF_RCFILE].svalue)) {
glob_prefs[PREF_RCFILE].ivalue = i;
break;
}
}
return 0;
}
int jp_pref_read_rc_file(char *filename, prefType prefs[], int num_prefs)
{
int i;
FILE *in;
char line[1024];
char *field1, *field2;
char *Pc;
in=jp_open_home_file(filename, "r");
if (!in) {
return -1;
}
while (!feof(in)) {
fgets(line, 1024, in);
if (feof(in)) break;
line[1023] = ' ';
line[1023] = '\0';
field1 = strtok(line, " ");
field2 = (field1 != NULL) ? strtok(NULL, "\n") : NULL;/* jonh */
if ((field1 == NULL) || (field2 == NULL)) {
continue;
}
if ((Pc = (char *)index(field2, '\n'))) {
Pc[0]='\0';
}
for(i=0; i<num_prefs; i++) {
if (!strcmp(prefs[i].name, field1)) {
if (prefs[i].filetype == INTTYPE) {
prefs[i].ivalue = atoi(field2);
}
if (prefs[i].filetype == CHARTYPE) {
if (pref_lstrncpy_realloc(&(prefs[i].svalue), field2,
&(prefs[i].svalue_size),
MAX_PREF_VALUE)==NULL) {
jpilot_logf(LOG_WARN, "Out of memory: read_rc_file()\n");
continue;
}
}
}
}
}
fclose(in);
return 0;
}
int pref_read_rc_file()
{
int r;
r=jp_pref_read_rc_file("jpilot.rc", glob_prefs, NUM_PREFS);
validate_glob_prefs();
return r;
}
int jp_pref_write_rc_file(char *filename, prefType prefs[], int num_prefs)
{
int i;
FILE *out;
jpilot_logf(LOG_DEBUG, "jp_pref_write_rc_file()\n");
out=jp_open_home_file(filename,"w" );
if (!out) {
return -1;
}
for(i=0; i<num_prefs; i++) {
if (prefs[i].filetype == INTTYPE) {
fprintf(out, "%s %ld\n", prefs[i].name, prefs[i].ivalue);
}
if (prefs[i].filetype == CHARTYPE) {
fprintf(out, "%s %s\n", prefs[i].name, prefs[i].svalue);
}
}
fclose(out);
return 0;
}
int pref_write_rc_file()
{
return jp_pref_write_rc_file("jpilot.rc", glob_prefs, NUM_PREFS);
}
|