|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + * SPDX-FileCopyrightText: 2026 elementary, Inc. <https://elementary.io> |
| 4 | + * |
| 5 | + * Authored by: Jeremy Wootten <jeremywootten@gmail.com> |
| 6 | + */ |
| 7 | + |
| 8 | +// Using static methods unless we find we require singleton |
| 9 | +// We keep a separate list of project infos for future use in e.g. recently closed project list |
| 10 | +// and for re-opening projects without recreating info object |
| 11 | +public class Scratch.FolderManager.ProjectInfoManager : Object { |
| 12 | + private class ProjectInfo : Object { |
| 13 | + const string PROJECT_INFO_SCHEMA_ID = "io.elementary.code.Projects"; |
| 14 | + const string PROJECT_INFO_SCHEMA_PATH_PREFIX = "/io/elementary/code/Projects/"; |
| 15 | + public string path { |
| 16 | + owned get { |
| 17 | + return project.path; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public ProjectFolderItem project { get; construct; } |
| 22 | + private Settings settings; |
| 23 | + |
| 24 | + public ProjectInfo (ProjectFolderItem project) { |
| 25 | + Object ( |
| 26 | + project: project |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + construct { |
| 31 | + var settings_path = PROJECT_INFO_SCHEMA_PATH_PREFIX + |
| 32 | + schema_name_from_path (path) + |
| 33 | + Path.DIR_SEPARATOR_S; |
| 34 | + |
| 35 | + settings = new Settings.with_path ( |
| 36 | + PROJECT_INFO_SCHEMA_ID, |
| 37 | + settings_path |
| 38 | + ); |
| 39 | + |
| 40 | + settings.bind ("expanded", project, "expanded", DEFAULT); |
| 41 | + } |
| 42 | + |
| 43 | + //Combine basename and parent folder name and convert to camelcase |
| 44 | + private string schema_name_from_path (string path) { |
| 45 | + var dir = Path.get_basename (Path.get_dirname (path)).normalize (); |
| 46 | + var basename = Path.get_basename (path).normalize (); |
| 47 | + var name = dir.substring (0, 1).up () + |
| 48 | + dir.substring (1, -1).down () + |
| 49 | + basename.substring (0, 1).up () + |
| 50 | + basename.substring (1, -1).down (); |
| 51 | + |
| 52 | + return name; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static Gee.HashMap<string, ProjectInfo>? map = null; |
| 57 | + private static Gee.HashMap<string, ProjectInfo> project_info_map { |
| 58 | + get { |
| 59 | + if (map == null) { |
| 60 | + map = new Gee.HashMap<string, ProjectInfo> (); |
| 61 | + } |
| 62 | + |
| 63 | + return map; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + //Called when folder created |
| 68 | + public static void get_project_info (ProjectFolderItem project_folder) { |
| 69 | + var info = project_info_map[project_folder.path]; |
| 70 | + if (info == null) { |
| 71 | + info = new ProjectInfo (project_folder); |
| 72 | + project_info_map[project_folder.path] = info; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments