Back to index

lightning-sunbird  0.9+nobinonly
DotNETProfileManager.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Roy Yokoyama <yokoyama@netscape.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #using <mscorlib.dll>
00040 #include "cstringt.h"
00041 
00042 #include "nsISupports.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsString.h"
00045 #include "nsXPIDLString.h"
00046 #include "nsIProfile.h"
00047 #include "DotNetProfileManager.h"
00048 
00049 using namespace Mozilla::Embedding;
00050 
00051 // static
00052 void
00053 ProfileManager::EnsureProfileService()
00054 {
00055   if (sProfileService) {
00056     return;
00057   }
00058 
00059   nsresult rv;
00060   nsCOMPtr<nsIProfile> profileService =
00061     do_GetService(NS_PROFILE_CONTRACTID, &rv);
00062   ThrowIfFailed(rv);
00063 
00064   sProfileService = profileService.get();
00065   NS_ADDREF(sProfileService);
00066 }
00067 
00068 // static
00069 Int32
00070 ProfileManager::get_ProfileCount() 
00071 { 
00072   EnsureProfileService();
00073 
00074   PRInt32 count;
00075   nsresult rv = sProfileService->GetProfileCount(&count);
00076   ThrowIfFailed(rv);
00077 
00078   return count;
00079 }
00080 
00081 // static
00082 String *
00083 ProfileManager::GetProfileList()[]
00084 {
00085   EnsureProfileService();
00086 
00087   PRUint32 profileCount;
00088   PRUnichar **profiles;
00089 
00090   nsresult rv = sProfileService->GetProfileList(&profileCount, &profiles);
00091   ThrowIfFailed(rv);
00092 
00093   String *list[] = new String *[profileCount];
00094 
00095   for (PRUint32 i = 0; i < profileCount; ++i) {
00096     list[i] = CopyString(nsDependentString(profiles[i]));
00097   }
00098 
00099   return list;
00100 }
00101 
00102 // static
00103 bool
00104 ProfileManager::ProfileExists(String *aProfileName)
00105 {
00106   EnsureProfileService();
00107 
00108   PRBool exists;
00109   const wchar_t __pin * profileName = PtrToStringChars(aProfileName);
00110 
00111   nsresult rv = sProfileService->ProfileExists(profileName, &exists);
00112   ThrowIfFailed(rv);
00113 
00114   return exists;
00115 }
00116 
00117 // static
00118 String*
00119 ProfileManager::get_CurrentProfile()
00120 {
00121   EnsureProfileService();
00122 
00123   nsXPIDLString currentProfile;
00124   nsresult rv =
00125     sProfileService->GetCurrentProfile(getter_Copies(currentProfile));
00126   ThrowIfFailed(rv);
00127 
00128   return CopyString(currentProfile);
00129 }
00130 
00131 // static
00132 void
00133 ProfileManager::set_CurrentProfile(String* aCurrentProfile)
00134 {
00135   EnsureProfileService();
00136 
00137   const wchar_t __pin * currentProfile = PtrToStringChars(aCurrentProfile);
00138   nsresult rv = sProfileService->SetCurrentProfile(currentProfile);
00139   ThrowIfFailed(rv);
00140 }
00141 
00142 // static
00143 void
00144 ProfileManager::ShutDownCurrentProfile(UInt32 shutDownType)
00145 {
00146   EnsureProfileService();
00147 
00148   nsresult rv = sProfileService->ShutDownCurrentProfile(shutDownType);
00149   ThrowIfFailed(rv);
00150 }
00151 
00152 // static
00153 void
00154 ProfileManager::CreateNewProfile(String* aProfileName,
00155                                  String *aNativeProfileDir, String* aLangcode,
00156                                  bool useExistingDir)
00157 {
00158   EnsureProfileService();
00159 
00160   const wchar_t __pin * profileName = PtrToStringChars(aProfileName);
00161   const wchar_t __pin * nativeProfileDir = PtrToStringChars(aNativeProfileDir);
00162   const wchar_t __pin * langCode = PtrToStringChars(aLangcode);
00163 
00164   nsresult rv = sProfileService->CreateNewProfile(profileName,
00165                                                   nativeProfileDir, langCode,
00166                                                   useExistingDir);
00167   ThrowIfFailed(rv);
00168 }
00169 
00170 // static
00171 void
00172 ProfileManager::RenameProfile(String* aOldName, String* aNewName)
00173 {
00174   EnsureProfileService();
00175 
00176   const wchar_t __pin * oldName = PtrToStringChars(aOldName);
00177   const wchar_t __pin * newName = PtrToStringChars(aNewName);
00178 
00179   nsresult rv = sProfileService->RenameProfile(oldName, newName);
00180   ThrowIfFailed(rv);
00181 }
00182 
00183 // static
00184 void
00185 ProfileManager::DeleteProfile(String* aName, bool aCanDeleteFiles)
00186 {
00187   EnsureProfileService();
00188 
00189   const wchar_t __pin * name = PtrToStringChars(aName);
00190   nsresult rv = sProfileService->DeleteProfile(name, aCanDeleteFiles);
00191   ThrowIfFailed(rv);
00192 }
00193 
00194 // static
00195 void
00196 ProfileManager::CloneProfile(String* aProfileName)
00197 {
00198   EnsureProfileService();
00199 
00200   const wchar_t __pin * profileName = PtrToStringChars(aProfileName);
00201 
00202   nsresult rv = sProfileService->CloneProfile(profileName);
00203   ThrowIfFailed(rv);
00204 }
00205