⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qprintdialog_unix.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
                } else if (printerName == QLatin1String("_all")) {                    // skip it.. any other cases we want to skip?                    printerName = QString();                    printerDesc = QString();                }                if (j > 0) {                    // try extracting a comment from the aliases                    aliases = printerDesc.mid(j + 1, i - j - 1).split(QLatin1Char('|'));                    printerComment = QPrintDialog::tr("Aliases: %1")                                     .arg(aliases.join(QLatin1String(", ")));                }                // look for signs of this being a remote printer                i = printerDesc.indexOf(                    QRegExp(QLatin1String(": *bsdaddr *=")));                if (i >= 0) {                    // point k at the end of remote host name                    while (printerDesc[i] != QLatin1Char('='))                        i++;                    while (printerDesc[i] == QLatin1Char('=') || printerDesc[i].isSpace())                        i++;                    j = i;                    while (j < (int)printerDesc.length() &&                            printerDesc[j] != QLatin1Char(':') && printerDesc[j] != QLatin1Char(','))                        j++;                    // and stuff that into the string                    printerHost = printerDesc.mid(i, j-i);                    // maybe stick the remote printer name into the comment                    if (printerDesc[j] == QLatin1Char(',')) {                        i = ++j;                        while (printerDesc[i].isSpace())                            i++;                        j = i;                        while (j < (int)printerDesc.length() &&                                printerDesc[j] != QLatin1Char(':') && printerDesc[j] != QLatin1Char(','))                            j++;                        if (printerName != printerDesc.mid(i, j-i)) {                            printerComment =                                QLatin1String("Remote name: ");                            printerComment += printerDesc.mid(i, j-i);                        }                    }                }            }            if (printerComment == QLatin1String(":"))                printerComment = QString(); // for cups            if (printerName.length())                perhapsAddPrinter(printers, printerName, printerHost,                                   printerComment, aliases);            // chop away the line, for processing the next one            printerDesc = QString();        }    }    delete[] line;    return defaultPrinter;}#ifndef QT_NO_NIS#if defined(Q_C_CALLBACKS)extern "C" {#endifstatic int pd_foreach(int /*status */, char * /*key */, int /*keyLen */,                    char *val, int valLen, char *data){    parsePrinterDesc(QString::fromLatin1(val, valLen), (QList<QPrinterDescription> *)data);    return 0;}#if defined(Q_C_CALLBACKS)}#endifstatic int retrieveNisPrinters(QList<QPrinterDescription> *printers){    typedef int (*WildCast)(int, char *, int, char *, int, char *);    char printersConfByname[] = "printers.conf.byname";    char *domain;    int err;    QLibrary lib(QLatin1String("nsl"));    typedef int (*ypGetDefaultDomain)(char **);    ypGetDefaultDomain _ypGetDefaultDomain = (ypGetDefaultDomain)lib.resolve("yp_get_default_domain");    typedef int (*ypAll)(const char *, const char *, const struct ypall_callback *);    ypAll _ypAll = (ypAll)lib.resolve("yp_all");    if (_ypGetDefaultDomain && _ypAll) {        err = _ypGetDefaultDomain(&domain);        if (err == 0) {            ypall_callback cb;            // wild cast to support K&R-style system headers            (WildCast &) cb.foreach = (WildCast) pd_foreach;            cb.data = (char *) printers;            err = _ypAll(domain, printersConfByname, &cb);        }        if (!err)            return Success;    }    return Unavail;}#endif // QT_NO_NISstatic char *parseNsswitchPrintersEntry(QList<QPrinterDescription> *printers, char *line){#define skipSpaces() \    while (isspace((uchar) line[k])) \        k++    char *defaultPrinter = 0;    bool stop = false;    int lastStatus = NotFound;    int k = 8;    skipSpaces();    if (line[k] != ':')        return 0;    k++;    char *cp = strchr(line, '#');    if (cp != 0)        *cp = '\0';    while (line[k] != '\0') {        if (isspace((uchar) line[k])) {            k++;        } else if (line[k] == '[') {            k++;            skipSpaces();            while (line[k] != '\0') {                char status = tolower(line[k]);                char action = '?';                while (line[k] != '=' && line[k] != ']' && line[k] != '\0')                    k++;                if (line[k] == '=') {                    k++;                    skipSpaces();                    action = tolower(line[k]);                    while (line[k] != '\0' && !isspace((uchar) line[k]) && line[k] != ']')                        k++;                } else if (line[k] == ']') {                    k++;                    break;                }                skipSpaces();                if (lastStatus == status)                    stop = (action == (char) Return);            }        } else {            if (stop)                break;            QByteArray source;            while (!isspace((uchar) line[k]) && line[k] != '[') {                source += line[k];                k++;            }            if (source == "user") {                lastStatus = parsePrintcap(printers,                        QDir::homePath() + QLatin1String("/.printers"));            } else if (source == "files") {                bool found;                defaultPrinter = parsePrintersConf(printers, &found);                if (found)                    lastStatus = Success;#ifndef QT_NO_NIS            } else if (source == "nis") {                lastStatus = retrieveNisPrinters(printers);#endif            } else {                // nisplus, dns, etc., are not implemented yet                lastStatus = NotFound;            }            stop = (lastStatus == Success);        }    }    return defaultPrinter;}static char *parseNsswitchConf(QList<QPrinterDescription> *printers){    QFile nc(QLatin1String("/etc/nsswitch.conf"));    if (!nc.open(QIODevice::ReadOnly))        return 0;    char *defaultPrinter = 0;    char *line = new char[1025];    line[1024] = '\0';    while (!nc.atEnd() &&            nc.readLine(line, 1024) > 0) {        if (qstrncmp(line, "printers", 8) == 0) {            defaultPrinter = parseNsswitchPrintersEntry(printers, line);            delete[] line;            return defaultPrinter;        }    }    strcpy(line, "printers: user files nis nisplus xfn");    defaultPrinter = parseNsswitchPrintersEntry(printers, line);    delete[] line;    return defaultPrinter;}// HP-UXstatic void parseEtcLpMember(QList<QPrinterDescription> *printers){    QDir lp(QLatin1String("/etc/lp/member"));    if (!lp.exists())        return;    QFileInfoList dirs = lp.entryInfoList();    if (dirs.isEmpty())        return;    QString tmp;    for (int i = 0; i < dirs.size(); ++i) {        QFileInfo printer = dirs.at(i);        // I haven't found any real documentation, so I'm guessing that        // since lpstat uses /etc/lp/member rather than one of the        // other directories, it's the one to use.  I did not find a        // decent way to locate aliases and remote printers.        if (printer.isFile())            perhapsAddPrinter(printers, printer.fileName(),                               QPrintDialog::tr("unknown"),                               QLatin1String(""));    }}// IRIX 6.xstatic void parseSpoolInterface(QList<QPrinterDescription> *printers){    QDir lp(QLatin1String("/usr/spool/lp/interface"));    if (!lp.exists())        return;    QFileInfoList files = lp.entryInfoList();    if(files.isEmpty())        return;    for (int i = 0; i < files.size(); ++i) {        QFileInfo printer = files.at(i);        if (!printer.isFile())            continue;        // parse out some information        QFile configFile(printer.filePath());        if (!configFile.open(QIODevice::ReadOnly))            continue;        QByteArray line;        line.resize(1025);        QString namePrinter;        QString hostName;        QString hostPrinter;        QString printerType;        QString nameKey(QLatin1String("NAME="));        QString typeKey(QLatin1String("TYPE="));        QString hostKey(QLatin1String("HOSTNAME="));        QString hostPrinterKey(QLatin1String("HOSTPRINTER="));        while (!configFile.atEnd() &&                (configFile.readLine(line.data(), 1024)) > 0) {            QString uline = QString::fromLocal8Bit(line);            if (uline.startsWith(typeKey) ) {                printerType = uline.mid(nameKey.length());                printerType = printerType.simplified();            } else if (uline.startsWith(hostKey)) {                hostName = uline.mid(hostKey.length());                hostName = hostName.simplified();            } else if (uline.startsWith(hostPrinterKey)) {                hostPrinter = uline.mid(hostPrinterKey.length());                hostPrinter = hostPrinter.simplified();            } else if (uline.startsWith(nameKey)) {                namePrinter = uline.mid(nameKey.length());                namePrinter = namePrinter.simplified();            }        }        configFile.close();        printerType = printerType.trimmed();        if (printerType.indexOf(QLatin1String("postscript"), 0, Qt::CaseInsensitive) < 0)            continue;        int ii = 0;        while ((ii = namePrinter.indexOf(QLatin1Char('"'), ii)) >= 0)            namePrinter.remove(ii, 1);        if (hostName.isEmpty() || hostPrinter.isEmpty()) {            perhapsAddPrinter(printers, printer.fileName(),                               QLatin1String(""), namePrinter);        } else {            QString comment;            comment = namePrinter;            comment += QLatin1String(" (");            comment += hostPrinter;            comment += QLatin1Char(')');            perhapsAddPrinter(printers, printer.fileName(),                               hostName, comment);        }    }}// Every unix must have its own.  It's a standard.  Here is AIX.static void parseQconfig(QList<QPrinterDescription> *printers){    QFile qconfig(QLatin1String("/etc/qconfig"));    if (!qconfig.open(QIODevice::ReadOnly))        return;    QTextStream ts(&qconfig);    QString line;    QString stanzaName; // either a queue or a device name    bool up = true; // queue up?  default true, can be false    QString remoteHost; // null if local    QString deviceName; // null if remote    QRegExp newStanza(QLatin1String("^[0-z\\-]*:$"));    // our basic strategy here is to process each line, detecting new    // stanzas.  each time we see a new stanza, we check if the    // previous stanza was a valid queue for a) a remote printer or b)    // a local printer.  if it wasn't, we assume that what we see is    // the start of the first stanza, or that the previous stanza was    // a device stanza, or that there is some syntax error (we don't    // report those).    do {        line = ts.readLine();        bool indented = line[0].isSpace();        line = line.simplified();        int i = line.indexOf(QLatin1Char('='));        if (indented && i != -1) { // line in stanza            QString variable = line.left(i).simplified();            QString value=line.mid(i+1, line.length()).simplified();            if (variable == QLatin1String("device"))                deviceName = value;            else if (variable == QLatin1String("host"))                remoteHost = value;            else if (variable == QLatin1String("up"))                up = !(value.toLower() == QLatin1String("false"));        } else if (line[0] == QLatin1Char('*')) { // comment            // nothing to do        } else if (ts.atEnd() || // end of file, or beginning of new stanza                    (!indented && line.contains(newStanza))) {            if (up && stanzaName.length() > 0 && stanzaName.length() < 21) {                if (remoteHost.length()) // remote printer                    perhapsAddPrinter(printers, stanzaName, remoteHost,                                       QString());                else if (deviceName.length()) // local printer                    perhapsAddPrinter(printers, stanzaName, QString(),                                       QString());            }            line.chop(1);            if (line.length() >= 1 && line.length() <= 20)                stanzaName = line;            up = true;            remoteHost.clear();            deviceName.clear();        } else {            // syntax error?  ignore.        }    } while (!ts.atEnd());}static void populatePaperSizes(QComboBox* cb){    cb->addItem(QPrintDialog::tr("A0 (841 x 1189 mm)"), QPrinter::A0);    cb->addItem(QPrintDialog::tr("A1 (594 x 841 mm)"), QPrinter::A1);    cb->addItem(QPrintDialog::tr("A2 (420 x 594 mm)"), QPrinter::A2);    cb->addItem(QPrintDialog::tr("A3 (297 x 420 mm)"), QPrinter::A3);    cb->addItem(QPrintDialog::tr("A4 (210 x 297 mm, 8.26 x 11.7 inches)"), QPrinter::A4);    cb->addItem(QPrintDialog::tr("A5 (148 x 210 mm)"), QPrinter::A5);    cb->addItem(QPrintDialog::tr("A6 (105 x 148 mm)"), QPrinter::A6);    cb->addItem(QPrintDialog::tr("A7 (74 x 105 mm)"), QPrinter::A7);    cb->addItem(QPrintDialog::tr("A8 (52 x 74 mm)"), QPrinter::A8);    cb->addItem(QPrintDialog::tr("A9 (37 x 52 mm)"), QPrinter::A9);    cb->addItem(QPrintDialog::tr("B0 (1000 x 1414 mm)"), QPrinter::B0);    cb->addItem(QPrintDialog::tr("B1 (707 x 1000 mm)"), QPrinter::B1);    cb->addItem(QPrintDialog::tr("B2 (500 x 707 mm)"), QPrinter::B2);    cb->addItem(QPrintDialog::tr("B3 (353 x 500 mm)"), QPrinter::B3);    cb->addItem(QPrintDialog::tr("B4 (250 x 353 mm)"), QPrinter::B4);    cb->addItem(QPrintDialog::tr("B5 (176 x 250 mm, 6.93 x 9.84 inches)"), QPrinter::B5);    cb->addItem(QPrintDialog::tr("B6 (125 x 176 mm)"), QPrinter::B6);    cb->addItem(QPrintDialog::tr("B7 (88 x 125 mm)"), QPrinter::B7);    cb->addItem(QPrintDialog::tr("B8 (62 x 88 mm)"), QPrinter::B8);    cb->addItem(QPrintDialog::tr("B9 (44 x 62 mm)"), QPrinter::B9);    cb->addItem(QPrintDialog::tr("B10 (31 x 44 mm)"), QPrinter::B10);    cb->addItem(QPrintDialog::tr("C5E (163 x 229 mm)"), QPrinter::C5E);    cb->addItem(QPrintDialog::tr("DLE (110 x 220 mm)"), QPrinter::DLE);    cb->addItem(QPrintDialog::tr("Executive (7.5 x 10 inches, 191 x 254 mm)"), QPrinter::Executive);    cb->addItem(QPrintDialog::tr("Folio (210 x 330 mm)"), QPrinter::Folio);    cb->addItem(QPrintDialog::tr("Ledger (432 x 279 mm)"), QPrinter::Ledger);    cb->addItem(QPrintDialog::tr("Legal (8.5 x 14 inches, 216 x 356 mm)"), QPrinter::Legal);    cb->addItem(QPrintDialog::tr("Letter (8.5 x 11 inches, 216 x 279 mm)"), QPrinter::Letter);    cb->addItem(QPrintDialog::tr("Tabloid (279 x 432 mm)"), QPrinter::Tabloid);    cb->addItem(QPrintDialog::tr("US Common #10 Envelope (105 x 241 mm)"), QPrinter::Comm10E);}static int getLprPrinters(QList<QPrinterDescription>& printers){    QByteArray etcLpDefault;    parsePrintcap(&printers, QLatin1String("/etc/printcap"));    parseEtcLpMember(&printers);    parseSpoolInterface(&printers);    parseQconfig(&printers);    QFileInfo f;    f.setFile(QLatin1String("/etc/lp/printers"));    if (f.isDir()) {        parseEtcLpPrinters(&printers);        QFile def(QLatin1String("/etc/lp/default"));        if (def.open(QIODevice::ReadOnly)) {            etcLpDefault.resize(1025);            if (def.readLine(etcLpDefault.data(), 1024) > 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -