{************************************}
{* *}
{* DS Date Sharing Manager *}
{* *}
{* © by Andreas Unterweger 2004 *}
{* *}
{************************************}
unit dsdsmgr;
interface
uses ds_core, Unit1, SysUtils, Dialogs, Unit5;
type
TStringArray =
Array of String;
TSharedTerminListArray =
Array of record
TerminList: TTerminList;
User:
String;
end;
TCheckState = (csDoesntExist, csIsOlder, csIsUpToDate);
TDateAction = (daAdd, daUpdate, daNone);
TUpdateTermin =
record
Termin: TTermin;
Action: TDateAction;
end;
TUpdateTerminList =
Array of TUpdateTermin;
function CreateSharedTerminList(TerminList: TTerminList): TTerminList;
function GetUsers(Users:
String): TStringArray;
function UserExists(UserArray: TSharedTerminListArray; User:
String): Boolean;
function GetUserPos(UserArray: TSharedTerminListArray; User:
String): Integer;
function SeparateTerminLists(TerminList: TTerminList): TSharedTerminListArray;
procedure SaveSharedTerminLists(UserArray: TSharedTerminListArray; UseOldName:
String = '
');
procedure SaveSharedDates(TerminList: TTerminList; UseOldName:
String = '
');
function LoadSharedDates(User, SharedUser:
String): TTerminList;
function SUIDToIndex(SUID: Integer; TerminList: TTerminList): Integer;
function CheckSharedTermin(t: TTermin; TerminList: TTerminList): TCheckState;
procedure Synchronize(
var TerminList: TTerminList; Update: TUpdateTerminList);
procedure DeleteDateBySUID(
var TerminList: TTerminList; SUID: Integer);
implementation
function CreateSharedTerminList(TerminList: TTerminList): TTerminList;
var
i: Integer;
begin
SetLength(Result, 0);
for i := Low(TerminList)
to High(TerminList)
do begin
if TerminList[i].Shared.Shared
then begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := TerminList[i];
end;
end;
end;
function GetUsers(Users:
String): TStringArray;
var
s, t:
String;
begin
SetLength(Result, 0);
s := Users;
while s <> '
'
do begin
SetLength(Result, Length(Result) + 1);
if Pos('
;', s) = 0
then begin
t := s;
s := '
';
end else begin
t := Copy(s, 1, Pos('
;', s) - 1);
Delete(s, 1, Pos('
;', s));
end;
Result[High(Result)] := t;
end;
end;
function UserExists(UserArray: TSharedTerminListArray; User:
String): Boolean;
begin
Result := (GetUserPos(UserArray, User) <> -1);
end;
function GetUserPos(UserArray: TSharedTerminListArray; User:
String): Integer;
var
i: Integer;
begin
Result := -1;
for i := Low(UserArray)
to High(UserArray)
do begin
if UserArray[i].User = User
then begin
Result := i;
exit;
end;
end;
end;
function SeparateTerminLists(TerminList: TTerminList): TSharedTerminListArray;
var
i, j: Integer;
a: TStringArray;
begin
SetLength(Result, 0);
for i := Low(TerminList)
to High(TerminList)
do begin
a := GetUsers(TerminList[i].Shared.SharedUsers);
for j := Low(a)
to High(a)
do begin
if not(UserExists(Result, a[j]))
then begin
//user does not exist --> create
SetLength(Result, Length(Result) + 1);
Result[High(Result)].User := a[j];
SetLength(Result[High(Result)].TerminList, 1);
Result[High(Result)].TerminList[0] := TerminList[i];
end else begin
//user already exists --> add
SetLength(Result[GetUserPos(Result, a[j])].TerminList, Length(Result[GetUserPos(Result, a[j])].TerminList) + 1);
Result[GetUserPos(Result, a[j])].TerminList[High(Result[GetUserPos(Result, a[j])].TerminList)] := TerminList[i];
end;
end;
end;
end;
procedure SaveSharedTerminLists(UserArray: TSharedTerminListArray; UseOldName:
String = '
');
var
i: Integer;
dummy: TTerminList;
dummy2:
String;
begin
if Length(UserArray) = 0
then begin {empty sync save}
dummy := termine;
termine :=
nil;
dummy2 := allowedusers;
if UseOldName <> '
'
then
allowedusers := act_user + '
;' + UseOldName;
if UseOldName <> '
'
then
Form1.SaveArrayNew(r_fp + '
Users\' + act_user + '
\s_data.' + UseOldName + '
.dstkf');
allowedusers := dummy2;
termine := dummy;
end;
for i := Low(UserArray)
to High(UserArray)
do begin
dummy := termine;
termine := UserArray[i].TerminList;
dummy2 := allowedusers;
if UseOldName <> '
'
then
allowedusers := act_user + '
;' + UseOldName
else
allowedusers := act_user + '
;' + UserArray[i].User;
if UseOldName <> '
'
then
Form1.SaveArrayNew(r_fp + '
Users\' + UserArray[i].User + '
\s_data.' + UseOldName + '
.dstkf')
else
Form1.SaveArrayNew(r_fp + '
Users\' + UserArray[i].User + '
\s_data.' + act_user + '
.dstkf');
allowedusers := dummy2;
termine := dummy;
end;
end;
procedure SaveSharedDates(TerminList: TTerminList; UseOldName:
String = '
');
begin
SaveSharedTerminLists(SeparateTerminLists(CreateSharedTerminList(TerminList)), UseOldName);
end;
function LoadSharedDates(User, SharedUser:
String): TTerminList;
var
temp: TTerminList;
begin
temp := termine;
Form1.LoadArray(false, r_fp + '
Users\' + User + '
\s_data.' + SharedUser + '
.dstkf');
Result := termine;
termine := temp;
end;
function SUIDToIndex(SUID: Integer; TerminList: TTerminList): Integer;
var
i: Integer;
begin
Result := -1;
for i := Low(TerminList)
to High(TerminList)
do begin
if TerminList[i].Shared.SharedUID = SUID
then begin
Result := i;
exit;
end;
end;
end;
function CheckSharedTermin(t: TTermin; TerminList: TTerminList): TCheckState;
var
i: Integer;
begin
i := SUIDToIndex(t.Shared.SharedUID, TerminList);
if i = -1
then begin
Result := csDoesntExist;
exit;
end;
if t.Shared.LastUpdate > TerminList[i].Shared.LastUpdate
then
Result := csIsOlder
else
Result := csIsUpToDate;
end;
procedure Synchronize(
var TerminList: TTerminList; Update: TUpdateTerminList);
var
i: Integer;
begin
for i := Low(Update)
to High(Update)
do begin
case Update[i].Action
of
daAdd: AddTermin(TerminList, Update[i].Termin);
daUpdate: EditTermin(TerminList, SUIDToIndex(Update[i].Termin.Shared.SharedUID, TerminList), Update[i].Termin);
// daNone: ; // do nothing
end;
end;
end;
procedure DeleteDateBySUID(
var TerminList: TTerminList; SUID: Integer);
begin
EraseTermin(TerminList, SUIDToIndex(SUID, TerminList));
end;
end.