aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/terminalhooks.h
blob: b37c51517c026d659ed46ce3caf5f25516a4e692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include "commandline.h"
#include "environment.h"
#include "filepath.h"
#include "id.h"

#include <functional>
#include <memory>

namespace Utils {
class ProcessInterface;

template<typename R, typename... Params>
class Hook
{
    Q_DISABLE_COPY_MOVE(Hook)

public:
    using Callback = std::function<R(Params...)>;

public:
    Hook() = delete;

    explicit Hook(Callback defaultCallback) { set(defaultCallback); }

    void set(Callback cb) { m_callback = cb; }
    R operator()(Params &&...params) { return m_callback(std::forward<Params>(params)...); }

private:
    Callback m_callback;
};

namespace Terminal {
class HooksPrivate;

enum class ExitBehavior { Close, Restart, Keep };

struct OpenTerminalParameters
{
    std::optional<CommandLine> shellCommand;
    std::optional<FilePath> workingDirectory;
    std::optional<Environment> environment;
    ExitBehavior m_exitBehavior{ExitBehavior::Close};
    std::optional<Id> identifier{std::nullopt};
};

struct NameAndCommandLine
{
    QString name;
    CommandLine commandLine;
};

QTCREATOR_UTILS_EXPORT FilePath defaultShellForDevice(const FilePath &deviceRoot);

class QTCREATOR_UTILS_EXPORT Hooks
{
public:
    using OpenTerminal = std::function<void(const OpenTerminalParameters &)>;
    using CreateTerminalProcessInterface = std::function<ProcessInterface *()>;

    struct CallbackSet
    {
        OpenTerminal openTerminal;
        CreateTerminalProcessInterface createTerminalProcessInterface;
    };

    using GetTerminalCommandsForDevicesHook = Hook<QList<NameAndCommandLine>>;

public:
    static Hooks &instance();
    ~Hooks();

    GetTerminalCommandsForDevicesHook &getTerminalCommandsForDevicesHook();

    void openTerminal(const OpenTerminalParameters &parameters) const;
    ProcessInterface *createTerminalProcessInterface() const;

    void addCallbackSet(const QString &name, const CallbackSet &callbackSet);
    void removeCallbackSet(const QString &name);

private:
    Hooks();
    std::unique_ptr<HooksPrivate> d;
};

} // namespace Terminal
} // namespace Utils