Skip to content

Commit

Permalink
Add IppPrinter info getters
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Feb 3, 2024
1 parent c55750c commit 35a1d12
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 9 deletions.
129 changes: 129 additions & 0 deletions lib/ippprinter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ippprinter.h"
#include "curlrequester.h"
#include "stringsplit.h"

IppPrinter::IppPrinter(std::string addr) : _addr(addr)
{
Expand All @@ -14,11 +15,139 @@ Error IppPrinter::refresh()
return error;
}

std::string IppPrinter::name()
{
return _printerAttrs.get<std::string>("printer-name");
}

std::string IppPrinter::uuid()
{
return _printerAttrs.get<std::string>("printer-uuid");
}

std::string IppPrinter::makeAndModel()
{
return _printerAttrs.get<std::string>("printer-make-and-model");
}

std::string IppPrinter::location()
{
return _printerAttrs.get<std::string>("printer-location");
}

int IppPrinter::state()
{
return _printerAttrs.get<int>("printer-state");
}

std::string IppPrinter::stateMessage()
{
return _printerAttrs.get<std::string>("printer-state-message");
}

List<std::string> IppPrinter::stateReasons()
{
return _printerAttrs.getList<std::string>("printer-state-reasons");
}

List<std::string> IppPrinter::ippVersionsSupported()
{
return _printerAttrs.getList<std::string>("ipp-versions-supported");
}

List<std::string> IppPrinter::ippFeaturesSupported()
{
return _printerAttrs.getList<std::string>("ipp-features-supported");
}

int IppPrinter::pagesPerMinute()
{
return _printerAttrs.get<int>("pages-per-minute");
}

int IppPrinter::pagesPerMinuteColor()
{
return _printerAttrs.get<int>("pages-per-minute-color");
}

bool IppPrinter::identifySupported()
{
return _printerAttrs.has("identify-actions-supported");
}

List<IppPrinter::Supply> IppPrinter::supplies()
{
List<IppPrinter::Supply> supplies;
List<std::string> names = _printerAttrs.getList<std::string>("marker-names");
List<std::string> types = _printerAttrs.getList<std::string>("marker-types");
List<std::string> colors = _printerAttrs.getList<std::string>("marker-colors");
List<int> levels = _printerAttrs.getList<int>("marker-levels");
List<int> lowLevels = _printerAttrs.getList<int>("marker-low-levels");
List<int> highLevels = _printerAttrs.getList<int>("marker-high-levels");

List<std::string>::iterator name = names.begin();
List<std::string>::iterator type = types.begin();
List<std::string>::iterator color = colors.begin();
List<int>::iterator level = levels.begin();
List<int>::iterator lowLevel = lowLevels.begin();
List<int>::iterator highLevel = highLevels.begin();

for(;
name != names.end() && type != types.end() && color != colors.end() &&
level != levels.end() && lowLevel != lowLevels.end() && highLevel != highLevels.end();
name++, type++, color++, level++, lowLevel++, highLevel++)
{
List<std::string> colorList;
for(std::string colorString : split_string(*color, "#"))
{
colorList.push_back("#" + colorString);
}
supplies.push_back({*name, *type, colorList, *level, *lowLevel, *highLevel});
}
return supplies;
}

List<IppPrinter::Firmware> IppPrinter::firmware()
{
List<IppPrinter::Firmware> firmware;
List<std::string> names = _printerAttrs.getList<std::string>("printer-firmware-name");
List<std::string> versions = _printerAttrs.getList<std::string>("printer-firmware-string-version");

for(List<std::string>::iterator name = names.begin(), version = versions.begin();
name != names.end() && version != versions.end();
name++, version++)
{
firmware.push_back({*name, *version});
}
return firmware;
}

int IppPrinter::Supply::getPercent() const
{
return (level*100.0)/highLevel;
}

bool IppPrinter::Supply::isLow() const
{
return level <= lowLevel;
}

bool IppPrinter::Supply::operator==(const IppPrinter::Supply& other) const
{
return other.name == name &&
other.type == type &&
other.colors == colors &&
other.level == level &&
other.lowLevel == lowLevel &&
other.highLevel == highLevel;
}

bool IppPrinter::Firmware::operator==(const IppPrinter::Firmware& other) const
{
return other.name == name &&
other.version == version;
}

