summaryrefslogtreecommitdiff
path: root/product/Presentation/Model/Projects/ProjectController.cs
blob: 3fffb228d1d9a99dc5ab7c4f94814b58b116bb76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Gorilla.Commons.Infrastructure.FileSystem;
using Gorilla.Commons.Infrastructure.Logging;
using Gorilla.Commons.Utility.Core;
using Gorilla.Commons.Utility.Extensions;
using MoMoney.Presentation.Model.messages;
using MoMoney.Service.Contracts.Infrastructure;
using MoMoney.Service.Infrastructure.Eventing;
using MoMoney.Service.Infrastructure.Transactions;

namespace MoMoney.Presentation.Model.Projects
{
    public class ProjectController : IProjectController, ICallback<IUnitOfWork>
    {
        readonly IEventAggregator broker;
        readonly IProjectTasks configuration;
        IProject project;
        bool unsaved_changes = false;

        public ProjectController(IEventAggregator broker, IProjectTasks configuration)
        {
            this.broker = broker;
            this.configuration = configuration;
            broker.subscribe(this);
            project = new EmptyProject();
        }

        public string name()
        {
            return project.name();
        }

        public void start_new_project()
        {
            close_project();
            project = new Project(null);
            broker.publish(new NewProjectOpened(name()));
        }

        public void open_project_from(IFile file)
        {
            if (!file.does_the_file_exist()) return;
            close_project();
            configuration.open(file);
            project = new Project(file);
            broker.publish(new NewProjectOpened(name()));
        }

        public void save_changes()
        {
            ensure_that_a_path_to_save_to_has_been_specified();
            configuration.copy_to(project.name());
            unsaved_changes = false;
            broker.publish<SavedChangesEvent>();
        }

        public void save_project_to(IFile new_file)
        {
            if (new_file.path.is_blank()) return;
            project = new Project(new_file);
            save_changes();
        }

        public void close_project()
        {
            if (!project.is_open()) return;
            configuration.close(project.name());
            project = new EmptyProject();
            broker.publish<ClosingProjectEvent>();
        }

        public bool has_been_saved_at_least_once()
        {
            return project.is_file_specified();
        }

        public bool has_unsaved_changes()
        {
            return unsaved_changes;
        }

        public bool is_open()
        {
            return project.is_open();
        }

        void ensure_that_a_path_to_save_to_has_been_specified()
        {
            if (!has_been_saved_at_least_once()) throw new FileNotSpecifiedException();
        }

        public void run(IUnitOfWork item)
        {
            unsaved_changes = item.is_dirty();
            if (unsaved_changes)
            {
                this.log().debug("unsaved changes: {0}", unsaved_changes);
                broker.publish<UnsavedChangesEvent>();
            }
        }
    }
}