* Bring back rbuild build to be used until bug 6372 is fixed.
[reactos.git] / dll / win32 / qmgr / enum_jobs.c
1 /*
2 * Queue Manager (BITS) Job Enumerator
3 *
4 * Copyright 2007 Google (Roy Shea)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "qmgr.h"
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
25
26 static void EnumBackgroundCopyJobsDestructor(EnumBackgroundCopyJobsImpl *This)
27 {
28 ULONG i;
29
30 for(i = 0; i < This->numJobs; i++)
31 IBackgroundCopyJob_Release(This->jobs[i]);
32
33 HeapFree(GetProcessHeap(), 0, This->jobs);
34 HeapFree(GetProcessHeap(), 0, This);
35 }
36
37 static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_AddRef(
38 IEnumBackgroundCopyJobs* iface)
39 {
40 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
41 return InterlockedIncrement(&This->ref);
42 }
43
44 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_QueryInterface(
45 IEnumBackgroundCopyJobs* iface,
46 REFIID riid,
47 void **ppvObject)
48 {
49 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
50 TRACE("IID: %s\n", debugstr_guid(riid));
51
52 if (IsEqualGUID(riid, &IID_IUnknown)
53 || IsEqualGUID(riid, &IID_IEnumBackgroundCopyJobs))
54 {
55 *ppvObject = &This->lpVtbl;
56 BITS_IEnumBackgroundCopyJobs_AddRef(iface);
57 return S_OK;
58 }
59
60 *ppvObject = NULL;
61 return E_NOINTERFACE;
62 }
63
64 static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_Release(
65 IEnumBackgroundCopyJobs* iface)
66 {
67 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
68 ULONG ref = InterlockedDecrement(&This->ref);
69
70 if (ref == 0)
71 EnumBackgroundCopyJobsDestructor(This);
72
73 return ref;
74 }
75
76 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Next(
77 IEnumBackgroundCopyJobs* iface,
78 ULONG celt,
79 IBackgroundCopyJob **rgelt,
80 ULONG *pceltFetched)
81 {
82 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
83 ULONG fetched;
84 ULONG i;
85 IBackgroundCopyJob *job;
86
87 fetched = min(celt, This->numJobs - This->indexJobs);
88 if (pceltFetched)
89 *pceltFetched = fetched;
90 else
91 {
92 /* We need to initialize this array if the caller doesn't request
93 the length because length_is will default to celt. */
94 for (i = 0; i < celt; ++i)
95 rgelt[i] = NULL;
96
97 /* pceltFetched can only be NULL if celt is 1 */
98 if (celt != 1)
99 return E_INVALIDARG;
100 }
101
102 /* Fill in the array of objects */
103 for (i = 0; i < fetched; ++i)
104 {
105 job = This->jobs[This->indexJobs++];
106 IBackgroundCopyJob_AddRef(job);
107 rgelt[i] = job;
108 }
109
110 return fetched == celt ? S_OK : S_FALSE;
111 }
112
113 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Skip(
114 IEnumBackgroundCopyJobs* iface,
115 ULONG celt)
116 {
117 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
118
119 if (This->numJobs - This->indexJobs < celt)
120 {
121 This->indexJobs = This->numJobs;
122 return S_FALSE;
123 }
124
125 This->indexJobs += celt;
126 return S_OK;
127 }
128
129 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Reset(
130 IEnumBackgroundCopyJobs* iface)
131 {
132 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
133 This->indexJobs = 0;
134 return S_OK;
135 }
136
137 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Clone(
138 IEnumBackgroundCopyJobs* iface,
139 IEnumBackgroundCopyJobs **ppenum)
140 {
141 FIXME("Not implemented\n");
142 return E_NOTIMPL;
143 }
144
145 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_GetCount(
146 IEnumBackgroundCopyJobs* iface,
147 ULONG *puCount)
148 {
149 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
150 *puCount = This->numJobs;
151 return S_OK;
152 }
153
154 static const IEnumBackgroundCopyJobsVtbl BITS_IEnumBackgroundCopyJobs_Vtbl =
155 {
156 BITS_IEnumBackgroundCopyJobs_QueryInterface,
157 BITS_IEnumBackgroundCopyJobs_AddRef,
158 BITS_IEnumBackgroundCopyJobs_Release,
159 BITS_IEnumBackgroundCopyJobs_Next,
160 BITS_IEnumBackgroundCopyJobs_Skip,
161 BITS_IEnumBackgroundCopyJobs_Reset,
162 BITS_IEnumBackgroundCopyJobs_Clone,
163 BITS_IEnumBackgroundCopyJobs_GetCount
164 };
165
166 HRESULT EnumBackgroundCopyJobsConstructor(LPVOID *ppObj,
167 IBackgroundCopyManager* copyManager)
168 {
169 BackgroundCopyManagerImpl *qmgr = (BackgroundCopyManagerImpl *) copyManager;
170 EnumBackgroundCopyJobsImpl *This;
171 BackgroundCopyJobImpl *job;
172 ULONG i;
173
174 TRACE("%p, %p)\n", ppObj, copyManager);
175
176 This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
177 if (!This)
178 return E_OUTOFMEMORY;
179 This->lpVtbl = &BITS_IEnumBackgroundCopyJobs_Vtbl;
180 This->ref = 1;
181
182 /* Create array of jobs */
183 This->indexJobs = 0;
184
185 EnterCriticalSection(&qmgr->cs);
186 This->numJobs = list_count(&qmgr->jobs);
187
188 if (0 < This->numJobs)
189 {
190 This->jobs = HeapAlloc(GetProcessHeap(), 0,
191 This->numJobs * sizeof *This->jobs);
192 if (!This->jobs)
193 {
194 LeaveCriticalSection(&qmgr->cs);
195 HeapFree(GetProcessHeap(), 0, This);
196 return E_OUTOFMEMORY;
197 }
198 }
199 else
200 This->jobs = NULL;
201
202 i = 0;
203 LIST_FOR_EACH_ENTRY(job, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
204 {
205 IBackgroundCopyJob *iJob = (IBackgroundCopyJob *) job;
206 IBackgroundCopyJob_AddRef(iJob);
207 This->jobs[i++] = iJob;
208 }
209 LeaveCriticalSection(&qmgr->cs);
210
211 *ppObj = &This->lpVtbl;
212 return S_OK;
213 }