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 HTML image galleries
8  *
9  * Copyright (C) 2012-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * ============================================================ */
22 
23 #include "htmlfinalpage.h"
24 
25 // Qt includes
26 
27 #include <QIcon>
28 #include <QSpacerItem>
29 #include <QVBoxLayout>
30 #include <QDesktopServices>
31 #include <QUrl>
32 #include <QApplication>
33 #include <QStyle>
34 #include <QTimer>
35 #include <QDir>
36 
37 // KDE includes
38 
39 #include <klocalizedstring.h>
40 
41 // Local includes
42 
43 #include "htmlwizard.h"
44 #include "abstractthemeparameter.h"
45 #include "galleryinfo.h"
46 #include "gallerygenerator.h"
47 #include "dlayoutbox.h"
48 #include "digikam_debug.h"
49 #include "dprogresswdg.h"
50 #include "dhistoryview.h"
51 #include "webbrowserdlg.h"
52 
53 namespace DigikamGenericHtmlGalleryPlugin
54 {
55 
56 class Q_DECL_HIDDEN HTMLFinalPage::Private
57 {
58 public:
59 
Private()60     explicit Private()
61       : progressView(nullptr),
62         progressBar (nullptr),
63         complete    (false)
64     {
65     }
66 
67     DHistoryView* progressView;
68     DProgressWdg* progressBar;
69     bool          complete;
70 };
71 
HTMLFinalPage(QWizard * const dialog,const QString & title)72 HTMLFinalPage::HTMLFinalPage(QWizard* const dialog, const QString& title)
73     : DWizardPage(dialog, title),
74       d          (new Private)
75 {
76     setObjectName(QLatin1String("FinalPage"));
77 
78     DVBox* const vbox = new DVBox(this);
79     d->progressView   = new DHistoryView(vbox);
80     d->progressBar    = new DProgressWdg(vbox);
81 
82     vbox->setStretchFactor(d->progressBar, 10);
83     vbox->setContentsMargins(QMargins());
84     vbox->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
85 
86     setPageWidget(vbox);
87     setLeftBottomPix(QIcon::fromTheme(QLatin1String("system-run")));
88 }
89 
~HTMLFinalPage()90 HTMLFinalPage::~HTMLFinalPage()
91 {
92     delete d;
93 }
94 
initializePage()95 void HTMLFinalPage::initializePage()
96 {
97     d->complete = false;
98     emit completeChanged();
99     QTimer::singleShot(0, this, SLOT(slotProcess()));
100 }
101 
slotProcess()102 void HTMLFinalPage::slotProcess()
103 {
104     HTMLWizard* const wizard = dynamic_cast<HTMLWizard*>(assistant());
105 
106     if (!wizard)
107     {
108         d->progressView->addEntry(i18n("Internal Error"),
109                                   DHistoryView::ErrorEntry);
110         return;
111     }
112 
113     d->progressView->clear();
114     d->progressBar->reset();
115 
116     GalleryInfo* const info  = wizard->galleryInfo();
117 
118     // Generate GalleryInfo
119 
120     qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << info;
121 
122     d->progressView->addEntry(i18n("Starting to generate gallery..."),
123                               DHistoryView::ProgressEntry);
124 
125     if (info->m_getOption == GalleryInfo::ALBUMS)
126     {
127         if (!info->m_iface)
128         {
129             return;
130         }
131 
132         d->progressView->addEntry(i18n("%1 albums to process:", info->m_albumList.count()),
133                                   DHistoryView::ProgressEntry);
134 
135         foreach (const QUrl& url, info->m_iface->albumsItems(info->m_albumList))
136         {
137             d->progressView->addEntry(QDir::toNativeSeparators(url.toLocalFile()),
138                                       DHistoryView::ProgressEntry);
139         }
140     }
141     else
142     {
143         d->progressView->addEntry(i18n("%1 items to process", info->m_imageList.count()),
144                                   DHistoryView::ProgressEntry);
145     }
146 
147     d->progressView->addEntry(i18n("Output directory: %1",
148                               QDir::toNativeSeparators(info->destUrl().toLocalFile())),
149                               DHistoryView::ProgressEntry);
150 
151     GalleryGenerator generator(info);
152     generator.setProgressWidgets(d->progressView, d->progressBar);
153 
154     if (!generator.run())
155     {
156         return;
157     }
158 
159     if (generator.warnings())
160     {
161         d->progressView->addEntry(i18n("Gallery is completed, but some warnings occurred."),
162                                   DHistoryView::WarningEntry);
163     }
164     else
165     {
166         d->progressView->addEntry(i18n("Gallery completed."),
167                                   DHistoryView::ProgressEntry);
168     }
169 
170     QUrl url = info->destUrl().adjusted(QUrl::StripTrailingSlash);
171     url.setPath(url.path() + QLatin1String("/index.html"));
172 
173     switch (info->openInBrowser())
174     {
175         case GalleryConfig::DESKTOP:
176         {
177             QDesktopServices::openUrl(url);
178             d->progressView->addEntry(i18n("Opening gallery with default desktop browser..."),
179                                       DHistoryView::ProgressEntry);
180             break;
181         }
182 
183         case GalleryConfig::INTERNAL:
184         {
185             WebBrowserDlg* const browser = new WebBrowserDlg(url, this);
186             browser->show();
187             d->progressView->addEntry(i18n("Opening gallery with internal browser..."),
188                                       DHistoryView::ProgressEntry);
189             break;
190         }
191 
192         default:
193         {
194             break;
195         }
196     }
197 
198     d->complete = true;
199     emit completeChanged();
200 }
201 
isComplete() const202 bool HTMLFinalPage::isComplete() const
203 {
204     return d->complete;
205 }
206 
207 } // namespace DigikamGenericHtmlGalleryPlugin
208