1// Aseprite
2// Copyright (C) 2021 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/app.h"
13#include "app/commands/command.h"
14#include "app/commands/commands.h"
15#include "app/context.h"
16#include "app/doc.h"
17#include "app/job.h"
18#include "app/ui/main_window.h"
19#include "ui/alert.h"
20
21#ifdef ENABLE_SCRIPTING
22#include "app/commands/debugger.h"
23#endif
24
25namespace app {
26
27class ExitCommand : public Command {
28public:
29 ExitCommand();
30
31protected:
32 void onExecute(Context* context) override;
33};
34
35ExitCommand::ExitCommand()
36 : Command(CommandId::Exit(), CmdUIOnlyFlag)
37{
38}
39
40void ExitCommand::onExecute(Context* ctx)
41{
42 // Ignore ExitCommand when we are saving documents or doing a
43 // background task
44 if (Job::runningJobs() > 0)
45 return;
46
47#ifdef ENABLE_SCRIPTING
48 if (auto debuggerCommand = dynamic_cast<DebuggerCommand*>(
49 Commands::instance()->byId(CommandId::Debugger()))) {
50 debuggerCommand->closeDebugger(ctx);
51 }
52#endif
53
54 if (ctx->hasModifiedDocuments()) {
55 Command* closeAll = Commands::instance()->byId(CommandId::CloseAllFiles());
56 Params params;
57 params.set("quitting", "1");
58 ctx->executeCommand(closeAll, params);
59
60 // The user didn't save all documents (canceled the exit)
61 if (ctx->hasModifiedDocuments())
62 return;
63 }
64
65 // Close the window
66 App::instance()->mainWindow()->closeWindow(NULL);
67}
68
69Command* CommandFactory::createExitCommand()
70{
71 return new ExitCommand;
72}
73
74} // namespace app
75