{------------------------------------------------------------------------------} { Unit Name: UserListForm Purpose : To show the users.html for all students from one demo Author : Vesa Lappalainen Date : 28.1.2001 Changed : ToDo : } {------------------------------------------------------------------------------} unit UserListForm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, kEditPnl, ExtCtrls, DemoWWWForm, savepos, ActnList; type TUser = class nr : integer; firstname : string; lastname : string; user : string; points : double; group : string; private function GetAsListString: string; function GetAsString: string; procedure SetAsString(const Value: string); public property AsString : string read GetAsString write SetAsString; property AsListString : string read GetAsListString; end; TFormUserList = class(TForm) PanelPath: TPanel; EditIndexFile: TEditPanel; EditIndexFileEdit: TEdit; OpenDialog1: TOpenDialog; PanelList: TPanel; ListBoxIndex: TListBox; SavePos1: TSavePos; ActionList1: TActionList; ActionRandom: TAction; PanelGroup: TPanel; ComboBoxGroup: TComboBox; ButtonSort: TButton; EditTask: TEditPanel; EditTaskEdit: TEdit; procedure EditIndexFileClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure ListBoxIndexClick(Sender: TObject); procedure ActionRandomExecute(Sender: TObject); procedure EditIndexFileEditClick(Sender: TObject); procedure EditIndexFileEditKeyPress(Sender: TObject; var Key: Char); procedure ComboBoxGroupChange(Sender: TObject); procedure ButtonSortClick(Sender: TObject); private DemoWWW : TFormDemoWWW; procedure ReadFile(const name: string); function ShowFile:boolean; procedure LoadUserFile; procedure AskUserFile; procedure ClearUsers; procedure ShowRandomUser; public { Public declarations } end; procedure FormUserListView(form:TFormDemoWWW; const name:string=''); procedure FormUserListUser(direction:integer); function FormUserListVisible:boolean; var FormUserList: TFormUserList; implementation uses kstring,kdouble; {$R *.DFM} const StartHRef = '%s = %f
', [nr,user,group,firstname,lastname,points]); end; procedure TUser.SetAsString(const Value: string); // 89 Vesa Lappalainen = 9.5
var s,dummy:string; begin s := Value; dummy := Separate(s,'<'); nr := StrToIntDef(Trim(dummy),0); dummy := Separate(s,'"'); user := Separate(s,'/'); dummy := Separate(s,'>'); Separate(dummy,'='); group := Trim(Separate(dummy,'"')); lastname := Trim(Separate(s,'<')); firstname := Separate(lastname,' '); dummy := Separate(s,'='); dummy := Separate(s,'<'); points := StrToDoubleDef(Trim(dummy),0); end; { TFormUserList } procedure TFormUserList.ClearUsers; var i:integer; begin for i:=0 to ListBoxIndex.Items.Count-1 do begin ListBoxIndex.Items.Objects[i].Free; end; end; procedure TFormUserList.ReadFile(const name:string); var f:TextFile; sg,s:string; i:integer; user : TUser; begin sg := 'All'; if ( ComboBoxGroup.ItemIndex >= 0 ) then sg := ComboBoxGroup.Items[ComboBoxGroup.ItemIndex]; AssignFile(f,name); ClearUsers; ListBoxIndex.Items.Clear; ComboBoxGroup.Items.Clear; ComboBoxGroup.Items.Add('All'); user := nil; try try Reset(f); while not Eof(f) do begin Readln(f,s); i := Pos(StartHRef,s); if ( i = 0 ) then continue; if ( user = nil ) then user := TUser.Create; user.AsString := s; if ( user.group <> '' ) and ( ComboBoxGroup.Items.IndexOf(user.group) < 0 ) then ComboBoxGroup.Items.Add(user.group); if ( sg = 'All' ) or ( sg = user.group ) then begin ListBoxIndex.Items.AddObject(user.AsListString,user); user := nil; end end; ComboBoxGroup.Items.Add(''); ComboBoxGroup.ItemIndex := ComboBoxGroup.Items.IndexOf(sg); except end; finally CloseFile(f); if ( user <> nil ) then user.Free; end; end; procedure TFormUserList.LoadUserFile; begin ReadFile(EditIndexFile.AsString); end; procedure TFormUserList.AskUserFile; begin OpenDialog1.FileName := EditIndexFile.AsString; if ( not OpenDialog1.Execute ) then exit; EditIndexFile.AsString := OpenDialog1.FileName; LoadUserFile; end; procedure TFormUserList.EditIndexFileClick(Sender: TObject); begin AskUserFile; end; procedure TFormUserList.FormCreate(Sender: TObject); begin Randomize; LoadUserFile; end; procedure TFormUserList.FormDestroy(Sender: TObject); begin ClearUsers; end; function FormUserListVisible:boolean; begin Result := FormUserList <> nil; end; procedure FormUserListView(form:TFormDemoWWW; const name:string); begin if ( FormUserList = nil ) then Application.CreateForm(TFormUserList, FormUserList); FormUserList.DemoWWW := form; FormUserList.Show; if ( name <> '' ) then begin FormUserList.EditIndexFile.AsString := name; FormUserList.LoadUserFile; end; end; procedure FormUserListUser(direction:integer); begin if ( FormUserList = nil ) then exit; if ( direction = 0 ) then begin FormUserList.ShowRandomUser; Exit; end; repeat FormUserList.ListBoxIndex.ItemIndex := FormUserList.ListBoxIndex.ItemIndex + direction; if ( FormUserList.ShowFile ) then break; until( FormUserList.ListBoxIndex.ItemIndex < 0 ) or ( FormUserList.ListBoxIndex.ItemIndex >= FormUserList.ListBoxIndex.Items.Count-1 ); end; function TFormUserList.ShowFile:boolean; var name : string; user :TUser; begin Result := false; if ( ListBoxIndex.ItemIndex < 0 ) then exit; if ( DemoWWW = nil ) then exit; user := TUser(ListBoxIndex.Items.Objects[ListBoxIndex.ItemIndex]); name := ExtractFilePath(EditIndexFile.AsString) + user.user + '\' + 'files.txt'; Result := DemoWWW.LoadFile(name,EditTask.Text); end; procedure TFormUserList.ListBoxIndexClick(Sender: TObject); begin ShowFile; end; procedure TFormUserList.ShowRandomUser; var i:integer; begin for i:=0 to 100 do begin ListBoxIndex.ItemIndex := Random(ListBoxIndex.Items.Count); if ( ShowFile ) then exit; end; end; procedure TFormUserList.ActionRandomExecute(Sender: TObject); begin ShowRandomUser; end; procedure TFormUserList.EditIndexFileEditClick(Sender: TObject); begin // end; procedure TFormUserList.EditIndexFileEditKeyPress(Sender: TObject; var Key: Char); begin if ( Key = #13 ) then begin LoadUserFile; Key := #0; end; end; procedure TFormUserList.ComboBoxGroupChange(Sender: TObject); begin LoadUserFile; end; function ComparePoints(List: TStringList; Index1, Index2: Integer): Integer; var user1,user2:TUser; begin user1 := TUser(List.Objects[Index1]); user2 := TUser(List.Objects[Index2]); Result := Round(user1.points - user2.points); if ( Result = 0 ) then begin if ( user1.LastName < user2.LastName ) then Result := -1 else if ( user1.LastName > user2.LastName ) then Result := 1 end; end; procedure TFormUserList.ButtonSortClick(Sender: TObject); var st : TStringList; begin st := TStringList.Create; st.Assign(ListBoxIndex.Items); st.CustomSort(ComparePoints); ListBoxIndex.Items.Assign(st); st.Free; end; end.