LCOV - code coverage report
Current view: top level - src/plugins/share - valent-share-preferences.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 57.3 % 75 43
Test Date: 2024-04-23 06:02:46 Functions: 80.0 % 10 8
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 17.9 % 28 5

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       2                 :             : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
       3                 :             : 
       4                 :             : #define G_LOG_DOMAIN "valent-share-preferences"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <glib/gi18n.h>
       9                 :             : #include <adwaita.h>
      10                 :             : #include <gtk/gtk.h>
      11                 :             : #include <valent.h>
      12                 :             : 
      13                 :             : #include "valent-share-preferences.h"
      14                 :             : 
      15                 :             : 
      16                 :             : struct _ValentSharePreferences
      17                 :             : {
      18                 :             :   ValentDevicePreferencesGroup  parent_instance;
      19                 :             : 
      20                 :             :   /* template */
      21                 :             :   GtkLabel                     *download_folder_label;
      22                 :             : };
      23                 :             : 
      24   [ +  +  +  - ]:          19 : G_DEFINE_FINAL_TYPE (ValentSharePreferences, valent_share_preferences, VALENT_TYPE_DEVICE_PREFERENCES_GROUP)
      25                 :             : 
      26                 :             : 
      27                 :             : /*
      28                 :             :  * Download Folder
      29                 :             :  */
      30                 :             : static gboolean
      31                 :           1 : on_download_folder_changed (GValue   *value,
      32                 :             :                             GVariant *variant,
      33                 :             :                             gpointer  user_data)
      34                 :             : {
      35                 :           1 :   const char *label;
      36                 :           2 :   g_autofree char *basename = NULL;
      37                 :           1 :   g_autofree char *result = NULL;
      38                 :             : 
      39                 :           1 :   label = g_variant_get_string (variant, NULL);
      40                 :           1 :   basename = g_path_get_basename (label);
      41                 :           1 :   result = g_strdup_printf ("…/%s", basename);
      42                 :             : 
      43                 :           1 :   g_value_set_string (value, result);
      44                 :             : 
      45                 :           1 :   return TRUE;
      46                 :             : }
      47                 :             : 
      48                 :             : static void
      49                 :           0 : gtk_file_dialog_select_folder_cb (GtkFileDialog                *dialog,
      50                 :             :                                   GAsyncResult                 *result,
      51                 :             :                                   ValentDevicePreferencesGroup *group)
      52                 :             : {
      53                 :           0 :   g_autoptr (GFile) file = NULL;
      54                 :           0 :   g_autoptr (GError) error = NULL;
      55                 :           0 :   GSettings *settings;
      56                 :           0 :   const char *path;
      57                 :             : 
      58         [ #  # ]:           0 :   g_assert (VALENT_IS_DEVICE_PREFERENCES_GROUP (group));
      59                 :             : 
      60                 :           0 :   file = gtk_file_dialog_select_folder_finish (dialog, result, &error);
      61                 :             : 
      62         [ #  # ]:           0 :   if (file == NULL)
      63                 :             :     {
      64   [ #  #  #  # ]:           0 :       if (!g_error_matches (error, GTK_DIALOG_ERROR, GTK_DIALOG_ERROR_CANCELLED) &&
      65                 :           0 :           !g_error_matches (error, GTK_DIALOG_ERROR, GTK_DIALOG_ERROR_DISMISSED))
      66                 :           0 :         g_warning ("%s(): %s", G_STRFUNC, error->message);
      67                 :             : 
      68         [ #  # ]:           0 :       return;
      69                 :             :     }
      70                 :             : 
      71                 :           0 :   path = g_file_peek_path (file);
      72                 :           0 :   settings = valent_device_preferences_group_get_settings (group);
      73         [ #  # ]:           0 :   g_settings_set_string (settings, "download-folder", path);
      74                 :             : }
      75                 :             : 
      76                 :             : /*
      77                 :             :  * GActions
      78                 :             :  */
      79                 :             : static void
      80                 :           0 : select_download_folder_action (GtkWidget  *widget,
      81                 :             :                                const char *action_name,
      82                 :             :                                GVariant   *parameter)
      83                 :             : {
      84                 :           0 :   ValentSharePreferences *self = VALENT_SHARE_PREFERENCES (widget);
      85                 :           0 :   ValentDevicePreferencesGroup *group = VALENT_DEVICE_PREFERENCES_GROUP (self);
      86                 :           0 :   g_autoptr (GtkFileDialog) dialog = NULL;
      87                 :           0 :   GSettings *settings;
      88         [ #  # ]:           0 :   g_autofree char *path = NULL;
      89                 :             : 
      90         [ #  # ]:           0 :   g_assert (VALENT_IS_SHARE_PREFERENCES (self));
      91                 :             : 
      92                 :           0 :   dialog = g_object_new (GTK_TYPE_FILE_DIALOG,
      93                 :             :                          "title",        _("Select download folder"),
      94                 :             :                          "accept-label", _("Open"),
      95                 :             :                          "modal",        TRUE,
      96                 :             :                          NULL);
      97                 :             : 
      98                 :           0 :   settings = valent_device_preferences_group_get_settings (group);
      99                 :           0 :   path = g_settings_get_string (settings, "download-folder");
     100                 :             : 
     101         [ #  # ]:           0 :   if (strlen (path) > 0)
     102                 :             :     {
     103                 :           0 :       g_autoptr (GFile) file = NULL;
     104                 :             : 
     105                 :           0 :       file = g_file_new_for_path (path);
     106         [ #  # ]:           0 :       gtk_file_dialog_set_initial_folder (dialog, file);
     107                 :             :     }
     108                 :             : 
     109                 :           0 :   gtk_file_dialog_select_folder (dialog,
     110                 :           0 :                                  GTK_WINDOW (gtk_widget_get_root (widget)),
     111                 :             :                                  NULL,
     112                 :             :                                  (GAsyncReadyCallback)gtk_file_dialog_select_folder_cb,
     113                 :             :                                  self);
     114                 :           0 : }
     115                 :             : 
     116                 :             : /*
     117                 :             :  * GObject
     118                 :             :  */
     119                 :             : static void
     120                 :           1 : valent_share_preferences_constructed (GObject *object)
     121                 :             : {
     122                 :           1 :   ValentSharePreferences *self = VALENT_SHARE_PREFERENCES (object);
     123                 :           1 :   ValentDevicePreferencesGroup *group = VALENT_DEVICE_PREFERENCES_GROUP (self);
     124                 :           2 :   g_autofree char *download_folder = NULL;
     125                 :           1 :   GSettings *settings;
     126                 :             : 
     127                 :           1 :   settings = valent_device_preferences_group_get_settings (group);
     128                 :           1 :   download_folder = g_settings_get_string (settings, "download-folder");
     129                 :             : 
     130   [ +  -  +  - ]:           1 :   if (download_folder == NULL || *download_folder == '\0')
     131                 :             :     {
     132                 :           1 :       const char *user_download = NULL;
     133                 :             : 
     134                 :           1 :       user_download = valent_get_user_directory (G_USER_DIRECTORY_DOWNLOAD);
     135                 :           1 :       g_set_str (&download_folder, user_download);
     136                 :           1 :       g_settings_set_string (settings, "download-folder", download_folder);
     137                 :             :     }
     138                 :             : 
     139                 :           1 :   g_settings_bind_with_mapping (settings,                    "download-folder",
     140                 :           1 :                                 self->download_folder_label, "label",
     141                 :             :                                 G_SETTINGS_BIND_GET,
     142                 :             :                                 on_download_folder_changed,
     143                 :             :                                 NULL,
     144                 :             :                                 NULL, NULL);
     145                 :             : 
     146                 :           1 :   G_OBJECT_CLASS (valent_share_preferences_parent_class)->constructed (object);
     147                 :           1 : }
     148                 :             : 
     149                 :             : static void
     150                 :           1 : valent_share_preferences_dispose (GObject *object)
     151                 :             : {
     152                 :           1 :   GtkWidget *widget = GTK_WIDGET (object);
     153                 :             : 
     154                 :           1 :   gtk_widget_dispose_template (widget, VALENT_TYPE_SHARE_PREFERENCES);
     155                 :             : 
     156                 :           1 :   G_OBJECT_CLASS (valent_share_preferences_parent_class)->dispose (object);
     157                 :           1 : }
     158                 :             : 
     159                 :             : static void
     160                 :           6 : valent_share_preferences_class_init (ValentSharePreferencesClass *klass)
     161                 :             : {
     162                 :           6 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     163                 :           6 :   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
     164                 :             : 
     165                 :           6 :   object_class->constructed = valent_share_preferences_constructed;
     166                 :           6 :   object_class->dispose = valent_share_preferences_dispose;
     167                 :             : 
     168                 :           6 :   gtk_widget_class_set_template_from_resource (widget_class, "/plugins/share/valent-share-preferences.ui");
     169                 :           6 :   gtk_widget_class_bind_template_child (widget_class, ValentSharePreferences, download_folder_label);
     170                 :           6 :   gtk_widget_class_install_action (widget_class, "preferences.select-download-folder", NULL, select_download_folder_action);
     171                 :           6 : }
     172                 :             : 
     173                 :             : static void
     174                 :           1 : valent_share_preferences_init (ValentSharePreferences *self)
     175                 :             : {
     176                 :           1 :   gtk_widget_init_template (GTK_WIDGET (self));
     177                 :           1 : }
     178                 :             : 
        

Generated by: LCOV version 2.0-1