gdi32: Move the handling of font family fallbacks out of freetype.c.
[wine/zf.git] / dlls / mstask / task.c
bloba15e0164b43ae9c0eecf241eb0c2717c9847d00c
1 /*
2 * Copyright (C) 2008 Google (Roy Shea)
3 * Copyright (C) 2018 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winternl.h"
27 #include "objbase.h"
28 #include "taskschd.h"
29 #include "mstask.h"
30 #include "mstask_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mstask);
35 typedef struct
37 USHORT product_version;
38 USHORT file_version;
39 UUID uuid;
40 USHORT name_size_offset;
41 USHORT trigger_offset;
42 USHORT error_retry_count;
43 USHORT error_retry_interval;
44 USHORT idle_deadline;
45 USHORT idle_wait;
46 UINT priority;
47 UINT maximum_runtime;
48 UINT exit_code;
49 HRESULT status;
50 UINT flags;
51 SYSTEMTIME last_runtime;
52 } FIXDLEN_DATA;
54 typedef struct
56 ITask ITask_iface;
57 IPersistFile IPersistFile_iface;
58 LONG ref;
59 ITaskDefinition *task;
60 IExecAction *action;
61 BYTE *data;
62 WORD data_count;
63 UUID uuid;
64 LPWSTR task_name;
65 HRESULT status;
66 WORD idle_minutes, deadline_minutes;
67 DWORD flags, priority, maxRunTime, exit_code;
68 SYSTEMTIME last_runtime;
69 LPWSTR accountName;
70 DWORD trigger_count;
71 TASK_TRIGGER *trigger;
72 BOOL is_dirty;
73 USHORT instance_count;
74 } TaskImpl;
76 static inline TaskImpl *impl_from_ITask(ITask *iface)
78 return CONTAINING_RECORD(iface, TaskImpl, ITask_iface);
81 static inline TaskImpl *impl_from_IPersistFile( IPersistFile *iface )
83 return CONTAINING_RECORD(iface, TaskImpl, IPersistFile_iface);
86 static void TaskDestructor(TaskImpl *This)
88 TRACE("%p\n", This);
89 if (This->action)
90 IExecAction_Release(This->action);
91 ITaskDefinition_Release(This->task);
92 heap_free(This->data);
93 heap_free(This->task_name);
94 heap_free(This->accountName);
95 heap_free(This->trigger);
96 heap_free(This);
97 InterlockedDecrement(&dll_ref);
100 static HRESULT WINAPI MSTASK_ITask_QueryInterface(
101 ITask* iface,
102 REFIID riid,
103 void **ppvObject)
105 TaskImpl * This = impl_from_ITask(iface);
107 TRACE("IID: %s\n", debugstr_guid(riid));
108 if (ppvObject == NULL)
109 return E_POINTER;
111 if (IsEqualGUID(riid, &IID_IUnknown) ||
112 IsEqualGUID(riid, &IID_ITask))
114 *ppvObject = &This->ITask_iface;
115 ITask_AddRef(iface);
116 return S_OK;
118 else if (IsEqualGUID(riid, &IID_IPersistFile))
120 *ppvObject = &This->IPersistFile_iface;
121 ITask_AddRef(iface);
122 return S_OK;
125 WARN("Unknown interface: %s\n", debugstr_guid(riid));
126 *ppvObject = NULL;
127 return E_NOINTERFACE;
130 static ULONG WINAPI MSTASK_ITask_AddRef(
131 ITask* iface)
133 TaskImpl *This = impl_from_ITask(iface);
134 ULONG ref;
135 TRACE("\n");
136 ref = InterlockedIncrement(&This->ref);
137 return ref;
140 static ULONG WINAPI MSTASK_ITask_Release(
141 ITask* iface)
143 TaskImpl * This = impl_from_ITask(iface);
144 ULONG ref;
145 TRACE("\n");
146 ref = InterlockedDecrement(&This->ref);
147 if (ref == 0)
148 TaskDestructor(This);
149 return ref;
152 HRESULT task_set_trigger(ITask *task, WORD idx, const TASK_TRIGGER *src)
154 TaskImpl *This = impl_from_ITask(task);
155 TIME_FIELDS field_time;
156 LARGE_INTEGER sys_time;
157 TASK_TRIGGER dst;
159 TRACE("(%p, %u, %p)\n", task, idx, src);
161 if (idx >= This->trigger_count)
162 return E_FAIL;
164 /* Verify valid structure size */
165 if (src->cbTriggerSize != sizeof(*src))
166 return E_INVALIDARG;
167 dst.cbTriggerSize = src->cbTriggerSize;
169 /* Reserved field must be zero */
170 dst.Reserved1 = 0;
172 /* Verify and set valid start date and time */
173 memset(&field_time, 0, sizeof(field_time));
174 field_time.Year = src->wBeginYear;
175 field_time.Month = src->wBeginMonth;
176 field_time.Day = src->wBeginDay;
177 field_time.Hour = src->wStartHour;
178 field_time.Minute = src->wStartMinute;
179 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
180 return E_INVALIDARG;
181 dst.wBeginYear = src->wBeginYear;
182 dst.wBeginMonth = src->wBeginMonth;
183 dst.wBeginDay = src->wBeginDay;
184 dst.wStartHour = src->wStartHour;
185 dst.wStartMinute = src->wStartMinute;
187 /* Verify valid end date if TASK_TRIGGER_FLAG_HAS_END_DATE flag is set */
188 if (src->rgFlags & TASK_TRIGGER_FLAG_HAS_END_DATE)
190 memset(&field_time, 0, sizeof(field_time));
191 field_time.Year = src->wEndYear;
192 field_time.Month = src->wEndMonth;
193 field_time.Day = src->wEndDay;
194 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
195 return E_INVALIDARG;
198 /* Set valid end date independent of TASK_TRIGGER_FLAG_HAS_END_DATE flag */
199 dst.wEndYear = src->wEndYear;
200 dst.wEndMonth = src->wEndMonth;
201 dst.wEndDay = src->wEndDay;
203 /* Verify duration and interval pair */
204 if (src->MinutesDuration <= src->MinutesInterval && src->MinutesInterval > 0)
205 return E_INVALIDARG;
206 dst.MinutesDuration = src->MinutesDuration;
207 dst.MinutesInterval = src->MinutesInterval;
209 /* Copy over flags */
210 dst.rgFlags = src->rgFlags;
212 /* Set TriggerType dependent fields of Type union */
213 dst.TriggerType = src->TriggerType;
214 switch (src->TriggerType)
216 case TASK_TIME_TRIGGER_DAILY:
217 dst.Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
218 break;
219 case TASK_TIME_TRIGGER_WEEKLY:
220 dst.Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
221 dst.Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
222 break;
223 case TASK_TIME_TRIGGER_MONTHLYDATE:
224 dst.Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
225 dst.Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
226 break;
227 case TASK_TIME_TRIGGER_MONTHLYDOW:
228 dst.Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
229 dst.Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
230 dst.Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
231 break;
232 case TASK_TIME_TRIGGER_ONCE:
233 case TASK_EVENT_TRIGGER_ON_IDLE:
234 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
235 case TASK_EVENT_TRIGGER_AT_LOGON:
236 default:
237 dst.Type = src->Type;
238 break;
241 /* Reserved field must be zero */
242 dst.Reserved2 = 0;
244 /* wRandomMinutesInterval not currently used and is initialized to zero */
245 dst.wRandomMinutesInterval = 0;
247 This->trigger[idx] = dst;
249 return S_OK;
252 HRESULT task_get_trigger(ITask *task, WORD idx, TASK_TRIGGER *dst)
254 TaskImpl *This = impl_from_ITask(task);
255 TASK_TRIGGER *src;
257 TRACE("(%p, %u, %p)\n", task, idx, dst);
259 if (idx >= This->trigger_count)
260 return SCHED_E_TRIGGER_NOT_FOUND;
262 src = &This->trigger[idx];
264 /* Native implementation doesn't verify equivalent cbTriggerSize fields */
266 /* Copy relevant fields of the structure */
267 dst->cbTriggerSize = src->cbTriggerSize;
268 dst->Reserved1 = 0;
269 dst->wBeginYear = src->wBeginYear;
270 dst->wBeginMonth = src->wBeginMonth;
271 dst->wBeginDay = src->wBeginDay;
272 dst->wEndYear = src->wEndYear;
273 dst->wEndMonth = src->wEndMonth;
274 dst->wEndDay = src->wEndDay;
275 dst->wStartHour = src->wStartHour;
276 dst->wStartMinute = src->wStartMinute;
277 dst->MinutesDuration = src->MinutesDuration;
278 dst->MinutesInterval = src->MinutesInterval;
279 dst->rgFlags = src->rgFlags;
280 dst->TriggerType = src->TriggerType;
281 switch (src->TriggerType)
283 case TASK_TIME_TRIGGER_DAILY:
284 dst->Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
285 break;
286 case TASK_TIME_TRIGGER_WEEKLY:
287 dst->Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
288 dst->Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
289 break;
290 case TASK_TIME_TRIGGER_MONTHLYDATE:
291 dst->Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
292 dst->Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
293 break;
294 case TASK_TIME_TRIGGER_MONTHLYDOW:
295 dst->Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
296 dst->Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
297 dst->Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
298 break;
299 case TASK_TIME_TRIGGER_ONCE:
300 case TASK_EVENT_TRIGGER_ON_IDLE:
301 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
302 case TASK_EVENT_TRIGGER_AT_LOGON:
303 default:
304 break;
306 dst->Reserved2 = 0;
307 dst->wRandomMinutesInterval = 0;
309 return S_OK;
312 static HRESULT WINAPI MSTASK_ITask_CreateTrigger(ITask *iface, WORD *idx, ITaskTrigger **task_trigger)
314 TaskImpl *This = impl_from_ITask(iface);
315 TASK_TRIGGER *new_trigger;
316 SYSTEMTIME time;
317 HRESULT hr;
319 TRACE("(%p, %p, %p)\n", iface, idx, task_trigger);
321 hr = TaskTriggerConstructor(iface, This->trigger_count, task_trigger);
322 if (hr != S_OK) return hr;
324 if (This->trigger)
325 new_trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * (This->trigger_count + 1));
326 else
327 new_trigger = heap_alloc(sizeof(This->trigger[0]));
328 if (!new_trigger)
330 ITaskTrigger_Release(*task_trigger);
331 return E_OUTOFMEMORY;
334 This->trigger = new_trigger;
336 new_trigger = &This->trigger[This->trigger_count];
338 /* Most fields default to zero. Initialize other fields to default values. */
339 memset(new_trigger, 0, sizeof(*new_trigger));
340 GetLocalTime(&time);
341 new_trigger->cbTriggerSize = sizeof(*new_trigger);
342 new_trigger->wBeginYear = time.wYear;
343 new_trigger->wBeginMonth = time.wMonth;
344 new_trigger->wBeginDay = time.wDay;
345 new_trigger->wStartHour = time.wHour;
346 new_trigger->wStartMinute = time.wMinute;
347 new_trigger->rgFlags = TASK_TRIGGER_FLAG_DISABLED;
348 new_trigger->TriggerType = TASK_TIME_TRIGGER_DAILY;
349 new_trigger->Type.Daily.DaysInterval = 1;
351 *idx = This->trigger_count++;
353 return hr;
356 static HRESULT WINAPI MSTASK_ITask_DeleteTrigger(ITask *iface, WORD idx)
358 TaskImpl *This = impl_from_ITask(iface);
360 TRACE("(%p, %u)\n", iface, idx);
362 if (idx >= This->trigger_count)
363 return SCHED_E_TRIGGER_NOT_FOUND;
365 This->trigger_count--;
366 memmove(&This->trigger[idx], &This->trigger[idx + 1], (This->trigger_count - idx) * sizeof(This->trigger[0]));
367 /* this shouldn't fail in practice since we're shrinking the memory block */
368 This->trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * This->trigger_count);
370 return S_OK;
373 static HRESULT WINAPI MSTASK_ITask_GetTriggerCount(ITask *iface, WORD *count)
375 TaskImpl *This = impl_from_ITask(iface);
377 TRACE("(%p, %p)\n", iface, count);
379 *count = This->trigger_count;
380 return S_OK;
383 static HRESULT WINAPI MSTASK_ITask_GetTrigger(ITask *iface, WORD idx, ITaskTrigger **trigger)
385 TaskImpl *This = impl_from_ITask(iface);
387 TRACE("(%p, %u, %p)\n", iface, idx, trigger);
389 if (idx >= This->trigger_count)
390 return SCHED_E_TRIGGER_NOT_FOUND;
392 return TaskTriggerConstructor(iface, idx, trigger);
395 static HRESULT WINAPI MSTASK_ITask_GetTriggerString(
396 ITask* iface,
397 WORD iTrigger,
398 LPWSTR *ppwszTrigger)
400 FIXME("(%p, %d, %p): stub\n", iface, iTrigger, ppwszTrigger);
401 return E_NOTIMPL;
404 static HRESULT WINAPI MSTASK_ITask_GetRunTimes(
405 ITask* iface,
406 const LPSYSTEMTIME pstBegin,
407 const LPSYSTEMTIME pstEnd,
408 WORD *pCount,
409 LPSYSTEMTIME *rgstTaskTimes)
411 FIXME("(%p, %p, %p, %p, %p): stub\n", iface, pstBegin, pstEnd, pCount,
412 rgstTaskTimes);
413 return E_NOTIMPL;
416 static void get_begin_time(const TASK_TRIGGER *trigger, FILETIME *ft)
418 SYSTEMTIME st;
420 st.wYear = trigger->wBeginYear;
421 st.wMonth = trigger->wBeginMonth;
422 st.wDay = trigger->wBeginDay;
423 st.wDayOfWeek = 0;
424 st.wHour = 0;
425 st.wMinute = 0;
426 st.wSecond = 0;
427 st.wMilliseconds = 0;
428 SystemTimeToFileTime(&st, ft);
431 static void get_end_time(const TASK_TRIGGER *trigger, FILETIME *ft)
433 SYSTEMTIME st;
435 if (!(trigger->rgFlags & TASK_TRIGGER_FLAG_HAS_END_DATE))
437 ft->dwHighDateTime = ~0u;
438 ft->dwLowDateTime = ~0u;
439 return;
442 st.wYear = trigger->wEndYear;
443 st.wMonth = trigger->wEndMonth;
444 st.wDay = trigger->wEndDay;
445 st.wDayOfWeek = 0;
446 st.wHour = 0;
447 st.wMinute = 0;
448 st.wSecond = 0;
449 st.wMilliseconds = 0;
450 SystemTimeToFileTime(&st, ft);
453 static void filetime_add_ms(FILETIME *ft, ULONGLONG ms)
455 union u_ftll
457 FILETIME ft;
458 ULONGLONG ll;
459 } *ftll = (union u_ftll *)ft;
461 ftll->ll += ms * (ULONGLONG)10000;
464 static void filetime_add_hours(FILETIME *ft, ULONG hours)
466 filetime_add_ms(ft, (ULONGLONG)hours * 60 * 60 * 1000);
469 static void filetime_add_days(FILETIME *ft, ULONG days)
471 filetime_add_hours(ft, (ULONGLONG)days * 24);
474 static void filetime_add_weeks(FILETIME *ft, ULONG weeks)
476 filetime_add_days(ft, (ULONGLONG)weeks * 7);
479 static HRESULT WINAPI MSTASK_ITask_GetNextRunTime(ITask *iface, SYSTEMTIME *rt)
481 TaskImpl *This = impl_from_ITask(iface);
482 HRESULT hr = SCHED_S_TASK_NO_VALID_TRIGGERS;
483 SYSTEMTIME st, current_st;
484 FILETIME current_ft, trigger_ft, begin_ft, end_ft, best_ft;
485 BOOL have_best_time = FALSE;
486 DWORD i;
488 TRACE("(%p, %p)\n", iface, rt);
490 if (This->flags & TASK_FLAG_DISABLED)
492 memset(rt, 0, sizeof(*rt));
493 return SCHED_S_TASK_DISABLED;
496 GetLocalTime(&current_st);
497 SystemTimeToFileTime(&current_st, &current_ft);
499 for (i = 0; i < This->trigger_count; i++)
501 if (!(This->trigger[i].rgFlags & TASK_TRIGGER_FLAG_DISABLED))
503 get_begin_time(&This->trigger[i], &begin_ft);
504 if (CompareFileTime(&begin_ft, &current_ft) < 0)
505 begin_ft = current_ft;
507 get_end_time(&This->trigger[i], &end_ft);
509 switch (This->trigger[i].TriggerType)
511 case TASK_EVENT_TRIGGER_ON_IDLE:
512 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
513 case TASK_EVENT_TRIGGER_AT_LOGON:
514 hr = SCHED_S_EVENT_TRIGGER;
515 break;
517 case TASK_TIME_TRIGGER_ONCE:
518 st = current_st;
519 st.wHour = This->trigger[i].wStartHour;
520 st.wMinute = This->trigger[i].wStartMinute;
521 st.wSecond = 0;
522 st.wMilliseconds = 0;
523 SystemTimeToFileTime(&st, &trigger_ft);
524 if (CompareFileTime(&begin_ft, &trigger_ft) <= 0 && CompareFileTime(&trigger_ft, &end_ft) < 0)
526 if (!have_best_time || CompareFileTime(&trigger_ft, &best_ft) < 0)
528 best_ft = trigger_ft;
529 have_best_time = TRUE;
532 break;
534 case TASK_TIME_TRIGGER_DAILY:
535 if (!This->trigger[i].Type.Daily.DaysInterval)
536 break; /* avoid infinite loop */
538 st = current_st;
539 st.wHour = This->trigger[i].wStartHour;
540 st.wMinute = This->trigger[i].wStartMinute;
541 st.wSecond = 0;
542 st.wMilliseconds = 0;
543 SystemTimeToFileTime(&st, &trigger_ft);
544 while (CompareFileTime(&trigger_ft, &end_ft) < 0)
546 if (CompareFileTime(&trigger_ft, &begin_ft) >= 0)
548 if (!have_best_time || CompareFileTime(&trigger_ft, &best_ft) < 0)
550 best_ft = trigger_ft;
551 have_best_time = TRUE;
553 break;
556 filetime_add_days(&trigger_ft, This->trigger[i].Type.Daily.DaysInterval);
558 break;
560 case TASK_TIME_TRIGGER_WEEKLY:
561 if (!This->trigger[i].Type.Weekly.rgfDaysOfTheWeek)
562 break; /* avoid infinite loop */
564 st = current_st;
565 st.wHour = This->trigger[i].wStartHour;
566 st.wMinute = This->trigger[i].wStartMinute;
567 st.wSecond = 0;
568 st.wMilliseconds = 0;
569 SystemTimeToFileTime(&st, &trigger_ft);
570 while (CompareFileTime(&trigger_ft, &end_ft) < 0)
572 FileTimeToSystemTime(&trigger_ft, &st);
574 if (CompareFileTime(&trigger_ft, &begin_ft) >= 0)
576 if (This->trigger[i].Type.Weekly.rgfDaysOfTheWeek & (1 << st.wDayOfWeek))
578 if (!have_best_time || CompareFileTime(&trigger_ft, &best_ft) < 0)
580 best_ft = trigger_ft;
581 have_best_time = TRUE;
583 break;
587 if (st.wDayOfWeek == 0 && This->trigger[i].Type.Weekly.WeeksInterval > 1) /* Sunday, goto next week */
588 filetime_add_weeks(&trigger_ft, This->trigger[i].Type.Weekly.WeeksInterval - 1);
589 else /* check next weekday */
590 filetime_add_days(&trigger_ft, 1);
592 break;
594 default:
595 FIXME("trigger type %u is not handled\n", This->trigger[i].TriggerType);
596 break;
601 if (have_best_time)
603 FileTimeToSystemTime(&best_ft, rt);
604 return S_OK;
607 memset(rt, 0, sizeof(*rt));
608 return hr;
611 static HRESULT WINAPI MSTASK_ITask_SetIdleWait(
612 ITask* iface,
613 WORD wIdleMinutes,
614 WORD wDeadlineMinutes)
616 FIXME("(%p, %d, %d): stub\n", iface, wIdleMinutes, wDeadlineMinutes);
617 return E_NOTIMPL;
620 static HRESULT WINAPI MSTASK_ITask_GetIdleWait(ITask *iface, WORD *idle_minutes, WORD *deadline_minutes)
622 TaskImpl *This = impl_from_ITask(iface);
624 TRACE("(%p, %p, %p): stub\n", iface, idle_minutes, deadline_minutes);
626 *idle_minutes = This->idle_minutes;
627 *deadline_minutes = This->deadline_minutes;
628 return S_OK;
631 static HRESULT WINAPI MSTASK_ITask_Run(ITask *iface)
633 TaskImpl *This = impl_from_ITask(iface);
635 TRACE("(%p)\n", iface);
637 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
638 return SCHED_E_TASK_NOT_READY;
640 This->flags |= 0x04000000;
641 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
644 static HRESULT WINAPI MSTASK_ITask_Terminate(ITask *iface)
646 TaskImpl *This = impl_from_ITask(iface);
648 TRACE("(%p)\n", iface);
650 if (!This->instance_count)
651 return SCHED_E_TASK_NOT_RUNNING;
653 This->flags |= 0x08000000;
654 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
657 static HRESULT WINAPI MSTASK_ITask_EditWorkItem(
658 ITask* iface,
659 HWND hParent,
660 DWORD dwReserved)
662 FIXME("(%p, %p, %d): stub\n", iface, hParent, dwReserved);
663 return E_NOTIMPL;
666 static HRESULT WINAPI MSTASK_ITask_GetMostRecentRunTime(ITask *iface, SYSTEMTIME *st)
668 TaskImpl *This = impl_from_ITask(iface);
670 TRACE("(%p, %p)\n", iface, st);
672 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
674 memset(st, 0, sizeof(*st));
675 return SCHED_S_TASK_HAS_NOT_RUN;
678 *st = This->last_runtime;
679 return S_OK;
682 static HRESULT WINAPI MSTASK_ITask_GetStatus(ITask *iface, HRESULT *status)
684 TaskImpl *This = impl_from_ITask(iface);
686 TRACE("(%p, %p)\n", iface, status);
688 *status = This->instance_count ? SCHED_S_TASK_RUNNING : This->status;
689 return S_OK;
692 static HRESULT WINAPI MSTASK_ITask_GetExitCode(ITask *iface, DWORD *exit_code)
694 TaskImpl *This = impl_from_ITask(iface);
696 TRACE("(%p, %p)\n", iface, exit_code);
698 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
700 *exit_code = 0;
701 return SCHED_S_TASK_HAS_NOT_RUN;
704 *exit_code = This->exit_code;
705 return S_OK;
708 static HRESULT WINAPI MSTASK_ITask_SetComment(ITask *iface, LPCWSTR comment)
710 TaskImpl *This = impl_from_ITask(iface);
711 IRegistrationInfo *info;
712 HRESULT hr;
714 TRACE("(%p, %s)\n", iface, debugstr_w(comment));
716 if (!comment || !comment[0])
717 comment = NULL;
719 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
720 if (hr == S_OK)
722 hr = IRegistrationInfo_put_Description(info, (BSTR)comment);
723 IRegistrationInfo_Release(info);
724 This->is_dirty = TRUE;
726 return hr;
729 static HRESULT WINAPI MSTASK_ITask_GetComment(ITask *iface, LPWSTR *comment)
731 TaskImpl *This = impl_from_ITask(iface);
732 IRegistrationInfo *info;
733 HRESULT hr;
734 BSTR description;
735 DWORD len;
737 TRACE("(%p, %p)\n", iface, comment);
739 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
740 if (hr != S_OK) return hr;
742 hr = IRegistrationInfo_get_Description(info, &description);
743 if (hr == S_OK)
745 len = description ? lstrlenW(description) + 1 : 1;
746 *comment = CoTaskMemAlloc(len * sizeof(WCHAR));
747 if (*comment)
749 if (!description)
750 *comment[0] = 0;
751 else
752 lstrcpyW(*comment, description);
753 hr = S_OK;
755 else
756 hr = E_OUTOFMEMORY;
758 SysFreeString(description);
761 IRegistrationInfo_Release(info);
762 return hr;
765 static HRESULT WINAPI MSTASK_ITask_SetCreator(ITask *iface, LPCWSTR creator)
767 TaskImpl *This = impl_from_ITask(iface);
768 IRegistrationInfo *info;
769 HRESULT hr;
771 TRACE("(%p, %s)\n", iface, debugstr_w(creator));
773 if (!creator || !creator[0])
774 creator = NULL;
776 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
777 if (hr == S_OK)
779 hr = IRegistrationInfo_put_Author(info, (BSTR)creator);
780 IRegistrationInfo_Release(info);
781 This->is_dirty = TRUE;
783 return hr;
786 static HRESULT WINAPI MSTASK_ITask_GetCreator(ITask *iface, LPWSTR *creator)
788 TaskImpl *This = impl_from_ITask(iface);
789 IRegistrationInfo *info;
790 HRESULT hr;
791 BSTR author;
792 DWORD len;
794 TRACE("(%p, %p)\n", iface, creator);
796 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
797 if (hr != S_OK) return hr;
799 hr = IRegistrationInfo_get_Author(info, &author);
800 if (hr == S_OK)
802 len = author ? lstrlenW(author) + 1 : 1;
803 *creator = CoTaskMemAlloc(len * sizeof(WCHAR));
804 if (*creator)
806 if (!author)
807 *creator[0] = 0;
808 else
809 lstrcpyW(*creator, author);
810 hr = S_OK;
812 else
813 hr = E_OUTOFMEMORY;
815 SysFreeString(author);
818 IRegistrationInfo_Release(info);
819 return hr;
822 static HRESULT WINAPI MSTASK_ITask_SetWorkItemData(ITask *iface, WORD count, BYTE data[])
824 TaskImpl *This = impl_from_ITask(iface);
826 TRACE("(%p, %u, %p)\n", iface, count, data);
828 if (count)
830 if (!data) return E_INVALIDARG;
832 heap_free(This->data);
833 This->data = heap_alloc(count);
834 if (!This->data) return E_OUTOFMEMORY;
835 memcpy(This->data, data, count);
836 This->data_count = count;
838 else
840 if (data) return E_INVALIDARG;
842 heap_free(This->data);
843 This->data = NULL;
844 This->data_count = 0;
847 return S_OK;
850 static HRESULT WINAPI MSTASK_ITask_GetWorkItemData(ITask *iface, WORD *count, BYTE **data)
852 TaskImpl *This = impl_from_ITask(iface);
854 TRACE("(%p, %p, %p)\n", iface, count, data);
856 if (!This->data)
858 *count = 0;
859 *data = NULL;
861 else
863 *data = CoTaskMemAlloc(This->data_count);
864 if (!*data) return E_OUTOFMEMORY;
865 memcpy(*data, This->data, This->data_count);
866 *count = This->data_count;
869 return S_OK;
872 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryCount(
873 ITask* iface,
874 WORD wRetryCount)
876 FIXME("(%p, %d): stub\n", iface, wRetryCount);
877 return E_NOTIMPL;
880 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryCount(ITask *iface, WORD *count)
882 TRACE("(%p, %p)\n", iface, count);
883 return E_NOTIMPL;
886 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryInterval(
887 ITask* iface,
888 WORD wRetryInterval)
890 FIXME("(%p, %d): stub\n", iface, wRetryInterval);
891 return E_NOTIMPL;
894 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryInterval(ITask *iface, WORD *interval)
896 TRACE("(%p, %p)\n", iface, interval);
897 return E_NOTIMPL;
900 static HRESULT WINAPI MSTASK_ITask_SetFlags(ITask *iface, DWORD flags)
902 TaskImpl *This = impl_from_ITask(iface);
904 TRACE("(%p, 0x%08x)\n", iface, flags);
905 This->flags &= 0xffff8000;
906 This->flags |= flags & 0x7fff;
907 This->is_dirty = TRUE;
908 return S_OK;
911 static HRESULT WINAPI MSTASK_ITask_GetFlags(ITask *iface, DWORD *flags)
913 TaskImpl *This = impl_from_ITask(iface);
915 TRACE("(%p, %p)\n", iface, flags);
916 *flags = LOWORD(This->flags);
917 return S_OK;
920 static HRESULT WINAPI MSTASK_ITask_SetAccountInformation(
921 ITask* iface,
922 LPCWSTR pwszAccountName,
923 LPCWSTR pwszPassword)
925 DWORD n;
926 TaskImpl *This = impl_from_ITask(iface);
927 LPWSTR tmp_account_name;
929 TRACE("(%p, %s, %s): partial stub\n", iface, debugstr_w(pwszAccountName),
930 debugstr_w(pwszPassword));
932 if (pwszPassword)
933 FIXME("Partial stub ignores passwords\n");
935 n = (lstrlenW(pwszAccountName) + 1);
936 tmp_account_name = heap_alloc(n * sizeof(WCHAR));
937 if (!tmp_account_name)
938 return E_OUTOFMEMORY;
939 lstrcpyW(tmp_account_name, pwszAccountName);
940 heap_free(This->accountName);
941 This->accountName = tmp_account_name;
942 This->is_dirty = TRUE;
943 return S_OK;
946 static HRESULT WINAPI MSTASK_ITask_GetAccountInformation(
947 ITask* iface,
948 LPWSTR *ppwszAccountName)
950 DWORD n;
951 TaskImpl *This = impl_from_ITask(iface);
953 TRACE("(%p, %p): partial stub\n", iface, ppwszAccountName);
955 /* This implements the WinXP behavior when accountName has not yet
956 * set. Win2K behaves differently, returning SCHED_E_CANNOT_OPEN_TASK */
957 if (!This->accountName)
958 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
960 n = (lstrlenW(This->accountName) + 1);
961 *ppwszAccountName = CoTaskMemAlloc(n * sizeof(WCHAR));
962 if (!*ppwszAccountName)
963 return E_OUTOFMEMORY;
964 lstrcpyW(*ppwszAccountName, This->accountName);
965 return S_OK;
968 static HRESULT WINAPI MSTASK_ITask_SetApplicationName(ITask *iface, LPCWSTR appname)
970 TaskImpl *This = impl_from_ITask(iface);
971 DWORD len;
972 HRESULT hr;
974 TRACE("(%p, %s)\n", iface, debugstr_w(appname));
976 /* Empty application name */
977 if (!appname || !appname[0])
978 return IExecAction_put_Path(This->action, NULL);
980 /* Attempt to set pwszApplicationName to a path resolved application name */
981 len = SearchPathW(NULL, appname, NULL, 0, NULL, NULL);
982 if (len)
984 LPWSTR tmp_name;
986 tmp_name = heap_alloc(len * sizeof(WCHAR));
987 if (!tmp_name)
988 return E_OUTOFMEMORY;
989 len = SearchPathW(NULL, appname, NULL, len, tmp_name, NULL);
990 if (len)
992 hr = IExecAction_put_Path(This->action, tmp_name);
993 if (hr == S_OK) This->is_dirty = TRUE;
995 else
996 hr = HRESULT_FROM_WIN32(GetLastError());
998 heap_free(tmp_name);
999 return hr;
1002 /* If unable to path resolve name, simply set to appname */
1003 hr = IExecAction_put_Path(This->action, (BSTR)appname);
1004 if (hr == S_OK) This->is_dirty = TRUE;
1005 return hr;
1008 static HRESULT WINAPI MSTASK_ITask_GetApplicationName(ITask *iface, LPWSTR *appname)
1010 TaskImpl *This = impl_from_ITask(iface);
1011 HRESULT hr;
1012 BSTR path;
1013 DWORD len;
1015 TRACE("(%p, %p)\n", iface, appname);
1017 hr = IExecAction_get_Path(This->action, &path);
1018 if (hr != S_OK) return hr;
1020 len = path ? lstrlenW(path) + 1 : 1;
1021 *appname = CoTaskMemAlloc(len * sizeof(WCHAR));
1022 if (*appname)
1024 if (!path)
1025 *appname[0] = 0;
1026 else
1027 lstrcpyW(*appname, path);
1028 hr = S_OK;
1030 else
1031 hr = E_OUTOFMEMORY;
1033 SysFreeString(path);
1034 return hr;
1037 static HRESULT WINAPI MSTASK_ITask_SetParameters(ITask *iface, LPCWSTR params)
1039 TaskImpl *This = impl_from_ITask(iface);
1040 HRESULT hr;
1042 TRACE("(%p, %s)\n", iface, debugstr_w(params));
1044 /* Empty parameter list */
1045 if (!params || !params[0])
1046 params = NULL;
1048 hr = IExecAction_put_Arguments(This->action, (BSTR)params);
1049 if (hr == S_OK) This->is_dirty = TRUE;
1050 return hr;
1053 static HRESULT WINAPI MSTASK_ITask_GetParameters(ITask *iface, LPWSTR *params)
1055 TaskImpl *This = impl_from_ITask(iface);
1056 HRESULT hr;
1057 BSTR args;
1058 DWORD len;
1060 TRACE("(%p, %p)\n", iface, params);
1062 hr = IExecAction_get_Arguments(This->action, &args);
1063 if (hr != S_OK) return hr;
1065 len = args ? lstrlenW(args) + 1 : 1;
1066 *params = CoTaskMemAlloc(len * sizeof(WCHAR));
1067 if (*params)
1069 if (!args)
1070 *params[0] = 0;
1071 else
1072 lstrcpyW(*params, args);
1073 hr = S_OK;
1075 else
1076 hr = E_OUTOFMEMORY;
1078 SysFreeString(args);
1079 return hr;
1082 static HRESULT WINAPI MSTASK_ITask_SetWorkingDirectory(ITask * iface, LPCWSTR workdir)
1084 TaskImpl *This = impl_from_ITask(iface);
1085 HRESULT hr;
1087 TRACE("(%p, %s)\n", iface, debugstr_w(workdir));
1089 if (!workdir || !workdir[0])
1090 workdir = NULL;
1092 hr = IExecAction_put_WorkingDirectory(This->action, (BSTR)workdir);
1093 if (hr == S_OK) This->is_dirty = TRUE;
1094 return hr;
1097 static HRESULT WINAPI MSTASK_ITask_GetWorkingDirectory(ITask *iface, LPWSTR *workdir)
1099 TaskImpl *This = impl_from_ITask(iface);
1100 HRESULT hr;
1101 BSTR dir;
1102 DWORD len;
1104 TRACE("(%p, %p)\n", iface, workdir);
1106 hr = IExecAction_get_WorkingDirectory(This->action, &dir);
1107 if (hr != S_OK) return hr;
1109 len = dir ? lstrlenW(dir) + 1 : 1;
1110 *workdir = CoTaskMemAlloc(len * sizeof(WCHAR));
1111 if (*workdir)
1113 if (!dir)
1114 *workdir[0] = 0;
1115 else
1116 lstrcpyW(*workdir, dir);
1117 hr = S_OK;
1119 else
1120 hr = E_OUTOFMEMORY;
1122 SysFreeString(dir);
1123 return hr;
1126 static HRESULT WINAPI MSTASK_ITask_SetPriority(
1127 ITask* iface,
1128 DWORD dwPriority)
1130 FIXME("(%p, 0x%08x): stub\n", iface, dwPriority);
1131 return E_NOTIMPL;
1134 static HRESULT WINAPI MSTASK_ITask_GetPriority(ITask *iface, DWORD *priority)
1136 TaskImpl *This = impl_from_ITask(iface);
1138 TRACE("(%p, %p)\n", iface, priority);
1140 *priority = This->priority;
1141 return S_OK;
1144 static HRESULT WINAPI MSTASK_ITask_SetTaskFlags(
1145 ITask* iface,
1146 DWORD dwFlags)
1148 FIXME("(%p, 0x%08x): stub\n", iface, dwFlags);
1149 return E_NOTIMPL;
1152 static HRESULT WINAPI MSTASK_ITask_GetTaskFlags(ITask *iface, DWORD *flags)
1154 FIXME("(%p, %p): stub\n", iface, flags);
1155 *flags = 0;
1156 return S_OK;
1159 static HRESULT WINAPI MSTASK_ITask_SetMaxRunTime(
1160 ITask* iface,
1161 DWORD dwMaxRunTime)
1163 TaskImpl *This = impl_from_ITask(iface);
1165 TRACE("(%p, %d)\n", iface, dwMaxRunTime);
1167 This->maxRunTime = dwMaxRunTime;
1168 This->is_dirty = TRUE;
1169 return S_OK;
1172 static HRESULT WINAPI MSTASK_ITask_GetMaxRunTime(
1173 ITask* iface,
1174 DWORD *pdwMaxRunTime)
1176 TaskImpl *This = impl_from_ITask(iface);
1178 TRACE("(%p, %p)\n", iface, pdwMaxRunTime);
1180 *pdwMaxRunTime = This->maxRunTime;
1181 return S_OK;
1184 static HRESULT WINAPI MSTASK_IPersistFile_QueryInterface(
1185 IPersistFile* iface,
1186 REFIID riid,
1187 void **ppvObject)
1189 TaskImpl *This = impl_from_IPersistFile(iface);
1190 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppvObject);
1191 return ITask_QueryInterface(&This->ITask_iface, riid, ppvObject);
1194 static ULONG WINAPI MSTASK_IPersistFile_AddRef(
1195 IPersistFile* iface)
1197 TaskImpl *This = impl_from_IPersistFile(iface);
1198 return ITask_AddRef(&This->ITask_iface);
1201 static ULONG WINAPI MSTASK_IPersistFile_Release(
1202 IPersistFile* iface)
1204 TaskImpl *This = impl_from_IPersistFile(iface);
1205 return ITask_Release(&This->ITask_iface);
1208 static HRESULT WINAPI MSTASK_IPersistFile_GetClassID(IPersistFile *iface, CLSID *clsid)
1210 TRACE("(%p, %p)\n", iface, clsid);
1212 *clsid = CLSID_CTask;
1213 return S_OK;
1216 static HRESULT WINAPI MSTASK_IPersistFile_IsDirty(IPersistFile *iface)
1218 TaskImpl *This = impl_from_IPersistFile(iface);
1219 TRACE("(%p)\n", iface);
1220 return This->is_dirty ? S_OK : S_FALSE;
1223 static DWORD load_unicode_strings(ITask *task, BYTE *data, DWORD limit)
1225 DWORD i, data_size = 0;
1226 USHORT len;
1228 for (i = 0; i < 5; i++)
1230 if (limit < sizeof(USHORT))
1232 TRACE("invalid string %u offset\n", i);
1233 break;
1236 len = *(USHORT *)data;
1237 data += sizeof(USHORT);
1238 data_size += sizeof(USHORT);
1239 limit -= sizeof(USHORT);
1240 if (limit < len * sizeof(WCHAR))
1242 TRACE("invalid string %u size\n", i);
1243 break;
1246 TRACE("string %u: %s\n", i, wine_dbgstr_wn((const WCHAR *)data, len));
1248 switch (i)
1250 case 0:
1251 ITask_SetApplicationName(task, (const WCHAR *)data);
1252 break;
1253 case 1:
1254 ITask_SetParameters(task, (const WCHAR *)data);
1255 break;
1256 case 2:
1257 ITask_SetWorkingDirectory(task, (const WCHAR *)data);
1258 break;
1259 case 3:
1260 ITask_SetCreator(task, (const WCHAR *)data);
1261 break;
1262 case 4:
1263 ITask_SetComment(task, (const WCHAR *)data);
1264 break;
1265 default:
1266 break;
1269 data += len * sizeof(WCHAR);
1270 data_size += len * sizeof(WCHAR);
1273 return data_size;
1276 static HRESULT load_job_data(TaskImpl *This, BYTE *data, DWORD size)
1278 ITask *task = &This->ITask_iface;
1279 HRESULT hr;
1280 const FIXDLEN_DATA *fixed;
1281 const SYSTEMTIME *st;
1282 DWORD unicode_strings_size, data_size, triggers_size;
1283 USHORT trigger_count, i;
1284 const USHORT *signature;
1285 TASK_TRIGGER *task_trigger;
1287 if (size < sizeof(*fixed))
1289 TRACE("no space for FIXDLEN_DATA\n");
1290 return SCHED_E_INVALID_TASK;
1293 fixed = (const FIXDLEN_DATA *)data;
1295 TRACE("product_version %04x\n", fixed->product_version);
1296 TRACE("file_version %04x\n", fixed->file_version);
1297 TRACE("uuid %s\n", wine_dbgstr_guid(&fixed->uuid));
1299 if (fixed->file_version != 0x0001)
1300 return SCHED_E_INVALID_TASK;
1302 This->uuid = fixed->uuid;
1304 TRACE("name_size_offset %04x\n", fixed->name_size_offset);
1305 TRACE("trigger_offset %04x\n", fixed->trigger_offset);
1306 TRACE("error_retry_count %u\n", fixed->error_retry_count);
1307 TRACE("error_retry_interval %u\n", fixed->error_retry_interval);
1308 TRACE("idle_deadline %u\n", fixed->idle_deadline);
1309 This->deadline_minutes = fixed->idle_deadline;
1310 TRACE("idle_wait %u\n", fixed->idle_wait);
1311 This->idle_minutes = fixed->idle_wait;
1312 TRACE("priority %08x\n", fixed->priority);
1313 This->priority = fixed->priority;
1314 TRACE("maximum_runtime %u\n", fixed->maximum_runtime);
1315 This->maxRunTime = fixed->maximum_runtime;
1316 TRACE("exit_code %#x\n", fixed->exit_code);
1317 This->exit_code = fixed->exit_code;
1318 TRACE("status %08x\n", fixed->status);
1319 This->status = fixed->status;
1320 TRACE("flags %08x\n", fixed->flags);
1321 This->flags = fixed->flags;
1322 This->last_runtime = fixed->last_runtime;
1323 st = &fixed->last_runtime;
1324 TRACE("last_runtime %u/%u/%u wday %u %u:%02u:%02u.%03u\n",
1325 st->wDay, st->wMonth, st->wYear, st->wDayOfWeek,
1326 st->wHour, st->wMinute, st->wSecond, st->wMilliseconds);
1328 /* Instance Count */
1329 if (size < sizeof(*fixed) + sizeof(USHORT))
1331 TRACE("no space for instance count\n");
1332 return SCHED_E_INVALID_TASK;
1335 This->instance_count = *(const USHORT *)(data + sizeof(*fixed));
1336 TRACE("instance count %u\n", This->instance_count);
1338 if (fixed->name_size_offset + sizeof(USHORT) < size)
1339 unicode_strings_size = load_unicode_strings(task, data + fixed->name_size_offset, size - fixed->name_size_offset);
1340 else
1342 TRACE("invalid name_size_offset\n");
1343 return SCHED_E_INVALID_TASK;
1345 TRACE("unicode strings end at %#x\n", fixed->name_size_offset + unicode_strings_size);
1347 if (size < fixed->trigger_offset + sizeof(USHORT))
1349 TRACE("no space for triggers count\n");
1350 return SCHED_E_INVALID_TASK;
1352 trigger_count = *(const USHORT *)(data + fixed->trigger_offset);
1353 TRACE("trigger_count %u\n", trigger_count);
1354 triggers_size = size - fixed->trigger_offset - sizeof(USHORT);
1355 TRACE("triggers_size %u\n", triggers_size);
1356 task_trigger = (TASK_TRIGGER *)(data + fixed->trigger_offset + sizeof(USHORT));
1358 data += fixed->name_size_offset + unicode_strings_size;
1359 size -= fixed->name_size_offset + unicode_strings_size;
1361 /* User Data */
1362 if (size < sizeof(USHORT))
1364 TRACE("no space for user data size\n");
1365 return SCHED_E_INVALID_TASK;
1368 data_size = *(const USHORT *)data;
1369 if (size < sizeof(USHORT) + data_size)
1371 TRACE("no space for user data\n");
1372 return SCHED_E_INVALID_TASK;
1374 TRACE("User Data size %#x\n", data_size);
1375 ITask_SetWorkItemData(task, data_size, data + sizeof(USHORT));
1377 size -= sizeof(USHORT) + data_size;
1378 data += sizeof(USHORT) + data_size;
1380 /* Reserved Data */
1381 if (size < sizeof(USHORT))
1383 TRACE("no space for reserved data size\n");
1384 return SCHED_E_INVALID_TASK;
1387 data_size = *(const USHORT *)data;
1388 if (size < sizeof(USHORT) + data_size)
1390 TRACE("no space for reserved data\n");
1391 return SCHED_E_INVALID_TASK;
1393 TRACE("Reserved Data size %#x\n", data_size);
1395 size -= sizeof(USHORT) + data_size;
1396 data += sizeof(USHORT) + data_size;
1398 /* Trigger Data */
1399 TRACE("trigger_offset %04x, triggers end at %04x\n", fixed->trigger_offset,
1400 (DWORD)(fixed->trigger_offset + sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER)));
1402 task_trigger = (TASK_TRIGGER *)(data + sizeof(USHORT));
1404 if (trigger_count * sizeof(TASK_TRIGGER) > triggers_size)
1406 TRACE("no space for triggers data\n");
1407 return SCHED_E_INVALID_TASK;
1410 This->trigger_count = 0;
1412 for (i = 0; i < trigger_count; i++)
1414 ITaskTrigger *trigger;
1415 WORD idx;
1417 hr = ITask_CreateTrigger(task, &idx, &trigger);
1418 if (hr != S_OK) return hr;
1420 hr = ITaskTrigger_SetTrigger(trigger, &task_trigger[i]);
1421 ITaskTrigger_Release(trigger);
1422 if (hr != S_OK)
1424 ITask_DeleteTrigger(task, idx);
1425 return hr;
1429 size -= sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1430 data += sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1432 if (size < 2 * sizeof(USHORT) + 64)
1434 TRACE("no space for signature\n");
1435 return S_OK; /* signature is optional */
1438 signature = (const USHORT *)data;
1439 TRACE("signature version %04x, client version %04x\n", signature[0], signature[1]);
1441 return S_OK;
1444 static HRESULT WINAPI MSTASK_IPersistFile_Load(IPersistFile *iface, LPCOLESTR file_name, DWORD mode)
1446 TaskImpl *This = impl_from_IPersistFile(iface);
1447 HRESULT hr;
1448 HANDLE file, mapping;
1449 DWORD access, sharing, size, try;
1450 void *data;
1452 TRACE("(%p, %s, 0x%08x)\n", iface, debugstr_w(file_name), mode);
1454 switch (mode & 0x000f)
1456 default:
1457 case STGM_READ:
1458 access = GENERIC_READ;
1459 break;
1460 case STGM_WRITE:
1461 case STGM_READWRITE:
1462 access = GENERIC_READ | GENERIC_WRITE;
1463 break;
1466 switch (mode & 0x00f0)
1468 default:
1469 case STGM_SHARE_DENY_NONE:
1470 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1471 break;
1472 case STGM_SHARE_DENY_READ:
1473 sharing = FILE_SHARE_WRITE;
1474 break;
1475 case STGM_SHARE_DENY_WRITE:
1476 sharing = FILE_SHARE_READ;
1477 break;
1478 case STGM_SHARE_EXCLUSIVE:
1479 sharing = 0;
1480 break;
1483 try = 1;
1484 for (;;)
1486 file = CreateFileW(file_name, access, sharing, NULL, OPEN_EXISTING, 0, 0);
1487 if (file != INVALID_HANDLE_VALUE) break;
1489 if (GetLastError() != ERROR_SHARING_VIOLATION || try++ >= 3)
1491 TRACE("Failed to open %s, error %u\n", debugstr_w(file_name), GetLastError());
1492 return HRESULT_FROM_WIN32(GetLastError());
1494 Sleep(100);
1497 size = GetFileSize(file, NULL);
1499 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, 0);
1500 if (!mapping)
1502 TRACE("Failed to create file mapping %s, error %u\n", debugstr_w(file_name), GetLastError());
1503 CloseHandle(file);
1504 return HRESULT_FROM_WIN32(GetLastError());
1507 data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1508 if (data)
1510 hr = load_job_data(This, data, size);
1511 if (hr == S_OK) This->is_dirty = FALSE;
1512 UnmapViewOfFile(data);
1514 else
1515 hr = HRESULT_FROM_WIN32(GetLastError());
1517 CloseHandle(mapping);
1518 CloseHandle(file);
1520 return hr;
1523 static BOOL write_signature(HANDLE hfile)
1525 struct
1527 USHORT SignatureVersion;
1528 USHORT ClientVersion;
1529 BYTE md5[64];
1530 } signature;
1531 DWORD size;
1533 signature.SignatureVersion = 0x0001;
1534 signature.ClientVersion = 0x0001;
1535 memset(&signature.md5, 0, sizeof(signature.md5));
1537 return WriteFile(hfile, &signature, sizeof(signature), &size, NULL);
1540 static BOOL write_reserved_data(HANDLE hfile)
1542 static const struct
1544 USHORT size;
1545 BYTE data[8];
1546 } user = { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1547 DWORD size;
1549 return WriteFile(hfile, &user, sizeof(user), &size, NULL);
1552 static BOOL write_user_data(HANDLE hfile, BYTE *data, WORD data_size)
1554 DWORD size;
1556 if (!WriteFile(hfile, &data_size, sizeof(data_size), &size, NULL))
1557 return FALSE;
1559 if (!data_size) return TRUE;
1561 return WriteFile(hfile, data, data_size, &size, NULL);
1564 static HRESULT write_triggers(TaskImpl *This, HANDLE hfile)
1566 WORD count, i, idx = 0xffff;
1567 DWORD size;
1568 HRESULT hr = S_OK;
1569 ITaskTrigger *trigger;
1571 count = This->trigger_count;
1573 /* Windows saves a .job with at least 1 trigger */
1574 if (!count)
1576 hr = ITask_CreateTrigger(&This->ITask_iface, &idx, &trigger);
1577 if (hr != S_OK) return hr;
1578 ITaskTrigger_Release(trigger);
1580 count = 1;
1583 if (WriteFile(hfile, &count, sizeof(count), &size, NULL))
1585 for (i = 0; i < count; i++)
1587 if (!WriteFile(hfile, &This->trigger[i], sizeof(This->trigger[0]), &size, NULL))
1589 hr = HRESULT_FROM_WIN32(GetLastError());
1590 break;
1594 else
1595 hr = HRESULT_FROM_WIN32(GetLastError());
1597 if (idx != 0xffff)
1598 ITask_DeleteTrigger(&This->ITask_iface, idx);
1600 return hr;
1603 static BOOL write_unicode_string(HANDLE hfile, const WCHAR *str)
1605 USHORT count;
1606 DWORD size;
1608 count = str ? (lstrlenW(str) + 1) : 0;
1609 if (!WriteFile(hfile, &count, sizeof(count), &size, NULL))
1610 return FALSE;
1612 if (!str) return TRUE;
1614 count *= sizeof(WCHAR);
1615 return WriteFile(hfile, str, count, &size, NULL);
1618 static HRESULT WINAPI MSTASK_IPersistFile_Save(IPersistFile *iface, LPCOLESTR task_name, BOOL remember)
1620 static WCHAR authorW[] = { 'W','i','n','e',0 };
1621 static WCHAR commentW[] = { 'C','r','e','a','t','e','d',' ','b','y',' ','W','i','n','e',0 };
1622 FIXDLEN_DATA fixed;
1623 WORD word, user_data_size = 0;
1624 HANDLE hfile;
1625 DWORD size, ver, disposition, try;
1626 TaskImpl *This = impl_from_IPersistFile(iface);
1627 ITask *task = &This->ITask_iface;
1628 LPWSTR appname = NULL, params = NULL, workdir = NULL, creator = NULL, comment = NULL;
1629 BYTE *user_data = NULL;
1630 HRESULT hr;
1632 TRACE("(%p, %s, %d)\n", iface, debugstr_w(task_name), remember);
1634 disposition = task_name ? CREATE_NEW : OPEN_ALWAYS;
1636 if (!task_name)
1638 task_name = This->task_name;
1639 remember = FALSE;
1642 ITask_GetComment(task, &comment);
1643 if (!comment) comment = commentW;
1644 ITask_GetCreator(task, &creator);
1645 if (!creator) creator = authorW;
1646 ITask_GetApplicationName(task, &appname);
1647 ITask_GetParameters(task, &params);
1648 ITask_GetWorkingDirectory(task, &workdir);
1649 ITask_GetWorkItemData(task, &user_data_size, &user_data);
1651 ver = GetVersion();
1652 fixed.product_version = MAKEWORD(ver >> 8, ver);
1653 fixed.file_version = 0x0001;
1654 fixed.name_size_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1655 fixed.trigger_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1656 fixed.trigger_offset += sizeof(USHORT); /* Application Name */
1657 if (appname)
1658 fixed.trigger_offset += (lstrlenW(appname) + 1) * sizeof(WCHAR);
1659 fixed.trigger_offset += sizeof(USHORT); /* Parameters */
1660 if (params)
1661 fixed.trigger_offset += (lstrlenW(params) + 1) * sizeof(WCHAR);
1662 fixed.trigger_offset += sizeof(USHORT); /* Working Directory */
1663 if (workdir)
1664 fixed.trigger_offset += (lstrlenW(workdir) + 1) * sizeof(WCHAR);
1665 fixed.trigger_offset += sizeof(USHORT); /* Author */
1666 if (creator)
1667 fixed.trigger_offset += (lstrlenW(creator) + 1) * sizeof(WCHAR);
1668 fixed.trigger_offset += sizeof(USHORT); /* Comment */
1669 if (comment)
1670 fixed.trigger_offset += (lstrlenW(comment) + 1) * sizeof(WCHAR);
1671 fixed.trigger_offset += sizeof(USHORT) + user_data_size; /* User Data */
1672 fixed.trigger_offset += 10; /* Reserved Data */
1674 fixed.error_retry_count = 0;
1675 fixed.error_retry_interval = 0;
1676 fixed.idle_wait = This->idle_minutes;
1677 fixed.idle_deadline = This->deadline_minutes;
1678 fixed.priority = This->priority;
1679 fixed.maximum_runtime = This->maxRunTime;
1680 fixed.exit_code = This->exit_code;
1681 if (This->status == SCHED_S_TASK_NOT_SCHEDULED && This->trigger_count)
1682 This->status = SCHED_S_TASK_HAS_NOT_RUN;
1683 fixed.status = This->status;
1684 fixed.flags = This->flags;
1685 fixed.last_runtime = This->last_runtime;
1687 try = 1;
1688 for (;;)
1690 hfile = CreateFileW(task_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, disposition, 0, 0);
1691 if (hfile != INVALID_HANDLE_VALUE) break;
1693 if (try++ >= 3)
1695 hr = HRESULT_FROM_WIN32(GetLastError());
1696 goto failed;
1698 Sleep(100);
1701 if (GetLastError() == ERROR_ALREADY_EXISTS)
1702 fixed.uuid = This->uuid;
1703 else
1704 CoCreateGuid(&fixed.uuid);
1706 if (!WriteFile(hfile, &fixed, sizeof(fixed), &size, NULL))
1708 hr = HRESULT_FROM_WIN32(GetLastError());
1709 goto failed;
1712 /* Instance Count: don't touch it in the client */
1713 if (SetFilePointer(hfile, sizeof(word), NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
1715 hr = HRESULT_FROM_WIN32(GetLastError());
1716 goto failed;
1718 /* Application Name */
1719 if (!write_unicode_string(hfile, appname))
1721 hr = HRESULT_FROM_WIN32(GetLastError());
1722 goto failed;
1724 /* Parameters */
1725 if (!write_unicode_string(hfile, params))
1727 hr = HRESULT_FROM_WIN32(GetLastError());
1728 goto failed;
1730 /* Working Directory */
1731 if (!write_unicode_string(hfile, workdir))
1733 hr = HRESULT_FROM_WIN32(GetLastError());
1734 goto failed;
1736 /* Author */
1737 if (!write_unicode_string(hfile, creator))
1739 hr = HRESULT_FROM_WIN32(GetLastError());
1740 goto failed;
1742 /* Comment */
1743 if (!write_unicode_string(hfile, comment))
1745 hr = HRESULT_FROM_WIN32(GetLastError());
1746 goto failed;
1749 /* User Data */
1750 if (!write_user_data(hfile, user_data, user_data_size))
1752 hr = HRESULT_FROM_WIN32(GetLastError());
1753 goto failed;
1756 /* Reserved Data */
1757 if (!write_reserved_data(hfile))
1759 hr = HRESULT_FROM_WIN32(GetLastError());
1760 goto failed;
1763 /* Triggers */
1764 hr = write_triggers(This, hfile);
1765 if (hr != S_OK)
1766 goto failed;
1768 /* Signature */
1769 if (!write_signature(hfile))
1771 hr = HRESULT_FROM_WIN32(GetLastError());
1772 goto failed;
1775 hr = S_OK;
1776 This->is_dirty = FALSE;
1778 failed:
1779 CoTaskMemFree(appname);
1780 CoTaskMemFree(params);
1781 CoTaskMemFree(workdir);
1782 if (creator != authorW)
1783 CoTaskMemFree(creator);
1784 if (comment != commentW)
1785 CoTaskMemFree(comment);
1786 CoTaskMemFree(user_data);
1788 if (hfile != INVALID_HANDLE_VALUE)
1790 CloseHandle(hfile);
1791 if (hr != S_OK)
1792 DeleteFileW(task_name);
1793 else if (remember)
1795 heap_free(This->task_name);
1796 This->task_name = heap_strdupW(task_name);
1799 return hr;
1802 static HRESULT WINAPI MSTASK_IPersistFile_SaveCompleted(
1803 IPersistFile* iface,
1804 LPCOLESTR pszFileName)
1806 FIXME("(%p, %p): stub\n", iface, pszFileName);
1807 return E_NOTIMPL;
1810 static HRESULT WINAPI MSTASK_IPersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *file_name)
1812 TaskImpl *This = impl_from_IPersistFile(iface);
1814 TRACE("(%p, %p)\n", iface, file_name);
1816 *file_name = CoTaskMemAlloc((lstrlenW(This->task_name) + 1) * sizeof(WCHAR));
1817 if (!*file_name) return E_OUTOFMEMORY;
1819 lstrcpyW(*file_name, This->task_name);
1820 return S_OK;
1823 static const ITaskVtbl MSTASK_ITaskVtbl =
1825 MSTASK_ITask_QueryInterface,
1826 MSTASK_ITask_AddRef,
1827 MSTASK_ITask_Release,
1828 MSTASK_ITask_CreateTrigger,
1829 MSTASK_ITask_DeleteTrigger,
1830 MSTASK_ITask_GetTriggerCount,
1831 MSTASK_ITask_GetTrigger,
1832 MSTASK_ITask_GetTriggerString,
1833 MSTASK_ITask_GetRunTimes,
1834 MSTASK_ITask_GetNextRunTime,
1835 MSTASK_ITask_SetIdleWait,
1836 MSTASK_ITask_GetIdleWait,
1837 MSTASK_ITask_Run,
1838 MSTASK_ITask_Terminate,
1839 MSTASK_ITask_EditWorkItem,
1840 MSTASK_ITask_GetMostRecentRunTime,
1841 MSTASK_ITask_GetStatus,
1842 MSTASK_ITask_GetExitCode,
1843 MSTASK_ITask_SetComment,
1844 MSTASK_ITask_GetComment,
1845 MSTASK_ITask_SetCreator,
1846 MSTASK_ITask_GetCreator,
1847 MSTASK_ITask_SetWorkItemData,
1848 MSTASK_ITask_GetWorkItemData,
1849 MSTASK_ITask_SetErrorRetryCount,
1850 MSTASK_ITask_GetErrorRetryCount,
1851 MSTASK_ITask_SetErrorRetryInterval,
1852 MSTASK_ITask_GetErrorRetryInterval,
1853 MSTASK_ITask_SetFlags,
1854 MSTASK_ITask_GetFlags,
1855 MSTASK_ITask_SetAccountInformation,
1856 MSTASK_ITask_GetAccountInformation,
1857 MSTASK_ITask_SetApplicationName,
1858 MSTASK_ITask_GetApplicationName,
1859 MSTASK_ITask_SetParameters,
1860 MSTASK_ITask_GetParameters,
1861 MSTASK_ITask_SetWorkingDirectory,
1862 MSTASK_ITask_GetWorkingDirectory,
1863 MSTASK_ITask_SetPriority,
1864 MSTASK_ITask_GetPriority,
1865 MSTASK_ITask_SetTaskFlags,
1866 MSTASK_ITask_GetTaskFlags,
1867 MSTASK_ITask_SetMaxRunTime,
1868 MSTASK_ITask_GetMaxRunTime
1871 static const IPersistFileVtbl MSTASK_IPersistFileVtbl =
1873 MSTASK_IPersistFile_QueryInterface,
1874 MSTASK_IPersistFile_AddRef,
1875 MSTASK_IPersistFile_Release,
1876 MSTASK_IPersistFile_GetClassID,
1877 MSTASK_IPersistFile_IsDirty,
1878 MSTASK_IPersistFile_Load,
1879 MSTASK_IPersistFile_Save,
1880 MSTASK_IPersistFile_SaveCompleted,
1881 MSTASK_IPersistFile_GetCurFile
1884 HRESULT TaskConstructor(ITaskService *service, const WCHAR *name, ITask **task)
1886 static const WCHAR tasksW[] = { '\\','T','a','s','k','s','\\',0 };
1887 static const WCHAR jobW[] = { '.','j','o','b',0 };
1888 TaskImpl *This;
1889 WCHAR task_name[MAX_PATH];
1890 ITaskDefinition *taskdef;
1891 IActionCollection *actions;
1892 HRESULT hr;
1894 TRACE("(%s, %p)\n", debugstr_w(name), task);
1896 if (wcschr(name, '.')) return E_INVALIDARG;
1898 GetWindowsDirectoryW(task_name, MAX_PATH);
1899 lstrcatW(task_name, tasksW);
1900 lstrcatW(task_name, name);
1901 lstrcatW(task_name, jobW);
1903 hr = ITaskService_NewTask(service, 0, &taskdef);
1904 if (hr != S_OK) return hr;
1906 This = heap_alloc(sizeof(*This));
1907 if (!This)
1909 ITaskDefinition_Release(taskdef);
1910 return E_OUTOFMEMORY;
1913 This->ITask_iface.lpVtbl = &MSTASK_ITaskVtbl;
1914 This->IPersistFile_iface.lpVtbl = &MSTASK_IPersistFileVtbl;
1915 This->ref = 1;
1916 This->task = taskdef;
1917 This->data = NULL;
1918 This->data_count = 0;
1919 This->task_name = heap_strdupW(task_name);
1920 This->flags = 0;
1921 This->status = SCHED_S_TASK_NOT_SCHEDULED;
1922 This->exit_code = 0;
1923 This->idle_minutes = 10;
1924 This->deadline_minutes = 60;
1925 This->priority = NORMAL_PRIORITY_CLASS;
1926 This->accountName = NULL;
1927 This->trigger_count = 0;
1928 This->trigger = NULL;
1929 This->is_dirty = FALSE;
1930 This->instance_count = 0;
1932 memset(&This->last_runtime, 0, sizeof(This->last_runtime));
1933 CoCreateGuid(&This->uuid);
1935 /* Default time is 3 days = 259200000 ms */
1936 This->maxRunTime = 259200000;
1938 hr = ITaskDefinition_get_Actions(This->task, &actions);
1939 if (hr == S_OK)
1941 hr = IActionCollection_Create(actions, TASK_ACTION_EXEC, (IAction **)&This->action);
1942 IActionCollection_Release(actions);
1943 if (hr == S_OK)
1945 *task = &This->ITask_iface;
1946 InterlockedIncrement(&dll_ref);
1947 return S_OK;
1951 ITaskDefinition_Release(This->task);
1952 ITask_Release(&This->ITask_iface);
1953 return hr;