-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJobItem.cpp
More file actions
415 lines (357 loc) · 8.72 KB
/
Copy pathJobItem.cpp
File metadata and controls
415 lines (357 loc) · 8.72 KB
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
#include "JobItem.h"
#include <tchar.h>
#include <stdio.h>
const PCTSTR JobItem::state_text[] = {TEXT("キュー"),TEXT("接続中"),TEXT("転送中"),TEXT("停止中"),TEXT("エラー"),TEXT("転送エラー"),TEXT("完了"),TEXT("問い合わせ中"),TEXT("中止")};
const PCTSTR JobItem::type_text[] = {TEXT("ダウンロード"),TEXT("アップロード"),TEXT("アップロード応答")};
JobItem::JobItem(IBackgroundCopyManager *mgr,const GUID *id)
{
HRESULT Result;
//::memcpy(&job_id,id,sizeof(GUID));
//::memcpy_s(&job_id, sizeof(this->job_id), id, sizeof(GUID));
this->job_id = *id;
this->guid_to_str();
this->item_job = nullptr;
Result = mgr->GetJob(*id, &this->item_job);
if(this->item_job == nullptr)
{
throw Result;
}
this->item_job_2 = nullptr;
this->item_job_3 = nullptr;
this->item_job_4 = nullptr;
this->item_job_5 = nullptr;
// 追加された機能を使えるように、QueryInterfaceを呼び出す
if(this->item_job->QueryInterface(IID_IBackgroundCopyJob5, (void**)&this->item_job_5) != S_OK)
{
if(this->item_job->QueryInterface(IID_IBackgroundCopyJob4, (void**)&this->item_job_4) != S_OK)
{
if(this->item_job->QueryInterface(IID_IBackgroundCopyJob3, (void**)&this->item_job_3) != S_OK)
{
this->item_job->QueryInterface(IID_IBackgroundCopyJob2, (void**)&this->item_job_2);
}
}
}
name = nullptr;
type = BG_JOB_TYPE_DOWNLOAD;
state = BG_JOB_STATE_QUEUED;
update_time = 0;
}
JobItem::~JobItem()
{
if(this->item_job_5)
{
this->item_job_5->Release();
}
if(this->item_job_4)
{
this->item_job_4->Release();
}
if(this->item_job_3)
{
this->item_job_3->Release();
}
if(this->item_job_2)
{
this->item_job_2->Release();
}
item_job->Release();
if(name)
{
delete[] name;
}
//::OutputDebugString(TEXT("~JobItem()\n"));
}
void JobItem::UpdateStatus()
{
size_t chars;
PWSTR ole_str;
BG_JOB_STATE jstate;
BG_JOB_TYPE jtype;
BG_JOB_PROGRESS jprogress;
::QueryPerformanceCounter((LARGE_INTEGER*)&this->update_time);
ole_str = nullptr;
this->item_job->GetDisplayName(&ole_str);
if(ole_str)
{
chars = ::wcslen(ole_str);
if(name)
{
delete[] name;
}
#ifdef UNICODE
chars++;
name = new wchar_t[chars];
::wcscpy_s(name, chars, ole_str);
#else
chars = WideCharToMultiByte(CP_ACP, 0, ole_str, -1, NULL, 0, NULL, NULL);
name = new char[chars];
WideCharToMultiByte(CP_ACP, 0, ole_str, -1, name, chars, NULL, NULL);
#endif
::CoTaskMemFree(ole_str);
}
this->item_job->GetState(&jstate);
this->item_job->GetType(&jtype);
this->item_job->GetProgress(&jprogress);
if(jstate != this->state)
{
this->state = jstate;
this->update_flags[2] = true;
}
if(jtype != this->type)
{
this->type = jtype;
this->update_flags[3] = true;
}
if(jprogress.BytesTotal != this->total_bytes)
{
this->total_bytes = jprogress.BytesTotal;
if((INT64)this->total_bytes != (INT64)-1)
{
::_stprintf_s(this->num_total, 32, _T("%llu"), this->total_bytes);
}
else
{
::_tcscpy_s(this->num_total, 32, _T("不明"));
}
this->update_flags[4] = true;
}
if(jprogress.BytesTransferred != this->complete_bytes)
{
this->complete_bytes = jprogress.BytesTransferred;
::_stprintf_s(this->num_complete, 32, _T("%llu"), this->complete_bytes);
this->update_flags[5] = true;
}
}
UINT64 JobItem::GetUpdateTime()
{
return this->update_time;
}
void JobItem::GetJobID(GUID *id)
{
//::memcpy(id, &job_id, sizeof(GUID));
*id = job_id;
}
bool JobItem::IsInfoUpdated(int n)
{
bool r;
if((n < 0) || (n >= 6))
{
return false;
}
r = this->update_flags[n];
this->update_flags[n] = false;
return r;
}
unsigned char JobItem::GetJobInterfaceVersion()
{
unsigned char r;
if(this->item_job_5)
{
r = 5;
}
else if(this->item_job_4)
{
r = 4;
}
else if(this->item_job_3)
{
r = 3;
}
else if(this->item_job_2)
{
r = 2;
}
else
{
r = 1;
}
return r;
}
BG_JOB_PRIORITY JobItem::JobGetPriority()
{
BG_JOB_PRIORITY p;
p = (BG_JOB_PRIORITY)0;
this->item_job->GetPriority(&p);
return p;
}
ULONG JobItem::JobGetMinimumRetryDelay()
{
ULONG p;
p = 0;
this->item_job->GetMinimumRetryDelay(&p);
return p;
}
ULONG JobItem::JobGetNoProgressTimeout()
{
ULONG p;
p = 0;
this->item_job->GetNoProgressTimeout(&p);
return p;
}
void JobItem::GetDispInfo(NMLVDISPINFO *info)
{
if(info->item.mask & (LVIF_PARAM | LVIF_TEXT))
{
switch(info->item.iSubItem)
{
case 0: // ジョブ名
info->item.pszText = ((JobItem*)info->item.lParam)->name;
break;
case 1: // GUID
info->item.pszText = ((JobItem*)info->item.lParam)->job_id_str;
break;
case 2: // 状態
info->item.pszText = (LPTSTR)state_text[((JobItem*)info->item.lParam)->state];
break;
case 3: // モード
info->item.pszText = (LPTSTR)type_text[((JobItem*)info->item.lParam)->type];
break;
case 4: // ファイルサイズ
info->item.pszText = ((JobItem*)info->item.lParam)->num_total;
break;
case 5: // 転送済みサイズ
info->item.pszText = ((JobItem*)info->item.lParam)->num_complete;
break;
}
}
}
void JobItem::DrawListItem(JobList *lv,const DRAWITEMSTRUCT *draw_info)
{
//HGDIOBJ old;
HWND h;
#if 0
HICON icon;
#endif
int y;
int sdc;
RECT drect, trect, pbar_rect;
LVITEM item;
//HDITEM hi;
PTSTR item_text;
unsigned char header_index;
bool isfocus;
int tc, bc;
double rate;
sdc = ::SaveDC(draw_info->hDC);
h = lv->GetWindow();
isfocus = (GetFocus() == h);
//CopyRect(&drect,&draw_info->rcItem);
y = (draw_info->rcItem.bottom - draw_info->rcItem.top) / 2;
if(draw_info->itemState & ODS_SELECTED)
{
if(isfocus)
{
bc = COLOR_HIGHLIGHT;
tc = COLOR_HIGHLIGHTTEXT;
}
else
{
bc = COLOR_BTNFACE;
tc = COLOR_BTNTEXT;
}
}
else
{
bc = COLOR_WINDOW;
tc = COLOR_WINDOWTEXT;
}
ListView_GetSubItemRect(h, draw_info->itemID, 5, LVIR_LABEL, &pbar_rect);
::CopyRect(&drect, &draw_info->rcItem);
drect.right = pbar_rect.left;
::FillRect(draw_info->hDC, &drect, ::GetSysColorBrush(bc));
::SetTextColor(draw_info->hDC, ::GetSysColor(tc));
item_text = new TCHAR[1024];
for(header_index = 0; header_index < 5; header_index++)
{
item.mask = LVIF_TEXT;
item.iItem = draw_info->itemID;
item.iSubItem = header_index;
item.pszText = item_text;
item.cchTextMax = 1024;
ListView_GetItem(h, &item);
if(!header_index)
{
ListView_GetItemRect(h, draw_info->itemID, &drect, LVIR_LABEL);
}
else
{
ListView_GetSubItemRect(h, draw_info->itemID, header_index, LVIR_LABEL, &drect);
}
::CopyRect(&trect, &drect);
::InflateRect(&trect, -1, 0);
::DrawText(draw_info->hDC, item_text, -1, &trect, DT_LEFT | DT_NOPREFIX | DT_SINGLELINE | DT_END_ELLIPSIS | DT_VCENTER);
}
::CopyRect(&drect, &pbar_rect);
::CopyRect(&trect, &pbar_rect);
item.mask = LVIF_TEXT;
item.iItem = draw_info->itemID;
item.iSubItem = header_index;
item.pszText = item_text;
item.cchTextMax = 1024;
ListView_GetItem(h,&item);
if((((JobItem*)draw_info->itemData)->total_bytes) && (((JobItem*)draw_info->itemData)->total_bytes != BG_SIZE_UNKNOWN))
{
rate = (double)((JobItem*)draw_info->itemData)->complete_bytes / (double)((JobItem*)draw_info->itemData)->total_bytes;
drect.right = drect.left + (LONG)((drect.right - drect.left) * rate);
JobItem::fill_gradiant(draw_info->hDC, &drect, 0xffffff, 0xc0ffc0);
drect.left = drect.right;
drect.right = trect.right;
}
JobItem::fill_gradiant(draw_info->hDC, &drect, 0xffffff, 0xc0c0c0);
::SetTextColor(draw_info->hDC, GetSysColor(COLOR_WINDOWTEXT));
::InflateRect(&trect, -1, 0);
::DrawText(draw_info->hDC, item_text, -1, &trect, DT_LEFT | DT_NOPREFIX | DT_SINGLELINE | DT_END_ELLIPSIS | DT_VCENTER);
delete [] item_text;
::RestoreDC(draw_info->hDC, sdc);
if(isfocus && (draw_info->itemState & ODS_FOCUS))
{
::CopyRect(&drect, &draw_info->rcItem);
drect.right = pbar_rect.left;
::DrawFocusRect(draw_info->hDC, &drect);
}
}
void JobItem::guid_to_str()
{
TCHAR hexb[4];
unsigned char i;
::_stprintf_s(job_id_str, 37, _T("%08X-%04hX-%04hX-"), job_id.Data1, job_id.Data2, job_id.Data3);
for(i = 0; i < 8; i++)
{
::_stprintf_s(hexb, 4, _T("%02X"), job_id.Data4[i]);
::_tcscat_s(job_id_str, 37, hexb);
}
}
#if 0
PTSTR JobItem::tstrdup(PCTSTR s)
{
TCHAR *nstr;
size_t l;
if(l = _tcslen(s))
{
l++;
nstr = new TCHAR[l];
_tcscpy_s(nstr,l,s);
}
else
nstr = NULL;
return nstr;
}
#endif
void JobItem::fill_gradiant(HDC dc,const RECT *r,COLORREF start,COLORREF end)
{
const static GRADIENT_RECT gr = {0,1};
TRIVERTEX v[2];
v[0].x = r->left;
v[0].y = r->top;
v[0].Red = GetRValue(start) << 8;
v[0].Green = GetGValue(start) << 8;
v[0].Blue = GetBValue(start) << 8;
v[0].Alpha = 0xff00;
v[1].x = r->right;
v[1].y = r->bottom;
v[1].Red = GetRValue(end) << 8;
v[1].Green = GetGValue(end) << 8;
v[1].Blue = GetBValue(end) << 8;
v[1].Alpha = 0xff00;
::GradientFill(dc, v, 2, (GRADIENT_RECT*)&gr, 1, GRADIENT_FILL_RECT_V);
}