Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / widget / src / xpwidgets / nsIdleService.cpp
blobeb47ce36b58c50b60bb724ba99d529fa5c0c63cf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
3 */
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is mozilla.org code.
19 * The Initial Developer of the Original Code is
20 * Gijs Kruitbosch <gijskruitbosch@gmail.com>.
21 * Portions created by the Initial Developer are Copyright (C) 2007
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Gijs Kruitbosch <gijskruitbosch@gmail.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsIdleService.h"
42 #include "nsString.h"
43 #include "nsIServiceManager.h"
44 #include "nsDebug.h"
45 #include "nsCOMArray.h"
47 // observer topics used:
48 #define OBSERVER_TOPIC_IDLE "idle"
49 #define OBSERVER_TOPIC_BACK "back"
50 // interval in milliseconds between internal idle time requests
51 #define IDLE_POLL_INTERVAL 5000
55 // Use this to find previously added observers in our array:
56 class IdleListenerComparator
58 public:
59 PRBool Equals(IdleListener a, IdleListener b) const
61 return (a.observer == b.observer) &&
62 (a.reqIdleTime == b.reqIdleTime);
66 nsIdleService::nsIdleService()
70 nsIdleService::~nsIdleService()
72 if (mTimer)
73 mTimer->Cancel();
76 NS_IMETHODIMP
77 nsIdleService::AddIdleObserver(nsIObserver* aObserver, PRUint32 aIdleTime)
79 NS_ENSURE_ARG_POINTER(aObserver);
80 NS_ENSURE_ARG(aIdleTime);
82 // Put the time + observer in a struct we can keep:
83 IdleListener listener(aObserver, aIdleTime);
85 if (!mArrayListeners.AppendElement(listener))
86 return NS_ERROR_OUT_OF_MEMORY;
88 // Initialize our timer callback if it's not there already.
89 if (!mTimer)
91 nsresult rv;
92 mTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
93 if (NS_FAILED(rv))
94 return rv;
95 mTimer->InitWithFuncCallback(IdleTimerCallback, this, IDLE_POLL_INTERVAL,
96 nsITimer::TYPE_REPEATING_SLACK);
99 // Make sure our observer goes into 'idle' immediately if applicable.
100 CheckAwayState();
102 return NS_OK;
105 NS_IMETHODIMP
106 nsIdleService::RemoveIdleObserver(nsIObserver* aObserver, PRUint32 aTime)
108 NS_ENSURE_ARG_POINTER(aObserver);
109 NS_ENSURE_ARG(aTime);
110 IdleListener listener(aObserver, aTime);
112 // Find the entry and remove it:
113 IdleListenerComparator c;
114 if (mArrayListeners.RemoveElement(listener, c))
116 if (mTimer && mArrayListeners.IsEmpty())
118 mTimer->Cancel();
119 mTimer = nsnull;
121 return NS_OK;
124 // If we get here, we haven't removed anything:
125 return NS_ERROR_FAILURE;
128 void
129 nsIdleService::IdleTimerCallback(nsITimer* aTimer, void* aClosure)
131 static_cast<nsIdleService*>(aClosure)->CheckAwayState();
134 void
135 nsIdleService::CheckAwayState()
137 // Get the idle time.
138 PRUint32 idleTime;
139 if (NS_FAILED(GetIdleTime(&idleTime)))
140 return;
142 nsAutoString timeStr;
143 timeStr.AppendInt(idleTime);
145 // Change state first, and save observers that need notification, so
146 // removing things will always work without upsetting notifications.
147 nsCOMArray<nsIObserver> idleListeners;
148 nsCOMArray<nsIObserver> hereListeners;
149 for (PRUint32 i = 0; i < mArrayListeners.Length(); i++)
151 IdleListener& curListener = mArrayListeners.ElementAt(i);
152 if ((curListener.reqIdleTime * 1000 <= idleTime) &&
153 !curListener.isIdle)
155 curListener.isIdle = PR_TRUE;
156 idleListeners.AppendObject(curListener.observer);
158 else if ((curListener.reqIdleTime * 1000 > idleTime) &&
159 curListener.isIdle)
161 curListener.isIdle = PR_FALSE;
162 hereListeners.AppendObject(curListener.observer);
166 // Notify listeners gone idle:
167 for (PRInt32 i = 0; i < idleListeners.Count(); i++)
169 idleListeners[i]->Observe(this, OBSERVER_TOPIC_IDLE, timeStr.get());
172 // Notify listeners that came back:
173 for (PRInt32 i = 0; i < hereListeners.Count(); i++)
175 hereListeners[i]->Observe(this, OBSERVER_TOPIC_BACK, timeStr.get());