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 the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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     mNext(NULL),
00052     mIndex(-1),
00053     mRefCount(0),
00054     mDepRefCount(0),
00055     mResPos(0),
00056     mDownloaded(FALSE)
00057 {
00058     int i;
00059 
00060     for (i = 0; i < MAX_URLS; i++)
00061         mURL[i] = NULL;
00062     for (i = 0; i < MAX_COMPONENTS; i++)
00063         mDependees[i] = NULL;
00064 }
00065 
00066 nsComponent::~nsComponent()
00067 {
00068     int i;
00069 
00070     XI_IF_FREE(mDescShort);
00071     XI_IF_FREE(mDescLong);
00072     XI_IF_FREE(mArchive);
00073     for (i = 0; i < MAX_URLS; i++)
00074         XI_IF_FREE(mURL[i]);
00075     for (i = 0; i < MAX_COMPONENTS; i++)
00076         XI_IF_FREE(mDependees[i]);
00077 }
00078 
00079 nsComponent *
00080 nsComponent::Duplicate()
00081 {
00082     nsComponent *zdup = new nsComponent();
00083     *zdup = *this;
00084     zdup->InitRefCount();
00085     zdup->InitNext();
00086 
00087     return zdup;
00088 }
00089 
00090 int
00091 nsComponent::SetDescShort(char *aDescShort)
00092 {
00093     if (!aDescShort)
00094         return E_PARAM;
00095 
00096     mDescShort = aDescShort;
00097 
00098     return OK;
00099 }
00100 
00101 char *
00102 nsComponent::GetDescShort()
00103 {
00104     if (mDescShort)
00105         return mDescShort;
00106 
00107     return NULL;
00108 }
00109 
00110 int
00111 nsComponent::SetDescLong(char *aDescLong)
00112 {
00113     if (!aDescLong)
00114         return E_PARAM;
00115 
00116     mDescLong = aDescLong;
00117 
00118     return OK;
00119 }
00120 
00121 char *
00122 nsComponent::GetDescLong()
00123 {
00124     if (mDescLong)
00125         return mDescLong;
00126 
00127     return NULL;
00128 }
00129 
00130 int
00131 nsComponent::SetArchive(char *aArchive)
00132 {
00133     if (!aArchive)
00134         return E_PARAM;
00135 
00136     mArchive = aArchive;
00137 
00138     return OK;
00139 }
00140 
00141 char *
00142 nsComponent::GetArchive()
00143 {
00144     if (mArchive)
00145         return mArchive;
00146 
00147     return NULL;
00148 }
00149 
00150 int
00151 nsComponent::SetInstallSize(int aInstallSize)
00152 {
00153     mInstallSize = aInstallSize;
00154 
00155     return OK;
00156 }
00157 
00158 int
00159 nsComponent::GetInstallSize()
00160 {
00161     if (mInstallSize >= 0)
00162         return mInstallSize;
00163 
00164     return 0;
00165 }
00166 
00167 int
00168 nsComponent::SetArchiveSize(int aArchiveSize)
00169 {
00170     mArchiveSize = aArchiveSize;
00171 
00172     return OK;
00173 }
00174 
00175 int
00176 nsComponent::GetArchiveSize()
00177 {
00178     if (mArchiveSize >= 0)
00179         return mArchiveSize;
00180 
00181     return 0;
00182 }
00183 
00184 int
00185 nsComponent::GetCurrentSize()
00186 {
00187     // assumes cwd is the same as the installer binary location
00188 
00189     char path[MAXPATHLEN];
00190     struct stat stbuf;
00191 
00192     if (!mArchive)
00193         return 0;
00194 
00195     sprintf(path, "./xpi/%s", mArchive);
00196     if (0 != stat(path, &stbuf))
00197         return 0;
00198 
00199     return (stbuf.st_size/1024); // return size in KB
00200 }
00201 
00202 int
00203 nsComponent::SetURL(char *aURL, int aIndex)
00204 {
00205     if (!aURL)
00206         return E_PARAM;
00207     if (mURL[aIndex])
00208         return E_URL_ALREADY;
00209 
00210     mURL[aIndex] = aURL;
00211     
00212     return OK;
00213 }
00214 
00215 char *
00216 nsComponent::GetURL(int aIndex)
00217 {
00218     if (aIndex < 0 || aIndex >= MAX_URLS)
00219         return NULL;
00220 
00221     return mURL[aIndex];
00222 }
00223 
00224 int
00225 nsComponent::AddDependee(char *aDependee)
00226 {
00227     if (!aDependee)
00228         return E_PARAM;
00229 
00230     mDependees[mNextDependeeIdx] = aDependee;
00231     mDependees[++mNextDependeeIdx] = NULL;
00232 
00233     return OK;
00234 }
00235 
00236 int 
00237 nsComponent::ResolveDependees(int aBeingSelected, nsComponentList *aComps)
00238 {
00239     int i;
00240     nsComponent *currComp = NULL;
00241     
00242     // param check
00243     if (!aComps)
00244         return E_PARAM;
00245 
00246     // loop over all dependees
00247     for (i = 0; i < mNextDependeeIdx; i++)
00248     {
00249         if (!mDependees[i])
00250             break;
00251 
00252         currComp = aComps->GetCompByShortDesc(mDependees[i]);
00253         if (!currComp)
00254             continue;
00255         
00256         if (aBeingSelected)
00257             currComp->DepAddRef();
00258         else    
00259             currComp->DepRelease();
00260     }
00261 
00262     return OK;
00263 }
00264 
00265 int
00266 nsComponent::SetSelected()
00267 {
00268     mAttributes |= nsComponent::SELECTED;
00269 
00270     return OK;
00271 }
00272 
00273 int
00274 nsComponent::SetUnselected()
00275 {
00276     if (IsSelected())
00277         mAttributes &= ~nsComponent::SELECTED;
00278     mDepRefCount = 0;
00279 
00280     return OK;
00281 }
00282 
00283 int
00284 nsComponent::IsSelected()
00285 {
00286     if (mAttributes & nsComponent::SELECTED)
00287         return TRUE;
00288 
00289     return FALSE;
00290 }
00291 
00292 int
00293 nsComponent::SetInvisible()
00294 {
00295     mAttributes |= nsComponent::INVISIBLE;
00296 
00297     return OK;
00298 }
00299 
00300 int
00301 nsComponent::SetVisible()
00302 {
00303     if (IsInvisible())
00304         mAttributes &= ~nsComponent::INVISIBLE;
00305 
00306     return OK;
00307 }
00308 
00309 int
00310 nsComponent::IsInvisible()
00311 {
00312     if (mAttributes & nsComponent::INVISIBLE)
00313         return TRUE;
00314 
00315     return FALSE;
00316 }
00317 
00318 int
00319 nsComponent::SetLaunchApp()
00320 {
00321     mAttributes |= nsComponent::LAUNCHAPP;
00322 
00323     return OK;
00324 }
00325 
00326 int
00327 nsComponent::SetDontLaunchApp()
00328 {
00329     if (IsLaunchApp())
00330         mAttributes &= ~nsComponent::LAUNCHAPP;
00331 
00332     return OK;
00333 }
00334 
00335 int
00336 nsComponent::IsLaunchApp()
00337 {
00338     if (mAttributes & nsComponent::LAUNCHAPP)
00339         return TRUE;
00340 
00341     return FALSE;
00342 }
00343 
00344 int 
00345 nsComponent::SetDownloadOnly()
00346 {
00347     mAttributes |= nsComponent::DOWNLOAD_ONLY;
00348 
00349     return OK;
00350 }
00351 
00352 int
00353 nsComponent::IsDownloadOnly()
00354 {
00355     if (mAttributes & nsComponent::DOWNLOAD_ONLY)
00356         return TRUE;
00357 
00358     return FALSE;
00359 }
00360 
00361 int
00362 nsComponent::SetMainComponent()
00363 {
00364     mAttributes |= nsComponent::MAIN_COMPONENT;
00365 
00366     return OK;
00367 }
00368 
00369 int
00370 nsComponent::IsMainComponent()
00371 {
00372     if (mAttributes & nsComponent::MAIN_COMPONENT)
00373         return TRUE;
00374 
00375     return FALSE;
00376 }
00377 
00378 int
00379 nsComponent::SetNext(nsComponent *aComponent)
00380 {
00381     if (!aComponent)
00382         return E_PARAM;
00383 
00384     mNext = aComponent;
00385     
00386     return OK;
00387 }
00388 
00389 int
00390 nsComponent::InitNext()
00391 {
00392     mNext = NULL;
00393 
00394     return OK;
00395 }
00396 
00397 nsComponent *
00398 nsComponent::GetNext()
00399 {
00400     if (mNext)
00401         return mNext;
00402 
00403     return NULL;
00404 }
00405 
00406 int
00407 nsComponent::SetIndex(int aIndex)
00408 {
00409     if (aIndex < 0 || aIndex > MAX_COMPONENTS)
00410         return E_OUT_OF_BOUNDS;
00411 
00412     mIndex = aIndex;
00413         
00414     return OK;
00415 }
00416 
00417 int
00418 nsComponent::GetIndex()
00419 {
00420     if (mIndex < 0 || mIndex > MAX_COMPONENTS)
00421         return E_OUT_OF_BOUNDS;
00422     
00423     return mIndex;
00424 }
00425 
00426 int
00427 nsComponent::AddRef()
00428 {
00429     mRefCount++;
00430     
00431     return OK;
00432 }
00433 
00434 int
00435 nsComponent::Release()
00436 {
00437     mRefCount--;
00438 
00439     if (mRefCount < 0)
00440         return E_REF_COUNT;
00441 
00442     if (mRefCount == 0)
00443         delete this;
00444 
00445     return OK;
00446 }
00447 
00448 int
00449 nsComponent::InitRefCount()
00450 {
00451     mRefCount = 1;
00452 
00453     return OK;
00454 }
00455 
00456 int 
00457 nsComponent::DepAddRef()
00458 {
00459     if (mDepRefCount == 0)
00460         SetSelected();
00461 
00462     mDepRefCount++;
00463         
00464     return OK;
00465 }
00466 
00467 int
00468 nsComponent::DepRelease()
00469 {
00470     mDepRefCount--;
00471 
00472     if (mDepRefCount < 0)
00473         mDepRefCount = 0;
00474 
00475     if (mDepRefCount == 0)
00476         SetUnselected();
00477  
00478     return OK;
00479 }
00480 
00481 int
00482 nsComponent::DepGetRefCount()
00483 {
00484     return mDepRefCount;
00485 }
00486 
00487 int
00488 nsComponent::SetResumePos(int aResPos)
00489 {
00490     mResPos = aResPos;
00491 
00492     return OK;
00493 }
00494 
00495 int
00496 nsComponent::GetResumePos()
00497 {
00498     if (mResPos > 0)
00499         return mResPos;
00500 
00501     return 0;
00502 }
00503 
00504 int
00505 nsComponent::SetDownloaded( int which )
00506 {
00507     mDownloaded = which;
00508 
00509     return OK;
00510 }
00511 
00512 int
00513 nsComponent::IsDownloaded()
00514 {
00515     return mDownloaded;
00516 }