File: unit1.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (456 lines) | stat: -rw-r--r-- 11,262 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
  ExtCtrls, Buttons, Menus, LCLIntf, LazFileUtils, PotFile;

type

  { TForm1 }

  TForm1 = class(TForm)
    chkSearchPoItemIgnoreUnit: TCheckBox;
    chkSearchPoItemIdText: TCheckBox;
    chkSearchPoItemFullWord: TCheckBox;
    edSearchPoItems: TEdit;
    lbPoFiles: TListBox;
    lbPoItems: TListBox;
    memInfo: TMemo;
    mnDupIgnCase: TMenuItem;
    mnDupIgnoreSpaceDiff: TMenuItem;
    OpenDialog1: TOpenDialog;
    Panel1: TPanel;
    pnlSearch: TPanel;
    btnClearSearchPoItem: TSpeedButton;
    popMoveTo: TPopupMenu;
    popDupFinder: TPopupMenu;
    Splitter1: TSplitter;
    Splitter2: TSplitter;
    Timer1: TTimer;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    tbMovePoItems: TToolButton;
    tbSave: TToolButton;
    tbDupId: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    tbShowMsgIdEntries: TToolButton;
    tbShowPoFileInfo: TToolButton;
    procedure btnClearSearchPoItemClick(Sender: TObject);
    procedure chkSearchPoItemFullWordChange(Sender: TObject);
    procedure edSearchPoItemsChange(Sender: TObject);
    procedure edSearchPoItemsEditingDone(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDropFiles(Sender: TObject; const FileNames: array of string);
    procedure lbPoFilesClick(Sender: TObject);
    procedure lbPoItemsClick(Sender: TObject);
    procedure tbDupIdClick(Sender: TObject);
    procedure tbSaveClick(Sender: TObject);
    procedure tbShowMsgIdEntriesClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    FPotList: TPotFileList;
    FCurPotFile: TPotFile;
    FInbPoFilesClick: Boolean;
    FDupIdPotFile: TPotFile;

    function AddFile(AFileName: String): TPotFile;
    procedure OnMoveClicked(Sender: TObject);
    procedure SetCurrentFile(ACurPotFile: TPotFile);
    procedure UpdateSaveButton;
    procedure UpdatePoItems;
    procedure UpdatePoItemFiles;
    procedure UpdatePotItems;
  public
    destructor Destroy; override;

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.ToolButton1Click(Sender: TObject);
var
  f: TPotFile;
begin
  if OpenDialog1.Execute then begin
    f := AddFile(OpenDialog1.FileName);
    SetCurrentFile(f);
  end;
end;

function TForm1.AddFile(AFileName: String): TPotFile;
var
  m: TMenuItem;
  i: Integer;
begin
  for i := 0 to lbPoFiles.Count - 1 do
    if TPotFile(lbPoFiles.Items.Objects[i]).FileName = AFileName then
      exit;

  Result := TPotFile.Create(AFileName);
  lbPoFiles.AddItem(Format('%s (%d)', [Result.Name, Result.Count]), Result);

  FPotList.Add(Result);

  m := TMenuItem.Create(popMoveTo);
  m.Caption := Result.Name;
  m.Tag := PtrInt(Result);
  m.OnClick := @OnMoveClicked;
  popMoveTo.Items.Add(m);
end;

procedure TForm1.OnMoveClicked(Sender: TObject);
var
  target: TPotFile;
  i, j, e: Integer;
  itm, newItm: TPotSection;
  po, targetPo: TPoFile;
  entry, newEntry: TPoSection;
  prefix: String;
begin
  target := TPotFile(TMenuItem(Sender).Tag);

  prefix := '';
  if target.Count > 1 then
    prefix := target.Items[1].VarNameUnit;
  if prefix = '' then
    prefix := LowerCase(target.Name);
  if not InputQuery('Move items', 'Enter the Unit-name used in the target file:', prefix) then
    exit;

  for i := 0 to lbPoItems.Count - 1 do begin
    if not lbPoItems.Selected[i] then
      continue;

    itm := TPotSection(lbPoItems.Items.Objects[i]);

    newItm := TPotSection.Create;
    newItm.Text := itm.Text;
    newItm.DeleteMsgCtxt;
    newItm.ReplaceUnitPrefix(prefix);
    target.Add(newItm);

    for j := 0 to FCurPotFile.PoFiles.Count - 1 do begin
      po := FCurPotFile.PoFiles[j];
      e := po.IndexOfVar(itm.VarName);
      if e < 0 then
        Continue;

      entry := po.Items[e];
      targetPo := target.PoFiles.PoFileForLang(po.LangName, True);
      newEntry := TPoSection.Create;
      newEntry.Text := entry.Text;
      newEntry.DeleteMsgCtxt;
      newEntry.ReplaceUnitPrefix(prefix);
      targetPo.Add(newEntry);

      po.Remove(entry);
    end;

    FCurPotFile.Remove(itm);
  end;

  FCurPotFile.SetModified;
  target.SetModified;
  UpdateSaveButton;
  UpdatePotItems;
  UpdatePoItems;
end;

procedure TForm1.SetCurrentFile(ACurPotFile: TPotFile);
var
  i: Integer;
begin
  FCurPotFile := ACurPotFile;

  i := lbPoFiles.Items.IndexOfObject(ACurPotFile);
  if i > 0 then
    lbPoFiles.ItemIndex := i;
  UpdatePoItems;

  UpdateSaveButton;

  for i := 0 to popMoveTo.Items.Count - 1 do
    popMoveTo.Items[i].Enabled := popMoveTo.Items[i].Tag <> PtrInt(FCurPotFile);
end;

procedure TForm1.UpdateSaveButton;
begin
  tbSave.Enabled := (FCurPotFile <> nil) and FCurPotFile.Modified;
  if FCurPotFile <> nil then
    tbSave.Caption := format('Save %s', [FCurPotFile.Name]);
end;

procedure TForm1.UpdatePoItems;
  function HasWord(AWord, AText: String): Boolean;
  var
    i: SizeInt;
  begin
    i := Pos(AWord, AText);
    Result := (i > 0) and
      ( (i = 1) or not (AText[i-1] in ['a'..'z', 'A'..'Z', '0'..'9', '_']) ) and
      ( (i + Length(AWord) > Length(AText)) or not (AText[i + Length(AWord)] in ['a'..'z', 'A'..'Z', '0'..'9', '_']) )
      ;
  end;
var
  i, j, CntAll, CntNoneEmpty: Integer;
  itm: TPotSection;
  MatchWords, MatchTxtId, fnd, MatchIgnUName: Boolean;
  t: String;
  FindTerms: TStringArray;
begin
  Timer1.Enabled := False;
  if tbShowPoFileInfo.Down then begin
    UpdatePoItemFiles;
    exit;
  end;

  lbPoItems.Clear;
  if FCurPotFile = nil then
    exit;

  // Start at 1, Item 0 is the header
  t := lowercase(edSearchPoItems.Text);
  FindTerms := t.Split(',');
  for j := Length(FindTerms) - 1 downto 0 do begin
    FindTerms[j] := Trim(FindTerms[j]);
    if FindTerms[j] = '' then
      Delete(FindTerms, j, 1);
  end;
  MatchWords := chkSearchPoItemFullWord.Checked;
  MatchIgnUName := chkSearchPoItemIgnoreUnit.Checked;
  MatchTxtId := chkSearchPoItemIdText.Checked;
  lbPoItems.Items.BeginUpdate;
  for i := 1 to FCurPotFile.Count - 1 do begin
    itm := FCurPotFile.Items[i];

    if Length(FindTerms) > 0 then begin
      fnd := False;
      for j := 0 to Length(FindTerms) - 1 do begin
        if MatchWords then begin
          if MatchIgnUName
          then fnd := HasWord(itm.VarNameIdent, FindTerms[j])
          else fnd := HasWord(itm.VarNameContent, FindTerms[j]);
        end
        else begin
          if MatchIgnUName
          then fnd := Pos(FindTerms[j], itm.VarNameIdent) > 0
          else fnd := Pos(FindTerms[j], itm.VarNameContent) > 0;
        end;
        if fnd then
          break;
        if MatchTxtId then begin
          if MatchWords
          then fnd := HasWord(itm.MsgIdCleanContent, FindTerms[j])
          else fnd := Pos(FindTerms[j], LowerCase(itm.MsgIdCleanContent)) > 0;
          if fnd then
            break;
        end;
      end;

      if not fnd then
        continue;
    end;

    //itm.GetTranslationyCount(CntAll, CntNoneEmpty);
    //lbPoItems.AddItem(Format('%s (%d / %d): %s', [itm.MsgIdContent, CntNoneEmpty, CntAll, itm.VarNameContent]), itm);
    lbPoItems.AddItem(Format('%s (%s)', [itm.MsgIdContent, itm.VarNameContent]), itm);
  end;
  lbPoItems.Items.EndUpdate;

  lbPoItemsClick(nil);
end;

procedure TForm1.UpdatePoItemFiles;
var
  i: Integer;
  po: TPoFile;
begin
  lbPoItems.Clear;
  if FCurPotFile = nil then
    exit;

  for i := 1 to FCurPotFile.PoFiles.Count - 1 do begin
    po := FCurPotFile.PoFiles[i];
    lbPoItems.AddItem(Format('%s (%d / %d)', [po.LangName, po.NoneEmptyCount, po.Count-1]), po);
  end;
end;

procedure TForm1.UpdatePotItems;
var
  i: Integer;
  pot: TPotFile;
begin
  for i := 0 to lbPoFiles.Count - 1 do begin
    pot := TPotFile(lbPoFiles.Items.Objects[i]);
    if pot.Modified
    then lbPoFiles.Items[i] := Format('* %s (%d)', [pot.Name, pot.Count - 1])
    else lbPoFiles.Items[i] := Format('%s (%d)', [pot.Name, pot.Count - 1]);
  end;
end;

destructor TForm1.Destroy;
begin
  inherited Destroy;
  FDupIdPotFile.Free;
  FPotList.Free;
end;

procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string
  );
