andre@827: #ifdef WIN32 andre@827: #include "taskscheduler.h" andre@827: andre@827: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik andre@827: * Software engineering by Intevation GmbH andre@827: * andre@827: * This file is Free Software under the GNU GPL (v>=2) andre@827: * and comes with ABSOLUTELY NO WARRANTY! andre@827: * See LICENSE.txt for details. andre@827: */ andre@827: andre@827: #include andre@827: andre@827: #include andre@827: #include andre@827: #include andre@827: andre@827: #define TASK_NAME L"TrustBridge Update" andre@827: andre@827: TaskScheduler::TaskScheduler() : andre@827: mInitialized (false), andre@827: mTaskScheduler (NULL) andre@827: { andre@827: HRESULT hr; andre@827: andre@827: hr = CoInitialize(NULL); andre@827: if(FAILED(hr)) { andre@827: qDebug() << "TaskScheduler CoInitializeEx failed: " << hr; andre@827: return; andre@827: } andre@827: andre@827: hr = CoCreateInstance(CLSID_CTaskScheduler, andre@827: NULL, andre@827: CLSCTX_INPROC_SERVER, andre@827: IID_ITaskScheduler, andre@827: (void**)&mTaskScheduler); andre@827: if(FAILED(hr)) { andre@827: qDebug() << "TaskScheduler create instance failed: " << hr; andre@827: CoUninitialize(); andre@827: return; andre@827: } andre@827: andre@827: mInitialized = true; andre@827: qDebug() << "Initialization done"; andre@827: return; andre@827: } andre@827: andre@827: TaskScheduler::~TaskScheduler() { andre@827: qDebug() << "TaskSched dtor;"; andre@827: andre@827: if (!isInitialized()) { andre@827: return; andre@827: } andre@827: mTaskScheduler->Release(); andre@827: CoUninitialize(); andre@827: } andre@827: andre@827: bool createTrigger(ITask *task, const QTime &startTime) andre@827: { andre@827: ITaskTrigger *pITaskTrigger = NULL; andre@827: WORD piNewTrigger = 0; andre@827: HRESULT hr; andre@827: TASK_TRIGGER pTrigger; andre@827: ZeroMemory(&pTrigger, sizeof (TASK_TRIGGER)); andre@827: QDate startdate = QDate::currentDate(); andre@827: andre@827: if (!startTime.isValid() || task == NULL) { andre@827: qDebug() << "Invalid args."; andre@827: return false; andre@827: } andre@827: andre@827: hr = task->CreateTrigger(&piNewTrigger, andre@827: &pITaskTrigger); andre@827: andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to create trigger."; andre@827: return false; andre@827: } andre@827: andre@827: pTrigger.wBeginDay = startdate.day(); andre@827: pTrigger.wBeginMonth = startdate.month(); andre@827: pTrigger.wBeginYear = startdate.year(); andre@827: pTrigger.wStartHour = startTime.hour(); andre@827: pTrigger.wStartMinute = startTime.minute(); andre@827: pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER); andre@827: pTrigger.TriggerType = TASK_TIME_TRIGGER_DAILY; andre@827: pTrigger.Type.Daily.DaysInterval = 1; andre@827: andre@827: hr = pITaskTrigger->SetTrigger(&pTrigger); andre@827: andre@827: pITaskTrigger->Release(); andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to create trigger."; andre@827: return false; andre@827: } andre@827: andre@827: return true; andre@827: } andre@827: andre@827: bool TaskScheduler::createDailyTask(const QString &executable, andre@827: const QString &args, andre@827: const QTime &startTime) { andre@827: ITask *task = NULL; andre@827: IPersistFile *persistFile = NULL; andre@827: HRESULT hr; andre@827: andre@827: hr = mTaskScheduler->NewWorkItem(TASK_NAME, andre@827: CLSID_CTask, andre@827: IID_ITask, andre@827: (IUnknown**)&task); andre@827: andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to create new work item."; andre@827: return false; andre@827: } andre@827: andre@827: /* Set the task parameters */ andre@827: hr = task->SetApplicationName(reinterpret_cast (executable.utf16())); andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to set application name."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: hr = task->SetParameters(reinterpret_cast (args.utf16())); andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to set parameters."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: hr = task->SetComment(reinterpret_cast(QObject::tr("TrustBridge automatic update check.").utf16())); andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to set comment."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: QString username = QString::fromLocal8Bit(qgetenv("USERNAME").constData()); andre@827: if (username.isEmpty()) { andre@827: qDebug() << "Failed to obtain username from envrionment."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: hr = task->SetAccountInformation(reinterpret_cast(username.utf16()), NULL); andre@827: andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to set username."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: hr = task->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON); andre@827: andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to set flags."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: /* Now create the trigger for this task */ andre@827: if (!createTrigger(task, startTime)) { andre@827: qDebug() << "Failed to create trigger."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: hr = task->QueryInterface(IID_IPersistFile, andre@827: (void **)&persistFile); andre@827: task->Release(); andre@827: andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to get persist file interface."; andre@827: task->Release(); andre@827: return false; andre@827: } andre@827: andre@827: hr = persistFile->Save(NULL, TRUE); andre@827: persistFile->Release(); andre@827: if (FAILED(hr)) { andre@827: qDebug() << "Failed to save task"; andre@827: return false; andre@827: } andre@827: andre@827: return true; andre@827: } andre@827: #endif /* WIN32 */