home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
p2demo21.exe
/
PEL
/
LOCKS.PEL
< prev
next >
Wrap
Text File
|
1995-03-14
|
19KB
|
609 lines
# $Header: P:\source\wmacros\locks.pev 1.18 14 Mar 1995 08:28:34 NOBLE $
##############################################################################
#
# Compuware Corporation
# 31440 Northwestern Highway
# Farmington Hills, Michigan 48334-2564
#
# This source code listing contains information that is
# proprietary to Compuware Corporation and may not be copied
# duplicated, translated, transmitted, stored, retrieved
# or in any manner or by any method conveyed or disclosed
# to a third party or parties without express written
# permission from Compuware Corporation.
#
#
##############################################################################
#### $Workfile: locks.pel $: file locking mechanism
local IDL_FILE_TYPE_LIST = 102
local IDR_FILE_COPY_ON_MOD = 1532
local IDR_FILE_COPY_ON_OPEN = 1533
local IDR_FILE_COPY_NO_ACTION = 1534
local IDC_FILE_LOCK_ON_MOD = 1531
local IDD_FILE_PAGE = 1530
local locked_filenames[]; # List of all semaphore file names
# to be deleted upon exit
local file_locking_enabled = 0; # File locking is enabled
local file_locking_cleanup_enabled = 0; # File locking has been enabled and
# cleanup needs to occur upon exit
local LOCKED_EXT_CHAR = "&" # extension character for locking files
## toggle_file_locking()
#
# Enable or disable the ability to lock files.
#
global function toggle_file_locking( on ) {
if( argcount() < 1 )
on = !file_locking_enabled;
else
on = 0+on
if (on)
enable_file_locking();
else
disable_file_locking();
}
## enable_file_locking()
#
# Enable file locking by attaching an event handler which will be called
# every time a new file is about to be edited.
#
# Also attach an event handler to cleanup all locked files upon exit.
#
local function enable_file_locking(){
if (!file_locking_enabled) {
# need a handler to create semaphore files for
# each file edited.
#
attach_event_handler( EVENT.NEW_EDIT_FILE, \
function_id( "lock_edit_file" ));
# need a handler to cleanup a semaphore files
#
if (!file_locking_cleanup_enabled) {
attach_event_handler( EVENT.EXIT_EDITOR, \
function_id( "unlock_edit_files" ));
file_locking_cleanup_enabled = 1;
}
}
}
## disable_file_locking()
#
# Disable file locking by deleting the event handler attached to the
# NEW_EDIT_FILE event.
#
local function disable_file_locking(){
if (file_locking_enabled) {
delete_event( EVENT.NEW_EDIT_FILE, \
function_id( "lock_edit_file" ));
}
}
## lock_edit_file()
#
# This function is attached to the NEW_EDIT_FILE event and is called
# for every file that is about to be loaded into the system either
# through edit_file() or create_buffer().
#
# This function only allows locking of non-SYSTEM buffers because multiple
# system buffers may need to be created without locking.
#
# Locking is obtained by taking the original filename and modifying the
# extension. If the new filename exists in the original directory, then
# the file is assummed to be locked and an error is generated. If the
# file can be created, it is and the original file is considered locked.
# If the new filename cannot be created, the original directory is assummed
# to be read-only and can't be written to anyway, so why lock it.
#
global function lock_edit_file(){
local fname;
local fid;
local bname = buffer_original_filename;
local pbuf = current_buffer;
local prev_pe = pause_on_error;
if ((buffer_original_filename) && (!and(buffer_flags, BUFFER_SYSTEM))){
while (next_buffer( "", 1 ) != pbuf) {
if ((buffer_original_filename == bname) \
&& (!and(buffer_flags, BUFFER_SYSTEM))){
current_buffer = pbuf;
return;
}
}
current_buffer = pbuf;
#
# only worry about locking non-system buffer files
#
fname = create_semaphore_fname( bname = buffer_original_filename, \
LOCKED_EXT_CHAR );
if (fname == bname){
# We are trying to edit one of the semaphore files.
# Could have been called to edit x.* and x.__& was
# created and found before findnext() was done.
# Just ignore the file for now.
buffer_filename = "";
return;
} else if (filetime( fname )) {
# the file exists so it must be locked by
# another user.
pause_on_error = 1;
buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
warning( "File `" bname "' already locked, loaded as read-only buffer." );
pause_on_error = prev_pe;
return;
## uncomment the next 2 lines and comment the
## previous 5 lines to prevent multiple
## users from seeing the same file
# buffer_filename = "";
# error( "File `" bname "' already locked." );
} else {
fid = fopen( fname, 1 ) # open for Write only
if (fid == -1){ # error occurred
if (filetime( fname )){ # created by another user
# the file exists so it must be locked by
# another user.
pause_on_error = 1;
buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
warning( "File `" bname "' locked, buffer is read-only." );
pause_on_error = prev_pe;
return;
## uncomment the next 2 lines and comment the
## previous 5 lines to prevent multiple
## users from seeing the same file
# buffer_filename = "";
# error( "File `" bname "' already locked." );
} else {
# can't open file so must be
# read-only directory
return;
}
} else {
#
# the file is now open
# save the name so we can delete it when the
# file is closed
#
locked_filenames[ fname ] = 0;
fclose( fid );
}
}
}
}
## unlock_edit_files()
#
# All files which have been locked are unlocked by deleting the associated
# semaphore file.
#
global function unlock_edit_files(){
local fn
for (fn in locked_filenames){
unlink( fn );
}
}
## unlock_file()
#
# All files which have been locked are unlocked by deleting the associated
# semaphore file.
#
global function unlock_file( fn ){
fn = create_semaphore_fname( fn, LOCKED_EXT_CHAR );
if (fn in locked_filenames){
unlink( fn );
delete( locked_filenames[ fn ] );
}
}
################################################################################
#
# Files section of the notebook
#
################################################################################
local dhAccessSettings = 0;
local phAccessSettings = 0;
local dhFileWindowSettings = 0;
local phFileWindowSettings = 0;
global function file_settings_notebook(dlgid)
{
local dhHelpSettings, phHelpSettings;
nb_initializing = TRUE;
if (dlgid)
{
if(status_bar_flags )
message( "Creating File Settings pages..." );
# Add the pages to the notebook
#
phHelpSettings = append_notebook_page( dlgid, dhHelpSettings,
" ",
"File", NBTAB_MAJOR )
nbPagePrefix[phHelpSettings].name = "filehelp";
# create_edithelp_page(phHelpSettings, dlgid);
phAccessSettings = append_notebook_page(dlgid, dhAccessSettings,
" File Locking and Virtual Memory",
"Access", NBTAB_MINOR )
nbPagePrefix[phAccessSettings].name = "access";
# phFileWindowSettings = append_notebook_page(dlgid, dhFileWindowSettings,
# " Files in windows",
# "Windows", NBTAB_MINOR )
#
# nbPagePrefix[phAccessSettings].name = "access";
# nbPagePrefix[phFileWindowSettings].name = "file_window";
}
else
{
warning("Illegal call to file_settings_notebook()");
}
nb_initializing = FALSE;
}
global function reset_file_settings_notebook()
{
dhAccessSettings = 0
phAccessSettings = 0
dhFileWindowSettings = 0
phFileWindowSettings = 0
}
global function create_filehelp_page(pageid, dlgid)
{
local dhHelpSettings;
dhHelpSettings = create_page(function_id( "nop_settings_callback"),
dlgid, IDD_FILE_HELP_PAGE, resource_dll );
attach_help(editor_helpfile, dhHelpSettings);
if ( isWindows() )
nbPagePrefix[pageid].help = "File Help Page";
else
nbPagePrefix[pageid].help = "filehelppage";
set_notebook_page_dialog(dlgid, pageid, dhHelpSettings);
nbPagePrefix[pageid].dialog_handle = dhHelpSettings;
}
global function create_access_page(pageid, dlgid)
{
dhAccessSettings = create_page(function_id( "access_settings_callback" ),
dlgid, IDD_FILE_PAGE, resource_dll )
add_dialog_item( dhAccessSettings, IDB_UNDO, DCTRL_PUSH_BUTTON )
attach_help(editor_helpfile, dhAccessSettings);
if ( isWindows() )
nbPagePrefix[pageid].help = "File Access Page";
else
nbPagePrefix[pageid].help = "fileaccesspage";
set_dialog_item( dhAccessSettings, IDL_FILE_TYPE_LIST, DAC_ADD_ITEM, "LAN files")
set_dialog_item( dhAccessSettings, IDL_FILE_TYPE_LIST, DAC_ADD_ITEM, "Local files")
set_dialog_item( dhAccessSettings, IDL_FILE_TYPE_LIST, DAC_ADD_ITEM, "Floppy files")
set_dialog_item( dhAccessSettings, IDL_FILE_TYPE_LIST, DAC_SELECT_INDEX, 0 )
initialize_access_settings( dhAccessSettings )
set_notebook_page_dialog(dlgid, pageid, dhAccessSettings);
nbPagePrefix[pageid].dialog_handle = dhAccessSettings;
set_dialog_item( dhAccessSettings, IDB_UNDO, DAC_DISABLE)
}
#global function create_file_window_page(pageid, dlgid)
#{
# dhFileWindowSettings = create_page(function_id( "file_window_settings_callback" ),
# dlgid, IDD_FILE_WINDOW_PAGE, resource_dll )
#
# add_dialog_item( dhFileWindowSettings, IDB_UNDO, DCTRL_PUSH_BUTTON )
# attach_help(editor_helpfile, dhFileWindowSettings);
#
# if ( isWindows() )
# nbPagePrefix[pageid].help = "File Window Page";
# else
# nbPagePrefix[pageid].help = "filewindowpage";
#
#
# initialize_file_window_settings( dhFileWindowSettings )
# set_notebook_page_dialog(dlgid, pageid, dhFileWindowSettings);
# nbPagePrefix[pageid].dialog_handle = dhFileWindowSettings;
#
# set_dialog_item( dhFileWindowSettings, IDB_UNDO, DAC_DISABLE)
#}
local lock_files[]
local function initialize_access_settings(handle)
{
lock_files[0].lock = lock_lan_files_on_mod
lock_files[0].copy = copy_lan_files_on_action
lock_files[1].lock = lock_local_files_on_mod
lock_files[1].copy = copy_local_files_on_action
lock_files[2].lock = lock_floppy_files_on_mod
lock_files[2].copy = copy_floppy_files_on_action
initialize_file_type(handle)
}
local function initialize_file_type(handle)
{
local type = query_dialog_item( handle, IDL_FILE_TYPE_LIST, DAC_SELECT_INDEX )
if ( lock_files[type].lock )
set_dialog_item( handle, IDC_FILE_LOCK_ON_MOD, DAC_CHECK )
else
set_dialog_item( handle, IDC_FILE_LOCK_ON_MOD, DAC_UNCHECK )
if ( lock_files[type].copy == "mod" )
{
set_dialog_item( handle, IDR_FILE_COPY_ON_MOD, DAC_CHECK )
set_dialog_item( handle, IDR_FILE_COPY_ON_OPEN, DAC_UNCHECK )
set_dialog_item( handle, IDR_FILE_COPY_NO_ACTION, DAC_UNCHECK )
}
else if ( lock_files[type].copy == "open" )
{
set_dialog_item( handle, IDR_FILE_COPY_ON_MOD, DAC_UNCHECK )
set_dialog_item( handle, IDR_FILE_COPY_ON_OPEN, DAC_CHECK )
set_dialog_item( handle, IDR_FILE_COPY_NO_ACTION, DAC_UNCHECK )
}
else
{
set_dialog_item( handle, IDR_FILE_COPY_ON_MOD, DAC_UNCHECK )
set_dialog_item( handle, IDR_FILE_COPY_ON_OPEN, DAC_UNCHECK )
set_dialog_item( handle, IDR_FILE_COPY_NO_ACTION, DAC_CHECK )
}
}
global function undo_access_settings(handle)
{
initialize_access_settings(handle)
}
global function access_settings_callback()
{
local handle = callback_dialog_handle
local ret_msg = DRC_CONTINUE;
local type
if(callback_msg == DM_HELPREQUESTED)
{
display_help(nbPagePrefix[current_nb_page].help, handle);
return DRC_MSG_PROCESSED;
}
else if(callback_msg == DM_CANCEL )
{
return DRC_MSG_PROCESSED
}
else if ( callback_msg == DM_SELECT )
{
initialize_file_type(handle)
}
else if((callback_msg == DM_CLICK) || (callback_msg == DM_DOUBLE_CLICK) )
{
if(callback_index == IDB_UNDO )
{
undo_access_settings(handle);
set_dialog_item( handle, IDB_UNDO, DAC_DISABLE)
return DRC_MSG_PROCESSED
}
else if( callback_index == IDB_DEFAULT )
{
nbPagePrefix[current_nb_page].modified = TRUE;
default_access_settings(handle);
return DRC_MSG_PROCESSED
}
else
{
set_dialog_item( handle, IDB_UNDO, DAC_ENABLE)
type = query_dialog_item( handle, IDL_FILE_TYPE_LIST, DAC_SELECT_INDEX )
nbPagePrefix[current_nb_page].modified = TRUE;
if ( callback_index == IDR_FILE_COPY_ON_MOD )
lock_files[type].copy = "mod"
else if ( callback_index == IDR_FILE_COPY_ON_OPEN )
lock_files[type].copy = "open"
else if ( callback_index == IDR_FILE_COPY_NO_ACTION )
lock_files[type].copy = ""
else if ( callback_index == IDC_FILE_LOCK_ON_MOD )
lock_files[type].lock = query_dialog_item( handle, callback_index, DAC_CHECK )
}
}
return ret_msg;
}
#local function initialize_file_window_settings( handle )
#{
# if ( mdi_mode )
# {
# set_dialog_item( handle, 102, DAC_HIDE )
#
# if ( create_new_bufwin )
# set_dialog_item(handle, IDC_OPEN_FILES_IN_WINDOW, DAC_CHECK )
# else
# set_dialog_item(handle, IDC_OPEN_FILES_IN_WINDOW, DAC_UNCHECK )
#
# if ( query_bind_buff_to_win() )
# set_dialog_item(handle, IDC_CLOSE_BUFFER_WINDOW, DAC_CHECK )
# else
# set_dialog_item(handle, IDC_CLOSE_BUFFER_WINDOW, DAC_UNCHECK )
# }
# else
# {
# set_dialog_item( handle, 102, DAC_SHOW )
# set_dialog_item(handle, IDC_OPEN_FILES_IN_WINDOW, DAC_DISABLE )
# set_dialog_item(handle, IDC_CLOSE_BUFFER_WINDOW, DAC_DISABLE )
# set_dialog_item(handle, IDB_DEFAULT, DAC_DISABLE )
# }
#}
#
#global function undo_file_window_settings(handle)
#{
# initialize_file_window_settings(handle)
#}
#
#global function default_file_window_settings(handle)
#{
# if ( mdi_mode )
# {
# set_dialog_item(handle, IDC_OPEN_FILES_IN_WINDOW, DAC_UNCHECK )
# set_dialog_item(handle, IDC_CLOSE_BUFFER_WINDOW, DAC_UNCHECK )
# }
# else
# {
# set_dialog_item(handle, IDC_OPEN_FILES_IN_WINDOW, DAC_DISABLE )
# set_dialog_item(handle, IDC_CLOSE_BUFFER_WINDOW, DAC_DISABLE )
# }
#}
#
#global function file_window_settings_callback()
#{
# local handle = callback_dialog_handle
# local ret_msg = DRC_CONTINUE;
#
# if(callback_msg == DM_HELPREQUESTED)
# {
# display_help(nbPagePrefix[current_nb_page].help, handle);
# return DRC_MSG_PROCESSED;
# }
# else if(callback_msg == DM_CANCEL )
# {
# return DRC_MSG_PROCESSED
# }
# else if((callback_msg == DM_CLICK) || (callback_msg == DM_DOUBLE_CLICK) )
# {
# if(callback_index == IDB_UNDO )
# {
# nbPagePrefix[current_nb_page].modified = FALSE;
# undo_file_window_settings(handle);
# set_dialog_item( handle, IDB_UNDO, DAC_DISABLE)
# return DRC_MSG_PROCESSED
# }
# else if( callback_index == IDB_DEFAULT )
# {
# nbPagePrefix[current_nb_page].modified = TRUE;
# default_file_window_settings(handle);
# return DRC_MSG_PROCESSED
# }
# else if( callback_index == IDC_OPEN_FILES_IN_WINDOW || callback_index == IDC_CLOSE_BUFFER_WINDOW )
# {
# nbPagePrefix[current_nb_page].modified = TRUE;
# set_dialog_item( handle, IDB_UNDO, DAC_ENABLE)
# }
# }
#
# return ret_msg;
#}
global function assign_access_changes(handle)
{
toggle_lock_lan_files_on_mod( lock_files[0].lock )
toggle_lock_local_files_on_mod( lock_files[1].lock )
toggle_lock_floppy_files_on_mod( lock_files[2].lock )
copy_lan_files_on( lock_files[0].copy )
copy_local_files_on( lock_files[1].copy )
copy_floppy_files_on( lock_files[2].copy )
}
#global function assign_file_window_changes(handle)
#{
# local ofiw = query_dialog_item( handle, IDC_OPEN_FILES_IN_WINDOW, DAC_CHECK )
# local prev_setting = create_new_bufwin;
# local new_setting = ofiw;
# local first, cb, list;
# local first_window = current_window;
#
# toggle_create_new_win( ofiw )
#
# ofiw = query_dialog_item( handle, IDC_CLOSE_BUFFER_WINDOW, DAC_CHECK )
# prev_setting = (prev_setting && query_bind_buff_to_win());
# new_setting = (new_setting && ofiw);
# toggle_bind_buff_to_win( ofiw )
#
# # If the user turned the feature on, put each buffer in it's own window;
# cascade_buffers();
## if(new_setting && !prev_setting)
## {
## first = current_buffer;
## cb = next_buffer();
##
## # Save each buffer filename in a list (create_buf_and_win didn't work
## # if the buffer we open is current;
## while(cb != first)
## {
## list[buffer_filename] = TRUE;
## cb = next_buffer();
## }
## cb = FALSE;
##
## # Attach each buffer in the list to its own window;
## for(cb in list)
## create_buf_and_win(cb);
## }
# # I go back to the first window because there is a busy cursor there
# # and the cursor won't be reset unless the notebook finishes processing
# # with this window current;
# current_window = first_window;
#}
global function default_access_settings(handle)
{
lock_files[0].lock = 0
lock_files[0].copy = ""
lock_files[1].lock = 0
lock_files[1].copy = ""
lock_files[2].lock = 0
lock_files[2].copy = ""
initialize_file_type(handle)
}