Back to index

lightning-sunbird  0.9+nobinonly
nsMailProfileMigratorUtils.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 The Browser Profile Migrator.
00016  *
00017  * The Initial Developer of the Original Code is Ben Goodger.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *  Ben Goodger <ben@bengoodger.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsMailProfileMigratorUtils.h"
00039 #include "nsIFile.h"
00040 #include "nsIProperties.h"
00041 #include "nsIProfileMigrator.h"
00042 
00043 #include "nsServiceManagerUtils.h"
00044 #include "nsAppDirectoryServiceDefs.h"
00045 #include "nsXPCOMCID.h"
00046 #include "nsCRT.h"
00047 
00048 void SetProxyPref(const nsACString& aHostPort, const char* aPref, 
00049                   const char* aPortPref, nsIPrefBranch* aPrefs) 
00050 {
00051   nsCAutoString hostPort(aHostPort);  
00052   PRInt32 portDelimOffset = hostPort.RFindChar(':');
00053   if (portDelimOffset > 0) {
00054     nsCAutoString host(Substring(hostPort, 0, portDelimOffset));
00055     nsCAutoString port(Substring(hostPort, portDelimOffset + 1, 
00056                                  hostPort.Length() - (portDelimOffset + 1)));
00057     
00058     aPrefs->SetCharPref(aPref, host.get());
00059     PRInt32 stringErr;
00060     PRInt32 portValue = port.ToInteger(&stringErr);
00061     aPrefs->SetIntPref(aPortPref, portValue);
00062   }
00063   else
00064     aPrefs->SetCharPref(aPref, hostPort.get());
00065 }
00066 
00067 void ParseOverrideServers(const char* aServers, nsIPrefBranch* aBranch)
00068 {
00069   // Windows (and Opera) formats its proxy override list in the form:
00070   // server;server;server where server is a server name or ip address, 
00071   // or "<local>". Mozilla's format is server,server,server, and <local>
00072   // must be translated to "localhost,127.0.0.1"
00073   nsCAutoString override(aServers);
00074   PRInt32 left = 0, right = 0;
00075   for (;;) {
00076     right = override.FindChar(';', right);
00077     const nsACString& host = Substring(override, left, 
00078                                        (right < 0 ? override.Length() : right) - left);
00079     if (host.Equals("<local>"))
00080       override.Replace(left, 7, NS_LITERAL_CSTRING("localhost,127.0.0.1"));
00081     if (right < 0)
00082       break;
00083     left = right + 1;
00084     override.Replace(right, 1, NS_LITERAL_CSTRING(","));
00085   }
00086   aBranch->SetCharPref("network.proxy.no_proxies_on", override.get());
00087 }
00088 
00089 void GetMigrateDataFromArray(MigrationData* aDataArray, PRInt32 aDataArrayLength, 
00090                              PRBool aReplace, nsIFile* aSourceProfile, 
00091                              PRUint16* aResult)
00092 {
00093   nsCOMPtr<nsIFile> sourceFile; 
00094   PRBool exists;
00095   MigrationData* cursor;
00096   MigrationData* end = aDataArray + aDataArrayLength;
00097   for (cursor = aDataArray; cursor < end && cursor->fileName; ++cursor) {
00098     // When in replace mode, all items can be imported. 
00099     // When in non-replace mode, only items that do not require file replacement
00100     // can be imported.
00101     if (aReplace || !cursor->replaceOnly) {
00102       aSourceProfile->Clone(getter_AddRefs(sourceFile));
00103       sourceFile->Append(nsDependentString(cursor->fileName));
00104       sourceFile->Exists(&exists);
00105       if (exists)
00106         *aResult |= cursor->sourceFlag;
00107     }
00108     nsCRT::free(cursor->fileName);
00109     cursor->fileName = nsnull;
00110   }
00111 }
00112 
00113 void
00114 GetProfilePath(nsIProfileStartup* aStartup, nsCOMPtr<nsIFile>& aProfileDir)
00115 {
00116   if (aStartup) {
00117     aStartup->GetDirectory(getter_AddRefs(aProfileDir));
00118   }
00119   else {
00120     nsCOMPtr<nsIProperties> dirSvc
00121       (do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
00122     if (dirSvc) {
00123       dirSvc->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile),
00124                   (void**) getter_AddRefs(aProfileDir));
00125     }
00126   }
00127 }