#!/bin/bash #------------------------------------------------------------------------------- # /var/install/bin/sabredav-user.cui.sh # Copyright (c) 2001-2017 the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #------------------------------------------------------------------------------- . /var/install/include/cuilib . /var/install/include/mysqllib-2 #============================================================================ # global constants #============================================================================ IDC_LISTVIEW='10' # listview ID IDC_HELPTEXT='11' # help text ID IDC_USERDLG_BUTOK='10' # dlg OK button ID IDC_USERDLG_BUTCANCEL='11' # dlg Cancel button ID IDC_USERDLG_LABEL1='12' # dlg label ID IDC_USERDLG_LABEL2='13' # dlg label ID IDC_USERDLG_LABEL3='14' # dlg label ID IDC_USERDLG_LABEL4='15' # dlg label ID IDC_USERDLG_LABEL5='16' # dlg label ID IDC_USERDLG_EDNAME='20' # dlg edit ID IDC_USERDLG_EDEMAIL='21' # dlg edit ID IDC_USERDLG_EDCOMMENT='22' # dlg edit ID IDC_USERDLG_EDPASSWD1='23' # dlg edit ID IDC_USERDLG_EDPASSWD2='24' # dlg edit ID IDC_INPUTDLG_BUTOK='10' # dlg OK button ID IDC_INPUTDLG_BUTCANCEL='11' # dlg Cancel button ID IDC_INPUTDLG_EDVALUE='20' # dlg edit ID selected_entry='' show_help="no" sabredav_sql_server="localhost" sabredav_sql_db_name="sabredav" sabredav_sql_user="sabredav" sabredav_sql_pass="" if [ -e /etc/config.d/sabredav ] then . /etc/config.d/sabredav sabredav_sql_server="$SABREDAV_SQL_HOST" sabredav_sql_db_name="$SABREDAV_SQL_DATABASE" sabredav_sql_user="$SABREDAV_SQL_USER" sabredav_sql_pass="$SABREDAV_SQL_PASS" fi #============================================================================ # helper functions #============================================================================ #---------------------------------------------------------------------------- # check if is a valid list index #---------------------------------------------------------------------------- function p_valid_index() { if [ -n "$1" -a "$1" -ge "0" ] then return 0 fi return 1 } #---------------------------------------------------------------------------- # check if SQL command was successful #---------------------------------------------------------------------------- function p_sql_success() { if [ -n "$1" -a "$1" != "0" ] then return 0 fi return 1 } #---------------------------------------------------------------------------- # read data from a MySQL database and copy result to the listview window #---------------------------------------------------------------------------- function load_data() { local win="$1" local ctrl local myres # execute query and return result cui_window_getctrl "$win" "$IDC_LISTVIEW" && ctrl="$p2" if cui_valid_handle $ctrl then cui_listview_clear "$ctrl" if [ -z "$keyword" ] then my_query_sql "$myconn" \ "SELECT users.username AS username, principals.email AS email, \ principals.displayname AS displayname, users.id AS id \ FROM users LEFT JOIN principals ON CONCAT('principals/', users.username) = principals.uri;" && myres="$p2" else my_query_sql "$myconn" \ "SELECT users.username AS username, principals.email AS email, \ principals.displayname AS displayname, users.id AS id \ FROM users LEFT JOIN principals ON CONCAT('principals/', users.username) = principals.uri \ WHERE users.username REGEXP '$keyword' ;" && myres="$p2" fi if cui_valid_handle "$myres" then my_result_status "$myres" if [ "$p2" == "$SQL_DATA_READY" ] then my_result_tolist "$myres" "$ctrl" "0" "$selected_entry" else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi my_result_free "$myres" fi cui_listview_update "$ctrl" fi } #---------------------------------------------------------------------------- # resize client windows #---------------------------------------------------------------------------- function resize_windows() { local win="$1" local ctrl cui_getclientrect "$win" local w="$p4" local h="$p5" local p="$[$h - $h / 4]" if [ "$show_help" == "yes" ] then cui_window_getctrl "$win" "$IDC_LISTVIEW" && ctrl="$p2" if cui_valid_handle $ctrl then cui_window_move "$ctrl" "0" "0" "$w" "$p" fi cui_window_getctrl "$win" "$IDC_HELPTEXT" && ctrl="$p2" if cui_valid_handle $ctrl then cui_window_move "$ctrl" "0" "$p" "$w" "$[$h -$p]" cui_window_hide "$ctrl" "0" fi else cui_window_getctrl "$win" "$IDC_LISTVIEW" && ctrl="$p2" if cui_valid_handle $ctrl then cui_window_move "$ctrl" "0" "0" "$w" "$h" fi cui_window_getctrl "$win" "$IDC_HELPTEXT" && ctrl="$p2" if cui_valid_handle $ctrl then cui_window_move "$ctrl" "0" "$h" "$w" "2" cui_window_hide "$ctrl" "1" cui_window_getfocus if [ "$p2" == "$ctrl" ] then cui_window_getctrl "$win" "$IDC_LISTVIEW" if cui_valid_handle $p2 then cui_window_setfocus "$p2" fi fi fi fi } #============================================================================ # data input dialog #============================================================================ #---------------------------------------------------------------------------- # inputdlg_ok_clicked # Ok button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function inputdlg_ok_clicked() { local win="$p2" local ctrl local idx cui_window_getctrl "$win" "$IDC_INPUTDLG_EDVALUE" && ctrl="$p2" if cui_valid_handle $ctrl then cui_edit_gettext "$ctrl" inputdlg_value="$p2" fi cui_window_close "$win" "$IDOK" cui_return 1 } #---------------------------------------------------------------------------- # inputdlg_cancel_clicked # Cancel button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function inputdlg_cancel_clicked() { cui_window_close "$p2" "$IDCANCEL" cui_return 1 } #---------------------------------------------------------------------------- # inputdlg_create_hook # Dialog create hook - create dialog controls # expects: $1 : window handle of dialog window # returns: 1 : event handled #---------------------------------------------------------------------------- function inputdlg_create_hook() { local dlg="$p2" local ctrl local idx if cui_label_new "$dlg" "Keyword:" 2 1 14 1 "$IDC_INPUTDLG_LABEL1" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi cui_edit_new "$dlg" "" 17 1 25 8 255 "$IDC_INPUTDLG_EDVALUE" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "$inputdlg_value" fi cui_button_new "$dlg" "&OK" 11 3 10 1 $IDC_INPUTDLG_BUTOK $CWS_DEFOK $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" inputdlg_ok_clicked cui_window_create "$ctrl" fi cui_button_new "$dlg" "&Cancel" 22 3 10 1 $IDC_INPUTDLG_BUTCANCEL $CWS_DEFCANCEL $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" inputdlg_cancel_clicked cui_window_create "$ctrl" fi cui_return 1 } #============================================================================ # user edit/create dialog #============================================================================ #---------------------------------------------------------------------------- # userdlg_ok_clicked # Ok button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function userdlg_ok_clicked() { local win="$p2" local ctrl local idx cui_window_getctrl "$win" "$IDC_USERDLG_EDNAME" && ctrl="$p2" if cui_valid_handle $ctrl then cui_edit_gettext "$ctrl" userdlg_loginname="$p2" fi cui_window_getctrl "$win" "$IDC_USERDLG_EDEMAIL" && ctrl="$p2" if cui_valid_handle $ctrl then cui_edit_gettext "$ctrl" userdlg_email="$p2" fi cui_window_getctrl "$win" "$IDC_USERDLG_EDCOMMENT" && ctrl="$p2" if cui_valid_handle $ctrl then cui_edit_gettext "$ctrl" userdlg_comment="$p2" fi cui_window_getctrl "$win" "$IDC_USERDLG_EDPASSWD1" && ctrl="$p2" if cui_valid_handle $ctrl then cui_edit_gettext "$ctrl" userdlg_passwd1="$p2" fi cui_window_getctrl "$win" "$IDC_USERDLG_EDPASSWD2" && ctrl="$p2" if cui_valid_handle $ctrl then cui_edit_gettext "$ctrl" userdlg_passwd2="$p2" fi if [ -z "$userdlg_loginname" ] then cui_message "$win" "No login name entered! Please enter a valid name" \ "Missing data" "$MB_ERROR" cui_return 1 return fi if [ -z "$userdlg_passwd1" ] then cui_message "$win" "Empty password supplied! Please enter a valid password" \ "Missing data" "$MB_ERROR" cui_return 1 return fi if [ "$userdlg_passwd1" != "$userdlg_passwd2" ] then cui_message "$win" "Passwords do not match. Please reenter passwords." \ "Missing data" "$MB_ERROR" cui_return 1 return fi cui_window_close "$win" "$IDOK" cui_return 1 } #---------------------------------------------------------------------------- # userdlg_cancel_clicked # Cancel button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function userdlg_cancel_clicked() { cui_window_close "$p2" "$IDCANCEL" cui_return 1 } #---------------------------------------------------------------------------- # userdlg_create_hook # Dialog create hook - create dialog controls # expects: $1 : window handle of dialog window # returns: 1 : event handled #---------------------------------------------------------------------------- function userdlg_create_hook() { local dlg="$p2" local ctrl local idx if cui_label_new "$dlg" "Login name:" 2 1 14 1 $IDC_USERDLG_LABEL1 $CWS_NONE $CWS_NONE then cui_window_create "$p2" fi if cui_label_new "$dlg" "E-Mail addr.:" 2 3 14 1 $IDC_USERDLG_LABEL2 $CWS_NONE $CWS_NONE then cui_window_create "$p2" fi if cui_label_new "$dlg" "Comment:" 2 5 14 1 $IDC_USERDLG_LABEL3 $CWS_NONE $CWS_NONE then cui_window_create "$p2" fi if cui_label_new "$dlg" "Password:" 2 7 14 1 $IDC_USERDLG_LABEL4 $CWS_NONE $CWS_NONE then cui_window_create "$p2" fi if cui_label_new "$dlg" "Retype passwd:" 2 9 14 1 $IDC_USERDLG_LABEL5 $CWS_NONE $CWS_NONE then cui_window_create "$p2" fi cui_edit_new "$dlg" "" 17 1 20 1 255 $IDC_USERDLG_EDNAME $CWS_NONE $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "$userdlg_loginname" fi cui_edit_new "$dlg" "" 17 3 30 1 255 "$IDC_USERDLG_EDEMAIL" $CWS_NONE $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "$userdlg_email" fi cui_edit_new "$dlg" "" 17 5 30 1 255 "$IDC_USERDLG_EDCOMMENT" $CWS_NONE $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "$userdlg_comment" fi cui_edit_new "$dlg" "" 17 7 25 1 255 "$IDC_USERDLG_EDPASSWD1" $EF_PASSWORD $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_window_create "$ctrl". cui_edit_settext "$ctrl" "$userdlg_passwd1" fi cui_edit_new "$dlg" "" 17 9 25 1 255 "$IDC_USERDLG_EDPASSWD2" $EF_PASSWORD $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_window_create "$ctrl". cui_edit_settext "$ctrl" "$userdlg_passwd2" fi cui_button_new "$dlg" "&OK" 12 12 10 1 $IDC_USERDLG_BUTOK $CWS_DEFOK $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" userdlg_ok_clicked cui_window_create "$ctrl" fi cui_button_new "$dlg" "&Cancel" 28 12 10 1 $IDC_USERDLG_BUTCANCEL $CWS_DEFCANCEL $CWS_NONE && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" userdlg_cancel_clicked cui_window_create "$ctrl" fi cui_return 1 } #============================================================================ # invoke user dialog due to key or menu selection #============================================================================ #---------------------------------------------------------------------------- # user_createuser_dialog # Create a new caldav & carddav user # returns: 0 : created (reload data) # 1 : not modified (don't reload data) #---------------------------------------------------------------------------- function user_createuser_dialog() { local win="$1" local result="$IDCANCEL" local dlg local myres local ncal=0 userdlg_loginname="" userdlg_email="" userdlg_comment="" userdlg_passwd1="" userdlg_passwd2="" cui_window_new "$win" 0 0 52 16 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED] && dlg="$p2" if cui_valid_handle $dlg then cui_window_setcolors "$dlg" "DIALOG" cui_window_settext "$dlg" "Create user" cui_window_sethook "$dlg" "$HOOK_CREATE" userdlg_create_hook cui_window_create "$dlg" cui_window_modal "$dlg" && result="$p2" if [ "$result" == "$IDOK" ] then cui_window_destroy "$dlg" # Step 1 -> create user with password userdlg_passwd2=$(md5 -q -s "${userdlg_loginname}:SabreDAV:${userdlg_passwd1}" ) my_exec_sql "$myconn" \ "INSERT INTO users (username, digesta1) \ VALUES ('$userdlg_loginname', \ '$userdlg_passwd2');" if p_sql_success "$p2" then # Step 2 -> create principals and email address my_exec_sql "$myconn" \ "INSERT INTO principals (uri, email, displayname) VALUES \ ('principals/$userdlg_loginname', '$userdlg_email','$userdlg_comment'), \ ('principals/${userdlg_loginname}/calendar-proxy-read', null, null), \ ('principals/${userdlg_loginname}/calendar-proxy-write', null, null);" if p_sql_success "$p2" then # Step 3 -> create default calendar my_exec_sql "$myconn" \ "INSERT INTO calendars (synctoken, components) VALUES (1, 'VEVENT,VTODO');" if p_sql_success "$p2" then # get the last id my_query_sql "$myconn" "SELECT id FROM calendars ORDER BY id DESC LIMIT 1 ;" && myres="$p2" if cui_valid_handle "$myres" then my_result_status "$myres" if [ "$p2" == "$SQL_DATA_READY" ] then my_result_fetch "$myres" if p_sql_success "$p2" then my_result_data "$myres" "0" && ncal="$p2" fi else ncal=0 my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi my_result_free "$myres" else ncal=0 my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi if [ $ncal -gt 0 ] then # Step 4 -> create default calendarinstances my_exec_sql "$myconn" \ "INSERT INTO calendarinstances (calendarid, principaluri, displayname, uri, calendarcolor) \ VALUES ( ${ncal} , \ 'principals/$userdlg_loginname', \ 'default', \ 'default', \ '#0E61B9FF' );" if p_sql_success "$p2" then # Step 5 -> create default addressbook my_exec_sql "$myconn" \ "INSERT INTO addressbooks (principaluri, displayname, uri) \ VALUES ( 'principals/$userdlg_loginname', \ 'default', \ 'default' );" if p_sql_success "$p2" then selected_entry="$userdlg_loginname" else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else cui_window_destroy "$dlg" fi fi [ "$result" == "$IDOK" ] return "$?" } #---------------------------------------------------------------------------- # user_edituser_dialog # Edit/Modify user # returns: 0 : created (reload data) # 1 : not modified (don't reload data) #---------------------------------------------------------------------------- function user_edituser_dialog() { local win="$1" local dlg local result="$IDCANCEL" local ctrl local idx local entryname local userdlg_loginold cui_window_getctrl "$win" "$IDC_LISTVIEW" && ctrl="$p2" if cui_valid_handle $ctrl then cui_listview_getsel "$ctrl" && idx="$p2" if p_valid_index $idx then cui_listview_gettext "$ctrl" "$idx" "0" userdlg_loginname="$p2" userdlg_loginold="$userdlg_loginname" cui_listview_gettext "$ctrl" "$idx" "1" userdlg_email="$p2" cui_listview_gettext "$ctrl" "$idx" "2" userdlg_comment="$p2" userdlg_passwd1="xxxxxxxxxxxxxxxx" userdlg_passwd2="xxxxxxxxxxxxxxxx" cui_window_new "$win" 0 0 52 16 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED] && dlg="$p2" if cui_valid_handle $dlg then cui_window_setcolors "$dlg" "DIALOG" cui_window_settext "$dlg" "Edit user" cui_window_sethook "$dlg" "$HOOK_CREATE" userdlg_create_hook cui_window_create "$dlg" cui_window_modal "$dlg" && result="$p2" if [ "$result" == "$IDOK" ] then cui_window_destroy "$dlg" userdlg_passwd2=$(md5 -q -s "${userdlg_loginname}:SabreDAV:${userdlg_passwd1}" ) # step 1 -> update all pricipals if [ "$userdlg_loginold" != "$userdlg_loginname" ] then my_exec_sql "$myconn" \ "UPDATE principals \ SET email='$userdlg_email', \ displayname='$userdlg_comment', \ uri='principals/$userdlg_loginname' \ WHERE uri = 'principals/$userdlg_loginold' ;" if p_sql_success "$p2" then my_exec_sql "$myconn" \ "UPDATE principals \ SET uri='principals/${userdlg_loginname}/calendar-proxy-read' \ WHERE uri = 'principals/${userdlg_loginold}/calendar-proxy-read' ;" my_exec_sql "$myconn" \ "UPDATE principals \ SET uri='principals/${userdlg_loginname}/calendar-proxy-write' \ WHERE uri = 'principals/${userdlg_loginold}/calendar-proxy-write' ;" # step 2 -> update login name if [ "$userdlg_passwd1" != "xxxxxxxxxxxxxxxx" ] then my_exec_sql "$myconn" \ "UPDATE users \ SET username='$userdlg_loginname', \ digesta1='$userdlg_passwd2' \ WHERE username='$userdlg_loginold';" else my_exec_sql "$myconn" \ "UPDATE users \ SET username='$userdlg_loginname' \ WHERE username='$userdlg_loginold';" fi if p_sql_success "$p2" then # step 3 -> update all calendarinstances my_exec_sql "$myconn" \ "UPDATE calendarinstances \ SET principaluri='principals/$userdlg_loginname' \ WHERE principaluri='principals/$userdlg_loginold';" if p_sql_success "$p2" then # step 4 -> update all addressbooks my_exec_sql "$myconn" \ "UPDATE addressbooks \ SET principaluri='principals/$userdlg_loginname' \ WHERE principaluri='principals/$userdlg_loginold';" if p_sql_success "$p2" then selected_entry="$userdlg_loginname" else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_exec_sql "$myconn" \ "UPDATE principals \ SET email='$userdlg_email', \ displayname='$userdlg_comment' \ WHERE uri = 'principals/$userdlg_loginold' ;" if [ "$userdlg_passwd1" != "xxxxxxxxxxxxxxxx" ] then my_exec_sql "$myconn" \ "UPDATE users \ SET digesta1='$userdlg_passwd2' \ WHERE username='$userdlg_loginold';" fi if p_sql_success "$p2" then selected_entry="$userdlg_loginname" else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi fi else cui_window_destroy "$dlg" fi fi else cui_message "$win" "No item selected" "Info" "$MB_OK" fi fi [ "$result" == "$IDOK" ] return "$?" } #---------------------------------------------------------------------------- # user_deleteuser_dialog # Delete a CalDAV or CardDAV user # returns: 0 : created (reload data) # 1 : not modified (don't reload data) #---------------------------------------------------------------------------- function user_deleteuser_dialog() { local win="$1" local result="$IDCANCEL" local ctrl local idx local entryname cui_window_getctrl "$win" "$IDC_LISTVIEW" && ctrl="$p2" if cui_valid_handle $ctrl then cui_listview_getsel "$ctrl" && idx="$p2" if p_valid_index $idx then cui_listview_gettext "$ctrl" "$idx" "0" userdlg_loginname="$p2" cui_message "$win" "Really delete user \"$userdlg_loginname\"?" "Question" "$MB_YESNO" if [ "$p2" == "$IDYES" ] then # step 1 -> delete user my_exec_sql "$myconn" \ "DELETE FROM users \ WHERE username='$userdlg_loginname';" if p_sql_success "$p2" then # step 2 -> delete principals my_exec_sql "$myconn" \ "DELETE FROM principals \ WHERE uri = 'principals/$userdlg_loginname' \ OR uri = 'principals/${userdlg_loginname}/calendar-proxy-read' \ OR uri = 'principals/${userdlg_loginname}/calendar-proxy-write' ;" if p_sql_success "$p2" then selected_entry="" result="$IDOK" else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" fi fi else cui_message "$win" "No item selected" "Info" "$MB_OK" fi fi [ "$result" == "$IDOK" ] return "$?" } #============================================================================ # select menu when hitting "ENTER" or "SPACE" on the list #============================================================================ #---------------------------------------------------------------------------- # menu_clicked_hook # expects: $p2 : window handle # $p3 : control window handle # returns: nothing #---------------------------------------------------------------------------- function menu_clicked_hook() { cui_window_close "$p3" "$IDOK" cui_return 1 } #---------------------------------------------------------------------------- # menu_escape_hook # expects: $p2 : window handle # $p3 : control window handle # returns: nothing #---------------------------------------------------------------------------- function menu_escape_hook() { cui_window_close "$p3" "$IDCANCEL" cui_return 1 } #---------------------------------------------------------------------------- # menu_postkey_hook # expects: $p2 : window handle # $p3 : control window handle # $p4 : key code # returns: 1 : Key handled, 2 : Key ignored #---------------------------------------------------------------------------- function menu_postkey_hook() { local ctrl="$p3" if [ "$p4" == "$KEY_F10" ] then cui_window_close "$ctrl" "$IDCANCEL" cui_window_quit 0 cui_return 1 else cui_return 0 fi } #============================================================================ # listview callbacks #============================================================================ #---------------------------------------------------------------------------- # listview_clicked_hook # listitem has been clicked # expects: $p1 : window handle of parent window # $p2 : control id # returns: 1 : event handled #---------------------------------------------------------------------------- function listview_clicked_hook() { local win="$p2" local ctrl="$p3" local dlg local menu local result local item cui_menu_new "$win" "Options" 0 0 25 13 1 "$[$CWS_CENTERED + $CWS_POPUP]" "$CWS_NONE" && menu="$p2" if cui_valid_handle $menu then cui_menu_additem "$menu" "Edit entry" 1 cui_menu_additem "$menu" "Delete entry" 2 cui_menu_additem "$menu" "Create new entry" 3 cui_menu_addseparator "$menu" cui_menu_additem "$menu" "Search filter" 4 cui_menu_addseparator "$menu" cui_menu_additem "$menu" "Exit application" 5 cui_menu_addseparator "$menu" cui_menu_additem "$menu" "Close menu" 0 cui_menu_selitem "$menu" 1 cui_menu_callback "$menu" "$MENU_CLICKED" "$win" "menu_clicked_hook" cui_menu_callback "$menu" "$MENU_ESCAPE" "$win" "menu_escape_hook" cui_menu_callback "$menu" "$MENU_POSTKEY" "$win" "menu_postkey_hook" cui_window_create "$menu" cui_window_modal "$menu" && result="$p2" if [ "$result" == "$IDOK" ] then cui_menu_getselitem "$menu" && item="$p2" case $item in 1) cui_window_destroy "$menu" if user_edituser_dialog $win then load_data "$win" fi ;; 2) cui_window_destroy "$menu" if user_deleteuser_dialog $win then load_data "$win" fi ;; 3) cui_window_destroy "$menu" if user_createuser_dialog $win then load_data "$win" fi ;; 4) cui_window_destroy "$menu" cui_window_new "$win" 0 0 46 7 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED] && dlg="$p2" if cui_valid_handle $dlg then cui_window_setcolors "$dlg" "DIALOG" cui_window_settext "$dlg" "Search Filter" cui_window_sethook "$dlg" "$HOOK_CREATE" inputdlg_create_hook cui_window_create "$dlg" inputdlg_value="$keyword" cui_window_modal "$dlg" && result="$p2" if [ "$result" == "$IDOK" ] then cui_window_destroy "$dlg" keyword="$inputdlg_value" load_data "$win" else cui_window_destroy "$dlg" fi fi ;; 5) cui_window_destroy "$menu" cui_window_quit 0 ;; *) cui_window_destroy "$menu" ;; esac else cui_window_destroy "$menu" fi fi cui_return 1 } #---------------------------------------------------------------------------- # listview_postkey_hook (catch ENTER key) # $p2 --> window handle of main window # $p3 --> window handle of list control # $p4 --> key #---------------------------------------------------------------------------- function listview_postkey_hook() { local win="$p2" local ctrl="$p3" local key="$p4" if [ "$key" == "$KEY_ENTER" ] then listview_clicked_hook "$win" "$ctrl" else cui_return 0 fi } #============================================================================ # main window hooks #============================================================================ #---------------------------------------------------------------------------- # mainwin_create_hook (for creation of child windows) # $p2 --> mainwin window handle #---------------------------------------------------------------------------- function mainwin_create_hook() { local win="$p2" local ctrl cui_listview_new "$win" "" 0 0 10 10 4 "$IDC_LISTVIEW" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cui_valid_handle $ctrl then cui_listview_callback "$ctrl" "$LISTBOX_CLICKED" "$win" "listview_clicked_hook" cui_listview_callback "$ctrl" "$LISTBOX_POSTKEY" "$win" "listview_postkey_hook" cui_listview_setcoltext "$ctrl" 0 "Login" cui_listview_setcoltext "$ctrl" 1 "E-Mail" cui_listview_setcoltext "$ctrl" 2 "Comment" cui_listview_setcoltext "$ctrl" 3 "-" cui_window_create "$ctrl" fi cui_textview_new "$win" "Help" 0 0 10 10 "$IDC_HELPTEXT" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cui_valid_handle $ctrl then cui_window_setcolors "$ctrl" "HELP" cui_window_create "$ctrl" fi cui_return 1 } #---------------------------------------------------------------------------- # mainwin_init_hook (load data) # $p2 --> mainwin window handle #---------------------------------------------------------------------------- function mainwin_init_hook() { local win="$p2" local ctrl cui_window_getctrl "$win" "$IDC_HELPTEXT" && ctrl="$p2" if cui_valid_handle "$ctrl" then cui_textview_add "$ctrl" "Use function keys to edit (F4), create (F7) or delete (F8) Cal- & CardDAV users." "0" cui_textview_add "$ctrl" " " "1" fi my_server_connect "$sabredav_sql_server" \ "0" \ "$sabredav_sql_user" \ "$sabredav_sql_pass" \ "$sabredav_sql_db_name" && myconn="$p2" if cui_valid_handle $myconn then my_server_isconnected "$myconn" if p_sql_success "$p2" then selected_entry='' load_data "$win" else my_server_geterror "$myconn" cui_message "$win" "$p2" "Error" "$MB_ERROR" cui_window_quit 0 fi else cui_message "$win" "Unable to connect to database!" "Error" "$MB_ERROR" cui_window_quit 0 fi cui_return 1 } #---------------------------------------------------------------------------- # mainwin_key_hook (handle key events for mainwin) # $p2 --> mainwin window handle # $p3 --> key code #---------------------------------------------------------------------------- function mainwin_key_hook() { local win="$p2" local key="$p3" case $key in "$KEY_F1") if [ "$show_help" == "yes" ] then show_help="no" else show_help="yes" fi resize_windows $win ;; "$KEY_F3") cui_window_new "$win" 0 0 46 7 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED] && dlg="$p2" if cui_valid_handle $dlg then cui_window_setcolors "$dlg" "DIALOG" cui_window_settext "$dlg" "Search Filter" cui_window_sethook "$dlg" "$HOOK_CREATE" inputdlg_create_hook cui_window_create "$dlg" inputdlg_value="$keyword" cui_window_modal "$dlg" && result="$p2" if [ "$result" == "$IDOK" ] then cui_window_destroy "$dlg" keyword="$inputdlg_value" load_data "$win" else cui_window_destroy "$dlg" fi fi ;; "$KEY_F4") if user_edituser_dialog $win then load_data "$win" fi ;; "$KEY_F7") if user_createuser_dialog $win then load_data "$win" fi ;; "$KEY_F8") if user_deleteuser_dialog $win then load_data "$win" fi ;; "$KEY_F10") cui_window_quit 0 ;; *) cui_return 0 return ;; esac cui_return 1 } #---------------------------------------------------------------------------- # mainwin_size_hook (handle resize events for mainwin) # $p2 --> mainwin window handle #---------------------------------------------------------------------------- function mainwin_size_hook() { resize_windows "$p2" cui_return 1 } #---------------------------------------------------------------------------- # mainwin_destroy_hook (destroy mainwin object) # $p2 --> mainwin window handle #---------------------------------------------------------------------------- function mainwin_destroy_hook() { local win="$p2" cui_return 1 } #---------------------------------------------------------------------------- # init routine (entry point of all shellrun.cui based programs) # $p2 --> desktop window handle #---------------------------------------------------------------------------- function init() { local win="$p2" # prepare mysql connection my_initmodule if [ "$?" != "0" ] then cui_message "$win" "Unable to load mysql shell extension!" "Error" "$MB_ERROR" cui_return 0 return fi # setup main window cui_window_new "$win" 0 0 0 0 $[$CWS_POPUP + $CWS_CAPTION + $CWS_STATUSBAR + $CWS_MAXIMIZED] && mainwin="$p2" if cui_valid_handle $mainwin then cui_window_setcolors "$mainwin" "DESKTOP" cui_window_settext "$mainwin" "CalDAV & CardDAV user administration" cui_window_setlstatustext "$mainwin" "Commands: F1=Help F3=Search F4=Edit F7=Create F8=Delete F10=Exit" cui_window_setrstatustext "$mainwin" "V1.0.0" cui_window_sethook "$mainwin" "$HOOK_CREATE" mainwin_create_hook cui_window_sethook "$mainwin" "$HOOK_INIT" mainwin_init_hook cui_window_sethook "$mainwin" "$HOOK_KEY" mainwin_key_hook cui_window_sethook "$mainwin" "$HOOK_SIZE" mainwin_size_hook cui_window_sethook "$mainwin" "$HOOK_DESTROY" mainwin_destroy_hook cui_window_create "$mainwin" fi cui_return 0 } #---------------------------------------------------------------------------- # main routines (always at the bottom of the file) #---------------------------------------------------------------------------- cui_init cui_run exit 0