Back to index

lightning-sunbird  0.9+nobinonly
nsAppShell.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  *  Zack Rusin <zack@kde.org>.
00020  * Portions created by the Initial Developer are Copyright (C) 2004
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Lars Knoll <knoll@kde.org>
00025  *   Zack Rusin <zack@kde.org>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 #include "nsAppShell.h"
00041 
00042 #include "nsEventQueueWatcher.h"
00043 
00044 #include "nsIEventQueueService.h"
00045 #include "nsServiceManagerUtils.h"
00046 #include "nsIEventQueue.h"
00047 
00048 #include <qapplication.h>
00049 
00050 nsAppShell::nsAppShell()
00051 {
00052 }
00053 
00054 nsAppShell::~nsAppShell()
00055 {
00056 }
00057 
00058 //-------------------------------------------------------------------------
00059 // nsISupports implementation macro
00060 //-------------------------------------------------------------------------
00061 NS_IMPL_ISUPPORTS1(nsAppShell, nsIAppShell)
00062 
00063 NS_IMETHODIMP
00064 nsAppShell::Create(int *argc, char **argv)
00065 {
00066     return NS_OK;
00067 }
00068 
00069 NS_IMETHODIMP
00070 nsAppShell::Run(void)
00071 {
00072     if (!mEventQueue)
00073         Spinup();
00074 
00075     if (!mEventQueue)
00076         return NS_ERROR_NOT_INITIALIZED;
00077 
00078     qApp->exec();
00079 
00080     Spindown();
00081 
00082     return NS_OK;
00083 }
00084 
00085 //i don't like this method flow, but i left it because it follows what
00086 //other ports are doing here
00087 NS_IMETHODIMP
00088 nsAppShell::Spinup()
00089 {
00090     nsresult rv = NS_OK;
00091 
00092     // Get the event queue service
00093     nsCOMPtr<nsIEventQueueService> eventQService =
00094         do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID,&rv);
00095 
00096     if (NS_FAILED(rv)) {
00097         NS_WARNING("Could not obtain event queue service");
00098         return rv;
00099     }
00100 
00101     //Get the event queue for the thread.
00102     rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD,
00103                                             getter_AddRefs(mEventQueue));
00104 
00105     // If a queue already present use it.
00106     if (mEventQueue)
00107         goto done;
00108 
00109     // Create the event queue for the thread
00110     rv = eventQService->CreateThreadEventQueue();
00111     if (NS_FAILED(rv)) {
00112         NS_WARNING("Could not create the thread event queue");
00113         return rv;
00114     }
00115 
00116     // Ask again for the event queue now that we have create one.
00117     rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD,getter_AddRefs(mEventQueue));
00118     if (NS_FAILED(rv)) {
00119         NS_ASSERTION("Could not obtain the thread event queue",PR_FALSE);
00120         return rv;
00121     }
00122 done:
00123     AddEventQueue(mEventQueue);
00124 
00125     return NS_OK;
00126 }
00127 
00128 NS_IMETHODIMP
00129 nsAppShell::Spindown(void)
00130 {
00131     // stop listening to the event queue
00132     if (mEventQueue) {
00133         RemoveEventQueue(mEventQueue);
00134         mEventQueue->ProcessPendingEvents();
00135         mEventQueue = nsnull;
00136     }
00137 
00138     return NS_OK;
00139 }
00140 
00141 NS_IMETHODIMP
00142 nsAppShell::ListenToEventQueue(nsIEventQueue *aQueue, PRBool aListen)
00143 {
00144     // whoever came up with the idea of passing bool to this
00145     // method to decide what its effect should be is getting
00146     // his ass kicked
00147 
00148     if (aListen)
00149         AddEventQueue(aQueue);
00150     else
00151         RemoveEventQueue(aQueue);
00152 
00153     return NS_OK;
00154 }
00155 
00156 void
00157 nsAppShell::AddEventQueue(nsIEventQueue *aQueue)
00158 {
00159     nsEventQueueWatcher *que = 0;
00160 
00161     if ((que = mQueueDict.find(aQueue->GetEventQueueSelectFD()))) {
00162         que->ref();
00163     }
00164     else {
00165         mQueueDict.insert(aQueue->GetEventQueueSelectFD(),
00166                           new nsEventQueueWatcher(aQueue, qApp));
00167     }
00168 }
00169 
00170 void
00171 nsAppShell::RemoveEventQueue(nsIEventQueue *aQueue)
00172 {
00173     nsEventQueueWatcher *qtQueue = 0;
00174 
00175     if ((qtQueue = mQueueDict.find(aQueue->GetEventQueueSelectFD()))) {
00176         qtQueue->DataReceived();
00177         qtQueue->deref();
00178         if (qtQueue->count <= 0) {
00179             mQueueDict.take(aQueue->GetEventQueueSelectFD());
00180             delete qtQueue;
00181         }
00182     }
00183 }
00184 
00185 NS_IMETHODIMP
00186 nsAppShell::GetNativeEvent(PRBool &aRealEvent, void * &aEvent)
00187 {
00188     aRealEvent = PR_FALSE;
00189     aEvent = 0;
00190 
00191     return NS_OK;
00192 }
00193 
00194 NS_IMETHODIMP
00195 nsAppShell::DispatchNativeEvent(PRBool aRealEvent, void *aEvent)
00196 {
00197     if (!mEventQueue)
00198         return NS_ERROR_NOT_INITIALIZED;
00199 
00200     qApp->processEvents();
00201 
00202     return NS_OK;
00203 }
00204 
00205 NS_IMETHODIMP
00206 nsAppShell::Exit(void)
00207 {
00208     qApp->exit(0);
00209     return NS_OK;
00210 }