class_name TimeSheet var source_path := "" var entries: Array[TimeEntry] = [] var entries_names := {} var current_entry: TimeEntry ## Loads the data file func load_file() -> bool: var file := FileAccess.open(source_path, FileAccess.READ) if file == null: file = FileAccess.open(source_path, FileAccess.WRITE) if file == null: printerr("Failed to open file %s"%[ProjectSettings.globalize_path(source_path)]) return false return true while not file.eof_reached(): var line := file.get_csv_line() if line.size() == 0 or "".join(line).length() == 0: continue if not TimeEntry.is_csv_line_valid(line): push_warning("CSV Line `%s` is not conform"%[",".join(line)]) continue var entry := TimeEntry.new().from_csv_line(line) entries.append(entry) if entry.closed == false: current_entry = entry if not entries_names.has(entry.name): entries_names[entry.name] = 0 entries_names[entry.name] += entry.get_elapsed_seconds() file.close() return true ## Adds a new time entry to the tree and to the data file func start_entry(entry_name: String) -> void: current_entry = TimeEntry.new().start_recording() current_entry.name = entry_name current_entry.closed = false if entry_name in entries_names: current_entry.previous_total = entries_names[entry_name] var file := FileAccess.open(source_path, FileAccess.READ_WRITE) if file == null: printerr("Could not open file") entries.append(current_entry) file.store_csv_line(current_entry.to_csv_line()) func update() -> void: current_entry.update() entries_names[current_entry.name] = current_entry.get_total_elapsed_seconds() func close_entry() -> void: current_entry.closed = true save() func get_period() -> String: return current_entry.get_period() func get_total_elapsed_seconds() -> int: return current_entry.get_total_elapsed_seconds() func save() -> void: var file := FileAccess.open(source_path, FileAccess.WRITE) if file == null: printerr("Could not open file") for time_entry in entries: file.store_csv_line(time_entry.to_csv_line()) static func restore(file_path: String) -> TimeSheet: var timesheet := TimeSheet.new() timesheet.source_path = file_path var success := timesheet.load_file() if success: return timesheet return null