1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <global.hxx>
21 #include "classfactory.hxx"
22 #include <infotips.hxx>
23 #include <propsheets.hxx>
24 #include <columninfo.hxx>
25 #include <thumbviewer.hxx>
26 #include <shlxthdl.hxx>
27
28
29 LONG CClassFactory::s_ServerLocks = 0;
30
31
CClassFactory(const CLSID & clsid)32 CClassFactory::CClassFactory(const CLSID& clsid) :
33 m_RefCnt(1),
34 m_Clsid(clsid)
35 {
36 InterlockedIncrement(&g_DllRefCnt);
37 }
38
39
~CClassFactory()40 CClassFactory::~CClassFactory()
41 {
42 InterlockedDecrement(&g_DllRefCnt);
43 }
44
45
46 // IUnknown methods
47
48
QueryInterface(REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)49 HRESULT STDMETHODCALLTYPE CClassFactory::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
50 {
51 *ppvObject = nullptr;
52
53 if (IID_IUnknown == riid || IID_IClassFactory == riid)
54 {
55 IUnknown* pUnk = this;
56 pUnk->AddRef();
57 *ppvObject = pUnk;
58 return S_OK;
59 }
60
61 return E_NOINTERFACE;
62 }
63
64
AddRef()65 ULONG STDMETHODCALLTYPE CClassFactory::AddRef()
66 {
67 return InterlockedIncrement(&m_RefCnt);
68 }
69
70
Release()71 ULONG STDMETHODCALLTYPE CClassFactory::Release()
72 {
73 LONG refcnt = InterlockedDecrement(&m_RefCnt);
74
75 if (0 == refcnt)
76 delete this;
77
78 return refcnt;
79 }
80
81
82 // IClassFactory methods
83
84
CreateInstance(IUnknown __RPC_FAR * pUnkOuter,REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)85 HRESULT STDMETHODCALLTYPE CClassFactory::CreateInstance(
86 IUnknown __RPC_FAR *pUnkOuter,
87 REFIID riid,
88 void __RPC_FAR *__RPC_FAR *ppvObject)
89 {
90 if (pUnkOuter != nullptr)
91 return CLASS_E_NOAGGREGATION;
92
93 IUnknown* pUnk = nullptr;
94
95 if (CLSID_PROPERTYSHEET_HANDLER == m_Clsid)
96 pUnk = static_cast<IShellExtInit*>(new CPropertySheet());
97
98 else if (CLSID_INFOTIP_HANDLER == m_Clsid)
99 pUnk = static_cast<IQueryInfo*>(new CInfoTip());
100
101 else if (CLSID_COLUMN_HANDLER == m_Clsid)
102 pUnk = static_cast<IColumnProvider*>(new CColumnInfo());
103
104 else if (CLSID_THUMBVIEWER_HANDLER == m_Clsid)
105 pUnk = static_cast<IExtractImage*>(new CThumbviewer());
106
107 if (nullptr == pUnk)
108 {
109 return E_OUTOFMEMORY;
110 }
111
112 HRESULT hr = pUnk->QueryInterface(riid, ppvObject);
113
114 // if QueryInterface failed the component will destroy itself
115 pUnk->Release();
116
117 return hr;
118 }
119
120
LockServer(BOOL fLock)121 HRESULT STDMETHODCALLTYPE CClassFactory::LockServer(BOOL fLock)
122 {
123 if (fLock)
124 InterlockedIncrement(&s_ServerLocks);
125 else
126 InterlockedDecrement(&s_ServerLocks);
127
128 return S_OK;
129 }
130
131
IsLocked()132 bool CClassFactory::IsLocked()
133 {
134 return (s_ServerLocks > 0);
135 }
136
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
138