Back to index

lightning-sunbird  0.9+nobinonly
nsMsgServiceProvider.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alec Flett <alecf@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or 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 #include "nsMsgServiceProvider.h"
00040 #include "nsIServiceManager.h"
00041 
00042 #include "nsXPIDLString.h"
00043 
00044 #include "nsRDFCID.h"
00045 #include "nsIRDFService.h"
00046 #include "nsIRDFRemoteDataSource.h"
00047 
00048 #include "nsIFileChannel.h"
00049 #include "nsIFileSpec.h"
00050 #include "nsNetUtil.h"
00051 #include "nsIMsgMailSession.h"
00052 #include "nsMsgBaseCID.h"
00053 
00054 #include "nsMailDirServiceDefs.h"
00055 #include "nsDirectoryServiceUtils.h"
00056 #include "nsDirectoryServiceDefs.h"
00057 #include "nsISimpleEnumerator.h"
00058 #include "nsIDirectoryEnumerator.h"
00059 
00060 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
00061 static NS_DEFINE_CID(kRDFCompositeDataSourceCID, NS_RDFCOMPOSITEDATASOURCE_CID);
00062 static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
00063 
00064 nsMsgServiceProviderService::nsMsgServiceProviderService()
00065 {}
00066 
00067 nsMsgServiceProviderService::~nsMsgServiceProviderService()
00068 {}
00069 
00070 NS_IMPL_ISUPPORTS1(nsMsgServiceProviderService, nsIRDFDataSource)
00071 
00072 nsresult
00073 nsMsgServiceProviderService::Init()
00074 {
00075   nsresult rv;
00076   nsCOMPtr<nsIRDFService> rdf = do_GetService(kRDFServiceCID, &rv);
00077   NS_ENSURE_SUCCESS(rv, rv);
00078   
00079   mInnerDataSource = do_CreateInstance(kRDFCompositeDataSourceCID, &rv);
00080   NS_ENSURE_SUCCESS(rv, rv);
00081 
00082   LoadISPFiles();
00083   return NS_OK;
00084 }
00085 
00090 void nsMsgServiceProviderService::LoadISPFiles()
00091 {
00092   nsresult rv;
00093   nsCOMPtr<nsIProperties> dirSvc = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
00094   if (NS_FAILED(rv))
00095     return;
00096 
00097   // Walk through the list of isp directories
00098   nsCOMPtr<nsISimpleEnumerator> ispDirectories;
00099   rv = dirSvc->Get(ISP_DIRECTORY_LIST,
00100                    NS_GET_IID(nsISimpleEnumerator), getter_AddRefs(ispDirectories));
00101   if (NS_FAILED(rv))
00102     return;
00103 
00104   PRBool hasMore;
00105   nsCOMPtr<nsIFile> ispDirectory;
00106   while (NS_SUCCEEDED(ispDirectories->HasMoreElements(&hasMore)) && hasMore) 
00107   {
00108     nsCOMPtr<nsISupports> elem;
00109     ispDirectories->GetNext(getter_AddRefs(elem));
00110 
00111     ispDirectory = do_QueryInterface(elem);
00112     if (ispDirectory)
00113       LoadISPFilesFromDir(ispDirectory);
00114   }
00115 }
00116 
00117 void nsMsgServiceProviderService::LoadISPFilesFromDir(nsIFile* aDir)
00118 {
00119   nsresult rv;
00120 
00121   PRBool check = PR_FALSE;
00122   rv = aDir->Exists(&check);
00123   if (NS_FAILED(rv) || !check)
00124     return;
00125 
00126   rv = aDir->IsDirectory(&check);
00127   if (NS_FAILED(rv) || !check)
00128     return;
00129 
00130   nsCOMPtr<nsISimpleEnumerator> e;
00131   rv = aDir->GetDirectoryEntries(getter_AddRefs(e));
00132   if (NS_FAILED(rv))
00133     return;
00134 
00135   nsCOMPtr<nsIDirectoryEnumerator> files(do_QueryInterface(e));
00136   if (!files)
00137     return;
00138 
00139   // we only care about the .rdf files in this directory
00140   nsCOMPtr<nsIFile> file;
00141   while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(file))) && file) {
00142     nsAutoString leafName;
00143     file->GetLeafName(leafName);
00144     if (!StringEndsWith(leafName, NS_LITERAL_STRING(".rdf")))
00145       continue;
00146 
00147     nsCAutoString urlSpec;
00148     rv = NS_GetURLSpecFromFile(file, urlSpec);
00149     if (NS_SUCCEEDED(rv))
00150       LoadDataSource(urlSpec.get());
00151   }
00152 }
00153 
00154 nsresult
00155 nsMsgServiceProviderService::LoadDataSource(const char *aURI)
00156 {
00157     nsresult rv;
00158 
00159     nsCOMPtr<nsIRDFDataSource> ds =
00160         do_CreateInstance(kRDFXMLDataSourceCID, &rv);
00161     NS_ENSURE_SUCCESS(rv,rv);
00162 
00163     nsCOMPtr<nsIRDFRemoteDataSource> remote =
00164         do_QueryInterface(ds, &rv);
00165     NS_ENSURE_SUCCESS(rv, rv);
00166     
00167     rv = remote->Init(aURI);
00168     NS_ENSURE_SUCCESS(rv, rv);
00169     // for now load synchronously (async seems to be busted)
00170     rv = remote->Refresh(PR_TRUE);
00171     NS_ASSERTION(NS_SUCCEEDED(rv), "failed refresh?\n");
00172 
00173     rv = mInnerDataSource->AddDataSource(ds);
00174 
00175     return rv;
00176 }