Back to index

lightning-sunbird  0.9+nobinonly
nsComponentList.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 "nsComponentList.h"
00041 #include "nsComponent.h"
00042 
00043 nsComponentList::nsComponentList() :
00044     mHeadItem(NULL),
00045     mTailItem(NULL),
00046     mNextItem(NULL),
00047     mLength(0)
00048 {
00049 }
00050 
00051 nsComponentList::~nsComponentList()
00052 {
00053     nsComponentItem *currItem = mHeadItem;
00054     nsComponentItem *nextItem = NULL;
00055 
00056     while (currItem)
00057     {
00058         nextItem = currItem->mNext;
00059         currItem->mComp->Release();
00060         delete currItem;
00061         currItem = nextItem;
00062     }
00063 
00064     mHeadItem = NULL;
00065     mTailItem = NULL;
00066     mLength = 0;
00067 }
00068 
00069 nsComponent *
00070 nsComponentList::GetHead()
00071 {
00072     if (mHeadItem)
00073     {
00074         mNextItem = mHeadItem->mNext;
00075         return mHeadItem->mComp;
00076     }
00077 
00078     return NULL;
00079 }
00080 
00081 nsComponent *
00082 nsComponentList::GetNext()
00083 {
00084     nsComponentItem *currItem = mNextItem;
00085 
00086     if (mNextItem)
00087     {
00088         mNextItem = mNextItem->mNext;
00089         return currItem->mComp;
00090     }
00091 
00092     return NULL;
00093 }
00094 
00095 nsComponent *
00096 nsComponentList::GetTail()
00097 {
00098     if (mTailItem)
00099         return mTailItem->mComp;
00100 
00101     return NULL;
00102 }
00103 
00104 int
00105 nsComponentList::GetLength()
00106 {
00107     return mLength;
00108 }
00109 
00110 int
00111 nsComponentList::GetLengthVisible()
00112 {
00113     int numVisible = 0;
00114     nsComponentItem *currItem;
00115 
00116     currItem = mHeadItem;
00117     if (!currItem) return 0;
00118 
00119     while (currItem)
00120     {
00121         if (!currItem->mComp->IsInvisible())
00122             numVisible++;
00123         currItem = currItem->mNext;
00124     }
00125         
00126     return numVisible;
00127 }
00128 
00129 int
00130 nsComponentList::GetLengthSelected()
00131 {
00132     int numSelected = 0;
00133     nsComponentItem *currItem;
00134 
00135     currItem = mHeadItem;
00136     if (!currItem) return 0;
00137 
00138     while (currItem)
00139     {
00140         if (currItem->mComp->IsSelected())
00141             numSelected++;
00142         currItem = currItem->mNext;
00143     }
00144         
00145     return numSelected;
00146 }
00147 
00148 int
00149 nsComponentList::AddComponent(nsComponent *aComponent)
00150 {
00151     if (!aComponent)
00152         return E_PARAM;
00153 
00154     aComponent->AddRef();
00155     nsComponentItem *newItem 
00156        = (nsComponentItem *) malloc(sizeof(nsComponentItem));
00157     newItem->mComp = aComponent;
00158     newItem->mNext = NULL;
00159     mLength++;
00160     
00161     if (mHeadItem)
00162     {
00163         // non-empty list: the new comp is tacked on then end
00164         mTailItem->mNext = newItem;
00165     }
00166     else
00167     {
00168         // empty list: head and tail are the new comp
00169         mHeadItem = newItem;
00170     }
00171 
00172     mTailItem = newItem;
00173 
00174     return OK;
00175 }
00176 
00177 int
00178 nsComponentList::RemoveComponent(nsComponent *aComponent)
00179 {
00180     nsComponentItem *currItem = mHeadItem;
00181     nsComponentItem *last = NULL;
00182 
00183     if (!aComponent)
00184         return E_PARAM;
00185 
00186     while (currItem)
00187     {
00188         if (aComponent == currItem->mComp)
00189         {
00190             // remove and link last to next while deleting current
00191             if (last)
00192             {
00193                 last->mNext = currItem->mNext;
00194             }
00195             else
00196             {
00197                 mHeadItem = currItem->mNext;
00198             }
00199 
00200             if (mTailItem == currItem)
00201                 mTailItem = NULL;
00202             if (mNextItem == currItem)
00203                 mNextItem = mNextItem->mNext;
00204 
00205             aComponent->Release();
00206             mLength--;
00207 
00208             return OK;
00209         }
00210         else
00211         {
00212             // move on to next
00213             last = currItem;
00214             currItem = currItem->mNext;
00215         }
00216     }
00217 
00218     return E_PARAM;
00219 }
00220 
00221 nsComponent *
00222 nsComponentList::GetCompByIndex(int aIndex)
00223 {
00224     nsComponentItem *currItem = mHeadItem;
00225     int i;
00226 
00227     // param check
00228     if (!currItem || mLength == 0) return NULL;
00229 
00230     for (i=0; i<mLength; i++)
00231     { 
00232         if (aIndex == currItem->mComp->GetIndex())
00233         {
00234             return currItem->mComp;
00235         }
00236 
00237         currItem = currItem->mNext;
00238     }
00239 
00240     return NULL;
00241 }
00242 
00243 nsComponent *
00244 nsComponentList::GetCompByArchive(char *aArchive)
00245 {
00246     nsComponentItem *currItem = mHeadItem;
00247     int i;
00248 
00249     // param check
00250     if (!currItem || mLength == 0 || !aArchive) return NULL;
00251 
00252     for (i=0; i<mLength; i++)
00253     { 
00254         if (0==strncmp(aArchive, currItem->mComp->GetArchive(), strlen(aArchive)))
00255         {
00256             return currItem->mComp;
00257         }
00258 
00259         currItem = currItem->mNext;
00260         if (!currItem) break;
00261     }
00262 
00263     return NULL;
00264 }
00265 
00266 nsComponent *
00267 nsComponentList::GetCompByShortDesc(char *aShortDesc)
00268 {
00269     nsComponentItem *currItem = mHeadItem;
00270     int i;
00271 
00272     // param check
00273     if (!currItem || mLength == 0 || !aShortDesc) return NULL;
00274 
00275     for (i=0; i<mLength; i++)
00276     { 
00277         if (0==strncmp(aShortDesc, currItem->mComp->GetDescShort(), 
00278                        strlen(aShortDesc)))
00279         {
00280             return currItem->mComp;
00281         }
00282 
00283         currItem = currItem->mNext;
00284         if (!currItem) break;
00285     }
00286 
00287     return NULL;
00288 }
00289 
00290 nsComponent *
00291 nsComponentList::GetFirstVisible()
00292 {
00293     int i;
00294     nsComponentItem *currItem = mHeadItem;
00295 
00296     // param check
00297     if (mLength == 0) return NULL;
00298 
00299     for (i=0; i<mLength; i++)
00300     { 
00301         if (!currItem->mComp->IsInvisible())
00302         {
00303             return currItem->mComp;
00304         }
00305 
00306         currItem = currItem->mNext;
00307         if (!currItem) break;
00308     }
00309 
00310     return NULL;
00311 }