1// Aseprite
2// Copyright (C) 2019-2021 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_RESOURCE_FINDER_H_INCLUDED
9#define APP_RESOURCE_FINDER_H_INCLUDED
10#pragma once
11
12#include "base/disable_copying.h"
13#include "base/paths.h"
14
15#include <atomic>
16#include <string>
17
18namespace app {
19
20 // Helper class to find configuration files in different directories
21 // in a priority order (e.g. first in the $HOME directory, then in
22 // data/ directory, etc.).
23 class ResourceFinder {
24 public:
25 ResourceFinder(bool log = true);
26
27 // Returns the current possible path. You cannot call this
28 // function if you haven't call first() or next() before.
29 const std::string& filename() const;
30 const std::string& defaultFilename() const;
31
32 // Goes to next possible path.
33 bool next();
34
35 // Iterates over all possible paths and returns true if the file
36 // is exists. Returns the first existent file.
37 bool findFirst();
38
39 // These functions add possible full paths to find files.
40 void addPath(const std::string& path);
41 void includeBinDir(const char* filename);
42 void includeDataDir(const char* filename);
43 void includeHomeDir(const char* filename);
44
45#if !defined(_WIN32) && !defined(__APPLE__)
46 // For Linux: It's $XDG_CONFIG_HOME or $HOME/.config
47 void includeHomeConfigDir(const char* filename);
48#endif
49
50 // Tries to add the given filename in these locations:
51 // For Windows:
52 // - If ASEPRITE_USER_FOLDER environment variable is defined, it
53 // should point the folder where the "user dir" is (it's useful
54 // for testing purposes to test with an empty preferences
55 // folder)
56 // - If the app is running in portable mode, the filename
57 // will be in the same location as the .exe file.
58 // - If the app is installed, the filename will be inside
59 // %AppData% location
60 // For Unix-like platforms:
61 // - The filename will be in $HOME/.config/aseprite/
62 void includeUserDir(const char* filename);
63
64 void includeDesktopDir(const char* filename);
65
66 // Returns the first file found or creates the whole directory
67 // structure to create the file in its default location.
68 std::string getFirstOrCreateDefault();
69
70 private:
71 bool m_log;
72 base::paths m_paths;
73 std::atomic<int> m_current;
74 std::string m_default;
75
76 DISABLE_COPYING(ResourceFinder);
77 };
78
79} // namespace app
80
81#endif
82