main
  1using Gorilla.Commons.Infrastructure.FileSystem;
  2using Gorilla.Commons.Infrastructure.Logging;
  3using gorilla.commons.utility;
  4using momoney.presentation.model.eventing;
  5using momoney.service.infrastructure;
  6using MoMoney.Service.Infrastructure.Eventing;
  7using momoney.service.infrastructure.transactions;
  8
  9namespace MoMoney.Presentation.Model.Projects
 10{
 11    public class ProjectController : IProjectController, Callback<IUnitOfWork>
 12    {
 13        readonly IEventAggregator broker;
 14        readonly IProjectTasks configuration;
 15        IProject project;
 16        bool unsaved_changes = false;
 17
 18        public ProjectController(IEventAggregator broker, IProjectTasks configuration)
 19        {
 20            this.broker = broker;
 21            this.configuration = configuration;
 22            broker.subscribe(this);
 23            project = new EmptyProject();
 24        }
 25
 26        public string name()
 27        {
 28            return project.name();
 29        }
 30
 31        public void start_new_project()
 32        {
 33            close_project();
 34            project = new Project(null);
 35            broker.publish(new NewProjectOpened(name()));
 36        }
 37
 38        public void open_project_from(File file)
 39        {
 40            if (!file.does_the_file_exist()) return;
 41            close_project();
 42            configuration.open(file);
 43            project = new Project(file);
 44            broker.publish(new NewProjectOpened(name()));
 45        }
 46
 47        public void save_changes()
 48        {
 49            ensure_that_a_path_to_save_to_has_been_specified();
 50            configuration.copy_to(project.name());
 51            unsaved_changes = false;
 52            broker.publish<SavedChangesEvent>();
 53        }
 54
 55        public void save_project_to(File new_file)
 56        {
 57            if (new_file.path.is_blank()) return;
 58            project = new Project(new_file);
 59            save_changes();
 60        }
 61
 62        public void close_project()
 63        {
 64            if (!project.is_open()) return;
 65            configuration.close(project.name());
 66            project = new EmptyProject();
 67            broker.publish<ClosingProjectEvent>();
 68        }
 69
 70        public bool has_been_saved_at_least_once()
 71        {
 72            return project.is_file_specified();
 73        }
 74
 75        public bool has_unsaved_changes()
 76        {
 77            return unsaved_changes;
 78        }
 79
 80        public bool is_open()
 81        {
 82            return project.is_open();
 83        }
 84
 85        void ensure_that_a_path_to_save_to_has_been_specified()
 86        {
 87            if (!has_been_saved_at_least_once()) throw new FileNotSpecifiedException();
 88        }
 89
 90        public void run(IUnitOfWork item)
 91        {
 92            unsaved_changes = item.is_dirty();
 93            if (unsaved_changes)
 94            {
 95                this.log().debug("unsaved changes: {0}", unsaved_changes);
 96                broker.publish<UnsavedChangesEvent>();
 97            }
 98        }
 99    }
100}