LibreOffice Module svl (master) 1
filenotation.cxx
Go to the documentation of this file.
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 <svl/filenotation.hxx>
21#include <osl/file.h>
22#include <osl/diagnose.h>
23#include <tools/urlobj.hxx>
24
25namespace svt
26{
27
28 OFileNotation::OFileNotation( const OUString& _rUrlOrPath )
29 {
30 construct( _rUrlOrPath );
31 }
32
33 OFileNotation::OFileNotation( const OUString& _rUrlOrPath, NOTATION _eInputNotation )
34 {
35 if ( _eInputNotation == N_URL )
36 {
37 INetURLObject aParser( _rUrlOrPath );
38 if ( aParser.GetProtocol() == INetProtocol::File )
39 implInitWithURLNotation( _rUrlOrPath );
40 else
41 m_sSystem = m_sFileURL = _rUrlOrPath;
42 }
43 else
44 implInitWithSystemNotation( _rUrlOrPath );
45 }
46
47 bool OFileNotation::implInitWithSystemNotation( const OUString& _rSystemPath )
48 {
49 bool bSuccess = false;
50
51 m_sSystem = _rSystemPath;
52 if ( ( osl_File_E_None != osl_getFileURLFromSystemPath( m_sSystem.pData, &m_sFileURL.pData ) )
53 && ( m_sFileURL.isEmpty() )
54 )
55 {
56 if ( !_rSystemPath.isEmpty() )
57 {
58 INetURLObject aSmartParser;
59 aSmartParser.SetSmartProtocol( INetProtocol::File );
60 if ( aSmartParser.SetSmartURL( _rSystemPath ) )
61 {
63 osl_getSystemPathFromFileURL( m_sFileURL.pData, &m_sSystem.pData );
64 bSuccess = true;
65 }
66 }
67 }
68 else
69 bSuccess = true;
70 return bSuccess;
71 }
72
73 void OFileNotation::implInitWithURLNotation( const OUString& _rURL )
74 {
75 m_sFileURL = _rURL;
76 osl_getSystemPathFromFileURL( _rURL.pData, &m_sSystem.pData );
77 }
78
79 void OFileNotation::construct( const OUString& _rUrlOrPath )
80 {
81 bool bSuccess = false;
82 // URL notation?
83 INetURLObject aParser( _rUrlOrPath );
84 switch ( aParser.GetProtocol() )
85 {
86 case INetProtocol::File:
87 // file URL
88 implInitWithURLNotation( _rUrlOrPath );
89 bSuccess = true;
90 break;
91
92 case INetProtocol::NotValid:
93 // assume system notation
94 bSuccess = implInitWithSystemNotation( _rUrlOrPath );
95 break;
96
97 default:
98 // it's a known scheme, but no file-URL -> assume that both the URL representation and the
99 // system representation are the URL itself
100 m_sSystem = m_sFileURL = _rUrlOrPath;
101 bSuccess = true;
102 break;
103 }
104
105 OSL_ENSURE( bSuccess, "OFileNotation::OFileNotation: could not detect the format!" );
106 }
107
108 OUString OFileNotation::get(NOTATION _eOutputNotation) const
109 {
110 switch (_eOutputNotation)
111 {
112 case N_SYSTEM: return m_sSystem;
113 case N_URL: return m_sFileURL;
114 }
115
116 OSL_FAIL("OFileNotation::get: invalid enum value!");
117 return OUString();
118 }
119
120} // namespace svt
121
122/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetSmartProtocol(INetProtocol eTheSmartScheme)
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool SetSmartURL(std::u16string_view rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
INetProtocol GetProtocol() const
SVL_DLLPRIVATE void implInitWithURLNotation(const OUString &_rURL)
OFileNotation(const OUString &_rUrlOrPath)
OUString get(NOTATION _eOutputNotation) const
SVL_DLLPRIVATE bool implInitWithSystemNotation(const OUString &_rSystemPath)
SVL_DLLPRIVATE void construct(const OUString &_rUrlOrPath)