| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <sal/config.h> |
| 21 |
|
| 22 |
#include <com/sun/star/io/IOException.hpp> |
| 23 |
#include <com/sun/star/io/NotConnectedException.hpp> |
| 24 |
#include <cppuhelper/queryinterface.hxx> |
| 25 |
|
| 26 |
#include "gio_seekable.hxx" |
| 27 |
#include "gio_content.hxx" |
| 28 |
|
| 29 |
namespace gio |
| 30 |
{ |
| 31 |
|
| 32 |
Seekable::Seekable(GSeekable *pStream) : mpStream(pStream) |
| 33 |
{ |
| 34 |
if (!mpStream) |
| 35 |
throw css::io::NotConnectedException(); |
| 36 |
} |
| 37 |
|
| 38 |
Seekable::~Seekable() |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
void SAL_CALL Seekable::truncate() |
| 43 |
{ |
| 44 |
if (!mpStream) |
| 45 |
throw css::io::NotConnectedException(); |
| 46 |
|
| 47 |
if (!g_seekable_can_truncate(mpStream)) |
| 48 |
throw css::io::IOException(u"Truncate unsupported"_ustr, |
| 49 |
getXWeak()); |
| 50 |
|
| 51 |
GError *pError=nullptr; |
| 52 |
if (!g_seekable_truncate(mpStream, 0, nullptr, &pError)) |
| 53 |
convertToIOException(pError, getXWeak()); |
| 54 |
} |
| 55 |
|
| 56 |
void SAL_CALL Seekable::seek( sal_Int64 location ) |
| 57 |
{ |
| 58 |
if (!mpStream) |
| 59 |
throw css::io::NotConnectedException(); |
| 60 |
|
| 61 |
if (!g_seekable_can_seek(mpStream)) |
| 62 |
throw css::io::IOException(u"Seek unsupported"_ustr, |
| 63 |
getXWeak()); |
| 64 |
|
| 65 |
GError *pError=nullptr; |
| 66 |
if (!g_seekable_seek(mpStream, location, G_SEEK_SET, nullptr, &pError)) |
| 67 |
convertToIOException(pError, getXWeak()); |
| 68 |
} |
| 69 |
|
| 70 |
sal_Int64 SAL_CALL Seekable::getPosition() |
| 71 |
{ |
| 72 |
if (!mpStream) |
| 73 |
throw css::io::NotConnectedException(); |
| 74 |
|
| 75 |
return g_seekable_tell(mpStream); |
| 76 |
} |
| 77 |
|
| 78 |
sal_Int64 SAL_CALL Seekable::getLength() |
| 79 |
{ |
| 80 |
if (!mpStream) |
| 81 |
throw css::io::NotConnectedException(); |
| 82 |
|
| 83 |
bool bOk = false; |
| 84 |
sal_uInt64 nSize = 0; |
| 85 |
|
| 86 |
GFileInfo* pInfo = G_IS_FILE_INPUT_STREAM(mpStream) |
| 87 |
? g_file_input_stream_query_info(G_FILE_INPUT_STREAM(mpStream), G_FILE_ATTRIBUTE_STANDARD_SIZE, nullptr, nullptr) |
| 88 |
: g_file_output_stream_query_info(G_FILE_OUTPUT_STREAM(mpStream), G_FILE_ATTRIBUTE_STANDARD_SIZE, nullptr, nullptr); |
| 89 |
|
| 90 |
if (pInfo) |
| 91 |
{ |
| 92 |
if (g_file_info_has_attribute(pInfo, G_FILE_ATTRIBUTE_STANDARD_SIZE)) |
| 93 |
{ |
| 94 |
nSize = g_file_info_get_size(pInfo); |
| 95 |
bOk = true; |
| 96 |
} |
| 97 |
g_object_unref(pInfo); |
| 98 |
} |
| 99 |
|
| 100 |
if (!bOk) |
| 101 |
{ |
| 102 |
GError *pError=nullptr; |
| 103 |
sal_Int64 nCurr = getPosition(); |
| 104 |
if (!g_seekable_seek(mpStream, 0, G_SEEK_END, nullptr, &pError)) |
| 105 |
convertToIOException(pError, getXWeak()); |
| 106 |
nSize = getPosition(); |
| 107 |
seek(nCurr); |
| 108 |
} |
| 109 |
|
| 110 |
return nSize; |
| 111 |
} |
| 112 |
|
| 113 |
css::uno::Any Seekable::queryInterface( const css::uno::Type &type ) |
| 114 |
{ |
| 115 |
css::uno::Any aRet = ::cppu::queryInterface ( type, |
| 116 |
static_cast< XSeekable * >( this ) ); |
| 117 |
|
| 118 |
if (!aRet.hasValue() && g_seekable_can_truncate(mpStream)) |
| 119 |
aRet = ::cppu::queryInterface ( type, static_cast< XTruncate * >( this ) ); |
| 120 |
|
| 121 |
return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type ); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |