/** * thunar_file_is_in_dot_hidden_file: * @file : a #ThunarFile instance. * * Checks whether @file name is contained in a .hidden * file in the same directory as @file. * * Return value: %TRUE if .hidden contains @file name, else %FALSE. **/ gboolean thunar_file_is_in_dot_hidden_file (const ThunarFile *file) { /* name of the last folder used when calling this function */ static gchar *last_parent = NULL; /* searchable contents of .hidden file */ static GHashTable *hidden_hash = NULL; /* basename of file */ gchar *basename = thunar_vfs_path_get_name (THUNAR_FILE ((file))->info->path); /* name of folder containing the file */ gchar *parent = NULL; /* get parent of file */ if (thunar_file_has_parent (THUNAR_FILE ((file)))) { parent = thunar_vfs_path_dup_string (thunar_vfs_path_get_parent (THUNAR_FILE ((file))->info->path)); } else { ThunarVfsPath *parent_path = thunar_vfs_path_get_for_root (); parent = thunar_vfs_path_dup_string (parent_path); thunar_vfs_path_unref (parent_path); } /* reread .hidden file when changing to new directory */ /* TODO: should also reread if .hidden file changes */ if ( g_strcmp0 (last_parent, parent) != 0 ) { gchar *hidden_file = g_build_filename (parent, ".hidden", NULL); g_hash_table_destroy (hidden_hash); hidden_hash = NULL; g_free (last_parent); last_parent = g_strdup (parent); /* If there is a .hidden file in the parent folder, read contents * into hash file */ if (g_file_test (hidden_file, G_FILE_TEST_IS_REGULAR)) { FILE *read = fopen (hidden_file, "r"); const int max_line_length = 255; g_char *line[max_line_length]; hidden_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); while (fgets (line, max_line_length, read)) { g_char *insert; /* remove trailing newlines */ int last = strlen (line)-1; if (line [last] == '\n') { line [last] = '\0'; } /* memory allocated for insert will be freed when hidden_hash is destroyed */ insert = g_strdup (line); g_hash_table_insert (hidden_hash, insert, insert); } fclose (read); } g_free (hidden_file); } g_free (parent); return ( hidden_hash != NULL) && ( g_hash_table_lookup (hidden_hash, basename ) ); }