summaryrefslogtreecommitdiff
path: root/shell/source/win32/shlxthandler/infotips/infotips.cxx
blob: 58f8a877b202d3553c41a0b0cc4b5d1f7e277a25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_shell.hxx"
#include "internal/global.hxx"
#include "internal/infotips.hxx"
#include "internal/shlxthdl.hxx"
#include "internal/metainforeader.hxx"
#include "internal/contentreader.hxx"
#include "internal/utilities.hxx"
#include "internal/registry.hxx"
#include "internal/fileextensions.hxx"
#include "internal/iso8601_converter.hxx"
#include "internal/config.hxx"

#include "internal/resource.h"
#include <stdio.h>
#include <utility>
#include <stdlib.h>


#define MAX_STRING 80
#define KB 1024.0
const std::wstring WSPACE = std::wstring(SPACE);

//-----------------------------
//
//-----------------------------

CInfoTip::CInfoTip(long RefCnt) : 
    m_RefCnt(RefCnt)
{
    ZeroMemory(m_szFileName, sizeof(m_szFileName));
    InterlockedIncrement(&g_DllRefCnt);
}

//-----------------------------
//
//-----------------------------

CInfoTip::~CInfoTip()
{
    InterlockedDecrement(&g_DllRefCnt);
}

//-----------------------------
// IUnknown methods
//-----------------------------

HRESULT STDMETHODCALLTYPE CInfoTip::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
{
    *ppvObject = 0;

    IUnknown* pUnk = 0;

    if (IID_IUnknown == riid || IID_IQueryInfo == riid)
    {
        pUnk = static_cast<IQueryInfo*>(this);
        pUnk->AddRef();
        *ppvObject = pUnk;
        return S_OK;
    }
    else if (IID_IPersistFile == riid)
    {
        pUnk = static_cast<IPersistFile*>(this);
        pUnk->AddRef();
        *ppvObject = pUnk;
        return S_OK;
    }

    return E_NOINTERFACE;
}

//----------------------------
// 
//----------------------------

ULONG STDMETHODCALLTYPE CInfoTip::AddRef(void)
{
    return InterlockedIncrement(&m_RefCnt);
}

//----------------------------
// 
//----------------------------
        
ULONG STDMETHODCALLTYPE CInfoTip::Release( void)
{
    long refcnt = InterlockedDecrement(&m_RefCnt);

    if (0 == m_RefCnt)
        delete this;

    return refcnt;
}

//********************helper functions for GetInfoTip functions**********************

/** get file type infomation from registry.
*/
std::wstring getFileTypeInfo(const std::string& file_extension)
{
    char extKeyValue[MAX_STRING];
    char typeKeyValue[MAX_STRING];
    ::std::string sDot(".");
    if (QueryRegistryKey(HKEY_CLASSES_ROOT, (sDot.append(file_extension)).c_str(), "", extKeyValue, MAX_STRING))
        if (QueryRegistryKey( HKEY_CLASSES_ROOT, extKeyValue, "",typeKeyValue, MAX_STRING))
            return StringToWString(typeKeyValue);
    
    return EMPTY_STRING;
}

/** get file size.
*/
DWORD getSizeOfFile( char* FileName )
{
    HANDLE hFile = CreateFile(StringToWString(FileName).c_str(),            // open file
                        GENERIC_READ,                                       // open for reading 
                        FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, // share for all operations 
                        NULL,                                               // no security 
                        OPEN_EXISTING,                                      // existing file only 
                        FILE_ATTRIBUTE_NORMAL,                              // normal file 
                        NULL);                                              // no attr. template 

    if (hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwSize = GetFileSize( HANDLE(hFile), NULL );
        CloseHandle( HANDLE(hFile) );
        return dwSize;
    }

    return INVALID_FILE_SIZE;
}

