summaryrefslogtreecommitdiffstats
path: root/src/tdebluezioslave/obex/obex.cpp
blob: 63d608263bc69ab9dd57a2c320d93d2fd0548446 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 This file is part of tdeio_obex.

 Copyright (c) 2003 Mathias Froehlich <Mathias.Froehlich@web.de>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License as published by the Free Software Foundation; either
 version 2 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
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
 Boston, MA 02110-1301, USA.
 */

#include "obex.h"

#include <kdebug.h>
#include <tqapplication.h>
#include <tqeventloop.h>
#include <tqregexp.h>

#include <sys/stat.h>

#include <tqdbusvariant.h>

#define MINIMUM_UID 500

Obex::Obex(TQString &protocol) :
        mProtocol(protocol)
{
    kdDebug() << k_funcinfo << endl;
    KUser user;
    m_effectiveUid = user.uid();
}

Obex::~Obex()
{
    kdDebug() << k_funcinfo << endl;
}

bool Obex::parseURL(const KURL &url, TQString &address, TQString &name, TQString &path) const
{
    kdDebug() << k_funcinfo << endl;
    TQString url_path = url.path(+1);

    if (url_path.find(TQRegExp("/\\[([0-9A-F]{2}:){5}[0-9A-F]{2}\\]"), 0) != -1)
    {
        address = url_path.remove(0, 2);
        address = address.remove(17, url_path.length());
        url_path = url_path.remove(0, 18);
        kdDebug() << "Obex::parseURL address	: " << address << endl;
        kdDebug() << "Obex::parseURL url_path   : " << url_path << endl;
    }
    else
        return false;

    int i = url_path.find('/', 1);
    if (i > 0)
    {
        name = url_path.mid(1, i - 1);
    }
    else
    {
        name = url_path.mid(1);
    }
    path = url_path;
    kdDebug() << "Obex::parseURL path   : " << path << endl;
    kdDebug() << "Obex::parseURL name   : " << name << endl;

    return true;
}

void Obex::addAtom(UDSEntry &entry, UDSAtomTypes type, const TQString &s)
{
    kdDebug() << k_funcinfo << endl;
    UDSAtom atom;
    atom.m_uds = type;
    atom.m_str = s;
    entry.append(atom);
}

void Obex::addAtom(UDSEntry &entry, UDSAtomTypes type, const long l)
{
    kdDebug() << k_funcinfo << endl;
    UDSAtom atom;
    atom.m_uds = type;
    atom.m_long = l;
    entry.append(atom);
}

void Obex::createTopLevelEntry(UDSEntry &entry)
{
    kdDebug() << k_funcinfo << endl;

    entry.clear();
    addAtom(entry, UDS_NAME, ".");
    addAtom(entry, UDS_FILE_TYPE, S_IFDIR);
    addAtom(entry, UDS_ACCESS, 0555);
    addAtom(entry, UDS_MIME_TYPE, "inode/directory");
    addAtom(entry, UDS_ICON_NAME, "pda_blue");
    addAtom(entry, UDS_USER, "root");
    addAtom(entry, UDS_GROUP, "root");
}

void Obex::createDirEntry(UDSEntry &entry, const TQString dir)
{
    kdDebug() << k_funcinfo << endl;
    entry.clear();
    addAtom(entry, UDS_NAME, "/" + dir);
    addAtom(entry, UDS_FILE_TYPE, S_IFDIR);
    addAtom(entry, UDS_ACCESS, 0755);
    addAtom(entry, UDS_MIME_TYPE, "inode/directory");
    addAtom(entry, UDS_ICON_NAME, "pda_blue");
    addAtom(entry, UDS_USER, "root");
    addAtom(entry, UDS_GROUP, "root");
}

void Obex::slotStatResult(Job *job)
{
    kdDebug() << k_funcinfo << endl;
    if (job->error() == 0)
    {
        StatJob *stat_job = static_cast<StatJob *>(job);
        m_entryBuffer = stat_job->statResult();
    }

    tqApp->eventLoop()->exitLoop();
}

