1# Copyright 2021 The GNOME Music developers
2#
3# GNOME Music is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# GNOME Music is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16#
17# The GNOME Music authors hereby grant permission for non-GPL compatible
18# GStreamer plugins to be used and distributed together with GStreamer
19# and GNOME Music.  This permission is above and beyond the permissions
20# granted by the GPL license by which GNOME Music is covered.  If you
21# modify this code, you may extend this exception to your version of the
22# code, but you are not obligated to do so.  If you do not wish to do so,
23# delete this exception statement from your version.
24
25from gi.repository import GLib, GObject, Gio
26
27
28class FileExistsAsync(GObject.GObject):
29    """Gio.File.file_exists async variant
30    """
31
32    __gtype_name__ = "FileExistsAsync"
33
34    __gsignals__ = {
35        "finished": (GObject.SignalFlags.RUN_FIRST, None, (bool, ))
36    }
37
38    def __init__(self) -> None:
39        """Initialize FileExistsAsync
40        """
41        super().__init__()
42
43    def start(self, thumb_file: Gio.File) -> None:
44        """Start async file_exists lookup
45
46        :param Gio.File thumb_file: File to check
47        """
48        thumb_file.query_info_async(
49            Gio.FILE_ATTRIBUTE_STANDARD_TYPE, Gio.FileQueryInfoFlags.NONE,
50            GLib.PRIORITY_DEFAULT, None, self._on_query_info_finished)
51
52    def _on_query_info_finished(
53            self, thumb_file: Gio.File, res: Gio.AsyncResult) -> None:
54        exists = True
55        try:
56            thumb_file.query_info_finish(res)
57        except GLib.Error:
58            # This indicates that the file has not been created, so
59            # there is no art in the MediaArt cache.
60            exists = False
61
62        self.emit("finished", exists)
63