vfs: check userland buffers before reading them.
[haiku.git] / src / libs / print / libprint / AddPrinterDlg.cpp
blobbffab740eb37c9863470f6870354d7149a1a9d2a
1 #include "AddPrinterDlg.h"
3 #include <GroupLayoutBuilder.h>
5 #include "PrinterCap.h"
6 #include "PrinterData.h"
9 enum MSGS {
10 kMsgCancel = 1,
11 kMsgOK,
12 kMsgProtocolClassChanged,
16 ProtocolClassItem::ProtocolClassItem(const ProtocolClassCap* cap)
17 : BStringItem(cap->fLabel.c_str())
18 , fProtocolClassCap(cap)
23 int
24 ProtocolClassItem::GetProtocolClass() const
26 return fProtocolClassCap->fProtocolClass;
29 const char *
30 ProtocolClassItem::GetDescription() const
32 return fProtocolClassCap->fDescription.c_str();
36 AddPrinterView::AddPrinterView(PrinterData* printerData,
37 const PrinterCap* printerCap)
39 BView("addPrinter", B_WILL_DRAW),
40 fPrinterData(printerData),
41 fPrinterCap(printerCap)
46 AddPrinterView::~AddPrinterView()
51 void
52 AddPrinterView::AttachedToWindow()
54 // protocol class box
55 BBox* protocolClassBox = new BBox("protocolClass");
56 protocolClassBox->SetLabel("Protocol classes:");
58 // protocol class
59 fProtocolClassList = new BListView("protocolClassList");
60 fProtocolClassList->SetExplicitMinSize(BSize(500, 200));
61 BScrollView* protocolClassScroller = new BScrollView(
62 "protocolClassListScroller",
63 fProtocolClassList,
65 false,
66 true,
67 B_NO_BORDER);
68 fProtocolClassList->SetSelectionMessage(
69 new BMessage(kMsgProtocolClassChanged));
70 fProtocolClassList->SetTarget(this);
72 protocolClassBox->AddChild(protocolClassScroller);
74 int count = fPrinterCap->CountCap(PrinterCap::kProtocolClass);
75 ProtocolClassCap **protocolClasses =
76 (ProtocolClassCap **)fPrinterCap->GetCaps(PrinterCap::kProtocolClass);
77 while (count--) {
78 const ProtocolClassCap *protocolClass = *protocolClasses;
80 BStringItem* item = new ProtocolClassItem(protocolClass);
81 fProtocolClassList->AddItem(item);
82 if (protocolClass->fIsDefault) {
83 int index = fProtocolClassList->IndexOf(item);
84 fProtocolClassList->Select(index);
86 protocolClasses ++;
89 // description of protocol class box
90 BBox* descriptionBox = new BBox("descriptionBox");
91 descriptionBox->SetLabel("Description:");
93 // description of protocol class
94 fDescription = new BTextView("description");
95 fDescription->SetExplicitMinSize(BSize(200, 200));
96 fDescription->SetViewColor(descriptionBox->ViewColor());
97 BScrollView* descriptionScroller = new BScrollView("descriptionScroller",
98 fDescription,
100 false,
101 true,
102 B_NO_BORDER);
103 fDescription->MakeEditable(false);
105 descriptionBox->AddChild(descriptionScroller);
107 // separator line
108 BBox *separator = new BBox("separator");
109 separator->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
111 // buttons
112 BButton* cancel = new BButton("cancel", "Cancel",
113 new BMessage(kMsgCancel));
114 BButton* ok = new BButton("ok", "OK", new BMessage(kMsgOK));
115 ok->MakeDefault(true);
117 SetLayout(new BGroupLayout(B_VERTICAL));
118 AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
119 .Add(protocolClassBox)
120 .Add(descriptionBox)
121 .AddGlue()
122 .Add(separator)
123 .AddGroup(B_HORIZONTAL, 10, 1.0f)
124 .AddGlue()
125 .Add(cancel)
126 .Add(ok)
127 .End()
128 .SetInsets(0, 0, 0, 0)
132 // update description
133 BMessage updateDescription(kMsgProtocolClassChanged);
134 MessageReceived(&updateDescription);
138 ProtocolClassItem*
139 AddPrinterView::CurrentSelection()
141 int selected = fProtocolClassList->CurrentSelection();
142 if (selected >= 0) {
143 return (ProtocolClassItem*)fProtocolClassList->ItemAt(selected);
145 return NULL;
149 void
150 AddPrinterView::MessageReceived(BMessage* msg)
152 if (msg->what == kMsgProtocolClassChanged) {
153 ProtocolClassItem *item = CurrentSelection();
154 if (item != NULL) {
155 fDescription->SetText(item->GetDescription());
157 } else {
158 BView::MessageReceived(msg);
163 void
164 AddPrinterView::Save()
166 ProtocolClassItem* item = CurrentSelection();
167 if (item != NULL) {
168 fPrinterData->SetProtocolClass(item->GetProtocolClass());
169 fPrinterData->Save();
174 AddPrinterDlg::AddPrinterDlg(PrinterData* printerData,
175 const PrinterCap *printerCap)
177 DialogWindow(BRect(100, 100, 120, 120),
178 "Add printer", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
179 B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS
180 | B_AUTO_UPDATE_SIZE_LIMITS)
182 SetResult(B_ERROR);
184 fAddPrinterView = new AddPrinterView(printerData, printerCap);
185 SetLayout(new BGroupLayout(B_VERTICAL));
186 AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
187 .Add(fAddPrinterView)
188 .SetInsets(10, 10, 10, 10)
193 void
194 AddPrinterDlg::MessageReceived(BMessage* msg)
196 switch (msg->what) {
197 case kMsgOK:
198 fAddPrinterView->Save();
199 SetResult(B_NO_ERROR);
200 PostMessage(B_QUIT_REQUESTED);
201 break;
203 case kMsgCancel:
204 PostMessage(B_QUIT_REQUESTED);
205 break;
207 default:
208 DialogWindow::MessageReceived(msg);
209 break;