var
  s: String;
  f: TPotFile;
begin
  f := nil;
  for s in FileNames do
    f := AddFile(s);
  SetCurrentFile(f);
end;

procedure TForm1.btnClearSearchPoItemClick(Sender: TObject);
begin
  if (edSearchPoItems.Text = '') and not Timer1.Enabled then
    exit;
  edSearchPoItems.Clear;
  if tbShowMsgIdEntries.Down then
    UpdatePoItems;
end;

procedure TForm1.chkSearchPoItemFullWordChange(Sender: TObject);
begin
  if (edSearchPoItems.Text = '') and not Timer1.Enabled then
    exit;
  if tbShowMsgIdEntries.Down then
    UpdatePoItems;
end;

procedure TForm1.edSearchPoItemsChange(Sender: TObject);
begin
  if tbShowPoFileInfo.Down then
    exit;
  Timer1.Enabled := False;
  Timer1.Enabled := True;
end;

procedure TForm1.edSearchPoItemsEditingDone(Sender: TObject);
begin
  if tbShowMsgIdEntries.Down then
    UpdatePoItems;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FPotList := TPotFileList.Create;
end;

procedure TForm1.lbPoFilesClick(Sender: TObject);
var
  i: Integer;
begin
  if FInbPoFilesClick then
    exit;

  i := lbPoFiles.ItemIndex;
  if i < 0 then
    exit;
  FInbPoFilesClick := True;
  SetCurrentFile(TPotFile(lbPoFiles.Items.Objects[i]));
  FInbPoFilesClick := False;
