Esempio n. 1
0
 public Controller(Ledger ledger, Timer timer)
 {
     this.ledger = ledger;
     this.timer = timer;
 }
Esempio n. 2
0
 public Controller(Ledger ledger, Timer timer)
 {
     this.ledger = ledger;
     this.timer  = timer;
 }
Esempio n. 3
0
        public View()
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), Settings.Default.Ledger);

            if (!File.Exists(path)) {
                File.WriteAllText(path, Settings.Default.NewLedger);
                MessageBox.Show(string.Format(Settings.Default.CreatedLedger, path), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            //create model and controller
            ledger = new Ledger(path);
            timer = new Timer();
            controller = new Controller(ledger, timer);
            tray = new NotifyIcon() { Icon = Resources.Off };

            //subscribe to model
            timer.TimingChanged += timing => { tray.Icon = timing ? Resources.On : Resources.Off; };
            ledger.ActiveChanged += active => { tray.Text = active == null ? Application.ProductName : active; };
            ledger.LedgerChanged += CreateContextMenu;

            controller.LoadLedger();

            //watch for changes to the ledger
            watcher = new FileSystemWatcher(Path.GetDirectoryName(ledger.File), Path.GetFileName(ledger.File)) { NotifyFilter = NotifyFilters.LastWrite };

            watcher.Changed += (sender, e) => {
                BypassWatcher(delegate {
                    if (timer.Timing)
                        MessageBox.Show(Settings.Default.ChangedWhileTiming, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else {
                        //wait for file locks
                        Thread.Sleep(200);
                        try {
                            if (tray.ContextMenuStrip.InvokeRequired)
                                tray.ContextMenuStrip.Invoke(new Action(controller.LoadLedger));
                            else
                                controller.LoadLedger();
                        }
                        catch (Exception ex) {
                            Program.Handle(ex);
                            MessageBox.Show(Settings.Default.CannotLoad, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        tray.BalloonTipText = Settings.Default.Reloaded;
                        tray.ShowBalloonTip(1000);
                    }
                });
            };

            watcher.EnableRaisingEvents = true;

            //subscribe to mouse event
            tray.MouseClick += (sender, e) => {
                if (e.Button == MouseButtons.Left)
                    if (timer.Timing)
                        BypassWatcher(controller.Stop);
                    else {
                        if (ledger.Active == null)
                            MessageBox.Show(Settings.Default.NoActiveItem, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        else
                            controller.Start();
                    }
            };
            tray.Visible = true;
        }