File indexing completed on 2024-04-28 05:49:55

0001 //  SPDX-FileCopyrightText: 2017 David Faure <faure@kde.org>
0002 //  SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "utils.h"
0005 
0006 #include <QDebug>
0007 #include <QScreen>
0008 #include <QWidget>
0009 
0010 #include <config-kdialog.h>
0011 
0012 #if defined HAVE_X11 && !defined K_WS_QTONLY
0013 #include <X11/Xlib.h>
0014 #include <X11/Xutil.h>
0015 #endif
0016 
0017 static QString xGeometry;
0018 
0019 void Utils::setGeometry(const QString &geometry)
0020 {
0021     xGeometry = geometry;
0022 }
0023 
0024 void Utils::handleXGeometry(QWidget *dlg)
0025 {
0026 #ifdef HAVE_X11
0027     if (!xGeometry.isEmpty()) {
0028         const QRect screenGeom = dlg->screen()->availableGeometry();
0029         int x;
0030         int y;
0031         int w;
0032         int h;
0033         const int m = XParseGeometry(xGeometry.toLatin1().constData(), &x, &y, (unsigned int *)&w, (unsigned int *)&h);
0034         if ((m & XNegative)) {
0035             x = screenGeom.width() + x - w;
0036         }
0037         if ((m & YNegative)) {
0038             y = screenGeom.height() + y - h;
0039         }
0040         dlg->setGeometry(x, y, w, h);
0041         qDebug() << "x: " << x << "  y: " << y << "  w: " << w << "  h: " << h;
0042     }
0043 #endif
0044 }
0045 
0046 QString Utils::parseString(const QString &str)
0047 {
0048     QString ret;
0049     ret.reserve(str.size());
0050     bool escaped = false;
0051     for (const QChar c : str) {
0052         if (escaped) {
0053             escaped = false;
0054             if (c == QLatin1Char('\\')) {
0055                 ret += c;
0056             } else if (c == QLatin1Char('n')) {
0057                 ret += QLatin1Char('\n');
0058             } else {
0059                 qWarning() << qPrintable(QString::fromLatin1("Unrecognized escape sequence \\%1").arg(c));
0060                 ret += QLatin1Char('\\');
0061                 ret += c;
0062             }
0063         } else {
0064             if (c == QLatin1Char('\\')) {
0065                 escaped = true;
0066             } else {
0067                 ret += c;
0068             }
0069         }
0070     }
0071     if (escaped) {
0072         qWarning() << "Unterminated escape sequence";
0073         ret += QLatin1Char('\\');
0074     }
0075     return ret;
0076 }