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