bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / misc / lockfilecommon.cxx
blobe19bd44fd7102159209a0a230787c9f515bdc3ef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <stdio.h>
23 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
24 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
25 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
26 #include <com/sun/star/ucb/NameClashException.hpp>
27 #include <com/sun/star/io/WrongFormatException.hpp>
29 #include <osl/time.h>
30 #include <osl/security.hxx>
31 #include <osl/socket.hxx>
32 #include <osl/file.hxx>
33 #include <o3tl/enumrange.hxx>
35 #include <rtl/string.hxx>
36 #include <rtl/ustring.hxx>
37 #include <rtl/strbuf.hxx>
38 #include <rtl/ustrbuf.hxx>
40 #include <comphelper/processfactory.hxx>
42 #include <tools/urlobj.hxx>
43 #include <unotools/bootstrap.hxx>
45 #include <ucbhelper/content.hxx>
47 #include <unotools/useroptions.hxx>
49 #include <salhelper/linkhelper.hxx>
51 #include <svl/lockfilecommon.hxx>
53 using namespace ::com::sun::star;
55 namespace svt {
58 LockFileCommon::LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix )
60 INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) );
62 OUString aShareURLString = aDocURL.GetPartBeforeLastName();
63 aShareURLString += aPrefix;
64 aShareURLString += aDocURL.GetName();
65 aShareURLString += "%23"; // '#'
66 m_aURL = INetURLObject( aShareURLString ).GetMainURL( INetURLObject::NO_DECODE );
70 LockFileCommon::~LockFileCommon()
75 INetURLObject LockFileCommon::ResolveLinks( const INetURLObject& aDocURL )
77 if ( aDocURL.HasError() )
78 throw lang::IllegalArgumentException();
80 OUString aURLToCheck = aDocURL.GetMainURL(INetURLObject::NO_DECODE);
82 // there is currently no UCB functionality to resolve the symbolic links;
83 // since the lock files are used only for local file systems the osl
84 // functionality is used directly
85 salhelper::LinkResolver aResolver(osl_FileStatus_Mask_FileName);
86 osl::FileBase::RC eStatus = aResolver.fetchFileStatus(aURLToCheck);
87 if (eStatus == osl::FileBase::E_None)
88 aURLToCheck = aResolver.m_aStatus.getFileURL();
89 else if (eStatus == osl::FileBase::E_MULTIHOP)
91 // do not allow too deep links
92 throw io::IOException();
95 return INetURLObject( aURLToCheck );
99 void LockFileCommon::ParseList( const uno::Sequence< sal_Int8 >& aBuffer, std::vector< LockFileEntry > & aResult )
101 sal_Int32 nCurPos = 0;
102 while ( nCurPos < aBuffer.getLength() )
104 aResult.push_back( ParseEntry( aBuffer, nCurPos ) );
109 LockFileEntry LockFileCommon::ParseEntry( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
111 LockFileEntry aResult;
113 for ( LockFileComponent nInd : o3tl::enumrange<LockFileComponent>() )
115 aResult[nInd] = ParseName( aBuffer, io_nCurPos );
116 if ( io_nCurPos >= aBuffer.getLength()
117 || ( nInd < LockFileComponent::LAST && aBuffer[io_nCurPos++] != ',' )
118 || ( nInd == LockFileComponent::LAST && aBuffer[io_nCurPos++] != ';' ) )
119 throw io::WrongFormatException();
122 return aResult;
126 OUString LockFileCommon::ParseName( const uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& io_nCurPos )
128 OStringBuffer aResult;
129 bool bHaveName = false;
130 bool bEscape = false;
132 while( !bHaveName )
134 if ( io_nCurPos >= aBuffer.getLength() )
135 throw io::WrongFormatException();
137 if ( bEscape )
139 if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' || aBuffer[io_nCurPos] == '\\' )
140 aResult.append( (sal_Char)aBuffer[io_nCurPos] );
141 else
142 throw io::WrongFormatException();
144 bEscape = false;
145 io_nCurPos++;
147 else if ( aBuffer[io_nCurPos] == ',' || aBuffer[io_nCurPos] == ';' )
148 bHaveName = true;
149 else
151 if ( aBuffer[io_nCurPos] == '\\' )
152 bEscape = true;
153 else
154 aResult.append( (sal_Char)aBuffer[io_nCurPos] );
156 io_nCurPos++;
160 return OStringToOUString( aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
164 OUString LockFileCommon::EscapeCharacters( const OUString& aSource )
166 OUStringBuffer aBuffer;
167 const sal_Unicode* pStr = aSource.getStr();
168 for ( sal_Int32 nInd = 0; nInd < aSource.getLength() && pStr[nInd] != 0; nInd++ )
170 if ( pStr[nInd] == '\\' || pStr[nInd] == ';' || pStr[nInd] == ',' )
171 aBuffer.append( '\\' );
172 aBuffer.append( pStr[nInd] );
175 return aBuffer.makeStringAndClear();
179 OUString LockFileCommon::GetOOOUserName()
181 SvtUserOptions aUserOpt;
182 OUString aName = aUserOpt.GetFirstName();
183 if ( !aName.isEmpty() )
184 aName += " ";
185 aName += aUserOpt.GetLastName();
187 return aName;
191 OUString LockFileCommon::GetCurrentLocalTime()
193 OUString aTime;
195 TimeValue aSysTime;
196 if ( osl_getSystemTime( &aSysTime ) )
198 TimeValue aLocTime;
199 if ( osl_getLocalTimeFromSystemTime( &aSysTime, &aLocTime ) )
201 oslDateTime aDateTime;
202 if ( osl_getDateTimeFromTimeValue( &aLocTime, &aDateTime ) )
204 char pDateTime[20];
205 sprintf( pDateTime, "%02d.%02d.%4d %02d:%02d", aDateTime.Day, aDateTime.Month, aDateTime.Year, aDateTime.Hours, aDateTime.Minutes );
206 aTime = OUString::createFromAscii( pDateTime );
211 return aTime;
215 LockFileEntry LockFileCommon::GenerateOwnEntry()
217 LockFileEntry aResult;
219 aResult[LockFileComponent::OOOUSERNAME] = GetOOOUserName();
221 ::osl::Security aSecurity;
222 aSecurity.getUserName( aResult[LockFileComponent::SYSUSERNAME] );
224 aResult[LockFileComponent::LOCALHOST] = ::osl::SocketAddr::getLocalHostname();
226 aResult[LockFileComponent::EDITTIME] = GetCurrentLocalTime();
228 ::utl::Bootstrap::locateUserInstallation( aResult[LockFileComponent::USERURL] );
231 return aResult;
234 } // namespace svt
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */