1 /* Copyright (c) Mark Harmstone 2016-17
2  *
3  * This file is part of WinBtrfs.
4  *
5  * WinBtrfs is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public Licence as published by
7  * the Free Software Foundation, either version 3 of the Licence, or
8  * (at your option) any later version.
9  *
10  * WinBtrfs is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public Licence for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public Licence
16  * along with WinBtrfs.  If not, see <http://www.gnu.org/licenses/>. */
17 
18 #pragma once
19 
20 #include <shlobj.h>
21 
22 extern LONG objs_loaded;
23 
24 class BtrfsContextMenu : public IShellExtInit, IContextMenu {
25 public:
26     BtrfsContextMenu() {
27         refcount = 0;
28         ignore = true;
29         stgm_set = false;
30         uacicon = nullptr;
31         allow_snapshot = false;
32         InterlockedIncrement(&objs_loaded);
33     }
34 
35     virtual ~BtrfsContextMenu() {
36         if (stgm_set) {
37             GlobalUnlock(stgm.hGlobal);
38             ReleaseStgMedium(&stgm);
39         }
40 
41         if (uacicon)
42             DeleteObject(uacicon);
43 
44         InterlockedDecrement(&objs_loaded);
45     }
46 
47     // IUnknown
48 
49     HRESULT __stdcall QueryInterface(REFIID riid, void **ppObj);
50 
51     ULONG __stdcall AddRef() {
52         return InterlockedIncrement(&refcount);
53     }
54 
55     ULONG __stdcall Release() {
56         LONG rc = InterlockedDecrement(&refcount);
57 
58         if (rc == 0)
59             delete this;
60 
61         return rc;
62     }
63 
64     // IShellExtInit
65 
66     virtual HRESULT __stdcall Initialize(PCIDLIST_ABSOLUTE pidlFolder, IDataObject* pdtobj, HKEY hkeyProgID);
67 
68     // IContextMenu
69 
70     virtual HRESULT __stdcall QueryContextMenu(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
71     virtual HRESULT __stdcall InvokeCommand(LPCMINVOKECOMMANDINFO pici);
72     virtual HRESULT __stdcall GetCommandString(UINT_PTR idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax);
73 
74 private:
75     LONG refcount;
76     bool ignore, allow_snapshot;
77     bool bg;
78     wstring path;
79     STGMEDIUM stgm;
80     bool stgm_set;
81     HBITMAP uacicon;
82 
83     void reflink_copy(HWND hwnd, const WCHAR* fn, const WCHAR* dir);
84     void get_uac_icon();
85 };
86