summaryrefslogtreecommitdiff
path: root/unotools/source/ucbhelper/ucbhelper.cxx
blob: 1b6902eceb82cb8ff65aca93a4abc88b196a53d8 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "sal/config.h"

#include <cassert>
#include <vector>

#include "com/sun/star/lang/XMultiServiceFactory.hpp"
#include "com/sun/star/sdbc/XResultSet.hpp"
#include "com/sun/star/task/XInteractionHandler.hpp"
#include "com/sun/star/ucb/CommandAbortedException.hpp"
#include "com/sun/star/ucb/ContentInfo.hpp"
#include "com/sun/star/ucb/ContentInfoAttribute.hpp"
#include "com/sun/star/ucb/IOErrorCode.hpp"
#include "com/sun/star/ucb/InteractiveIOException.hpp"
#include "com/sun/star/ucb/NameClashException.hpp"
#include "com/sun/star/ucb/UniversalContentBroker.hpp"
#include "com/sun/star/ucb/XCommandEnvironment.hpp"
#include "com/sun/star/ucb/XContentAccess.hpp"
#include "com/sun/star/ucb/XContentIdentifier.hpp"
#include "com/sun/star/ucb/XProgressHandler.hpp"
#include "com/sun/star/ucb/XUniversalContentBroker.hpp"
#include "com/sun/star/uno/Any.hxx"
#include "com/sun/star/uno/Exception.hpp"
#include "com/sun/star/uno/Reference.hxx"
#include "com/sun/star/uno/RuntimeException.hpp"
#include "com/sun/star/uno/Sequence.hxx"
#include "com/sun/star/util/DateTime.hpp"
#include "comphelper/processfactory.hxx"
#include "cppuhelper/exc_hlp.hxx"
#include "osl/file.hxx"
#include "rtl/string.h"
#include "rtl/ustring.h"
#include "rtl/ustring.hxx"
#include "sal/log.hxx"
#include "sal/types.h"
#include "tools/datetime.hxx"
#include "tools/urlobj.hxx"
#include "ucbhelper/commandenvironment.hxx"
#include "ucbhelper/content.hxx"
#include "unotools/localfilehelper.hxx"
#include "unotools/ucbhelper.hxx"

namespace {

namespace css = com::sun::star;

rtl::OUString canonic(rtl::OUString const & url) {
    INetURLObject o(url);
    SAL_WARN_IF(o.HasError(), "unotools", "Invalid URL \"" << url << '"');
    return o.GetMainURL(INetURLObject::NO_DECODE);
}

ucbhelper::Content content(rtl::OUString const & url) {
    return ucbhelper::Content(
        canonic(url),
        css::uno::Reference<css::ucb::XCommandEnvironment>(),
        comphelper::getProcessComponentContext());
}

ucbhelper::Content content(INetURLObject const & url) {
    return ucbhelper::Content(
        url.GetMainURL(INetURLObject::NO_DECODE),
        css::uno::Reference<css::ucb::XCommandEnvironment>(),
        comphelper::getProcessComponentContext());
}

std::vector<rtl::OUString> getContents(rtl::OUString const & url) {
    try {
        std::vector<rtl::OUString> cs;
        ucbhelper::Content c(content(url));
        css::uno::Sequence<rtl::OUString> args(1);
        args[0] = rtl::OUString("Title");
        css::uno::Reference<css::sdbc::XResultSet> res(
            c.createCursor(args, ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS),
            css::uno::UNO_SET_THROW);
        css::uno::Reference<com::sun::star::ucb::XContentAccess> acc(
            res, css::uno::UNO_QUERY_THROW);
        while (res->next()) {
            cs.push_back(acc->queryContentIdentifierString());
        }
        return cs;
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "getContents(" << url << ") " << e.getValueType().getTypeName()
                << " \"" << e.get<css::uno::Exception>().Message << '"');
        return std::vector<rtl::OUString>();
    }
}

rtl::OUString getCasePreservingUrl(INetURLObject url) {
    return
        content(url).executeCommand(
            rtl::OUString("getCasePreservingURL"),
            css::uno::Any()).
        get<rtl::OUString>();
}

