8b01fd301de4d865d94bdf8c9ad3bde11e09646f
[reactos.git] / reactos / dll / win32 / qmgr / enum_files.c
1 /*
2 * Queue Manager (BITS) File Enumerator
3 *
4 * Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
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 IEnumBackgroundCopyFiles IEnumBackgroundCopyFiles_iface;
26 LONG ref;
27 IBackgroundCopyFile **files;
28 ULONG numFiles;
29 ULONG indexFiles;
30 } EnumBackgroundCopyFilesImpl;
31
32 static inline EnumBackgroundCopyFilesImpl *impl_from_IEnumBackgroundCopyFiles(IEnumBackgroundCopyFiles *iface)
33 {
34 return CONTAINING_RECORD(iface, EnumBackgroundCopyFilesImpl, IEnumBackgroundCopyFiles_iface);
35 }
36
37 static HRESULT WINAPI BITS_IEnumBackgroundCopyFiles_QueryInterface(IEnumBackgroundCopyFiles *iface,
38 REFIID riid, void **ppv)
39 {
40 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppv);
41
42 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IEnumBackgroundCopyFiles))
43 {
44 *ppv = iface;
45 IEnumBackgroundCopyFiles_AddRef(iface);
46 return S_OK;
47 }
48
49 *ppv = NULL;
50 return E_NOINTERFACE;
51 }
52
53 static ULONG WINAPI BITS_IEnumBackgroundCopyFiles_AddRef(IEnumBackgroundCopyFiles *iface)
54 {
55 EnumBackgroundCopyFilesImpl *This = impl_from_IEnumBackgroundCopyFiles(iface);
56 ULONG ref = InterlockedIncrement(&This->ref);
57
58 TRACE("(%p) ref=%d\n", This, ref);
59 return ref;
60 }
61
62 static ULONG WINAPI BITS_IEnumBackgroundCopyFiles_Release(IEnumBackgroundCopyFiles *iface)
63 {
64 EnumBackgroundCopyFilesImpl *This = impl_from_IEnumBackgroundCopyFiles(iface);
65 ULONG ref = InterlockedDecrement(&This->ref);
66 ULONG i;
67
68 if (ref == 0)
69 {
70 for(i = 0; i < This->numFiles; i++)
71 IBackgroundCopyFile_Release(This->files[i]);
72 HeapFree(GetProcessHeap(), 0, This->files);
73 HeapFree(GetProcessHeap(), 0, This);
74 }
75
76 return ref;
77 }
78
79 /* Return reference to one or more files in the file enumerator */
80 static HRESULT WINAPI BITS_IEnumBackgroundCopyFiles_Next(IEnumBackgroundCopyFiles *iface,
81 ULONG celt, IBackgroundCopyFile **rgelt, ULONG *pceltFetched)
82 {
83 EnumBackgroundCopyFilesImpl *This = impl_from_IEnumBackgroundCopyFiles(iface);
84 ULONG fetched;
85 ULONG i;
86 IBackgroundCopyFile *file;
87
88 /* Despite documented behavior, Windows (tested on XP) is not verifying
89 that the caller set pceltFetched to zero. No check here. */
90
91 fetched = min(celt, This->numFiles - This->indexFiles);
92 if (pceltFetched)
93 *pceltFetched = fetched;
94 else
95 {
96 /* We need to initialize this array if the caller doesn't request
97 the length because length_is will default to celt. */
98 for (i = 0; i < celt; i++)
99 rgelt[i] = NULL;
100
101 /* pceltFetched can only be NULL if celt is 1 */
102 if (celt != 1)
103 return E_INVALIDARG;
104 }
105
106 /* Fill in the array of objects */
107 for (i = 0; i < fetched; i++)
108 {
109 file = This->files[This->indexFiles++];
110 IBackgroundCopyFile_AddRef(file);
111 rgelt[i] = file;
112 }
113
114 return fetched == celt ? S_OK : S_FALSE;
115 }
116
117 /* Skip over one or more files in the file enumerator */
118 static HRESULT WINAPI BITS_IEnumBackgroundCopyFiles_Skip(IEnumBackgroundCopyFiles *iface,
119 ULONG celt)
120 {
121 EnumBackgroundCopyFilesImpl *This = impl_from_IEnumBackgroundCopyFiles(iface);
122
123 if (celt > This->numFiles - This->indexFiles)
124 {
125 This->indexFiles = This->numFiles;
126 return S_FALSE;
127 }
128
129 This->indexFiles += celt;
130 return S_OK;
131 }
132
133 static HRESULT WINAPI BITS_IEnumBackgroundCopyFiles_Reset(IEnumBackgroundCopyFiles *iface)
134 {
135 EnumBackgroundCopyFilesImpl *This = impl_from_IEnumBackgroundCopyFiles(iface);
136 This->indexFiles = 0;
137 return S_OK;
138 }
139
140 static HRESULT WINAPI BITS_IEnumBackgroundCopyFiles_Clone(IEnumBackgroundCopyFiles *iface,
141 IEnumBackgroundCopyFiles **ppenum)
142 {
143 FIXME("Not implemented\n");
144 return E_NOTIMPL;
145 }
146
147 static HRESULT WINAPI BITS_IEnumBackgroundCopyFiles_GetCount(IEnumBackgroundCopyFiles *iface,
148 ULONG *puCount)
149 {
150 EnumBackgroundCopyFilesImpl *This = impl_from_IEnumBackgroundCopyFiles(iface);
151 *puCount = This->numFiles;
152 return S_OK;
153 }
154
155 static const IEnumBackgroundCopyFilesVtbl BITS_IEnumBackgroundCopyFiles_Vtbl =
156 {
157 BITS_IEnumBackgroundCopyFiles_QueryInterface,
158 BITS_IEnumBackgroundCopyFiles_AddRef,
159 BITS_IEnumBackgroundCopyFiles_Release,
160 BITS_IEnumBackgroundCopyFiles_Next,
161 BITS_IEnumBackgroundCopyFiles_Skip,
162 BITS_IEnumBackgroundCopyFiles_Reset,
163 BITS_IEnumBackgroundCopyFiles_Clone,
164 BITS_IEnumBackgroundCopyFiles_GetCount
165 };
166
167 HRESULT EnumBackgroundCopyFilesConstructor(BackgroundCopyJobImpl *job, IEnumBackgroundCopyFiles **enum_files)
168 {
169 EnumBackgroundCopyFilesImpl *This;
170 BackgroundCopyFileImpl *file;
171 ULONG i;
172
173 TRACE("%p, %p)\n", job, enum_files);
174
175 This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
176 if (!This)
177 return E_OUTOFMEMORY;
178
179 This->IEnumBackgroundCopyFiles_iface.lpVtbl = &BITS_IEnumBackgroundCopyFiles_Vtbl;
180 This->ref = 1;
181
182 /* Create array of files */
183 This->indexFiles = 0;
184 EnterCriticalSection(&job->cs);
185 This->numFiles = list_count(&job->files);
186 This->files = NULL;
187 if (This->numFiles > 0)
188 {
189 This->files = HeapAlloc(GetProcessHeap(), 0,
190 This->numFiles * sizeof This->files[0]);
191 if (!This->files)
192 {
193 LeaveCriticalSection(&job->cs);
194 HeapFree(GetProcessHeap(), 0, This);
195 return E_OUTOFMEMORY;
196 }
197 }
198
199 i = 0;
200 LIST_FOR_EACH_ENTRY(file, &job->files, BackgroundCopyFileImpl, entryFromJob)
201 {
202 IBackgroundCopyFile_AddRef(&file->IBackgroundCopyFile_iface);
203 This->files[i] = &file->IBackgroundCopyFile_iface;
204 ++i;
205 }
206 LeaveCriticalSection(&job->cs);
207
208 *enum_files = &This->IEnumBackgroundCopyFiles_iface;
209 return S_OK;
210 }