initial commit

This commit is contained in:
itwrx
2023-06-25 16:34:23 -05:00
commit 188d6d1a3e
13 changed files with 923 additions and 0 deletions

42
views/app_menu_button.nim Normal file
View File

@@ -0,0 +1,42 @@
#[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 owlkettle
import ../models/routine
viewable AppMenuButton:
## A button that opens the main menu of the application
routineModel: RoutineModel
method view(button: AppMenuButtonState): Widget {.locks: "unknown".} =
result = gui:
MenuButton:
icon = "open-menu-symbolic"
# A menu is created using the PopoverMenu widget.
# It allows us to create menus & submenus.
PopoverMenu:
Box:
orient = OrientY
Box(orient = OrientX, spacing=6, margin=6) {.expand: true.}:
Button:
icon = "help-about-symbolic"
style = [ButtonSuggested]
proc clicked() =
discard button.app.open: gui:
AboutDialog:
programName = "EZ-Bkup"
logo = "EZ-Bkup-icon"
style = [StyleClass("about-dialog")]
version = "0.9.0"
credits = @{
"Created By": @["https://ITwrx.org"],
"License": @["GPLv3"]
}
Label:
text = "About EZ-Bkup"
export AppMenuButton

View File

@@ -0,0 +1,41 @@
#[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 owlkettle
import ../models/routine
import routine_editor
type EditRoutineDialogMode* = enum
EditRoutineCreate = "Create"
EditRoutineUpdate = "Update"
viewable EditRoutineDialog:
## A dialog for editing a routine. We use the same dialog for creating and updating
## a routine. Since we want to use different titles and labels for the buttons in each case,
## the EditRoutineDialog.mode field specifies the purpose of the dialog.
routine: Routine ## The routine being edited
mode: EditRoutineDialogMode ## Purpose of the dialog (create/update)
method view(dialog: EditRoutineDialogState): Widget {.locks: "unknown".} =
result = gui:
Dialog:
title = $dialog.mode & " Routine"
defaultSize = (800, 600)
DialogButton {.addButton.}:
# Create / Update Button
text = $dialog.mode
style = [ButtonSuggested]
res = DialogAccept
DialogButton {.addButton.}:
text = "Cancel"
res = DialogCancel
# Content
RoutineEditor:
routine = dialog.routine
proc changed(routine: Routine) =
dialog.routine = routine
export EditRoutineDialog, EditRoutineDialogState

221
views/routine_editor.nim Normal file
View File

