UBImportVirtualPrinter.cpp 5.18 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3 4 5
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Claudio Valerio's avatar
Claudio Valerio committed
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9 10 11 12 13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
 */

#include "UBImportVirtualPrinter.h"

#include "core/UBApplication.h"
#include "core/UBPersistenceManager.h"
#include "core/UBDocumentManager.h"

#include "document/UBDocumentProxy.h"

#include "UBIniFileParser.h"

#include <windows.h>

28
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

QString UBImportVirtualPrinter::sOriginalDefaultPrintername = "";

QPointer<UBDocumentProxy> UBImportVirtualPrinter::pendingDocument;


UBImportVirtualPrinter::UBImportVirtualPrinter(QObject* parent)
    : UBImportAdaptor(parent)
{
    // NOOP
}


UBImportVirtualPrinter::~UBImportVirtualPrinter()
{
    // NOOP
}


QStringList UBImportVirtualPrinter::supportedExtentions()
{
    return QStringList("ini");
}


QString UBImportVirtualPrinter::importFileFilter()
{
    // we don't want user import manually ini file.
    return "";
}


QString UBImportVirtualPrinter::pdfFileName(const QFile& pFile)
{

    UBIniFileParser iniParser(pFile.fileName());

    // check if we have 1 PDF file to import
    QString totalPdfString = iniParser.getStringValue("PDF", "Count");
    bool ok;
    int totalPdf = totalPdfString.toInt(&ok);
    QString pdfFilename;

    if (ok && totalPdf > 0)
    {
        pdfFilename = iniParser.getStringValue("PDF", "File0");
        if (pdfFilename == "")
        {
            qWarning() << "Unable to retreive PDF file name from ini file " << pFile.fileName();
        }
    }

    return pdfFilename;
}


QStringList UBImportVirtualPrinter::emfFileNames(const QFile& pFile)
{

    UBIniFileParser iniParser(pFile.fileName());

    // check if we have 1 PDF file to import
    QString totalEmfString = iniParser.getStringValue("EMF", "Count");
    bool ok;
    int totalEmf = totalEmfString.toInt(&ok);
    QStringList emfFilenames;

    if (ok)
    {
        for(int i = 0; i < totalEmf; i++)
        {
            emfFilenames << iniParser.getStringValue("EMF", QString("File%1").arg(i));
        }
    }

    return emfFilenames;
}



void UBImportVirtualPrinter::cleanUp(const QFile& pFile, const QString& pPdfFileName, QStringList pEmfFilenames)
{

    //restore default printer if needed
    if (UBImportVirtualPrinter::sOriginalDefaultPrintername != "")
    {
        LPTSTR wDefaultPrinterName = new TCHAR[255];
        int i = UBImportVirtualPrinter::sOriginalDefaultPrintername.toWCharArray(wDefaultPrinterName);
        wDefaultPrinterName[i] = 0;
        SetDefaultPrinter(wDefaultPrinterName);
        UBImportVirtualPrinter::sOriginalDefaultPrintername = "";
        delete[] wDefaultPrinterName;
    }

    // delete tmp ini file
    if (!QFile::remove(pFile.fileName()))
    {
        qWarning() << "Unable to remove file " << pFile.fileName();
    }
    // delete tmp PDF file
    if (pPdfFileName != "" && !QFile::remove(pPdfFileName))
    {
        qWarning() << "Unable to remove file " << pPdfFileName;
    }

    foreach(QString f, pEmfFilenames)
    {
        QFile::remove(f);
    }
}


UBDocumentProxy* UBImportVirtualPrinter::importFile(const QFile& pFile, const QString& pGroup)
{
143
    UBApplication::showMessage(tr("Importing Sankore printer file ..."));
Claudio Valerio's avatar
Claudio Valerio committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    UBDocumentProxy *document = 0;

    QString pdfFilename = pdfFileName(pFile);

    if (pdfFilename.size() > 0)
    {
        QFile pdfFile(pdfFilename);

                document = UBDocumentManager::documentManager()->importFile(pdfFile, pGroup);

                UBIniFileParser iniParser(pFile.fileName());

        QString documentName = iniParser.getStringValue("Document", "Name");

        // we must update the document name otherwise the imported document will have the name of the tmp PDF file.
        if (document)
        {
            if (documentName != "")
                document->setMetaData(UBSettings::documentName, documentName);

            UBPersistenceManager::persistenceManager()->persistDocumentMetadata(document);
        }
    }

    if (!document)
    {
170
        UBApplication::showMessage(tr("Error while importing Sankore printer file."));
Claudio Valerio's avatar
Claudio Valerio committed
171 172 173 174 175 176 177 178 179 180
    }

    cleanUp(pFile, pdfFilename, emfFileNames(pFile));

    return document;
}


bool UBImportVirtualPrinter::addFileToDocument(UBDocumentProxy* pDocument, const QFile& pFile)
{
181
    UBApplication::showMessage(tr("Importing Sankore printer file ..."));
Claudio Valerio's avatar
Claudio Valerio committed
182 183 184 185 186 187 188 189 190 191 192 193
    bool result = false;

    QString pdfFilename = pdfFileName(pFile);

    if (pdfFilename.size() > 0)
    {
        QFile pdfFile(pdfFilename);
        result = UBDocumentManager::documentManager()->addFileToDocument(pDocument, pdfFile);
    }

    if (!result)
    {
194
        UBApplication::showMessage(tr("Error while importing Sankore printer file."));
Claudio Valerio's avatar
Claudio Valerio committed
195 196 197 198 199 200 201
    }

    cleanUp(pFile, pdfFilename, emfFileNames(pFile));

    return result;
}