Debian Codegraph @

core/dplugins/generic/tools/jalbum/generator/jalbumgenerator.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2006-04-04
7 * Description : a tool to generate jAlbum image galleries
8 *
9 * SPDX-FileCopyrightText: 2013-2019 by Andrew Goodbody <ajg zero two at elfringham dot co dot uk>
10 *
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 * ============================================================ */
14
15 #include "jalbumgenerator.h"
16
17 // Qt includes
18
19 #include <QDir>
20 #include <QFutureWatcher>
21 #include <QRegularExpression>
22 #include <QStringList>
23 #include <QApplication>
24 #include <QProcess>
25 #include <QUrl>
26 #include <QList>
27
28 // KDE includes
29
30 #include <klocalizedstring.h>
31
32 // Local includes
33
34 #include "digikam_debug.h"
35 #include "digikam_globals.h"
36 #include "jalbumsettings.h"
37 #include "jalbumwizard.h"
38 #include "dfileoperations.h"
39
40 namespace DigikamGenericJAlbumPlugin
41 {
42
43 class Q_DECL_HIDDEN JAlbumGenerator::Private
44 {
45 public:
46
47 Private() = default;
48
49 public:
50
51 JAlbumGenerator* that = nullptr;
52 JAlbumSettings* settings = nullptr;
53 QList<QUrl> urls;
54
55 // State settings
56 bool warnings = false;
57
58 bool cancel = false;
59 DHistoryView* pview = nullptr;
60 DProgressWdg* pbar = nullptr;
61
62 public:
63
64 bool init()
65 {
66 cancel = false;
67
68 pview->setVisible(true);
69 pbar->setVisible(true);
70
71 return true;
72 }
73
74 bool createDir(const QString& dirName)
75 {
76 logInfo(i18n("Create directories"));
77
78 if (!QDir().mkpath(dirName))
79 {
80 logError(i18n("Could not create folder '%1'", QDir::toNativeSeparators(dirName)));
81 return false;
82 }
83
84 return true;
85 }
86
87 bool createUrlsList()
88 {
89 if (settings->m_getOption == JAlbumSettings::ALBUMS)
90 {
91 // Loop over albums selection
92
93 DInfoInterface::DAlbumIDs::ConstIterator albumIt = settings->m_albumList.constBegin();
94 DInfoInterface::DAlbumIDs::ConstIterator albumEnd = settings->m_albumList.constEnd();
95
96 for ( ; albumIt != albumEnd ; ++albumIt)
97 {
98 int id = *albumIt;
99
100 // Gather image element list
101 QList<QUrl> imageList;
102
103 if (settings->m_iface)
104 {
105 imageList = settings->m_iface->albumsItems(DInfoInterface::DAlbumIDs() << id);
106 urls.append(imageList);
107 }
108 }
109 }
110 else
111 {
112 urls = settings->m_imageList;
113 }
114
115 return true;
116 }
117
118 bool createProjectFiles(const QString& projDir)
119 {
120 logInfo(i18n("Create jAlbum project files"));
121
122 QDir newAlbumDir = QDir(projDir);
123
124 QFile createFile(newAlbumDir.filePath(QString::fromLatin1("albumfiles.txt")));
125
126 if (!createFile.open(QIODevice::WriteOnly | QIODevice::Text))
127 {
128 logInfo(i18n("Failed to create project files"));
129 return false;
130 }
131
132 QTextStream out(&createFile);
133
134 for (QList<QUrl>::ConstIterator it = urls.constBegin(); it != urls.constEnd(); ++it)
135 {
136 out << (*it).fileName().toLocal8Bit().data() << "\t" << (*it).path().toLocal8Bit().data() << "\n";
137 }
138
139 createFile.close();
140
141 QFile settingsFile(newAlbumDir.filePath(QString::fromLatin1("jalbum-settings.jap")));
142
143 if (!settingsFile.open(QIODevice::WriteOnly | QIODevice::Text))
144 {
145 logInfo(i18n("Failed to create settings file"));
146 return false;
147 }
148
149 QTextStream out2(&settingsFile);
150 out2 << "#jAlbum Project\n";
151
152 settingsFile.close();
153
154 return true;
155 }
156
157 bool launchJalbum(const QString& projDir, const QString& jarDir, const QString& javaExecutable)
158 {
159 logInfo(i18n("Launch jAlbum with new project files"));
160
161 /*
162 QString javaExecutable;
163 QDir jrePath = QFileInfo(jarDir).dir();
164
165 if (jrePath.cd(QString::fromLatin1("jre64/bin/")))
166 {
167 javaExecutable = jrePath.filePath(QString::fromLatin1("java"));
168 }
169 else
170 {
171 javaExecutable = QString::fromLatin1("java");
172 }
173 */
174
175 QDir newAlbumDir = QDir(projDir);
176
177 QStringList args;
178 args.append(QString::fromLatin1("-Xmx400M"));
179 args.append(QString::fromLatin1("-jar"));
180 args.append(jarDir);
181 args.append(QDir::toNativeSeparators(newAlbumDir.filePath(QString::fromLatin1("jalbum-settings.jap"))));
182
183 QProcess process;
184 process.setProcessEnvironment(adjustedEnvironmentForAppImage());
185 process.startDetached(javaExecutable, args);
186
187 return true;
188 }
189
190 void logInfo(const QString& msg)
191 {
192 pview->addEntry(msg, DHistoryView::ProgressEntry);
193 }
194
195 void logError(const QString& msg)
196 {
197 pview->addEntry(msg, DHistoryView::ErrorEntry);
198 }
199
200 void logWarning(const QString& msg)
201 {
202 pview->addEntry(msg, DHistoryView::WarningEntry);
203 warnings = true;
204 }
205 };
206
207 // ----------------------------------------------------------------------
208
209 JAlbumGenerator::JAlbumGenerator(JAlbumSettings* const settings)
210 : QObject(),
211 d (new Private)
212 {
213 d->that = this;
214 d->settings = settings;
215 d->warnings = false;
216
217 connect(this, SIGNAL(logWarningRequested(QString)),
218 SLOT(logWarning(QString)), Qt::QueuedConnection);
219 }
220
221 JAlbumGenerator::~JAlbumGenerator()
222 {
223 delete d;
224 }
225
226 bool JAlbumGenerator::run()
227 {
228 if (!d->init())
229 {
230 return false;
231 }
232
233 QString destDir = d->settings->m_destPath;
234 qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << destDir;
235
236 QString javaDir = d->settings->m_javaPath;
237 qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << javaDir;
238
239 QString jarDir = d->settings->m_jalbumPath;
240 qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << jarDir;
241
242 QString projDir = destDir + QString::fromLatin1("/") + d->settings->m_imageSelectionTitle;
243 qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << projDir;
244
245 if (!d->createDir(projDir))
246 {
247 return false;
248 }
249
250 bool result = d->createUrlsList();
251
252 if (result)
253 {
254 result = d->createProjectFiles(projDir);
255 }
256
257 if (result)
258 {
259 result = d->launchJalbum(projDir, jarDir, javaDir);
260 }
261
262 return result;
263 }
264
265 bool JAlbumGenerator::warnings() const
266 {
267 return d->warnings;
268 }
269
270 void JAlbumGenerator::logWarning(const QString& text)
271 {
272 d->logWarning(text);
273 }
274
275 void JAlbumGenerator::slotCancel()
276 {
277 d->cancel = true;
278 }
279
280 void JAlbumGenerator::setProgressWidgets(DHistoryView* const pView, DProgressWdg* const pBar)
281 {
282 d->pview = pView;
283 d->pbar = pBar;
284
285 connect(d->pbar, SIGNAL(signalProgressCanceled()),
286 this, SLOT(slotCancel()));
287 }
288
289 } // namespace DigikamGenericJAlbumPlugin
290
291 #include "moc_jalbumgenerator.cpp"