PageRenderTime 89ms CodeModel.GetById 22ms app.highlight 59ms RepoModel.GetById 2ms app.codeStats 0ms

/vp_plugins/gs_plugin/gs_plugin.cpp

http://cupsfilter.googlecode.com/
C++ | 836 lines | 669 code | 97 blank | 70 comment | 88 complexity | 7e8871e915c5c618e1e37cf202b5f9fa MD5 | raw file
  1#include "gs_plugin.h"
  2#include "proc_thread.h"
  3#include "clientdata.h"
  4
  5#include <QtCore/QDebug>
  6#include <QtCore/QString>
  7#include <QtCore/QStringList>
  8#include <QtCore/QFile>
  9#include <QtCore/QFileInfo>
 10#include <QtCore/QDir>
 11#include <QtCore/QFileInfoList>
 12#include <QtCore/QRegExp>
 13#include <QtCore/QTemporaryFile>
 14#include <QtCore/QTextStream>
 15#include <QtCore/QDateTime>
 16#include <QtCore/QUuid>
 17
 18#include <QtGui/QPixmap>
 19
 20GS_plugin::GS_plugin(QObject *parent)
 21        :QObject (parent)
 22{
 23    //    if (!QMetaType::isRegistered(QMetaType::type("JobsType"))){
 24    //        qRegisterMetaType<VPrn::Jobs>("JobsType");
 25    //    }
 26}
 27
 28
 29void GS_plugin::final_clear()
 30{
 31    //????? ????????? ??????????, ??????? ????????? ????  gs_rcp;
 32    // ? ?????????? ??????? ????? ??????????? ?????????? gs_lib
 33    QFile::remove(gs_rcp);
 34}
 35
 36void GS_plugin::init(const QString &gs_bin, const QString &pdftk_bin,
 37                     const QString &gsprint_bin,const QString &temp_folder)
 38{
 39    QDir dir;
 40    const QString startnow = QDir::currentPath();    
 41    // ???????? ????????????? ? ??????????? ??????????
 42    if (!QFile::exists(gs_bin)){
 43        emit error(VPrn::FileIOError,
 44                   QObject::trUtf8("???? %1 ?? ?????????? ??? ?????????? ???? ???????\n%2")
 45                   .arg(gs_bin)
 46                   .arg(QString(Q_FUNC_INFO)));
 47        emit pluginNotReady();
 48        return;
 49    }else{
 50        gsBin = gs_bin;
 51    }
 52    // ???????? ????????????? ? ??????????? ??????????
 53    if (!QFile::exists(pdftk_bin)){
 54        emit error(VPrn::FileIOError,
 55                   QObject::trUtf8("???? %1 ?? ?????????? ??? ?????????? ???? ???????\n%2")
 56                   .arg(pdftk_bin)
 57                   .arg(QString(Q_FUNC_INFO)));
 58        emit pluginNotReady();
 59        return;
 60    }else{
 61        pdftkBin = pdftk_bin;
 62    }
 63    if (!QFile::exists(gsprint_bin)){
 64        emit error(VPrn::FileIOError,
 65                   QObject::trUtf8("???? %1 ?? ?????????? ??? ?????????? ???? ???????\n%2")
 66                   .arg(gsprint_bin)
 67                   .arg(QString(Q_FUNC_INFO)));
 68        emit pluginNotReady();
 69        return;
 70    }else{
 71        gsprintBin = gsprint_bin;
 72    }
 73    if (dir.cd(temp_folder)){// ???????? ???? ????????????? ?????????? ????????
 74        spoolDir = temp_folder;
 75        myEnv << QObject::trUtf8("TMPDIR=%1").arg(temp_folder);
 76        createRCPfile(spoolDir);
 77    }else{
 78        emit error(VPrn::FileIOError,
 79                   QObject::trUtf8("????????? ??????? %1 ?? ??????\n%2")
 80                   .arg(temp_folder).arg(QString(Q_FUNC_INFO)));
 81        emit pluginNotReady();
 82    }
 83    QDir::setCurrent(startnow);
 84}
 85
 86void GS_plugin::convertPs2Pdf(const QString &client_uuid,const QString &input_fn)
 87{
 88    // ???????? ?????? ??????? ???????? ?????
 89    qDebug() << Q_FUNC_INFO << input_fn;
 90    if (QFile::exists(input_fn)) {
 91        // ????? ?????? ??? ???????
 92        ClientData *c_data = findClientData(client_uuid);
 93
 94        // ????????? ???????? ???????? ???? ??? ???????
 95        c_data->setMainFile(QString("%1/%2/main.pdf")
 96                            .arg( spoolDir, client_uuid ) );
 97
 98        // ????????? ?????? ??????? ??? ??????? ?????
 99        QStringList args;
100
101        args.append("-q" );
102        args.append(QString("@%1").arg(gs_rcp));
103        args.append(QString("-sOutputFile=%1").arg(c_data->getMainFile()));
104        args.append("-c");
105        args.append(".setpdfwrite");
106        args.append("-f");
107        args.append(input_fn);
108        start_proc(client_uuid,gsBin,args,VPrn::job_ConvertPs2Pdf);
109
110    }else {
111        emit error(VPrn::FileIOError,
112                   QObject::trUtf8("ERROR : ???? %1 ?? ??????\n%2")
113                   .arg(input_fn)
114                   .arg(QString(Q_FUNC_INFO)));
115    }
116}
117
118void GS_plugin::directPrint(const QString &client_uuid,const QString &file_name,
119                            const QString &printer_name,int copies)
120{
121    // ????? ?????? ??? ???????
122    QStringList args;
123    QProcess proc;
124
125#if defined(Q_OS_UNIX)                
126    args.append(QString("-P %1").arg(printer_name));
127    args.append(tr("-#%1").arg(copies,0,10));
128    args.append(QString("\"%1\"").arg(file_name).toLatin1());
129
130    proc.start( gsprintBin, args );
131#elif defined(Q_OS_WIN) //-color -noquery -all -printer %1 -copies %2 %3
132    args.append("-color");
133    args.append("-noquery");
134    args.append("-all");
135    args.append(QString("-printer \"%1\"").arg(printer_name).toLatin1());
136    args.append(QString("-copies %1").arg(copies,0,10));
137    args.append(file_name);
138
139    /// ?????? ??? ???????? ??????? ??????????
140    QFile t_file;
141    QString tmp_cmd;
142    {
143        // ????????? ??????????? ????? ?????
144        QTemporaryFile tmp_file;// ???????? ????????? ????
145        tmp_file.setFileTemplate("XXXXXXXX.bat");
146        if (tmp_file.open()){
147            QFileInfo f_info(tmp_file);
148            tmp_cmd = f_info.absoluteFilePath();
149            tmp_file.close();
150            t_file.setFileName(tmp_cmd);
151        }else{
152            emit error(VPrn::FileIOError,
153                       QObject::trUtf8("?????? ???????? ?????????? ????? %1\n%2")
154                       .arg( tmp_cmd )
155                       .arg(QString(Q_FUNC_INFO))
156                       );
157            return;
158        }
159    }
160
161    QTextStream out(&t_file);
162    if (t_file.open(QIODevice::WriteOnly)){
163        out << QObject::tr("@echo off\n");
164        if (gsprintBin.contains(" ") ){
165            out << QObject::tr("\"%1\" ").arg(gsprintBin);
166        }else{
167            out << QObject::tr("%1 ").arg(gsprintBin);
168        }
169        for (int i=0;i<args.size();i++){
170            out << args.at(i) << " ";
171        }
172        t_file.close();
173
174        QFile::copy( tmp_cmd, "c:/gsprint.bat");
175
176        proc.start( tmp_cmd );
177    }else{
178        emit error(VPrn::FileIOError,
179                   QObject::trUtf8("?????? ?????? ?????? ?? ????????? ???? %1\n%2")
180                   .arg( tmp_cmd )
181                   .arg(QString(Q_FUNC_INFO))
182                   );
183        return;
184    }
185#endif
186    // ???????? ???????? ???????? ???????
187
188    if (!proc.waitForStarted(5000)){
189        emit error (VPrn::InternalAppError,
190                    QObject::trUtf8("?????? gsprint (??????? ?? ?????????) ??? ?????? ????? %1!\n%2")
191                    .arg(file_name)
192                    .arg(QString(Q_FUNC_INFO))
193                    );
194        return;
195    }
196    proc.waitForFinished(-1);
197    proc.closeWriteChannel();
198    qDebug() << "Direct print output: " << proc.readAll();//.trimmed();
199    emit filePrinted(client_uuid);
200#if defined(Q_OS_WIN)
201    t_file.remove();
202#endif
203
204}
205
206void GS_plugin::catPdf(const QString &client_uuid,const QString &file_nameA,
207                       const QString &file_nameB, const QString &output_name)
208{
209    // ???????? ??? ???? ??? ??????????
210    if (!QFile::exists(file_nameA)){
211        //?????? ????????? ???? ????? ????????????
212        emit error (VPrn::FileNotFound,
213                    QObject::trUtf8("?????? ???? %1 ?? ??????????!\n%2")
214                    .arg(file_nameA)
215                    .arg(QString(Q_FUNC_INFO))
216                    );
217        return;
218    }
219    if (QFile::exists(file_nameB)){
220        QStringList args;
221        args.append(QString("A=%1").arg(file_nameA));
222        args.append(QString("B=%1").arg(file_nameB));
223        args.append("cat A B output ");
224        args.append(output_name);
225
226        start_proc(client_uuid,pdftkBin,args,VPrn::job_CatPages);
227
228    }else{
229        QFile::rename(file_nameA,output_name);
230    }
231}
232
233void GS_plugin::mergeWithTemplate(const QString &client_uuid,
234                                  const QStringList &t_files,
235                                  VPrn::PreviewMode prn_mode)
236{
237
238    // ????? ?????? ??? ???????
239    ClientData *c_data = findClientData(client_uuid);
240    if (!QFile::exists(c_data->getFirstPage() ) ){
241        emit error (VPrn::FileIOError,
242                    QObject::trUtf8("???? %1 ?? ??????!\n%2")
243                    .arg(c_data->getFirstPage()).arg(QString(Q_FUNC_INFO)) );
244        return;
245    }
246
247    if (t_files.size() == 0){
248        emit error (VPrn::InternalAppError,
249                    QObject::trUtf8("?????? ??? ?????????? ???????!\n%1").arg(QString(Q_FUNC_INFO))
250                    );
251
252        return;
253    }
254    // ???? ?? ???? ????????? ???????, ??? ??????? ??????????
255    QString backPdf;
256    QString outPdf;
257    for (int i=0; i< t_files.size();i++){
258        backPdf = t_files.at(i);
259        // spoolDir/client_uuid/t_firstpage.pdf
260        QRegExp rx("/(.+)/(.+)/(.-copy)/(t_.+).pdf");
261        if(rx.indexIn( backPdf ) != -1){
262            // ??? ?????? ????? ????????????
263            QString copy_num = rx.cap(3);
264            QString page_type  = rx.cap(4);
265
266            if ( page_type.compare("t_firstpage",Qt::CaseInsensitive) == 0){
267                // ??????? ??????? i ???
268                outPdf = QString("%1/%2/%3/firstpage_out.pdf")
269                         .arg(spoolDir,client_uuid,copy_num);
270                c_data->startMerge( i );
271                mergePdf(client_uuid,c_data->getFirstPage(),backPdf,outPdf);
272            }
273            if ( page_type.compare("t_otherpage",Qt::CaseInsensitive) == 0  &&
274                 c_data->getPageCount() >1    ){
275                outPdf = QString("%1/%2/%3/otherpage_out.pdf")
276                         .arg(spoolDir,client_uuid,copy_num);
277                c_data->startMerge( i );
278                // ??????? ??????? ??????? ? ??????????? ??????
279                mergePdf(client_uuid,c_data->getOtherPage(),backPdf,outPdf);
280            }
281            if ( page_type.compare("t_oversidepage",Qt::CaseInsensitive) == 0){
282                outPdf = QString("%1/%2/%3/oversidepage_out.pdf")
283                         .arg(spoolDir,client_uuid,copy_num);
284                QFile::copy(backPdf,outPdf);
285            }
286            if ( page_type.compare("t_lastpage",Qt::CaseInsensitive) == 0){
287                outPdf = QString("%1/%2/%3/lastpage_out.pdf")
288                         .arg(spoolDir,client_uuid,copy_num);
289                QFile::copy(backPdf,outPdf);
290            }
291
292        }
293    }
294
295    if (prn_mode == VPrn::pre_ClearPrintMode ){
296        c_data->setConvertMode(true);
297    }else{
298        c_data->setConvertMode(false);
299    }    
300}
301
302void GS_plugin::convertPdf2Ps(const QString &client_uuid)
303{
304    //gs -dNOPAUSE -dBATCH -sDEVICE=pswrite -sOutputFile=out.ps in.pdf
305    // ????? ?????? ??? ???????
306    ClientData *c_data = findClientData(client_uuid);
307
308    if (c_data ){
309        // ?????? ???????? ???????????
310        for (int i=1;i<6;i++){
311            //?????? ?????? i ??????????
312            QStringList out_list = findFiles4Copy(client_uuid,i,QStringList()
313                                                  <<"face_pages_out.pdf"
314                                                  <<"lastpage_out.pdf"
315                                                  <<"oversidepage_out.pdf"
316                                                  );
317            //?????? ?????? ?????
318            for (int j=0; j<out_list.count();j++){
319                QRegExp rx("/(.+)/(.+)/(.-copy)/(.+)_out.pdf");
320                if (rx.indexIn(out_list.at(j)) != -1){
321
322                    c_data->startConvertPdf2Ps( j );
323
324                    // ??? ?????? ????? ????????????
325                    QString copy_num = rx.cap(3);
326                    QString page_type  = rx.cap(4);
327                    QStringList args;
328                    args.append("-q");
329                    args.append("-dQUIET");
330                    args.append("-dNOPAUSE");
331                    args.append("-dBATCH");
332                    args.append("-dPARANOIDSAFER");
333                    args.append("-sDEVICE=pswrite");
334                    args.append(QString("-sOutputFile=%1/%2/%3/%4_prn.ps")
335                                .arg(spoolDir,client_uuid,copy_num,page_type));
336                    args.append(out_list.at(j));
337                    start_proc(client_uuid,gsBin,args,VPrn::job_ConvertPdf2Ps);
338
339                }
340            }
341        }
342
343
344
345    }
346}
347
348void GS_plugin::convertPdfToPng(const QString &client_uuid,
349                                const QStringList &files)
350{
351    // ????? ?????? ??? ???????
352    ClientData *c_data = findClientData(client_uuid);
353
354    if (files.size() == 0){
355        emit error (VPrn::FileNotFound,
356                    QObject::trUtf8("?????? ??? ?????? ?????? ??? ??????????????, ?????? ?? ???????? !\n%1")
357                    .arg(QString(Q_FUNC_INFO))
358                    );
359        return;
360    }
361    if (c_data){
362        QString file_name;
363        for (int i=0; i<files.size();i++){
364            //gs -q -sDEVICE=png16m -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=5 -r300 -sOutputFile=out_%d.png in.pdf
365            file_name = files.at(i);
366
367            QRegExp rx("/(.+)/(.+)/(.-copy)/(.+)_out.pdf");
368            if (rx.indexIn(file_name) != -1){
369                // ??? ?????? ????? ????????????
370                QString copy_num = rx.cap(3);
371                QString page_type  = rx.cap(4);
372
373                c_data->startConvertPdf2Png( i );
374                QStringList args;
375                args.append("-q");
376                args.append("-dQUIET");
377                args.append("-dNOPAUSE");
378                args.append("-dBATCH");
379                args.append("-dPARANOIDSAFER");
380                ///@todo ???????? ?????????? ?????????? max (106*72/max_x,106*72/max_y)
381
382                args.append("-r150");
383                args.append("-sDEVICE=png16m");
384                args.append("-dFirstPage=1");
385                //????????? ??? ?????? ??? ?????????????
386                if (!c_data->getConvertMode()){
387                    args.append("-dLastPage=1");
388                    args.append(QString("-sOutputFile=%1/%2/%3/%4_0.png")
389                                .arg(spoolDir,client_uuid,copy_num,page_type));
390                }else{
391                    args.append(QString("-sOutputFile=%1/%2/%3/%4_\%d.png")
392                                .arg(spoolDir,client_uuid,copy_num,page_type));
393                }
394                args.append(file_name);
395                start_proc(client_uuid,gsBin,args,VPrn::job_ConvertToPng);
396            }else{
397                emit error (VPrn::FileIOError,
398                            QObject::trUtf8("?????? ? ????? ?????, ??? ??????????? ? pdf -> png!\n%1").arg(QString(Q_FUNC_INFO))
399                            );
400
401            }
402        }
403    }
404}
405
406void GS_plugin::setConvertToPngMode(const QString &client_uuid, bool full_doc )
407{
408    ClientData *c_d(0);
409    c_d = findClientData (client_uuid);
410    if (c_d){
411        c_d->setConvertMode(full_doc);
412    }
413}
414
415QString GS_plugin::getUuid() const
416{
417    QString uuid = QUuid::createUuid().toString().replace("{","").replace("}","");
418    qDebug() << uuid;
419    return uuid;
420}
421
422void GS_plugin::createClientData(const QString &client_uuid)
423{
424    /**
425    * @todo ??? ????? ??????????? ?? ?????? ? ????? ?
426    * ????????? ??? ????????? ??????? ?????? ?????????? ???????? ????? ?? ??????? ???????
427    * ???????? ??? ???????
428    **/
429
430    ClientData *c_d(0);
431    c_d = findClientData (client_uuid);
432    QDir w_dir;
433
434    QString wrk_dir =QString("%1/%2").arg(this->spoolDir,client_uuid);
435    bool Ok = true;
436    {
437        if (c_d){
438            //w_dir = QDir(wrk_dir);
439
440            //if ( w_dir.exists() ){
441            //  recursiveDeletion( wrk_dir );
442            //}
443
444            // ????????? ????????? ????????
445            Ok &= w_dir.mkpath(wrk_dir);
446            for (int i=1; i<6;i++){
447                wrk_dir = QString("%1/%2/%3-copy")
448                          .arg(this->spoolDir,client_uuid)
449                          .arg(i,0,10);
450
451                if ( QDir(wrk_dir).exists() ){
452                    recursiveDeletion( wrk_dir );
453                }
454
455                Ok &= w_dir.mkpath(wrk_dir);
456                if (!Ok){
457                    emit error(VPrn::FileIOError,
458                               QObject::trUtf8("?????? ???????? ???????? ???????? ??? ???????! %1\n%2")
459                               .arg(wrk_dir).arg(QString(Q_FUNC_INFO))
460                               );
461                    return;
462                }
463            }
464        }else{
465            Ok &=false;
466            emit error(VPrn::InternalAppError,
467                       QObject::trUtf8("?????? ???????? ?????????? ??????? ?????? ??? ???????!\n%1")
468                       .arg(QString(Q_FUNC_INFO))
469                       );
470
471        }
472    }
473}
474
475void GS_plugin::deleteClientData(const QString &client_uuid)
476{
477    clients_data.remove(client_uuid);
478}
479
480
481//---------------------------- PRIVATE SLOTS --------------------------------------------------
482void GS_plugin::threadFinish(const QString &jobKey,int code,
483                             const QString &output_message)
484{
485
486    QString m_client_uuid = clients_list.take(jobKey);
487    VPrn::Jobs m_job = jobs_list.take(jobKey);
488    // ????? ?????? ??? ???????
489    ClientData *c_data = findClientData(m_client_uuid);
490
491    if (code != 0 ){
492        //?????? ????????? ? ???????, ??????? ??????
493        ///@todo ?????????? ?? ????????? ?? ??????
494        emit jobFinish(m_client_uuid, m_job,code,output_message);
495        return;
496    }
497
498    // ????????? ???? ??? ??????
499    QString mainPDF       = c_data->getMainFile();
500
501    // ????????? ??? ?? ????? ?????? ??? ????? ??????
502    QRegExp rx;
503    switch (m_job){
504    case VPrn::job_ConvertPdf2Ps:
505        if (c_data->isFinishedConvertPdf2Ps() ){
506            emit docReady4print( m_client_uuid );
507        }
508        break;
509    case VPrn::job_ConvertToPng:
510        if ( c_data->isFinishedConvertPdf2Png() ){
511            //???????? ????? ??????????????? ? png ???????? ??????? ????????
512            catFirstPages( m_client_uuid );
513            emit docReady4preview( m_client_uuid );
514        }
515        break;
516    case VPrn::job_CatPages:
517        if (c_data->isFinishedCat() ){
518            this->convertPdf2Ps(m_client_uuid);
519        }
520        break;
521    case VPrn::job_MergePdf:
522        if (c_data->isFinishedMerge() ){
523            // ??? ???????? ????????? ??????????? ? ???????? ???????? ??????
524            QStringList out_list = this->findFiles(m_client_uuid, QStringList()
525                                                   << "*_out.pdf"
526                                                   );
527            convertPdfToPng(m_client_uuid,out_list);            
528        }
529        break;
530    case VPrn::job_PrintFile:
531        emit filePrinted(m_client_uuid);
532        break;
533    case VPrn::job_ConvertPs2Pdf: // ??????????? ?????? ??????????????? ps ?
534        emit docConvertedToPdf(m_client_uuid);
535        calcPageCount(m_client_uuid,mainPDF);
536        break;
537    case VPrn::job_SplitPageFirst:
538        break;
539    case VPrn::job_SplitPageOther:
540        if ( !c_data->getOtherPage().isEmpty()  && c_data->getPageCount() > 1  ){
541            emit docReady4work(m_client_uuid,c_data->getPageCount());
542        }
543        break;
544    case VPrn::job_CalcPageCount: // ??????????? ?????? ????????? dump_info ?? pdf ?????????
545        int pagesCount; // ????? ??????? ? ?????????????? ?????????
546        if (!output_message.isEmpty()) {
547            rx.setPattern("NumberOfPages:\\s+(\\d{1,}).*");
548            //rx.setMinimal(true);
549            if (rx.indexIn(output_message) != -1) {
550                bool ok;
551                qDebug() << Q_FUNC_INFO << rx.cap(0) << rx.cap(1);
552                pagesCount = rx.cap(1).toInt(&ok, 10);
553
554                if (ok && pagesCount >0 ){
555                    c_data->setPageCount(pagesCount);
556
557                    c_data->setFirstPage( QObject::trUtf8("%1/%2/firstpage.pdf")
558                                          .arg( spoolDir, m_client_uuid ) );
559                    if ( pagesCount ==1){
560                        if ( QFile::exists(c_data->getFirstPage()) ){
561                            QFile::remove( c_data->getFirstPage() );
562                        }
563                        if ( !QFile::copy(mainPDF,c_data->getFirstPage() )){
564                            emit error(VPrn::FileIOError,
565                                       QObject::trUtf8("?????? ??????????? ????????? PDF ?? %1 ? %2\n%3")
566                                       .arg(mainPDF,c_data->getFirstPage().arg(QString(Q_FUNC_INFO)) )
567                                       );
568                        }else{
569                            emit docReady4work(m_client_uuid,pagesCount);
570                        }
571                    }else{
572                        c_data->setOtherPage( QObject::trUtf8("%1/%2/otherpage.pdf")
573                                              .arg( spoolDir, m_client_uuid) );
574                        // ????? ????? ?? 2 ?????
575                        splitPdf(m_client_uuid,c_data->getMainFile(),
576                                 c_data->getFirstPage(),
577                                 c_data->getOtherPage());
578                    }
579                }else{
580                    emit error(VPrn::InternalAppError,
581                               QObject::trUtf8("?????? ??????? ????????? PDF %1\n%2")
582                               .arg(mainPDF).arg(QString(Q_FUNC_INFO))
583                               );
584                }
585
586            }
587        }
588        break;
589    }
590
591}
592
593//----------------------------------- PRIVATE ---------------------------------------------
594
595void GS_plugin::start_proc(const QString &client_uuid,const QString &bin,
596                           QStringList &arg_list,VPrn::Jobs job_id)
597{
598    // ????????? ???? ? ??????? ?????? ? ??????
599    QString j_key = this->getUuid();
600    clients_list.insert(j_key,client_uuid);
601    jobs_list.insert(j_key,job_id);
602
603    ProcessT *proc = new ProcessT(this,j_key);
604    // ?????????? ????????? ????????? ??????
605    connect(proc, SIGNAL(jobFinish     (const QString &,int,const QString &)),
606            this, SLOT  (threadFinish  (const QString &,int,const QString &))
607            );
608    proc->addToEnv(myEnv);
609    proc->execute(bin, arg_list,QProcess::MergedChannels);
610}
611
612void GS_plugin::calcPageCount(const QString &client_uuid,const QString &input_fn)
613{
614    ClientData *c_d(0);
615    if (input_fn.isEmpty()){
616        c_d = findClientData (client_uuid);
617        emit docReady4work(client_uuid,c_d->getPageCount());
618    }else{
619        //pdfTk input_file dump_data
620        QStringList args;
621        args.append(input_fn);
622        args.append("dump_data");
623        start_proc(client_uuid,pdftkBin,args,VPrn::job_CalcPageCount);
624    }
625}
626
627
628void GS_plugin::splitPdf(const QString &client_uuid,const QString &main_pdf,
629                         const QString &first_page, const QString &other_pages)
630{
631    //pdftk %1 cat %2 output page_%2.pdf
632    // ??? ??? ???? ???????? ?????? ?????? ???? ? ????????? ?????? 1 ????????,
633    // ?? ????????? 2 ?????? ?????????????? 1 ???, ? ???????????? ???????????
634
635    // ?????? ????????
636    QStringList args;
637    args.append(main_pdf);
638    args.append("cat");
639    args.append("1");
640    args.append("output");
641    args.append(first_page);
642
643    start_proc(client_uuid,pdftkBin,args,VPrn::job_SplitPageFirst);
644
645    args.clear();
646    args.append(main_pdf);
647    args.append("cat");
648    args.append("2-end");
649    args.append("output");
650    args.append(other_pages);
651
652    start_proc(client_uuid,pdftkBin,args,VPrn::job_SplitPageOther);
653}
654
655void GS_plugin::mergePdf(const QString &client_uuid,const QString &in_pdf,
656                         const QString &back_pdf,const QString &out_pdf)
657{
658    //pdftk in.pdf background back.pdf output out.pdf
659    if (!QFile::exists(in_pdf)) {
660        emit error(VPrn::FileIOError,
661                   QObject::trUtf8("ERROR : ???? %1 ?? ??????\n%2").arg(in_pdf).arg(QString(Q_FUNC_INFO)));
662        return;
663    }
664
665    if (!QFile::exists(back_pdf)) {
666        emit error(VPrn::FileIOError,
667                   QObject::trUtf8("ERROR : ???? %1 ?? ??????\n%2").arg(back_pdf).arg(QString(Q_FUNC_INFO)));
668        return;
669    }
670
671    QStringList args;
672    args.append(in_pdf);
673    args.append("background");
674    args.append(back_pdf);
675    args.append("output");
676    args.append(out_pdf);
677
678    start_proc(client_uuid,pdftkBin,args,VPrn::job_MergePdf);
679}
680
681void GS_plugin::catFirstPages(const QString &client_uuid )
682{    
683    // ????? ?????? ??? ???????
684    ClientData *c_data = findClientData(client_uuid);
685    if (c_data){
686        // ?????? ???????? ???????????
687        for (int i=1;i<6;i++){
688            //?????? ?????? i ??????????
689            QStringList out_list = findFiles4Copy(client_uuid,i,
690                                                  QStringList()<<"firstpage_out.pdf"
691                                                  <<"otherpage_out.pdf"
692                                                  );
693            QString fileA;
694            QString fileB;
695            QString outFile = QString("%1/%2/%3-copy/face_pages_out.pdf")
696                              .arg(spoolDir,client_uuid).arg(i,0,10);
697
698            for (int j=0;j<out_list.size();j++){
699                QString line = out_list.at(j);
700                QRegExp rx("/(.+)/(.+)/(.-copy)/(.+)_out.pdf");
701                if (rx.indexIn(line) != -1){
702                    // ??? ?????? ????? ????????????
703                    QString copy_num = rx.cap(3);
704                    QString page_type  = rx.cap(4);
705                    if (page_type.compare("firstpage",Qt::CaseInsensitive) == 0){
706                        fileA = line;
707                    }
708                    if (page_type.compare("otherpage",Qt::CaseInsensitive) == 0){
709                        fileB = line;
710                    }
711                }
712            }
713
714            //????????? ?????
715            c_data->startCat( i );
716            catPdf(client_uuid,fileA,fileB,outFile);
717        }
718        if ( c_data->getPageCount() == 1){
719            // ?????? ???????????? ????? ? ps
720            convertPdf2Ps(client_uuid);
721        }
722    }else{
723        emit error(VPrn::InternalAppError,
724                   QObject::trUtf8("?????? ?????? ?????? ???????!\n%1")
725                   .arg(QString(Q_FUNC_INFO)));
726    }
727}
728
729ClientData * GS_plugin::findClientData(const QString &client_uuid)
730{
731    ClientData *c_d(0);
732
733    QMap<QString,ClientData *>::const_iterator i = clients_data.find(client_uuid);
734    if ( i == clients_data.end() ){
735        clients_data.insert(client_uuid,new ClientData());
736    }
737    c_d = clients_data.value(client_uuid);
738    return c_d;
739}
740
741QStringList GS_plugin::findFiles(const QString &client_uuid,
742                                 const QStringList &filters)
743{
744    return QStringList() << findFiles4Copy(client_uuid,1,filters)
745            << findFiles4Copy(client_uuid,2,filters)
746            << findFiles4Copy(client_uuid,3,filters)
747            << findFiles4Copy(client_uuid,4,filters)
748            << findFiles4Copy(client_uuid,5,filters);
749}
750
751QStringList GS_plugin::findFiles4Copy(const QString &client_uuid, /*ID ???????*/
752                                      int copyNum, /*????? ?????????? ?????????*/
753                                      const QStringList &filters /*??? ??????*/
754                                      )
755{
756    QStringList files;
757    QDir dir;
758    if (copyNum >0  && copyNum <6){
759        // ????????? ????? ?????? ??? ??????????? ???. ?????????
760        dir = QDir(QString ("%1/%2/%3-copy")
761                   .arg(spoolDir,client_uuid)
762                   .arg(copyNum,0,10) );
763        dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
764        dir.setNameFilters(filters);
765        QFileInfoList list = dir.entryInfoList();
766        for (int i = 0; i < list.size(); ++i)
767        {
768            QFileInfo fileInfo = list.at(i);
769            files.append(dir.absoluteFilePath(fileInfo.fileName()));
770        }
771    }
772    return files;
773}
774
775void GS_plugin::recursiveDeletion(QString path)
776{
777    QDir dir(path);
778    QStringList files = dir.entryList(QDir::Files);
779
780    QStringList::Iterator itFile = files.begin();
781    while (itFile != files.end())
782    {
783        QFile file(path + "/" + *itFile);
784        if ( !file.remove()){
785            emit error(VPrn::FileIOError,
786                       QObject::trUtf8("?????? ???????? ????? %1!\n%2").arg(*itFile).arg(QString(Q_FUNC_INFO)));
787        }
788        ++itFile;
789    }
790
791    QStringList dirs = dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
792    QStringList::Iterator itDir = dirs.begin();
793    while (itDir != dirs.end())
794    {
795        recursiveDeletion(path + "/" + *itDir);
796        ++itDir;
797    }
798
799    if ( !dir.rmdir(path)){
800
801        emit error(VPrn::FileIOError,
802                   QObject::trUtf8("?????? ???????? ???????? %1!\n%2").arg(path).arg(QString(Q_FUNC_INFO)));
803
804    }
805}
806
807void GS_plugin::createRCPfile(const QString &tmp_dir)
808{
809    gs_rcp=QString("%1/%2.rcp").arg(tmp_dir,getUuid());
810    QFile new_file;
811
812    new_file.setFileName(gs_rcp);
813    if (new_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
814        QTextStream out(&new_file);
815        out     << QObject::trUtf8("-dQUIET\n")
816                << QObject::trUtf8("-dNOPAUSE\n")
817                << QObject::trUtf8("-dPARANOIDSAFER\n")
818                << QObject::trUtf8("-dBATCH\n")
819                << QObject::trUtf8("-r600\n")
820                << QObject::trUtf8("-dPDFSETTINGS=/prepress\n")
821                << QObject::trUtf8("-dEmbedAllFonts=true\n")
822                << QObject::trUtf8("-sDEVICE=pdfwrite\n");
823        new_file.close();
824    }else{
825        emit error(VPrn::FileIOError,
826                   QObject::trUtf8("?????? ???????? ????? %1\n?????? %2\n%3")
827                   .arg(gs_rcp)
828                   .arg(new_file.errorString())
829                   .arg(QString(Q_FUNC_INFO)));
830        emit pluginNotReady();
831    }
832}
833
834
835
836Q_EXPORT_PLUGIN2(Igs_plugin, GS_plugin);