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 
CInfoTip(long RefCnt)42 CInfoTip::CInfoTip(long RefCnt) :
43     m_RefCnt(RefCnt)
44 {
45     ZeroMemory(m_szFileName, sizeof(m_szFileName));
46     InterlockedIncrement(&g_DllRefCnt);
47 }
48 
49 
~CInfoTip()50 CInfoTip::~CInfoTip()
51 {
52     InterlockedDecrement(&g_DllRefCnt);
53 }
54 
55 
56 // IUnknown methods
57 
58 
QueryInterface(REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)59 HRESULT 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 
AddRef()84 ULONG STDMETHODCALLTYPE CInfoTip::AddRef()
85 {
86     return InterlockedIncrement(&m_RefCnt);
87 }
88 
89 
Release()90 ULONG 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 
102 /** get file type information from registry.
103 */
getFileTypeInfo(const std::wstring & file_extension)104 static 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 
116 /** get file size.
117 */
getSizeOfFile(wchar_t const * FileName)118 static 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 
138 /** format file size in to be more readable.
139 */
formatSizeOfFile(DWORD dwSize)140 static 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 
179 /** get file size information.
180 */
getFileSizeInfo(wchar_t const * FileName)181 static 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 
GetInfoTip(DWORD,PWSTR * ppwszTip)194 COM_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 
GetInfoFlags(DWORD *)300 COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE CInfoTip::GetInfoFlags(DWORD * /*pdwFlags*/ )
301 {
302     return E_NOTIMPL;
303 }
304 
305 
306 // IPersist methods
307 
308 
GetClassID(CLSID * pClassID)309 HRESULT STDMETHODCALLTYPE CInfoTip::GetClassID(CLSID* pClassID)
310 {
311     pClassID = const_cast<CLSID*>(&CLSID_INFOTIP_HANDLER);
312     return S_OK;
313 }
314 
315 
316 // IPersistFile methods
317 
318 
Load(LPCOLESTR pszFileName,DWORD)319 HRESULT 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 
IsDirty()342 HRESULT STDMETHODCALLTYPE CInfoTip::IsDirty()
343 {
344     return E_NOTIMPL;
345 }
346 
347 
Save(LPCOLESTR,BOOL)348 HRESULT STDMETHODCALLTYPE CInfoTip::Save(LPCOLESTR /*pszFileName*/, BOOL /*fRemember*/)
349 {
350     return E_NOTIMPL;
351 }
352 
353 
SaveCompleted(LPCOLESTR)354 HRESULT STDMETHODCALLTYPE CInfoTip::SaveCompleted(LPCOLESTR /*pszFileName*/)
355 {
356     return E_NOTIMPL;
357 }
358 
359 
GetCurFile(LPOLESTR __RPC_FAR *)360 HRESULT STDMETHODCALLTYPE CInfoTip::GetCurFile(LPOLESTR __RPC_FAR * /*ppszFileName*/)
361 {
362     return E_NOTIMPL;
363 }
364 
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
366