summaryrefslogtreecommitdiffstats
path: root/src/util.cpp
blob: 4975663a911b4bf01473e76dfadb0734065c3c72 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
  XDG desktop portal implementation for TDE
  Copyright © 2024 Mavridis Philippe <mavridisf@gmail.com>

  This program or library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
  details.

  You should have received a copy of the GNU Lesser General Public License
  along with this library; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

  Improvements and feedback are welcome!
*******************************************************************************/

// TQt
#include <tqfileinfo.h>

// TDE
#include <kservice.h>
#include <kdebug.h>

// Portal
#include "util.h"

WId parse_window_id(const TQString data)
{
    if (!data.startsWith("x11:"))
    {
        kdWarning() << "[FileChooser] Window Identifiers are currently only "
                << "supported for X11. Created dialog will be parentless."
                << endl;
        return 0;
    }

    bool ok;
    WId wid = data.mid(4).toInt(&ok, 16);
    return ok ? wid : 0;
}

ApplicationInfo application_info_from_wid(WId wid)
{
    ApplicationInfo app;
    if (wid)
    {
        // we need to get the pid but only deprecated KWin::Info has it
        KWin::Info info = KWin::info(wid);
        TQFileInfo fi(TQString("/proc/%1/exe").arg(info.pid));
        if (fi.exists() && fi.isSymLink()) {
            fi = TQFileInfo(fi.readLink());

            app.path = fi.filePath();

            // try to find corresponding desktop file
            KService::List all = KService::allServices();
            KService::List::ConstIterator svc;
            for (svc = all.begin(); svc != all.end(); ++svc) {
                TQString exec((*svc)->exec());
                if (exec.startsWith(fi.filePath()) ||
                    exec.startsWith(fi.fileName()))
                {
                    app.desktopFile = (*svc)->desktopEntryPath();
                    app.name = (*svc)->name();
                    break;
                }
            }

            // Last resort
            if (app.name.isEmpty())
                app.name = fi.fileName();
        }
    }
    return app;
}

bool check_variant(TQT_DBusVariant variant, TQString signature)
{
    return !variant.signature.isNull() && variant.signature == signature;
}

TQString bytelist_to_string(TQT_DBusDataList list)
{
    if (list.type() != TQT_DBusData::Byte) return TQString::null;
    TQCString bytes;
    TQValueList<TQ_UINT8> vl = list.toByteList();
    TQValueList<TQ_UINT8>::iterator vlit;
    for (vlit = vl.begin(); vlit != vl.end(); ++vlit)
    {
        bytes += (*vlit);
    }
    return TQString::fromLocal8Bit(bytes.data(), bytes.size());
}

TQT_DBusDataList string_to_bytelist(TQString str)
{
    TQCString bytes = str.latin1();
    TQValueList<TQ_UINT8> vl;
    char *bit;
    for (bit = bytes.begin(); bit != bytes.end(); ++bit)
    {
        vl << (TQ_UINT8)(*bit);
    }
    return TQT_DBusDataList(vl);
}

TQT_DBusDataList kurl_list_to_datalist(KURL::List urls)
{
    return TQT_DBusDataList(urls.toStringList());
}

bool make_variant(TQVariant variant, TQT_DBusVariant &other)
{
    TQString signature;
    switch (variant.type()) {
        case TQVariant::String:
        case TQVariant::Color:
        case TQVariant::Font:
        case TQVariant::Date:
        case TQVariant::DateTime:
        case TQVariant::Time:
        case TQVariant::KeySequence:
            other.signature = "s";
            other.value = TQT_DBusData::fromString(variant.toString());
            break;

        case TQVariant::Int:
            other.signature = "i";
            other.value = TQT_DBusData::fromInt32(variant.toInt());
            break;

        case TQVariant::UInt:
            other.signature = "u";
            other.value = TQT_DBusData::fromUInt32(variant.toUInt());
            break;

        case TQVariant::Double:
        case TQVariant::LongLong:
        case TQVariant::ULongLong:
            other.signature = "d";
            other.value = TQT_DBusData::fromDouble(variant.toDouble());
            break;

        case TQVariant::Bool:
            other.signature = "b";
            other.value = TQT_DBusData::fromBool(variant.toBool());
            break;

        case TQVariant::StringList:
            other.signature = "as";
            other.value = TQT_DBusData::fromList(TQT_DBusDataList(variant.toStringList()));
            break;

        default:
            if (variant.canCast(TQVariant::String)) {
                other.signature = "s";
                other.value = TQT_DBusData::fromString(variant.toString());
            }
            else {
                kdDebug() << "Cannot cast TQVariant of type " << variant.type()
                          << "to TQT_DBusVariant!" << endl;
                return false;
            }
    }
    return true;
}