summaryrefslogtreecommitdiff
path: root/plugins/ImportTXT/importtxtdlg.pas
blob: 0fafe155c14d976ce1145d5ab5395dba97894efd (plain)
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
unit ImportTxtDlg;

interface

uses
  m_api,
  Windows, Messages, SysUtils, CommDlg, IniFiles,
  ImportThrd, general, ImportTU, FileDlgs;


{$I ImpDlgRes.inc}

function IDMainWndProc(Dialog:HWnd; hMessage:uint;wParam:WPARAM;lParam:LPARAM):lresult; stdcall;

implementation

{$R ImpTxtDlg.res}

type
  TImpTxtDlgData = record
    cbSize: integer;
    CanStart: boolean;
    ChoFile: boolean;
    ChoPatt: boolean;
    PattFError: boolean;
    ITStarted: boolean;
    ITFinished: boolean;
    StartTime: TDateTime;
    FinishTime: TDateTime;
    IndexOfPattern: integer;
    oFN: integer;
    hContact: THandle;
    ImportThrd: TImportThrd;
  end;

type PImpTxtDlgData = ^TImpTxtDlgData;

var ITDD:TImpTxtDlgData;

procedure AddStatusMessage(hDLG:HWND; msg:WideString);
begin
  SendDlgItemMessageW(hDLG,IDC_STATUSLIST,LB_SETCURSEL,
    SendDlgItemMessageW(hDLG,IDC_STATUSLIST,LB_ADDSTRING,0,lparam(PWideChar(msg))),0);
end;

procedure FillComboBox(hDlg:hWnd);  //Заполняем список именами шаблонов
var
  i:integer;
begin
  ITDD.PattFError:=true;
  ReadPatterns;
  SendDlgItemMessage(hDLG, IDC_TYPECOMBO, CB_RESETCONTENT, 0, 0);
  if Length(TxtPatterns) > 0 then
  begin
    For i := 0 to High(TxtPatterns) do
      SendDlgItemMessage(hDLG, IDC_TYPECOMBO, CB_ADDSTRING, 0, lparam(PChar(TxtPatterns[i].Name)));
    ITDD.PattFError := false;
  end
  else
  begin
    AddStatusMessage(hDLG, TranslateWideString('No Files Patterns Found'));
    EnableWindow(GetDlgItem(hDLG, IDC_TYPECOMBO), false);
    ITDD.ChoPatt := false;
  end;
end;

procedure MStart(hDLG: HWnd);    //проверяем всё ли сделано чтоб начать импорт
begin
  with ITDD do
  begin
    CanStart := ChoFile and ChoPatt and (not PattFError);
    EnableWindow(GetDlgItem(hDLG, IDSTART), CanStart);
  end;
end;

function IDMainWndProc(Dialog:HWnd; hMessage:uint;wParam:WPARAM;lParam:LPARAM):lresult; stdcall;
//Процедура окна диалога
var
  s:WideString;
  tempwstr:PWideChar;
  H,Mi,sec,ms:word;

