1 /* ============================================================
2  *
3  * This file is a part of KDE project
4  *
5  *
6  * Date        : 2011-02-11
7  * Description : A kipi plugin to export images to a MediaWiki wiki
8  *
9  * Copyright (C) 2011      by Alexandre Mendes <alex dot mendes1988 at gmail dot com>
10  * Copyright (C) 2011-2018 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  * Copyright (C) 2012      by Nathan Damie <nathan dot damie at gmail dot com>
12  * Copyright (C) 2012      by Iliya Ivanov <ilko2002 at abv dot bg>
13  * Copyright (C) 2012      by Peter Potrowl <peter dot potrowl at gmail dot com>
14  *
15  * This program is free software; you can redistribute it
16  * and/or modify it under the terms of the GNU General
17  * Public License as published by the Free Software Foundation;
18  * either version 2, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * ============================================================ */
26 
27 #include "wmtalker.h"
28 
29 // Qt includes
30 
31 #include <QApplication>
32 #include <QMessageBox>
33 #include <QFile>
34 #include <QTimer>
35 #include <QStringList>
36 
37 // KDE includes
38 
39 #include <klocalizedstring.h>
40 
41 // MediaWiki includes
42 
43 #include <MediaWiki/Upload>
44 #include <MediaWiki/MediaWiki>
45 
46 // KIPI includes
47 
48 #include <KIPI/Interface>
49 
50 // Local includes
51 
52 #include "kipiplugins_debug.h"
53 
54 namespace KIPIWikiMediaPlugin
55 {
56 
57 class WMTalker::Private
58 {
59 public:
60 
Private()61     Private()
62     {
63         interface = nullptr;
64         mediawiki = nullptr;
65     }
66 
67     QList<QUrl>                              urls;
68     Interface*                               interface;
69     MediaWiki*                               mediawiki;
70     QString                                  error;
71     QString                                  currentFile;
72     QMap <QString, QMap <QString, QString> > imageDesc;
73 };
74 
WMTalker(Interface * const interface,MediaWiki * const mediawiki,QObject * const parent)75 WMTalker::WMTalker(Interface* const interface, MediaWiki* const mediawiki, QObject* const parent)
76     : KJob(parent),
77       d(new Private)
78 {
79     d->interface = interface;
80     d->mediawiki = mediawiki;
81 }
82 
~WMTalker()83 WMTalker::~WMTalker()
84 {
85     delete d;
86 }
87 
start()88 void WMTalker::start()
89 {
90     QTimer::singleShot(0, this, SLOT(uploadHandle()));
91 }
92 
begin()93 void WMTalker::begin()
94 {
95     start();
96 }
97 
setImageMap(const QMap<QString,QMap<QString,QString>> & imageDesc)98 void WMTalker::setImageMap(const QMap <QString,QMap <QString,QString> >& imageDesc)
99 {
100     d->imageDesc = imageDesc;
101     qCDebug(KIPIPLUGINS_LOG) << "Map length:" << imageDesc.size();
102 }
103 
uploadHandle(KJob * j)104 void WMTalker::uploadHandle(KJob* j)
105 {
106     if (j != nullptr)
107     {
108         qCDebug(KIPIPLUGINS_LOG) << "Upload error" << j->error() << j->errorString() << j->errorText();
109         emit uploadProgress(100);
110 
111         disconnect(j, SIGNAL(result(KJob*)),
112                    this, SLOT(uploadHandle(KJob*)));
113 
114         disconnect(j, SIGNAL(percent(KJob*, ulong)),
115                    this, SLOT(slotUploadProgress(KJob*, ulong)));
116 
117         // Error from previous upload
118 
119         if ((int)j->error() != 0)
120         {
121             const QString errorText = j->errorText();
122 
123             if (errorText.isEmpty())
124             {
125                 d->error.append(i18n("Error on file '%1'\n", d->currentFile));
126             }
127             else
128             {
129                 d->error.append(i18n("Error on file '%1': %2\n", d->currentFile, errorText));
130             }
131         }
132     }
133 
134     // Upload next image
135 
136     if (!d->imageDesc.isEmpty())
137     {
138         QList<QString> keys        = d->imageDesc.keys();
139         QMap<QString,QString> info = d->imageDesc.take(keys.first());
140         Upload* const e1           = new Upload(*d->mediawiki, this);
141 
142         qCDebug(KIPIPLUGINS_LOG) << "Path:" << keys.first();
143 
144         QFile* const file = new QFile(keys.first(),this);
145 
146         if (!file->open(QIODevice::ReadOnly))
147         {
148             qCDebug(KIPIPLUGINS_LOG) << "File open error:" << keys.first();
149             delete file;
150             return;
151         }
152 
153         //emit fileUploadProgress(done = 0, total file.size());
154 
155         e1->setFile(file);
156         d->currentFile = file->fileName();
157         qCDebug(KIPIPLUGINS_LOG) << "Name:" << file->fileName();
158         e1->setFilename(info[QLatin1String("title")].replace(QLatin1String(" "), QLatin1String("_")));
159         qCDebug(KIPIPLUGINS_LOG) << "Title:" << info[QLatin1String("title")];
160 
161         if (!info[QLatin1String("comments")].isEmpty())
162         {
163             e1->setComment(info[QLatin1String("comments")]);
164         }
165         else
166         {
167             e1->setComment(i18n("Uploaded via KIPI uploader"));
168         }
169 
170         e1->setText(buildWikiText(info));
171         keys.removeFirst();
172 
173         connect(e1, SIGNAL(result(KJob*)),
174                 this, SLOT(uploadHandle(KJob*)));
175 
176         connect(e1, SIGNAL(percent(KJob*, ulong)),
177                 this, SLOT(slotUploadProgress(KJob*, ulong)));
178 
179         emit uploadProgress(0);
180         e1->start();
181     }
182     else
183     {
184         // Finish upload
185 
186         if (d->error.size() > 0)
187         {
188             QMessageBox::critical(QApplication::activeWindow(), i18n("Error"), d->error);
189         }
190         else
191         {
192             emit endUpload();
193         }
194 
195         d->error.clear();
196     }
197 }
198 
buildWikiText(const QMap<QString,QString> & info) const199 QString WMTalker::buildWikiText(const QMap<QString, QString>& info) const
200 {
201     QString text = QString::fromUtf8("=={{int:filedesc}}==");
202     text.append(QLatin1String("\n{{Information"));
203     text.append(QLatin1String("\n|Description=")).append(info[QLatin1String("description")]);
204     text.append(QLatin1String("\n|Source="));
205 
206     if (!info[QLatin1String("source")].isEmpty())
207     {
208         text.append(info[QLatin1String("source")]);
209     }
210 
211     text.append(QLatin1String("\n|Author="));
212 
213     if (!info[QLatin1String("author")].isEmpty())
214     {
215         text.append(info[QLatin1String("author")]);
216     }
217 
218     text.append(QLatin1String("\n|Date=")).append(info[QLatin1String("date")]);
219     text.append(QLatin1String("\n|Permission="));
220     text.append(QLatin1String("\n|other_versions="));
221     text.append(QLatin1String("\n}}\n"));
222 
223     QString latitude  = info[QLatin1String("latitude")];
224     QString longitude = info[QLatin1String("longitude")];
225 
226     if (!latitude.isEmpty() && !longitude.isEmpty())
227     {
228         qCDebug(KIPIPLUGINS_LOG) << "Latitude:" << latitude << "; longitude:" << longitude;
229         text.append(QLatin1String("{{Location|")).append(latitude).append(QLatin1String("|")).append(longitude).append(QLatin1String("}}\n"));
230     }
231 
232     if (!info[QLatin1String("genText")].isEmpty())
233     {
234         text.append(info[QLatin1String("genText")]).append(QLatin1String("\n"));
235     }
236 
237     if (!info[QLatin1String("license")].isEmpty())
238     {
239         text.append(QLatin1String("\n=={{int:license-header}}==\n"));
240         text.append(info[QLatin1String("license")]).append(QLatin1String("\n\n"));
241     }
242 
243     QStringList categories;
244 
245     if (!info[QLatin1String("categories")].isEmpty())
246     {
247         categories = info[QLatin1String("categories")].split(QLatin1String("\n"), QString::SkipEmptyParts);
248 
249         for (int i = 0; i < categories.size(); i++)
250         {
251             text.append(QLatin1String("[[Category:")).append(categories[i]).append(QLatin1String("]]\n"));
252         }
253     }
254 
255     if (!info[QLatin1String("genCategories")].isEmpty())
256     {
257         categories = info[QLatin1String("genCategories")].split(QLatin1String("\n"), QString::SkipEmptyParts);
258 
259         for (int i = 0; i < categories.size(); i++)
260         {
261             text.append(QLatin1String("[[Category:")).append(categories[i]).append(QLatin1String("]]\n"));
262         }
263     }
264 
265     return text;
266 }
267 
slotUploadProgress(KJob * job,unsigned long percent)268 void WMTalker::slotUploadProgress(KJob* job, unsigned long percent)
269 {
270     Q_UNUSED(job)
271     emit uploadProgress((int)percent);
272 }
273 
274 } // namespace KIPIWikiMediaPlugin
275