summaryrefslogtreecommitdiff
path: root/plugins/ImportTXT/importtxtdlg.pas
blob: 84f1c3a6cfe7adf1d0031910cb05bd15175d1c70 (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
228
229
230
231
232
233
unit ImportTxtDlg;

interface

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


{$I ImpDlgRes.inc}


function IDMainWndProc(Dialog:HWnd; hMessage,wParam,lParam:DWord):integer; stdcall;
function GetContactDisplayName(hContact:THandle):string;

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;

function GetContactDisplayName(hContact:THandle):string;
begin
 Result := PChar(pluginLink^.CallService(MS_CLIST_GETCONTACTDISPLAYNAME,hContact,0));
end;

procedure AddStatusMessage(hDLG:HWND; msg:WideString);
begin
  SendDlgItemMessageW(hDLG,IDC_STATUSLIST,LB_SETCURSEL,SendDlgItemMessageW(hDLG,IDC_STATUSLIST,LB_ADDSTRING,0,dword(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 PatternsCount>0 then
  begin
   For i:=0 to PatternsCount-1 do
    SendDlgItemMessage(hDlg,IDC_TYPECOMBO,CB_ADDSTRING,0,Integer(PatternNames[i]));
   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
if ChoFile and ChoPatt and (not PattFError)  then
           begin
          CanStart:=true;
          EnableWindow(GetDlgItem(hDlg, IDSTART), TRUE);
           end
                    else
           begin
          CanStart:=false;
          EnableWindow(GetDlgItem(hDlg, IDSTART), FALSE);
           end;
end;
end;


function IDMainWndProc(Dialog:HWnd; hMessage,wParam,lParam:DWord):integer; stdcall;
//Процедура окна диалога
var
    s:WideString;
    tempwstr:PWideChar;

var 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:=ANSIToWide(PChar(GetContactDisplayName(lParam)),tempwstr,cp);
    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
     if LongBool(IsDlgButtonChecked(Dialog,IDC_CHKDUP)) then EnableWindow(GetDlgItem(Dialog, IDC_SHOWDUP), TRUE)
                                              else EnableWindow(GetDlgItem(Dialog, IDC_SHOWDUP), FALSE);
    case loword(wParam) of
      IDCLOSE:     DestroyWindow(Dialog);
      IDC_BRWSBTN: begin    //Нажали кнопку "..."
          s:=OpenDialogExecute(Dialog,0,ITDD.oFN,TxtPatterns[ITDD.IndexOfPattern].DefExtension);
          if s<>'' then ITDD.ChoFile:=true
                   else ITDD.ChoFile:=false;
          SendDlgItemMessageW(Dialog, IDC_FILENAME,WM_SETTEXT,0,dword(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,dword(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
     if IsMirandaUnicode then
       begin
      tempwstr:=UTF8toWide(PChar(GetContactID(wParam,'',true)),tempwstr);
      s:= tempwstr;
      tempwstr:=UTF8toWide(PChar(GetContactNick(wParam,'',true)),tempwstr);
      s:=s +  ' | ' + tempwstr;
       end
                         else
      s:=GetContactID(wParam,'',true)+' | '+GetContactNick(wParam,'',true);

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

end.