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"
23 #include <sys/types.h>
29 #if !defined WIN32_LEAN_AND_MEAN
30 # define WIN32_LEAN_AND_MEAN
37 #include <osl/security.hxx>
38 #include <osl/file.hxx>
39 #include <osl/thread.h>
41 using namespace ::com::sun::star::mozilla
;
46 OUString
lcl_getUserDataDirectory()
48 ::osl::Security aSecurity
;
51 #if defined(_WIN32) || defined(MACOSX)
52 aSecurity
.getConfigDir( aConfigPath
);
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
57 aSecurity
.getHomeDir( aConfigPath
);
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
] =
74 { "Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
75 { "Mozilla/Firefox/", nullptr, nullptr, nullptr },
76 { "Thunderbird/", "Mozilla/Thunderbird/", nullptr, nullptr }
78 { "../Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
79 { "Firefox/", nullptr, nullptr, nullptr },
80 { "../Thunderbird/", nullptr, nullptr, nullptr }
82 { ".mozilla/seamonkey/", nullptr, nullptr, nullptr },
83 { ".mozilla/firefox/", nullptr, nullptr, nullptr },
84 { ".thunderbird/", ".mozilla-thunderbird/", ".mozilla/thunderbird/", ".icedove/" }
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
] );
110 sProductPath
= OUString( pProfileByEnv
, rtl_str_getLength( pProfileByEnv
), osl_getThreadTextEncoding() );
111 // assume that this is fine, no further checks
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
] )
124 sProductDirCandidate
= lcl_getUserDataDirectory() +
125 OUString::createFromAscii( DefaultProductDir
[ productIndex
][ i
] );
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
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
)
158 return lcl_guessProfileRoot( product
);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */