delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kioslave / thumbnail / djvucreator.cpp
blob03797edef18601abe1f3f170a3dd945871898b60
1 /* This file is part of the KDE libraries
2 Copyright (C) 2001 Malte Starostik <malte@kde.org>
3 Copyright (C) 2001 Leon Bottou <leon@bottou.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "djvucreator.h"
24 #include <config-runtime.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <signal.h>
30 #ifdef HAVE_SYS_SELECT_H
31 #include <sys/select.h>
32 #endif
33 #include <sys/time.h>
34 #include <sys/wait.h>
35 #include <fcntl.h>
36 #include <errno.h>
38 #include <QFile>
39 #include <QImage>
41 #include <kdemacros.h>
45 extern "C"
47 KDE_EXPORT ThumbCreator *new_creator()
49 return new DjVuCreator;
53 bool DjVuCreator::create(const QString &path, int width, int height, QImage &img)
55 int output[2];
56 QByteArray data(1024, 'k');
57 bool ok = false;
59 if (pipe(output) == -1)
60 return false;
62 const char* argv[8];
63 QByteArray sizearg, fnamearg;
64 sizearg = QByteArray::number(width) + 'x' + QByteArray::number(height);
65 fnamearg = QFile::encodeName( path );
66 argv[0] = "ddjvu";
67 argv[1] = "-page";
68 argv[2] = "1";
69 argv[3] = "-size";
70 argv[4] = sizearg.data();
71 argv[5] = fnamearg.data();
72 argv[6] = 0;
74 pid_t pid = fork();
75 if (pid == 0)
77 close(output[0]);
78 dup2(output[1], STDOUT_FILENO);
79 execvp(argv[0], const_cast<char *const *>(argv));
80 exit(1);
82 else if (pid >= 0)
84 close(output[1]);
85 int offset = 0;
86 while (!ok) {
87 fd_set fds;
88 FD_ZERO(&fds);
89 FD_SET(output[0], &fds);
90 struct timeval tv;
91 tv.tv_sec = 20;
92 tv.tv_usec = 0;
93 if (select(output[0] + 1, &fds, 0, 0, &tv) <= 0) {
94 if (errno == EINTR || errno == EAGAIN)
95 continue;
96 break; // error or timeout
98 if (FD_ISSET(output[0], &fds)) {
99 int count = read(output[0], data.data() + offset, 1024);
100 if (count == -1)
101 break;
102 if (count) // prepare for next block
104 offset += count;
105 data.resize(offset + 1024);
107 else // got all data
109 data.resize(offset);
110 ok = true;
114 if (!ok)
115 kill(pid, SIGTERM);
116 int status = 0;
117 if (waitpid(pid, &status, 0) != pid || (status != 0 && status != 256) )
118 ok = false;
120 else
122 close(output[1]);
125 close(output[0]);
126 int l = img.loadFromData( data );
127 return ok && l;
131 ThumbCreator::Flags DjVuCreator::flags() const
133 return static_cast<Flags>(DrawFrame);