claenup Routine.nim and add copyright/license notice

master
itwrx 1 year ago
parent 20f480d253
commit c37891ede9
  1. 49
      models/routine.nim

@ -1,3 +1,8 @@
#[Copyright 2023 ITwrx.
This file is part of EZ-Bkup.
EZ-Bkup is released under the General Public License 3.0.
See COPYING or <https://www.gnu.org/licenses/> for details.]#
import std/[tables, strutils, sugar, hashes, algorithm, os] import std/[tables, strutils, sugar, hashes, algorithm, os]
from std/sequtils import toSeq from std/sequtils import toSeq
import tiny_sqlite import tiny_sqlite
@ -18,13 +23,12 @@ type Routine* = object
id*: RoutineId id*: RoutineId
name*: string name*: string
selected*: bool selected*: bool
#selByDef == selected by default. #selected by default.
selByDef*: bool selByDef*: bool
sources*: seq[string] sources*: seq[string]
destinations*: seq[string] destinations*: seq[string]
proc stringToSeq(commaSepString: string):seq[string] = proc stringToSeq(commaSepString: string):seq[string] =
#return toSeq(commaSepString.split(','))
var newSeq: seq[string] var newSeq: seq[string]
if hasCommas(commaSepString) == true: if hasCommas(commaSepString) == true:
newSeq = toSeq(commaSepString.split(',')) newSeq = toSeq(commaSepString.split(','))
@ -34,7 +38,6 @@ proc stringToSeq(commaSepString: string):seq[string] =
return newSeq return newSeq
proc seqToString(strSeq: seq[string]):string = proc seqToString(strSeq: seq[string]):string =
#return strSeq.join(",")
var newStr: string var newStr: string
if strSeq.len >= 1: if strSeq.len >= 1:
newStr = strSeq.join(",") newStr = strSeq.join(",")
@ -67,11 +70,6 @@ proc locationsExist*(routine: Routine): bool =
else: else:
return true return true
#[proc matches*(routine: Routine, filter: string): bool =
## Checks if the routine matches the given filter.
## This function is used to search the list of routines.
filter.toLowerAscii() in toLowerAscii(routine.name)]#
type RoutineModel* = ref object type RoutineModel* = ref object
## Model for storing all routines. We model this as a ref object, so that changes ## Model for storing all routines. We model this as a ref object, so that changes
## made by any widget are also known to all other widgets that use the model. ## made by any widget are also known to all other widgets that use the model.
@ -83,7 +81,7 @@ proc cmpRoutines(a, b: Routine): int =
cmp(a.name, b.name) cmp(a.name, b.name)
#this is our stand-in for the search proc. returns a seq so we can iterate in routine_list. #returns a seq so we can iterate in routine_list.
proc routineSeq*(model: RoutineModel): seq[Routine] = proc routineSeq*(model: RoutineModel): seq[Routine] =
var routineSeq: seq[Routine] var routineSeq: seq[Routine]
## Returns a seq of all routines. "id" is necessary to loop through table. ## Returns a seq of all routines. "id" is necessary to loop through table.
@ -92,8 +90,6 @@ proc routineSeq*(model: RoutineModel): seq[Routine] =
routineSeq.sort(cmpRoutines, Ascending) routineSeq.sort(cmpRoutines, Ascending)
#echo routineSeq
return routineSeq return routineSeq
let homePath = expandTilde("~") let homePath = expandTilde("~")
@ -141,9 +137,6 @@ proc newRoutineModel*(path: string = homePath & "/.ez-bkup/ez-bkup.sqlite"): Rou
destinationsSeq = stringToSeq(destinations) destinationsSeq = stringToSeq(destinations)
routines[id] = Routine(id: id, name: name, selByDef: selByDef, sources: sourcesSeq, destinations: destinationsSeq) routines[id] = Routine(id: id, name: name, selByDef: selByDef, sources: sourcesSeq, destinations: destinationsSeq)
#echo "newRoutineModel:"
#echo routines
result = RoutineModel(db: db, routines: routines) result = RoutineModel(db: db, routines: routines)
@ -184,14 +177,6 @@ proc update*(model: RoutineModel, routine: Routine) =
# Update RoutineModel.routines # Update RoutineModel.routines
model.routines[routine.id] = routine model.routines[routine.id] = routine
#[proc search*(model: UserModel, filter: string): seq[User] {.locks: 0.} =
## Returns a seq of all users that match the given filter.
for id, user in model.users:
if user.matches(filter):
result.add(user)
result.sort((a, b: User) => cmp(a.lastName, b.lastName))]#
proc delete*(model: RoutineModel, id: RoutineId) = proc delete*(model: RoutineModel, id: RoutineId) =
## Deletes the routine with the given ID ## Deletes the routine with the given ID
@ -199,22 +184,4 @@ proc delete*(model: RoutineModel, id: RoutineId) =
model.db.exec("DELETE FROM Routine WHERE id = ?", id) model.db.exec("DELETE FROM Routine WHERE id = ?", id)
# Update RoutineModel.routines # Update RoutineModel.routines
model.routines.del(id) model.routines.del(id)
proc clear*(model: RoutineModel) =
## Deletes all routines
# Delete all routines from database
model.db.exec("DELETE FROM Routine")
# Update RoutineModel.routines
model.routines.clear()
#preload the selected seq with routines that have selByDef == true.
proc selectedPreload*(): seq[RoutineId] =
var selected: seq[RoutineId]
let model = newRoutineModel(databasePath)
for routine in model.routineSeq():
if routine.selByDef == true:
selected.add(routine.id)
return selected
Loading…
Cancel
Save