1 /***************************************************************************
2     Copyright (C) 2007-2009 Robby Stephenson <robby@periapsis.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or         *
8  *   modify it under the terms of the GNU General Public License as        *
9  *   published by the Free Software Foundation; either version 2 of        *
10  *   the License or (at your option) version 3 or any later version        *
11  *   accepted by the membership of KDE e.V. (or its successor approved     *
12  *   by the membership of KDE e.V.), which shall act as a proxy            *
13  *   defined in Section 14 of version 3 of the license.                    *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #include "xmphandler.h"
26 #include "../tellico_debug.h"
27 
28 #include <config.h>
29 
30 #include <QFile>
31 #include <QTextStream>
32 
33 #ifdef HAVE_EXEMPI
34 #include <exempi/xmp.h>
35 #endif
36 
37 using Tellico::XMPHandler;
38 
39 bool XMPHandler::s_needInit = true;
40 int XMPHandler::s_initCount = 0;
41 
isXMPEnabled()42 bool XMPHandler::isXMPEnabled() {
43 #ifdef HAVE_EXEMPI
44   return true;
45 #else
46   return false;
47 #endif
48 }
49 
XMPHandler()50 XMPHandler::XMPHandler() {
51 #ifdef HAVE_EXEMPI
52   ++s_initCount;
53 #endif
54 }
55 
~XMPHandler()56 XMPHandler::~XMPHandler() {
57 #ifdef HAVE_EXEMPI
58   --s_initCount;
59   if(s_initCount == 0 && !s_needInit) {
60     xmp_terminate();
61     s_needInit = true;
62   }
63 #endif
64 }
65 
extractXMP(const QString & file)66 QString XMPHandler::extractXMP(const QString& file) {
67   QString result;
68 #ifdef HAVE_EXEMPI
69   if(s_needInit) {
70     xmp_init();
71     s_needInit = false;
72   }
73   XmpFilePtr xmpfile = xmp_files_open_new(QFile::encodeName(file).constData(), XMP_OPEN_READ);
74   if(!xmpfile) {
75     myDebug() << "unable to open " << file;
76     return result;
77   }
78   XmpPtr xmp = xmp_files_get_new_xmp(xmpfile);
79   if(xmp) {
80     XmpStringPtr buffer = xmp_string_new();
81     xmp_serialize(xmp, buffer, 0, 0);
82     result = QString::fromUtf8(xmp_string_cstr(buffer));
83     xmp_string_free(buffer);
84 //    myDebug() << result;
85 #if 0
86     myWarning() << "turn me off!";
87     QFile f1(QLatin1String("/tmp/xmp.xml"));
88     if(f1.open(QIODevice::WriteOnly)) {
89       QTextStream t(&f1);
90       t << result;
91     }
92     f1.close();
93 #endif
94     xmp_free(xmp);
95     xmp_files_close(xmpfile, XMP_CLOSE_NOOPTION);
96     xmp_files_free(xmpfile);
97   } else {
98     myDebug() << "unable to parse " << file;
99   }
100 #else
101   Q_UNUSED(file);
102 #endif
103   return result;
104 }
105