Back to index

lightning-sunbird  0.9+nobinonly
nsComponent.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsComponent.h"
00041 #include <sys/stat.h>
00042 
00043 nsComponent::nsComponent() :
00044     mDescShort(NULL),
00045     mDescLong(NULL),
00046     mArchive(NULL),
00047     mInstallSize(0),
00048     mArchiveSize(0),
00049     mNextDependeeIdx(0),
00050     mAttributes(NO_ATTR),
00051     mIndex(-1),
00052     mRefCount(0),
00053     mDepRefCount(0),
00054     mResPos(0),
00055     mDownloaded(FALSE)
00056 {
00057     int i;
00058 
00059     for (i = 0; i < MAX_URLS; i++)
00060         mURL[i] = NULL;
00061     for (i = 0; i < MAX_COMPONENTS; i++)
00062         mDependees[i] = NULL;
00063 }
00064 
00065 nsComponent::~nsComponent()
00066 {
00067     int i;
00068 
00069     XI_IF_FREE(mDescShort);
00070     XI_IF_FREE(mDescLong);
00071     XI_IF_FREE(mArchive);
00072     for (i = 0; i < MAX_URLS; i++)
00073         XI_IF_FREE(mURL[i]);
00074     for (i = 0; i < MAX_COMPONENTS; i++)
00075         XI_IF_FREE(mDependees[i]);
00076 }
00077 
00078 nsComponent *
00079 nsComponent::Duplicate()
00080 {
00081     nsComponent *zdup = new nsComponent();
00082     *zdup = *this;
00083     zdup->InitRefCount();
00084 
00085     return zdup;
00086 }
00087 
00088 int
00089 nsComponent::SetDescShort(char *aDescShort)
00090 {
00091     if (!aDescShort)
00092         return E_PARAM;
00093 
00094     mDescShort = aDescShort;
00095 
00096     return OK;
00097 }
00098 
00099 char *
00100 nsComponent::GetDescShort()
00101 {
00102     if (mDescShort)
00103         return mDescShort;
00104 
00105     return NULL;
00106 }
00107 
00108 int
00109 nsComponent::SetDescLong(char *aDescLong)
00110 {
00111     if (!aDescLong)
00112         return E_PARAM;
00113 
00114     mDescLong = aDescLong;
00115 
00116     return OK;
00117 }
00118 
00119 char *
00120 nsComponent::GetDescLong()
00121 {
00122     if (mDescLong)
00123         return mDescLong;
00124 
00125     return NULL;
00126 }
00127 
00128 int
00129 nsComponent::SetArchive(char *aArchive)
00130 {
00131     if (!aArchive)
00132         return E_PARAM;
00133 
00134     mArchive = aArchive;
00135 
00136     return OK;
00137 }
00138 
00139 char *
00140 nsComponent::GetArchive()
00141 {
00142     if (mArchive)
00143         return mArchive;
00144 
00145     return NULL;
00146 }
00147 
00148 int
00149 nsComponent::SetInstallSize(int aInstallSize)
00150 {
00151     mInstallSize = aInstallSize;
00152 
00153     return OK;
00154 }
00155 
00156 int
00157 nsComponent::GetInstallSize()
00158 {
00159     if (mInstallSize >= 0)
00160         return mInstallSize;
00161 
00162     return 0;
00163 }
00164 
00165 int
00166 nsComponent::SetArchiveSize(int aArchiveSize)
00167 {
00168     mArchiveSize = aArchiveSize;
00169 
00170     return OK;
00171 }
00172 
00173 int
00174 nsComponent::GetArchiveSize()
00175 {
00176     if (mArchiveSize >= 0)
00177         return mArchiveSize;
00178 
00179     return 0;
00180 }
00181 
00182 int
00183 nsComponent::GetCurrentSize()
00184 {
00185     // assumes cwd is the same as the installer binary location
00186 
00187     char path[MAXPATHLEN];
00188     struct stat stbuf;
00189 
00190     if (!mArchive)
00191         return 0;
00192 
00193     sprintf(path, "./xpi/%s", mArchive);
00194     if (0 != stat(path, &stbuf))
00195         return 0;
00196 
00197     return (stbuf.st_size/1024); // return size in KB
00198 }
00199 
00200 int
00201 nsComponent::SetURL(char *aURL, int aIndex)
00202 {
00203     if (!aURL)
00204         return E_PARAM;
00205     if (mURL[aIndex])
00206         return E_URL_ALREADY;
00207 
00208     mURL[aIndex] = aURL;
00209     
00210     return OK;
00211 }
00212 
00213 char *
00214 nsComponent::GetURL(int aIndex)
00215 {
00216     if (aIndex < 0 || aIndex >= MAX_URLS)
00217         return NULL;
00218 
00219     return mURL[aIndex];
00220 }
00221 
00222 int
00223 nsComponent::AddDependee(char *aDependee)
00224 {
00225     if (!aDependee)
00226         return E_PARAM;
00227 
00228     mDependees[mNextDependeeIdx] = aDependee;
00229     mDependees[++mNextDependeeIdx] = NULL;
00230 
00231     return OK;
00232 }
00233 
00234 int 
00235 nsComponent::ResolveDependees(int aBeingSelected, nsComponentList *aComps)
00236 {
00237     int i;
00238     nsComponent *currComp = NULL;
00239     
00240     // param check
00241     if (!aComps)
00242         return E_PARAM;
00243 
00244     // loop over all dependees
00245     for (i = 0; i < mNextDependeeIdx; i++)
00246     {
00247         if (!mDependees[i])
00248             break;
00249 
00250         currComp = aComps->GetCompByShortDesc(mDependees[i]);
00251         if (!currComp)
00252             continue;
00253         
00254         if (aBeingSelected)
00255             currComp->DepAddRef();
00256         else    
00257             currComp->DepRelease();
00258     }
00259 
00260     return OK;
00261 }
00262 
00263 int
00264 nsComponent::SetSelected()
00265 {
00266     mAttributes |= nsComponent::SELECTED;
00267 
00268     return OK;
00269 }
00270 
00271 int
00272 nsComponent::SetUnselected()
00273 {
00274     if (IsSelected())
00275         mAttributes &= ~nsComponent::SELECTED;
00276     mDepRefCount = 0;
00277 
00278     return OK;
00279 }
00280 
00281 int
00282 nsComponent::IsSelected()
00283 {
00284     if (mAttributes & nsComponent::SELECTED)
00285         return TRUE;
00286 
00287     return FALSE;
00288 }
00289 
00290 int
00291 nsComponent::SetInvisible()
00292 {
00293     mAttributes |= nsComponent::INVISIBLE;
00294 
00295     return OK;
00296 }
00297 
00298 int
00299 nsComponent::SetVisible()
00300 {
00301     if (IsInvisible())
00302         mAttributes &= ~nsComponent::INVISIBLE;
00303 
00304     return OK;
00305 }
00306 
00307 int
00308 nsComponent::IsInvisible()
00309 {
00310     if (mAttributes & nsComponent::INVISIBLE)
00311         return TRUE;
00312 
00313     return FALSE;
00314 }
00315 
00316 int
00317 nsComponent::SetLaunchApp()
00318 {
00319     mAttributes |= nsComponent::LAUNCHAPP;
00320 
00321     return OK;
00322 }
00323 
00324 int
00325 nsComponent::SetDontLaunchApp()
00326 {
00327     if (IsLaunchApp())
00328         mAttributes &= ~nsComponent::LAUNCHAPP;
00329 
00330     return OK;
00331 }
00332 
00333 int
00334 nsComponent::IsLaunchApp()
00335 {
00336     if (mAttributes & nsComponent::LAUNCHAPP)
00337         return TRUE;
00338 
00339     return FALSE;
00340 }
00341 
00342 int 
00343 nsComponent::SetDownloadOnly()
00344 {
00345     mAttributes |= nsComponent::DOWNLOAD_ONLY;
00346 
00347     return OK;
00348 }
00349 
00350 int
00351 nsComponent::IsDownloadOnly()
00352 {
00353     if (mAttributes & nsComponent::DOWNLOAD_ONLY)
00354         return TRUE;
00355 
00356     return FALSE;
00357 }
00358 
00359 int
00360 nsComponent::SetIndex(int aIndex)
00361 {
00362     if (aIndex < 0 || aIndex > MAX_COMPONENTS)
00363         return E_OUT_OF_BOUNDS;
00364 
00365     mIndex = aIndex;
00366         
00367     return OK;
00368 }
00369 
00370 int
00371 nsComponent::GetIndex()
00372 {
00373     if (mIndex < 0 || mIndex > MAX_COMPONENTS)
00374         return E_OUT_OF_BOUNDS;
00375     
00376     return mIndex;
00377 }
00378 
00379 int
00380 nsComponent::AddRef()
00381 {
00382     mRefCount++;
00383     
00384     return OK;
00385 }
00386 
00387 int
00388 nsComponent::Release()
00389 {
00390     mRefCount--;
00391 
00392     if (mRefCount < 0)
00393         return E_REF_COUNT;
00394 
00395     if (mRefCount == 0)
00396         delete this;
00397 
00398     return OK;
00399 }
00400 
00401 int
00402 nsComponent::InitRefCount()
00403 {
00404     mRefCount = 1;
00405 
00406     return OK;
00407 }
00408 
00409 int 
00410 nsComponent::DepAddRef()
00411 {
00412     if (mDepRefCount == 0)
00413         SetSelected();
00414 
00415     mDepRefCount++;
00416         
00417     return OK;
00418 }
00419 
00420 int
00421 nsComponent::DepRelease()
00422 {
00423     mDepRefCount--;
00424 
00425     if (mDepRefCount < 0)
00426         mDepRefCount = 0;
00427 
00428     if (mDepRefCount == 0)
00429         SetUnselected();
00430  
00431     return OK;
00432 }
00433 
00434 int
00435 nsComponent::DepGetRefCount()
00436 {
00437     return mDepRefCount;
00438 }
00439 
00440 int
00441 nsComponent::SetResumePos(int aResPos)
00442 {
00443     mResPos = aResPos;
00444 
00445     return OK;
00446 }
00447 
00448 int
00449 nsComponent::GetResumePos()
00450 {
00451     if (mResPos > 0)
00452         return mResPos;
00453 
00454     return 0;
00455 }
00456 
00457 int
00458 nsComponent::SetDownloaded( int which )
00459 {
00460     mDownloaded = which;
00461 
00462     return OK;
00463 }
00464 
00465 int
00466 nsComponent::IsDownloaded()
00467 {
00468     return mDownloaded;
00469 }