From 091ae3db3137f3a57fbae7cce728beda5be17b1d Mon Sep 17 00:00:00 2001
From: Jake Petroules <jake.petroules@qt.io>
Date: Wed, 5 Jul 2017 20:21:32 -0700
Subject: [PATCH] Remove the last usage of deprecated Carbon APIs from Qt
 Creator

Change-Id: I05c8345e5f1618e7ecbb8a9c27feb5b9a321dc11
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io>
Reviewed-by: hjk <hjk@qt.io>
---
 src/libs/utils/processhandle.cpp              |  7 ++++
 src/libs/utils/processhandle.h                |  2 +
 src/libs/utils/processhandle_mac.mm           | 39 +++++++++++++++++++
 src/libs/utils/utils-lib.pri                  |  5 ++-
 src/libs/utils/utils.qbs                      | 10 ++++-
 .../projectexplorer/projectexplorer.pro       |  2 -
 .../projectexplorer/projectexplorer.qbs       |  4 --
 .../projectexplorer/runconfiguration.cpp      | 25 +-----------
 .../projectexplorer/runconfiguration.h        |  1 -
 9 files changed, 61 insertions(+), 34 deletions(-)
 create mode 100644 src/libs/utils/processhandle_mac.mm

diff --git a/src/libs/utils/processhandle.cpp b/src/libs/utils/processhandle.cpp
index 5ce2571463a..51e231b957d 100644
--- a/src/libs/utils/processhandle.cpp
+++ b/src/libs/utils/processhandle.cpp
@@ -68,4 +68,11 @@ bool ProcessHandle::equals(const ProcessHandle &rhs) const
     return m_pid == rhs.m_pid;
 }
 
+#ifndef Q_OS_MACOS
+bool ProcessHandle::activate()
+{
+    return false;
+}
+#endif
+
 } // Utils
diff --git a/src/libs/utils/processhandle.h b/src/libs/utils/processhandle.h
index bbd942b8750..7b5bb639238 100644
--- a/src/libs/utils/processhandle.h
+++ b/src/libs/utils/processhandle.h
@@ -43,6 +43,8 @@ public:
 
     bool equals(const ProcessHandle &) const;
 
+    bool activate();
+
 private:
     qint64 m_pid;
 };
diff --git a/src/libs/utils/processhandle_mac.mm b/src/libs/utils/processhandle_mac.mm
new file mode 100644
index 00000000000..adc39e24d37
--- /dev/null
+++ b/src/libs/utils/processhandle_mac.mm
@@ -0,0 +1,39 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+****************************************************************************/
+
+#include "processhandle.h"
+#import <AppKit/AppKit.h>
+
+namespace Utils {
+
+bool ProcessHandle::activate()
+{
+    NSRunningApplication *app = [NSRunningApplication
+            runningApplicationWithProcessIdentifier:pid()];
+    return app && [app activateWithOptions:static_cast<NSApplicationActivationOptions>(
+            NSApplicationActivateIgnoringOtherApps)];
+}
+
+} // Utils
diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri
index a20a41d1596..bfc9d6084e1 100644
--- a/src/libs/utils/utils-lib.pri
+++ b/src/libs/utils/utils-lib.pri
@@ -252,8 +252,9 @@ osx {
     HEADERS += \
         $$PWD/fileutils_mac.h
     OBJECTIVE_SOURCES += \
-        $$PWD/fileutils_mac.mm
-    LIBS += -framework Foundation
+        $$PWD/fileutils_mac.mm \
+        $$PWD/processhandle_mac.mm
+    LIBS += -framework Foundation -framework AppKit
 }
 
 include(mimetypes/mimetypes.pri)
diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs
index 03a47ae060a..559129df164 100644
--- a/src/libs/utils/utils.qbs
+++ b/src/libs/utils/utils.qbs
@@ -30,7 +30,7 @@ Project {
 
         Properties {
             condition: qbs.targetOS.contains("macos")
-            cpp.frameworks: ["Foundation"]
+            cpp.frameworks: ["Foundation", "AppKit"]
         }
 
         Depends { name: "Qt"; submodules: ["concurrent", "network", "qml", "widgets"] }
@@ -297,6 +297,14 @@ Project {
             ]
         }
 
