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