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