merge the formfield patch from ooo-build
[ooovba.git] / ucb / source / ucp / gio / gio_seekable.cxx
blob6c3b68012179c554c0af054ae2a33a9e10b18469
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gio_seekable.cxx,v $
10 * $Revision: 1.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <rtl/memory.h>
32 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
33 #include <ucbhelper/cancelcommandexecution.hxx>
34 #include <string.h>
36 #include "gio_seekable.hxx"
37 #include "gio_content.hxx"
39 using namespace com::sun::star;
41 namespace gio
44 Seekable::Seekable(GSeekable *pStream) : mpStream(pStream)
46 if (!mpStream)
47 throw io::NotConnectedException();
50 Seekable::~Seekable( void )
54 void SAL_CALL Seekable::truncate( void )
55 throw( io::IOException, uno::RuntimeException )
57 if (!mpStream)
58 throw io::NotConnectedException();
60 if (!g_seekable_can_truncate(mpStream))
61 throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Truncate unsupported")),
62 static_cast< cppu::OWeakObject * >(this));
64 GError *pError=NULL;
65 if (!g_seekable_truncate(mpStream, 0, NULL, &pError))
66 convertToException(pError, static_cast< cppu::OWeakObject * >(this));
69 void SAL_CALL Seekable::seek( sal_Int64 location )
70 throw( lang::IllegalArgumentException, io::IOException, uno::RuntimeException )
72 if (!mpStream)
73 throw io::NotConnectedException();
75 if (!g_seekable_can_seek(mpStream))
76 throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Seek unsupported")),
77 static_cast< cppu::OWeakObject * >(this));
79 GError *pError=NULL;
80 if (!g_seekable_seek(mpStream, location, G_SEEK_SET, NULL, &pError))
81 convertToException(pError, static_cast< cppu::OWeakObject * >(this));
84 sal_Int64 SAL_CALL Seekable::getPosition() throw( io::IOException, uno::RuntimeException )
86 if (!mpStream)
87 throw io::NotConnectedException();
89 return g_seekable_tell(mpStream);
92 sal_Int64 SAL_CALL Seekable::getLength() throw( io::IOException, uno::RuntimeException )
94 if (!mpStream)
95 throw io::NotConnectedException();
97 bool bOk = false;
98 sal_uInt64 nSize = 0;
100 GFileInfo* pInfo = G_IS_FILE_INPUT_STREAM(mpStream)
101 ? g_file_input_stream_query_info(G_FILE_INPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL)
102 : g_file_output_stream_query_info(G_FILE_OUTPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL);
104 if (pInfo)
106 if (g_file_info_has_attribute(pInfo, G_FILE_ATTRIBUTE_STANDARD_SIZE))
108 nSize = g_file_info_get_size(pInfo);
109 bOk = true;
111 g_object_unref(pInfo);
114 if (!bOk)
116 GError *pError=NULL;
117 sal_Int64 nCurr = getPosition();
118 if (!g_seekable_seek(mpStream, 0, G_SEEK_END, NULL, &pError))
119 convertToException(pError, static_cast< cppu::OWeakObject * >(this));
120 nSize = getPosition();
121 seek(nCurr);
122 bOk = true;
125 if (!bOk)
126 throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Getting size unsupported")),
127 static_cast< cppu::OWeakObject * >(this));
129 return nSize;
132 uno::Any Seekable::queryInterface( const uno::Type &type ) throw( uno::RuntimeException )
134 uno::Any aRet = ::cppu::queryInterface ( type,
135 static_cast< XSeekable * >( this ) );
137 if (!aRet.hasValue() && g_seekable_can_truncate(mpStream))
138 aRet = ::cppu::queryInterface ( type, static_cast< XTruncate * >( this ) );
140 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );