1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2013 David Faure <faure@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
6 | */ |
7 | |
8 | #include "kjobwidgets.h" |
9 | #include <QPointer> |
10 | #include <QVariant> |
11 | #include <QWidget> |
12 | #include <QWindow> |
13 | |
14 | void KJobWidgets::setWindow(QObject *job, QWidget *widget) |
15 | { |
16 | QPointer<QWidget> p = widget; |
17 | job->setProperty(name: "widget" , value: QVariant::fromValue(value: p)); |
18 | |
19 | QPointer<QWindow> windowHandle = widget ? widget->windowHandle() : nullptr; |
20 | job->setProperty(name: "window" , value: QVariant::fromValue(value: windowHandle)); |
21 | if (windowHandle) { |
22 | job->setProperty(name: "window-id" , value: QVariant::fromValue(value: windowHandle->winId())); |
23 | } |
24 | } |
25 | |
26 | #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(6, 5) |
27 | void KJobWidgets::setWindowHandle(QObject *job, QWindow *window) |
28 | { |
29 | job->setProperty(name: "window" , value: QVariant::fromValue(value: window)); |
30 | if (window) { |
31 | job->setProperty(name: "window-id" , value: QVariant::fromValue(value: window->winId())); |
32 | } |
33 | } |
34 | #endif |
35 | |
36 | QWidget *KJobWidgets::window(QObject *job) |
37 | { |
38 | return job->property(name: "widget" ).value<QPointer<QWidget>>(); |
39 | } |
40 | |
41 | #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(6, 5) |
42 | QWindow *KJobWidgets::windowHandle(QObject *job) |
43 | { |
44 | return job->property(name: "window" ).value<QPointer<QWindow>>(); |
45 | } |
46 | #endif |
47 | |
48 | // duplicated from kwindowsystem |
49 | static int timestampCompare(unsigned long time1_, unsigned long time2_) // like strcmp() |
50 | { |
51 | quint32 time1 = time1_; |
52 | quint32 time2 = time2_; |
53 | if (time1 == time2) { |
54 | return 0; |
55 | } |
56 | return quint32(time1 - time2) < 0x7fffffffU ? 1 : -1; // time1 > time2 -> 1, handle wrapping |
57 | } |
58 | |
59 | void KJobWidgets::updateUserTimestamp(QObject *job, unsigned long time) |
60 | { |
61 | unsigned long currentTimestamp = userTimestamp(job); |
62 | if (currentTimestamp == 0 || timestampCompare(time1_: time, time2_: currentTimestamp) > 0) { |
63 | job->setProperty(name: "userTimestamp" , value: qulonglong(time)); |
64 | } |
65 | } |
66 | |
67 | unsigned long KJobWidgets::userTimestamp(QObject *job) |
68 | { |
69 | return job->property(name: "userTimestamp" ).toULongLong(); |
70 | } |
71 | |