rat-times/scripts/time_sheet.gd

88 lines
2.3 KiB
GDScript3
Raw Normal View History

2023-03-03 21:12:54 +00:00
class_name TimeSheet
var source_path := ""
2023-03-09 20:26:57 +00:00
var entries := []
2023-03-09 23:25:50 +00:00
var tree := TimeEntryTreeItem.new()
2023-03-09 20:26:57 +00:00
2023-03-03 21:12:54 +00:00
## Loads the data file
func load_file() -> bool:
2023-03-09 20:26:57 +00:00
var file := File.new()
var success := file.open(source_path, File.READ)
if success != OK:
success = file.open(source_path, File.WRITE)
if success != OK:
2023-03-03 21:12:54 +00:00
printerr("Failed to open file %s"%[ProjectSettings.globalize_path(source_path)])
return false
return true
2023-03-09 23:25:50 +00:00
2023-03-03 21:12:54 +00:00
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)
2023-03-09 23:25:50 +00:00
# warning-ignore:return_value_discarded
entry.connect("closed", self, "save")
2023-03-03 21:12:54 +00:00
file.close()
2023-03-09 23:25:50 +00:00
entries.sort_custom(self, "_sort_entries")
for entry_index in entries.size():
var entry: TimeEntry = entries[entry_index]
var parts: PoolStringArray = entry.name.split("/")
var repo: TimeEntryTreeItem = tree.get_child(parts, true)
repo.append(entry)
2023-03-03 21:12:54 +00:00
return true
2023-03-09 23:25:50 +00:00
func _sort_entries(a: TimeEntry, b: TimeEntry) -> bool:
return a.name < b.name
2023-03-03 21:12:54 +00:00
## Adds a new time entry to the tree and to the data file
func start_entry(entry_name: String) -> void:
2023-03-09 23:25:50 +00:00
var current_entry := TimeEntry.new().start_recording()
2023-03-03 21:12:54 +00:00
current_entry.name = entry_name
current_entry.closed = false
2023-03-09 20:26:57 +00:00
var file := File.new()
var success := file.open(source_path, File.READ_WRITE)
if success != OK:
2023-03-03 21:12:54 +00:00
printerr("Could not open file")
return
2023-03-03 21:12:54 +00:00
entries.append(current_entry)
file.store_csv_line(current_entry.to_csv_line())
2023-03-09 20:26:57 +00:00
emit_signal("entry_started")
2023-03-03 21:12:54 +00:00
func update() -> void:
2023-03-09 23:25:50 +00:00
for entry in entries:
var time_entry := entry as TimeEntry
if time_entry.is_closed == false:
time_entry.update()
2023-03-03 21:12:54 +00:00
func save() -> void:
2023-03-09 20:26:57 +00:00
var file := File.new()
var success := file.open(source_path, File.WRITE)
if success != OK:
2023-03-03 21:12:54 +00:00
printerr("Could not open file")
return
2023-03-03 21:12:54 +00:00
for time_entry in entries:
file.store_csv_line(time_entry.to_csv_line())
static func restore(file_path: String) -> TimeSheet:
2023-03-09 20:26:57 +00:00
var timesheet = load("res://scripts/time_sheet.gd").new()
2023-03-03 21:12:54 +00:00
timesheet.source_path = file_path
2023-03-09 20:26:57 +00:00
var success: bool = timesheet.load_file()
2023-03-03 21:12:54 +00:00
if success:
return timesheet
return null