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