/** format file size in to be more readable.
*/
std::wstring formatSizeOfFile( DWORD dwSize )
{
    if ( dwSize < 1000 ) 
    {
        char buffer[3];
        int dFileSize = dwSize;
        
        _itoa( dFileSize, buffer, 10 );
        return StringToWString( buffer ).append(StringToWString("B"));
    }

    char *buffer=NULL;
    int  decimal, sign;
    double dFileSize = (double)dwSize/(double)KB;

    buffer = _fcvt( dFileSize, 1, &decimal, &sign );

    ::std::wstring wsTemp = StringToWString( buffer );
    int  pos=decimal % 3;
    ::std::wstring wsBuffer = wsTemp.substr( 0,pos);

    if ( decimal )
        for (;decimal - pos > 2;pos += 3)
        {
            if (pos)
                wsBuffer.append(StringToWString(","));
            wsBuffer.append( wsTemp.substr( pos, 3) );
        }
    else
        wsBuffer.append(StringToWString("0"));

    wsBuffer.append(StringToWString("."));
    wsBuffer.append(wsTemp.substr( decimal, wsTemp.size()-decimal ));
    wsBuffer.append(StringToWString("KB"));

    return wsBuffer;
}


/** get file size infomation.
*/
std::wstring getFileSizeInfo(char* FileName)
{
    DWORD dwSize=getSizeOfFile(FileName);
    if (dwSize != INVALID_FILE_SIZE) 
        return formatSizeOfFile( dwSize );

    return EMPTY_STRING;
}

//----------------------------
// IQueryInfo methods
//----------------------------

HRESULT STDMETHODCALLTYPE CInfoTip::GetInfoTip(DWORD /*dwFlags*/, wchar_t** ppwszTip)
{
    std::wstring msg;
    const std::wstring CONST_SPACE(SPACE);

    //display File Type, no matter other info is loaded successfully or not.
    std::wstring tmpTypeStr = getFileTypeInfo( get_file_name_extension(m_szFileName) );
    if ( tmpTypeStr != EMPTY_STRING )
    {
        msg += GetResString(IDS_TYPE_COLON) + CONST_SPACE;
        msg += tmpTypeStr;
    }

    try
    {
        CMetaInfoReader meta_info_accessor(m_szFileName);

        //display document title;
        if ( meta_info_accessor.getTagData( META_INFO_TITLE ).length() > 0)
        {
            if ( msg != EMPTY_STRING )
                msg += L"\n";
            msg += GetResString(IDS_TITLE_COLON) + CONST_SPACE;
            msg += meta_info_accessor.getTagData( META_INFO_TITLE );
        }
        else
        {
            if ( msg != EMPTY_STRING )
                msg += L"\n";
            msg += GetResString(IDS_TITLE_COLON) + CONST_SPACE;
            msg += m_FileNameOnly;
        }
            
        //display document author;
        if ( meta_info_accessor.getTagData( META_INFO_AUTHOR ).length() > 0)
        {
            if ( msg != EMPTY_STRING )
                msg += L"\n";
            msg += GetResString( IDS_AUTHOR_COLON ) + CONST_SPACE;
            msg += meta_info_accessor.getTagData( META_INFO_AUTHOR );   
        }

        //display document subject;
        if ( meta_info_accessor.getTagData( META_INFO_SUBJECT ).length() > 0)
        {
            if ( msg != EMPTY_STRING )
                msg += L"\n";
            msg += GetResString(IDS_SUBJECT_COLON) + CONST_SPACE;
            msg += meta_info_accessor.getTagData( META_INFO_SUBJECT );
        }
        
        //display document description;
        if ( meta_info_accessor.getTagData( META_INFO_DESCRIPTION ).length() > 0)
        {
            if ( msg != EMPTY_STRING )
                msg += L"\n";
            msg += GetResString( IDS_COMMENTS_COLON ) + CONST_SPACE;
            msg += meta_info_accessor.getTagData( META_INFO_DESCRIPTION );
        }

        //display midified time formated into locale representation.
        if ( iso8601_date_to_local_date(meta_info_accessor.getTagData(META_INFO_MODIFIED )).length() > 0)
        {
            if ( msg != EMPTY_STRING )
                msg += L"\n";
            msg += GetResString( IDS_MODIFIED_COLON ) + CONST_SPACE;
            msg += iso8601_date_to_local_date(meta_info_accessor.getTagData(META_INFO_MODIFIED ));
        }
    }
    catch (const std::exception&)
    {
        //return E_FAIL;
    }

    //display file size, no matter other infomation is loaded successfully or not.
    std::wstring tmpSizeStr = getFileSizeInfo( m_szFileName );
    if ( tmpSizeStr != EMPTY_STRING )
    {
        msg += L"\n";
        msg += GetResString( IDS_SIZE_COLON ) + CONST_SPACE;
        msg += tmpSizeStr;
    }


    //finalize and assignthe string.
    LPMALLOC lpMalloc;
    HRESULT hr = SHGetMalloc(&lpMalloc);

    if (SUCCEEDED(hr))
    {
        size_t len = sizeof(wchar_t) * msg.length() + sizeof(wchar_t);
        wchar_t* pMem = reinterpret_cast<wchar_t*>(lpMalloc->Alloc(len));
        
        ZeroMemory(pMem, len);

        msg.copy(pMem,msg.length());

        *ppwszTip = pMem;
        lpMalloc->Release();
                                                    
        return S_OK;
    }       

    return E_FAIL;
}

