Debian Codegraph @

src/gui/operations/SaveOperation.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "SaveOperation.h"
21
22 #include <utility>
23
24 SaveOperation::SaveOperation(
25 QImage image,
26 bool isInstantSave,
27 const QSharedPointer<INotificationService> &notificationService,
28 const QSharedPointer<IRecentImageService> &recentImageService,
29 const QSharedPointer<IImageSaver> &imageSaver,
30 const QSharedPointer<ISavePathProvider> &savePathProvider,
31 const QSharedPointer<IFileDialogService> &fileDialogService,
32 const QSharedPointer<IConfig> &config,
33 QWidget *parent) :
34 mParent(parent),
35 mImage(std::move(image)),
36 mIsInstantSave(isInstantSave),
37 mNotificationService(notificationService),
38 mRecentImageService(recentImageService),
39 mImageSaver(imageSaver),
40 mSavePathProvider(savePathProvider),
41 mFileDialogService(fileDialogService),
42 mConfig(config)
43 {
44 Q_ASSERT(mParent != nullptr);
45 }
46
47 SaveOperation::SaveOperation(
48 const QImage &image,
49 bool isInstantSave,
50 const QString &pathToImageSource,
51 const QSharedPointer<INotificationService> &notificationService,
52 const QSharedPointer<IRecentImageService> &recentImageService,
53 const QSharedPointer<IImageSaver> &imageSaver,
54 const QSharedPointer<ISavePathProvider> &savePathProvider,
55 const QSharedPointer<IFileDialogService> &fileDialogService,
56 const QSharedPointer<IConfig> &config,
57 QWidget *parent) :
58 SaveOperation(image, isInstantSave, notificationService, recentImageService, imageSaver, savePathProvider, fileDialogService, config, parent)
59 {
60 mPathToImageSource = pathToImageSource;
61 }
62
63 SaveResultDto SaveOperation::execute()
64 {
65 auto path = getSavePath();
66
67 if(!mIsInstantSave){
68 auto title = tr("Save As");
69 auto filter = tr("Image Files") + FileDialogFilterHelper::ImageFilesExport() + tr("All Files") + FileDialogFilterHelper::AllFiles();
70 auto selectedSavePath = mFileDialogService->getSavePath(mParent, title, path, filter);
71
72 if (selectedSavePath.isNull()) {
73 return SaveResultDto(false, path);
74 }
75
76 path = selectedSavePath;
77 }
78
79 auto saveResult = save(path);
80 updateSaveDirectoryIfRequired(path, saveResult);
81
82 if (saveResult.isSuccessful) {
83 mRecentImageService->storeImagePath(path);
84 }
85
86 return saveResult;
87 }
88
89 void SaveOperation::updateSaveDirectoryIfRequired(const QString &path, const SaveResultDto &saveResult) const
90 {
91 if(!mIsInstantSave && saveResult.isSuccessful && mConfig->rememberLastSaveDirectory()){
92 auto directory = PathHelper::extractParentDirectory(path);
93 mConfig->setSaveDirectory(directory);
94 }
95 }
96
97 QString SaveOperation::getSavePath() const
98 {
99 return PathHelper::isPathValid(mPathToImageSource) ? mPathToImageSource : mSavePathProvider->savePath();
100 }
101
102 SaveResultDto SaveOperation::save(const QString &path)
103 {
104 auto successful = mImageSaver->save(mImage, path);
105 if(successful) {
106 notify(tr("Image Saved"), tr("Saved to %1").arg(path), path, NotificationTypes::Information);
107 } else {
108 notify(tr("Saving Image Failed"), tr("Failed to save image to %1").arg(path), path, NotificationTypes::Critical);
109 }
110 return SaveResultDto(successful, path);
111 }
112
113 void SaveOperation::notify(const QString &title, const QString &message, const QString &path, NotificationTypes notificationType) const
114 {
115 NotifyOperation operation(title, message, path, notificationType, mNotificationService, mConfig);
116 operation.execute();
117 }