end;

procedure TForm1.lbPoItemsClick(Sender: TObject);
var
  i, j, cnt: Integer;
  itm: TPotSection;
  s: String;
  po: TPoFile;
  e: LongInt;
begin
  if not tbShowMsgIdEntries.Down then
    exit;
  memInfo.Clear;
  cnt := 0;
  for i := 0 to lbPoItems.Count - 1 do begin
    if not lbPoItems.Selected[i] then
      continue;

    inc(cnt);
    if not (lbPoItems.Items.Objects[i] is TPotSection) then
      continue;
    itm := TPotSection(lbPoItems.Items.Objects[i]);
    //itmid := itm.MsgId;
    s := itm.VarName + LineEnding;
    s := s + itm.MsgId + LineEnding;
    s := s + itm.MsgStr + LineEnding;
    for j := 0 to FCurPotFile.PoFiles.Count - 1 do begin
      po := FCurPotFile.PoFiles[j];
      e := po.IndexOfVar(itm.VarName);
      if e >= 0 then begin
        s := s + po.LangName + ': ' + po.Items[e].MsgStr + LineEnding;
      end;
    end;

    s := s + LineEnding;
    memInfo.Append(s);
  end;

  tbMovePoItems.Enabled := cnt > 0;
  tbMovePoItems.Caption := format('Move %d items to', [cnt]);
end;

procedure TForm1.tbDupIdClick(Sender: TObject);
var
  fl: TFindDupFlags;
begin
  if FDupIdPotFile = nil then begin
    FDupIdPotFile := TPotFile.Create('', True);
    lbPoFiles.AddItem('Dups', FDupIdPotFile);
  end;

  fl := [];
  if mnDupIgnCase.Checked then
    Include(fl, fdIgnoreCase);
  if mnDupIgnoreSpaceDiff.Checked then
    Include(fl, fdIgnoreSpaceDiff);

  FPotList.FindDuplicateMsgId(FDupIdPotFile, fl);

  UpdatePotItems;
  SetCurrentFile(FDupIdPotFile);
end;

procedure TForm1.tbSaveClick(Sender: TObject);
begin
  if FCurPotFile = nil then
    exit;

  FCurPotFile.Save;
  UpdatePotItems;
  tbSave.Enabled := False;
end;

procedure TForm1.tbShowMsgIdEntriesClick(Sender: TObject);
begin
  UpdatePoItems;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  UpdatePoItems;
end;

end.