1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_shell.hxx"
26 #include "internal/global.hxx"
27 #include "internal/columninfo.hxx"
28 #include "internal/fileextensions.hxx"
29 #include "internal/metainforeader.hxx"
30 #include "internal/utilities.hxx"
31 #include "internal/config.hxx"
32 
33 #include <malloc.h>
34 
35 //----------------------------
36 //
37 //----------------------------
38 
39 namespace /* private */
40 {
41 	SHCOLUMNINFO ColumnInfoTable[] =
42 	{
43 		{{PSGUID_SUMMARYINFORMATION, PIDSI_TITLE},     VT_BSTR, LVCFMT_LEFT, 30, SHCOLSTATE_TYPE_STR, L"Title",     L"Title"},
44 		{{PSGUID_SUMMARYINFORMATION, PIDSI_AUTHOR},    VT_BSTR, LVCFMT_LEFT, 30, SHCOLSTATE_TYPE_STR, L"Author",    L"Author"},
45 		{{PSGUID_SUMMARYINFORMATION, PIDSI_SUBJECT},   VT_BSTR, LVCFMT_LEFT, 30, SHCOLSTATE_TYPE_STR, L"Subject",   L"Subject"},
46 		{{PSGUID_SUMMARYINFORMATION, PIDSI_KEYWORDS},  VT_BSTR, LVCFMT_LEFT, 30, SHCOLSTATE_TYPE_STR, L"Keywords",  L"Keywords"},
47 		{{PSGUID_SUMMARYINFORMATION, PIDSI_COMMENTS},  VT_BSTR, LVCFMT_LEFT, 30, SHCOLSTATE_TYPE_STR, L"Comments",  L"Comments"},
48 		{{PSGUID_SUMMARYINFORMATION, PIDSI_PAGECOUNT}, VT_BSTR, LVCFMT_LEFT, 30, SHCOLSTATE_TYPE_STR, L"Pagecount", L"Pagecount"}
49 	};
50 
51 	size_t ColumnInfoTableSize = sizeof(ColumnInfoTable)/sizeof(ColumnInfoTable[0]);
52 }
53 
54 //----------------------------
55 //
56 //----------------------------
57 
CColumnInfo(long RefCnt)58 CColumnInfo::CColumnInfo(long RefCnt) :
59 	m_RefCnt(RefCnt)
60 {
61 	InterlockedIncrement(&g_DllRefCnt);
62 }
63 
64 //----------------------------
65 //
66 //----------------------------
67 
~CColumnInfo()68 CColumnInfo::~CColumnInfo()
69 {
70 	InterlockedDecrement(&g_DllRefCnt);
71 }
72 
73 //-----------------------------
74 // IUnknown methods
75 //-----------------------------
76 
QueryInterface(REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)77 HRESULT STDMETHODCALLTYPE CColumnInfo::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
78 {
79 	*ppvObject = 0;
80 
81 	if (IID_IUnknown == riid || IID_IColumnProvider == riid)
82 	{
83 		IUnknown* pUnk = static_cast<IColumnProvider*>(this);
84 		pUnk->AddRef();
85 		*ppvObject = pUnk;
86 		return S_OK;
87 	}
88 
89 	return E_NOINTERFACE;
90 }
91 
92 //----------------------------
93 //
94 //----------------------------
95 
AddRef(void)96 ULONG STDMETHODCALLTYPE CColumnInfo::AddRef(void)
97 {
98 	return InterlockedIncrement(&m_RefCnt);
99 }
100 
101 //----------------------------
102 //
103 //----------------------------
104 
Release(void)105 ULONG STDMETHODCALLTYPE CColumnInfo::Release( void)
106 {
107 	long refcnt = InterlockedDecrement(&m_RefCnt);
108 
109 	if (0 == m_RefCnt)
110 		delete this;
111 
112 	return refcnt;
113 }
114 
115 //-----------------------------
116 // IColumnProvider
117 //-----------------------------
118 
Initialize(LPCSHCOLUMNINIT)119 HRESULT STDMETHODCALLTYPE CColumnInfo::Initialize(LPCSHCOLUMNINIT /*psci*/)
120 {
121 	return S_OK;
122 }
123 
124 //-----------------------------
125 // Register all columns we support
126 //-----------------------------
127 
GetColumnInfo(DWORD dwIndex,SHCOLUMNINFO * psci)128 HRESULT STDMETHODCALLTYPE CColumnInfo::GetColumnInfo(DWORD dwIndex, SHCOLUMNINFO *psci)
129 {
130 	if (dwIndex >= ColumnInfoTableSize)
131 		return S_FALSE;
132 
133 	// Return information on each column we support. Return S_FALSE
134 	// to indicate that we have returned information on all our
135 	// columns. GetColumnInfo will be called repeatedly until S_FALSE
136 	// or an error is returned
137 	psci->scid.fmtid = ColumnInfoTable[dwIndex].scid.fmtid;
138 	psci->scid.pid   = ColumnInfoTable[dwIndex].scid.pid;
139 	ZeroMemory(psci->wszTitle, sizeof(psci->wszTitle));
140 	wcsncpy(psci->wszTitle, ColumnInfoTable[dwIndex].wszTitle, (sizeof(psci->wszTitle) - 1));
141 
142 	//wcscpy(psci->wszTitle, ColumnInfoTable[dwIndex].wszTitle);
143 
144 	return S_OK;
145 }
146 
147 //-----------------------------
148 //
149 //-----------------------------
150 
GetItemData(LPCSHCOLUMNID pscid,LPCSHCOLUMNDATA pscd,VARIANT * pvarData)151 HRESULT STDMETHODCALLTYPE CColumnInfo::GetItemData(LPCSHCOLUMNID pscid, LPCSHCOLUMNDATA pscd, VARIANT *pvarData)
152 {
153 	if (IsOOFileExtension(pscd->pwszExt))
154 	{
155 		try
156 		{
157 			std::wstring fname = getShortPathName( std::wstring( pscd->wszFile ) );
158 
159 			CMetaInfoReader meta_info_accessor(WStringToString(fname));
160 
161 			VariantClear(pvarData);
162 
163 			if (IsEqualGUID (pscid->fmtid, FMTID_SummaryInformation) && pscid->pid == PIDSI_TITLE)
164 			{
165 				pvarData->vt = VT_BSTR;
166 				pvarData->bstrVal = SysAllocString(meta_info_accessor.getTagData( META_INFO_TITLE ).c_str());
167 
168 				return S_OK;
169 			}
170 			else if (IsEqualGUID (pscid->fmtid, FMTID_SummaryInformation) && pscid->pid == PIDSI_AUTHOR)
171 			{
172 				pvarData->vt = VT_BSTR;
173 				pvarData->bstrVal = SysAllocString(meta_info_accessor.getTagData( META_INFO_AUTHOR).c_str());
174 
175 				return S_OK;
176 			}
177 			else if (IsEqualGUID (pscid->fmtid, FMTID_SummaryInformation) && pscid->pid == PIDSI_SUBJECT)
178 			{
179 				pvarData->vt = VT_BSTR;
180 				pvarData->bstrVal = SysAllocString(meta_info_accessor.getTagData( META_INFO_SUBJECT).c_str());
181 
182 				return S_OK;
183 			}
184 			else if (IsEqualGUID (pscid->fmtid, FMTID_SummaryInformation) && pscid->pid == PIDSI_KEYWORDS)
185 			{
186 				pvarData->vt = VT_BSTR;
187 				pvarData->bstrVal = SysAllocString(meta_info_accessor.getTagData( META_INFO_KEYWORDS).c_str());
188 
189 				return S_OK;
190 			}
191 			else if (IsEqualGUID (pscid->fmtid, FMTID_SummaryInformation) && pscid->pid == PIDSI_COMMENTS)
192 			{
193 				pvarData->vt = VT_BSTR;
194 				pvarData->bstrVal = SysAllocString(meta_info_accessor.getTagData( META_INFO_DESCRIPTION).c_str());
195 
196 				return S_OK;
197 			}
198 			else if (IsEqualGUID (pscid->fmtid, FMTID_SummaryInformation) && pscid->pid == PIDSI_PAGECOUNT)
199 			{
200 				pvarData->vt = VT_BSTR;
201 				pvarData->bstrVal = SysAllocString(meta_info_accessor.getTagAttribute( META_INFO_DOCUMENT_STATISTIC, META_INFO_PAGES).c_str());
202 
203 				return S_OK;
204 			}
205 		}
206 		catch (const std::exception&)
207 		{
208 			return S_FALSE;
209 		}
210 	}
211 
212 	return S_FALSE;
213 }
214 
215 //-----------------------------
216 //
217 //-----------------------------
218 
IsOOFileExtension(wchar_t * Extension) const219 bool CColumnInfo::IsOOFileExtension(wchar_t* Extension) const
220 {
221 	for (size_t i = 0; i < OOFileExtensionTableSize; i++)
222 	{
223 		if (0 == _wcsicmp(Extension, OOFileExtensionTable[i].ExtensionUnicode))
224 			return true;
225 	}
226 
227 	return false;
228 }
229 
230 /* vim: set noet sw=4 ts=4: */
231