1 /* ============================================================
2  *
3  * This file is a part of KDE project
4  *
5  *
6  * Date        : 2009-09-28
7  * Description : a tool to export image to a KIO accessible
8  *               location
9  *
10  * Copyright (C) 2006-2009 by Johannes Wienke <languitar at semipol dot de>
11  * Copyright (C) 2011-2018 by Gilles Caulier <caulier dot gilles at gmail dot com>
12  *
13  * This program is free software; you can redistribute it
14  * and/or modify it under the terms of the GNU General
15  * Public License as published by the Free Software Foundation;
16  * either version 2, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #include "KioExportWindow.h"
26 
27 // Qt includes
28 
29 #include <QWindow>
30 #include <QCloseEvent>
31 #include <QMenu>
32 #include <QMessageBox>
33 
34 // KDE includes
35 
36 #include <kio/copyjob.h>
37 #include <kconfig.h>
38 #include <klocalizedstring.h>
39 #include <kwindowconfig.h>
40 
41 // Local includes
42 
43 #include "kipiplugins_debug.h"
44 #include "kpaboutdata.h"
45 #include "kpversion.h"
46 #include "kpimageslist.h"
47 #include "KioExportWidget.h"
48 
49 namespace KIPIRemoteStoragePlugin
50 {
51 
52 const QString KioExportWindow::TARGET_URL_PROPERTY  = QString::fromLatin1("targetUrl");
53 const QString KioExportWindow::HISTORY_URL_PROPERTY = QString::fromLatin1("historyUrls");
54 const QString KioExportWindow::CONFIG_GROUP         = QString::fromLatin1("KioExport");
55 
KioExportWindow(QWidget * const)56 KioExportWindow::KioExportWindow(QWidget* const /*parent*/)
57     : KPToolDialog(nullptr)
58 {
59     m_exportWidget = new KioExportWidget(this);
60     setMainWidget(m_exportWidget);
61 
62     // -- Window setup ------------------------------------------------------
63 
64     setWindowTitle(i18n("Export to Remote Storage"));
65     setModal(false);
66 
67     startButton()->setText(i18n("Start export"));
68     startButton()->setToolTip(i18n("Start export to the specified target"));
69 
70     connect(startButton(), SIGNAL(clicked()),
71             this, SLOT(slotUpload()));
72 
73     connect(this, SIGNAL(finished(int)),
74             this, SLOT(slotFinished()));
75 
76     connect(m_exportWidget->imagesList(), SIGNAL(signalImageListChanged()),
77             this, SLOT(slotImageListChanged()));
78 
79     connect(m_exportWidget, SIGNAL(signalTargetUrlChanged(QUrl)),
80             this, SLOT(slotTargetUrlChanged(QUrl)));
81 
82     // -- About data and help button ----------------------------------------
83 
84     KPAboutData* const about = new KPAboutData(ki18n("Export to remote storage"),
85                                                ki18n("A tool to export images over network using KIO-Slave"),
86                                                ki18n("(c) 2009, Johannes Wienke"));
87 
88     about->addAuthor(ki18n("Johannes Wienke").toString(),
89                      ki18n("Developer and maintainer").toString(),
90                      QString::fromLatin1("languitar at semipol dot de"));
91 
92     about->setHandbookEntry(QString::fromLatin1("tool-remotestorage"));
93     setAboutData(about);
94 
95     // -- initial sync ------------------------------------------------------
96 
97     restoreSettings();
98     updateUploadButton();
99 }
100 
~KioExportWindow()101 KioExportWindow::~KioExportWindow()
102 {
103 }
104 
slotFinished()105 void KioExportWindow::slotFinished()
106 {
107     saveSettings();
108     m_exportWidget->imagesList()->listView()->clear();
109 }
110 
closeEvent(QCloseEvent * e)111 void KioExportWindow::closeEvent(QCloseEvent* e)
112 {
113     if (!e)
114     {
115         return;
116     }
117 
118     slotFinished();
119     e->accept();
120 }
121 
reactivate()122 void KioExportWindow::reactivate()
123 {
124     m_exportWidget->imagesList()->loadImagesFromCurrentSelection();
125     show();
126 }
127 
restoreSettings()128 void KioExportWindow::restoreSettings()
129 {
130     qCDebug(KIPIPLUGINS_LOG) <<  "pass here";
131     KConfig config(QString::fromLatin1("kipirc"));
132     KConfigGroup group  = config.group(CONFIG_GROUP);
133     m_exportWidget->setHistory(group.readEntry(HISTORY_URL_PROPERTY, QList<QUrl>()));
134     m_exportWidget->setTargetUrl(group.readEntry(TARGET_URL_PROPERTY, QUrl()));
135 
136     winId();
137     KConfigGroup group2 = config.group(QString::fromLatin1("Kio Export Dialog"));
138     KWindowConfig::restoreWindowSize(windowHandle(), group2);
139     resize(windowHandle()->size());
140 }
141 
saveSettings()142 void KioExportWindow::saveSettings()
143 {
144     qCDebug(KIPIPLUGINS_LOG) <<  "pass here";
145     KConfig config(QString::fromLatin1("kipirc"));
146     KConfigGroup group = config.group(CONFIG_GROUP);
147     group.writeEntry(HISTORY_URL_PROPERTY, m_exportWidget->history());
148     group.writeEntry(TARGET_URL_PROPERTY,  m_exportWidget->targetUrl().url());
149 
150     KConfigGroup group2 = config.group(QString::fromLatin1("Kio Export Dialog"));
151     KWindowConfig::saveWindowSize(windowHandle(), group2);
152     config.sync();
153 }
154 
slotImageListChanged()155 void KioExportWindow::slotImageListChanged()
156 {
157     updateUploadButton();
158 }
159 
slotTargetUrlChanged(const QUrl & target)160 void KioExportWindow::slotTargetUrlChanged(const QUrl & target)
161 {
162     Q_UNUSED(target);
163     updateUploadButton();
164 }
165 
updateUploadButton()166 void KioExportWindow::updateUploadButton()
167 {
168     bool listNotEmpty = !m_exportWidget->imagesList()->imageUrls().empty();
169     startButton()->setEnabled(listNotEmpty && m_exportWidget->targetUrl().isValid());
170 
171     qCDebug(KIPIPLUGINS_LOG) << "Updated upload button with listNotEmpty = "
172                              << listNotEmpty << ", targetUrl().isValid() = "
173                              << m_exportWidget->targetUrl().isValid();
174 }
175 
slotCopyingDone(KIO::Job * job,const QUrl & from,const QUrl & to,const QDateTime & mtime,bool directory,bool renamed)176 void KioExportWindow::slotCopyingDone(KIO::Job* job, const QUrl& from, const QUrl& to,
177                                       const QDateTime& mtime, bool directory, bool renamed)
178 {
179     Q_UNUSED(job);
180     Q_UNUSED(to);
181     Q_UNUSED(mtime);
182     Q_UNUSED(directory);
183     Q_UNUSED(renamed);
184 
185     qCDebug(KIPIPLUGINS_LOG) << "copied " << to.toDisplayString();
186 
187     m_exportWidget->imagesList()->removeItemByUrl(from);
188 }
189 
slotCopyingFinished(KJob * job)190 void KioExportWindow::slotCopyingFinished(KJob* job)
191 {
192     Q_UNUSED(job);
193 
194     setEnabled(true);
195 
196     if (!m_exportWidget->imagesList()->imageUrls().empty())
197     {
198         QMessageBox::information(this, i18n("Upload not completed"),
199                                  i18n("Some of the images have not been transferred "
200                                       "and are still in the list. "
201                                       "You can retry to export these images now."));
202     }
203 }
204 
slotUpload()205 void KioExportWindow::slotUpload()
206 {
207     saveSettings();
208 
209     // start copying and react on signals
210     setEnabled(false);
211     KIO::CopyJob* const copyJob = KIO::copy(m_exportWidget->imagesList()->imageUrls(),
212                                             m_exportWidget->targetUrl());
213 
214     connect(copyJob, SIGNAL(copyingDone(KIO::Job*, QUrl, QUrl, QDateTime, bool, bool)),
215             this, SLOT(slotCopyingDone(KIO::Job*, QUrl, QUrl, QDateTime, bool, bool)));
216 
217     connect(copyJob, SIGNAL(result(KJob*)),
218             this, SLOT(slotCopyingFinished(KJob*)));
219 }
220 
221 } // namespace KIPIRemoteStoragePlugin
222