begin
 result:= 0;
  case hMessage of
    WM_DESTROY:
      begin
        if ITDD.ImportThrd <> Nil then
          ITDD.ImportThrd.Terminate;
        SetLastPattern(SendDlgItemMessage(Dialog, IDC_TYPECOMBO, CB_GETCURSEL, 0, 0));
      end;

    WM_INITDIALOG:
      begin
        TranslateDialogDefault(Dialog);
        tempwstr := PWideChar(CallService(MS_CLIST_GETCONTACTDISPLAYNAME,lParam,GCDNF_UNICODE));
        s := WideFormat(TranslateWideString('Import history to %s (%s)'), [tempwstr, GetContactID(lParam)]);
        SetWindowTextW(Dialog, PWideChar(s));
        SendMessage(Dialog, WM_SETICON, ICON_SMALL, LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DEFAULT)));
        FillChar(ITDD, SizeOf(TImpTxtDlgData), 0);
        with ITDD do
        begin
          cbSize := SizeOf(TImpTxtDlgData);
          hContact := lParam;
          CanStart := false;
          ChoFile := false;
          ChoPatt := true;
          PattFError := false;
          ITStarted := false;
          ITFinished := false;
        end;

        FillComboBox(Dialog);
        SendDlgItemMessage(Dialog, IDC_TYPECOMBO, CB_SETCURSEL, GetLastPattern, 0);
        ITDD.IndexOfPattern := GetLastPattern;
        MStart(Dialog);
        CheckDlgButton(Dialog, IDC_CHKDUP, BST_CHECKED);
        CheckForDuplicates := true;
        CheckDlgButton(Dialog, IDC_SHOWDUP, BST_UNCHECKED);
        CheckForDuplicates := false;
        ShowWindow(Dialog, SW_NORMAL);
      end;

    WM_COMMAND:
      begin
        if (HiWord(wParam) = CBN_SELCHANGE) and (LoWord(wParam) = IDC_TYPECOMBO) then // Сделали выбор шаблона
        begin
          ITDD.ChoPatt := true;
          ITDD.IndexOfPattern := SendDlgItemMessage(Dialog, IDC_TYPECOMBO, CB_GETCURSEL, 0, 0);
          MStart(Dialog);
        end;

        if (HiWord(wParam) = BN_CLICKED) and (LoWord(wParam) = IDC_CHKDUP) then
          EnableWindow(GetDlgItem(Dialog, IDC_SHOWDUP),
            IsDlgButtonChecked(Dialog, IDC_CHKDUP)<>BST_UNCHECKED);

        case LoWord(wParam) of
          IDCLOSE:
            DestroyWindow(Dialog);
          IDC_BRWSBTN:
            begin // Нажали кнопку "..."
              s := OpenDialogExecute(Dialog, 0, ITDD.oFN,
                TxtPatterns[ITDD.IndexOfPattern].DefExtension);
              ITDD.ChoFile := s <> '';
              SendDlgItemMessageW(Dialog, IDC_FILENAME, WM_SETTEXT, 0, tlparam(PWideChar(s)));
              MStart(Dialog);
            end;
          IDSTART:
            begin // Старт
              with ITDD do
                if CanStart then
                begin
                  EnableWindow(GetDlgItem(Dialog, IDSTART), false);
                  EnableWindow(GetDlgItem(Dialog, IDCLOSE), false);
                  EnableWindow(GetDlgItem(Dialog, IDC_TYPECOMBO), false);
                  EnableWindow(GetDlgItem(Dialog, IDC_BRWSBTN), false);
                  ImportThrd := TImportThrd.Create(true);
                  ImportThrd.FreeOnTerminate := true;
                  ImportThrd.DContact.hContact := hContact;
                  CheckForDuplicates := LongBool(IsDlgButtonChecked(Dialog, IDC_CHKDUP));
                  ShowDuplicates := LongBool(IsDlgButtonChecked(Dialog, IDC_SHOWDUP));
                  H := SendDlgItemMessageW(Dialog, IDC_FILENAME, WM_GETTEXTLENGTH, 0, 0) + 1;
                  SetLength(s, H);
                  SendDlgItemMessageW(Dialog, IDC_FILENAME, WM_GETTEXT, H, tlparam(PWideChar(s)));
                  ImportThrd.FileNames := s + #0 + #0;
                  ImportThrd.OffsetFileName := oFN;
                  ImportThrd.WorkPattern := TxtPatterns[IndexOfPattern];
                  ImportThrd.ParentHWND := Dialog;
                  ImportThrd.Resume;
                end;
            end;
        end;
      end;
    WM_CLOSE:
      begin
        DestroyWindow(Dialog);
      end;
    // Далее оброботка сообщений от потока импорта
    // Начали
    ITXT_THREAD_ALLSTARTED:
      begin
        ITDD.ITStarted := true;
        ITDD.StartTime := Time;
      end;
    ITXT_THREAD_START:
      begin
        AddStatusMessage(Dialog, TranslateWideString('Import started...'));
      end;
    // Известна длинна файла выставляем диапазон прогрессбара
    ITXT_THREAD_MAXPROGRESS:
      SendDlgItemMessage(Dialog, IDC_PROGRESS, PBM_SETRANGE, 0, MakeLParam(0, lParam));
    // Идет прогресс ...
    ITXT_THREAD_PROGRESS:
      SendDlgItemMessage(Dialog, IDC_PROGRESS, PBM_SETPOS, wParam, 0);
    // Возникла ошибка
    ITXT_THREAD_ERROR:
      AddStatusMessage(Dialog, TranslateWideString(PWideChar(wParam)));
    // Закончили
    ITXT_THREAD_FINISH:
      begin
        ITDD.ITFinished := true;
        ITDD.FinishTime := Time;
        DecodeTime(ITDD.FinishTime - ITDD.StartTime, H, Mi, sec, ms);
        AddStatusMessage(Dialog, WideFormat(TranslateWideString('Added: %d messages'), [wParam]));
        AddStatusMessage(Dialog, WideFormat(TranslateWideString('Duplicates: %d messages'), [lParam]));
        AddStatusMessage(Dialog, WideFormat(TranslateWideString('In: %d:%d:%d'), [H, Mi, sec]));
        AddStatusMessage(Dialog, '');
        SendDlgItemMessageW(Dialog, IDC_FILENAME, WM_SETTEXT, 0, 0);
        ITDD.ChoFile := false;
        EnableWindow(GetDlgItem(Dialog, IDC_TYPECOMBO), true);
        EnableWindow(GetDlgItem(Dialog, IDC_BRWSBTN), true);
        EnableWindow(GetDlgItem(Dialog, IDCLOSE), true);
      end;
    // начали новый файл
    ITXT_THREAD_START_FILE:
      AddStatusMessage(Dialog, WideFormat(TranslateWideString('File: %s'), [PWideChar(wParam)]));
    // определили контакт
    ITXT_THREAD_DEST_CONTACT:
      begin
        s := GetContactID(wParam, '', true) + ' | ' + GetContactNick(wParam, '', true);

        AddStatusMessage(Dialog, WideFormat(TranslateWideString('To: %s'), [s]));
      end;
  end;
end;

end.