fileop.pas

来自「是一个手机功能的模拟程序」· PAS 代码 · 共 675 行 · 第 1/2 页

PAS
675
字号
    if filelist.Items[0]<>nil then freemem(filelist.Items[0]);
    filelist.Delete(0);
  end;

  tempnode:=TreeView_dir.Selected;
  if tempnode=nil then exit;
  pathname:='';
  if tempnode.level>0 then
  begin
    repeat
      pathname:='/'+tempnode.Text+pathname;
      tempnode:=tempnode.Parent;
      tempnode.ImageIndex:=1;
    until tempnode.Level=0;
  end;


  for i:=0 to FileListBox1.Items.Count-1 do
  begin
    if FileListBox1.Selected[i] then
    begin
      if FileExists(filelistbox1.Directory+'\'+filelistbox1.Items[i]) then
      begin
        tempitem:=ListView_file.FindCaption(0,filelistbox1.Items[i],false,true,true);
        if tempitem<>nil then
        begin
          if MessageDlg('上载的文件'''+tempitem.Caption+'''在手机上已存在,是否覆盖?'
               ,mtConfirmation, [mbYes, mbNo], 0)=mrNo then continue;
          if tempitem.subitems[1]='是' then
          begin
            showmessage('要覆盖的文件是只读的,无法上载!');
            continue;
          end;
        end;

        getmem(tempFileRec,sizeof(TFileRec));
        strpcopy(tempFileRec.Pathname,pathname);
        filename:=filelistbox1.Directory+'\'+filelistbox1.Items[i];
        strpcopy(tempFileRec.Filename,filename);
        FileAttrs:=0;
        if fileexists(filename) then
        begin
          if FindFirst(filename, FileAttrs, sr) = 0 then
            tempFileRec.Filesize:=sr.Size;
        end;
        tempFileRec.ReadOnly:=false;
        FileList.Add(tempFileRec);
      end;
    end;
  end;
  if FileList.Count>0 then
  begin
    Form_fileop.statusbar_fileop.Panels[0].Text:='正在上载文件';
    status_FILE:=PC_FILE_status_ULfile;
    putfile(@pc_callback_FILEGetInfo,strpas(PFileRec(filelist.items[0]).filename),strpas(PFileRec(filelist.items[0]).Pathname)+'/');
    showstr:='正在上载文件(共 '+inttostr(filelist.Count)+'个)';
    form_progressbar.Label_status.Caption:=showstr;
    form_progressbar.ProgressBar1.Max:=filelist.Count+1;
    form_progressbar.ProgressBar1.Min:=1;
    form_progressbar.ProgressBar1.Step:=1;
    form_progressbar.ProgressBar1.Position:=1;
    form_progressbar.Showmodal;
  end;
end;

procedure TForm_fileop.SpeedButton_DelfileClick(Sender: TObject);
var pathname:string;
    tempnode:TTreeNode;
    itemSelected:TListItem;
    tempFileRec:PFileRec;
    showstr:string;
begin
  while FileList.Count>0 do
  begin
    if filelist.Items[0]<>nil then freemem(filelist.Items[0]);
    filelist.Delete(0);
  end;

  tempnode:=TreeView_dir.Selected;
  if tempnode=nil then exit;
  pathname:='';
  if tempnode.level>0 then
  begin
    repeat
      pathname:='/'+tempnode.Text+pathname;
      tempnode:=tempnode.Parent;
      tempnode.ImageIndex:=1;
    until tempnode.Level=0;
  end;
  if MessageDlg('确定要删除吗?',mtConfirmation, [mbYes, mbNo], 0)=mrNo then exit;

  itemSelected:=ListView_file.Selected;
  while itemSelected<>nil do
  begin
    getmem(tempFileRec,sizeof(TFileRec));
    strpcopy(tempFileRec.Pathname,pathname);
    strpcopy(tempFileRec.Filename,itemSelected.Caption);
    tempFileRec.Filesize:=strtoint64(transdigital(itemSelected.SubItems[0]));
    tempFileRec.ReadOnly:=itemSelected.SubItems[1]='是';
    FileList.Add(tempFileRec);
    itemSelected:=ListView_file.GetNextItem(itemSelected, sdAll, [isSelected]);
  end;
  if FileList.Count>0 then
  begin
    Form_fileop.statusbar_fileop.Panels[0].Text:='正在删除文件';
    status_FILE:=PC_FILE_status_DeLfile;
    Delfile(@pc_callback_FILEGetInfo,strpas(PFileRec(filelist.items[0]).Pathname)+'/'+strpas(PFileRec(filelist.items[0]).filename));
    showstr:='正在删除文件(共 '+inttostr(filelist.Count)+'个)';
    form_progressbar.Label_status.Caption:=showstr;
    form_progressbar.ProgressBar1.Max:=filelist.Count+1;
    form_progressbar.ProgressBar1.Min:=1;
    form_progressbar.ProgressBar1.Step:=1;
    form_progressbar.ProgressBar1.Position:=1;
    form_progressbar.Showmodal;
  end;
end;

procedure TForm_fileop.ListView_fileColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  if FLastIndex_file = Column.Index then
  begin
    ListView_file.CustomSort(@CustSortDesc, Column.Index);
    FLastIndex_file := -1;
  end  else
  begin
    ListView_file.CustomSort(@CustSortAsc, Column.Index);
    FLastIndex_file := Column.Index;
  end;
end;

procedure TForm_fileop.SpeedButton_downloaddirClick(Sender: TObject);
var pathname,directory:string;
    tempnode:TTreeNode;
    itemSelected:TListItem;
    tempFileRec:PFileRec;
    i:integer;
    showstr:string;
begin
  if TreeView_dir.Selected<>nil then
  begin
    while FileList.Count>0 do
    begin
      if filelist.Items[0]<>nil then freemem(filelist.Items[0]);
      filelist.Delete(0);
    end;
    tempnode:=TreeView_dir.Selected;
    if tempnode=nil then exit;
    directory:=tempnode.Text;
    pathname:='';
    if tempnode.level>0 then
    begin
      repeat
        pathname:='/'+tempnode.Text+pathname;
        tempnode:=tempnode.Parent;
        tempnode.ImageIndex:=1;
      until tempnode.Level=0;
    end;
    if not DirectoryExists(FileListBox1.Directory+'\'+directory) then
    begin
      if not CreateDir(FileListBox1.Directory+'\'+directory) then
      begin
        raise Exception.Create('无法在PC上创建目录'''+FileListBox1.Directory+'\'+directory+'''');
        exit;
      end;
    end else
      if MessageDlg('下载的目录'''+FileListBox1.Directory+'\'+directory+'''在PC上已存在,是否要完全覆盖?',mtConfirmation, [mbYes, mbNo], 0)=mrNo then exit;
    FileListBox1.Directory:=FileListBox1.Directory+'\'+directory;
    FileListBox1.Update;

    for i:=0 to ListView_file.Items.Count-1 do
    begin
      itemSelected:=ListView_file.Items[i];
      getmem(tempFileRec,sizeof(TFileRec));
      strpcopy(tempFileRec.Pathname,pathname);
      strpcopy(tempFileRec.Filename,itemSelected.Caption);
      tempFileRec.Filesize:=strtoint64(transdigital(itemSelected.SubItems[0]));
      tempFileRec.ReadOnly:=itemSelected.SubItems[1]='是';
      FileList.Add(tempFileRec);
    end;

    if FileList.Count>0 then
    begin
      Form_fileop.statusbar_fileop.Panels[0].Text:='正在下载文件';
      status_FILE:=PC_FILE_status_DLfile;
      getfile(@pc_callback_FILEGetInfo,strpas(PFileRec(filelist.items[0]).Pathname)+'/'+strpas(PFileRec(filelist.items[0]).filename));
      showstr:='正在下载文件(共 '+inttostr(filelist.Count)+'个)';
      form_progressbar.Label_status.Caption:=showstr;
      form_progressbar.ProgressBar1.Max:=filelist.Count+1;
      form_progressbar.ProgressBar1.Min:=1;
      form_progressbar.ProgressBar1.Step:=1;
      form_progressbar.ProgressBar1.Position:=1;
      form_progressbar.Showmodal;
      //    SpeedButton_download.Enabled:=false;
    end;
  end;
end;

procedure TForm_fileop.SpeedButton_uploaddirClick(Sender: TObject);
var pathname,filename:string;
    tempnode,childnode:TTreeNode;
    i:integer;
    tempFileRec:PFileRec;
    sr: TSearchRec;
    FileAttrs: Integer;
    showstr:string;
begin
  DirectoryListBox1.OpenCurrent;
  if DirectoryExists(FileListBox1.Directory) then
  begin
    while FileList.Count>0 do
    begin
      if filelist.Items[0]<>nil then freemem(filelist.Items[0]);
      filelist.Delete(0);
    end;

    tempnode:=TreeView_dir.Selected;
    if tempnode=nil then exit;
    pathname:='';
    if tempnode.level>0 then
    begin
      repeat
        pathname:='/'+tempnode.Text+pathname;
        tempnode:=tempnode.Parent;
        tempnode.ImageIndex:=1;
      until tempnode.Level=0;
    end;

    if TreeView_dir.Selected.HasChildren then
    begin
      childnode:=TreeView_dir.Selected.getFirstChild;
      while childnode<>nil do
      begin
        if childnode.Text=ExtractFileName(FileListBox1.Directory) then
          if MessageDlg('上载的目录'''+pathname+'/'+childnode.Text+'''在手机上已存在,是否要完全覆盖?',mtConfirmation, [mbYes, mbNo], 0)=mrNo then
            exit
          else
            break;
        childnode:=childnode.GetNextSibling;
      end;
    end;

    for i:=0 to FileListBox1.Count-1 do
    begin
      getmem(tempFileRec,sizeof(TFileRec));
      strpcopy(tempFileRec.Pathname,pathname+'/'+ExtractFileName(FileListBox1.Directory));
      filename:=FileListBox1.Directory+'\'+FileListBox1.items[i];
      strpcopy(tempFileRec.Filename,filename);
      FileAttrs:=0;
      if fileexists(filename) then
      begin
        if FindFirst(filename, FileAttrs, sr) = 0 then
          tempFileRec.Filesize:=sr.Size;
      end;
      tempFileRec.ReadOnly:=false;
      FileList.Add(tempFileRec);
    end;

    if FileList.Count>0 then
    begin
      Form_fileop.statusbar_fileop.Panels[0].Text:='正在上载文件';
      status_FILE:=PC_FILE_status_ULfile;
      putfile(@pc_callback_FILEGetInfo,strpas(PFileRec(filelist.items[0]).filename),strpas(PFileRec(filelist.items[0]).Pathname)+'/');
      showstr:='正在上载文件(共 '+inttostr(filelist.Count)+'个)';
      form_progressbar.Label_status.Caption:=showstr;
      form_progressbar.ProgressBar1.Max:=filelist.Count+1;
      form_progressbar.ProgressBar1.Min:=1;
      form_progressbar.ProgressBar1.Step:=1;
      form_progressbar.ProgressBar1.Position:=1;
      form_progressbar.Showmodal;
    end;
  end;
end;

procedure TForm_fileop.FormActivate(Sender: TObject);
begin
  TreeView_dir.SetFocus;
end;

procedure TForm_fileop.SpeedButton_deldirClick(Sender: TObject);
var pathname:string;
    tempnode:TTreeNode;
    tempFileRec:PFileRec;
begin
  if TreeView_dir.Selected<>nil then
  begin
    while FileList.Count>0 do
    begin
      if filelist.Items[0]<>nil then freemem(filelist.Items[0]);
      filelist.Delete(0);
    end;

    tempnode:=TreeView_dir.Selected;
    if tempnode=nil then exit;
    pathname:='';
    tempnode.Expanded:=true;
    if tempnode.level>0 then
    begin
      repeat
        pathname:='/'+tempnode.Text+pathname;
        tempnode:=tempnode.Parent;
        tempnode.ImageIndex:=1;
      until tempnode.Level=0;
    end;

    if MessageDlg('确定要删除目录所有内容吗?',mtConfirmation, [mbYes, mbNo], 0)=mrNo then exit;
//删除整个目录下的所有文件
//    Form_fileop.TreeView_dir.Selected:=Form_fileop.TreeView_dir.Selected.Parent;
    getmem(tempFileRec,sizeof(TFileRec));
    strpcopy(tempFileRec.Pathname,pathname);
    strpcopy(tempFileRec.Filename,'');
    tempFileRec.Filesize:=0;
    tempFileRec.ReadOnly:=false;
    FileList.Add(tempFileRec);
    if FileList.Count>0 then
    begin
      Form_fileop.statusbar_fileop.Panels[0].Text:='正在删除目录';
      status_FILE:=PC_FILE_status_DeLfile;
      Delfile(@pc_callback_FILEGetInfo,strpas(PFileRec(filelist.items[0]).Pathname)+'/'+strpas(PFileRec(filelist.items[0]).filename));
      form_progressbar.Label_status.Caption:='正在删除目录';
      form_progressbar.ProgressBar1.Max:=filelist.Count+1;
      form_progressbar.ProgressBar1.Min:=1;
      form_progressbar.ProgressBar1.Step:=1;
      form_progressbar.ProgressBar1.Position:=1;
      form_progressbar.Showmodal;
    end;
  end;
end;

procedure TForm_fileop.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
//  TreeView_dir.Selected:=nil;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?