LCOV - code coverage report
Current view: top level - libgnucash/app-utils - gnc-prefs-utils.c (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 48.9 % 47 23
Test Date: 2025-02-07 16:25:45 Functions: 80.0 % 5 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /********************************************************************\
       2                 :             :  * gnc-prefs-utils.c -- utility functions for preferences management*
       3                 :             :  * Copyright (C) 2013 Geert Janssens <geert@kobaltwit.be>           *
       4                 :             :  *                                                                  *
       5                 :             :  * This program is free software; you can redistribute it and/or    *
       6                 :             :  * modify it under the terms of the GNU General Public License as   *
       7                 :             :  * published by the Free Software Foundation; either version 2 of   *
       8                 :             :  * the License, or (at your option) any later version.              *
       9                 :             :  *                                                                  *
      10                 :             :  * This program is distributed in the hope that it will be useful,  *
      11                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
      12                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
      13                 :             :  * GNU General Public License for more details.                     *
      14                 :             :  *                                                                  *
      15                 :             :  * You should have received a copy of the GNU General Public License*
      16                 :             :  * along with this program; if not, contact:                        *
      17                 :             :  *                                                                  *
      18                 :             :  * Free Software Foundation           Voice:  +1-617-542-5942       *
      19                 :             :  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
      20                 :             :  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
      21                 :             :  *                                                                  *
      22                 :             : \********************************************************************/
      23                 :             : 
      24                 :             : #include <config.h>
      25                 :             : 
      26                 :             : #include "gnc-gsettings.h"
      27                 :             : #include "gnc-prefs-utils.h"
      28                 :             : #include "gnc-prefs.h"
      29                 :             : #include "xml/gnc-backend-xml.h"
      30                 :             : 
      31                 :             : static QofLogModule log_module = G_LOG_DOMAIN;
      32                 :             : 
      33                 :             : /* Keys used for core preferences */
      34                 :             : #define GNC_PREF_FILE_COMPRESSION    "file-compression"
      35                 :             : #define GNC_PREF_RETAIN_TYPE_NEVER   "retain-type-never"
      36                 :             : #define GNC_PREF_RETAIN_TYPE_DAYS    "retain-type-days"
      37                 :             : #define GNC_PREF_RETAIN_TYPE_FOREVER "retain-type-forever"
      38                 :             : #define GNC_PREF_RETAIN_DAYS         "retain-days"
      39                 :             : 
      40                 :             : /***************************************************************
      41                 :             :  * Initialization                                              *
      42                 :             :  ***************************************************************/
      43                 :             : static void
      44                 :           1 : file_retain_changed_cb(gpointer gsettings, gchar *key, gpointer user_data)
      45                 :             : {
      46                 :           1 :     if (gnc_prefs_is_set_up())
      47                 :             :     {
      48                 :           0 :         gint days = (int)gnc_prefs_get_float(GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS);
      49                 :           0 :         gnc_prefs_set_file_retention_days (days);
      50                 :             :     }
      51                 :           1 : }
      52                 :             : 
      53                 :             : static void
      54                 :           1 : file_retain_type_changed_cb(gpointer gsettings, gchar *key, gpointer user_data)
      55                 :             : {
      56                 :           1 :     XMLFileRetentionType type = XML_RETAIN_ALL;
      57                 :             : 
      58                 :           1 :     if (gnc_prefs_is_set_up())
      59                 :             :     {
      60                 :           0 :         if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_NEVER))
      61                 :           0 :             type = XML_RETAIN_NONE;
      62                 :           0 :         else if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_DAYS))
      63                 :           0 :             type = XML_RETAIN_DAYS;
      64                 :           0 :         else if (!gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER))
      65                 :           0 :             PWARN("no file retention policy was set, assuming conservative policy 'forever'");
      66                 :             : 
      67                 :           0 :         gnc_prefs_set_file_retention_policy (type);
      68                 :             :     }
      69                 :           1 : }
      70                 :             : 
      71                 :             : static void
      72                 :           1 : file_compression_changed_cb(gpointer gsettings, gchar *key, gpointer user_data)
      73                 :             : {
      74                 :           1 :     if (gnc_prefs_is_set_up())
      75                 :             :     {
      76                 :           0 :         gboolean file_compression = gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL, GNC_PREF_FILE_COMPRESSION);
      77                 :           0 :         gnc_prefs_set_file_save_compressed (file_compression);
      78                 :             :     }
      79                 :           1 : }
      80                 :             : 
      81                 :             : 
      82                 :           1 : void gnc_prefs_init (void)
      83                 :             : {
      84                 :           1 :     gnc_gsettings_load_backend();
      85                 :             : 
      86                 :             :     /* Initialize the core preferences by reading their values from the loaded backend.
      87                 :             :      * Note: if no backend was loaded, these functions will return sane default values.
      88                 :             :      */
      89                 :           1 :     file_retain_changed_cb (NULL, NULL, NULL);
      90                 :           1 :     file_retain_type_changed_cb (NULL, NULL, NULL);
      91                 :           1 :     file_compression_changed_cb (NULL, NULL, NULL);
      92                 :             : 
      93                 :             :     /* Check for invalid retain_type (days)/retain_days (0) combo.
      94                 :             :      * This can happen either because a user changed the preferences
      95                 :             :      * manually outside of GnuCash, or because the user upgraded from
      96                 :             :      * gnucash version 2.3.15 or older. Back then, 0 retain_days meant
      97                 :             :      * "keep forever". From 2.3.15 on this is controlled via a multiple
      98                 :             :      * choice ("retain_type").
      99                 :             :      * So if we find a 0 retain_days value with a "days" retain_type,
     100                 :             :      * we will silently and conservatively interpret is as meaning
     101                 :             :      * retain forever ("forever" retain_type).
     102                 :             :      */
     103                 :           2 :     if ( (gnc_prefs_get_file_retention_policy () == XML_RETAIN_DAYS) &&
     104                 :           1 :             (gnc_prefs_get_file_retention_days () == 0 ) )
     105                 :             :     {
     106                 :           0 :         gnc_prefs_set_file_retention_policy (XML_RETAIN_ALL);
     107                 :           0 :         gnc_prefs_set_file_retention_days (30);
     108                 :           0 :         gnc_prefs_set_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER, TRUE);
     109                 :           0 :         gnc_prefs_set_float (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS, 30);
     110                 :           0 :         PWARN("retain 0 days policy was set, but this is probably not what the user wanted,\n"
     111                 :             :               "assuming conservative policy 'forever'");
     112                 :             :     }
     113                 :             : 
     114                 :             :     /* Add hooks to update core preferences whenever the associated preference changes */
     115                 :           1 :     gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS,
     116                 :             :                            file_retain_changed_cb, NULL);
     117                 :           1 :     gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_NEVER,
     118                 :             :                            file_retain_type_changed_cb, NULL);
     119                 :           1 :     gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_DAYS,
     120                 :             :                            file_retain_type_changed_cb, NULL);
     121                 :           1 :     gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER,
     122                 :             :                            file_retain_type_changed_cb, NULL);
     123                 :           1 :     gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_FILE_COMPRESSION,
     124                 :             :                            file_compression_changed_cb, NULL);
     125                 :             : 
     126                 :           1 : }
     127                 :             : 
     128                 :             : void
     129                 :           0 : gnc_prefs_remove_registered (void)
     130                 :             : {
     131                 :             :     // remove the registered pref call backs above
     132                 :           0 :     gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS,
     133                 :             :                            file_retain_changed_cb, NULL);
     134                 :           0 :     gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_NEVER,
     135                 :             :                            file_retain_type_changed_cb, NULL);
     136                 :           0 :     gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_DAYS,
     137                 :             :                            file_retain_type_changed_cb, NULL);
     138                 :           0 :     gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER,
     139                 :             :                            file_retain_type_changed_cb, NULL);
     140                 :           0 :     gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_FILE_COMPRESSION,
     141                 :             :                            file_compression_changed_cb, NULL);
     142                 :           0 :     gnc_gsettings_shutdown ();
     143                 :           0 : }
        

Generated by: LCOV version 2.0-1