📄 filebrowser.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include <windows.h>
#include <Commctrl.h>
#include <winuser.h>
#include <Commdlg.h>
#include <windowsx.h>
#if defined (WIN32_PLATFORM_PSPC)
#include <aygshell.h>
#elif defined(SDK_BUILD)
#pragma message ("WARNING:You will need to manually remove aygshell.lib from the list of input libraries...")
#pragma message ("WARNING:...when building for HPC until we can figure out conditional linking in the EVC IDE")
#endif
#include "newres.h"
#include "DeviceChooser.h"
#include "..\parser\parser.h"
#include "resource.h"
#include "TreeUtils.h"
#include "obex.h"
//BUGBUG: these need to be in the idl
#define OBEX_TYPE_UNICODE 0x00
#define OBEX_TYPE_BYTESEQ 0x40
#define OBEX_TYPE_BYTE 0x80
#define OBEX_TYPE_DWORD 0xc0
#define OBEX_HID_CONNECTIONID (0x0b | OBEX_TYPE_DWORD)
#define LISTING_TYPE "x-obex/folder-listing"
#define INNER_BAR_GAP 2
#define MENU_HEIGHT 26
#define WM_DO_UPDATE 101
#define WM_ABORT 102
struct DIR_NODE
{
HTREEITEM htiParent;
TCHAR *pPath;
TCHAR *pFileName;
DWORD dwSize;
BOOL fDir;
BOOL fTraversed;
};
struct DIRNODE_LIST
{
DIR_NODE *pItem;
DIRNODE_LIST *pNext;
};
#define CONNECTED_NOTHING 0
#define CONNECTED_FILEBROWSER 1
#define CONNECTED_INBOX 2
// Global Variables:
HINSTANCE g_hInst;
HWND g_hTreeWnd;
HWND g_hwndCB;
HWND g_hWnd;
HMENU g_hMenu;
HWND g_hWndStatus;
HWND g_hWndAbortButton;
UINT g_fConnectedTo;
HTREEITEM g_htiParentNode;
BOOL g_SelectionDisabled;
BOOL g_fServerBrowsable;
DIR_NODE *g_SelectedNode;
CRITICAL_SECTION g_csLock;
BOOL g_fTransferInProgress;
BOOL g_fAbort = FALSE;
int g_iMenuHeight;
DWORD dwBytesSent;
DWORD dwBytesTotal;
IObexDevice *g_pObexDevice;
IObex *g_pObex;
DIRNODE_LIST *g_pNodeList;
WCHAR szRequestDir[MAX_PATH];
WCHAR szStorageDir[MAX_PATH];
WCHAR szClientPassword[MAX_PATH];
WCHAR szNewDirName[MAX_PATH];
DIR_NODE g_ParentNode;
HMODULE g_hAygShellDll = NULL;
#define IDC_CMDBAR 1
//GUID CLSID_FileExchange = // {F9ec7bc4-953c-11d2-984e-525400dc9e09}
//{ 0xF9ec7bc4, 0x953c, 0x11d2, {0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09}};
GUID CLSID_FileExchange_NetOrder = // {F9ec7bc4-953c-11d2-984e-525400dc9e09}
{ 0xc47becf9, 0x3c95, 0xd211, {0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09}};
typedef BOOL (*OBEXClientPFunct) (LPCTSTR objectName);
const LPCTSTR k_szBrowserReg = TEXT("Software\\Microsoft\\Obex\\FileBrowser");
#define OBEX_FTP_BASE_REG L"Software\\Microsoft\\Obex\\Services\\{F9ec7bc4-953c-11d2-984e-525400dc9e09}"
#define OBEX_FTP_BASE_REG L"Software\\Microsoft\\Obex\\Services\\{F9ec7bc4-953c-11d2-984e-525400dc9e09}"
#define OBEX_VCARD_BASE_REG L"Software\\Microsoft\\Obex\\Services\\{00000000-0000-0000-0000-000000000000}\\MimeTypes\\text/x-vCard"
// Forward declarations of functions included in this code module:
LRESULT CALLBACK MainProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChooseServiceProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK StatusProc (HWND, UINT, WPARAM, LPARAM);
HWND CreateRpCommandBar(HWND hwnd);
HRESULT RefreshDirectory(LPTSTR pswDir, HTREEITEM parentNode);
BOOL DrawBarGraph(HDC hdc, RECT *outline_box, LONG lvalue, LONG lmin, LONG lmax);
BOOL ObexFindFileNext (ObexFileSearch *pSearch, WIN32_FIND_DATA *pwfd);
ObexFileSearch *ObexFindFileFirst (IStream *myStream, WCHAR *szPattern, WIN32_FIND_DATA *pwfd);
BOOL ObexCloseSearch (ObexFileSearch *pSearch);
HRESULT FetchFile(LPTSTR file, LPTSTR path);
HRESULT SendFile();
HRESULT FetchFileByMimeType(char *mimeType, WCHAR *destFileName);
BOOL CALLBACK AddDirectoryDlgProc(HWND hDlg, UINT message ,WPARAM wParam, LPARAM lParam);
BOOL CALLBACK ConfigServerDlgProc(HWND hDlg, UINT message ,WPARAM wParam, LPARAM lParam);
BOOL CALLBACK ConfigClientDlgProc(HWND hDlg, UINT message ,WPARAM wParam, LPARAM lParam);
HFONT CreateCustomFont(HDC dc, UINT iHeight);
WINSHELLAPI COLORREF SHGetSysColor(int nIndex);
void SHFillRectClr(HDC hdc, LPRECT prc, COLORREF clr);
/*****************************************************************************/
/* */
/* PopFileOpenDlg */
/* */
/* Open the 'file open' dialog box */
/*****************************************************************************/
BOOL PopFileOpenDlg(HINSTANCE hInst, HWND hwnd, LPTSTR pstrFileName, LPTSTR pstrTitleName) {
OPENFILENAME ofn;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = hInst;
ofn.lpstrFilter = _T("All Files (*.*)\0*.*\0\0");
ofn.lpstrCustomFilter=NULL;
ofn.nMaxCustFilter=0;
ofn.nFilterIndex=0;
ofn.lpstrFile=NULL;
ofn.nMaxFile=MAX_PATH;
ofn.lpstrFileTitle=NULL;
ofn.nMaxFileTitle = MAX_PATH;
ofn.lpstrInitialDir = 0;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt=0;
ofn.lCustData=0;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
ofn.hwndOwner = hwnd;
ofn.lpstrFile = pstrFileName;
ofn.lpstrFileTitle = pstrTitleName;
ofn.Flags = 0;
return GetOpenFileName (&ofn);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd )
{
MSG msg;
WNDCLASS wndclass;
//make sure we are the only copy running
HWND hWndPrev = FindWindow(_T("OBEXFileBrowser"), _T("OBEXFileBrowser"));
if(NULL != hWndPrev)
{
SetForegroundWindow(hWndPrev);
UpdateWindow(hWndPrev);
return(0);
}
//initilize globals
g_hInst = hInstance;
g_pObexDevice = 0;
g_fConnectedTo = CONNECTED_NOTHING;
g_SelectionDisabled = FALSE;
g_SelectedNode = 0;
g_fServerBrowsable = FALSE;
g_pNodeList = 0;
g_hWnd = 0;
InitializeCriticalSection(&g_csLock);
g_fTransferInProgress = FALSE;
#ifdef SDK_BUILD
g_hAygShellDll = LoadLibrary (L"aygshell.dll");
#endif
//initialize COM
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
return 1;
hr = CoCreateInstance(CLSID_Obex, NULL, CLSCTX_INPROC_SERVER, IID_IObex,
(void **)&g_pObex);
if((FAILED(hr)) || (!g_pObex) || (FAILED( g_pObex->Initialize())))
{
MessageBox(NULL, L"Error getting IObex object", L"Error", MB_OK);
CoUninitialize();
return 1;
}
//set device caps
IObexCaps *pObexCaps = NULL;
hr = g_pObex->QueryInterface(IID_IObexCaps, (LPVOID *)&pObexCaps);
if(SUCCEEDED(hr))
{
pObexCaps->SetCaps(SEND_DEVICE_UPDATES);
pObexCaps->Release();
}
wcscpy(szRequestDir, L"\\");
wcscpy(szStorageDir, L"\\My Documents");
//open our key
HKEY hkey;
DWORD cb, dwType, dwRes;
if ((ERROR_SUCCESS == (dwRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, k_szBrowserReg, 0, KEY_QUERY_VALUE, &hkey))))
{
//look for the value
cb=MAX_PATH * sizeof(WCHAR);
dwRes = RegQueryValueEx(hkey, L"RequestDir", NULL, &dwType, (LPBYTE)szRequestDir, &cb);
if ((dwRes != ERROR_SUCCESS) || (dwType != REG_SZ) || (cb >= (MAX_PATH*sizeof(WCHAR)) ) )
{
wcscpy(szRequestDir, L"\\");
}
//look for the value
cb=MAX_PATH * sizeof(WCHAR);
dwRes = RegQueryValueEx(hkey, L"StorageDir", NULL, &dwType, (LPBYTE)szStorageDir, &cb);
if ((dwRes != ERROR_SUCCESS) || (dwType != REG_SZ) || (cb >= (MAX_PATH*sizeof(WCHAR)) ) )
{
wcscpy(szStorageDir, L"\\My Documents");
}
RegCloseKey(hkey);
}
//setup information on the parent node
UINT uiDirSize = wcslen(szRequestDir) + 1;
memset(&g_ParentNode, 0, sizeof(DIR_NODE));
g_ParentNode.fDir = TRUE;
g_ParentNode.pPath = new TCHAR[uiDirSize];
g_ParentNode.pFileName = 0;
wcscpy(g_ParentNode.pPath, szRequestDir);
g_SelectedNode = &g_ParentNode;
//initilize the wndclass structure to contain
// information on the window
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC) MainProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = 0;
wndclass.hCursor = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//DKGRAY
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = _T("OBEXFileBrowser");
//register the window
if(!RegisterClass(&wndclass))
{
DWORD err = GetLastError();
MessageBox(0, TEXT("Error registering class"), NULL, MB_OK);
return 0;
}
wndclass.lpszClassName = _T("OBEXStatus");
wndclass.lpfnWndProc = (WNDPROC)StatusProc;
wndclass.lpszMenuName = 0;
//register the window
if(!RegisterClass(&wndclass))
{
DWORD err = GetLastError();
MessageBox(0, TEXT("Error registering class"), NULL, MB_OK);
return 0;
}
//create the main window
g_hWnd = CreateWindow(_T("OBEXFileBrowser"), _T("OBEXFileBrowser"),
WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
if(NULL == g_hWnd)
return -1;
//create the main window
g_hWndStatus = CreateWindow(_T("OBEXStatus"), _T("OBEXStatus"),
WS_BORDER | WS_POPUP,
15,116,210,85,
g_hWnd,NULL,hInstance,NULL);
if(NULL == g_hWndStatus)
return -1;
if(NULL == (g_hWndAbortButton = CreateWindow(_T("BUTTON"), _T("Abort"), WS_VISIBLE|WS_CHILD, 55, 50, 100,20,
g_hWndStatus, (HMENU) WM_ABORT, hInstance, NULL))) {
return -1;
}
//make sure the windows are visible
ShowWindow (g_hWnd, nShowCmd);
UpdateWindow(g_hWnd);
//When the main window is created using CW_USEDEFAULT the height of the
// menubar (if one is created is not taken into account). So we resize
//the window after creating it if a menubar is present
{
RECT rc;
GetWindowRect(g_hWnd, &rc);
rc.bottom -=(MENU_HEIGHT+25);
if (g_hwndCB)
MoveWindow(g_hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
}
//loop recieving and dispatching windows
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*****************************************************************************/
/*
/* Function: CreateRpCommandBar
/* Purpose: To place IDR_MENUBAR in the shell menu bar
/*
/*****************************************************************************/
HWND CreateRpCommandBar(HWND hWnd)
{
#if defined (WIN32_PLATFORM_PSPC)
BOOL (*local_SHCreateMenuBar)(SHMENUBARINFO *pmbi) = NULL;
if (g_hAygShellDll)
local_SHCreateMenuBar = (BOOL (*)(SHMENUBARINFO *))GetProcAddress (g_hAygShellDll, L"SHCreateMenuBar");
if (! local_SHCreateMenuBar)
return NULL;
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENUBAR;
mbi.hInstRes = g_hInst;
mbi.nBmpId = 0;
mbi.cBmpImages = 0;
if (! local_SHCreateMenuBar(&mbi))
MessageBox(NULL, L"ERROR", L"ERROR", MB_OK);
return mbi.hwndMB;
#else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -