Debian Codegraph @

src/gui/operations/CanDiscardOperation.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 "CanDiscardOperation.h"
21
22 CanDiscardOperation::CanDiscardOperation(
23 QImage image,
24 bool isUnsaved,
25 QString pathToImageSource,
26 QString filename,
27 const QSharedPointer<INotificationService> &notificationService,
28 const QSharedPointer<IRecentImageService> &recentImageService,
29 const QSharedPointer<IMessageBoxService> &messageBoxService,
30 const QSharedPointer<IImageSaver> &imageSaver,
31 const QSharedPointer<ISavePathProvider> &savePathProvider,
32 const QSharedPointer<IFileDialogService> &fileDialogService,
33 const QSharedPointer<IConfig> &config,
34 QWidget *parent) :
35 mParent(parent),
36 mImage(std::move(image)),
37 mIsUnsaved(isUnsaved),
38 mPathToImageSource(std::move(pathToImageSource)),
39 mFilename(std::move(filename)),
40 mConfig(config),
41 mNotificationService(notificationService),
42 mMessageBoxService(messageBoxService),
43 mRecentImageService(recentImageService),
44 mImageSaver(imageSaver),
45 mSavePathProvider(savePathProvider),
46 mFileDialogService(fileDialogService)
47 {
48
49 }
50
51 bool CanDiscardOperation::execute()
52 {
53 if (mConfig->promptSaveBeforeExit() && mIsUnsaved) {
54 auto saveBeforeDiscardResponse = getSaveBeforeDiscard();
55
56 if (saveBeforeDiscardResponse == MessageBoxResponse::Yes) {
57 return saveImage();
58 } else if (saveBeforeDiscardResponse == MessageBoxResponse::Cancel) {
59 return false;
60 }
61 }
62
63 return true;
64 }
65
66 bool CanDiscardOperation::saveImage() const
67 {
68 SaveOperation operation(
69 mImage,
70 true,
71 mPathToImageSource,
72 mNotificationService,
73 mRecentImageService,
74 mImageSaver,
75 mSavePathProvider,
76 mFileDialogService,
77 mConfig,
78 mParent);
79 return operation.execute().isSuccessful;
80 }
81
82 MessageBoxResponse CanDiscardOperation::getSaveBeforeDiscard() const
83 {
84 auto quote = mFilename.isEmpty() ? QString() : QLatin1String("\"");
85 return mMessageBoxService->yesNoCancel(tr("Warning - ") + QApplication::applicationName(),
86 tr("The capture %1%2%3 has been modified.\nDo you want to save it?").arg(quote, mFilename, quote));
87 }