File indexing completed on 2024-06-23 05:14:19

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     utils/types.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <config-kleopatra.h>
0011 
0012 #include "utils/gui-helper.h"
0013 #include "utils/types.h"
0014 
0015 #include <QList>
0016 #include <QWidget>
0017 
0018 #ifdef Q_OS_WIN
0019 #include <windows.h>
0020 #endif
0021 
0022 using namespace Kleo;
0023 
0024 class ExecutionContextUser::Private
0025 {
0026     friend class ::Kleo::ExecutionContextUser;
0027     ExecutionContextUser *const q;
0028 
0029 public:
0030     explicit Private(const std::shared_ptr<const ExecutionContext> &ctx, ExecutionContextUser *qq)
0031         : q(qq)
0032         , executionContext(ctx)
0033         , idApplied()
0034     {
0035     }
0036 
0037 private:
0038     void applyWindowID(QWidget *w);
0039 
0040 private:
0041     std::weak_ptr<const ExecutionContext> executionContext;
0042     QList<QWidget *> idApplied;
0043 };
0044 
0045 void ExecutionContextUser::applyWindowID(QWidget *wid)
0046 {
0047     if (d->idApplied.contains(wid)) {
0048         return;
0049     }
0050     if (const std::shared_ptr<const ExecutionContext> ctx = d->executionContext.lock()) {
0051         ctx->applyWindowID(wid);
0052         d->idApplied.append(wid);
0053     }
0054 }
0055 
0056 ExecutionContextUser::ExecutionContextUser()
0057     : d(new Private(std::shared_ptr<const ExecutionContext>(), this))
0058 {
0059 }
0060 
0061 ExecutionContextUser::ExecutionContextUser(const std::shared_ptr<const ExecutionContext> &ctx)
0062     : d(new Private(ctx, this))
0063 {
0064 }
0065 
0066 ExecutionContextUser::~ExecutionContextUser()
0067 {
0068 }
0069 
0070 void ExecutionContextUser::setExecutionContext(const std::shared_ptr<const ExecutionContext> &ctx)
0071 {
0072     d->executionContext = ctx;
0073     d->idApplied.clear();
0074 }
0075 
0076 std::shared_ptr<const ExecutionContext> ExecutionContextUser::executionContext() const
0077 {
0078     return d->executionContext.lock();
0079 }
0080 
0081 void ExecutionContextUser::bringToForeground(QWidget *wid, bool stayOnTop)
0082 {
0083     applyWindowID(wid);
0084     wid->show();
0085     aggressive_raise(wid, stayOnTop);
0086 }