UDSEntry Obex::createUDSEntry(const TQMap<TQString, TQT_DBusData> &map)
{
    kdDebug() << k_funcinfo << endl;

    UDSEntry entry;
    long mode;
    uint isFile = 0;

    TQMap<TQString, TQT_DBusData>::const_iterator mit = map.begin();
    for (mit; mit != map.end(); ++mit)
    {

        UDSAtom atom;
        if (mit.key() == "Accessed")
        {
            TQString v = mit.data().toVariant().value.toString();
            atom.m_uds = UDS_ACCESS_TIME;
            atom.m_long = stringToTime_t(v);
            entry.append(atom);
        }
        if (mit.key() == "Created")
        {
            TQString v = mit.data().toVariant().value.toString();
            atom.m_uds = UDS_CREATION_TIME;
            atom.m_long = stringToTime_t(v);
            entry.append(atom);
        }
        if (mit.key() == "Group-perm")
        {
            TQString v = mit.data().toVariant().value.toString();
            if (v.contains('R', FALSE))
                mode |= S_IRGRP;
            if (v.contains('W', FALSE))
                mode |= S_IWGRP;
        }
        if (mit.key() == "Modified")
        {
            TQString v = mit.data().toVariant().value.toString();
            atom.m_uds = UDS_MODIFICATION_TIME;
            atom.m_long = stringToTime_t(v);
            entry.append(atom);
        }
        if (mit.key() == "Size")
        {
            TQ_UINT64 v = mit.data().toVariant().value.toUInt64();
            atom.m_uds = UDS_SIZE;
            atom.m_long = v;
            entry.append(atom);
        }
        if (mit.key() == "Name")
        {
            TQString v = mit.data().toVariant().value.toString();
            atom.m_uds = UDS_NAME;
            atom.m_str = v;
            entry.append(atom);
        }
        if (mit.key() == "Other-perm")
        {
            TQString v = mit.data().toVariant().value.toString();
            if (v.contains('R', FALSE))
                mode |= S_IROTH;
            if (v.contains('W', FALSE))
                mode |= S_IWOTH;
        }
        if (mit.key() == "Type")
        {
            TQString v = mit.data().toVariant().value.toString();
            if (v == "folder")
            {
                isFile = 1;
            }
            if (v == "file")
            {
                TQString v = mit.data().toVariant().value.toString();
                isFile = 2;
            }
        }
        if (mit.key() == "User-perm")
        {
            TQString v = mit.data().toVariant().value.toString();
            if (v.contains('R', FALSE))
                mode |= S_IRUSR;
            if (v.contains('W', FALSE))
                mode |= S_IWUSR;
        }
    }

    if (isFile == 1)
    {
        if (mode & S_IRUSR)
            mode |= S_IXUSR;
        if (mode & S_IRGRP)
            mode |= S_IXGRP;
        if (mode & S_IROTH)
            mode |= S_IXOTH;

        UDSAtom atom;
        atom.m_uds = UDS_ACCESS;
        atom.m_long = mode;
        entry.append(atom);

        // set the file type
        atom.m_uds = UDS_FILE_TYPE;
        atom.m_long = S_IFDIR;
        entry.append(atom);
    }
    else if (isFile == 2)
    {
        UDSAtom atom;
        atom.m_uds = UDS_ACCESS;
        atom.m_long = mode;
        entry.append(atom);

        // set the file type
        atom.m_uds = UDS_FILE_TYPE;
        atom.m_long = S_IFREG;
        entry.append(atom);
    }
    else
    {
        //FIXME fall back
    }
    return entry;
}

time_t Obex::stringToTime_t(TQString str)
{
    kdDebug() << k_funcinfo << endl;

    str.insert(13, ':');
    str.insert(11, ':');
    str.insert(6, '-');
    str.insert(4, '-');
    TQDateTime time = TQDateTime::fromString(str, TQt::ISODate);

    return time.toTime_t();
}

UDSEntry Obex::extractUrlInfos(const KURL &url)
{
    kdDebug() << k_funcinfo << endl;

    m_entryBuffer.clear();

    StatJob *job = stat(url, false);
    connect(job, TQ_SIGNAL(result(Job *)), this, TQ_SLOT(slotStatResult(Job *)));
    tqApp->eventLoop()->enterLoop();

    UDSEntry::iterator it = m_entryBuffer.begin();
    UDSEntry::iterator end = m_entryBuffer.end();

    UDSEntry infos;

    for (; it != end; ++it)
    {
        switch ((*it).m_uds)
        {
        case UDS_ACCESS:
        case UDS_USER:
        case UDS_GROUP:
        case UDS_CREATION_TIME:
        case UDS_MODIFICATION_TIME:
        case UDS_ACCESS_TIME:
            infos.append(*it);
            break;
        default:
            break;
        }
    }

    addAtom(infos, UDS_LOCAL_PATH, url.path());

    return infos;
}

#include "obex.moc"