@@ -0,0 +1,221 @@
#[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 owlkettle
import "../models/routine", "../shared"
viewable RoutineEditor:
## A form for editing a Routine
routine: Routine ## The routine that is being edited
# Since Routine is passed by value, we need to notify the parent widget of
# any changes to the routine. The changed callback
proc changed(routine: Routine)
method view(editor: RoutineEditorState): Widget {.locks: "unknown".} =
result = gui:
#must have top level container that everything else is inside of.
Box:
orient = OrientY
margin = 12
spacing = 12
Box {.expand: false.}:
orient = OrientX
spacing = 6
Label {.expand: false.}:
text = "<span weight=\"bold\">Name:</span>"
xAlign = 0
useMarkup = true
Entry {.expand: false.}:
text = editor.routine.name
xAlign = 0
proc changed(text: string) =
editor.routine.name = text
# Call the changed callback
if not editor.changed.isNil:
editor.changed.callback(editor.routine)
Box {.expand: false.}:
orient = OrientX
spacing = 6
Label {.expand: false.}:
#later, this may be "selected by default?", instead.
#"selected" checkboxes wiould be added to the list view.
#to allow the user to select which routines are used for a given run.
text = "<span weight=\"bold\">Enabled?</span>"
xAlign = 0
useMarkup = true
Switch {.expand: false.}:
state = editor.routine.selByDef
proc changed(state: bool) =
editor.routine.selByDef = state
if not editor.changed.isNil:
editor.changed.callback(editor.routine)
Label {.expand: false.}:
text = "<span weight=\"bold\">Sources:</span>"
xAlign = 0
useMarkup = true
Box {.expand: false.}:
spacing = 6
Button {.expand: false.}:
text = "+ File"
style = [ButtonSuggested]
proc clicked() =
let (res, state) = editor.app.open: gui:
FileChooserDialog:
title = "Add Source Files to Routine"
action = FileChooserOpen
selectMultiple = true
DialogButton {.addButton.}:
text = "Cancel"
res = DialogCancel
DialogButton {.addButton.}:
text = "Confirm"
res = DialogAccept
style = [ButtonSuggested]
if res.kind == DialogAccept:
for filename in FileChooserDialogState(state).filenames:
if hasCommas(filename):
discard editor.app.open: gui:
MessageDialog:
message = "Commas in Source filename/path are not supported."
style = [StyleClass("error")]
DialogButton {.addButton.}:
text = "Ok"
res = DialogAccept
elif filename in editor.routine.sources:
discard editor.app.open: gui:
MessageDialog:
message = "Duplicate Routine Source filenames (" & filename & ") are not supported."
style = [StyleClass("error")]
DialogButton {.addButton.}:
text = "Ok"
res = DialogAccept
else:
editor.routine.sources.add(filename)
if not editor.changed.isNil:
editor.changed.callback(editor.routine)
Button {.expand: false.}:
text = "+ Folder"
style = [ButtonSuggested]
proc clicked() =
let (res, state) = editor.app.open: gui:
FileChooserDialog:
title = "Add Source Folders to Routine"
action = FileChooserSelectFolder
selectMultiple = true
DialogButton {.addButton.}:
text = "Cancel"
res = DialogCancel
DialogButton {.addButton.}:
text = "Confirm"
res = DialogAccept
style = [ButtonSuggested]
if res.kind == DialogAccept:
for filename in FileChooserDialogState(state).filenames:
if hasCommas(filename):
discard editor.app.open: gui:
MessageDialog:
message = "Commas in Source filename/path are not supported."
style = [StyleClass("error")]
DialogButton {.addButton.}:
text = "Ok"
res = DialogAccept
elif filename in editor.routine.sources:
discard editor.app.open: gui:
MessageDialog:
message = "Duplicate Routine Source (" & filename & ") not supported."
style = [StyleClass("error")]
DialogButton {.addButton.}:
text = "Ok"
res = DialogAccept
else:
editor.routine.sources.add(filename)
if not editor.changed.isNil:
editor.changed.callback(editor.routine)
Box {.expand: false.}:
orient = OrientY
margin = 6
spacing = 6
#routineSeq returns an empty seq when no sources exist.
if editor.routine.sources.len != 0:
for it, routineSource in editor.routine.sources:
Box:
orient = OrientX
spacing = 6
Label:
text = routineSource
xAlign = 0
Button {.expand: false.}:
icon = "user-trash-symbolic"
proc clicked() =
editor.routine.sources.delete(it)
if not editor.changed.isNil:
editor.changed.callback(editor.routine)
Label {.expand: false.}:
text = "<span weight=\"bold\">Destinations:</span>"
xAlign = 0
useMarkup = true
Box {.expand: false.}:
spacing = 6
Button {.expand: false.}:
text = "+ Folder"
style = [ButtonSuggested]
proc clicked() =
let (res, state) = editor.app.open: gui:
FileChooserDialog:
title = "Add Destination Folders to Routine"
action = FileChooserSelectFolder
selectMultiple = true
DialogButton {.addButton.}:
text = "Cancel"
res = DialogCancel
DialogButton {.addButton.}:
text = "Confirm"
res = DialogAccept
style = [ButtonSuggested]
if res.kind == DialogAccept:
for filename in FileChooserDialogState(state).filenames:
if hasCommas(filename):
discard editor.app.open: gui:
MessageDialog:
message = "Commas in Destination filename/path are not supported."
style = [StyleClass("error")]
DialogButton {.addButton.}:
text = "Ok"
res = DialogAccept
elif filename in editor.routine.destinations:
discard editor.app.open: gui:
MessageDialog:
message = "Duplicate Routine Destination (" & filename & ") not supported."
style = [StyleClass("error")]
DialogButton {.addButton.}:
text = "Ok"
res = DialogAccept
else:
editor.routine.destinations.add(filename)
if not editor.changed.isNil:
editor.changed.callback(editor.routine)
Box {.expand: false.}:
orient = OrientY
margin = 6
spacing = 6
#routineSeq returns an empty seq when no destinations exist.
if editor.routine.destinations.len != 0:
for it, routineDestination in editor.routine.destinations:
Box:
orient = OrientX
spacing = 6
Label:
text = routineDestination
xAlign = 0
Button {.expand: false.}:
icon = "user-trash-symbolic"
proc clicked() =
editor.routine.destinations.delete(it)
if not editor.changed.isNil:
editor.changed.callback(editor.routine)
export RoutineEditor

