* remove and delete listview items

* removed unmaintained fNode var from listview
* don't exit just in case we can't load the icons,
  instead draw an string what the default printer is
* call create_directory only if the printers dir does not exist



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25120 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Karsten Heimrich 2008-04-23 22:32:14 +00:00
parent 0babd6b999
commit ccf92546c4
2 changed files with 192 additions and 176 deletions

View File

@ -41,7 +41,9 @@
#include <Mime.h>
#include <StorageKit.h>
// Implementation of PrinterListView
// #pragma mark -- PrinterListView
PrinterListView::PrinterListView(BRect frame)
: Inherited(frame, "printers_list", B_SINGLE_SELECTION_LIST, B_FOLLOW_ALL),
@ -51,27 +53,29 @@ PrinterListView::PrinterListView(BRect frame)
}
PrinterListView::~PrinterListView()
{
while (!IsEmpty())
delete RemoveItem(0L);
}
void
PrinterListView::BuildPrinterList()
{
// clear list
const BListItem** items = Items();
for (int32 i = CountItems()-1; i >= 0; i --)
delete items[i];
MakeEmpty();
while (!IsEmpty())
delete RemoveItem(0L);
// Find directory containing printer definition nodes
BPath path;
status_t rc = ::find_directory(B_USER_PRINTERS_DIRECTORY, &path);
if (rc != B_OK)
if (find_directory(B_USER_PRINTERS_DIRECTORY, &path) != B_OK)
return;
BDirectory dir(path.Path());
rc = dir.InitCheck();
if (rc != B_OK)
if (dir.InitCheck() != B_OK)
return;
fNode = dir;
BEntry entry;
while(dir.GetNextEntry(&entry) == B_OK) {
BDirectory printer(&entry);
@ -90,13 +94,20 @@ PrinterListView::AttachedToWindow()
SetTarget(Window());
BPath path;
bool hasPrintersDirectory = find_directory(B_USER_PRINTERS_DIRECTORY, &path) == B_OK;
if (find_directory(B_USER_PRINTERS_DIRECTORY, &path) != B_OK)
return;
if (hasPrintersDirectory) {
create_directory(path.Path(), 0777);
// directory has to exist in order to start watching it
BDirectory dir(path.Path());
if (dir.InitCheck() != B_OK) {
// directory has to exist in order to start watching it
if (create_directory(path.Path(), 0777) != B_OK)
return;
dir.SetTo(path.Path());
}
fFolder = new FolderWatcher(Window(), dir, true);
fFolder->SetListener(this);
BuildPrinterList();
// Select active printer
@ -109,21 +120,13 @@ PrinterListView::AttachedToWindow()
break;
}
}
if (hasPrintersDirectory) {
BDirectory dir(path.Path());
if (dir.InitCheck() == B_OK) {
fFolder = new FolderWatcher(this->Window(), dir, true);
fFolder->SetListener(this);
}
}
}
bool
PrinterListView::QuitRequested()
{
delete fFolder; fFolder = NULL;
delete fFolder;
return true;
}
@ -134,17 +137,17 @@ PrinterListView::AddPrinter(BDirectory& printer)
BString state;
node_ref node;
// If the entry is a directory
if (printer.InitCheck() == B_OK &&
printer.GetNodeRef(&node) == B_OK &&
FindItem(&node) == NULL &&
printer.ReadAttrString(PSRV_PRINTER_ATTR_STATE, &state) == B_OK &&
state == "free") {
if (printer.InitCheck() == B_OK
&& printer.GetNodeRef(&node) == B_OK
&& FindItem(&node) == NULL
&& printer.ReadAttrString(PSRV_PRINTER_ATTR_STATE, &state) == B_OK
&& state == "free") {
// Check it's Mime type for a spool director
BNodeInfo info(&printer);
char buffer[256];
if (info.GetType(buffer) == B_OK &&
strcmp(buffer, PSRV_PRINTER_FILETYPE) == 0) {
if (info.GetType(buffer) == B_OK
&& strcmp(buffer, PSRV_PRINTER_FILETYPE) == 0) {
// Yes, it is a printer definition node
AddItem(new PrinterItem(dynamic_cast<PrintersWindow*>(Window()), printer));
}
@ -152,7 +155,8 @@ PrinterListView::AddPrinter(BDirectory& printer)
}
PrinterItem* PrinterListView::FindItem(node_ref* node)
PrinterItem*
PrinterListView::FindItem(node_ref* node) const
{
for (int32 i = CountItems()-1; i >= 0; i --) {
PrinterItem* item = dynamic_cast<PrinterItem*>(ItemAt(i));
@ -185,6 +189,7 @@ void PrinterListView::EntryRemoved(node_ref* node)
}
}
void
PrinterListView::AttributeChanged(node_ref* node)
{
@ -192,6 +197,7 @@ PrinterListView::AttributeChanged(node_ref* node)
AddPrinter(printer);
}
void
PrinterListView::UpdateItem(PrinterItem* item)
{
@ -199,10 +205,11 @@ PrinterListView::UpdateItem(PrinterItem* item)
InvalidateItem(IndexOf(item));
}
PrinterItem* PrinterListView::SelectedItem()
PrinterItem*
PrinterListView::SelectedItem() const
{
BListItem* item = ItemAt(CurrentSelection());
return dynamic_cast<PrinterItem*>(item);
return dynamic_cast<PrinterItem*>(ItemAt(CurrentSelection()));
}
@ -220,6 +227,9 @@ PrinterListView::SetActivePrinter(PrinterItem* item)
}
// #pragma mark -- PrinterItem
BBitmap* PrinterItem::sIcon = NULL;
BBitmap* PrinterItem::sSelectedIcon = NULL;
@ -229,47 +239,36 @@ PrinterItem::PrinterItem(PrintersWindow* window, const BDirectory& node)
fFolder(NULL),
fNode(node)
{
BRect rect(0, 0, B_LARGE_ICON - 1, B_LARGE_ICON - 1);
if (sIcon == NULL) {
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
sIcon = new BBitmap(BRect(0,0,B_LARGE_ICON-1,B_LARGE_ICON-1), B_RGBA32);
sIcon = new BBitmap(rect, B_RGBA32);
#else
sIcon = new BBitmap(BRect(0,0,B_LARGE_ICON-1,B_LARGE_ICON-1), B_CMAP8);
sIcon = new BBitmap(rect, B_CMAP8);
#endif
BMimeType type(PSRV_PRINTER_FILETYPE);
type.GetIcon(sIcon, B_LARGE_ICON);
}
if (sSelectedIcon == NULL) {
if (sIcon && sIcon->IsValid() && sSelectedIcon == NULL) {
BBitmap *checkMark = LoadBitmap("check_mark_icon", 'BBMP');
if (checkMark == NULL) {
BAlert *alert = new BAlert("Error",
"Fatal error: Check mark icon could not be loaded from resources!",
"Bye");
alert->Go();
exit(1);
sSelectedIcon = new BBitmap(rect, B_RGBA32, true);
if (checkMark && checkMark->IsValid()
&& sSelectedIcon && sSelectedIcon->IsValid()) {
// draw check mark at bottom left over printer icon
BView *view = new BView(rect, "offscreen", B_FOLLOW_ALL, B_WILL_DRAW);
float y = rect.Height() - checkMark->Bounds().Height();
sSelectedIcon->Lock();
sSelectedIcon->AddChild(view);
view->DrawBitmap(sIcon);
view->SetDrawingMode(B_OP_ALPHA);
view->DrawBitmap(checkMark, BPoint(0, y));
view->Sync();
view->RemoveSelf();
sSelectedIcon->Unlock();
delete view;
delete checkMark;
}
// draw check mark at bottom left over printer icon
sSelectedIcon = new BBitmap(sIcon->Bounds(), B_RGBA32, true);
if (!sSelectedIcon->IsValid()) {
BAlert *alert = new BAlert("Error",
"Fatal error: Bitmap could not be created for selected printer icon!",
"Bye");
alert->Go();
exit(1);
}
BView *view = new BView(sIcon->Bounds(), "offscreen", B_FOLLOW_ALL, B_WILL_DRAW);
float y = sIcon->Bounds().Height() - checkMark->Bounds().Height();
sSelectedIcon->Lock();
sSelectedIcon->AddChild(view);
view->DrawBitmap(sIcon);
view->SetDrawingMode(B_OP_ALPHA);
view->DrawBitmap(checkMark, BPoint(0, y));
view->Sync();
view->RemoveSelf();
sSelectedIcon->Unlock();
delete view;
delete checkMark;
}
// Get Name of printer
@ -279,20 +278,22 @@ PrinterItem::PrinterItem(PrintersWindow* window, const BDirectory& node)
GetStringProperty(PSRV_PRINTER_ATTR_DRV_NAME, fDriverName);
BPath path;
if (find_directory(B_USER_PRINTERS_DIRECTORY, &path) != B_OK)
return;
// Setup spool folder
if (find_directory(B_USER_PRINTERS_DIRECTORY, &path) == B_OK) {
path.Append(fName.String());
BDirectory dir(path.Path());
if (dir.InitCheck() != B_OK) return;
path.Append(fName.String());
BDirectory dir(path.Path());
if (dir.InitCheck() == B_OK) {
fFolder = new SpoolFolder(window, this, dir);
UpdatePendingJobs();
}
UpdatePendingJobs();
}
PrinterItem::~PrinterItem()
{
delete fFolder; fFolder = NULL;
delete fFolder;
}
@ -311,24 +312,20 @@ PrinterItem::Update(BView *owner, const BFont *font)
font_height height;
font->GetHeight(&height);
SetHeight( (height.ascent+height.descent+height.leading) * 3.0 +4 );
SetHeight((height.ascent + height.descent + height.leading) * 3.0 + 8.0);
}
bool PrinterItem::Remove(BListView* view)
{
BMessenger msgr;
if (GetPrinterServerMessenger(msgr) == B_OK)
{
if (GetPrinterServerMessenger(msgr) == B_OK) {
BMessage script(B_DELETE_PROPERTY);
BMessage reply;
script.AddSpecifier("Printer", view->IndexOf(this));
if (msgr.SendMessage(&script,&reply) == B_OK) {
BMessage reply;
if (msgr.SendMessage(&script,&reply) == B_OK)
return true;
}
}
return false;
}
@ -340,36 +337,44 @@ void PrinterItem::DrawItem(BView *owner, BRect /*bounds*/, bool complete)
if (list == NULL)
return;
font_height height;
BFont font;
owner->GetFont(&font);
font_height height;
font.GetHeight(&height);
float fntheight = height.ascent+height.descent+height.leading;
float fntheight = height.ascent + height.descent + height.leading;
BRect bounds = list->ItemFrame(list->IndexOf(this));
rgb_color color = owner->ViewColor();
if ( IsSelected() )
rgb_color oldViewColor = color;
rgb_color oldLowColor = owner->LowColor();
rgb_color oldHighColor = owner->HighColor();
if (IsSelected())
color = tint_color(color, B_HIGHLIGHT_BACKGROUND_TINT);
rgb_color oldviewcolor = owner->ViewColor();
rgb_color oldlowcolor = owner->LowColor();
rgb_color oldcolor = owner->HighColor();
owner->SetViewColor( color );
owner->SetHighColor( color );
owner->SetLowColor( color );
owner->FillRect(bounds);
owner->SetLowColor( oldlowcolor );
owner->SetHighColor( oldcolor );
owner->SetViewColor(color);
owner->SetLowColor(color);
owner->SetHighColor(color);
BPoint iconPt = bounds.LeftTop() + BPoint(2,2);
BPoint namePt = iconPt + BPoint(B_LARGE_ICON+8, fntheight);
BPoint driverPt = iconPt + BPoint(B_LARGE_ICON+8, fntheight*2);
BPoint commentPt = iconPt + BPoint(B_LARGE_ICON+8, fntheight*3);
owner->FillRect(bounds);
owner->SetLowColor(oldLowColor);
owner->SetHighColor(oldHighColor);
float x = B_LARGE_ICON + 8.0;
BPoint iconPt(bounds.LeftTop() + BPoint(2.0, 2.0));
BPoint namePt(iconPt + BPoint(x, fntheight));
BPoint driverPt(iconPt + BPoint(x, fntheight * 2.0));
BPoint defaultPt(iconPt + BPoint(x, fntheight * 3.0));
float width = owner->StringWidth("No pending jobs.");
BPoint pendingPt(bounds.right - width -8, namePt.y);
BPoint transportPt(bounds.right - width -8, driverPt.y);
BPoint pendingPt(bounds.right - width - 8.0, namePt.y);
BPoint transportPt(bounds.right - width - 8.0, driverPt.y);
BPoint commentPt(bounds.right - width - 8.0, defaultPt.y);
drawing_mode mode = owner->DrawingMode();
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
@ -377,38 +382,48 @@ void PrinterItem::DrawItem(BView *owner, BRect /*bounds*/, bool complete)
#else
owner->SetDrawingMode(B_OP_OVER);
#endif
owner->DrawBitmap(IsActivePrinter() ? sSelectedIcon : sIcon, iconPt);
if (IsActivePrinter()) {
if (sSelectedIcon && sSelectedIcon->IsValid())
owner->DrawBitmap(sSelectedIcon, iconPt);
else
owner->DrawString("Default Printer", defaultPt);
} else {
if (sIcon && sIcon->IsValid())
owner->DrawBitmap(sIcon, iconPt);
}
owner->SetDrawingMode(B_OP_OVER);
// left of item
owner->DrawString(fName.String(), fName.Length(), namePt);
owner->DrawString(fDriverName.String(), fDriverName.Length(), driverPt);
owner->DrawString(fComments.String(), fComments.Length(), commentPt);
// right of item
owner->DrawString(fPendingJobs.String(), 16, pendingPt);
owner->DrawString(fTransport.String(), fTransport.Length(), transportPt);
owner->DrawString(fComments.String(), fComments.Length(), commentPt);
owner->SetDrawingMode(mode);
owner->SetViewColor(oldviewcolor);
owner->SetViewColor(oldViewColor);
}
bool
PrinterItem::IsActivePrinter()
PrinterItem::IsActivePrinter() const
{
return fName == ActivePrinterName();
}
bool
PrinterItem::HasPendingJobs()
PrinterItem::HasPendingJobs() const
{
return fFolder && fFolder->CountJobs() > 0;
}
SpoolFolder* PrinterItem::Folder()
SpoolFolder*
PrinterItem::Folder() const
{
return fFolder;
}
@ -430,7 +445,7 @@ PrinterItem::UpdatePendingJobs()
fPendingJobs = "1 pending job.";
return;
} else if (pendingJobs > 1) {
fPendingJobs << pendingJobs << " pending jobs.h";
fPendingJobs << pendingJobs << " pending jobs.";
return;
}
}

View File

@ -48,7 +48,7 @@ class PrinterListView : public BListView, public FolderListener {
typedef BListView Inherited;
void AddPrinter(BDirectory &printer);
PrinterItem *FindItem(node_ref *node);
PrinterItem *FindItem(node_ref *node) const;
void EntryCreated(node_ref *node, entry_ref *entry);
void EntryRemoved(node_ref *node);
@ -56,18 +56,19 @@ class PrinterListView : public BListView, public FolderListener {
public:
PrinterListView(BRect frame);
~PrinterListView();
void AttachedToWindow();
bool QuitRequested();
void BuildPrinterList();
PrinterItem *SelectedItem();
PrinterItem *SelectedItem() const;
void UpdateItem(PrinterItem *item);
PrinterItem* ActivePrinter() const;
void SetActivePrinter(PrinterItem* item);
private:
BDirectory fNode;
FolderWatcher *fFolder;
PrinterItem *fActivePrinter;
};
@ -83,14 +84,14 @@ class PrinterItem : public BListItem {
void Update(BView *owner, const BFont *font);
bool Remove(BListView *view);
bool IsActivePrinter();
bool HasPendingJobs();
bool IsActivePrinter() const;
bool HasPendingJobs() const;
const char *Name() const {
return fName.String();
}
SpoolFolder *Folder();
SpoolFolder *Folder() const;
BDirectory *Node();
void UpdatePendingJobs();