1 /** @file processcheckdialog.cpp Dialog for checking running processes on Windows.
2  * @ingroup updater
3  *
4  * @authors Copyright © 2012-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
5  *
6  * @par License
7  * GPL: http://www.gnu.org/licenses/gpl.html
8  *
9  * <small>This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version. This program is distributed in the hope that it
13  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details. You should have received a copy of the GNU
16  * General Public License along with this program; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA</small>
19  */
20 
21 #include "updater/processcheckdialog.h"
22 #include "ui/clientwindow.h"
23 
24 #include <QProcess>
25 #include <de/MessageDialog>
26 
27 #ifdef WIN32
28 
isProcessRunning(char const * name)29 static bool isProcessRunning(char const *name)
30 {
31     QProcess wmic;
32     wmic.start("wmic.exe", QStringList() << "PROCESS" << "get" << "Caption");
33     if (!wmic.waitForStarted()) return false;
34     if (!wmic.waitForFinished()) return false;
35 
36     QByteArray result = wmic.readAll();
37     foreach (QString p, QString(result).split("\n", QString::SkipEmptyParts))
38     {
39         if (!p.trimmed().compare(QLatin1String(name), Qt::CaseInsensitive))
40             return true;
41     }
42     return false;
43 }
44 
Updater_AskToStopProcess(char const * processName,char const * message)45 dd_bool Updater_AskToStopProcess(char const *processName, char const *message)
46 {
47     using namespace de;
48 
49     while (isProcessRunning(processName))
50     {
51         MessageDialog *msg = new MessageDialog;
52         msg->setDeleteAfterDismissed(true);
53         msg->title().setText(QObject::tr("Files In Use"));
54         msg->message().setText(QString(message) + "\n\n" _E(2) +
55                                QObject::tr("There is a running process called %1.")
56                                .arg(_E(b) + QString(processName) + _E(.)));
57 
58         msg->buttons()
59                 << new DialogButtonItem(DialogWidget::Accept | DialogWidget::Default, QObject::tr("Retry"))
60                 << new DialogButtonItem(DialogWidget::Reject, QObject::tr("Ignore"));
61 
62         // Show a notification dialog.
63         if (!msg->exec(ClientWindow::main().root()))
64         {
65             return !isProcessRunning(processName);
66         }
67     }
68     return true;
69 }
70 
71 #endif // WIN32
72