xref: /aoo41x/main/ucb/source/ucp/gio/gio_seekable.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <rtl/memory.h>
29 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
30 #include <ucbhelper/cancelcommandexecution.hxx>
31 #include <string.h>
32 
33 #include "gio_seekable.hxx"
34 #include "gio_content.hxx"
35 
36 using namespace com::sun::star;
37 
38 namespace gio
39 {
40 
41 Seekable::Seekable(GSeekable *pStream) : mpStream(pStream)
42 {
43     if (!mpStream)
44         throw io::NotConnectedException();
45 }
46 
47 Seekable::~Seekable( void )
48 {
49 }
50 
51 void SAL_CALL Seekable::truncate( void )
52     throw( io::IOException, uno::RuntimeException )
53 {
54     if (!mpStream)
55         throw io::NotConnectedException();
56 
57     if (!g_seekable_can_truncate(mpStream))
58         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Truncate unsupported")),
59             static_cast< cppu::OWeakObject * >(this));
60 
61     GError *pError=NULL;
62     if (!g_seekable_truncate(mpStream, 0, NULL, &pError))
63         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
64 }
65 
66 void SAL_CALL Seekable::seek( sal_Int64 location )
67     throw( lang::IllegalArgumentException, io::IOException, uno::RuntimeException )
68 {
69     if (!mpStream)
70         throw io::NotConnectedException();
71 
72     if (!g_seekable_can_seek(mpStream))
73         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Seek unsupported")),
74             static_cast< cppu::OWeakObject * >(this));
75 
76     GError *pError=NULL;
77     if (!g_seekable_seek(mpStream, location, G_SEEK_SET, NULL, &pError))
78         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
79 }
80 
81 sal_Int64 SAL_CALL Seekable::getPosition() throw( io::IOException, uno::RuntimeException )
82 {
83     if (!mpStream)
84         throw io::NotConnectedException();
85 
86     return g_seekable_tell(mpStream);
87 }
88 
89 sal_Int64 SAL_CALL Seekable::getLength() throw( io::IOException, uno::RuntimeException )
90 {
91     if (!mpStream)
92         throw io::NotConnectedException();
93 
94     bool bOk = false;
95     sal_uInt64 nSize = 0;
96 
97     GFileInfo* pInfo = G_IS_FILE_INPUT_STREAM(mpStream)
98         ? g_file_input_stream_query_info(G_FILE_INPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL)
99         : g_file_output_stream_query_info(G_FILE_OUTPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL);
100 
101     if (pInfo)
102     {
103         if (g_file_info_has_attribute(pInfo, G_FILE_ATTRIBUTE_STANDARD_SIZE))
104         {
105             nSize = g_file_info_get_size(pInfo);
106             bOk = true;
107         }
108         g_object_unref(pInfo);
109     }
110 
111     if (!bOk)
112     {
113         GError *pError=NULL;
114         sal_Int64 nCurr = getPosition();
115         if (!g_seekable_seek(mpStream, 0, G_SEEK_END, NULL, &pError))
116             convertToException(pError, static_cast< cppu::OWeakObject * >(this));
117         nSize = getPosition();
118         seek(nCurr);
119         bOk = true;
120     }
121 
122     if (!bOk)
123         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Getting size unsupported")),
124             static_cast< cppu::OWeakObject * >(this));
125 
126     return nSize;
127 }
128 
129 uno::Any Seekable::queryInterface( const uno::Type &type ) throw( uno::RuntimeException )
130 {
131     uno::Any aRet = ::cppu::queryInterface ( type,
132         static_cast< XSeekable * >( this ) );
133 
134     if (!aRet.hasValue() && g_seekable_can_truncate(mpStream))
135         aRet = ::cppu::queryInterface ( type, static_cast< XTruncate * >( this ) );
136 
137     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
138 }
139 
140 }
141