Annotation of gnutrition/gui_main.c, revision 1.1

1.1     ! asm         1: // SPDX-License-Identifier: GPL-3.0-or-later
        !             2: /*
        !             3:  * $Id$
        !             4:  *
        !             5:  * gui_main.c - Entry point for gnutrition-gui
        !             6:  *
        !             7:  * Copyright (C) 2026 Free Software Foundation, Inc.
        !             8:  *
        !             9:  * Author: Jason Self <jself@gnu.org>
        !            10:  *         Anton McClure <asm@gnu.org>
        !            11:  */
        !            12: 
        !            13: #ifdef HAVE_CONFIG_H
        !            14: #include <config.h>
        !            15: #endif
        !            16: 
        !            17: #include "budget.h"
        !            18: #include "db.h"
        !            19: #include "gui.h"
        !            20: #include "log.h"
        !            21: #include "i18n.h"
        !            22: 
        !            23: #include <errno.h>
        !            24: #include <locale.h>
        !            25: #include <stdio.h>
        !            26: #include <stdlib.h>
        !            27: #include <string.h>
        !            28: #include <sys/stat.h>
        !            29: 
        !            30: #define PROGRAM_NAME "gnutrition-gui"
        !            31: 
        !            32: /* Get the path to the user's log database.  Uses
        !            33:    $XDG_DATA_HOME/gnutrition/log.db or ~/.local/share/gnutrition/log.db.
        !            34:    Returns a dynamically-allocated string.  */
        !            35: static char *
        !            36: get_log_path (void)
        !            37: {
        !            38:   const char *data_home;
        !            39:   char *path;
        !            40:   size_t len;
        !            41: 
        !            42:   data_home = getenv ("XDG_DATA_HOME");
        !            43:   if (data_home && data_home[0] != '\0')
        !            44:     {
        !            45:       len = strlen (data_home) + strlen ("/gnutrition/log.db") + 1;
        !            46:       path = malloc (len);
        !            47:       if (!path)
        !            48:         {
        !            49:           fprintf (stderr, _("%s: memory exhausted\n"), PROGRAM_NAME);
        !            50:           return NULL;
        !            51:         }
        !            52:       snprintf (path, len, "%s/gnutrition/log.db", data_home);
        !            53:     }
        !            54:   else
        !            55:     {
        !            56:       const char *home = getenv ("HOME");
        !            57:       if (!home)
        !            58:         {
        !            59:           fprintf (stderr, _("%s: HOME is not set\n"), PROGRAM_NAME);
        !            60:           return NULL;
        !            61:         }
        !            62:       len = strlen (home)
        !            63:             + strlen ("/.local/share/gnutrition/log.db") + 1;
        !            64:       path = malloc (len);
        !            65:       if (!path)
        !            66:         {
        !            67:           fprintf (stderr, _("%s: memory exhausted\n"), PROGRAM_NAME);
        !            68:           return NULL;
        !            69:         }
        !            70:       snprintf (path, len, "%s/.local/share/gnutrition/log.db", home);
        !            71:     }
        !            72: 
        !            73:   return path;
        !            74: }
        !            75: 
        !            76: /* Ensure the directory containing PATH exists.  */
        !            77: static int
        !            78: ensure_dir (const char *path)
        !            79: {
        !            80:   char *slash;
        !            81:   char *copy;
        !            82: 
        !            83:   copy = strdup (path);
        !            84:   if (!copy)
        !            85:     {
        !            86:       fprintf (stderr, _("%s: memory exhausted\n"), PROGRAM_NAME);
        !            87:       return -1;
        !            88:     }
        !            89: 
        !            90:   slash = strrchr (copy, '/');
        !            91:   if (slash)
        !            92:     {
        !            93:       char *p;
        !            94:       *slash = '\0';
        !            95: 
        !            96:       for (p = copy + 1; *p; p++)
        !            97:         {
        !            98:           if (*p == '/')
        !            99:             {
        !           100:               *p = '\0';
        !           101:               if (mkdir (copy, 0755) < 0 && errno != EEXIST)
        !           102:                 {
        !           103:                   fprintf (stderr,
        !           104:                            _("%s: cannot create directory '%s': %s\n"),
        !           105:                            PROGRAM_NAME, copy, strerror (errno));
        !           106:                   free (copy);
        !           107:                   return -1;
        !           108:                 }
        !           109:               *p = '/';
        !           110:             }
        !           111:         }
        !           112:       if (mkdir (copy, 0755) < 0 && errno != EEXIST)
        !           113:         {
        !           114:           fprintf (stderr, _("%s: cannot create directory '%s': %s\n"),
        !           115:                    PROGRAM_NAME, copy, strerror (errno));
        !           116:           free (copy);
        !           117:           return -1;
        !           118:         }
        !           119:     }
        !           120: 
        !           121:   free (copy);
        !           122:   return 0;
        !           123: }
        !           124: 
        !           125: int
        !           126: main (int argc, char **argv)
        !           127: {
        !           128:   const char *db_path;
        !           129:   char *log_path;
        !           130:   sqlite3 *food_db;
        !           131:   sqlite3 *log_db;
        !           132:   int calories;
        !           133:   int exit_status;
        !           134: 
        !           135:   setlocale (LC_ALL, "");
        !           136:   bindtextdomain ("gnutrition", LOCALEDIR);
        !           137:   textdomain ("gnutrition");
        !           138: 
        !           139:   static char food_db_full_path[1024];
        !           140:   snprintf(food_db_full_path, sizeof(food_db_full_path), "%s/food.db", GNUTRITION_DATADIR);
        !           141:   db_path = food_db_full_path;
        !           142: 
        !           143:   calories = 2000;
        !           144: 
        !           145:   /* Open the food database.  */
        !           146:   food_db = db_open (db_path);
        !           147:   if (!food_db)
        !           148:     return 1;
        !           149: 
        !           150:   /* Open the user's log database.  */
        !           151:   log_path = get_log_path ();
        !           152:   if (!log_path)
        !           153:     {
        !           154:       db_close (food_db);
        !           155:       return 1;
        !           156:     }
        !           157: 
        !           158:   if (ensure_dir (log_path) < 0)
        !           159:     {
        !           160:       db_close (food_db);
        !           161:       free (log_path);
        !           162:       return 1;
        !           163:     }
        !           164: 
        !           165:   log_db = log_open (log_path);
        !           166:   if (!log_db)
        !           167:     {
        !           168:       db_close (food_db);
        !           169:       free (log_path);
        !           170:       return 1;
        !           171:     }
        !           172: 
        !           173:   /* Load saved profile calorie target if available.  */
        !           174:   {
        !           175:     struct user_profile saved;
        !           176:     int rc = log_get_profile (log_db, &saved);
        !           177:     if (rc == 0 && saved.calorie_target > 0)
        !           178:       calories = saved.calorie_target;
        !           179:   }
        !           180: 
        !           181:   exit_status = gui_run (food_db, log_db, calories, argc, argv);
        !           182: 
        !           183:   log_close (log_db);
        !           184:   db_close (food_db);
        !           185:   free (log_path);
        !           186: 
        !           187:   return exit_status;
        !           188: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>