1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_svl.hxx" 30 #include <svl/filenotation.hxx> 31 #include <osl/file.h> 32 #include <osl/diagnose.h> 33 #include <tools/urlobj.hxx> 34 35 //......................................................................... 36 namespace svt 37 { 38 //......................................................................... 39 40 //===================================================================== 41 //= OFileNotation 42 //===================================================================== 43 //--------------------------------------------------------------------- 44 OFileNotation::OFileNotation( const ::rtl::OUString& _rUrlOrPath ) 45 { 46 construct( _rUrlOrPath ); 47 } 48 49 //--------------------------------------------------------------------- 50 OFileNotation::OFileNotation( const ::rtl::OUString& _rUrlOrPath, NOTATION _eInputNotation ) 51 { 52 if ( _eInputNotation == N_URL ) 53 { 54 INetURLObject aParser( _rUrlOrPath ); 55 if ( aParser.GetProtocol() == INET_PROT_FILE ) 56 implInitWithURLNotation( _rUrlOrPath ); 57 else 58 m_sSystem = m_sFileURL = _rUrlOrPath; 59 } 60 else 61 implInitWithSystemNotation( _rUrlOrPath ); 62 } 63 64 //--------------------------------------------------------------------- 65 bool OFileNotation::implInitWithSystemNotation( const ::rtl::OUString& _rSystemPath ) 66 { 67 bool bSuccess = false; 68 69 m_sSystem = _rSystemPath; 70 if ( ( osl_File_E_None != osl_getFileURLFromSystemPath( m_sSystem.pData, &m_sFileURL.pData ) ) 71 && ( 0 == m_sFileURL.getLength() ) 72 ) 73 { 74 if ( _rSystemPath.getLength() ) 75 { 76 INetURLObject aSmartParser; 77 aSmartParser.SetSmartProtocol( INET_PROT_FILE ); 78 if ( aSmartParser.SetSmartURL( _rSystemPath ) ) 79 { 80 m_sFileURL = aSmartParser.GetMainURL( INetURLObject::NO_DECODE ); 81 osl_getSystemPathFromFileURL( m_sFileURL.pData, &m_sSystem.pData ); 82 bSuccess = true; 83 } 84 } 85 } 86 else 87 bSuccess = true; 88 return bSuccess; 89 } 90 91 //--------------------------------------------------------------------- 92 bool OFileNotation::implInitWithURLNotation( const ::rtl::OUString& _rURL ) 93 { 94 m_sFileURL = _rURL; 95 osl_getSystemPathFromFileURL( _rURL.pData, &m_sSystem.pData ); 96 return true; 97 } 98 99 //--------------------------------------------------------------------- 100 void OFileNotation::construct( const ::rtl::OUString& _rUrlOrPath ) 101 { 102 bool bSuccess = false; 103 // URL notation? 104 INetURLObject aParser( _rUrlOrPath ); 105 switch ( aParser.GetProtocol() ) 106 { 107 case INET_PROT_FILE: 108 // file URL 109 bSuccess = implInitWithURLNotation( _rUrlOrPath ); 110 break; 111 112 case INET_PROT_NOT_VALID: 113 // assume system notation 114 bSuccess = implInitWithSystemNotation( _rUrlOrPath ); 115 break; 116 117 default: 118 // it's a known scheme, but no file-URL -> assume that bothe the URL representation and the 119 // system representation are the URL itself 120 m_sSystem = m_sFileURL = _rUrlOrPath; 121 bSuccess = true; 122 break; 123 } 124 125 OSL_ENSURE( bSuccess, "OFileNotation::OFileNotation: could not detect the format!" ); 126 } 127 128 //--------------------------------------------------------------------- 129 ::rtl::OUString OFileNotation::get(NOTATION _eOutputNotation) 130 { 131 switch (_eOutputNotation) 132 { 133 case N_SYSTEM: return m_sSystem; 134 case N_URL: return m_sFileURL; 135 } 136 137 OSL_ENSURE(sal_False, "OFileNotation::get: inavlid enum value!"); 138 return ::rtl::OUString(); 139 } 140 141 //......................................................................... 142 } // namespace svt 143 //......................................................................... 144 145