summaryrefslogtreecommitdiffstats
path: root/src/util.cpp
blob: 7474fa1ca3ce49a34e1e2cb839c8bf07eadddcc9 (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
/*******************************************************************************
  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!
*******************************************************************************/

// TDE
#include <kdebug.h>

// Portal
#include "util.h"

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;
}