1 /* 2 * Copyright 2008 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Julun, <host.haiku@gmx.de 7 */ 8 9 #include <PrinterRoster.h> 10 11 #include <FindDirectory.h> 12 #include <Node.h> 13 #include <NodeMonitor.h> 14 #include <Path.h> 15 #include <Printer.h> 16 17 18 #include <new> 19 20 21 namespace BPrivate { 22 namespace Print { 23 24 25 BPrinterRoster::BPrinterRoster() 26 : fListener(NULL) 27 { 28 BPath path; 29 find_directory(B_USER_PRINTERS_DIRECTORY, &path, true); 30 BNode(path.Path()).GetNodeRef(&fUserPrintersNodRef); 31 32 fUserPrintersDirectory.SetTo(&fUserPrintersNodRef); 33 } 34 35 36 BPrinterRoster::~BPrinterRoster() 37 { 38 StopWatching(); 39 } 40 41 42 int32 43 BPrinterRoster::CountPrinters() 44 { 45 Rewind(); 46 47 int32 i = 0; 48 BPrinter printer; 49 while (GetNextPrinter(&printer) == B_OK) 50 i++; 51 52 Rewind(); 53 return i; 54 } 55 56 57 status_t 58 BPrinterRoster::GetNextPrinter(BPrinter* printer) 59 { 60 if (!printer) 61 return B_BAD_VALUE; 62 63 status_t status = fUserPrintersDirectory.InitCheck(); 64 if (!status == B_OK) 65 return status; 66 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(); 76 } 77 } 78 return status; 79 } 80 81 82 status_t 83 BPrinterRoster::GetDefaultPrinter(BPrinter* printer) 84 { 85 if (!printer) 86 return B_BAD_VALUE; 87 88 BDirectory dir(&fUserPrintersNodRef); 89 status_t status = dir.InitCheck(); 90 if (!status == B_OK) 91 return status; 92 93 BEntry entry; 94 while (dir.GetNextEntry(&entry) == B_OK) { 95 if (!entry.IsDirectory()) 96 continue; 97 98 printer->SetTo(entry); 99 if (printer->IsValid() && printer->IsDefault()) 100 return B_OK; 101 } 102 printer->Unset(); 103 return B_ERROR; 104 } 105 106 107 status_t 108 BPrinterRoster::FindPrinter(const BString& name, BPrinter* printer) 109 { 110 if (name.Length() <= 0 || !printer) 111 return B_BAD_VALUE; 112 113 BDirectory dir(&fUserPrintersNodRef); 114 status_t status = dir.InitCheck(); 115 if (!status == B_OK) 116 return status; 117 118 BEntry entry; 119 while (dir.GetNextEntry(&entry) == B_OK) { 120 if (!entry.IsDirectory()) 121 continue; 122 123 printer->SetTo(entry); 124 if (printer->IsValid() && printer->Name() == name) 125 return B_OK; 126 } 127 printer->Unset(); 128 return B_ERROR; 129 130 } 131 132 133 status_t 134 BPrinterRoster::Rewind() 135 { 136 return fUserPrintersDirectory.Rewind(); 137 } 138 139 140 status_t 141 BPrinterRoster::StartWatching(const BMessenger& listener) 142 { 143 StopWatching(); 144 145 if (!listener.IsValid()) 146 return B_BAD_VALUE; 147 148 fListener = new(std::nothrow) BMessenger(listener); 149 if (!fListener) 150 return B_NO_MEMORY; 151 152 return watch_node(&fUserPrintersNodRef, B_WATCH_DIRECTORY, *fListener); 153 } 154 155 156 void 157 BPrinterRoster::StopWatching() 158 { 159 if (fListener) { 160 stop_watching(*fListener); 161 delete fListener; 162 fListener = NULL; 163 } 164 } 165 166 167 } // namespace Print 168 } // namespace BPrivate 169