summaryrefslogtreecommitdiff
path: root/src/libaudcore/art-search.cc
blob: 0fe71e9b27ce5cf999075293fe3c5537f9e55ed4 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * art-search.c
 * Copyright 2006-2013 Michael Hanselmann, Yoshiki Yazawa, and John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "internal.h"

#include <string.h>

#include <glib.h>  /* for g_dir_open, g_file_test */

#include "audstrings.h"
#include "index.h"
#include "runtime.h"

struct SearchParams {
    String filename;
    Index<String> include, exclude;
};

static bool has_front_cover_extension (const char * name)
{
    const char * ext = strrchr (name, '.');
    if (! ext)
        return false;

    return ! strcmp_nocase (ext, ".jpg") || ! strcmp_nocase (ext, ".jpeg") ||
     ! strcmp_nocase (ext, ".png");
}

static bool cover_name_filter (const char * name,
 const Index<String> & keywords, bool ret_on_empty)
{
    if (! keywords.len ())
        return ret_on_empty;

    for (const String & keyword : keywords)
    {
        if (strstr_nocase (name, keyword))
            return true;
    }

    return false;
}

static String fileinfo_recursive_get_image (const char * path,
 const SearchParams * params, int depth)
{
    GDir * d = g_dir_open (path, 0, nullptr);
    if (! d)
        return String ();

    const char * name;

    if (aud_get_bool (nullptr, "use_file_cover") && ! depth)
    {
        /* Look for images matching file name */
        while ((name = g_dir_read_name (d)))
        {
            StringBuf newpath = filename_build ({path, name});

            if (! g_file_test (newpath, G_FILE_TEST_IS_DIR) &&
             has_front_cover_extension (name) &&
             same_basename (name, params->filename))
            {
                g_dir_close (d);
                return String (newpath);
            }
        }

        g_dir_rewind (d);
    }

    /* Search for files using filter */
    while ((name = g_dir_read_name (d)))
    {
        StringBuf newpath = filename_build ({path, name});

        if (! g_file_test (newpath, G_FILE_TEST_IS_DIR) &&
         has_front_cover_extension (name) &&
         cover_name_filter (name, params->include, true) &&
         ! cover_name_filter (name, params->exclude, false))
        {
            g_dir_close (d);
            return String (newpath);
        }
    }

    g_dir_rewind (d);

    if (aud_get_bool (nullptr, "recurse_for_cover") && depth < aud_get_int (nullptr, "recurse_for_cover_depth"))
    {
        /* Descend into directories recursively. */
        while ((name = g_dir_read_name (d)))
        {
            StringBuf newpath = filename_build ({path, name});

            if (g_file_test (newpath, G_FILE_TEST_IS_DIR))
            {
                String tmp = fileinfo_recursive_get_image (newpath, params, depth + 1);

                if (tmp)
                {
                    g_dir_close (d);
                    return tmp;
                }
            }
        }
    }

    g_dir_close (d);
    return String ();
}

String art_search (const char * filename)
{
    StringBuf local = uri_to_filename (filename);
    if (! local)
        return String ();

    const char * elem = last_path_element (local);
    if (! elem)
        return String ();

    String include = aud_get_str (nullptr, "cover_name_include");
    String exclude = aud_get_str (nullptr, "cover_name_exclude");

    SearchParams params = {
        String (elem),
        str_list_to_index (include, ", "),
        str_list_to_index (exclude, ", ")
    };

    cut_path_element (local, elem - local);

    String image_local = fileinfo_recursive_get_image (local, & params, 0);
    return image_local ? String (filename_to_uri (image_local)) : String ();
}