vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / print / drivers / gutenprint / SelectPrinterDialog.cpp
blob2b88b2440e828cc20f97451cce0f341a88971f9a
1 /*
2 * Copyright 2010, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ithamar R. Adema <ithamar.adema@team-embedded.nl>
7 * Michael Pfeiffer
8 */
11 #include "SelectPrinterDialog.h"
13 #include <GroupLayout.h>
14 #include <GroupLayoutBuilder.h>
16 #include <Button.h>
17 #include <Debug.h>
18 #include <Directory.h>
19 #include <Entry.h>
20 #include <ListView.h>
21 #include <Message.h>
22 #include <Path.h>
23 #include <ScrollBar.h>
24 #include <ScrollView.h>
25 #include <String.h>
26 #include <StringItem.h>
28 #include "GPBinding.h"
31 enum {
32 kMsgCancel = 'stop',
33 kMsgOK = 'okok',
35 kMsgManufacturerSelected = 'msel',
36 kMsgPrinterSelected = 'psel',
40 class StringValueItem : public BStringItem {
41 public:
42 StringValueItem(const BString& text, const BString& value)
44 BStringItem(text.String()),
45 fValue(value)
49 BString fValue;
53 SelectPrinterDialog::SelectPrinterDialog(GPData* data)
55 DialogWindow(BRect(100, 100, 500, 500),
56 "Select Printer", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
57 B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS
58 | B_CLOSE_ON_ESCAPE),
59 fData(data)
61 SetResult(B_ERROR);
63 BButton* ok;
64 BButton* cancel;
66 ok = new BButton("btn:ok", "OK", new BMessage(kMsgOK));
67 ok->MakeDefault(true);
68 ok->SetEnabled(false);
69 fOKButton = ok;
71 cancel = new BButton("btn:cancel", "Cancel", new BMessage(kMsgCancel));
73 BScrollView* manuScroller, *printerScroller;
74 fManufacturersListView = new BListView("olv:manufacturers");
75 manuScroller = new BScrollView("scr:manufacturers", fManufacturersListView,
76 0, false, true);
77 fPrintersListView = new BListView("olv:printers");
78 printerScroller = new BScrollView("scr:printers", fPrintersListView, 0,
79 false, true);
81 fPrintersListView->SetSelectionMessage(new BMessage(kMsgPrinterSelected));
82 fManufacturersListView->SetSelectionMessage(
83 new BMessage(kMsgManufacturerSelected));
84 PopulateManufacturers();
86 // Build the layout
87 SetLayout(new BGroupLayout(B_VERTICAL));
89 AddChild(BGroupLayoutBuilder(B_VERTICAL, 10)
90 .AddGroup(B_HORIZONTAL, 5, 1.0f)
91 .Add(manuScroller)
92 .Add(printerScroller)
93 .End()
94 .AddGroup(B_HORIZONTAL, 5, 2.0f)
95 .AddGlue()
96 .Add(cancel)
97 .Add(ok)
98 .End()
99 .SetInsets(10, 10, 10, 10)
104 void
105 SelectPrinterDialog::PopulateManufacturers()
107 BMessage manufacturers;
108 GPBinding binding;
109 binding.GetPrinterManufacturers(manufacturers);
111 BString id;
112 BString displayName;
113 for (int32 index = 0; binding.ExtractManufacturer(manufacturers, index, id,
114 displayName); index ++) {
115 fManufacturersListView->AddItem(new StringValueItem(id, displayName));
120 void
121 SelectPrinterDialog::PopulatePrinters()
123 BString manufacturer = GetSelectedItemValue(fManufacturersListView).String();
124 if (manufacturer.Length() == 0)
125 return;
127 BMessage models;
128 GPBinding binding;
129 binding.GetPrinterModels(manufacturer, models);
131 BString displayName;
132 BString driver;
133 for (int32 index = 0; binding.ExtractModel(models, index, displayName, driver);
134 index ++) {
135 StringValueItem* item = new StringValueItem(displayName, driver);
136 fPrintersListView->AddItem(item);
141 BString
142 SelectPrinterDialog::GetSelectedItemValue(BListView* listView)
144 int32 index = listView->CurrentSelection();
146 // Bail out if no manufacturer is selected
147 if (index < 0)
148 return "";
149 BListItem* item = listView->ItemAt(index);
150 StringValueItem* valueItem = dynamic_cast<StringValueItem*>(item);
151 ASSERT(valueItem != NULL);
153 return valueItem->fValue;
156 void
157 SelectPrinterDialog::PrinterSelected()
159 int32 index = fPrintersListView->CurrentSelection();
160 fOKButton->SetEnabled(index >= 0);
164 void
165 SelectPrinterDialog::Save()
167 BString driverName = GetSelectedItemValue(fPrintersListView);
168 fData->fGutenprintDriverName = driverName;
169 fData->Save();
173 void
174 SelectPrinterDialog::MessageReceived(BMessage* msg)
176 switch (msg->what) {
177 case kMsgManufacturerSelected:
178 fPrintersListView->MakeEmpty();
179 PopulatePrinters();
180 PrinterSelected();
181 break;
182 case kMsgPrinterSelected:
183 PrinterSelected();
184 break;
185 case kMsgOK:
186 Save();
187 SetResult(B_NO_ERROR);
188 PostMessage(B_QUIT_REQUESTED);
189 break;
190 case kMsgCancel:
191 PostMessage(B_QUIT_REQUESTED);
192 break;
193 default:
194 DialogWindow::MessageReceived(msg);
195 break;