1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: infotips.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
33 #include "internal/global.hxx"
34 #include "internal/infotips.hxx"
35 #include "internal/shlxthdl.hxx"
36 #include "internal/metainforeader.hxx"
37 #include "internal/contentreader.hxx"
38 #include "internal/utilities.hxx"
39 #include "internal/registry.hxx"
40 #include "internal/fileextensions.hxx"
41 #include "internal/iso8601_converter.hxx"
42 #include "internal/config.hxx"
44 #include "internal/resource.h"
50 const std::wstring WSPACE
= std::wstring(SPACE
);
52 //-----------------------------
54 //-----------------------------
56 CInfoTip::CInfoTip(long RefCnt
) :
59 ZeroMemory(m_szFileName
, sizeof(m_szFileName
));
60 InterlockedIncrement(&g_DllRefCnt
);
63 //-----------------------------
65 //-----------------------------
69 InterlockedDecrement(&g_DllRefCnt
);
72 //-----------------------------
74 //-----------------------------
76 HRESULT STDMETHODCALLTYPE
CInfoTip::QueryInterface(REFIID riid
, void __RPC_FAR
*__RPC_FAR
*ppvObject
)
82 if (IID_IUnknown
== riid
|| IID_IQueryInfo
== riid
)
84 pUnk
= static_cast<IQueryInfo
*>(this);
89 else if (IID_IPersistFile
== riid
)
91 pUnk
= static_cast<IPersistFile
*>(this);
100 //----------------------------
102 //----------------------------
104 ULONG STDMETHODCALLTYPE
CInfoTip::AddRef(void)
106 return InterlockedIncrement(&m_RefCnt
);
109 //----------------------------
111 //----------------------------
113 ULONG STDMETHODCALLTYPE
CInfoTip::Release( void)
115 long refcnt
= InterlockedDecrement(&m_RefCnt
);
123 //********************helper functions for GetInfoTip functions**********************
125 /** get file type infomation from registry.
127 std::wstring
getFileTypeInfo(const std::string
& file_extension
)
129 char extKeyValue
[MAX_STRING
];
130 char typeKeyValue
[MAX_STRING
];
131 ::std::string
sDot(".");
132 if (QueryRegistryKey(HKEY_CLASSES_ROOT
, (sDot
.append(file_extension
)).c_str(), "", extKeyValue
, MAX_STRING
))
133 if (QueryRegistryKey( HKEY_CLASSES_ROOT
, extKeyValue
, "",typeKeyValue
, MAX_STRING
))
134 return StringToWString(typeKeyValue
);
141 DWORD
getSizeOfFile( char* FileName
)
143 HANDLE hFile
= CreateFile(StringToWString(FileName
).c_str(), // open file
144 GENERIC_READ
, // open for reading
145 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
, // share for all operations
147 OPEN_EXISTING
, // existing file only
148 FILE_ATTRIBUTE_NORMAL
, // normal file
149 NULL
); // no attr. template
151 if (hFile
!= INVALID_HANDLE_VALUE
)
153 DWORD dwSize
= GetFileSize( HANDLE(hFile
), NULL
);
154 CloseHandle( HANDLE(hFile
) );
158 return INVALID_FILE_SIZE
;
161 /** format file size in to be more readable.
163 std::wstring
formatSizeOfFile( DWORD dwSize
)
168 int dFileSize
= dwSize
;
170 _itoa( dFileSize
, buffer
, 10 );
171 return StringToWString( buffer
).append(StringToWString("B"));
176 double dFileSize
= (double)dwSize
/(double)KB
;
178 buffer
= _fcvt( dFileSize
, 1, &decimal
, &sign
);
180 ::std::wstring wsTemp
= StringToWString( buffer
);
182 ::std::wstring wsBuffer
= wsTemp
.substr( 0,pos
);
185 for (;decimal
- pos
> 2;pos
+= 3)
188 wsBuffer
.append(StringToWString(","));
189 wsBuffer
.append( wsTemp
.substr( pos
, 3) );
192 wsBuffer
.append(StringToWString("0"));
194 wsBuffer
.append(StringToWString("."));
195 wsBuffer
.append(wsTemp
.substr( decimal
, wsTemp
.size()-decimal
));
196 wsBuffer
.append(StringToWString("KB"));
202 /** get file size infomation.
204 std::wstring
getFileSizeInfo(char* FileName
)
206 DWORD dwSize
=getSizeOfFile(FileName
);
207 if (dwSize
!= INVALID_FILE_SIZE
)
208 return formatSizeOfFile( dwSize
);
213 //----------------------------
214 // IQueryInfo methods
215 //----------------------------
217 HRESULT STDMETHODCALLTYPE
CInfoTip::GetInfoTip(DWORD
/*dwFlags*/, wchar_t** ppwszTip
)
220 const std::wstring
CONST_SPACE(SPACE
);
222 //display File Type, no matter other info is loaded successfully or not.
223 std::wstring tmpTypeStr
= getFileTypeInfo( get_file_name_extension(m_szFileName
) );
224 if ( tmpTypeStr
!= EMPTY_STRING
)
226 msg
+= GetResString(IDS_TYPE_COLON
) + CONST_SPACE
;
232 CMetaInfoReader
meta_info_accessor(m_szFileName
);
234 //display document title;
235 if ( meta_info_accessor
.getTagData( META_INFO_TITLE
).length() > 0)
237 if ( msg
!= EMPTY_STRING
)
239 msg
+= GetResString(IDS_TITLE_COLON
) + CONST_SPACE
;
240 msg
+= meta_info_accessor
.getTagData( META_INFO_TITLE
);
244 if ( msg
!= EMPTY_STRING
)
246 msg
+= GetResString(IDS_TITLE_COLON
) + CONST_SPACE
;
247 msg
+= m_FileNameOnly
;
250 //display document author;
251 if ( meta_info_accessor
.getTagData( META_INFO_AUTHOR
).length() > 0)
253 if ( msg
!= EMPTY_STRING
)
255 msg
+= GetResString( IDS_AUTHOR_COLON
) + CONST_SPACE
;
256 msg
+= meta_info_accessor
.getTagData( META_INFO_AUTHOR
);
259 //display document subject;
260 if ( meta_info_accessor
.getTagData( META_INFO_SUBJECT
).length() > 0)
262 if ( msg
!= EMPTY_STRING
)
264 msg
+= GetResString(IDS_SUBJECT_COLON
) + CONST_SPACE
;
265 msg
+= meta_info_accessor
.getTagData( META_INFO_SUBJECT
);
268 //display document description;
269 if ( meta_info_accessor
.getTagData( META_INFO_DESCRIPTION
).length() > 0)
271 if ( msg
!= EMPTY_STRING
)
273 msg
+= GetResString( IDS_COMMENTS_COLON
) + CONST_SPACE
;
274 msg
+= meta_info_accessor
.getTagData( META_INFO_DESCRIPTION
);
277 //display midified time formated into locale representation.
278 if ( iso8601_date_to_local_date(meta_info_accessor
.getTagData(META_INFO_MODIFIED
)).length() > 0)
280 if ( msg
!= EMPTY_STRING
)
282 msg
+= GetResString( IDS_MODIFIED_COLON
) + CONST_SPACE
;
283 msg
+= iso8601_date_to_local_date(meta_info_accessor
.getTagData(META_INFO_MODIFIED
));
286 catch (const std::exception
&)
291 //display file size, no matter other infomation is loaded successfully or not.
292 std::wstring tmpSizeStr
= getFileSizeInfo( m_szFileName
);
293 if ( tmpSizeStr
!= EMPTY_STRING
)
296 msg
+= GetResString( IDS_SIZE_COLON
) + CONST_SPACE
;
301 //finalize and assignthe string.
303 HRESULT hr
= SHGetMalloc(&lpMalloc
);
307 size_t len
= sizeof(wchar_t) * msg
.length() + sizeof(wchar_t);
308 wchar_t* pMem
= reinterpret_cast<wchar_t*>(lpMalloc
->Alloc(len
));
310 ZeroMemory(pMem
, len
);
312 msg
.copy(pMem
,msg
.length());
323 //----------------------------
325 //----------------------------
327 HRESULT STDMETHODCALLTYPE
CInfoTip::GetInfoFlags(DWORD
* /*pdwFlags*/ )
332 //----------------------------
334 //----------------------------
336 HRESULT STDMETHODCALLTYPE
CInfoTip::GetClassID(CLSID
* pClassID
)
338 pClassID
= const_cast<CLSID
*>(&CLSID_INFOTIP_HANDLER
);
342 //----------------------------
343 // IPersistFile methods
344 //----------------------------
346 HRESULT STDMETHODCALLTYPE
CInfoTip::Load(LPCOLESTR pszFileName
, DWORD
/*dwMode*/)
348 std::wstring fname
= pszFileName
;
350 // there must be a '\' and there must even be an
351 // extension, else we would not have been called
352 std::wstring::iterator begin
= fname
.begin() + fname
.find_last_of(L
"\\") + 1;
353 std::wstring::iterator end
= fname
.end();
355 m_FileNameOnly
= std::wstring(begin
, end
);
357 std::string fnameA
= WStringToString(fname
);
360 // ZeroMemory because strncpy doesn't '\0'-terminates the destination
361 // string; reserve the last place in the buffer for the final '\0'
362 // that's why '(sizeof(m_szFileName) - 1)'
363 ZeroMemory(m_szFileName
, sizeof(m_szFileName
));
364 strncpy(m_szFileName
, fnameA
.c_str(), (sizeof(m_szFileName
) - 1));
369 //----------------------------
371 //----------------------------
373 HRESULT STDMETHODCALLTYPE
CInfoTip::IsDirty(void)
378 //----------------------------
380 //----------------------------
382 HRESULT STDMETHODCALLTYPE
CInfoTip::Save(LPCOLESTR
/*pszFileName*/, BOOL
/*fRemember*/)
387 //----------------------------
389 //----------------------------
391 HRESULT STDMETHODCALLTYPE
CInfoTip::SaveCompleted(LPCOLESTR
/*pszFileName*/)
396 //----------------------------
398 //----------------------------
400 HRESULT STDMETHODCALLTYPE
CInfoTip::GetCurFile(LPOLESTR __RPC_FAR
* /*ppszFileName*/)