{
  this script will search for catalog labels without assigned images
  (empty labels)

  author: Hert van Zwietering
  initial date: 2008-02-20

  V3.1; 2023-06-07; added message for no labels found, and uses separet progress
  V3; 2014-08-19; made compatible with Photo Supreme
}

var
  AProps: TCatalogItemProps;
  AProp: TCatalogItemProp;
  i: Integer;
  AList: TTntStringList;
  ASl: TTntStringList;
  AResult: Integer;
  ACheck: Boolean;
  ADoAsk: Boolean;
  AHit: Integer;
  AProgress: TxomProgress;
begin
  AProps := TCatalogItemProps.Create(TCatalogItemProp, '');
  AList := TTntStringList.Create;

  AProgress := TxomProgress.Create(nil);

  AProgress.Caption := 'Finding catalog labels in database...';
  AProgress.Show;
  Application.ProcessMessages;

  PublicCatalog.EnumAllProps (AProps);

  AProgress.Max := AProps.Count;
  AProgress.Pos := 0;

  for i := 0 to AProps.Count - 1 do
  begin
    AProgress.Pos := i + 1;
    AProgress.ProgressText := IntToStr(MulDiv (i, 100, AProps.Count)) + '%; Found ' + IntToStr(AList.Count) + ' empty labels';

    AProp := AProps.Items[i];
    if AProp.PropValue = AutoPropValue then    // skip auto-props
      Continue;

    //if not PublicCatalog.PropHasItems (AProp) then
    if AProp.PhotoCount <= 0 then
      AList.AddObject (PublicCatalog.PathNameForProp(AProp, '::', True), AProp);

    // for testing
    //if AList.Count = 100 then
    //  break;

    if AProgress.Cancel then
      break;
  end;

  AProgress.Hide;

  ASaved := False;
  ASl := TTntStringList.Create;
  ASl.Add('Save results to file');
  ASl.Add('Show results on-screen');
  ASl.Add('Delete all ' + IntToStr(AList.Count) + ' empty catalog labels');
  ASl.Add('Cancel');

  ADoAsk := AList.Count > 0;
  if ADoAsk then
  begin
    while ADoAsk do
    begin
      ACheck := False;
      AResult := AskCustom ('What would you like to do?', 'There are ' + IntToStr(AList.Count) + ' empty catalog labels found.', '', '', ACheck, ASl);

      case AResult of
        0:      // save to file
        begin
          SaveDialog.FileName := WideIncludeTrailingBackslash(WindowsDesktopDir) + 'empty labels.txt';
          if SaveDialog.Execute then
          begin
            AList.SaveToFile (SaveDialog.FileName);
            Say ('The results are saved to file ' + SaveDialog.FileName);
            ADoAsk := False;
          end;
        end;
        1:      // show on screen
        begin
          Say (AList.Text);
          ADoAsk := True;
        end;
        2:      // delete empty labels
        begin
          if Ask ('Are you sure you want to permanently delete these ' + IntToStr(AList.Count) + ' empty catalog labels?') then
          begin
            AProgress.Cancel  := False;
            AProgress.Max     := AList.Count;
            AProgress.Pos     := 0;
            AProgress.Show;

            AHit := 0;
            for i := 0 to AList.Count - 1 do
            begin
              AProgress.Pos := i + i;
              AProgress.ProgressText := IntToStr(MulDiv(i, 100, AList.Count)) + ' %';

              AProp := AList.Objects[i];

              //Say (AProp.PropName);
              PublicCatalog.DeleteProp (AProp);
              Inc(AHit);

              if AProgress.Cancel then
                break;
            end;

            AProgress.Hide;

            Say (IntToStr(AHit) + ' empty catalog labels are deleted from the catalog');
            ADoAsk := not AProgress.Cancel;
          end;
        end;
        -1, 3:      // cancel
        begin
          ADoAsk := False;
        end;
      end;
    end;
  end
  else
  begin
    AProgress.Hide;
    Say('No empty Catalog Label found');
  end;

  AList.Free;
  AProps.Free;
  AProgress.Free;
end;

