Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / cmis / cmis_url.cxx
blobae2bce9dd59a08033bcee97f47fc9d6455d27988
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/.
8 */
10 #include <sal/config.h>
12 #include <rtl/uri.hxx>
13 #include <tools/urlobj.hxx>
15 #include "cmis_url.hxx"
17 using namespace std;
19 namespace cmis
21 URL::URL( OUString const & urlStr )
23 INetURLObject aUrl( urlStr );
25 // Decode the authority to get the binding URL and repository id
26 OUString sDecodedHost = aUrl.GetHost( INetURLObject::DecodeMechanism::WithCharset );
27 INetURLObject aHostUrl( sDecodedHost );
28 m_sBindingUrl = aHostUrl.GetURLNoMark( );
29 m_sRepositoryId = aHostUrl.GetMark( );
31 m_sUser = aUrl.GetUser( INetURLObject::DecodeMechanism::WithCharset );
32 m_sPass = aUrl.GetPass( INetURLObject::DecodeMechanism::WithCharset );
34 // Store the path to the object
35 m_sPath = aUrl.GetURLPath( INetURLObject::DecodeMechanism::WithCharset );
36 m_sId = aUrl.GetMark( INetURLObject::DecodeMechanism::WithCharset );
38 if ( m_sPath == "/" && m_sBindingUrl.indexOf( "google" ) != -1 )
39 m_sId = "root";
43 void URL::setObjectPath( const OUString& sPath )
45 m_sPath = sPath;
48 void URL::setObjectId( const OUString& sId )
50 m_sId = sId;
53 void URL::setUsername( const OUString& sUser )
55 m_sUser = sUser;
58 OUString URL::asString( )
60 OUString sUrl;
61 // Related tdf#96174, can no longer save on Google Drive
62 // the user field may contain characters that need to be escaped according to
63 // RFC3896 userinfo URI field
64 // see <https://tools.ietf.org/html/rfc3986#section-3.2.1>
65 OUString sEncodedUser = ( m_sUser.isEmpty() ?
66 OUString() :
67 rtl::Uri::encode( m_sUser, rtl_UriCharClassUserinfo,
68 rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8) );
69 OUString sEncodedBinding = rtl::Uri::encode(
70 m_sBindingUrl + "#" + m_sRepositoryId,
71 rtl_UriCharClassRelSegment,
72 rtl_UriEncodeKeepEscapes,
73 RTL_TEXTENCODING_UTF8 );
74 sUrl = "vnd.libreoffice.cmis://" +
75 ( sEncodedUser.isEmpty() ? OUString( ) : (sEncodedUser + "@") ) +
76 sEncodedBinding;
78 if ( !m_sPath.isEmpty( ) )
80 sal_Int32 nPos = -1;
81 OUStringBuffer sEncodedPath;
84 sal_Int32 nStartPos = nPos + 1;
85 nPos = m_sPath.indexOf( '/', nStartPos );
86 sal_Int32 nLen = nPos - nStartPos;
87 if ( nPos == -1 )
88 nLen = m_sPath.getLength( ) - nStartPos;
89 OUString sSegment = m_sPath.copy( nStartPos, nLen );
91 if ( !sSegment.isEmpty( ) )
93 sEncodedPath.append("/").append(rtl::Uri::encode( sSegment,
94 rtl_UriCharClassRelSegment,
95 rtl_UriEncodeKeepEscapes,
96 RTL_TEXTENCODING_UTF8 ));
99 while ( nPos != -1 );
100 sUrl += sEncodedPath.makeStringAndClear();
101 } else if ( !m_sId.isEmpty( ) )
103 sUrl += "#" + rtl::Uri::encode( m_sId,
104 rtl_UriCharClassRelSegment,
105 rtl_UriEncodeKeepEscapes,
106 RTL_TEXTENCODING_UTF8 );
109 return sUrl;
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */