Untrack Makefiles
[qcomicbook-oblomov] / src / imgarchiver.cpp
1 /*
2  * This file is a part of QComicBook.
3  *
4  * Copyright (C) 2005-2006 Pawel Stolowski <yogin@linux.bydg.org>
5  *
6  * QComicBook is free software; you can redestribute it and/or modify it
7  * under terms of GNU General Public License by Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY. See GPL for more details.
11  */
12
13 #include "imgarchiver.h"
14 #include <qprocess.h>
15 #include <qapplication.h>
16
17 using namespace QComicBook;
18
19 ImgArchiver::ImgArchiver(const ImgDirSink &sink): ImgArchiveSink(sink)
20 {
21         pomp = new QProcess(this);
22         connect(pomp, SIGNAL(processExited()), this, SLOT(compressExited()));
23         connect(pomp, SIGNAL(readyReadStdout()), this, SLOT(compressStdoutReady()));
24 }
25
26 ImgArchiver::~ImgArchiver()
27 {
28         doCleanup();
29 }
30
31 void ImgArchiver::compressStdoutReady()
32 {
33         QByteArray b = pomp->readStdout();
34         for (int i=0; i<b.size(); i++)
35                 if (b[i] == '\n')
36                         ++cmpcnt;
37         emit createProgress(cmpcnt, newfilesnum);
38         qApp->processEvents();
39 }
40
41 void ImgArchiver::compressExited()
42 {
43         doCleanup();
44         if (pomp->normalExit())
45                 emit createReady();
46         else
47                 emit createError(0);
48 }
49
50 void ImgArchiver::doCleanup()
51 {
52         if (!tmpcreatpath.isEmpty())
53         {
54                 QDir dir(tmpcreatpath);
55                 //
56                 // remove temporary files and dirs
57                 for (QStringList::const_iterator it = newfiles.begin(); it !=newfiles.end(); ++it)
58                         dir.remove(*it);
59                 dir.rmdir(tmpcreatpath);
60                 tmpcreatpath = QString::null;
61         }
62 }
63
64 void ImgArchiver::create(const QString &destname, ArchiveType type, QValueList<int> pages)
65 {
66         bool status = false;
67
68         //
69         // match archive type, set subprocess options
70         for (QValueList<ArchiveTypeInfo>::const_iterator it = archinfo.begin(); it!=archinfo.end(); it++)
71         {
72                 const ArchiveTypeInfo &inf = *it;
73                 if (type == inf.type)
74                 {
75                         pomp->setArguments(inf.compressopts);
76                         status = true;
77                         break;
78                 }
79         }
80
81         if (!status)
82         {
83                 emit createError(SINKERR_NOTSUPPORTED);
84                 return;
85         }
86
87         tmpcreatpath = makeTempDir();
88         pomp->addArgument(destname);
89         pomp->addArgument("*");
90
91         char *fmt;
92         if (pages.size() < 100)
93                 fmt = "%.2d";
94         else
95                 fmt = "%.3d";
96
97         int cnt = 0; //pages counter, used to create file names with new numbering scheme
98         for (QValueList<int>::const_iterator it = pages.begin(); it != pages.end(); it++)
99         {
100                 QString fname;
101                 QString ext = getKnownImageExtension(getFullFileName(*it));
102                 if (ext.isEmpty())
103                         ; //TODO
104                 fname.sprintf(fmt, cnt++);
105                 fname = tmpcreatpath + "/" + fname + ext;
106                 if (symlink(getFullFileName(*it), fname) == 0)
107                         newfiles.append(fname);
108                 else
109                         ; //TODO: error handling
110
111         }
112
113         pomp->setWorkingDirectory(tmpcreatpath);
114
115         cmpcnt = 0;
116         newfilesnum = pages.count();
117         if (!pomp->start())
118                 emit createError(0);
119 }
120