Debian Codegraph @

shell/source/win32/shlxthandler/infotips/infotips.cxx

raw
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 <infotips.hxx>
22 #include <shlxthdl.hxx>
23 #include <metainforeader.hxx>
24 #include <contentreader.hxx>
25 #include <utilities.hxx>
26 #include <registry.hxx>
27 #include <fileextensions.hxx>
28 #include <iso8601_converter.hxx>
29 #include <config.hxx>
30
31 #include <resource.h>
32 #include <stdio.h>
33 #include <utility>
34 #include <stdlib.h>
35
36
37 #define MAX_STRING 80
38 #define KB 1024.0
39 const std::wstring WSPACE(SPACE);
40
41
42 CInfoTip::CInfoTip(LONG RefCnt) :
43 m_RefCnt(RefCnt)
44 {
45 InterlockedIncrement(&g_DllRefCnt);
46 }
47
48
49 CInfoTip::~CInfoTip()
50 {
51 InterlockedDecrement(&g_DllRefCnt);
52 }
53
54
55 // IUnknown methods
56
57
58 HRESULT STDMETHODCALLTYPE CInfoTip::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
59 {
60 *ppvObject = nullptr;
61
62 IUnknown* pUnk = nullptr;
63
64 if (IID_IUnknown == riid || IID_IQueryInfo == riid)
65 {
66 pUnk = static_cast<IQueryInfo*>(this);
67 pUnk->AddRef();
68 *ppvObject = pUnk;
69 return S_OK;
70 }
71 else if (IID_IPersistFile == riid)
72 {
73 pUnk = static_cast<IPersistFile*>(this);
74 pUnk->AddRef();
75 *ppvObject = pUnk;
76 return S_OK;
77 }
78
79 return E_NOINTERFACE;
80 }
81
82
83 ULONG STDMETHODCALLTYPE CInfoTip::AddRef()
84 {
85 return InterlockedIncrement(&m_RefCnt);
86 }
87
88
89 ULONG STDMETHODCALLTYPE CInfoTip::Release()
90 {
91 LONG refcnt = InterlockedDecrement(&m_RefCnt);
92
93 if (0 == m_RefCnt)
94 delete this;
95
96 return refcnt;
97 }
98
99 //********************helper functions for GetInfoTip functions**********************
100
101 /** get file type information from registry.
102 */
103 static std::wstring getFileTypeInfo(const std::wstring& file_extension)
104 {
105 wchar_t extKeyValue[MAX_STRING];
106 wchar_t typeKeyValue[MAX_STRING];
107 ::std::wstring sDot(L".");
108 if (QueryRegistryKey(HKEY_CLASSES_ROOT, sDot.append(file_extension).c_str(), L"", extKeyValue, MAX_STRING))
109 if (QueryRegistryKey( HKEY_CLASSES_ROOT, extKeyValue, L"",typeKeyValue, MAX_STRING))
110 return typeKeyValue;
111
112 return EMPTY_STRING;
113 }
114
115 /** get file size.
116 */
117 static DWORD getSizeOfFile( wchar_t const * FileName )
118 {
119 HANDLE hFile = CreateFileW(FileName, // open file
120 GENERIC_READ, // open for reading
121 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, // share for all operations
122 nullptr, // no security
123 OPEN_EXISTING, // existing file only
124 FILE_ATTRIBUTE_NORMAL, // normal file
125 nullptr); // no attr. template
126
127 if (hFile != INVALID_HANDLE_VALUE)
128 {
129 DWORD dwSize = GetFileSize( hFile, nullptr );
130 CloseHandle( hFile );
131 return dwSize;
132 }
133
134 return INVALID_FILE_SIZE;
135 }
136
137 /** format file size in to be more readable.
138 */
139 static std::wstring formatSizeOfFile( DWORD dwSize )
140 {
141 if ( dwSize < 1000 )
142 {
143 char buffer[3];
144 int dFileSize = dwSize;
145
146 _itoa( dFileSize, buffer, 10 );
147 return StringToWString( buffer ).append(StringToWString("B"));
148 }
149
150 char *buffer=nullptr;
151 int decimal, sign;
152 double dFileSize = static_cast<double>(dwSize)/KB;
153
154 buffer = _fcvt( dFileSize, 1, &decimal, &sign );
155
156 ::std::wstring wsTemp = StringToWString( buffer );
157 int pos=decimal % 3;
158 ::std::wstring wsBuffer = wsTemp.substr( 0,pos);
159
160 if ( decimal )
161 for (;decimal - pos > 2;pos += 3)
162 {
163 if (pos)
164 wsBuffer.append(StringToWString(","));
165 wsBuffer.append( wsTemp.substr( pos, 3) );
166 }
167 else
168 wsBuffer.append(StringToWString("0"));
169
170 wsBuffer.append(StringToWString("."));
171 wsBuffer.append(wsTemp.substr( decimal, wsTemp.size()-decimal ));
172 wsBuffer.append(StringToWString("KB"));
173
174 return wsBuffer;
175 }
176
177
178 /** get file size information.
179 */
180 static std::wstring getFileSizeInfo(wchar_t const * FileName)
181 {
182 DWORD dwSize=getSizeOfFile(FileName);
183 if (dwSize != INVALID_FILE_SIZE)
184 return formatSizeOfFile( dwSize );
185
186 return EMPTY_STRING;
187 }
188
189
190 // IQueryInfo methods
191
192
193 COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE CInfoTip::GetInfoTip(DWORD /*dwFlags*/, PWSTR* ppwszTip)
194 {
195 std::wstring msg;
196 const std::wstring CONST_SPACE(SPACE);
197
198 //display File Type, no matter other info is loaded successfully or not.
199 std::wstring tmpTypeStr = getFileTypeInfo( get_file_name_extension(m_FileName) );
200 if ( tmpTypeStr != EMPTY_STRING )
201 {
202 msg += GetResString(IDS_TYPE_COLON) + CONST_SPACE;
203 msg += tmpTypeStr;
204 }
205
206 try
207 {
208 CMetaInfoReader meta_info_accessor(m_FileName);
209
210 //display document title;
211 if ( meta_info_accessor.getTagData( META_INFO_TITLE ).length() > 0)
212 {
213 if ( msg != EMPTY_STRING )
214 msg += L"\n";
215 msg += GetResString(IDS_TITLE_COLON) + CONST_SPACE;
216 msg += meta_info_accessor.getTagData( META_INFO_TITLE );
217 }
218 else
219 {
220 if ( msg != EMPTY_STRING )
221 msg += L"\n";
222 msg += GetResString(IDS_TITLE_COLON) + CONST_SPACE;
223 msg += m_FileNameOnly;
224 }
225
226 //display document author;
227 if ( meta_info_accessor.getTagData( META_INFO_AUTHOR ).length() > 0)
228 {
229 if ( msg != EMPTY_STRING )
230 msg += L"\n";
231 msg += GetResString( IDS_AUTHOR_COLON ) + CONST_SPACE;
232 msg += meta_info_accessor.getTagData( META_INFO_AUTHOR );
233 }
234
235 //display document subject;
236 if ( meta_info_accessor.getTagData( META_INFO_SUBJECT ).length() > 0)
237 {
238 if ( msg != EMPTY_STRING )
239 msg += L"\n";
240 msg += GetResString(IDS_SUBJECT_COLON) + CONST_SPACE;
241 msg += meta_info_accessor.getTagData( META_INFO_SUBJECT );
242 }
243
244 //display document description;
245 if ( meta_info_accessor.getTagData( META_INFO_DESCRIPTION ).length() > 0)
246 {
247 if ( msg != EMPTY_STRING )
248 msg += L"\n";
249 msg += GetResString( IDS_COMMENTS_COLON ) + CONST_SPACE;
250 msg += meta_info_accessor.getTagData( META_INFO_DESCRIPTION );
251 }
252
253 //display modified time formatted into locale representation.
254 if ( iso8601_date_to_local_date(meta_info_accessor.getTagData(META_INFO_MODIFIED )).length() > 0)
255 {
256 if ( msg != EMPTY_STRING )
257 msg += L"\n";
258 msg += GetResString( IDS_MODIFIED_COLON ) + CONST_SPACE;
259 msg += iso8601_date_to_local_date(meta_info_accessor.getTagData(META_INFO_MODIFIED ));
260 }
261 }
262 catch (const std::exception&)
263 {
264 }
265
266 //display file size, no matter other information is loaded successfully or not.
267 std::wstring tmpSizeStr = getFileSizeInfo(m_FileName.c_str());
268 if ( tmpSizeStr != EMPTY_STRING )
269 {
270 msg += L"\n";
271 msg += GetResString( IDS_SIZE_COLON ) + CONST_SPACE;
272 msg += tmpSizeStr;
273 }
274
275
276 //finalize and assign the string.
277 LPMALLOC lpMalloc;
278 HRESULT hr = SHGetMalloc(&lpMalloc);
279
280 if (SUCCEEDED(hr))
281 {
282 size_t len = sizeof(wchar_t) * msg.length() + sizeof(wchar_t);
283 wchar_t* pMem = static_cast<wchar_t*>(lpMalloc->Alloc(len));
284
285 ZeroMemory(pMem, len);
286
287 wcscpy_s(pMem, msg.length()+1, msg.c_str());
288
289 *ppwszTip = pMem;
290 lpMalloc->Release();
291
292 return S_OK;
293 }
294
295 return E_FAIL;
296 }
297
298
299 COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE CInfoTip::GetInfoFlags(DWORD * /*pdwFlags*/ )
300 {
301 return E_NOTIMPL;
302 }
303
304
305 // IPersist methods
306
307
308 HRESULT STDMETHODCALLTYPE CInfoTip::GetClassID(CLSID* pClassID)
309 {
310 *pClassID = CLSID_INFOTIP_HANDLER;
311 return S_OK;
312 }
313
314
315 // IPersistFile methods
316
317
318 HRESULT STDMETHODCALLTYPE CInfoTip::Load(LPCOLESTR pszFileName, DWORD /*dwMode*/)
319 {
320 std::wstring fname = pszFileName;
321
322 // there must be a '\' and there must even be an
323 // extension, else we would not have been called
324 std::wstring::iterator begin = fname.begin() + fname.find_last_of(L"\\") + 1;
325 std::wstring::iterator end = fname.end();
326
327 m_FileNameOnly = std::wstring(begin, end);
328
329 m_FileName = getShortPathName(fname);
330
331 return S_OK;
332 }
333
334
335 HRESULT STDMETHODCALLTYPE CInfoTip::IsDirty()
336 {
337 return E_NOTIMPL;
338 }
339
340
341 HRESULT STDMETHODCALLTYPE CInfoTip::Save(LPCOLESTR /*pszFileName*/, BOOL /*fRemember*/)
342 {
343 return E_NOTIMPL;
344 }
345
346
347 HRESULT STDMETHODCALLTYPE CInfoTip::SaveCompleted(LPCOLESTR /*pszFileName*/)
348 {
349 return E_NOTIMPL;
350 }
351
352
353 HRESULT STDMETHODCALLTYPE CInfoTip::GetCurFile(LPOLESTR __RPC_FAR * /*ppszFileName*/)
354 {
355 return E_NOTIMPL;
356 }
357
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */