bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / mozab / bootstrap / MNSFolders.cxx
blob07376ab262f70508f5941bc1a99d7875ff2636e8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "MNSFolders.hxx"
22 #ifdef UNIX
23 #include <sys/types.h>
24 #include <strings.h>
25 #include <string.h>
26 #endif // End UNIX
28 #ifdef _WIN32
29 #if !defined WIN32_LEAN_AND_MEAN
30 # define WIN32_LEAN_AND_MEAN
31 #endif
32 #include <windows.h>
33 #include <stdlib.h>
34 #include <shlobj.h>
35 #include <objidl.h>
36 #endif // End WNT
37 #include <osl/security.hxx>
38 #include <osl/file.hxx>
39 #include <osl/thread.h>
41 using namespace ::com::sun::star::mozilla;
43 namespace
46 OUString lcl_getUserDataDirectory()
48 ::osl::Security aSecurity;
49 OUString aConfigPath;
51 #if defined(_WIN32) || defined(MACOSX)
52 aSecurity.getConfigDir( aConfigPath );
53 #else
54 //This is to find the dir under which .mozilla/.thunderbird is created.
55 //mozilla doesn't honour XDG_CONFIG_HOME, so raw home dir required here
56 //not xdg-config dir
57 aSecurity.getHomeDir( aConfigPath );
58 #endif
60 return aConfigPath + "/";
64 const size_t NB_PRODUCTS = 3;
65 const size_t NB_CANDIDATES = 4;
67 // The order (index) of entries in DefaultProductDir and
68 // ProductRootEnvironmentVariable *must* match the constants
69 // (minus 1) in offapi/com/sun/star/mozilla/MozillaProductType.idl
70 // DO NOT CHANGE THE ORDER; ADD ONLY TO THE END
71 static const char* DefaultProductDir[NB_PRODUCTS][NB_CANDIDATES] =
73 #if defined(_WIN32)
74 { "Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
75 { "Mozilla/Firefox/", nullptr, nullptr, nullptr },
76 { "Thunderbird/", "Mozilla/Thunderbird/", nullptr, nullptr }
77 #elif defined(MACOSX)
78 { "../Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
79 { "Firefox/", nullptr, nullptr, nullptr },
80 { "../Thunderbird/", nullptr, nullptr, nullptr }
81 #else
82 { ".mozilla/seamonkey/", nullptr, nullptr, nullptr },
83 { ".mozilla/firefox/", nullptr, nullptr, nullptr },
84 { ".thunderbird/", ".mozilla-thunderbird/", ".mozilla/thunderbird/", ".icedove/" }
85 #endif
88 static const char* ProductRootEnvironmentVariable[NB_PRODUCTS] =
90 "MOZILLA_PROFILE_ROOT",
91 "MOZILLA_FIREFOX_PROFILE_ROOT",
92 "MOZILLA_THUNDERBIRD_PROFILE_ROOT",
96 OUString const & lcl_guessProfileRoot( MozillaProductType _product )
98 size_t productIndex = static_cast<int>(_product) - 1;
100 static OUString s_productDirectories[NB_PRODUCTS];
102 if ( s_productDirectories[ productIndex ].isEmpty() )
104 OUString sProductPath;
106 // check whether we have an environment variable which help us
107 const char* pProfileByEnv = getenv( ProductRootEnvironmentVariable[ productIndex ] );
108 if ( pProfileByEnv )
110 sProductPath = OUString( pProfileByEnv, rtl_str_getLength( pProfileByEnv ), osl_getThreadTextEncoding() );
111 // assume that this is fine, no further checks
113 else
115 OUString sProductDirCandidate;
116 const char pProfileRegistry[] = "profiles.ini";
118 // check all possible candidates
119 for ( size_t i=0; i<NB_CANDIDATES; ++i )
121 if ( nullptr == DefaultProductDir[ productIndex ][ i ] )
122 break;
124 sProductDirCandidate = lcl_getUserDataDirectory() +
125 OUString::createFromAscii( DefaultProductDir[ productIndex ][ i ] );
127 // check existence
128 ::osl::DirectoryItem aRegistryItem;
129 ::osl::FileBase::RC result = ::osl::DirectoryItem::get( sProductDirCandidate + pProfileRegistry, aRegistryItem );
130 if ( result == ::osl::FileBase::E_None )
132 ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
133 result = aRegistryItem.getFileStatus( aStatus );
134 if ( result == ::osl::FileBase::E_None )
136 // the registry file exists
137 break;
142 ::osl::FileBase::getSystemPathFromFileURL( sProductDirCandidate, sProductPath );
145 s_productDirectories[ productIndex ] = sProductPath;
148 return s_productDirectories[ productIndex ];
153 OUString getRegistryDir(MozillaProductType product)
155 if (product == MozillaProductType_Default)
156 return OUString();
158 return lcl_guessProfileRoot( product );
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */