File indexing completed on 2024-05-12 05:10:14

0001 /***************************************************************************
0002     Copyright (C) 2007-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "xmphandler.h"
0026 #include "../tellico_debug.h"
0027 
0028 #include <config.h>
0029 
0030 #include <QFile>
0031 #include <QTextStream>
0032 
0033 #ifdef HAVE_EXEMPI
0034 #include <exempi/xmp.h>
0035 #endif
0036 
0037 using Tellico::XMPHandler;
0038 
0039 bool XMPHandler::s_needInit = true;
0040 int XMPHandler::s_initCount = 0;
0041 
0042 bool XMPHandler::isXMPEnabled() {
0043 #ifdef HAVE_EXEMPI
0044   return true;
0045 #else
0046   return false;
0047 #endif
0048 }
0049 
0050 XMPHandler::XMPHandler() {
0051 #ifdef HAVE_EXEMPI
0052   ++s_initCount;
0053 #endif
0054 }
0055 
0056 XMPHandler::~XMPHandler() {
0057 #ifdef HAVE_EXEMPI
0058   --s_initCount;
0059   if(s_initCount == 0 && !s_needInit) {
0060     xmp_terminate();
0061     s_needInit = true;
0062   }
0063 #endif
0064 }
0065 
0066 QString XMPHandler::extractXMP(const QString& file) {
0067   QString result;
0068 #ifdef HAVE_EXEMPI
0069   if(s_needInit) {
0070     xmp_init();
0071     s_needInit = false;
0072   }
0073   XmpFilePtr xmpfile = xmp_files_open_new(QFile::encodeName(file).constData(), XMP_OPEN_READ);
0074   if(!xmpfile) {
0075     myDebug() << "unable to open " << file;
0076     return result;
0077   }
0078   XmpPtr xmp = xmp_files_get_new_xmp(xmpfile);
0079   if(xmp) {
0080     XmpStringPtr buffer = xmp_string_new();
0081     xmp_serialize(xmp, buffer, 0, 0);
0082     result = QString::fromUtf8(xmp_string_cstr(buffer));
0083     xmp_string_free(buffer);
0084 //    myDebug() << result;
0085 #if 0
0086     myWarning() << "turn me off!";
0087     QFile f1(QLatin1String("/tmp/xmp.xml"));
0088     if(f1.open(QIODevice::WriteOnly)) {
0089       QTextStream t(&f1);
0090       t << result;
0091     }
0092     f1.close();
0093 #endif
0094     xmp_free(xmp);
0095     xmp_files_close(xmpfile, XMP_CLOSE_NOOPTION);
0096     xmp_files_free(xmpfile);
0097   } else {
0098     myDebug() << "unable to parse " << file;
0099   }
0100 #else
0101   Q_UNUSED(file);
0102 #endif
0103   return result;
0104 }