* 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

@ -1,8 +1,8 @@
/*****************************************************************************/
// Printers Preference Application.
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// Copyright (c) 2001-2003 OpenBeOS Project
@ -10,18 +10,18 @@
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
@ -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);
@ -80,23 +84,30 @@ PrinterListView::BuildPrinterList()
}
void
void
PrinterListView::AttachedToWindow()
{
Inherited::AttachedToWindow();
SetSelectionMessage(new BMessage(kMsgPrinterSelected));
SetInvocationMessage(new BMessage(kMsgMakeDefaultPrinter));
SetTarget(Window());
SetTarget(Window());
BPath path;
bool hasPrintersDirectory = find_directory(B_USER_PRINTERS_DIRECTORY, &path) == B_OK;
if (hasPrintersDirectory) {
create_directory(path.Path(), 0777);
// directory has to exist in order to start watching it
BPath path;
if (find_directory(B_USER_PRINTERS_DIRECTORY, &path) != B_OK)
return;
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,42 +120,34 @@ 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()
bool
PrinterListView::QuitRequested()
{
delete fFolder; fFolder = NULL;
delete fFolder;
return true;
}
void
PrinterListView::AddPrinter(BDirectory& printer)
void
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));
@ -165,15 +169,15 @@ PrinterItem* PrinterListView::FindItem(node_ref* node)
}
void
PrinterListView::EntryCreated(node_ref* node, entry_ref* entry)
void
PrinterListView::EntryCreated(node_ref* node, entry_ref* entry)
{
BDirectory printer(node);
AddPrinter(printer);
}
void PrinterListView::EntryRemoved(node_ref* node)
void PrinterListView::EntryRemoved(node_ref* node)
{
PrinterItem* item = FindItem(node);
if (item) {
@ -185,24 +189,27 @@ void PrinterListView::EntryRemoved(node_ref* node)
}
}
void
PrinterListView::AttributeChanged(node_ref* node)
void
PrinterListView::AttributeChanged(node_ref* node)
{
BDirectory printer(node);
AddPrinter(printer);
}
void
void
PrinterListView::UpdateItem(PrinterItem* item)
{
item->UpdatePendingJobs();
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,56 +278,54 @@ 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()
PrinterItem::~PrinterItem()
{
delete fFolder; fFolder = NULL;
delete fFolder;
}
void
void
PrinterItem::GetStringProperty(const char* propName, BString& outString)
{
fNode.ReadAttrString(propName, &outString);
}
void
void
PrinterItem::Update(BView *owner, const BFont *font)
{
BListItem::Update(owner,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);
script.AddSpecifier("Printer", view->IndexOf(this));
BMessage reply;
script.AddSpecifier("Printer", view->IndexOf(this));
if (msgr.SendMessage(&script,&reply) == B_OK) {
if (msgr.SendMessage(&script,&reply) == B_OK)
return true;
}
}
return false;
}
@ -340,82 +337,100 @@ 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() )
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 );
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);
rgb_color color = owner->ViewColor();
rgb_color oldViewColor = color;
rgb_color oldLowColor = owner->LowColor();
rgb_color oldHighColor = owner->HighColor();
if (IsSelected())
color = tint_color(color, B_HIGHLIGHT_BACKGROUND_TINT);
owner->SetViewColor(color);
owner->SetLowColor(color);
owner->SetHighColor(color);
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
owner->SetDrawingMode(B_OP_ALPHA);
#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->SetDrawingMode(mode);
owner->DrawString(fComments.String(), fComments.Length(), commentPt);
owner->SetViewColor(oldviewcolor);
owner->SetDrawingMode(mode);
owner->SetViewColor(oldViewColor);
}
bool
PrinterItem::IsActivePrinter()
bool
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;
}
BDirectory*
PrinterItem::Node()
BDirectory*
PrinterItem::Node()
{
return &fNode;
}
@ -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

@ -1,8 +1,8 @@
/*****************************************************************************/
// Printers Preference Application.
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// Copyright (c) 2001-2003 OpenBeOS Project
@ -10,18 +10,18 @@
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
@ -46,29 +46,30 @@ class PrinterListView;
class PrinterListView : public BListView, public FolderListener {
private:
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);
void AttributeChanged(node_ref *node);
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;
FolderWatcher *fFolder;
PrinterItem *fActivePrinter;
};
@ -78,25 +79,25 @@ class PrinterItem : public BListItem {
public:
PrinterItem(PrintersWindow *window, const BDirectory &node);
~PrinterItem();
void DrawItem(BView *owner, BRect bounds, bool complete);
void Update(BView *owner, const BFont *font);
bool Remove(BListView *view);
bool IsActivePrinter();
bool HasPendingJobs();
const char *Name() const {
return fName.String();
bool IsActivePrinter() const;
bool HasPendingJobs() const;
const char *Name() const {
return fName.String();
}
SpoolFolder *Folder();
SpoolFolder *Folder() const;
BDirectory *Node();
void UpdatePendingJobs();
private:
void GetStringProperty(const char *propName, BString &outString);
SpoolFolder *fFolder;
BDirectory fNode;
BString fComments;
@ -104,7 +105,7 @@ class PrinterItem : public BListItem {
BString fDriverName;
BString fName;
BString fPendingJobs;
static BBitmap *sIcon;
static BBitmap *sSelectedIcon;
};