+        Group {
+            name: "ProcessHandle_macos"
+            condition: qbs.targetOS.contains("macos")
+            files: [
+                "processhandle_mac.mm",
+            ]
+        }
+
         Group {
             name: "MimeTypes"
             prefix: "mimetypes/"
diff --git a/src/plugins/projectexplorer/projectexplorer.pro b/src/plugins/projectexplorer/projectexplorer.pro
index 31987ee6ee0..21d97fb2c27 100644
--- a/src/plugins/projectexplorer/projectexplorer.pro
+++ b/src/plugins/projectexplorer/projectexplorer.pro
@@ -330,8 +330,6 @@ journald {
     LIBS += -lsystemd
 }
 
-macx:LIBS += -framework Carbon
-
 RESOURCES += projectexplorer.qrc
 
 # Some way to override the architecture used in Abi:
diff --git a/src/plugins/projectexplorer/projectexplorer.qbs b/src/plugins/projectexplorer/projectexplorer.qbs
index f61ab5a221a..47bc9d39478 100644
--- a/src/plugins/projectexplorer/projectexplorer.qbs
+++ b/src/plugins/projectexplorer/projectexplorer.qbs
@@ -15,10 +15,6 @@ Project {
         Depends { name: "TextEditor" }
 
         cpp.defines: base.concat("QTC_CPU=X86Architecture")
-        Properties {
-            condition: qbs.targetOS.contains("macos")
-            cpp.frameworks: base.concat(["Carbon"])
-        }
 
         Group {
             name: "General"
diff --git a/src/plugins/projectexplorer/runconfiguration.cpp b/src/plugins/projectexplorer/runconfiguration.cpp
index 1f4b9eee23d..ab597a98fbd 100644
--- a/src/plugins/projectexplorer/runconfiguration.cpp
+++ b/src/plugins/projectexplorer/runconfiguration.cpp
@@ -704,11 +704,6 @@ public:
     RunControlState state = RunControlState::Initialized;
 
     QList<QPointer<RunWorker>> m_workers;
-
-#ifdef Q_OS_OSX
-    // This is used to bring apps in the foreground on Mac
-    int foregroundCount;
-#endif
 };
 
 } // Internal
@@ -1242,25 +1237,7 @@ void RunControlPrivate::debugMessage(const QString &msg)
 */
 void RunControl::bringApplicationToForeground()
 {
-#ifdef Q_OS_OSX
-    d->foregroundCount = 0;
-    bringApplicationToForegroundInternal();
-#endif
-}
-
-void RunControl::bringApplicationToForegroundInternal()
-{
-#ifdef Q_OS_OSX
-    ProcessSerialNumber psn;
-    GetProcessForPID(d->applicationProcessHandle.pid(), &psn);
-    if (SetFrontProcess(&psn) == procNotFound && d->foregroundCount < 15) {
-        // somehow the mac/carbon api says
-        // "-600 no eligible process with specified process id"
-        // if we call SetFrontProcess too early
-        ++d->foregroundCount;
-        QTimer::singleShot(200, this, &RunControl::bringApplicationToForegroundInternal);
-    }
-#endif
+    d->applicationProcessHandle.activate();
 }
 
 void RunControl::appendMessage(const QString &msg, Utils::OutputFormat format)
diff --git a/src/plugins/projectexplorer/runconfiguration.h b/src/plugins/projectexplorer/runconfiguration.h
index 08efd90f097..31c0e88692a 100644
--- a/src/plugins/projectexplorer/runconfiguration.h
+++ b/src/plugins/projectexplorer/runconfiguration.h
@@ -488,7 +488,6 @@ private:
     friend class Internal::RunWorkerPrivate;
 
     static void addWorkerFactory(const WorkerFactory &workerFactory);
-    void bringApplicationToForegroundInternal();
     Internal::RunControlPrivate *d;
 };
 
-- 
GitLab