//----------------------------
// 
//----------------------------

HRESULT STDMETHODCALLTYPE CInfoTip::GetInfoFlags(DWORD * /*pdwFlags*/ )
{
    return E_NOTIMPL;
}

//----------------------------
// IPersist methods
//----------------------------

HRESULT STDMETHODCALLTYPE CInfoTip::GetClassID(CLSID* pClassID)
{
    pClassID = const_cast<CLSID*>(&CLSID_INFOTIP_HANDLER);
    return S_OK;
}

//----------------------------
// IPersistFile methods
//----------------------------

HRESULT STDMETHODCALLTYPE CInfoTip::Load(LPCOLESTR pszFileName, DWORD /*dwMode*/)
{
    std::wstring fname = pszFileName;

    // there must be a '\' and there must even be an
    // extension, else we would not have been called
    std::wstring::iterator begin = fname.begin() + fname.find_last_of(L"\\") + 1;
    std::wstring::iterator end   = fname.end();

    m_FileNameOnly = std::wstring(begin, end);
    
    std::string fnameA = WStringToString(fname);

    // #115531#
    // ZeroMemory because strncpy doesn't '\0'-terminates the destination
    // string; reserve the last place in the buffer for the final '\0' 
    // that's why '(sizeof(m_szFileName) - 1)'
    ZeroMemory(m_szFileName, sizeof(m_szFileName));    
    strncpy(m_szFileName, fnameA.c_str(), (sizeof(m_szFileName) - 1));

    return S_OK;
}

//----------------------------
// 
//----------------------------

HRESULT STDMETHODCALLTYPE CInfoTip::IsDirty(void)
{
    return E_NOTIMPL;
}

//----------------------------
// 
//----------------------------
                
HRESULT STDMETHODCALLTYPE CInfoTip::Save(LPCOLESTR /*pszFileName*/, BOOL /*fRemember*/)
{
    return E_NOTIMPL;
}

//----------------------------
// 
//----------------------------
        
HRESULT STDMETHODCALLTYPE CInfoTip::SaveCompleted(LPCOLESTR /*pszFileName*/)
{
    return E_NOTIMPL;
}

//----------------------------
// 
//----------------------------
        
HRESULT STDMETHODCALLTYPE CInfoTip::GetCurFile(LPOLESTR __RPC_FAR * /*ppszFileName*/)
{
    return E_NOTIMPL;
}