DateTime convert(css::util::DateTime const & dt) {
    return DateTime(
        Date(dt.Day, dt.Month, dt.Year),
        Time(dt.Hours, dt.Minutes, dt.Seconds, dt.HundredthSeconds));
}

}

bool utl::UCBContentHelper::IsDocument(rtl::OUString const & url) {
    try {
        return content(url).isDocument();
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::IsDocument(" << url << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
        return false;
    }
}

css::uno::Any utl::UCBContentHelper::GetProperty(
    rtl::OUString const & url, rtl::OUString const & property)
{
    try {
        return content(url).getPropertyValue(property);
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::GetProperty(" << url << ", " << property << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
        return css::uno::Any();
    }
}

bool utl::UCBContentHelper::IsFolder(rtl::OUString const & url) {
    try {
        return content(url).isFolder();
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::IsFolder(" << url << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
        return false;
    }
}

bool utl::UCBContentHelper::GetTitle(
    rtl::OUString const & url, rtl::OUString * title)
{
    assert(title != 0);
    try {
        return content(url).getPropertyValue(rtl::OUString("Title")) >>= *title;
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::GetTitle(" << url << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
        return false;
    }
}

bool utl::UCBContentHelper::Kill(rtl::OUString const & url) {
    try {
        content(url).executeCommand(
            rtl::OUString("delete"),
            css::uno::makeAny(true));
        return true;
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::Kill(" << url << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
        return false;
    }
}

bool utl::UCBContentHelper::MakeFolder(
    ucbhelper::Content & parent, rtl::OUString const & title,
    ucbhelper::Content & result, bool exclusive)
{
    bool exists = false;
    try {
        css::uno::Sequence<css::ucb::ContentInfo> info(
            parent.queryCreatableContentsInfo());
        for (sal_Int32 i = 0; i < info.getLength(); ++i) {
            // Simply look for the first KIND_FOLDER:
            if ((info[i].Attributes
                 & css::ucb::ContentInfoAttribute::KIND_FOLDER)
                != 0)
            {
                // Make sure the only required bootstrap property is "Title":
                if ( info[i].Properties.getLength() != 1 || info[i].Properties[0].Name != "Title" )
                {
                    continue;
                }
                css::uno::Sequence<rtl::OUString> keys(1);
                keys[0] = rtl::OUString("Title");
                css::uno::Sequence<css::uno::Any> values(1);
                values[0] <<= title;
                if (parent.insertNewContent(info[i].Type, keys, values, result))
                {
                    return true;
                }
            }
        }
    } catch (css::ucb::InteractiveIOException const & e) {
        if (e.Code == css::ucb::IOErrorCode_ALREADY_EXISTING) {
            exists = true;
        } else {
            SAL_INFO(
                "unotools",
                "UCBContentHelper::MakeFolder(" << title
                    << ") InteractiveIOException \"" << e.Message
                    << "\", code " << +e.Code);
        }
    } catch (css::ucb::NameClashException const &) {
        exists = true;
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::MakeFolder(" << title << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
    }
    if (exists && !exclusive) {
        INetURLObject o(parent.getURL());
        o.Append(title);
        result = content(o);
        return true;
    } else {
        return false;
    }
}

sal_Int64 utl::UCBContentHelper::GetSize(rtl::OUString const & url) {
    try {
        sal_Int64 n = 0;
        bool ok = (content(url).getPropertyValue(rtl::OUString("Size")) >>= n);
        SAL_INFO_IF(
            !ok, "unotools",
            "UCBContentHelper::GetSize(" << url
                << "): Size cannot be determined");
        return n;
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::GetSize(" << url << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
        return 0;
    }
}

bool utl::UCBContentHelper::IsYounger(
    rtl::OUString const & younger, rtl::OUString const & older)
{
    try {
        return
            convert(
                content(younger).getPropertyValue(
                    rtl::OUString("DateModified")).
                get<css::util::DateTime>())
            > convert(
                content(older).getPropertyValue(
                    rtl::OUString("DateModified")).
                get<css::util::DateTime>());
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::IsYounger(" << younger << ", " << older << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
        return false;
    }
}

bool utl::UCBContentHelper::Exists(rtl::OUString const & url) {
    rtl::OUString pathname;
    if (utl::LocalFileHelper::ConvertURLToPhysicalName(url, pathname)) {
        // Try to create a directory entry for the given URL:
        rtl::OUString url2;
        if (osl::FileBase::getFileURLFromSystemPath(pathname, url2)
            == osl::FileBase::E_None)
        {
            // #106526 osl_getDirectoryItem is an existence check, no further
            // osl_getFileStatus call necessary:
            osl::DirectoryItem item;
            return osl::DirectoryItem::get(url2, item) == osl::FileBase::E_None;
        } else {
            return false;
        }
    } else {
        // Divide URL into folder and name part:
        INetURLObject o(url);
        rtl::OUString name(
            o.getName(
                INetURLObject::LAST_SEGMENT, true,
                INetURLObject::DECODE_WITH_CHARSET));
        o.removeSegment();
        o.removeFinalSlash();
        std::vector<rtl::OUString> cs(
            getContents(o.GetMainURL(INetURLObject::NO_DECODE)));
        for (std::vector<rtl::OUString>::iterator i(cs.begin()); i != cs.end();
             ++i)
        {
            if (INetURLObject(*i).getName(
                    INetURLObject::LAST_SEGMENT, true,
                    INetURLObject::DECODE_WITH_CHARSET).
                equalsIgnoreAsciiCase(name))
            {
                return true;
            }
        }
        return false;
    }
}

bool utl::UCBContentHelper::IsSubPath(
    rtl::OUString const & parent, rtl::OUString const & child)
{
    // The comparison is done in the following way:
    // - First, compare case sensitively
    // - If names are different, try a fallback comparing case insensitively
    // - If the last comparison succeeded, get case preserving normalized names
    //   for the files and compare them
    // (The second step is required because retrieving the normalized names
    // might be very expensive in some cases.)
    INetURLObject candidate(child);
    INetURLObject folder(parent);
    if (candidate.GetProtocol() != folder.GetProtocol()) {
        return false;
    }
    INetURLObject candidateLower(child.toAsciiLowerCase());
    INetURLObject folderLower(parent.toAsciiLowerCase());
    try {
        INetURLObject tmp;
        do {
            if (candidate == folder
                || (candidate.GetProtocol() == INET_PROT_FILE
                    && candidateLower == folderLower
                    && (getCasePreservingUrl(candidate)
                        == getCasePreservingUrl(folder))))
            {
                return true;
            }
            tmp = candidate;
        } while (candidate.removeSegment() && candidateLower.removeSegment()
                 && candidate != tmp);
            // INetURLObject::removeSegment sometimes returns true without
            // modifying the URL, e.g., in case of "file:///"
    } catch (css::uno::RuntimeException const &) {
        throw;
    } catch (css::ucb::CommandAbortedException const &) {
        assert(false); // this cannot happen
        throw;
    } catch (css::uno::Exception const &) {
        css::uno::Any e(cppu::getCaughtException());
        SAL_INFO(
            "unotools",
            "UCBContentHelper::IsSubPath(" << parent << ", " << child << ") "
                << e.getValueType().getTypeName() << " \""
                << e.get<css::uno::Exception>().Message << '"');
    }
    return false;
}

bool utl::UCBContentHelper::EqualURLs(
    rtl::OUString const & url1, rtl::OUString const & url2)
{
    if (url1.isEmpty() || url2.isEmpty()) {
        return false;
    }
    css::uno::Reference< css::ucb::XUniversalContentBroker > ucb(
        css::ucb::UniversalContentBroker::create(
            comphelper::getProcessComponentContext()));
    return
        ucb->compareContentIds(
            ucb->createContentIdentifier(canonic(url1)),
            ucb->createContentIdentifier(canonic(url2)))
        == 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */