jscript: Added Dll[Un]RegisterServer implementation.
[wine] / dlls / qmgr / file.c
1 /*
2  * Queue Manager (BITS) File
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 BackgroundCopyFileDestructor(BackgroundCopyFileImpl *This)
27 {
28     HeapFree(GetProcessHeap(), 0, This->info.LocalName);
29     HeapFree(GetProcessHeap(), 0, This->info.RemoteName);
30     HeapFree(GetProcessHeap(), 0, This);
31 }
32
33 static ULONG WINAPI BITS_IBackgroundCopyFile_AddRef(IBackgroundCopyFile* iface)
34 {
35     BackgroundCopyFileImpl *This = (BackgroundCopyFileImpl *) iface;
36     return InterlockedIncrement(&This->ref);
37 }
38
39 static HRESULT WINAPI BITS_IBackgroundCopyFile_QueryInterface(
40     IBackgroundCopyFile* iface,
41     REFIID riid,
42     void **ppvObject)
43 {
44     BackgroundCopyFileImpl *This = (BackgroundCopyFileImpl *) iface;
45
46     if (IsEqualGUID(riid, &IID_IUnknown)
47         || IsEqualGUID(riid, &IID_IBackgroundCopyFile))
48     {
49         *ppvObject = &This->lpVtbl;
50         BITS_IBackgroundCopyFile_AddRef(iface);
51         return S_OK;
52     }
53
54     *ppvObject = NULL;
55     return E_NOINTERFACE;
56 }
57
58
59 static ULONG WINAPI BITS_IBackgroundCopyFile_Release(
60     IBackgroundCopyFile* iface)
61 {
62     BackgroundCopyFileImpl *This = (BackgroundCopyFileImpl *) iface;
63     ULONG ref = InterlockedDecrement(&This->ref);
64
65     if (ref == 0)
66         BackgroundCopyFileDestructor(This);
67
68     return ref;
69 }
70
71 /* Get the remote name of a background copy file */
72 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetRemoteName(
73     IBackgroundCopyFile* iface,
74     LPWSTR *pVal)
75 {
76     BackgroundCopyFileImpl *This = (BackgroundCopyFileImpl *) iface;
77     int n = (lstrlenW(This->info.RemoteName) + 1) * sizeof(WCHAR);
78
79     *pVal = CoTaskMemAlloc(n);
80     if (!*pVal)
81         return E_OUTOFMEMORY;
82
83     memcpy(*pVal, This->info.RemoteName, n);
84     return S_OK;
85 }
86
87 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetLocalName(
88     IBackgroundCopyFile* iface,
89     LPWSTR *pVal)
90 {
91     BackgroundCopyFileImpl *This = (BackgroundCopyFileImpl *) iface;
92     int n = (lstrlenW(This->info.LocalName) + 1) * sizeof(WCHAR);
93
94     *pVal = CoTaskMemAlloc(n);
95     if (!*pVal)
96         return E_OUTOFMEMORY;
97
98     memcpy(*pVal, This->info.LocalName, n);
99     return S_OK;
100 }
101
102 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetProgress(
103     IBackgroundCopyFile* iface,
104     BG_FILE_PROGRESS *pVal)
105 {
106     BackgroundCopyFileImpl *This = (BackgroundCopyFileImpl *) iface;
107
108     pVal->BytesTotal = This->fileProgress.BytesTotal;
109     pVal->BytesTransferred = This->fileProgress.BytesTransferred;
110     pVal->Completed = This->fileProgress.Completed;
111
112     return S_OK;
113 }
114
115 static const IBackgroundCopyFileVtbl BITS_IBackgroundCopyFile_Vtbl =
116 {
117     BITS_IBackgroundCopyFile_QueryInterface,
118     BITS_IBackgroundCopyFile_AddRef,
119     BITS_IBackgroundCopyFile_Release,
120     BITS_IBackgroundCopyFile_GetRemoteName,
121     BITS_IBackgroundCopyFile_GetLocalName,
122     BITS_IBackgroundCopyFile_GetProgress
123 };
124
125 HRESULT BackgroundCopyFileConstructor(LPCWSTR remoteName,
126                                       LPCWSTR localName, LPVOID *ppObj)
127 {
128     BackgroundCopyFileImpl *This;
129     int n;
130
131     TRACE("(%s,%s,%p)\n", debugstr_w(remoteName),
132             debugstr_w(localName), ppObj);
133
134     This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
135     if (!This)
136         return E_OUTOFMEMORY;
137
138     n = (lstrlenW(remoteName) + 1) * sizeof(WCHAR);
139     This->info.RemoteName = HeapAlloc(GetProcessHeap(), 0, n);
140     if (!This->info.RemoteName)
141     {
142         HeapFree(GetProcessHeap(), 0, This);
143         return E_OUTOFMEMORY;
144     }
145     memcpy(This->info.RemoteName, remoteName, n);
146
147     n = (lstrlenW(localName) + 1) * sizeof(WCHAR);
148     This->info.LocalName = HeapAlloc(GetProcessHeap(), 0, n);
149     if (!This->info.LocalName)
150     {
151         HeapFree(GetProcessHeap(), 0, This->info.RemoteName);
152         HeapFree(GetProcessHeap(), 0, This);
153         return E_OUTOFMEMORY;
154     }
155     memcpy(This->info.LocalName, localName, n);
156
157     This->lpVtbl = &BITS_IBackgroundCopyFile_Vtbl;
158     This->ref = 1;
159
160     This->fileProgress.BytesTotal = BG_SIZE_UNKNOWN;
161     This->fileProgress.BytesTransferred = 0;
162     This->fileProgress.Completed = FALSE;
163
164     *ppObj = &This->lpVtbl;
165     return S_OK;
166 }