219
views/routine_list.nim Normal file
View File

@@ -0,0 +1,219 @@
#[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 owlkettle
import std/osproc
import edit_routine_dialog
import "../models/routine", "../shared"
##preload selectedRoutines using routines' selByDef field value from DB.
#var selectedRoutines: seq[SelectedRoutine]
viewable RoutineList:
## Displays a list of routines
#filter: string ## Filter used to search for routines
routineModel: RoutineModel ## Model of all routines
#selectedRoutines: selectedRoutines
#selectedRoutines: seq[SelectedRoutine]
runStatus: string
#proc changed(currentRun: CurrentRun)
proc changed(state: bool)
method view(list: RoutineListState): Widget {.locks: "unknown".} =
result = gui:
ScrolledWindow:
Box:
orient = OrientY
if list.routineModel.routineSeq().len() > 0:
ListBox:
#for routine in list.model.search(list.filter):
for it, routine in list.routineModel.routineSeq():
Box:
orient = OrientY
margin = 6
spacing = 6
Box:
orient = OrientX
margin = 6
spacing = 6
#[Switch {.expand: false.}:
state = routine.selByDef
proc changed(state: bool) =
#if state == true:
#list.selectedRoutines.ids.add(routine.id)
if not list.changed.isNil:
list.changed.callback(list.selectedRoutines) ]#
#[CheckButton {.expand: false.}:
state = routine.selected
#if routine.selected == true:
#echo list.currentRun.selected
#list.currentRun.selected[it] = true
#state = t
#state = list.currentRun.selectedRoutines[it]
proc changed(state: bool) =
list.currentRun.selectedRoutines.add(routine.id)
if routine.id in list.currentRun.selectedRoutines:
= state
#echo "hehe"
#list.currentRun.selectedRoutines[it] = state ]#
Label:
#text = "<span size=\"large\" weight=\"bold\">" & routine.name & "</span>"
text = "<span size=\"large\">" & routine.name & "</span>"
xAlign = 0 # Align left
useMarkup = true
# Edit Button
Button {.expand: false.}:
#icon = "entity-edit"
icon = "entity-edit-dark-theme"
proc clicked() =
## Opens the EditRoutineDialog for updating the existing routine
let (res, state) = list.app.open: gui:
EditRoutineDialog:
routine = routine
mode = EditRoutineUpdate
if res.kind == DialogAccept:
# The "Update" button was clicked
list.routineModel.update(EditRoutineDialogState(state).routine)
# Delete Button
Button {.expand: false.}:
icon = "user-trash-symbolic"
proc clicked() =
list.routineModel.delete(routine.id)
if routine.selByDef:
Box:
orient = OrientY
margin = 6
spacing = 6
Label:
text = "<span color=\"#6fffa3\">Sources:</span>"
xAlign = 0
useMarkup = true
#routineSeq returns an empty string when no sources exist.
if routine.sources.len != 0:
for it, routineSource in routine.sources:
Box:
orient = OrientX
spacing = 6
margin = 6
Label:
text = routineSource
xAlign = 0
else:
Box:
orient = OrientX
spacing = 6
margin = 6
Label:
text = "<span color=\"#ff6b6b\">Routine will be ignored. Source required.</span>"
xAlign = 0
useMarkup = true
Label:
text = "<span color=\"#6fffa3\">Destinations:</span>"
xAlign = 0
useMarkup = true
#routineSeq returns an empty string when no destinations exist.
if routine.destinations.len != 0:
for it, routineDestination in routine.destinations:
Box:
orient = OrientX
spacing = 6
margin = 6
Label:
text = routineDestination
xAlign = 0
else:
Box:
orient = OrientX
spacing = 6
margin = 6
Label:
text = "<span color=\"#ff6b6b\">Routine will be ignored. Destination required.</span>"
xAlign = 0
useMarkup = true
Box {.expand: false.}:
orient = OrientX
spacing = 6
margin = 6
Label {.expand: false.}:
text = "<span size=\"large\">Status: </span>"
xAlign = 0
useMarkup = true
Label:
if list.runStatus != "":
text = list.runStatus
else:
text = "<span color=\"#6AC9FF\" size=\"large\">Waiting patiently...</span>"
xAlign = 0
margin = 6
useMarkup = true
# Run Button
Button {.expand: false.}:
Box:
orient = OrientX
spacing = 6
margin = 6
Icon:
name = "media-floppy"
pixelSize = 40
margin = 4
Label {.expand: false.}:
text = "<span size=\"large\">Run Bkup!</span>"
xAlign = 0
useMarkup = true
proc clicked() =
list.runStatus = "<span color=\"#FFE97B\" size=\"large\">Running Bkup...</span>"
var rsyncRun: tuple[output: string, exitCode: int]
var rsyncErrors: seq[string]
var routineRunCount = 0
for routine in list.routineModel.routineSeq():
#using this as "selected" for now.
if routine.selByDef == true:
#skip routines that don't have at least one source and one destination.
#list.routineModel.routineSeq() returns an empty seq so len != 0 when empty...
if routine.sources.len != 0 and routine.destinations.len != 0:
routineRuncount += 1
for source in routine.sources:
for destination in routine.destinations:
list.runStatus = "<span color=\"#FFE97B\" size=\"large\">Bkup " & source & " to " & destination & "...</span>"
#try without requiring superuser privs by default.
rsyncRun = execCmdEx("rsync -aq " & source & " " & destination)
if rsyncRun.exitCode != 0:
#handle permission denied error.
if rsyncRun.exitCode == 23:
let rsyncRunCmd = "SUDO_ASKPASS=" & getAskPassPath() & " sudo -A rsync -aq " & source & " " & destination
rsyncRun = execCmdEx(rsyncRunCmd)
if rsyncRun.exitCode != 0:
rsyncErrors.add("EZ-Bkup's rsync process(es) returned error (" & $rsyncRun.output & ") while attempting to back up " & source & " to " & destination)
else:
rsyncErrors.add("EZ-Bkup's rsync process(es) returned error (" & $rsyncRun.output & ") while attempting to back up " & source & " to " & destination)
#makes the "Bkup Complete" msg below wait on the rsyncRun to finish.
if rsyncRun.exitCode == 1 or rsyncRun.exitCode == 0:
if rsyncErrors.len > 0:
list.runStatus = "<span color=\"#ff6b6b\" size=\"large\">Error! Please see the log at ~/.ez-bkup/errors.log</span>"
for err in rsyncErrors:
writeErrorToLog(err)
echo err
elif routineRunCount == 0:
list.runStatus = "<span color=\"#FFA651\" size=\"large\">Meh. No Bkup Routines were run.</span>"
else:
list.runStatus = "<span color=\"#6fffa3\" size=\"large\">Bkup Complete!</span>"
else:
Box {.expand: false.}:
orient = OrientY
margin = 6
spacing = 12
Label:
text = "<span size=\"large\">You don't have any Bkup Routines!!! 🙂</span>"
xAlign = 0
useMarkup = true
Label:
text = "<span size=\"large\">Please click on the + Routine button above to create your first Routine.</span>"
xAlign = 0
useMarkup = true
export RoutineList