Back to index

lightning-sunbird  0.9+nobinonly
nsInstallVersion.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 
00038 #include "nsSoftwareUpdate.h"
00039 
00040 #include "nsInstall.h"
00041 #include "nsInstallVersion.h"
00042 #include "nsIDOMInstallVersion.h"
00043 
00044 #include "nscore.h"
00045 #include "nsString.h"
00046 #include "nsIFactory.h"
00047 #include "nsISupports.h"
00048 #include "nsIScriptGlobalObject.h"
00049 
00050 #include "prprf.h"
00051 
00052 static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
00053 static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
00054 
00055 static NS_DEFINE_IID(kIInstallVersion_IID, NS_IDOMINSTALLVERSION_IID);
00056 
00057 
00058 nsInstallVersion::nsInstallVersion()
00059 {
00060     mScriptObject   = nsnull;
00061 }
00062 
00063 nsInstallVersion::~nsInstallVersion()
00064 {
00065 }
00066 
00067 NS_IMETHODIMP
00068 nsInstallVersion::QueryInterface(REFNSIID aIID,void** aInstancePtr)
00069 {
00070     if (aInstancePtr == NULL)
00071     {
00072         return NS_ERROR_NULL_POINTER;
00073     }
00074 
00075     // Always NULL result, in case of failure
00076     *aInstancePtr = NULL;
00077 
00078     if ( aIID.Equals(kIScriptObjectOwnerIID))
00079     {
00080         *aInstancePtr = (void*) ((nsIScriptObjectOwner*)this);
00081         AddRef();
00082         return NS_OK;
00083     }
00084     else if ( aIID.Equals(kIInstallVersion_IID) )
00085     {
00086         *aInstancePtr = (void*) ((nsIDOMInstallVersion*)this);
00087         AddRef();
00088         return NS_OK;
00089     }
00090     else if ( aIID.Equals(kISupportsIID) )
00091     {
00092         *aInstancePtr = (void*)(nsISupports*)(nsIScriptObjectOwner*)this;
00093         AddRef();
00094         return NS_OK;
00095     }
00096 
00097      return NS_NOINTERFACE;
00098 }
00099 
00100 
00101 NS_IMPL_ADDREF(nsInstallVersion)
00102 NS_IMPL_RELEASE(nsInstallVersion)
00103 
00104 
00105 
00106 NS_IMETHODIMP
00107 nsInstallVersion::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject)
00108 {
00109     NS_PRECONDITION(nsnull != aScriptObject, "null arg");
00110     nsresult res = NS_OK;
00111 
00112     if (!mScriptObject)
00113     {
00114         res = NS_NewScriptInstallVersion(aContext,
00115                                          (nsISupports *)(nsIDOMInstallVersion*)this,
00116                                          nsnull,
00117                                          &mScriptObject);
00118     }
00119 
00120 
00121     *aScriptObject = mScriptObject;
00122     return res;
00123 }
00124 
00125 NS_IMETHODIMP
00126 nsInstallVersion::SetScriptObject(void *aScriptObject)
00127 {
00128   mScriptObject = aScriptObject;
00129   return NS_OK;
00130 }
00131 
00132 //  this will go away when our constructors can have parameters.
00133 NS_IMETHODIMP
00134 nsInstallVersion::Init(PRInt32 aMajor, PRInt32 aMinor, PRInt32 aRelease, PRInt32 aBuild)
00135 {
00136     mMajor   = aMajor;
00137     mMinor   = aMinor;
00138     mRelease = aRelease;
00139     mBuild   = aBuild;
00140 
00141     return NS_OK;
00142 }
00143 
00144 NS_IMETHODIMP
00145 nsInstallVersion::Init(const nsString& version)
00146 {
00147     mMajor = mMinor = mRelease = mBuild = 0;
00148 
00149     // nsString is a flat string
00150     if (PR_sscanf(NS_ConvertUTF16toUTF8(version).get(),"%d.%d.%d.%d",&mMajor,&mMinor,&mRelease,&mBuild) < 1)
00151         return NS_ERROR_UNEXPECTED;
00152 
00153     return NS_OK;
00154 }
00155 
00156 
00157 NS_IMETHODIMP
00158 nsInstallVersion::GetMajor(PRInt32* aMajor)
00159 {
00160     *aMajor = mMajor;
00161     return NS_OK;
00162 }
00163 
00164 NS_IMETHODIMP
00165 nsInstallVersion::SetMajor(PRInt32 aMajor)
00166 {
00167     mMajor = aMajor;
00168     return NS_OK;
00169 }
00170 
00171 NS_IMETHODIMP
00172 nsInstallVersion::GetMinor(PRInt32* aMinor)
00173 {
00174     *aMinor = mMinor;
00175     return NS_OK;
00176 }
00177 
00178 NS_IMETHODIMP
00179 nsInstallVersion::SetMinor(PRInt32 aMinor)
00180 {
00181     mMinor = aMinor;
00182     return NS_OK;
00183 }
00184 
00185 NS_IMETHODIMP
00186 nsInstallVersion::GetRelease(PRInt32* aRelease)
00187 {
00188     *aRelease = mRelease;
00189     return NS_OK;
00190 }
00191 
00192 NS_IMETHODIMP
00193 nsInstallVersion::SetRelease(PRInt32 aRelease)
00194 {
00195     mRelease = aRelease;
00196     return NS_OK;
00197 }
00198 
00199 NS_IMETHODIMP
00200 nsInstallVersion::GetBuild(PRInt32* aBuild)
00201 {
00202     *aBuild = mBuild;
00203     return NS_OK;
00204 }
00205 
00206 NS_IMETHODIMP
00207 nsInstallVersion::SetBuild(PRInt32 aBuild)
00208 {
00209     mBuild = aBuild;
00210     return NS_OK;
00211 }
00212 
00213 
00214 NS_IMETHODIMP
00215 nsInstallVersion::CompareTo(nsIDOMInstallVersion* aVersion, PRInt32* aReturn)
00216 {
00217     PRInt32 aMajor, aMinor, aRelease, aBuild;
00218 
00219     aVersion->GetMajor(&aMajor);
00220     aVersion->GetMinor(&aMinor);
00221     aVersion->GetRelease(&aRelease);
00222     aVersion->GetBuild(&aBuild);
00223 
00224     CompareTo(aMajor, aMinor, aRelease, aBuild, aReturn);
00225 
00226     return NS_OK;
00227 }
00228 
00229 
00230 NS_IMETHODIMP
00231 nsInstallVersion::CompareTo(const nsString& aString, PRInt32* aReturn)
00232 {
00233     nsInstallVersion inVersion;
00234     inVersion.Init(aString);
00235 
00236     return CompareTo(&inVersion, aReturn);
00237 }
00238 
00239 NS_IMETHODIMP
00240 nsInstallVersion::CompareTo(PRInt32 aMajor, PRInt32 aMinor, PRInt32 aRelease, PRInt32 aBuild, PRInt32* aReturn)
00241 {
00242     int diff;
00243 
00244     if ( mMajor == aMajor )
00245     {
00246         if ( mMinor == aMinor )
00247         {
00248             if ( mRelease == aRelease )
00249             {
00250                 if ( mBuild == aBuild )
00251                     diff = EQUAL;
00252                 else if ( mBuild > aBuild )
00253                     diff = BLD_DIFF;
00254                 else
00255                     diff = BLD_DIFF_MINUS;
00256             }
00257             else if ( mRelease > aRelease )
00258                 diff = REL_DIFF;
00259             else
00260                 diff = REL_DIFF_MINUS;
00261         }
00262         else if (  mMinor > aMinor )
00263             diff = MINOR_DIFF;
00264         else
00265             diff = MINOR_DIFF_MINUS;
00266     }
00267     else if ( mMajor > aMajor )
00268         diff = MAJOR_DIFF;
00269     else
00270         diff = MAJOR_DIFF_MINUS;
00271 
00272     *aReturn = diff;
00273 
00274     return NS_OK;
00275 }
00276 
00277 NS_IMETHODIMP
00278 nsInstallVersion::ToString(nsString& aReturn)
00279 {
00280     char buf[128];
00281     PRUint32 len;
00282 
00283     len=PR_snprintf(buf, sizeof(buf), "%d.%d.%d.%d", mMajor, mMinor, mRelease, mBuild);
00284     aReturn.AssignASCII(buf,len);
00285 
00286     return NS_OK;
00287 }