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