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