xref: /aoo42x/main/svl/source/misc/filenotation.cxx (revision fb0b81f5)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 #include <svl/filenotation.hxx>
27 #include <osl/file.h>
28 #include <osl/diagnose.h>
29 #include <tools/urlobj.hxx>
30 
31 //.........................................................................
32 namespace svt
33 {
34 //.........................................................................
35 
36 	//=====================================================================
37 	//= OFileNotation
38 	//=====================================================================
39 	//---------------------------------------------------------------------
OFileNotation(const::rtl::OUString & _rUrlOrPath)40 	OFileNotation::OFileNotation( const ::rtl::OUString& _rUrlOrPath )
41 	{
42 		construct( _rUrlOrPath );
43 	}
44 
45 	//---------------------------------------------------------------------
OFileNotation(const::rtl::OUString & _rUrlOrPath,NOTATION _eInputNotation)46 	OFileNotation::OFileNotation( const ::rtl::OUString& _rUrlOrPath, NOTATION _eInputNotation )
47 	{
48         if ( _eInputNotation == N_URL )
49         {
50 		    INetURLObject aParser( _rUrlOrPath );
51 		    if ( aParser.GetProtocol() == INET_PROT_FILE )
52                 implInitWithURLNotation( _rUrlOrPath );
53             else
54 				m_sSystem = m_sFileURL = _rUrlOrPath;
55         }
56         else
57             implInitWithSystemNotation( _rUrlOrPath );
58 	}
59 
60 	//---------------------------------------------------------------------
implInitWithSystemNotation(const::rtl::OUString & _rSystemPath)61 	bool OFileNotation::implInitWithSystemNotation( const ::rtl::OUString& _rSystemPath )
62     {
63         bool bSuccess = false;
64 
65         m_sSystem = _rSystemPath;
66 		if  (  ( osl_File_E_None != osl_getFileURLFromSystemPath( m_sSystem.pData, &m_sFileURL.pData ) )
67             && ( 0 == m_sFileURL.getLength() )
68             )
69 		{
70 			if ( _rSystemPath.getLength() )
71 			{
72 				INetURLObject aSmartParser;
73 				aSmartParser.SetSmartProtocol( INET_PROT_FILE );
74 				if ( aSmartParser.SetSmartURL( _rSystemPath ) )
75 				{
76 					m_sFileURL = aSmartParser.GetMainURL( INetURLObject::NO_DECODE );
77 					osl_getSystemPathFromFileURL( m_sFileURL.pData, &m_sSystem.pData );
78 					bSuccess = true;
79 				}
80 			}
81 		}
82 		else
83 			bSuccess = true;
84         return bSuccess;
85     }
86 
87 	//---------------------------------------------------------------------
implInitWithURLNotation(const::rtl::OUString & _rURL)88 	bool OFileNotation::implInitWithURLNotation( const ::rtl::OUString& _rURL )
89     {
90 		m_sFileURL = _rURL;
91 		osl_getSystemPathFromFileURL( _rURL.pData, &m_sSystem.pData );
92         return true;
93     }
94 
95 	//---------------------------------------------------------------------
construct(const::rtl::OUString & _rUrlOrPath)96 	void OFileNotation::construct( const ::rtl::OUString& _rUrlOrPath )
97 	{
98 		bool bSuccess = false;
99 		// URL notation?
100 		INetURLObject aParser( _rUrlOrPath );
101 		switch ( aParser.GetProtocol() )
102 		{
103 			case INET_PROT_FILE:
104 				// file URL
105 				bSuccess = implInitWithURLNotation( _rUrlOrPath );
106 				break;
107 
108 			case INET_PROT_NOT_VALID:
109 				// assume system notation
110                 bSuccess = implInitWithSystemNotation( _rUrlOrPath );
111 				break;
112 
113 			default:
114 				// it's a known scheme, but no file-URL -> assume that both the URL representation and the
115 				// system representation are the URL itself
116 				m_sSystem = m_sFileURL = _rUrlOrPath;
117 				bSuccess = true;
118 				break;
119 		}
120 
121 		OSL_ENSURE( bSuccess, "OFileNotation::OFileNotation: could not detect the format!" );
122 	}
123 
124 	//---------------------------------------------------------------------
get(NOTATION _eOutputNotation)125 	::rtl::OUString OFileNotation::get(NOTATION _eOutputNotation)
126 	{
127 		switch (_eOutputNotation)
128 		{
129 			case N_SYSTEM: return m_sSystem;
130 			case N_URL: return m_sFileURL;
131 		}
132 
133 		OSL_ENSURE(sal_False, "OFileNotation::get: inavlid enum value!");
134 		return ::rtl::OUString();
135 	}
136 
137 //.........................................................................
138 }	// namespace svt
139 //.........................................................................
140 
141