Back to index

lightning-sunbird  0.9+nobinonly
grabpage.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 Communicator client 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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or 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 #include "nsIStreamListener.h"
00038 #include "nsIInputStream.h"
00039 #include "nsIURL.h"
00040 
00041 #include "nsNetCID.h"
00042 #include "nsCOMPtr.h"
00043 #include "nsIIOService.h"
00044 #include "nsIChannel.h"
00045 #include "nsILocalFile.h"
00046 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
00047 
00048 #include "nsIEventQueueService.h"
00049 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00050 static nsIEventQueue* gEventQ = nsnull;
00051 
00052 #include "nsString.h"
00053 #include "nsReadableUtils.h"
00054 #include "nsCRT.h"
00055 #include "prprf.h"
00056 
00057 #ifdef XP_WIN
00058 #include <windows.h>
00059 #include <sys/types.h>
00060 #include <sys/stat.h>
00061 #endif
00062 #ifdef XP_UNIX
00063 #include <sys/types.h>
00064 #include <sys/stat.h>
00065 #endif
00066 #ifdef XP_OS2
00067 #include <os2.h>
00068 #include <sys/types.h>
00069 #include <sys/stat.h>
00070 #endif
00071 
00072 class StreamToFile : public nsIStreamListener {
00073 public:
00074   StreamToFile(FILE* fp);
00075 
00076   NS_DECL_ISUPPORTS
00077 
00078   NS_IMETHOD GetBindInfo(nsIURI* aURL);
00079   NS_IMETHOD OnProgress(nsIURI* aURL, PRInt32 Progress, PRInt32 ProgressMax);
00080   NS_IMETHOD OnStatus(nsIURI* aURL, const nsString& aMsg);
00081   NS_IMETHOD OnStartRequest(nsIRequest* aRequest, nsISupports *);
00082   NS_IMETHOD OnDataAvailable(nsIRequest* aRequest, nsISupports *, nsIInputStream *pIStream, PRUint32 aOffset, PRUint32 aCount);
00083   NS_IMETHOD OnStopRequest(nsIRequest* aRequest, nsISupports *, PRUint32 status);
00084 
00085   PRBool IsDone() const { return mDone; }
00086   PRBool HaveError() const { return mError; }
00087 
00088 protected:
00089   virtual ~StreamToFile();
00090 
00091   PRBool mDone;
00092   PRBool mError;
00093   FILE* mFile;
00094 };
00095 
00096 StreamToFile::StreamToFile(FILE* fp)
00097 {
00098   mDone = PR_FALSE;
00099   mError = PR_FALSE;
00100   mFile = fp;
00101 }
00102 
00103 NS_IMPL_ISUPPORTS1(StreamToFile, nsIStreamListener)
00104 
00105 StreamToFile::~StreamToFile()
00106 {
00107   if (nsnull != mFile) {
00108     fclose(mFile);
00109   }
00110 }
00111 
00112 NS_IMETHODIMP
00113 StreamToFile::GetBindInfo(nsIURI* aURL)
00114 {
00115   return 0;
00116 }
00117 
00118 NS_IMETHODIMP
00119 StreamToFile::OnProgress(nsIURI* aURL, PRInt32 Progress, PRInt32 ProgressMax)
00120 {
00121   return 0;
00122 }
00123 
00124 NS_IMETHODIMP
00125 StreamToFile::OnStatus(nsIURI* aURL, const nsString& aMsg)
00126 {
00127   return 0;
00128 }
00129 
00130 NS_IMETHODIMP
00131 StreamToFile::OnStartRequest(nsIRequest *aRequest, nsISupports *aSomething)
00132 {
00133   return 0;
00134 }
00135 
00136 NS_IMETHODIMP
00137 StreamToFile::OnDataAvailable(
00138   nsIRequest* aRequest,
00139   nsISupports *,
00140   nsIInputStream *pIStream,
00141   PRUint32 aOffset,
00142   PRUint32 aCount)
00143 {
00144   PRUint32 len;
00145   do {
00146     char buffer[4000];
00147     nsresult err = pIStream->Read(buffer, sizeof(buffer), &len);
00148     if (NS_SUCCEEDED(err)) {
00149       if (nsnull != mFile) {
00150         fwrite(buffer, 1, len, mFile);
00151       }
00152     }
00153   } while (len > 0);
00154   return 0;
00155 }
00156 
00157 
00158 NS_IMETHODIMP
00159 StreamToFile::OnStopRequest(nsIRequest *aRequest, nsISupports *aSomething, PRUint32 status)
00160 {
00161   mDone = PR_TRUE;
00162   if (0 != status) {
00163     mError = PR_TRUE;
00164   }
00165   return 0;
00166 }
00167 
00168 //----------------------------------------------------------------------
00169 
00170 // This could turn into a handy utility someday...
00171 
00172 class PageGrabber {
00173 public:
00174   PageGrabber();
00175   ~PageGrabber();
00176 
00177   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00178 
00179   nsresult Init(nsILocalFile *aDirectory);
00180 
00181   nsresult Grab(const nsAFlatCString& aURL);
00182 
00183 protected:
00184   nsILocalFile* NextFile(const char* aExtension);
00185 
00186   nsILocalFile *mDirectory;
00187   PRInt32 mFileNum;
00188 };
00189 
00190 PageGrabber::PageGrabber()
00191 {
00192 }
00193 
00194 PageGrabber::~PageGrabber()
00195 {
00196 }
00197 
00198 nsresult
00199 PageGrabber::Init(nsILocalFile *aDirectory)
00200 {
00201   mDirectory = aDirectory;
00202   return NS_OK;
00203 }
00204 
00205 nsILocalFile*
00206 PageGrabber::NextFile(const char* aExtension)
00207 {
00208   nsCAutoString name(NS_LITERAL_CSTRING("grab."));
00209   name += nsDependentCString(aExtension);
00210   nsIFile *cfile;
00211   mDirectory->Clone(&cfile);
00212   nsCOMPtr<nsILocalFile> file = do_QueryInterface(cfile);
00213   file->AppendRelativeNativePath(name);
00214   file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0660);
00215   return file;
00216 }
00217 
00218 nsresult
00219 PageGrabber::Grab(const nsAFlatCString& aURL)
00220 {
00221   nsresult rv;
00222   // Create the Event Queue for this thread...
00223   // Unix needs this
00224   nsCOMPtr<nsIEventQueueService> eventQService =
00225            do_GetService(kEventQueueServiceCID, &rv);
00226   if (NS_FAILED(rv)) return rv;
00227 
00228   eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00229 
00230   nsCOMPtr<nsILocalFile> file = NextFile("html");
00231   if (!file) {
00232     return NS_ERROR_OUT_OF_MEMORY;
00233   }
00234   FILE* fp;
00235   rv = file->OpenANSIFileDesc("wb", &fp);
00236   if (NS_FAILED(rv)) {
00237     return rv;
00238   }
00239   printf("Copying ");
00240   fputs(aURL.get(), stdout);
00241   nsAutoString path;
00242   file->GetPath(path);
00243   NS_ConvertUCS2toUTF8 cpath(path);
00244   printf(" to %s\n", cpath.get());
00245 
00246   // Create the URL object...
00247   nsCOMPtr<nsIURI> url;
00248 
00249   nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
00250   if (NS_FAILED(rv)) return rv;
00251 
00252   rv = ioService->NewURI(aURL, nsnull, nsnull, getter_AddRefs(url));
00253   nsIChannel *channel = nsnull;
00254   rv = ioService->NewChannelFromURI(url, &channel);
00255   if (NS_FAILED(rv)) return rv;
00256 
00257   // Start the URL load...
00258   StreamToFile* copier = new StreamToFile(fp);
00259   if (!copier)
00260     return NS_ERROR_OUT_OF_MEMORY;
00261 
00262   NS_ADDREF(copier);
00263 
00264   rv = channel->AsyncOpen(copier, nsnull);
00265 
00266   if (NS_FAILED(rv)) {
00267     NS_RELEASE(copier);
00268     return rv;
00269   }
00270     
00271   // Enter the message pump to allow the URL load to proceed.
00272   while ( !copier->IsDone() ) {
00273     PLEvent *gEvent;
00274     gEventQ->WaitForEvent(&gEvent);
00275     gEventQ->HandleEvent(gEvent);
00276   }
00277 
00278   rv = copier->HaveError() ? NS_ERROR_FAILURE : NS_OK;
00279   NS_RELEASE(copier);
00280 
00281   return rv;
00282 }
00283 
00284 //----------------------------------------------------------------------
00285 
00286 int
00287 main(int argc, char **argv)
00288 {
00289   nsString url_address;
00290 
00291   if (argc != 3) {
00292     fprintf(stderr, "Usage: grabpage url directory\n");
00293     return -1;
00294   }
00295   PageGrabber* grabber = new PageGrabber();
00296   if(grabber) {
00297     nsCOMPtr <nsILocalFile> directory(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));;
00298     if (NS_FAILED(directory->InitWithNativePath(nsDependentCString(argv[2])))) {
00299       fprintf(stderr, "InitWithNativePath failed\n");
00300       return -2;
00301     }
00302     grabber->Init(directory);
00303     if (NS_OK != grabber->Grab(nsDependentCString(argv[1]))) {
00304       return -1;
00305     }
00306   }
00307   return 0;
00308 }