msvcrt: Add __swprintf_l.
[wine.git] / dlls / qmgr / enum_jobs.c
blobf6dd40c772d42ecf1c2ed93bd0e3ec5f13c2d7df
1 /*
2 * Queue Manager (BITS) Job Enumerator
4 * Copyright 2007 Google (Roy Shea)
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.
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.
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
21 #include "qmgr.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
26 typedef struct
28 IEnumBackgroundCopyJobs IEnumBackgroundCopyJobs_iface;
29 LONG ref;
30 IBackgroundCopyJob **jobs;
31 ULONG numJobs;
32 ULONG indexJobs;
33 } EnumBackgroundCopyJobsImpl;
35 static inline EnumBackgroundCopyJobsImpl *impl_from_IEnumBackgroundCopyJobs(IEnumBackgroundCopyJobs *iface)
37 return CONTAINING_RECORD(iface, EnumBackgroundCopyJobsImpl, IEnumBackgroundCopyJobs_iface);
40 static HRESULT WINAPI EnumBackgroundCopyJobs_QueryInterface(IEnumBackgroundCopyJobs *iface,
41 REFIID riid, void **ppv)
43 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
45 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
47 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IEnumBackgroundCopyJobs))
49 *ppv = iface;
50 IEnumBackgroundCopyJobs_AddRef(iface);
51 return S_OK;
54 *ppv = NULL;
55 return E_NOINTERFACE;
58 static ULONG WINAPI EnumBackgroundCopyJobs_AddRef(IEnumBackgroundCopyJobs *iface)
60 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
61 ULONG ref = InterlockedIncrement(&This->ref);
63 TRACE("(%p)->(%d)\n", This, ref);
65 return ref;
68 static ULONG WINAPI EnumBackgroundCopyJobs_Release(IEnumBackgroundCopyJobs *iface)
70 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
71 ULONG ref = InterlockedDecrement(&This->ref);
72 ULONG i;
74 TRACE("(%p)->(%d)\n", This, ref);
76 if (ref == 0) {
77 for(i = 0; i < This->numJobs; i++)
78 IBackgroundCopyJob_Release(This->jobs[i]);
79 HeapFree(GetProcessHeap(), 0, This->jobs);
80 HeapFree(GetProcessHeap(), 0, This);
83 return ref;
86 static HRESULT WINAPI EnumBackgroundCopyJobs_Next(IEnumBackgroundCopyJobs *iface, ULONG celt,
87 IBackgroundCopyJob **rgelt, ULONG *pceltFetched)
89 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
90 ULONG fetched;
91 ULONG i;
92 IBackgroundCopyJob *job;
94 TRACE("(%p)->(%d %p %p)\n", This, celt, rgelt, pceltFetched);
96 fetched = min(celt, This->numJobs - This->indexJobs);
97 if (pceltFetched)
98 *pceltFetched = fetched;
99 else
101 /* We need to initialize this array if the caller doesn't request
102 the length because length_is will default to celt. */
103 for (i = 0; i < celt; ++i)
104 rgelt[i] = NULL;
106 /* pceltFetched can only be NULL if celt is 1 */
107 if (celt != 1)
108 return E_INVALIDARG;
111 /* Fill in the array of objects */
112 for (i = 0; i < fetched; ++i)
114 job = This->jobs[This->indexJobs++];
115 IBackgroundCopyJob_AddRef(job);
116 rgelt[i] = job;
119 return fetched == celt ? S_OK : S_FALSE;
122 static HRESULT WINAPI EnumBackgroundCopyJobs_Skip(IEnumBackgroundCopyJobs *iface, ULONG celt)
124 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
126 TRACE("(%p)->(%d)\n", This, celt);
128 if (This->numJobs - This->indexJobs < celt)
130 This->indexJobs = This->numJobs;
131 return S_FALSE;
134 This->indexJobs += celt;
135 return S_OK;
138 static HRESULT WINAPI EnumBackgroundCopyJobs_Reset(IEnumBackgroundCopyJobs *iface)
140 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
142 TRACE("(%p)\n", This);
144 This->indexJobs = 0;
145 return S_OK;
148 static HRESULT WINAPI EnumBackgroundCopyJobs_Clone(IEnumBackgroundCopyJobs *iface,
149 IEnumBackgroundCopyJobs **ppenum)
151 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
152 FIXME("(%p)->(%p): stub\n", This, ppenum);
153 return E_NOTIMPL;
156 static HRESULT WINAPI EnumBackgroundCopyJobs_GetCount(IEnumBackgroundCopyJobs *iface,
157 ULONG *puCount)
159 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
161 TRACE("(%p)->(%p)\n", This, puCount);
163 *puCount = This->numJobs;
164 return S_OK;
167 static const IEnumBackgroundCopyJobsVtbl EnumBackgroundCopyJobsVtbl =
169 EnumBackgroundCopyJobs_QueryInterface,
170 EnumBackgroundCopyJobs_AddRef,
171 EnumBackgroundCopyJobs_Release,
172 EnumBackgroundCopyJobs_Next,
173 EnumBackgroundCopyJobs_Skip,
174 EnumBackgroundCopyJobs_Reset,
175 EnumBackgroundCopyJobs_Clone,
176 EnumBackgroundCopyJobs_GetCount
179 HRESULT enum_copy_job_create(BackgroundCopyManagerImpl *qmgr, IEnumBackgroundCopyJobs **enumjob)
181 EnumBackgroundCopyJobsImpl *This;
182 BackgroundCopyJobImpl *job;
183 ULONG i;
185 TRACE("%p, %p)\n", qmgr, enumjob);
187 This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
188 if (!This)
189 return E_OUTOFMEMORY;
190 This->IEnumBackgroundCopyJobs_iface.lpVtbl = &EnumBackgroundCopyJobsVtbl;
191 This->ref = 1;
193 /* Create array of jobs */
194 This->indexJobs = 0;
196 EnterCriticalSection(&qmgr->cs);
197 This->numJobs = list_count(&qmgr->jobs);
199 if (0 < This->numJobs)
201 This->jobs = HeapAlloc(GetProcessHeap(), 0,
202 This->numJobs * sizeof *This->jobs);
203 if (!This->jobs)
205 LeaveCriticalSection(&qmgr->cs);
206 HeapFree(GetProcessHeap(), 0, This);
207 return E_OUTOFMEMORY;
210 else
211 This->jobs = NULL;
213 i = 0;
214 LIST_FOR_EACH_ENTRY(job, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
216 IBackgroundCopyJob *job_iface = (IBackgroundCopyJob*)&job->IBackgroundCopyJob2_iface;
217 IBackgroundCopyJob_AddRef(job_iface);
218 This->jobs[i++] = job_iface;
220 LeaveCriticalSection(&qmgr->cs);
222 *enumjob = &This->IEnumBackgroundCopyJobs_iface;
223 return S_OK;