1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_extensions.hxx"
24 #include <MNSFolders.hxx>
25 
26 #ifdef UNIX
27 #include <sys/types.h>
28 #include <strings.h>
29 #include <string.h>
30 #endif // End UNIX
31 
32 #ifdef WNT
33 #include "pre_include_windows.h"
34 #include <windows.h>
35 #include <stdlib.h>
36 #include <shlobj.h>
37 #include <objidl.h>
38 #include "post_include_windows.h"
39 #endif // End WNT
40 #include <osl/security.hxx>
41 #include <osl/file.hxx>
42 #include <osl/thread.h>
43 
44 using namespace ::com::sun::star::mozilla;
45 
46 namespace
47 {
48     // -------------------------------------------------------------------
lcl_getUserDataDirectory()49     static ::rtl::OUString lcl_getUserDataDirectory()
50     {
51         ::osl::Security   aSecurity;
52         ::rtl::OUString   aConfigPath;
53 
54         aSecurity.getConfigDir( aConfigPath );
55         return aConfigPath + ::rtl::OUString::createFromAscii( "/" );
56     }
57 
58     // -------------------------------------------------------------------
59     static const char* DefaultProductDir[3][3] =
60     {
61     #if defined(XP_WIN)
62         { "Mozilla/SeaMonkey/", NULL, NULL },
63         { "Mozilla/Firefox/", NULL, NULL },
64         { "Thunderbird/", "Mozilla/Thunderbird/", NULL }
65     #elif(MACOSX)
66         { "../Mozilla/SeaMonkey/", NULL, NULL },
67         { "Firefox/", NULL, NULL },
68         { "../Thunderbird/", NULL, NULL }
69     #else
70         { ".mozilla/seamonkey/", NULL, NULL },
71         { ".mozilla/firefox/", NULL, NULL },
72         { ".thunderbird/", ".mozilla-thunderbird/", ".mozilla/thunderbird/" }
73     #endif
74     };
75 
76     static const char* ProductRootEnvironmentVariable[3] =
77     {
78         "MOZILLA_PROFILE_ROOT",
79         "MOZILLA_FIREFOX_PROFILE_ROOT",
80         "MOZILLA_THUNDERBIRD_PROFILE_ROOT",
81     };
82 
83     // -------------------------------------------------------------------
lcl_guessProfileRoot(MozillaProductType _product)84     static ::rtl::OUString lcl_guessProfileRoot( MozillaProductType _product )
85     {
86         size_t productIndex = _product - 1;
87 
88         static ::rtl::OUString s_productDirectories[3];
89 
90         if ( !s_productDirectories[ productIndex ].getLength() )
91         {
92             ::rtl::OUString sProductPath;
93 
94             // check whether we have an anevironment variable which helps us
95             const char* pProfileByEnv = getenv( ProductRootEnvironmentVariable[ productIndex ] );
96             if ( pProfileByEnv )
97             {
98                 sProductPath = ::rtl::OUString( pProfileByEnv, rtl_str_getLength( pProfileByEnv ), osl_getThreadTextEncoding() );
99                 // assume that this is fine, no further checks
100             }
101             else
102             {
103                 ::rtl::OUString sProductDirCandidate;
104                 const char* pProfileRegistry = "profiles.ini";
105 
106                 // check all possible candidates
107                 for ( size_t i=0; i<3; ++i )
108                 {
109                     if ( NULL == DefaultProductDir[ productIndex ][ i ] )
110                         break;
111 
112                     sProductDirCandidate = lcl_getUserDataDirectory() +
113                         ::rtl::OUString::createFromAscii( DefaultProductDir[ productIndex ][ i ] );
114 
115                     // check existence
116                     ::osl::DirectoryItem aRegistryItem;
117                     ::osl::FileBase::RC result = ::osl::DirectoryItem::get( sProductDirCandidate + ::rtl::OUString::createFromAscii( pProfileRegistry ), aRegistryItem );
118                     if ( result == ::osl::FileBase::E_None  )
119                     {
120                         ::osl::FileStatus aStatus( FileStatusMask_Validate );
121                         result = aRegistryItem.getFileStatus( aStatus );
122                         if ( result == ::osl::FileBase::E_None  )
123                         {
124                             // the registry file exists
125                             break;
126                         }
127                     }
128                 }
129 
130                 ::osl::FileBase::getSystemPathFromFileURL( sProductDirCandidate, sProductPath );
131             }
132 
133             s_productDirectories[ productIndex ] = sProductPath;
134         }
135 
136         return s_productDirectories[ productIndex ];
137     }
138 }
139 
140 // -----------------------------------------------------------------------
getRegistryDir(MozillaProductType product)141 ::rtl::OUString getRegistryDir(MozillaProductType product)
142 {
143     if (product == MozillaProductType_Default)
144         return ::rtl::OUString();
145 
146     return lcl_guessProfileRoot( product );
147 }
148