Error IppPrinter::identify()
{
if(!identifySupported())
Expand Down
33 changes: 33 additions & 0 deletions lib/ippprinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
class IppPrinter
{
public:
struct Supply
{
std::string name;
std::string type;
List<std::string> colors;
int level = 0;
int lowLevel = 0;
int highLevel = 0;
int getPercent() const;
bool isLow() const;
bool operator==(const Supply& other) const;
};
struct Firmware
{
std::string name;
std::string version;
bool operator==(const Firmware& other) const;
};

IppPrinter() = delete;
IppPrinter(std::string addr);
Error refresh();
Expand All @@ -29,6 +48,20 @@ class IppPrinter
return IppPrintJob(_printerAttrs);
}

std::string name();
std::string uuid();
std::string makeAndModel();
std::string location();
int state();
std::string stateMessage();
List<std::string> stateReasons();
List<std::string> ippVersionsSupported();
List<std::string> ippFeaturesSupported();
int pagesPerMinute();
int pagesPerMinuteColor();
List<Supply> supplies();
List<Firmware> firmware();

bool identifySupported();
Error identify();

Expand Down
80 changes: 71 additions & 9 deletions utils/ippclient.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <bytestream.h>
#include <iostream>
#include <filesystem>
#include <regex>

#include <poppler.h>
#include <poppler-document.h>
Expand All @@ -17,25 +18,60 @@ inline void print_error(std::string hint, std::string argHelp)
std::cerr << hint << std::endl << std::endl << argHelp << std::endl << HELPTEXT << std::endl;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, List<T> bl)
std::string print_colors(const List<std::string>& colors)
{
if(bl.size() > 0)
std::stringstream res;
std::smatch match;
const std::regex colorRegex("^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$");
for(const std::string& color : colors)
{
os << "[" << bl.takeFront();
for(T b : bl)
if(std::regex_match(color, match, colorRegex))
{
os << ", " << b;
unsigned long r = std::stoul(match[1], nullptr, 16);
unsigned long g = std::stoul(match[2], nullptr, 16);
unsigned long b = std::stoul(match[3], nullptr, 16);
res << "\x1b[48;2;" << r << ";" << g << ";" << b << "m" << " " << "\x1b[0m";
}
os << "]";
}
else
return res.str();
}

std::ostream& operator<<(std::ostream& os, List<IppPrinter::Supply> supplies)
{
for(List<IppPrinter::Supply>::iterator supply = supplies.begin(); supply != supplies.end(); supply++)
{
os << "[]";
std::string color = print_colors(supply->colors);
os << "* " << supply->name << std::endl
<< " " << ((color != "") ? color+" " : "")
<< supply->getPercent() << "%" << (supply->isLow() ? "(low)" : "")
<< " " << supply->type;
if(std::next(supply) != supplies.end())
{
os << std::endl;
}
}
return os;
}

std::ostream& operator<<(std::ostream& os, List<IppPrinter::Firmware> firmwares)
{
for(List<IppPrinter::Firmware>::iterator firmware = firmwares.begin(); firmware != firmwares.end(); firmware++)
{
os << firmware->name << ": " << firmware->version;
if(std::next(firmware) != firmwares.end())
{
os << std::endl;
}
}
return os;
}

std::ostream& operator<<(std::ostream& os, List<std::string> sl)
{
os << join_string(sl, ", ");
return os;
}

std::string resolution_list(List<IppResolution> l)
{
bool first = true;
Expand All @@ -56,6 +92,15 @@ std::string resolution_list(List<IppResolution> l)
return ss.str();
}

template <typename T>
void print_if_set(std::string title, T value)
{
if(value != T())
{
std::cout << title << std::endl << value << std::endl << std::endl;
}
}

template <typename O, typename S, typename V>
void set_or_fail(const O& opt, S& setting, V value, bool force)
{
Expand Down Expand Up @@ -168,6 +213,8 @@ int main(int argc, char** argv)

SubArgGet args({{"get-attrs", {{&helpOpt, &verboseOpt},
{&addrArg}}},
{"info", {{&helpOpt, &verboseOpt},
{&addrArg}}},
{"identify", {{&helpOpt, &verboseOpt},
{&addrArg}}},
{"print", {{&helpOpt, &verboseOpt, &forceOpt, &oneStageOpt,
Expand Down Expand Up @@ -204,6 +251,21 @@ int main(int argc, char** argv)
{
std::cout << printer.attributes();
}
else if(args.subCommand() == "info")
{
print_if_set("Name:", printer.name());
print_if_set("Make and model:", printer.makeAndModel());
print_if_set("Location:", printer.location());
print_if_set("UUID:", printer.uuid());
print_if_set("Printer state message:", printer.stateMessage());
print_if_set("Printer state reasons:", join_string(printer.stateReasons(), "\n"));
print_if_set("IPP versions supported:", join_string(printer.ippVersionsSupported(), ", "));
print_if_set("IPP features supported:", join_string(printer.ippFeaturesSupported(), "\n"));
print_if_set("Pages per minute:", printer.pagesPerMinute());
print_if_set("Pages per minute (color):", printer.pagesPerMinuteColor());
print_if_set("Supplies:", printer.supplies());
print_if_set("Firmware:", printer.firmware());
}
else if(args.subCommand() == "identify")
{
error = printer.identify();
Expand Down

0 comments on commit 35a1d12

Please sign in to comment.