Back to index

lightning-sunbird  0.9+nobinonly
RegFactory.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) 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 
00038 #include <iostream.h>
00039 #include "plstr.h"
00040 #include "prlink.h"
00041 #include "nsIComponentRegistrar.h"
00042 #include "nsIComponentManager.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsILocalFile.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsString.h"
00047 
00048 static PRBool gUnreg = PR_FALSE;
00049 
00050 void print_err(nsresult err)
00051 {
00052   switch (err) {
00053   case NS_ERROR_FACTORY_NOT_LOADED:
00054     cerr << "Factory not loaded";
00055     break;
00056   case NS_NOINTERFACE:
00057     cerr << "No Interface";
00058     break;
00059   case NS_ERROR_NULL_POINTER:
00060     cerr << "Null pointer";
00061     break;
00062   case NS_ERROR_OUT_OF_MEMORY:
00063     cerr << "Out of memory";
00064     break;
00065   default:
00066     cerr << hex << err << dec;
00067   }
00068 }
00069 
00070 nsresult Register(nsIComponentRegistrar* registrar, const char *path) 
00071 { 
00072   nsCOMPtr<nsILocalFile> file;
00073   nsresult rv =
00074     NS_NewLocalFile(
00075       NS_ConvertUTF8toUCS2(path),
00076       PR_TRUE,
00077       getter_AddRefs(file));
00078   if (NS_FAILED(rv)) return rv;
00079   rv = registrar->AutoRegister(file);
00080   return rv;
00081 }
00082 
00083 nsresult Unregister(const char *path) 
00084 {
00085   /* NEEDS IMPLEMENTATION */
00086 #if 0
00087     nsresult res = nsComponentManager::AutoUnregisterComponent(path);
00088   return res;
00089 #else
00090   return NS_ERROR_FAILURE;
00091 #endif
00092 }
00093 
00094 int ProcessArgs(nsIComponentRegistrar* registrar, int argc, char *argv[])
00095 {
00096   int i = 1;
00097   nsresult res;
00098 
00099   while (i < argc) {
00100     if (argv[i][0] == '-') {
00101       int j;
00102       for (j = 1; argv[i][j] != '\0'; j++) {
00103         switch (argv[i][j]) {
00104         case 'u':
00105           gUnreg = PR_TRUE;
00106           break;
00107         default:
00108           cerr << "Unknown option '" << argv[i][j] << "'\n";
00109         }
00110       }
00111       i++;
00112     } else {
00113       if (gUnreg == PR_TRUE) {
00114         res = Unregister(argv[i]);
00115         if (NS_SUCCEEDED(res)) {
00116           cout << "Successfully unregistered: " << argv[i] << "\n";
00117         } else {
00118           cerr << "Unregister failed (";
00119           print_err(res);
00120           cerr << "): " << argv[i] << "\n";
00121         }
00122       } else {
00123         res = Register(registrar, argv[i]);
00124         if (NS_SUCCEEDED(res)) {
00125           cout << "Successfully registered: " << argv[i] << "\n";
00126         } else {
00127           cerr << "Register failed (";
00128           print_err(res);
00129           cerr << "): " << argv[i] << "\n";
00130         }
00131       }
00132       i++;
00133     }
00134   }
00135   return 0;
00136 }
00137 
00138 int main(int argc, char *argv[])
00139 {
00140   int ret = 0;
00141   nsresult rv;
00142   {
00143     nsCOMPtr<nsIServiceManager> servMan;
00144     rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00145     if (NS_FAILED(rv)) return -1;
00146     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00147     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00148 
00149     /* With no arguments, RegFactory will autoregister */
00150     if (argc <= 1)
00151     {
00152       rv = registrar->AutoRegister(nsnull);
00153       ret = (NS_FAILED(rv)) ? -1 : 0;
00154     }
00155     else
00156       ret = ProcessArgs(registrar, argc, argv);
00157   } // this scopes the nsCOMPtrs
00158   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00159   rv = NS_ShutdownXPCOM( NULL );
00160   NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00161   return ret;
00162 }