comparison ui/taskscheduler.cpp @ 827:8de162b91a22

(Issue49) Create a Scheduled daily task to run trustbridge
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 23 Jul 2014 19:47:59 +0200
parents
children
comparison
equal deleted inserted replaced
826:4aa33c408776 827:8de162b91a22
1 #ifdef WIN32
2 #include "taskscheduler.h"
3
4 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
5 * Software engineering by Intevation GmbH
6 *
7 * This file is Free Software under the GNU GPL (v>=2)
8 * and comes with ABSOLUTELY NO WARRANTY!
9 * See LICENSE.txt for details.
10 */
11
12 #include <windows.h>
13
14 #include <QDate>
15 #include <QDebug>
16 #include <QApplication>
17
18 #define TASK_NAME L"TrustBridge Update"
19
20 TaskScheduler::TaskScheduler() :
21 mInitialized (false),
22 mTaskScheduler (NULL)
23 {
24 HRESULT hr;
25
26 hr = CoInitialize(NULL);
27 if(FAILED(hr)) {
28 qDebug() << "TaskScheduler CoInitializeEx failed: " << hr;
29 return;
30 }
31
32 hr = CoCreateInstance(CLSID_CTaskScheduler,
33 NULL,
34 CLSCTX_INPROC_SERVER,
35 IID_ITaskScheduler,
36 (void**)&mTaskScheduler);
37 if(FAILED(hr)) {
38 qDebug() << "TaskScheduler create instance failed: " << hr;
39 CoUninitialize();
40 return;
41 }
42
43 mInitialized = true;
44 qDebug() << "Initialization done";
45 return;
46 }
47
48 TaskScheduler::~TaskScheduler() {
49 qDebug() << "TaskSched dtor;";
50
51 if (!isInitialized()) {
52 return;
53 }
54 mTaskScheduler->Release();
55 CoUninitialize();
56 }
57
58 bool createTrigger(ITask *task, const QTime &startTime)
59 {
60 ITaskTrigger *pITaskTrigger = NULL;
61 WORD piNewTrigger = 0;
62 HRESULT hr;
63 TASK_TRIGGER pTrigger;
64 ZeroMemory(&pTrigger, sizeof (TASK_TRIGGER));
65 QDate startdate = QDate::currentDate();
66
67 if (!startTime.isValid() || task == NULL) {
68 qDebug() << "Invalid args.";
69 return false;
70 }
71
72 hr = task->CreateTrigger(&piNewTrigger,
73 &pITaskTrigger);
74
75 if (FAILED(hr)) {
76 qDebug() << "Failed to create trigger.";
77 return false;
78 }
79
80 pTrigger.wBeginDay = startdate.day();
81 pTrigger.wBeginMonth = startdate.month();
82 pTrigger.wBeginYear = startdate.year();
83 pTrigger.wStartHour = startTime.hour();
84 pTrigger.wStartMinute = startTime.minute();
85 pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER);
86 pTrigger.TriggerType = TASK_TIME_TRIGGER_DAILY;
87 pTrigger.Type.Daily.DaysInterval = 1;
88
89 hr = pITaskTrigger->SetTrigger(&pTrigger);
90
91 pITaskTrigger->Release();
92 if (FAILED(hr)) {
93 qDebug() << "Failed to create trigger.";
94 return false;
95 }
96
97 return true;
98 }
99
100 bool TaskScheduler::createDailyTask(const QString &executable,
101 const QString &args,
102 const QTime &startTime) {
103 ITask *task = NULL;
104 IPersistFile *persistFile = NULL;
105 HRESULT hr;
106
107 hr = mTaskScheduler->NewWorkItem(TASK_NAME,
108 CLSID_CTask,
109 IID_ITask,
110 (IUnknown**)&task);
111
112 if (FAILED(hr)) {
113 qDebug() << "Failed to create new work item.";
114 return false;
115 }
116
117 /* Set the task parameters */
118 hr = task->SetApplicationName(reinterpret_cast<LPCWSTR> (executable.utf16()));
119 if (FAILED(hr)) {
120 qDebug() << "Failed to set application name.";
121 task->Release();
122 return false;
123 }
124
125 hr = task->SetParameters(reinterpret_cast<LPCWSTR> (args.utf16()));
126 if (FAILED(hr)) {
127 qDebug() << "Failed to set parameters.";
128 task->Release();
129 return false;
130 }
131
132 hr = task->SetComment(reinterpret_cast<LPCWSTR>(QObject::tr("TrustBridge automatic update check.").utf16()));
133 if (FAILED(hr)) {
134 qDebug() << "Failed to set comment.";
135 task->Release();
136 return false;
137 }
138
139 QString username = QString::fromLocal8Bit(qgetenv("USERNAME").constData());
140 if (username.isEmpty()) {
141 qDebug() << "Failed to obtain username from envrionment.";
142 task->Release();
143 return false;
144 }
145
146 hr = task->SetAccountInformation(reinterpret_cast<LPCWSTR>(username.utf16()), NULL);
147
148 if (FAILED(hr)) {
149 qDebug() << "Failed to set username.";
150 task->Release();
151 return false;
152 }
153
154 hr = task->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
155
156 if (FAILED(hr)) {
157 qDebug() << "Failed to set flags.";
158 task->Release();
159 return false;
160 }
161
162 /* Now create the trigger for this task */
163 if (!createTrigger(task, startTime)) {
164 qDebug() << "Failed to create trigger.";
165 task->Release();
166 return false;
167 }
168
169 hr = task->QueryInterface(IID_IPersistFile,
170 (void **)&persistFile);
171 task->Release();
172
173 if (FAILED(hr)) {
174 qDebug() << "Failed to get persist file interface.";
175 task->Release();
176 return false;
177 }
178
179 hr = persistFile->Save(NULL, TRUE);
180 persistFile->Release();
181 if (FAILED(hr)) {
182 qDebug() << "Failed to save task";
183 return false;
184 }
185
186 return true;
187 }
188 #endif /* WIN32 */

http://wald.intevation.org/projects/trustbridge/