{$MODE DELPHI}

Program testmime;

uses
  mimepart, synachar, classes;

type
  Tc = class(TObject)
  public
    class procedure ph(const Sender: TMimePart);
  end;

class procedure Tc.ph(const Sender: TMimePart);
begin
  Sender.DecodePart;
  Sender.EncodePart;
end;

Procedure ProcessParts(const Sender: TMimePart; ThePartCount: integer);

var
  m1: tmimepart;
  MyPart: integer;
  MyPartCount: integer;
//
begin
  writeln;
  writeln(' entering ProcessParts...');
  for MyPart := 1 to ThePartCount do
    begin
      if MyPart > 1 then
        writeln;
      writeln('  allocate m1-',MyPart);
      m1 := tmimepart.create;
      try
        writeln('   assign OnWalkPart-',MyPart);
        m1.OnWalkPart:=tc.ph;
        writeln('   pulling part ',MyPart,' into m1-',MyPart);
        m1:= Sender.GetSubPart(MyPart - 1);
        writeln('   fire DecomposeParts-',MyPart);
        m1.DecomposeParts;
        writeln('   return from DecomposeParts-',MyPart);
        writeln('   fire WalkPart-',MyPart);
        m1.WalkPart;
        writeln('   return from WalkPart-',MyPart);
        MyPartCount := m1.GetSubPartCount;
        writeln('    WP-',MyPart,' Target Charset  : ',m1.TargetCharset);
        writeln('    WP-',MyPart,' Default Charset : ',m1.DefaultCharset);
        writeln('    WP-',MyPart,' GetSubPartCount : ',MyPartCount);
        writeln('    WP-',MyPart,' SubLevel        : ',m1.SubLevel);
        writeln('    WP-',MyPart,' MaxSubLevel     : ',m1.MaxSubLevel);
        writeln('    WP-',MyPart,' Primary         : ',m1.Primary);
        writeln('    WP-',MyPart,' Secondary       : ',m1.Secondary);
        writeln('    WP-',MyPart,' Encoding        : ',m1.Encoding);
        writeln('    WP-',MyPart,' Boundary        : ',m1.Boundary);
        writeln('    WP-',MyPart,' ContentID       : ',m1.ContentID);

      finally
        writeln('   finally');
        writeln('    free m1-',MyPart);
//        m1.free;
        writeln('  done m1-',MyPart);
      end;
    end;
  writeln(' vacating ProcessParts...');
end;

var
  l: tstringlist;
  m: tmimepart;
  PartCount: integer;

begin
  l := TStringList.create;
  m := tmimepart.create;
  try
    m.OnWalkPart:=tc.ph;
    m.Lines.LoadFromFile(paramstr(1));
    writeln('Target Charset     : ',m.TargetCharset);
    m.TargetCharset := GetCPFromID('ISO_8859_1');
    writeln('Target Charset     : ',m.TargetCharset);
    writeln('Default Charset    : ',m.DefaultCharset);
    m.DefaultCharset := 'ISO_8859_1';
    writeln('Default Charset    : ',m.DefaultCharset);
    m.DecomposeParts;
    writeln('DP Target Charset  : ',m.TargetCharset);
    m.TargetCharset := GetCPFromID('ISO_8859_1');
    writeln('DP Target Charset  : ',m.TargetCharset);
    writeln('DP Default Charset : ',m.DefaultCharset);
    m.WalkPart;
    PartCount := m.GetSubPartCount;
    writeln('WP Target Charset  : ',m.TargetCharset);
    writeln('WP Target Charset  : ',m.TargetCharset);
    writeln('WP Default Charset : ',m.DefaultCharset);
    writeln('WP GetSubPartCount : ',PartCount);
    writeln('WP SubLevel        : ',m.SubLevel);
    writeln('WP MaxSubLevel     : ',m.MaxSubLevel);
    writeln('WP Primary         : ',m.Primary);
    writeln('WP Secondary       : ',m.Secondary);
    writeln('WP Encoding        : ',m.Encoding);
    writeln('WP Boundary        : ',m.Boundary);
    writeln('WP ContentID       : ',m.ContentID);

    if m.Primary = 'MULTIPART' then
      ProcessParts(m, PartCount);

    writeln;
    writeln(' fire ComposeParts...');
    m.ComposeParts;
    writeln(' return from ComposeParts...');
    writeln('CP Target Charset  : ',m.TargetCharset);
    writeln('CP Target Charset  : ',m.TargetCharset);
    writeln('CP Default Charset : ',m.DefaultCharset);
    writeln(' fire SaveToFile...');
    m.Lines.SaveToFile(paramstr(1) + '.repack');
    writeln(' return from SaveTofile...');
  finally
    writeln;
    writeln('finally');
    writeln(' free m');
    m.free;
    writeln(' done m');
    writeln(' free l');
    l.free;
    writeln(' done l');
    writeln('done...');
  end;
end.
