1// Aseprite
2// Copyright (C) 2019-2020 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/res/palettes_loader_delegate.h"
13
14#include "app/app.h"
15#include "app/extensions.h"
16#include "app/file/palette_file.h"
17#include "app/file_system.h"
18#include "app/res/palette_resource.h"
19#include "app/resource_finder.h"
20#include "base/fs.h"
21#include "base/scoped_value.h"
22#include "doc/palette.h"
23#include "ui/system.h"
24
25namespace app {
26
27PalettesLoaderDelegate::PalettesLoaderDelegate()
28{
29 // Necessary to load preferences in the UI-thread which will be used
30 // in a FileOp executed in a background thread.
31 m_config.fillFromPreferences();
32}
33
34void PalettesLoaderDelegate::getResourcesPaths(std::map<std::string, std::string>& idAndPath) const
35{
36 // Include extension palettes
37 idAndPath = App::instance()->extensions().palettes();
38
39 // Search old palettes too
40 std::string path;
41 ResourceFinder rf;
42 rf.includeDataDir("palettes"); // data/palettes/ in all places
43 rf.includeUserDir("palettes"); // palettes/ in user home
44 while (rf.next()) {
45 if (base::is_directory(rf.filename())) {
46 path = rf.filename();
47 path = base::fix_path_separators(path);
48 for (const auto& fn : base::list_files(path)) {
49 // Ignore the default palette that is inside the palettes/ dir
50 // in the user home dir.
51 if (fn == "default.ase" ||
52 fn == "default.gpl")
53 continue;
54
55 std::string fullFn = base::join_path(path, fn);
56 if (base::is_file(fullFn))
57 idAndPath[base::get_file_title(fn)] = fullFn;
58 }
59 }
60 }
61}
62
63Resource* PalettesLoaderDelegate::loadResource(const std::string& id,
64 const std::string& path)
65{
66 auto palette = load_palette(path.c_str(), &m_config);
67 if (palette)
68 return new PaletteResource(id, path, std::move(palette));
69 else
70 return nullptr;
71}
72
73} // namespace app
74