Line data Source code
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 <sys/types.h>
24 : #include <strings.h>
25 : #include <string.h>
26 : #endif // End UNIX
27 :
28 : #ifdef WNT
29 : #include "pre_include_windows.h"
30 : #include <windows.h>
31 : #include <stdlib.h>
32 : #include <shlobj.h>
33 : #include <objidl.h>
34 : #include "post_include_windows.h"
35 : #endif // End WNT
36 : #include <osl/security.hxx>
37 : #include <osl/file.hxx>
38 : #include <osl/thread.h>
39 :
40 : using namespace ::com::sun::star::mozilla;
41 :
42 : namespace
43 : {
44 :
45 3 : static OUString lcl_getUserDataDirectory()
46 : {
47 3 : ::osl::Security aSecurity;
48 6 : OUString aConfigPath;
49 :
50 : #if defined(XP_WIN) || defined(MACOSX)
51 : aSecurity.getConfigDir( aConfigPath );
52 : #else
53 : //This is to find the dir under which .mozilla/.thunderbird is created.
54 : //mozilla doesn't honour XDG_CONFIG_HOME, so raw home dir required here
55 : //not xdg-config dir
56 3 : aSecurity.getHomeDir( aConfigPath );
57 : #endif
58 :
59 6 : return aConfigPath + "/";
60 : }
61 :
62 :
63 : const size_t NB_PRODUCTS = 3;
64 : const size_t NB_CANDIDATES = 4;
65 :
66 : // The order (index) of entries in DefaultProductDir and
67 : // ProductRootEnvironmentVariable *must* match the constants
68 : // (minus 1) in offapi/com/sun/star/mozilla/MozillaProductType.idl
69 : // DO NOT CHANGE THE ORDER; ADD ONLY TO THE END
70 : static const char* DefaultProductDir[NB_PRODUCTS][NB_CANDIDATES] =
71 : {
72 : #if defined(XP_WIN)
73 : { "Mozilla/SeaMonkey/", NULL, NULL, NULL },
74 : { "Mozilla/Firefox/", NULL, NULL, NULL },
75 : { "Thunderbird/", "Mozilla/Thunderbird/", NULL, NULL }
76 : #elif defined(MACOSX)
77 : { "../Mozilla/SeaMonkey/", NULL, NULL, NULL },
78 : { "Firefox/", NULL, NULL, NULL },
79 : { "../Thunderbird/", NULL, NULL, NULL }
80 : #else
81 : { ".mozilla/seamonkey/", NULL, NULL, NULL },
82 : { ".mozilla/firefox/", NULL, NULL, NULL },
83 : { ".thunderbird/", ".mozilla-thunderbird/", ".mozilla/thunderbird/", ".icedove/" }
84 : #endif
85 : };
86 :
87 : static const char* ProductRootEnvironmentVariable[NB_PRODUCTS] =
88 : {
89 : "MOZILLA_PROFILE_ROOT",
90 : "MOZILLA_FIREFOX_PROFILE_ROOT",
91 : "MOZILLA_THUNDERBIRD_PROFILE_ROOT",
92 : };
93 :
94 :
95 3 : static OUString lcl_guessProfileRoot( MozillaProductType _product )
96 : {
97 3 : size_t productIndex = _product - 1;
98 :
99 4 : static OUString s_productDirectories[NB_PRODUCTS];
100 :
101 3 : if ( s_productDirectories[ productIndex ].isEmpty() )
102 : {
103 3 : OUString sProductPath;
104 :
105 : // check whether we have an anevironment variable which helps us
106 3 : const char* pProfileByEnv = getenv( ProductRootEnvironmentVariable[ productIndex ] );
107 3 : if ( pProfileByEnv )
108 : {
109 0 : sProductPath = OUString( pProfileByEnv, rtl_str_getLength( pProfileByEnv ), osl_getThreadTextEncoding() );
110 : // assume that this is fine, no further checks
111 : }
112 : else
113 : {
114 3 : OUString sProductDirCandidate;
115 3 : const char* pProfileRegistry = "profiles.ini";
116 :
117 : // check all possible candidates
118 8 : for ( size_t i=0; i<NB_CANDIDATES; ++i )
119 : {
120 4 : if ( NULL == DefaultProductDir[ productIndex ][ i ] )
121 4 : break;
122 :
123 6 : sProductDirCandidate = lcl_getUserDataDirectory() +
124 9 : OUString::createFromAscii( DefaultProductDir[ productIndex ][ i ] );
125 :
126 : // check existence
127 3 : ::osl::DirectoryItem aRegistryItem;
128 3 : ::osl::FileBase::RC result = ::osl::DirectoryItem::get( sProductDirCandidate + OUString::createFromAscii( pProfileRegistry ), aRegistryItem );
129 3 : if ( result == ::osl::FileBase::E_None )
130 : {
131 2 : ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
132 2 : result = aRegistryItem.getFileStatus( aStatus );
133 2 : if ( result == ::osl::FileBase::E_None )
134 : {
135 : // the registry file exists
136 2 : break;
137 0 : }
138 : }
139 1 : }
140 :
141 3 : ::osl::FileBase::getSystemPathFromFileURL( sProductDirCandidate, sProductPath );
142 : }
143 :
144 3 : s_productDirectories[ productIndex ] = sProductPath;
145 : }
146 :
147 3 : return s_productDirectories[ productIndex ];
148 : }
149 : }
150 :
151 :
152 3 : OUString getRegistryDir(MozillaProductType product)
153 : {
154 3 : if (product == MozillaProductType_Default)
155 0 : return OUString();
156 :
157 3 : return lcl_guessProfileRoot( product );
158 : }
159 :
160 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|