view scid/scid.c @ 46:16f1c72d1635

scid: simplify theme code
author David Demelier <markand@malikania.fr>
date Mon, 15 Aug 2022 17:57:34 +0200
parents 576f4b1ec79f
children 85c59fbf1407
line wrap: on
line source

/*
 * scid.c -- main scid file and configuration
 *
 * Copyright (c) 2021-2022 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <assert.h>
#include <stdio.h>

#include "db.h"
#include "log.h"
#include "scid.h"
#include "theme.h"

struct scid scid = {
	.dbpath = VARDIR "/db/sci/sci.db"
};

static void
init_misc(void)
{
	log_open("scid: version " VERSION);
	log_info("scid: opening database %s", scid.dbpath);

	theme_open(scid.themedir);
}

static void
init_database(void)
{
	if (db_open(scid.dbpath) < 0)
		log_die("scid: unable to open database");

	/* Retrieve or create the API key if not existing. */
	switch (db_key_get(scid.apikey, sizeof (scid.apikey))) {
	case 0:
		/* Not found, create immediately. */
		if (db_key_init(scid.apikey, sizeof (scid.apikey)) < 0)
			log_die("scid: unable to insert key");

		break;
	case -1:
		log_die("scid: unable to retrieve API key in database");
		break;
	default:
		/* We already fetched it. */
		break;
	}
}

void
scid_init(void)
{
	init_misc();
	init_database();
}

void
scid_finish(void)
{
	db_finish();
	log_finish();
}