2 * Copyright 2001-2010, Haiku.
3 * Distributed under the terms of the MIT License.
10 #include "JobListView.h"
20 #include "pr_server.h"
24 #include "SpoolFolder.h"
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "JobListView"
31 // #pragma mark -- JobListView
34 JobListView::JobListView(BRect frame
)
36 Inherited(frame
, "jobs_list", B_SINGLE_SELECTION_LIST
, B_FOLLOW_ALL
,
37 B_WILL_DRAW
| B_FRAME_EVENTS
| B_NAVIGABLE
| B_FULL_UPDATE_ON_RESIZE
)
42 JobListView::~JobListView()
45 delete RemoveItem((int32
)0);
50 JobListView::AttachedToWindow()
52 Inherited::AttachedToWindow();
54 SetSelectionMessage(new BMessage(kMsgJobSelected
));
60 JobListView::SetSpoolFolder(SpoolFolder
* folder
)
64 delete RemoveItem((int32
)0);
69 // Find directory containing printer definition nodes
70 for (int32 i
= 0; i
< folder
->CountJobs(); i
++)
71 AddJob(folder
->JobAt(i
));
76 JobListView::FindJob(Job
* job
) const
78 const int32 n
= CountItems();
79 for (int32 i
= 0; i
< n
; i
++) {
80 JobItem
* item
= dynamic_cast<JobItem
*>(ItemAt(i
));
81 if (item
&& item
->GetJob() == job
)
89 JobListView::SelectedItem() const
91 return dynamic_cast<JobItem
*>(ItemAt(CurrentSelection()));
96 JobListView::AddJob(Job
* job
)
98 AddItem(new JobItem(job
));
104 JobListView::RemoveJob(Job
* job
)
106 JobItem
* item
= FindJob(job
);
116 JobListView::UpdateJob(Job
* job
)
118 JobItem
* item
= FindJob(job
);
121 InvalidateItem(IndexOf(item
));
127 JobListView::RestartJob()
129 JobItem
* item
= SelectedItem();
130 if (item
&& item
->GetJob()->Status() == kFailed
) {
131 // setting the state changes the file attribute and
132 // we will receive a notification from SpoolFolder
133 item
->GetJob()->SetStatus(kWaiting
);
139 JobListView::CancelJob()
141 JobItem
* item
= SelectedItem();
142 if (item
&& item
->GetJob()->Status() != kProcessing
) {
143 item
->GetJob()->SetStatus(kFailed
);
144 item
->GetJob()->Remove();
149 // #pragma mark -- JobItem
152 JobItem::JobItem(Job
* job
)
173 BNode
node(&fJob
->EntryRef());
174 if (node
.InitCheck() != B_OK
)
177 node
.ReadAttrString(PSRV_SPOOL_ATTR_DESCRIPTION
, &fName
);
180 node
.ReadAttrString(PSRV_SPOOL_ATTR_MIMETYPE
, &mimeType
);
183 if (fIcon
== NULL
&& be_roster
->FindApp(mimeType
.String(), &ref
) == B_OK
) {
184 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
185 font_height fontHeight
;
186 be_plain_font
->GetHeight(&fontHeight
);
187 float height
= (fontHeight
.ascent
+ fontHeight
.descent
188 + fontHeight
.leading
) * 2.0;
189 BRect
rect(0, 0, height
, height
);
190 fIcon
= new BBitmap(rect
, B_RGBA32
);
192 BRect
rect(0, 0, B_MINI_ICON
- 1, B_MINI_ICON
- 1);
193 fIcon
= new BBitmap(rect
, B_CMAP8
);
195 BMimeType
type(mimeType
.String());
196 if (type
.GetIcon(fIcon
, B_MINI_ICON
) != B_OK
) {
204 if (node
.ReadAttr(PSRV_SPOOL_ATTR_PAGECOUNT
,
205 B_INT32_TYPE
, 0, &pages
, sizeof(pages
)) == sizeof(pages
)) {
208 fPages
<< " " << B_TRANSLATE("pages") << ".";
210 fPages
<< " " << B_TRANSLATE("page") << ".";
212 fPages
<< "??? " << B_TRANSLATE("pages") << ".";
217 if (node
.GetSize(&size
) == B_OK
) {
219 snprintf(buffer
, sizeof(buffer
), B_TRANSLATE("%.2f KB"),
225 switch (fJob
->Status()) {
227 fStatus
= B_TRANSLATE("Waiting");
231 fStatus
= B_TRANSLATE("Processing");
235 fStatus
= B_TRANSLATE("Failed");
239 fStatus
= B_TRANSLATE("Completed");
243 fStatus
= B_TRANSLATE("Unknown status");
249 JobItem::Update(BView
*owner
, const BFont
*font
)
251 BListItem::Update(owner
, font
);
254 font
->GetHeight(&height
);
256 SetHeight((height
.ascent
+ height
.descent
+ height
.leading
) * 2.0 + 8.0);
261 JobItem::DrawItem(BView
*owner
, BRect
, bool complete
)
263 BListView
* list
= dynamic_cast<BListView
*>(owner
);
266 owner
->GetFont(&font
);
269 font
.GetHeight(&height
);
270 float fntheight
= height
.ascent
+ height
.descent
+ height
.leading
;
272 BRect bounds
= list
->ItemFrame(list
->IndexOf(this));
274 rgb_color color
= owner
->ViewColor();
275 rgb_color oldLowColor
= owner
->LowColor();
276 rgb_color oldHighColor
= owner
->HighColor();
279 color
= ui_color(B_LIST_SELECTED_BACKGROUND_COLOR
);
281 owner
->SetHighColor(color
);
282 owner
->SetLowColor(color
);
284 owner
->FillRect(bounds
);
286 owner
->SetLowColor(oldLowColor
);
287 owner
->SetHighColor(oldHighColor
);
289 BPoint
iconPt(bounds
.LeftTop() + BPoint(2.0, 2.0));
290 float iconHeight
= B_MINI_ICON
;
291 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
293 iconHeight
= fIcon
->Bounds().Height();
295 BPoint
leftTop(bounds
.LeftTop() + BPoint(12.0 + iconHeight
, 2.0));
296 BPoint
namePt(leftTop
+ BPoint(0.0, fntheight
));
297 BPoint
statusPt(leftTop
+ BPoint(0.0, fntheight
* 2.0));
299 float x
= owner
->StringWidth(fPages
.String()) + 32.0;
300 BPoint
pagePt(bounds
.RightTop() + BPoint(-x
, fntheight
));
301 BPoint
sizePt(bounds
.RightTop() + BPoint(-x
, fntheight
* 2.0));
303 drawing_mode mode
= owner
->DrawingMode();
304 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
305 owner
->SetDrawingMode(B_OP_ALPHA
);
307 owner
->SetDrawingMode(B_OP_OVER
);
311 owner
->DrawBitmap(fIcon
, iconPt
);
314 BString name
= fName
;
315 owner
->TruncateString(&name
, B_TRUNCATE_MIDDLE
, pagePt
.x
- namePt
.x
);
316 owner
->DrawString(name
.String(), name
.Length(), namePt
);
317 BString status
= fStatus
;
318 owner
->TruncateString(&status
, B_TRUNCATE_MIDDLE
, sizePt
.x
- statusPt
.x
);
319 owner
->DrawString(status
.String(), status
.Length(), statusPt
);
322 owner
->DrawString(fPages
.String(), fPages
.Length(), pagePt
);
323 owner
->DrawString(fSize
.String(), fSize
.Length(), sizePt
);
325 owner
->SetDrawingMode(mode
);