view scid/scid.c @ 60:3804a2ab60ec

misc: documentation improvements
author David Demelier <markand@malikania.fr>
date Thu, 18 Aug 2022 10:12:54 +0200
parents 85c59fbf1407
children 5076be758687
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 inline void
init_log(void)
{
	log_open("scid");
}

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;
	}
}

static inline void
init_theme(void)
{
	theme_open(scid.themedir);
}

void
scid_init(enum scid_init init)
{
	if (init & SCID_INIT_LOG)
		init_log();
	if (init & SCID_INIT_DATABASE)
		init_database();
	if (init & SCID_INIT_THEME)
		init_theme();
}

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