LibreOffice Module connectivity (master) 1
MNSFolders.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "MNSFolders.hxx"
21
22#ifdef UNIX
23#include <string.h>
24#endif // End UNIX
25
26#ifdef _WIN32
27#if !defined WIN32_LEAN_AND_MEAN
28# define WIN32_LEAN_AND_MEAN
29#endif
30#include <windows.h>
31#include <stdlib.h>
32#include <shlobj.h>
33#include <objidl.h>
34#endif // End _WIN32
35#include <osl/security.hxx>
36#include <osl/file.hxx>
37#include <osl/thread.h>
38
39using namespace ::com::sun::star::mozilla;
40
41namespace
42{
43
44 OUString lcl_getUserDataDirectory()
45 {
46 ::osl::Security aSecurity;
47 OUString aConfigPath;
48
49 #if defined(_WIN32) || defined(MACOSX)
50 aSecurity.getConfigDir( aConfigPath );
51 #else
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
54 //not xdg-config dir
55 aSecurity.getHomeDir( aConfigPath );
56 #endif
57
58 return aConfigPath + "/";
59 }
60
61
62 const size_t NB_PRODUCTS = 3;
63 const size_t NB_CANDIDATES = 4;
64
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] =
70 {
71 #if defined(_WIN32)
72 { "Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
73 { "Mozilla/Firefox/", nullptr, nullptr, nullptr },
74 { "Thunderbird/", "Mozilla/Thunderbird/", nullptr, nullptr }
75 #elif defined(MACOSX)
76 { "../Mozilla/SeaMonkey/", nullptr, nullptr, nullptr },
77 { "Firefox/", nullptr, nullptr, nullptr },
78 { "../Thunderbird/", nullptr, nullptr, nullptr }
79 #else
80 { ".mozilla/seamonkey/", nullptr, nullptr, nullptr },
81 { ".mozilla/firefox/", nullptr, nullptr, nullptr },
82 { ".thunderbird/", ".mozilla-thunderbird/", ".mozilla/thunderbird/", ".icedove/" }
83 #endif
84 };
85
86 const char* ProductRootEnvironmentVariable[NB_PRODUCTS] =
87 {
88 "MOZILLA_PROFILE_ROOT",
89 "MOZILLA_FIREFOX_PROFILE_ROOT",
90 "MOZILLA_THUNDERBIRD_PROFILE_ROOT",
91 };
92
93
94 OUString const & lcl_guessProfileRoot( MozillaProductType _product )
95 {
96 size_t productIndex = static_cast<int>(_product) - 1;
97
98 static OUString s_productDirectories[NB_PRODUCTS];
99
100 if ( s_productDirectories[ productIndex ].isEmpty() )
101 {
102 OUString sProductPath;
103
104 // check whether we have an environment variable which help us
105 const char* pProfileByEnv = getenv( ProductRootEnvironmentVariable[ productIndex ] );
106 if ( pProfileByEnv )
107 {
108 sProductPath = OUString( pProfileByEnv, rtl_str_getLength( pProfileByEnv ), osl_getThreadTextEncoding() );
109 // assume that this is fine, no further checks
110 }
111 else
112 {
113 OUString sProductDirCandidate;
114 const char pProfileRegistry[] = "profiles.ini";
115
116 // check all possible candidates
117 for ( size_t i=0; i<NB_CANDIDATES; ++i )
118 {
119 if ( nullptr == DefaultProductDir[ productIndex ][ i ] )
120 break;
121
122 sProductDirCandidate = lcl_getUserDataDirectory() +
123 OUString::createFromAscii( DefaultProductDir[ productIndex ][ i ] );
124
125 // check existence
126 ::osl::DirectoryItem aRegistryItem;
127 ::osl::FileBase::RC result = ::osl::DirectoryItem::get( sProductDirCandidate + pProfileRegistry, aRegistryItem );
128 if ( result == ::osl::FileBase::E_None )
129 {
130 ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
131 result = aRegistryItem.getFileStatus( aStatus );
132 if ( result == ::osl::FileBase::E_None )
133 {
134 // the registry file exists
135 break;
136 }
137 }
138 }
139
140 ::osl::FileBase::getSystemPathFromFileURL( sProductDirCandidate, sProductPath );
141 }
142
143 s_productDirectories[ productIndex ] = sProductPath;
144 }
145
146 return s_productDirectories[ productIndex ];
147 }
148}
149
150
151OUString getRegistryDir(MozillaProductType product)
152{
153 if (product == MozillaProductType_Default)
154 return OUString();
155
156 return lcl_guessProfileRoot( product );
157}
158
159/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString getRegistryDir(MozillaProductType product)
Definition: MNSFolders.cxx:151
int i
Any result