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      {
42          ProfileStruct::ProfileStruct(MozillaProductType aProduct,::rtl::OUString aProfileName,
43              const ::rtl::OUString& aProfilePath)
44          {
45              product=aProduct;
46              profileName = aProfileName;
47              profilePath = aProfilePath;
48          }
49          ::rtl::OUString ProfileStruct::getProfilePath()
50          {
51              return profilePath;
52          }
53  
54          ProfileAccess::~ProfileAccess()
55          {
56          }
57          ProfileAccess::ProfileAccess()
58          {
59              LoadProductsInfo();
60          }
61  
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
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  
127                      ProfileStruct*  profileItem     = new ProfileStruct(product,profileName,
128                              regDir + profilePath);
129                      m_Product.mProfileList[profileName] = profileItem;
130  
131                      sal_Int32 isDefault = 0;
132                      if (sIsDefault.getLength() != 0)
133                      {
134                          isDefault = sIsDefault.toInt32();
135                      }
136                      if (isDefault)
137                          m_Product.mCurrentProfileName = profileName;
138  
139                  }
140  
141              }
142              return static_cast< ::sal_Int32 >(m_Product.mProfileList.size());
143          }
144  
145          ::rtl::OUString ProfileAccess::getProfilePath( ::com::sun::star::mozilla::MozillaProductType product, const ::rtl::OUString& profileName ) throw (::com::sun::star::uno::RuntimeException)
146          {
147              sal_Int32 index=product;
148              ProductStruct &m_Product = m_ProductProfileList[index];
149              if (!m_Product.mProfileList.size() || m_Product.mProfileList.find(profileName) == m_Product.mProfileList.end())
150              {
151                  //Profile not found
152                  return ::rtl::OUString();
153              }
154              else
155                  return m_Product.mProfileList[profileName]->getProfilePath();
156          }
157  
158          ::sal_Int32 ProfileAccess::getProfileCount( ::com::sun::star::mozilla::MozillaProductType product) throw (::com::sun::star::uno::RuntimeException)
159          {
160              sal_Int32 index=product;
161              ProductStruct &m_Product = m_ProductProfileList[index];
162              return static_cast< ::sal_Int32 >(m_Product.mProfileList.size());
163          }
164          ::sal_Int32 ProfileAccess::getProfileList( ::com::sun::star::mozilla::MozillaProductType product, ::com::sun::star::uno::Sequence< ::rtl::OUString >& list ) throw (::com::sun::star::uno::RuntimeException)
165          {
166              sal_Int32 index=product;
167              ProductStruct &m_Product = m_ProductProfileList[index];
168              list.realloc(static_cast<sal_Int32>(m_Product.mProfileList.size()));
169              sal_Int32 i=0;
170              for(ProfileList::iterator itor=m_Product.mProfileList.begin();
171                  itor != m_Product.mProfileList.end();
172                  itor++)
173              {
174                  ProfileStruct * aProfile = (*itor).second;
175                  list[i] = aProfile->getProfileName();
176                  i++;
177              }
178  
179              return static_cast< ::sal_Int32 >(m_Product.mProfileList.size());
180          }
181  
182          ::rtl::OUString ProfileAccess::getDefaultProfile( ::com::sun::star::mozilla::MozillaProductType product ) throw (::com::sun::star::uno::RuntimeException)
183          {
184              sal_Int32 index=product;
185              ProductStruct &m_Product = m_ProductProfileList[index];
186              if (m_Product.mCurrentProfileName.getLength() != 0)
187              {
188                  //default profile setted in mozilla registry
189                  return m_Product.mCurrentProfileName;
190              }
191              if (m_Product.mProfileList.size() == 0)
192              {
193                  //there are not any profiles
194                  return ::rtl::OUString();
195              }
196              ProfileStruct * aProfile = (*m_Product.mProfileList.begin()).second;
197              return aProfile->getProfileName();
198          }
199  
200          ::sal_Bool ProfileAccess::isProfileLocked( ::com::sun::star::mozilla::MozillaProductType product, const ::rtl::OUString& profileName ) throw (::com::sun::star::uno::RuntimeException)
201          {
202              (void)product; /* avoid warning about unused parameter */
203              (void)profileName; /* avoid warning about unused parameter */
204              return sal_True;
205          }
206  
207          ::sal_Bool ProfileAccess::getProfileExists( ::com::sun::star::mozilla::MozillaProductType product, const ::rtl::OUString& profileName ) throw (::com::sun::star::uno::RuntimeException)
208          {
209              sal_Int32 index=product;
210              ProductStruct &m_Product = m_ProductProfileList[index];
211              if (!m_Product.mProfileList.size() || m_Product.mProfileList.find(profileName) == m_Product.mProfileList.end())
212              {
213                  return sal_False;
214              }
215              else
216                  return sal_True;
217          }
218      }
219  }
220  
221  
222