| 1 | unit FormCompare;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, DataFile,
|
|---|
| 7 | VCardFile, VCard, Common, RegistryEx, FormEx;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 |
|
|---|
| 11 | { TFormCompare }
|
|---|
| 12 |
|
|---|
| 13 | TFormCompare = class(TFormEx)
|
|---|
| 14 | ButtonBrowse: TButton;
|
|---|
| 15 | ButtonCancel: TButton;
|
|---|
| 16 | ButtonCompare: TButton;
|
|---|
| 17 | CheckBoxNormalizePhoneNumbers: TCheckBox;
|
|---|
| 18 | CheckBoxRemoveExactDuplicates: TCheckBox;
|
|---|
| 19 | CheckBoxSortContacts: TCheckBox;
|
|---|
| 20 | CheckBoxWithoutPhotos: TCheckBox;
|
|---|
| 21 | EditAnotherFile: TEdit;
|
|---|
| 22 | Label1: TLabel;
|
|---|
| 23 | OpenDialog1: TOpenDialog;
|
|---|
| 24 | ScrollBox1: TScrollBox;
|
|---|
| 25 | procedure ButtonBrowseClick(Sender: TObject);
|
|---|
| 26 | procedure ButtonCompareClick(Sender: TObject);
|
|---|
| 27 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
|---|
| 28 | procedure FormCreate(Sender: TObject);
|
|---|
| 29 | procedure FormDestroy(Sender: TObject);
|
|---|
| 30 | procedure FormShow(Sender: TObject);
|
|---|
| 31 | private
|
|---|
| 32 | LeftVCard: TVCardFile;
|
|---|
| 33 | RightVCard: TVCardFile;
|
|---|
| 34 | procedure CompareInternal;
|
|---|
| 35 | procedure CompareExternal;
|
|---|
| 36 | procedure LoadConfig;
|
|---|
| 37 | procedure SaveConfig;
|
|---|
| 38 | end;
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | implementation
|
|---|
| 42 |
|
|---|
| 43 | {$R *.lfm}
|
|---|
| 44 |
|
|---|
| 45 | uses
|
|---|
| 46 | Core, FormCompareSideBySide, VCardProcessor;
|
|---|
| 47 |
|
|---|
| 48 | resourcestring
|
|---|
| 49 | SCompareToolNotFound = 'Compare tool ''%s'' not found. Select valid compare tool in the application settings.';
|
|---|
| 50 |
|
|---|
| 51 | { TFormCompare }
|
|---|
| 52 |
|
|---|
| 53 | procedure TFormCompare.FormCreate(Sender: TObject);
|
|---|
| 54 | begin
|
|---|
| 55 | LeftVCard := TVCardFile.Create(nil);
|
|---|
| 56 | RightVCard := TVCardFile.Create(nil);
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | procedure TFormCompare.FormDestroy(Sender: TObject);
|
|---|
| 60 | begin
|
|---|
| 61 | FreeAndNil(LeftVCard);
|
|---|
| 62 | FreeAndNil(RightVCard);
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | procedure TFormCompare.FormClose(Sender: TObject; var CloseAction: TCloseAction
|
|---|
| 66 | );
|
|---|
| 67 | begin
|
|---|
| 68 | SaveConfig;
|
|---|
| 69 | end;
|
|---|
| 70 |
|
|---|
| 71 | procedure TFormCompare.ButtonCompareClick(Sender: TObject);
|
|---|
| 72 | var
|
|---|
| 73 | VCardProcessor: TVCardProcessor;
|
|---|
| 74 | begin
|
|---|
| 75 | LeftVCard.Assign(TVCardFile(Core.Core.DataFile));
|
|---|
| 76 | RightVCard.LoadFromFile(EditAnotherFile.Text);
|
|---|
| 77 |
|
|---|
| 78 | VCardProcessor := TVCardProcessor.Create(nil);
|
|---|
| 79 | with VCardProcessor do
|
|---|
| 80 | try
|
|---|
| 81 | DefaultPhoneCountryCode := Core.Core.DefaultPhoneCountryCode;
|
|---|
| 82 | RemovePhotos := CheckBoxWithoutPhotos.Checked;
|
|---|
| 83 | RemovePhoneSpaces := CheckBoxNormalizePhoneNumbers.Checked;
|
|---|
| 84 | AddDefaultPhoneCountryPrefix := CheckBoxNormalizePhoneNumbers.Checked;
|
|---|
| 85 | RemoveExactDuplicates := CheckBoxRemoveExactDuplicates.Checked;
|
|---|
| 86 | Order := CheckBoxSortContacts.Checked;
|
|---|
| 87 | Process(LeftVCard.VCard);
|
|---|
| 88 | Process(RightVCard.VCard);
|
|---|
| 89 | finally
|
|---|
| 90 | Free;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | CompareExternal;
|
|---|
| 94 | end;
|
|---|
| 95 |
|
|---|
| 96 | procedure TFormCompare.ButtonBrowseClick(Sender: TObject);
|
|---|
| 97 | var
|
|---|
| 98 | TempFile: TDataFile;
|
|---|
| 99 | begin
|
|---|
| 100 | TempFile := Core.Core.DefaultDataFileClass.Create(nil);
|
|---|
| 101 | try
|
|---|
| 102 | OpenDialog1.Filter := TempFile.GetFileFilter;
|
|---|
| 103 | finally
|
|---|
| 104 | TempFile.Free;
|
|---|
| 105 | end;
|
|---|
| 106 |
|
|---|
| 107 | OpenDialog1.DefaultExt := '';
|
|---|
| 108 | OpenDialog1.InitialDir := ExtractFileDir(EditAnotherFile.Text);
|
|---|
| 109 | OpenDialog1.FileName := ExtractFileName(EditAnotherFile.Text);
|
|---|
| 110 | OpenDialog1.Options := OpenDialog1.Options - [ofAllowMultiSelect];
|
|---|
| 111 | if OpenDialog1.Execute then begin
|
|---|
| 112 | EditAnotherFile.Text := OpenDialog1.FileName;
|
|---|
| 113 | end;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure TFormCompare.FormShow(Sender: TObject);
|
|---|
| 117 | begin
|
|---|
| 118 | LoadConfig;
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | procedure TFormCompare.CompareInternal;
|
|---|
| 122 | var
|
|---|
| 123 | TempFileName: string;
|
|---|
| 124 | begin
|
|---|
| 125 | with TFormCompareSideBySide.Create(nil) do
|
|---|
| 126 | try
|
|---|
| 127 | TempFileName := Core.Core.GetTempDir +
|
|---|
| 128 | DirectorySeparator + 'Compare' + VCardFileExt;
|
|---|
| 129 | ForceDirectories(ExtractFileDir(TempFileName));
|
|---|
| 130 | TVCardFile(Core.Core.DataFile).SaveToFile(TempFileName);
|
|---|
| 131 | LoadFileLeft(TempFileName);
|
|---|
| 132 | LoadFileRight(EditAnotherFile.Text);
|
|---|
| 133 | ShowModal;
|
|---|
| 134 | finally
|
|---|
| 135 | Free;
|
|---|
| 136 | end;
|
|---|
| 137 | end;
|
|---|
| 138 |
|
|---|
| 139 | procedure CompareText(TextLeft, TextRight: string; FileNameLeft: string = 'FileLeft.txt';
|
|---|
| 140 | FileNameRight: string = 'FileRight.txt');
|
|---|
| 141 | var
|
|---|
| 142 | TempFileRight: string;
|
|---|
| 143 | TempFileLeft: string;
|
|---|
| 144 | begin
|
|---|
| 145 | if not DirectoryExists(Core.Core.GetTempDir) then
|
|---|
| 146 | CreateDir(Core.Core.GetTempDir);
|
|---|
| 147 | TempFileLeft := Core.Core.GetTempDir + DirectorySeparator + FileNameLeft;
|
|---|
| 148 | TempFileRight := Core.Core.GetTempDir + DirectorySeparator + FileNameRight;
|
|---|
| 149 | SaveStringToFile(TextLeft, TempFileLeft);
|
|---|
| 150 | SaveStringToFile(TextRight, TempFileRight);
|
|---|
| 151 | if FileExists(Core.Core.CompareTool) then
|
|---|
| 152 | ExecuteProgram(Core.Core.CompareTool, [TempFileLeft, TempFileRight], [])
|
|---|
| 153 | else ShowMessage(Format(SCompareToolNotFound, [Core.Core.CompareTool]));
|
|---|
| 154 | end;
|
|---|
| 155 |
|
|---|
| 156 | procedure TFormCompare.CompareExternal;
|
|---|
| 157 | begin
|
|---|
| 158 | CompareText(LeftVCard.VCard.AsString, RightVCard.VCard.AsString,
|
|---|
| 159 | ExtractFileName(LeftVCard.FileName), ExtractFileName(EditAnotherFile.Text));
|
|---|
| 160 | end;
|
|---|
| 161 |
|
|---|
| 162 | procedure TFormCompare.LoadConfig;
|
|---|
| 163 | begin
|
|---|
| 164 | with TRegistryEx.Create do
|
|---|
| 165 | try
|
|---|
| 166 | CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
|
|---|
| 167 | EditAnotherFile.Text := ReadStringWithDefault('LastCompareFileName', '');
|
|---|
| 168 | CheckBoxWithoutPhotos.Checked := ReadBoolWithDefault('WithoutPhotos', True);
|
|---|
| 169 | CheckBoxSortContacts.Checked := ReadBoolWithDefault('SortContacts', True);
|
|---|
| 170 | CheckBoxNormalizePhoneNumbers.Checked := ReadBoolWithDefault('NormalizePhoneNumbers', True);
|
|---|
| 171 | CheckBoxRemoveExactDuplicates.Checked := ReadBoolWithDefault('RemoveExactDuplicates', True);
|
|---|
| 172 | finally
|
|---|
| 173 | Free;
|
|---|
| 174 | end;
|
|---|
| 175 | end;
|
|---|
| 176 |
|
|---|
| 177 | procedure TFormCompare.SaveConfig;
|
|---|
| 178 | begin
|
|---|
| 179 | with TRegistryEx.Create do
|
|---|
| 180 | try
|
|---|
| 181 | CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
|
|---|
| 182 | WriteString('LastCompareFileName', EditAnotherFile.Text);
|
|---|
| 183 | WriteBool('WithoutPhotos', CheckBoxWithoutPhotos.Checked);
|
|---|
| 184 | WriteBool('SortContacts', CheckBoxSortContacts.Checked);
|
|---|
| 185 | WriteBool('NormalizePhoneNumbers', CheckBoxNormalizePhoneNumbers.Checked);
|
|---|
| 186 | WriteBool('RemoveExactDuplicates', CheckBoxRemoveExactDuplicates.Checked);
|
|---|
| 187 | finally
|
|---|
| 188 | Free;
|
|---|
| 189 | end;
|
|---|
| 190 | end;
|
|---|
| 191 |
|
|---|
| 192 | end.
|
|---|
| 193 |
|
|---|