malcontent/libmalcontent/session-history.c

101 lines
2.8 KiB
C
Raw Normal View History

/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright © 2020 Endless Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* - Philip Withnall <withnall@endlessm.com>
*/
#include "config.h"
#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <libmalcontent/session-history.h>
#include "libmalcontent/session-history-private.h"
/* struct _MctSessionHistory is defined in session-history-private.h */
G_DEFINE_BOXED_TYPE (MctSessionHistory, mct_session_history,
mct_session_history_ref, mct_session_history_unref)
/**
* mct_session_history_ref:
* @history: (transfer none): an #MctSessionHistory
*
* Increment the reference count of @history, and return the same pointer to it.
*
* Returns: (transfer full): the same pointer as @history
* Since: 0.5.0
*/
MctSessionHistory *
mct_session_history_ref (MctSessionHistory *history)
{
g_return_val_if_fail (history != NULL, NULL);
g_return_val_if_fail (history->ref_count >= 1, NULL);
g_return_val_if_fail (history->ref_count <= G_MAXINT - 1, NULL);
history->ref_count++;
return history;
}
/**
* mct_session_history_unref:
* @history: (transfer full): an #MctSessionHistory
*
* Decrement the reference count of @history. If the reference count reaches
* zero, free the @history and all its resources.
*
* Since: 0.5.0
*/
void
mct_session_history_unref (MctSessionHistory *history)
{
g_return_if_fail (history != NULL);
g_return_if_fail (history->ref_count >= 1);
history->ref_count--;
if (history->ref_count <= 0)
{
g_clear_pointer (&history->start, g_date_time_unref);
g_clear_pointer (&history->end, g_date_time_unref);
g_free (history);
}
}
/**
* mct_session_history_get_user_id:
* @history: an #MctSessionHistory
*
* Get the user ID of the user this #MctSessionHistory is for.
*
* Returns: user ID of the relevant user
* Since: 0.5.0
*/
uid_t
mct_session_history_get_user_id (MctSessionHistory *history)
{
g_return_val_if_fail (history != NULL, (uid_t) -1);
g_return_val_if_fail (history->ref_count >= 1, (uid_t) -1);
return history->user_id;
}