src/gui/painting/qprinterinfo_win.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qprinterinfo.h"
       
    43 
       
    44 #include <qstringlist.h>
       
    45 
       
    46 #include <qt_windows.h>
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 #ifndef QT_NO_PRINTER
       
    51 
       
    52 extern QPrinter::PaperSize mapDevmodePaperSize(int s);
       
    53 
       
    54 class QPrinterInfoPrivate
       
    55 {
       
    56 Q_DECLARE_PUBLIC(QPrinterInfo)
       
    57 public:
       
    58     ~QPrinterInfoPrivate();
       
    59     QPrinterInfoPrivate();
       
    60     QPrinterInfoPrivate(const QString& name);
       
    61 
       
    62 private:
       
    63     QString                     m_name;
       
    64     bool                        m_default;
       
    65     bool                        m_isNull;
       
    66 
       
    67     QPrinterInfo*               q_ptr;
       
    68 };
       
    69 
       
    70 static QPrinterInfoPrivate nullQPrinterInfoPrivate;
       
    71 
       
    72 class QPrinterInfoPrivateDeleter
       
    73 {
       
    74 public:
       
    75     static inline void cleanup(QPrinterInfoPrivate *d)
       
    76     {
       
    77         if (d != &nullQPrinterInfoPrivate)
       
    78             delete d;
       
    79     }
       
    80 };
       
    81 
       
    82 /////////////////////////////////////////////////////////////////////////////
       
    83 /////////////////////////////////////////////////////////////////////////////
       
    84 
       
    85 QList<QPrinterInfo> QPrinterInfo::availablePrinters()
       
    86 {
       
    87     QList<QPrinterInfo> printers;
       
    88     LPBYTE buffer;
       
    89     DWORD needed = 0;
       
    90     DWORD returned = 0;
       
    91 
       
    92     if ( !EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, 0, 0, &needed, &returned))
       
    93     {
       
    94         buffer = new BYTE[needed];
       
    95         if (!EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS , NULL,
       
    96                            4, buffer, needed, &needed, &returned))
       
    97         {
       
    98             delete [] buffer;
       
    99             return printers;
       
   100         }
       
   101         PPRINTER_INFO_4 infoList = reinterpret_cast<PPRINTER_INFO_4>(buffer);
       
   102         QPrinterInfo defPrn = defaultPrinter();
       
   103         for (uint i = 0; i < returned; ++i) {
       
   104             printers.append(QPrinterInfo(QString::fromWCharArray(infoList[i].pPrinterName)));
       
   105             if (printers.at(i).printerName() == defPrn.printerName())
       
   106                 printers[i].d_ptr->m_default = true;
       
   107         }
       
   108         delete [] buffer;
       
   109     }
       
   110 
       
   111     return printers;
       
   112 }
       
   113 
       
   114 QPrinterInfo QPrinterInfo::defaultPrinter()
       
   115 {
       
   116     QString noPrinters(QLatin1String("qt_no_printers"));
       
   117     wchar_t buffer[256];
       
   118     GetProfileString(L"windows", L"device", (wchar_t*)noPrinters.utf16(), buffer, 256);
       
   119     QString output = QString::fromWCharArray(buffer);
       
   120 
       
   121     // Filter out the name of the printer, which should be everything
       
   122     // before a comma.
       
   123     bool noConfiguredPrinters = (output == noPrinters);
       
   124     QStringList info = output.split(QLatin1Char(','));
       
   125     QString printerName = noConfiguredPrinters ? QString() : info.at(0);
       
   126 
       
   127     QPrinterInfo prn(printerName);
       
   128     prn.d_ptr->m_default = true;
       
   129     if (noConfiguredPrinters)
       
   130         prn.d_ptr->m_isNull = true;
       
   131     return prn;
       
   132 }
       
   133 
       
   134 /////////////////////////////////////////////////////////////////////////////
       
   135 /////////////////////////////////////////////////////////////////////////////
       
   136 
       
   137 QPrinterInfo::QPrinterInfo()
       
   138     : d_ptr(&nullQPrinterInfoPrivate)
       
   139 {
       
   140 }
       
   141 
       
   142 QPrinterInfo::QPrinterInfo(const QString& name)
       
   143     : d_ptr(new QPrinterInfoPrivate(name))
       
   144 {
       
   145     d_ptr->q_ptr = this;
       
   146 }
       
   147 
       
   148 QPrinterInfo::QPrinterInfo(const QPrinterInfo& src)
       
   149     : d_ptr(&nullQPrinterInfoPrivate)
       
   150 {
       
   151     *this = src;
       
   152 }
       
   153 
       
   154 QPrinterInfo::QPrinterInfo(const QPrinter& prn)
       
   155     : d_ptr(&nullQPrinterInfoPrivate)
       
   156 {
       
   157     QList<QPrinterInfo> list = availablePrinters();
       
   158     for (int c = 0; c < list.size(); ++c) {
       
   159         if (prn.printerName() == list[c].printerName()) {
       
   160             *this = list[c];
       
   161             return;
       
   162         }
       
   163     }
       
   164 
       
   165     *this = QPrinterInfo();
       
   166 }
       
   167 
       
   168 QPrinterInfo::~QPrinterInfo()
       
   169 {
       
   170 }
       
   171 
       
   172 QPrinterInfo& QPrinterInfo::operator=(const QPrinterInfo& src)
       
   173 {
       
   174     Q_ASSERT(d_ptr);
       
   175     d_ptr.reset(new QPrinterInfoPrivate(*src.d_ptr));
       
   176     d_ptr->q_ptr = this;
       
   177     return *this;
       
   178 }
       
   179 
       
   180 QString QPrinterInfo::printerName() const
       
   181 {
       
   182     const Q_D(QPrinterInfo);
       
   183     return d->m_name;
       
   184 }
       
   185 
       
   186 bool QPrinterInfo::isNull() const
       
   187 {
       
   188     const Q_D(QPrinterInfo);
       
   189     return d->m_isNull;
       
   190 }
       
   191 
       
   192 bool QPrinterInfo::isDefault() const
       
   193 {
       
   194     const Q_D(QPrinterInfo);
       
   195     return d->m_default;
       
   196 }
       
   197 
       
   198 QList<QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const
       
   199 {
       
   200     const Q_D(QPrinterInfo);
       
   201     QList<QPrinter::PaperSize> paperList;
       
   202 
       
   203     DWORD size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->m_name.utf16()),
       
   204                                     NULL, DC_PAPERS, NULL, NULL);
       
   205     if ((int)size == -1)
       
   206         return paperList;
       
   207 
       
   208     wchar_t *papers = new wchar_t[size];
       
   209     size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->m_name.utf16()),
       
   210                               NULL, DC_PAPERS, papers, NULL);
       
   211 
       
   212     for (int c = 0; c < (int)size; ++c) {
       
   213         paperList.append(mapDevmodePaperSize(papers[c]));
       
   214     }
       
   215 
       
   216     delete [] papers;
       
   217 
       
   218     return paperList;
       
   219 }
       
   220 
       
   221 /////////////////////////////////////////////////////////////////////////////
       
   222 /////////////////////////////////////////////////////////////////////////////
       
   223 
       
   224 QPrinterInfoPrivate::QPrinterInfoPrivate() :
       
   225     m_default(false),
       
   226     m_isNull(true),
       
   227     q_ptr(NULL)
       
   228 {
       
   229 }
       
   230 
       
   231 QPrinterInfoPrivate::QPrinterInfoPrivate(const QString& name) :
       
   232     m_name(name),
       
   233     m_default(false),
       
   234     m_isNull(false),
       
   235     q_ptr(NULL)
       
   236 {
       
   237 }
       
   238 
       
   239 QPrinterInfoPrivate::~QPrinterInfoPrivate()
       
   240 {
       
   241 }
       
   242 
       
   243 #endif // QT_NO_PRINTER
       
   244 
       
   245 QT_END_NAMESPACE