c942547fb90ed5eab32c6de58844601bd4bf17cf
[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
23 typedef struct
24 {
25 IEnumBackgroundCopyJobs IEnumBackgroundCopyJobs_iface;
26 LONG ref;
27 IBackgroundCopyJob3 **jobs;
28 ULONG numJobs;
29 ULONG indexJobs;
30 } EnumBackgroundCopyJobsImpl;
31
32 static inline EnumBackgroundCopyJobsImpl *impl_from_IEnumBackgroundCopyJobs(IEnumBackgroundCopyJobs *iface)
33 {
34 return CONTAINING_RECORD(iface, EnumBackgroundCopyJobsImpl, IEnumBackgroundCopyJobs_iface);
35 }
36
37 static HRESULT WINAPI EnumBackgroundCopyJobs_QueryInterface(IEnumBackgroundCopyJobs *iface,
38 REFIID riid, void **ppv)
39 {
40 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
41
42 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
43
44 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IEnumBackgroundCopyJobs))
45 {
46 *ppv = iface;
47 IEnumBackgroundCopyJobs_AddRef(iface);
48 return S_OK;
49 }
50
51 *ppv = NULL;
52 return E_NOINTERFACE;
53 }
54
55 static ULONG WINAPI EnumBackgroundCopyJobs_AddRef(IEnumBackgroundCopyJobs *iface)
56 {
57 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
58 ULONG ref = InterlockedIncrement(&This->ref);
59
60 TRACE("(%p)->(%d)\n", This, ref);
61
62 return ref;
63 }
64
65 static ULONG WINAPI EnumBackgroundCopyJobs_Release(IEnumBackgroundCopyJobs *iface)
66 {
67 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
68 ULONG ref = InterlockedDecrement(&This->ref);
69 ULONG i;
70
71 TRACE("(%p)->(%d)\n", This, ref);
72
73 if (ref == 0) {
74 for(i = 0; i < This->numJobs; i++)
75 IBackgroundCopyJob3_Release(This->jobs[i]);
76 HeapFree(GetProcessHeap(), 0, This->jobs);
77 HeapFree(GetProcessHeap(), 0, This);
78 }
79
80 return ref;
81 }
82
83 static HRESULT WINAPI EnumBackgroundCopyJobs_Next(IEnumBackgroundCopyJobs *iface, ULONG celt,
84 IBackgroundCopyJob **rgelt, ULONG *pceltFetched)
85 {
86 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
87 ULONG fetched;
88 ULONG i;
89 IBackgroundCopyJob3 *job;
90
91 TRACE("(%p)->(%d %p %p)\n", This, celt, rgelt, pceltFetched);
92
93 fetched = min(celt, This->numJobs - This->indexJobs);
94 if (pceltFetched)
95 *pceltFetched = fetched;
96 else
97 {
98 /* We need to initialize this array if the caller doesn't request
99 the length because length_is will default to celt. */
100 for (i = 0; i < celt; ++i)
101 rgelt[i] = NULL;
102
103 /* pceltFetched can only be NULL if celt is 1 */
104 if (celt != 1)
105 return E_INVALIDARG;
106 }
107
108 /* Fill in the array of objects */
109 for (i = 0; i < fetched; ++i)
110 {
111 job = This->jobs[This->indexJobs++];
112 IBackgroundCopyJob3_AddRef(job);
113 rgelt[i] = (IBackgroundCopyJob *)job;
114 }
115
116 return fetched == celt ? S_OK : S_FALSE;
117 }
118
119 static HRESULT WINAPI EnumBackgroundCopyJobs_Skip(IEnumBackgroundCopyJobs *iface, ULONG celt)
120 {
121 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
122
123 TRACE("(%p)->(%d)\n", This, celt);
124
125 if (This->numJobs - This->indexJobs < celt)
126 {
127 This->indexJobs = This->numJobs;
128 return S_FALSE;
129 }
130
131 This->indexJobs += celt;
132 return S_OK;
133 }
134
135 static HRESULT WINAPI EnumBackgroundCopyJobs_Reset(IEnumBackgroundCopyJobs *iface)
136 {
137 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
138
139 TRACE("(%p)\n", This);
140
141 This->indexJobs = 0;
142 return S_OK;
143 }
144
145 static HRESULT WINAPI EnumBackgroundCopyJobs_Clone(IEnumBackgroundCopyJobs *iface,
146 IEnumBackgroundCopyJobs **ppenum)
147 {
148 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
149 FIXME("(%p)->(%p): stub\n", This, ppenum);
150 return E_NOTIMPL;
151 }
152
153 static HRESULT WINAPI EnumBackgroundCopyJobs_GetCount(IEnumBackgroundCopyJobs *iface,
154 ULONG *puCount)
155 {
156 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
157
158 TRACE("(%p)->(%p)\n", This, puCount);
159
160 *puCount = This->numJobs;
161 return S_OK;
162 }
163
164 static const IEnumBackgroundCopyJobsVtbl EnumBackgroundCopyJobsVtbl =
165 {
166 EnumBackgroundCopyJobs_QueryInterface,
167 EnumBackgroundCopyJobs_AddRef,
168 EnumBackgroundCopyJobs_Release,
169 EnumBackgroundCopyJobs_Next,
170 EnumBackgroundCopyJobs_Skip,
171 EnumBackgroundCopyJobs_Reset,
172 EnumBackgroundCopyJobs_Clone,
173 EnumBackgroundCopyJobs_GetCount
174 };
175
176 HRESULT enum_copy_job_create(BackgroundCopyManagerImpl *qmgr, IEnumBackgroundCopyJobs **enumjob)
177 {
178 EnumBackgroundCopyJobsImpl *This;
179 BackgroundCopyJobImpl *job;
180 ULONG i;
181
182 TRACE("%p, %p)\n", qmgr, enumjob);
183
184 This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
185 if (!This)
186 return E_OUTOFMEMORY;
187 This->IEnumBackgroundCopyJobs_iface.lpVtbl = &EnumBackgroundCopyJobsVtbl;
188 This->ref = 1;
189
190 /* Create array of jobs */
191 This->indexJobs = 0;
192
193 EnterCriticalSection(&qmgr->cs);
194 This->numJobs = list_count(&qmgr->jobs);
195
196 if (0 < This->numJobs)
197 {
198 This->jobs = HeapAlloc(GetProcessHeap(), 0,
199 This->numJobs * sizeof *This->jobs);
200 if (!This->jobs)
201 {
202 LeaveCriticalSection(&qmgr->cs);
203 HeapFree(GetProcessHeap(), 0, This);
204 return E_OUTOFMEMORY;
205 }
206 }
207 else
208 This->jobs = NULL;
209
210 i = 0;
211 LIST_FOR_EACH_ENTRY(job, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
212 {
213 IBackgroundCopyJob3_AddRef(&job->IBackgroundCopyJob3_iface);
214 This->jobs[i++] = &job->IBackgroundCopyJob3_iface;
215 }
216 LeaveCriticalSection(&qmgr->cs);
217
218 *enumjob = &This->IEnumBackgroundCopyJobs_iface;
219 return S_OK;
220 }