1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
27 #if !defined WIN32_LEAN_AND_MEAN
28 # define WIN32_LEAN_AND_MEAN
35 #include <osl/security.hxx>
36 #include <osl/file.hxx>
37 #include <osl/thread.h>
39 using namespace ::com::sun::star::mozilla
;
44 OUString
lcl_getUserDataDirectory()
46 ::osl::Security aSecurity
;
49 #if defined(_WIN32) || defined(MACOSX)
50 aSecurity
.getConfigDir( aConfigPath
);
52 //This is to find the dir under which .mozilla/.thunderbird is created.
53 //mozilla doesn't honour XDG_CONFIG_HOME, so raw home dir required here
55 aSecurity
.getHomeDir( aConfigPath
);
58 return aConfigPath
+ "/";
62 const size_t NB_PRODUCTS
= 3;
63 const size_t NB_CANDIDATES
= 4;
65 // The order (index) of entries in DefaultProductDir and
66 // ProductRootEnvironmentVariable *must* match the constants
67 // (minus 1) in offapi/com/sun/star/mozilla/MozillaProductType.idl
68 // DO NOT CHANGE THE ORDER; ADD ONLY TO THE END
69 const char* DefaultProductDir
[NB_PRODUCTS
][NB_CANDIDATES
] =
72 { "Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
73 { "Mozilla/Firefox/", nullptr, nullptr, nullptr },
74 { "Thunderbird/", "Mozilla/Thunderbird/", nullptr, nullptr }
76 { "../Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
77 { "Firefox/", nullptr, nullptr, nullptr },
78 { "../Thunderbird/", nullptr, nullptr, nullptr }
80 { ".mozilla/seamonkey/", nullptr, nullptr, nullptr },
81 { ".mozilla/firefox/", nullptr, nullptr, nullptr },
82 { ".thunderbird/", ".mozilla-thunderbird/", ".mozilla/thunderbird/", ".icedove/" }
86 const char* ProductRootEnvironmentVariable
[NB_PRODUCTS
] =
88 "MOZILLA_PROFILE_ROOT",
89 "MOZILLA_FIREFOX_PROFILE_ROOT",
90 "MOZILLA_THUNDERBIRD_PROFILE_ROOT",
94 OUString
const & lcl_guessProfileRoot( MozillaProductType _product
)
96 size_t productIndex
= static_cast<int>(_product
) - 1;
98 static OUString s_productDirectories
[NB_PRODUCTS
];
100 if ( s_productDirectories
[ productIndex
].isEmpty() )
102 OUString sProductPath
;
104 // check whether we have an environment variable which help us
105 const char* pProfileByEnv
= getenv( ProductRootEnvironmentVariable
[ productIndex
] );
108 sProductPath
= OUString( pProfileByEnv
, rtl_str_getLength( pProfileByEnv
), osl_getThreadTextEncoding() );
109 // assume that this is fine, no further checks
113 OUString sProductDirCandidate
;
114 const char pProfileRegistry
[] = "profiles.ini";
116 // check all possible candidates
117 for ( size_t i
=0; i
<NB_CANDIDATES
; ++i
)
119 if ( nullptr == DefaultProductDir
[ productIndex
][ i
] )
122 sProductDirCandidate
= lcl_getUserDataDirectory() +
123 OUString::createFromAscii( DefaultProductDir
[ productIndex
][ i
] );
126 ::osl::DirectoryItem aRegistryItem
;
127 ::osl::FileBase::RC result
= ::osl::DirectoryItem::get( sProductDirCandidate
+ pProfileRegistry
, aRegistryItem
);
128 if ( result
== ::osl::FileBase::E_None
)
130 ::osl::FileStatus
aStatus( osl_FileStatus_Mask_Validate
);
131 result
= aRegistryItem
.getFileStatus( aStatus
);
132 if ( result
== ::osl::FileBase::E_None
)
134 // the registry file exists
140 ::osl::FileBase::getSystemPathFromFileURL( sProductDirCandidate
, sProductPath
);
143 s_productDirectories
[ productIndex
] = sProductPath
;
146 return s_productDirectories
[ productIndex
];
151 OUString
getRegistryDir(MozillaProductType product
)
153 if (product
== MozillaProductType_Default
)
156 return lcl_guessProfileRoot( product
);
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */