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 "MNSProfileDiscover.hxx"
25 
26 // Registry Keys
27 
28 static ::rtl::OUString szProfileSubtreeString=::rtl::OUString::createFromAscii("Profiles");
29 static ::rtl::OUString szCurrentProfileString= ::rtl::OUString::createFromAscii("CurrentProfile");
30 static ::rtl::OUString szDirectoryString =::rtl::OUString::createFromAscii("directory");
31 
32 #ifndef MAXPATHLEN
33 #define MAXPATHLEN 1024
34 #endif
35 #include <MNSFolders.hxx>
36 #include <MNSINIParser.hxx>
37 
38 namespace connectivity
39 {
40     namespace mozab
41     {
ProfileStruct(MozillaProductType aProduct,::rtl::OUString aProfileName,const::rtl::OUString & aProfilePath)42         ProfileStruct::ProfileStruct(MozillaProductType aProduct,::rtl::OUString aProfileName,
43             const ::rtl::OUString& aProfilePath)
44         {
45             product=aProduct;
46             profileName = aProfileName;
47             profilePath = aProfilePath;
48         }
getProfilePath()49         ::rtl::OUString ProfileStruct::getProfilePath()
50         {
51             return profilePath;
52         }
53 
~ProfileAccess()54         ProfileAccess::~ProfileAccess()
55         {
56         }
ProfileAccess()57         ProfileAccess::ProfileAccess()
58         {
59             LoadProductsInfo();
60         }
61 
LoadProductsInfo()62         sal_Int32 ProfileAccess::LoadProductsInfo()
63         {
64             //load SeaMonkey 2 profiles to m_ProductProfileList
65             sal_Int32 count = LoadXPToolkitProfiles(MozillaProductType_Mozilla);
66 
67             //load thunderbird profiles to m_ProductProfileList
68             count += LoadXPToolkitProfiles(MozillaProductType_Thunderbird);
69 
70             //load firefox profiles to m_ProductProfileList
71             //firefox profile does not containt address book, but maybe others need them
72             count += LoadXPToolkitProfiles(MozillaProductType_Firefox);
73             return count;
74         }
75         //Thunderbird and firefox profiles are saved in profiles.ini
LoadXPToolkitProfiles(MozillaProductType product)76         sal_Int32 ProfileAccess::LoadXPToolkitProfiles(MozillaProductType product)
77         {
78             sal_Int32 index=product;
79             ProductStruct &m_Product = m_ProductProfileList[index];
80 
81             ::rtl::OUString regDir = getRegistryDir(product);
82             ::rtl::OUString profilesIni( regDir );
83             profilesIni += ::rtl::OUString::createFromAscii( "profiles.ini" );
84             IniParser parser( profilesIni );
85             IniSectionMap &mAllSection = *(parser.getAllSection());
86 
87             IniSectionMap::iterator iBegin = mAllSection.begin();
88             IniSectionMap::iterator iEnd = mAllSection.end();
89             for(;iBegin != iEnd;iBegin++)
90             {
91                 ini_Section *aSection = &(*iBegin).second;
92                 ::rtl::OUString profileName;
93                 ::rtl::OUString profilePath;
94                 ::rtl::OUString sIsRelative;
95                 ::rtl::OUString sIsDefault;
96 
97                 for(NameValueList::iterator itor=aSection->lList.begin();
98                     itor != aSection->lList.end();
99                     itor++)
100                 {
101                         struct ini_NameValue * aValue = &(*itor);
102                         if (aValue->sName.equals(::rtl::OUString::createFromAscii("Name")))
103                         {
104                             profileName = aValue->sValue;
105                         }
106                         else if (aValue->sName.equals(::rtl::OUString::createFromAscii("IsRelative")))
107                         {
108                             sIsRelative = aValue->sValue;
109                         }
110                         else if (aValue->sName.equals(::rtl::OUString::createFromAscii("Path")))
111                         {
112                             profilePath = aValue->sValue;
113                         }
114                         else if (aValue->sName.equals(::rtl::OUString::createFromAscii("Default")))
115                         {
116                             sIsDefault = aValue->sValue;
117                         }
118                 }
119                 if (profileName.getLength() != 0 || profilePath.getLength() != 0)
120                 {
121                     sal_Int32 isRelative = 0;
122                     if (sIsRelative.getLength() != 0)
123                     {
124                         isRelative = sIsRelative.toInt32();
125                     }
126                     if (isRelative)
127                     {
128                         // Make it absolute
129                         profilePath = regDir + profilePath;
130                     }
131 
132                     ProfileStruct*  profileItem     = new ProfileStruct(product,profileName,
133                             profilePath);
134                     m_Product.mProfileList[profileName] = profileItem;
135 
136                     sal_Int32 isDefault = 0;
137                     if (sIsDefault.getLength() != 0)
138                     {
139                         isDefault = sIsDefault.toInt32();
140                     }
141                     if (isDefault)
142                         m_Product.mCurrentProfileName = profileName;
143 
144                 }
145 
146             }
147             return static_cast< ::sal_Int32 >(m_Product.mProfileList.size());
148         }
149 
getProfilePath(::com::sun::star::mozilla::MozillaProductType product,const::rtl::OUString & profileName)150         ::rtl::OUString ProfileAccess::getProfilePath( ::com::sun::star::mozilla::MozillaProductType product, const ::rtl::OUString& profileName ) throw (::com::sun::star::uno::RuntimeException)
151         {
152             sal_Int32 index=product;
153             ProductStruct &m_Product = m_ProductProfileList[index];
154             if (!m_Product.mProfileList.size() || m_Product.mProfileList.find(profileName) == m_Product.mProfileList.end())
155             {
156                 //Profile not found
157                 return ::rtl::OUString();
158             }
159             else
160                 return m_Product.mProfileList[profileName]->getProfilePath();
161         }
162 
getProfileCount(::com::sun::star::mozilla::MozillaProductType product)163         ::sal_Int32 ProfileAccess::getProfileCount( ::com::sun::star::mozilla::MozillaProductType product) throw (::com::sun::star::uno::RuntimeException)
164         {
165             sal_Int32 index=product;
166             ProductStruct &m_Product = m_ProductProfileList[index];
167             return static_cast< ::sal_Int32 >(m_Product.mProfileList.size());
168         }
getProfileList(::com::sun::star::mozilla::MozillaProductType product,::com::sun::star::uno::Sequence<::rtl::OUString> & list)169         ::sal_Int32 ProfileAccess::getProfileList( ::com::sun::star::mozilla::MozillaProductType product, ::com::sun::star::uno::Sequence< ::rtl::OUString >& list ) throw (::com::sun::star::uno::RuntimeException)
170         {
171             sal_Int32 index=product;
172             ProductStruct &m_Product = m_ProductProfileList[index];
173             list.realloc(static_cast<sal_Int32>(m_Product.mProfileList.size()));
174             sal_Int32 i=0;
175             for(ProfileList::iterator itor=m_Product.mProfileList.begin();
176                 itor != m_Product.mProfileList.end();
177                 itor++)
178             {
179                 ProfileStruct * aProfile = (*itor).second;
180                 list[i] = aProfile->getProfileName();
181                 i++;
182             }
183 
184             return static_cast< ::sal_Int32 >(m_Product.mProfileList.size());
185         }
186 
getDefaultProfile(::com::sun::star::mozilla::MozillaProductType product)187         ::rtl::OUString ProfileAccess::getDefaultProfile( ::com::sun::star::mozilla::MozillaProductType product ) throw (::com::sun::star::uno::RuntimeException)
188         {
189             sal_Int32 index=product;
190             ProductStruct &m_Product = m_ProductProfileList[index];
191             if (m_Product.mCurrentProfileName.getLength() != 0)
192             {
193                 //default profile setted in mozilla registry
194                 return m_Product.mCurrentProfileName;
195             }
196             if (m_Product.mProfileList.size() == 0)
197             {
198                 //there are not any profiles
199                 return ::rtl::OUString();
200             }
201             ProfileStruct * aProfile = (*m_Product.mProfileList.begin()).second;
202             return aProfile->getProfileName();
203         }
204 
isProfileLocked(::com::sun::star::mozilla::MozillaProductType product,const::rtl::OUString & profileName)205         ::sal_Bool ProfileAccess::isProfileLocked( ::com::sun::star::mozilla::MozillaProductType product, const ::rtl::OUString& profileName ) throw (::com::sun::star::uno::RuntimeException)
206         {
207             (void)product; /* avoid warning about unused parameter */
208             (void)profileName; /* avoid warning about unused parameter */
209             return sal_True;
210         }
211 
getProfileExists(::com::sun::star::mozilla::MozillaProductType product,const::rtl::OUString & profileName)212         ::sal_Bool ProfileAccess::getProfileExists( ::com::sun::star::mozilla::MozillaProductType product, const ::rtl::OUString& profileName ) throw (::com::sun::star::uno::RuntimeException)
213         {
214             sal_Int32 index=product;
215             ProductStruct &m_Product = m_ProductProfileList[index];
216             if (!m_Product.mProfileList.size() || m_Product.mProfileList.find(profileName) == m_Product.mProfileList.end())
217             {
218                 return sal_False;
219             }
220             else
221                 return sal_True;
222         }
223     }
224 }
225 
226 
227