File indexing completed on 2022-11-29 20:22:17

0001 /***************************************************************************
0002  *   Copyright (C) 2006 by Sébastien Laoût                                 *
0003  *   slaout@linux62.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include <KCModule>
0022 #include <QTemporaryDir>
0023 #include <QTextCodec>
0024 #include <QTextStream>
0025 #include <qdir.h>
0026 #include <qfile.h>
0027 #include <qfileinfo.h>
0028 #include <qstringlist.h>
0029 
0030 #include "basketthumbcreator.h"
0031 
0032 bool BasketThumbCreator::create(const QString &path, int /*width*/, int /*height*/, QImage &image)
0033 {
0034     // Create the temporary folder:
0035     QTemporaryDir tempDir;
0036     tempDir.setAutoRemove(true);
0037     QString tempFolder = tempDir.path();
0038     QDir dir;
0039     dir.mkdir(tempFolder);
0040     const unsigned long int BUFFER_SIZE = 1024;
0041 
0042     QFile file(path);
0043     if (file.open(QIODevice::ReadOnly)) {
0044         QTextStream stream(&file);
0045         stream.setCodec(QTextCodec::codecForName("UTF-8"));
0046         QString line = stream.readLine();
0047         if (line != "BasKetNP:archive" && line != "BasKetNP:template") {
0048             file.close();
0049             return false;
0050         }
0051         while (!stream.atEnd()) {
0052             // Get Key/Value Pair From the Line to Read:
0053             line = stream.readLine();
0054             int index = line.indexOf(':');
0055             QString key;
0056             QString value;
0057             if (index >= 0) {
0058                 key = line.left(index);
0059                 value = line.right(line.length() - index - 1);
0060             } else {
0061                 key = line;
0062                 value = "";
0063             }
0064             if (key == "preview*") {
0065                 bool ok;
0066                 ulong size = value.toULong(&ok);
0067                 if (!ok) {
0068                     file.close();
0069                     return false;
0070                 }
0071                 // Get the preview file:
0072                 QFile previewFile(tempFolder + "preview.png");
0073                 if (previewFile.open(QIODevice::WriteOnly)) {
0074                     char *buffer = new char[BUFFER_SIZE];
0075                     long int sizeRead;
0076                     while ((sizeRead = file.read(buffer, qMin(BUFFER_SIZE, size))) > 0) {
0077                         previewFile.write(buffer, sizeRead);
0078                         size -= sizeRead;
0079                     }
0080                     previewFile.close();
0081                     delete[] buffer;
0082                     image = QImage(tempFolder + "preview.png");
0083                     file.close();
0084                     return true;
0085                 }
0086             } else if (key.endsWith('*')) {
0087                 // We do not know what it is, but we should read the embedded-file in order to discard it:
0088                 bool ok;
0089                 ulong size = value.toULong(&ok);
0090                 if (!ok) {
0091                     file.close();
0092                     return false;
0093                 }
0094                 // Get the archive file:
0095                 char *buffer = new char[BUFFER_SIZE];
0096                 long int sizeRead;
0097                 while ((sizeRead = file.read(buffer, qMin(BUFFER_SIZE, size))) > 0) {
0098                     size -= sizeRead;
0099                 }
0100                 delete[] buffer;
0101             }
0102         }
0103         file.close();
0104     }
0105     return false;
0106 }
0107 
0108 ThumbCreator::Flags BasketThumbCreator::flags() const
0109 {
0110     return (Flags)(DrawFrame | BlendIcon);
0111 }
0112 
0113 extern "C" {
0114 Q_DECL_EXPORT ThumbCreator *new_creator()
0115 {
0116     return new BasketThumbCreator();
0117 }
0118 };