headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / print / PrinterRoster.cpp
blob88574fec54cfb85df1ab4b4cbb0d310c7b717b1e
1 /*
2 * Copyright 2008 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Julun, <host.haiku@gmx.de
7 */
9 #include <PrinterRoster.h>
11 #include <FindDirectory.h>
12 #include <Node.h>
13 #include <NodeMonitor.h>
14 #include <Path.h>
15 #include <Printer.h>
18 #include <new>
21 namespace BPrivate {
22 namespace Print {
25 BPrinterRoster::BPrinterRoster()
26 : fListener(NULL)
28 BPath path;
29 find_directory(B_USER_PRINTERS_DIRECTORY, &path, true);
30 BNode(path.Path()).GetNodeRef(&fUserPrintersNodRef);
32 fUserPrintersDirectory.SetTo(&fUserPrintersNodRef);
36 BPrinterRoster::~BPrinterRoster()
38 StopWatching();
42 int32
43 BPrinterRoster::CountPrinters()
45 Rewind();
47 int32 i = 0;
48 BPrinter printer;
49 while (GetNextPrinter(&printer) == B_OK)
50 i++;
52 Rewind();
53 return i;
57 status_t
58 BPrinterRoster::GetNextPrinter(BPrinter* printer)
60 if (!printer)
61 return B_BAD_VALUE;
63 status_t status = fUserPrintersDirectory.InitCheck();
64 if (!status == B_OK)
65 return status;
67 BEntry entry;
68 bool next = true;
69 while (status == B_OK && next) {
70 status = fUserPrintersDirectory.GetNextEntry(&entry);
71 if (status == B_OK) {
72 printer->SetTo(entry);
73 next = !printer->IsValid();
74 } else {
75 printer->Unset();
78 return status;
82 status_t
83 BPrinterRoster::GetDefaultPrinter(BPrinter* printer)
85 if (!printer)
86 return B_BAD_VALUE;
88 BDirectory dir(&fUserPrintersNodRef);
89 status_t status = dir.InitCheck();
90 if (!status == B_OK)
91 return status;
93 BEntry entry;
94 while (dir.GetNextEntry(&entry) == B_OK) {
95 if (!entry.IsDirectory())
96 continue;
98 printer->SetTo(entry);
99 if (printer->IsValid() && printer->IsDefault())
100 return B_OK;
102 printer->Unset();
103 return B_ERROR;
107 status_t
108 BPrinterRoster::FindPrinter(const BString& name, BPrinter* printer)
110 if (name.Length() <= 0 || !printer)
111 return B_BAD_VALUE;
113 BDirectory dir(&fUserPrintersNodRef);
114 status_t status = dir.InitCheck();
115 if (!status == B_OK)
116 return status;
118 BEntry entry;
119 while (dir.GetNextEntry(&entry) == B_OK) {
120 if (!entry.IsDirectory())
121 continue;
123 printer->SetTo(entry);
124 if (printer->IsValid() && printer->Name() == name)
125 return B_OK;
127 printer->Unset();
128 return B_ERROR;
133 status_t
134 BPrinterRoster::Rewind()
136 return fUserPrintersDirectory.Rewind();
140 status_t
141 BPrinterRoster::StartWatching(const BMessenger& listener)
143 StopWatching();
145 if (!listener.IsValid())
146 return B_BAD_VALUE;
148 fListener = new(std::nothrow) BMessenger(listener);
149 if (!fListener)
150 return B_NO_MEMORY;
152 return watch_node(&fUserPrintersNodRef, B_WATCH_DIRECTORY, *fListener);
156 void
157 BPrinterRoster::StopWatching()
159 if (fListener) {
160 stop_watching(*fListener);
161 delete fListener;
162 fListener = NULL;
167 } // namespace Print
168 } // namespace BPrivate