Beispiel #1
0
        /// <summary>
        /// Updates the icon of the taskbar button
        /// </summary>
        public void UpdateIcon()
        {
            switch (DiscardCycle.DryRun(Program.GetDiscardDirectories().Select(i => new DirectoryInfo(i)), 0)
                    .DiscardFiles
                    .OrderBy(i => i.DaysLeft)
                    .FirstOrDefault()?
                    .DaysLeft ?? 3
                    )
            {
            case int n when n <= 0:
                _icon.Icon = Properties.Resources.DiscardError;
                _icon.Text = "One or more files are overdue for deletion";
                break;

            case 1:
                _icon.Icon = Properties.Resources.DiscardWarning;
                _icon.Text = "One or more files will be deleted tomorrow";
                break;

            default:
                _icon.Icon = Properties.Resources.DiscardOK;
#if DEBUG
                _icon.Text = "Discard (Debug Mode)";
#else
                _icon.Text = "Discard";
#endif
                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Runs a cycle if it hasn't been run today
        /// </summary>
        private static void RunOnAwake()
        {
            if (HasBeenRunToday())
            {
                //App has allready been run and will not be run again
                return;
            }

#if DEBUG
            //To allow debug mode to override the once-per-day rule
            Properties.Settings.Default.LastDiscardCycle = DateTime.Today.AddDays(-1);
#endif

            //Runs a discard cycle;
            DiscardCycle.RunNow(
                where : GetDiscardDirectories().Select(i => new DirectoryInfo(i)),
                cycles: (int)(DateTime.Today - Properties.Settings.Default.LastDiscardCycle).TotalDays
                );

            //Writes the flag file
            WriteFlagFile();

            _icon.UpdateIcon();
        }
Beispiel #3
0
        /// <summary>
        /// Regenerates the menu
        /// </summary>
        /// <returns></returns>
        private ContextMenuStrip GenerateContext()
        {
            //Clear existing items
            _context.Items.Clear();

            /*
             * FILES
             */

            //Fetches discard files
            DiscardCycle cycle = DiscardCycle.DryRun(Program.GetDiscardDirectories().Select(i => new DirectoryInfo(i)), 0);

            /*
             * Add tracked files
             */
            IEnumerable <IGrouping <int, DiscardFile> > trackedFiles = cycle.DiscardFiles
                                                                       .Where(i => !i.Untracked)
                                                                       .GroupBy(i => i.DaysLeft)
                                                                       .OrderBy(i => i.Key);

            foreach (IGrouping <int, DiscardFile> group in trackedFiles)
            {
                //Day header
                _context.Items.Add("-");
                ToolStripMenuItem header = new ToolStripMenuItem(group.Key + (Math.Abs(group.Key) == 1 ? " day" : " days"))
                {
                    Font = _boldFont,
                };

                switch (group.Key)
                {
                case int i when(i <= 0):
                    header.ForeColor = Color.Red;

                    header.BackColor = Color.Pink;
                    break;

                case 1:
                    header.BackColor = Color.Wheat;
                    break;

                case int i when(i > Properties.Settings.Default.DefaultDays):
                    header.ForeColor = Color.FromArgb(75, 75, 75);

                    break;
                }

                _context.Items.Add(header);

                foreach (DiscardFile i in group)
                {
                    if (group.Count() >= 8)
                    {
                        header.DropDownItems.Add(CreateContextMenuForDiscardFile(i));
                    }
                    else
                    {
                        _context.Items.Add(CreateContextMenuForDiscardFile(i));
                    }
                }
            }

            /*
             * Add untracked files
             */
            IEnumerable <DiscardFile> untrackedFiles = cycle.DiscardFiles
                                                       .Where(i => i.Untracked);

            if (untrackedFiles.Any())
            {
                _context.Items.Add("-");

                ToolStripMenuItem header = new ToolStripMenuItem("Untracked")
                {
                    Font      = _boldFont,
                    ForeColor = Color.DodgerBlue,
                    BackColor = Color.LightSkyBlue
                };

                _context.Items.Add(header);

                foreach (DiscardFile i in untrackedFiles)
                {
                    if (untrackedFiles.Count() >= 8)
                    {
                        _context.Items.Add(CreateContextMenuForDiscardFile(i));
                    }
                    else
                    {
                        _context.Items.Add(CreateContextMenuForDiscardFile(i));
                    }
                }
            }



            /*
             * The other stuff
             */
            _context.Items.Add("-");
            _context.Items.Add("&Settings...", null, (s, e) => new Settings().ShowDialog());
            _context.Items.Add("&Create...", null, (s, e) => new CreateFileDialog().ShowDialog()).Enabled = Program.GetDiscardDirectories().Any();

            //Open button
            _context.Items.Add("&Open...", null).Enabled = Program.GetDiscardDirectories().Any();
            {
                ToolStripMenuItem last = (ToolStripMenuItem)_context.Items[_context.Items.Count - 1];
                foreach (string i in Program.GetDiscardDirectories())
                {
                    last.DropDownItems.Add(i, null, (s, e) => Process.Start(i));
                }
            }

            _context.Items.Add("-");
            _context.Items.Add("Exit", null, (s, e) => Application.Exit());

            return(_context);
        }
Beispiel #4
0
        /// <summary>
        /// Runs a discard cycle now on the given directory
        /// </summary>
        public static bool RunNow(IEnumerable <DirectoryInfo> where, int cycles)
        {
            //Do not show dialog if the application is already running
            if (IsRunning)
            {
                Console.WriteLine("Discard was already running a cycle and will not run again");
                return(false);
            }

            IsRunning = true;

            DiscardCycle _ = new DiscardCycle(where, cycles);

            //Updates file labels
            foreach (DiscardFile i in _.DiscardFiles)
            {
                try
                {
                    i.DaysLeft -= cycles;
                }
                catch (Exception)
                {
                    Console.WriteLine("Could not update file label. File might be locked.");
                }
            }

            //Shows the dialog for the files that are to be deleted
            IEnumerable <DiscardFile> filesToPrompt    = _.DiscardFiles.Where(i => i.Expired && !i.NoWarning);
            List <DiscardFile>        filesForDeletion = _.DiscardFiles.Where(i => i.NoWarning && i.Expired).ToList();

            if (filesToPrompt.Any())
            {
                DiscardDialogFull dia = new DiscardDialogFull(filesToPrompt);
                dia.ShowDialog();

                if (dia.DialogResult == DialogResult.Cancel)
                {
                    IsRunning = false;
                    return(false);
                }

                filesForDeletion.AddRange(dia.GetFilesForDeletion());

                //Postpone files
                foreach (DiscardFile i in dia.GetFilesForPostponement())
                {
                    try
                    {
                        /* I have decided against actually updating the file labels. Instead just let it go negative */
                        //i.Postpone();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("The file/folder " + DiscardFile.GetRealName(i.Source.Name) + " could not be updated. It might be in use\r\n" + ex.Message, "Discard", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            //Delete files
            foreach (DiscardFile i in filesForDeletion)
            {
                try
                {
                    i.Delete();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("The file/folder " + DiscardFile.GetRealName(i.Source.Name) + " could not be deleted. It might be in use\r\n" + ex.Message, "Discard", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            IsRunning = false;
            return(true);
        }