summaryrefslogtreecommitdiff
path: root/src/nautilus-date-utilities.h
blob: 60a2fbecdad23ca333ff15f1d671936531aeec6a (plain)
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
/*
 * Copyright (C) 2023 The GNOME project contributors
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#pragma once

#include <glib.h>


void
nautilus_date_setup_preferences (void);

char *
nautilus_date_to_str (GDateTime *timestamp,
                      gboolean   use_short_format);

char *
nautilus_date_range_to_str (GPtrArray *date_range,
                            gboolean   use_short_format);

char *
nautilus_date_preview_detailed_format (GDateTime *timestamp,
                                       gboolean   use_detailed);

/* TODO Upstreamed to GLib in 2.89.3. Remove once the minimum required glib
   version is higher. */
#if !GLIB_CHECK_VERSION (2, 89, 3)
/**
 * g_set_date_time: (skip)
 * @date_time_pointer: (inout) (not optional) (nullable): a pointer to either
 *   a #GDateTime or `NULL`
 * @new_date_time: (nullable): a #GDateTime to assign to @date_time_pointer
 *
 * Updates a pointer to a #GDateTime to @new_date_time, adjusts the ref-counts
 * accordingly and returns whether @date_time_pointer was changed.
 *
 * If @new_date_time matches the previous date time, this function is a no-op.
 * If @new_date_time is different, its ref-count will be increased and it will
 * be assigned to @date_time_pointer.
 * The previous date time pointed to by @date_time_pointer will have its
 * ref-count decreased.
 *
 * @date_time_pointer must not be `NULL`, but can point to a `NULL` value.
 *
 * Returns: true if the value of @date_time_pointer changed, false otherwise
 */
static inline gboolean
g_set_date_time (GDateTime **date_time_pointer,
                 GDateTime  *new_date_time)
{
    GDateTime *old_date_time;

    if (*date_time_pointer == new_date_time ||
        (*date_time_pointer != NULL &&
         new_date_time != NULL &&
         g_date_time_compare (*date_time_pointer, new_date_time) == 0))
    {
        return FALSE;
    }

    if (new_date_time != NULL)
    {
        g_date_time_ref (new_date_time);
    }

    old_date_time = *date_time_pointer;
    *date_time_pointer = new_date_time;

    if (old_date_time != NULL)
    {
        g_date_time_unref (old_date_time);
    }

    return TRUE;
}
#endif

/* This is meant to be upstreamed to GLib, but live in-tree for now */

/**
 * g_set_ptr_array: (skip)
 * @ptr_array_pointer: (inout) (not optional) (nullable): a pointer to either
 *   a #GPtrArray or `NULL`
 * @new_ptr_array: (nullable): a #GPtrArray to assign to @ptr_array_pointer
 *
 * Updates a pointer to a #GPtrArray to @new_ptr_array, adjusts the ref-counts
 * accordingly and returns whether @ptr_array_pointer was changed.
 *
 * If @new_ptr_array matches the previous array, this function is a no-op.
 * If @new_ptr_array is different, its ref-count will be increased and it will
 * be assigned to @ptr_array_pointer.
 * The previous array pointed to by @ptr_array_pointer will have its
 * ref-count decreased.
 *
 * @ptr_array_pointer must not be `NULL`, but can point to a `NULL` value.
 *
 * Returns: true if the value of @ptr_array_pointer changed, false otherwise
 */
static inline gboolean
g_set_ptr_array (GPtrArray **ptr_array_pointer,
                 GPtrArray  *new_ptr_array)
{
    if (*ptr_array_pointer == new_ptr_array)
    {
        return FALSE;
    }

    if (new_ptr_array != NULL)
    {
        g_ptr_array_ref (new_ptr_array);
    }

    GPtrArray *old_ptr_array = *ptr_array_pointer;
    *ptr_array_pointer = new_ptr_array;

    if (old_ptr_array != NULL)
    {
        g_ptr_array_unref (old_ptr_array);
    }

    return TRUE;
}