Sync with trunk r63637.
[reactos.git] / base / shell / filebrowser / filebrowser.c
1 /*
2 * ReactOS Explorer
3 *
4 * Copyright 2014 Giannis Adamopoulos
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22 #include <tchar.h>
23 #include <windows.h>
24 #include <shobjidl.h>
25 #include <shlobj.h>
26
27 typedef HRESULT (WINAPI *SH_OPEN_NEW_FRAME)(LPITEMIDLIST pidl, IUnknown *paramC, long param10, long param14);
28
29 int _tmain(int argc, _TCHAR* argv[])
30 {
31 WCHAR root[MAX_PATH];
32 HRESULT hr;
33 LPSHELLFOLDER pDesktopFolder = NULL;
34 LPITEMIDLIST pidlRoot = NULL;
35 typedef HRESULT(WINAPI *SH_OPEN_NEW_FRAME)(LPITEMIDLIST pidl, IUnknown *paramC, long param10, long param14);
36 SH_OPEN_NEW_FRAME SHOpenNewFrame;
37
38 HMODULE hBrowseui = LoadLibraryW(L"browseui.dll");
39
40 if (!hBrowseui)
41 return 1;
42
43
44 if (argc < 2)
45 {
46 SH_OPEN_NEW_FRAME SHOpenNewFrame = (SH_OPEN_NEW_FRAME) GetProcAddress(hBrowseui, (LPCSTR) 103);
47 LPITEMIDLIST pidlDrives;
48 SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidlDrives);
49 SHOpenNewFrame((LPITEMIDLIST) pidlDrives, NULL, 0, 0);
50 }
51 else
52 {
53 /* A shell is already loaded. Parse the command line arguments
54 and unless we need to do something specific simply display
55 the desktop in a separate explorer window */
56 /* FIXME */
57
58 /* Commandline switches:
59 *
60 * /n Open a new window, even if an existing one still exists.
61 * /e Start with the explorer sidebar shown.
62 * /root,<object> Open a window for the given object path.
63 * /select,<object> Open a window with the given object selected.
64 */
65
66 /* FIXME: Do it right */
67 WCHAR* tmp = wcsstr(argv[1], L"/root,");
68 if (tmp)
69 {
70 WCHAR* tmp2;
71
72 tmp += 6; // skip to beginning of path
73 tmp2 = wcschr(tmp, L',');
74
75 if (tmp2)
76 {
77 wcsncpy(root, tmp, tmp2 - tmp);
78 }
79 else
80 {
81 wcscpy(root, tmp);
82 }
83 }
84 else
85 {
86 wcscpy(root, argv[1]);
87 }
88
89 if (root[0] == L'"')
90 {
91 int len = wcslen(root) - 2;
92 wcsncpy(root, root + 1, len);
93 root[len] = 0;
94 }
95
96 if (wcslen(root) > 0)
97 {
98 LPITEMIDLIST pidl;
99 ULONG chEaten;
100 ULONG dwAttributes;
101
102 if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
103 {
104 hr = pDesktopFolder->lpVtbl->ParseDisplayName(pDesktopFolder,
105 NULL,
106 NULL,
107 root,
108 &chEaten,
109 &pidl,
110 &dwAttributes);
111 if (SUCCEEDED(hr))
112 {
113 pidlRoot = pidl;
114 }
115 }
116 }
117
118 if (!pidlRoot)
119 {
120 hr = SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidlRoot);
121 if (FAILED(hr))
122 return 0;
123 }
124
125 SHOpenNewFrame = (SH_OPEN_NEW_FRAME) GetProcAddress(hBrowseui, (LPCSTR) 103);
126
127 hr = SHOpenNewFrame(pidlRoot, (IUnknown*) pDesktopFolder, 0, 0);
128 if (FAILED(hr))
129 return 0;
130 }
131
132 /* FIXME: we should wait a bit here and see if a window was created. If not we should exit this process. */
133 Sleep(1000);
134
135 ExitThread(0);
136 }
137