1// PrinterSharingDlg.cpp : implementation file
2//
3
4#include "stdafx.h"
5
6#include "ctype.h"
7#include "sys/types.h"
8#include "sys/stat.h"
9
10#include "becompat.h"
11#include "betalk.h"
12
13#include "FileSharing.h"
14#include "PrinterSharingDlg.h"
15#include "PrinterProperties.h"
16#include "printing.h"
17#include "Security.h"
18
19#ifdef _DEBUG
20#define new DEBUG_NEW
21#undef THIS_FILE
22static char THIS_FILE[] = __FILE__;
23#endif
24
25extern bt_printer sharedPrinters[];
26extern char authServerName[];
27extern unsigned int authServerIP;
28
29extern char tokBuffer[MAX_NAME_LENGTH + 1], *tokPtr;
30extern char *keywords[];
31
32
33/////////////////////////////////////////////////////////////////////////////
34// CPrinterSharingDlg dialog
35
36CPrinterSharingDlg::CPrinterSharingDlg()
37	: CPropertyPage(CPrinterSharingDlg::IDD)
38{
39	//{{AFX_DATA_INIT(CPrinterSharingDlg)
40	//}}AFX_DATA_INIT
41	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
42	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
43}
44
45void CPrinterSharingDlg::DoDataExchange(CDataExchange* pDX)
46{
47	CPropertyPage::DoDataExchange(pDX);
48	//{{AFX_DATA_MAP(CPrinterSharingDlg)
49	DDX_Control(pDX, IDC_MAIN_HEADING, m_heading);
50	DDX_Control(pDX, IDC_PRINTERS, m_printerList);
51	DDX_Control(pDX, IDC_REMOVE_PRINTER, m_removeBtn);
52	DDX_Control(pDX, IDC_EDIT_PRINTER, m_editBtn);
53	//}}AFX_DATA_MAP
54}
55
56BEGIN_MESSAGE_MAP(CPrinterSharingDlg, CPropertyPage)
57	//{{AFX_MSG_MAP(CPrinterSharingDlg)
58	ON_WM_PAINT()
59	ON_WM_QUERYDRAGICON()
60	ON_NOTIFY(NM_CLICK, IDC_PRINTERS, OnClickPrinters)
61	ON_NOTIFY(NM_DBLCLK, IDC_PRINTERS, OnDblclkPrinters)
62	ON_BN_CLICKED(IDC_EDIT_PRINTER, OnEditPrinter)
63	ON_BN_CLICKED(IDC_REMOVE_PRINTER, OnRemovePrinter)
64	ON_BN_CLICKED(IDC_NEW_PRINTER, OnNewPrinter)
65	ON_BN_CLICKED(IDC_SECURITY, OnSecurity)
66	//}}AFX_MSG_MAP
67END_MESSAGE_MAP()
68
69/////////////////////////////////////////////////////////////////////////////
70// CPrinterSharingDlg message handlers
71
72BOOL CPrinterSharingDlg::OnInitDialog()
73{
74	CPropertyPage::OnInitDialog();
75
76	// Set the icon for this dialog.  The framework does this automatically
77	//  when the application's main window is not a dialog
78	SetIcon(m_hIcon, TRUE);			// Set big icon
79	SetIcon(m_hIcon, FALSE);		// Set small icon
80
81	m_editBtn.EnableWindow(FALSE);
82	m_removeBtn.EnableWindow(FALSE);
83
84	m_printerList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
85	m_printerList.InsertColumn(0, "Printer Name", LVCFMT_LEFT, 120);
86	m_printerList.InsertColumn(1, "Local Device", LVCFMT_LEFT, 120);
87	m_printerList.InsertColumn(2, "Queue Path", LVCFMT_LEFT, 200);
88
89	CBitmap printer;
90	printer.LoadBitmap(IDB_USER);
91
92	m_images.Create(16, 16, FALSE, 2, 0);
93	m_images.Add(&printer, (COLORREF) 0);
94	m_printerList.SetImageList(&m_images, LVSIL_SMALL);
95
96	m_hBoldFont = InitializeControlFont("Tahoma", FW_BOLD, 8);
97	m_heading.SendMessage(WM_SETFONT, (WPARAM) m_hBoldFont, MAKELPARAM(TRUE, 0));
98
99	ListPrinters();
100
101	return TRUE;
102}
103
104// If you add a minimize button to your dialog, you will need the code below
105//  to draw the icon.  For MFC applications using the document/view model,
106//  this is automatically done for you by the framework.
107
108void CPrinterSharingDlg::OnPaint()
109{
110	if (IsIconic())
111	{
112		CPaintDC dc(this); // device context for painting
113
114		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
115
116		// Center icon in client rectangle
117		int cxIcon = GetSystemMetrics(SM_CXICON);
118		int cyIcon = GetSystemMetrics(SM_CYICON);
119		CRect rect;
120		GetClientRect(&rect);
121		int x = (rect.Width() - cxIcon + 1) / 2;
122		int y = (rect.Height() - cyIcon + 1) / 2;
123
124		// Draw the icon
125		dc.DrawIcon(x, y, m_hIcon);
126	}
127	else
128	{
129		CPropertyPage::OnPaint();
130	}
131}
132
133// The system calls this to obtain the cursor to display while the user drags
134//  the minimized window.
135HCURSOR CPrinterSharingDlg::OnQueryDragIcon()
136{
137	return (HCURSOR) m_hIcon;
138}
139
140void CPrinterSharingDlg::OnClickPrinters(NMHDR* pNMHDR, LRESULT* pResult)
141{
142	*pResult = 0;
143	m_editBtn.EnableWindow(GetSelectedListItem(&m_printerList) != -1);
144	m_removeBtn.EnableWindow(GetSelectedListItem(&m_printerList) != -1);
145}
146
147void CPrinterSharingDlg::OnDblclkPrinters(NMHDR* pNMHDR, LRESULT* pResult)
148{
149	*pResult = 0;
150	OnEditPrinter();
151}
152
153void CPrinterSharingDlg::OnEditPrinter()
154{
155	CPrinterProperties printer;
156	int nItem = GetSelectedListItem(&m_printerList);
157	if (nItem != -1)
158		if (printer.ShowProperties(m_printerList.GetItemData(nItem)) == IDOK)
159			RefreshPrinter(nItem);
160}
161
162void CPrinterSharingDlg::OnRemovePrinter()
163{
164	int nItem = GetSelectedListItem(&m_printerList);
165	if (nItem >= 0)
166		if (MessageBox(WARN_REMOVE_PRINTER, sharedPrinters[nItem].printerName, MB_YESNO | MB_ICONQUESTION) == IDYES)
167		{
168			sharedPrinters[nItem].used = false;
169			m_printerList.DeleteItem(nItem);
170		}
171}
172
173void CPrinterSharingDlg::OnNewPrinter()
174{
175	CPrinterProperties printer;
176	struct stat st;
177	int nIndex;
178
179	for (nIndex = 0; nIndex < BT_MAX_PRINTER_SHARES; nIndex++)
180		if (!sharedPrinters[nIndex].used)
181			break;
182
183	if (nIndex == BT_MAX_PRINTER_SHARES)
184		return;
185
186	strcpy(sharedPrinters[nIndex].printerName, "Untitled");
187	strcpy(sharedPrinters[nIndex].spoolDir, "c:\\temp");
188	if (printer.ShowProperties(nIndex) == IDOK)
189	{
190		int nItem = m_printerList.InsertItem(0, sharedPrinters[nIndex].printerName, 0);
191		m_printerList.SetItemText(nItem, 1, sharedPrinters[nIndex].deviceName);
192		m_printerList.SetItemText(nItem, 2, stat(sharedPrinters[nIndex].spoolDir, &st) == 0 ? sharedPrinters[nIndex].spoolDir : "--- Invalid Path ---");
193		m_printerList.SetItemData(nItem, nIndex);
194		sharedPrinters[nIndex].used = true;
195	}
196}
197
198void CPrinterSharingDlg::OnSecurity()
199{
200	CSecurity security;
201	security.DoModal();
202}
203
204// ListPrinters()
205//
206void CPrinterSharingDlg::ListPrinters()
207{
208	struct stat st;
209	int i, nItem;
210
211	for (i = 0; i < BT_MAX_PRINTER_SHARES; i++)
212		if (sharedPrinters[i].used)
213		{
214			nItem = m_printerList.InsertItem(0, sharedPrinters[i].printerName, 0);
215			m_printerList.SetItemText(nItem, 1, sharedPrinters[i].deviceName);
216			m_printerList.SetItemText(nItem, 2, stat(sharedPrinters[i].spoolDir, &st) == 0 ? sharedPrinters[i].spoolDir : "--- Invalid Path ---");
217			m_printerList.SetItemData(nItem, i);
218		}
219}
220
221void CPrinterSharingDlg::RefreshPrinter(int nItem)
222{
223	struct stat st;
224	int nIndex = m_printerList.GetItemData(nItem);
225	m_printerList.SetItemText(nItem, 0, sharedPrinters[nItem].printerName);
226	m_printerList.SetItemText(nItem, 1, sharedPrinters[nItem].deviceName);
227	m_printerList.SetItemText(nItem, 2, stat(sharedPrinters[nItem].spoolDir, &st) == 0 ? sharedPrinters[nItem].spoolDir : "--- Invalid Path ---");
228}
229
230// WritePrinters()
231//
232void CPrinterSharingDlg::WritePrinters(FILE *fp)
233{
234	bt_user_rights *ur;
235	int i;
236
237	if (fp)
238	{
239		for (i = 0; i < BT_MAX_PRINTER_SHARES; i++)
240			if (sharedPrinters[i].used)
241			{
242				fprintf(fp, "printer ");
243				PrintString(fp, sharedPrinters[i].printerName);
244				fprintf(fp, " is type ");
245				PrintString(fp, sharedPrinters[i].deviceType);
246				fprintf(fp, " device ");
247				PrintString(fp, sharedPrinters[i].deviceName);
248				fprintf(fp, " spooled to ");
249				PrintString(fp, sharedPrinters[i].spoolDir);
250				fputc('\n', fp);
251
252				for (ur = sharedPrinters[i].rights; ur; ur = ur->next)
253					if (ur->rights & BT_RIGHTS_PRINT)
254					{
255						fprintf(fp, "grant print on ");
256						PrintString(fp, sharedPrinters[i].printerName);
257						fprintf(fp, " to ");
258						if (ur->isGroup)
259							fprintf(fp, "group ");
260						fprintf(fp, "%s\n", ur->user);
261					}
262			}
263	}
264}
265