/src/libreoffice/svl/source/misc/filenotation.cxx
Line | Count | Source |
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 | | |
25 | | namespace svt |
26 | | { |
27 | | |
28 | | OFileNotation::OFileNotation( const OUString& _rUrlOrPath ) |
29 | 0 | { |
30 | 0 | construct( _rUrlOrPath ); |
31 | 0 | } |
32 | | |
33 | | OFileNotation::OFileNotation( const OUString& _rUrlOrPath, NOTATION _eInputNotation ) |
34 | 0 | { |
35 | 0 | if ( _eInputNotation == N_URL ) |
36 | 0 | { |
37 | 0 | INetURLObject aParser( _rUrlOrPath ); |
38 | 0 | if ( aParser.GetProtocol() == INetProtocol::File ) |
39 | 0 | implInitWithURLNotation( _rUrlOrPath ); |
40 | 0 | else |
41 | 0 | m_sSystem = m_sFileURL = _rUrlOrPath; |
42 | 0 | } |
43 | 0 | else |
44 | 0 | implInitWithSystemNotation( _rUrlOrPath ); |
45 | 0 | } |
46 | | |
47 | | bool OFileNotation::implInitWithSystemNotation( const OUString& _rSystemPath ) |
48 | 0 | { |
49 | 0 | bool bSuccess = false; |
50 | |
|
51 | 0 | m_sSystem = _rSystemPath; |
52 | 0 | if ( ( osl_File_E_None != osl_getFileURLFromSystemPath( m_sSystem.pData, &m_sFileURL.pData ) ) |
53 | 0 | && ( m_sFileURL.isEmpty() ) |
54 | 0 | ) |
55 | 0 | { |
56 | 0 | if ( !_rSystemPath.isEmpty() ) |
57 | 0 | { |
58 | 0 | INetURLObject aSmartParser; |
59 | 0 | aSmartParser.SetSmartProtocol( INetProtocol::File ); |
60 | 0 | if ( aSmartParser.SetSmartURL( _rSystemPath ) ) |
61 | 0 | { |
62 | 0 | m_sFileURL = aSmartParser.GetMainURL( INetURLObject::DecodeMechanism::NONE ); |
63 | 0 | osl_getSystemPathFromFileURL( m_sFileURL.pData, &m_sSystem.pData ); |
64 | 0 | bSuccess = true; |
65 | 0 | } |
66 | 0 | } |
67 | 0 | } |
68 | 0 | else |
69 | 0 | bSuccess = true; |
70 | 0 | return bSuccess; |
71 | 0 | } |
72 | | |
73 | | void OFileNotation::implInitWithURLNotation( const OUString& _rURL ) |
74 | 0 | { |
75 | 0 | m_sFileURL = _rURL; |
76 | 0 | osl_getSystemPathFromFileURL( _rURL.pData, &m_sSystem.pData ); |
77 | 0 | } |
78 | | |
79 | | void OFileNotation::construct( const OUString& _rUrlOrPath ) |
80 | 0 | { |
81 | 0 | bool bSuccess = false; |
82 | | // URL notation? |
83 | 0 | INetURLObject aParser( _rUrlOrPath ); |
84 | 0 | switch ( aParser.GetProtocol() ) |
85 | 0 | { |
86 | 0 | case INetProtocol::File: |
87 | | // file URL |
88 | 0 | implInitWithURLNotation( _rUrlOrPath ); |
89 | 0 | bSuccess = true; |
90 | 0 | break; |
91 | | |
92 | 0 | case INetProtocol::NotValid: |
93 | | // assume system notation |
94 | 0 | bSuccess = implInitWithSystemNotation( _rUrlOrPath ); |
95 | 0 | break; |
96 | | |
97 | 0 | 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 | 0 | m_sSystem = m_sFileURL = _rUrlOrPath; |
101 | 0 | bSuccess = true; |
102 | 0 | break; |
103 | 0 | } |
104 | | |
105 | 0 | OSL_ENSURE( bSuccess, "OFileNotation::OFileNotation: could not detect the format!" ); |
106 | 0 | } |
107 | | |
108 | | const OUString & OFileNotation::get(NOTATION _eOutputNotation) const |
109 | 0 | { |
110 | 0 | switch (_eOutputNotation) |
111 | 0 | { |
112 | 0 | case N_SYSTEM: return m_sSystem; |
113 | 0 | case N_URL: return m_sFileURL; |
114 | 0 | } |
115 | | |
116 | 0 | OSL_FAIL("OFileNotation::get: invalid enum value!"); |
117 | 0 | return EMPTY_OUSTRING; |
118 | 0 | } |
119 | | |
120 | | } // namespace svt |
121 | | |
122 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |