예제 #1
0
        private void PopulateListView()
        {
            int index = 0;

            foreach (DiscardFile i in Files)
            {
                ListViewItem listItem = new ListViewItem(DiscardFile.GetRealName(i.Source.Name))
                {
                    Tag        = i,
                    ImageIndex = index++,
                };

                listItem.SubItems.Add(GetSizeStr(i.Source, out long size));
                listItem.SubItems.Add(GetLastUsedStr(i.Source, out int days));

                if (size == 0)
                {
                    listItem.ForeColor = Color.Green;
                }
                else if (days == 2)
                {
                    listItem.ForeColor = Color.Goldenrod;
                }
                else if (days == 1)
                {
                    listItem.ForeColor = Color.Orange;
                }
                else if (days == 0)
                {
                    listItem.ForeColor = Color.Red;
                }

                lstvwDelete.Items.Add(listItem);
            }
        }
예제 #2
0
        private void EvFileRenamed(object sender, RenamedEventArgs e)
        {
            if (Path.GetExtension(e.OldFullPath) == ".discard")
            {
                return;
            }

            //Rename tracker file
            try
            {
                DiscardFile oldDiscardFile = new DiscardFile(new FileInfo(e.OldFullPath));
                DiscardFile newDiscardFile = new DiscardFile(new FileInfo(e.FullPath));

                if (oldDiscardFile.HasExternalCounter)
                {
                    (oldDiscardFile.CounterFile as FileInfo).MoveTo(
                        Path.Combine(Path.GetDirectoryName(e.FullPath), DiscardFile.ConstructFileName(
                                         days: oldDiscardFile.DaysLeft,
                                         noWarn: oldDiscardFile.NoWarning,
                                         name: DiscardFile.GetRealName(newDiscardFile.Source.Name)
                                         ) + ".discard"));
                }
            }
            catch (Exception ex)
            {
                //Really hacky way to fix edge case scenario where the warning would trigger when merging files
                //This may actually break stuff idk.
                if (ex.HResult == -2147024894)
                {
                    return;
                }

                System.Windows.Forms.MessageBox.Show("Unable to rename the trailing counter file for former '" + e.OldName + "'. You need to rename it manually");
            }
        }
예제 #3
0
        private void BtnSendToArchive_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem i in lstvwDelete.SelectedItems)
            {
                DiscardFile discardFile = i.Tag as DiscardFile;

                //Shows the save file dialog
                SaveFileDialog dia = new SaveFileDialog()
                {
                    Title = "Archive " + DiscardFile.GetRealName(discardFile.Source.Name)
                };

                //Add extension filters
                if (discardFile.Source is FileInfo f)
                {
                    dia.Filter   = $"Current extension (*{f.Extension})|*{f.Extension}|Any extension (*.*)|*";
                    dia.FileName = DiscardFile.GetRealName(discardFile.Source.Name);
                }
                else if (discardFile.Source is DirectoryInfo d)
                {
                    dia.Filter   = "File Directory|*";
                    dia.FileName = DiscardFile.GetRealName(discardFile.Source.Name);
                }

                //Shows the dialog
                if (dia.ShowDialog() == DialogResult.Cancel)
                {
                    //If the user hit cancel, continue to the next file in the selection
                    continue;
                }

                try
                {
                    discardFile.Archive(dia.FileName);
                }
                catch (Exception)
                {
                    MessageBox.Show("Could not archive file, Please try again, or in another location", "Archive failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    continue;
                }

                lstvwDelete.Items.Remove(i);
            }
        }
예제 #4
0
        private void EvFileDeleted(object sender, FileSystemEventArgs e)
        {
            if (Path.GetExtension(e.FullPath) == ".discard")
            {
                return;
            }

            //Delete tracker file
            try
            {
                DiscardFile discardFile = new DiscardFile(new FileInfo(e.FullPath));

                if (discardFile.HasExternalCounter)
                {
                    discardFile.CounterFile.Delete();
                }
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("Unable to delete the trailing counter file for '" + e.Name + "'. You need to delete it manually");
            }
        }
예제 #5
0
        /// <summary>
        /// Creates a ToolStripMenuItem from a discard file
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private ToolStripMenuItem CreateContextMenuForDiscardFile(DiscardFile file)
        {
            //Create the button for the file
            ToolStripMenuItem fileButton = new ToolStripMenuItem()
            {
                Text  = DiscardFile.GetRealName(file.Source.Name) + (file.Source is DirectoryInfo ? "\\" : ""),
                Image = ThumbnailGenerator.WindowsThumbnailProvider.GetThumbnail(file.Source.FullName, 16, 16, ThumbnailGenerator.ThumbnailOptions.None),
                Font  = Control.DefaultFont
            };

            fileButton.Click += (s, e) => Process.Start(file.Source.FullName);

            /*
             * Colorization
             */

            //Based on extended timers
            if (file.DaysLeft > Properties.Settings.Default.DefaultDays)
            {
                fileButton.ForeColor   = Color.FromArgb(75, 75, 75);
                fileButton.ToolTipText = "This " + (file.Source is FileInfo ? "file" : "directory") + " has an extended timer";
            }

            //Based on modification time
            switch ((DateTime.Now - file.Source.LastWriteTime).Days)
            {
            case 0:
                fileButton.ForeColor   = Color.Red;
                fileButton.ToolTipText = "This " + (file.Source is FileInfo ? "file" : "directory") + " was edited less than 24 hours ago";
                break;

            case 1:
                fileButton.ForeColor   = Color.Orange;
                fileButton.ToolTipText = "This " + (file.Source is FileInfo ? "file" : "directory") + " was edited less than 48 hours ago";
                break;

            case 2:
                fileButton.ForeColor   = Color.Goldenrod;
                fileButton.ToolTipText = "This " + (file.Source is FileInfo ? "file" : "directory") + " was edited less than 72 hours ago";
                break;
            }

            //Based on tracking
            if (file.Untracked)
            {
                fileButton.ForeColor   = Color.DodgerBlue;
                fileButton.BackColor   = Color.LightSkyBlue;
                fileButton.ToolTipText = "This " + (file.Source is FileInfo ? "file" : "directory") + " is untracked";
            }

            //Based on emptyness
            if ((file.Source is FileInfo f && f.Length == 0) || (file.Source is DirectoryInfo d && !d.EnumerateFileSystemInfos().Any()))
            {
                fileButton.ForeColor   = Color.Green;
                fileButton.ToolTipText = "This " + (file.Source is FileInfo ? "file" : "directory") + " is empty and can safely be deleted";
            }

            //Backcolor
            if (file.DaysLeft <= 0)
            {
                fileButton.BackColor = Color.Pink;
            }
            else if (file.DaysLeft == 1 && !file.Untracked)
            {
                fileButton.BackColor = Color.Wheat;
            }

            /*
             * Sub-buttons
             */
            fileButton.DropDown.Items.Add("Open", null, (s, e) =>
            {
                Process.Start(file.Source.FullName);
            });

            fileButton.DropDown.Items.Add("Open file location", null, (s, e) =>
            {
                if (file.Source is FileInfo fi)
                {
                    Process.Start(fi.DirectoryName);
                }
                else if (file.Source is DirectoryInfo di)
                {
                    Process.Start(di.Parent.FullName);
                }
            });
            fileButton.DropDown.Items.Add("-");

            if (file.HasExternalCounter)
            {
                fileButton.DropDown.Items.Add("Merge external tracker file", null, (s, e) =>
                {
                    file.MergeCounterFile();
                });
            }
            else
            {
                fileButton.DropDown.Items.Add("Create external tracker file", null, (s, e) =>
                {
                    file.CreateCounterFile();
                });
            }


            fileButton.DropDown.Items.Add("Archive...", null, (s, e) =>
            {
                //Copied from the discard dialog

                //Shows the save file dialog
                SaveFileDialog dia = new SaveFileDialog()
                {
                    Title = "Archive " + DiscardFile.GetRealName(file.Source.Name)
                };

                //Add extension filters
                if (file.Source is FileInfo fi)
                {
                    dia.Filter   = $"Current extension (*{fi.Extension})|*{fi.Extension}|Any extension (*.*)|*";
                    dia.FileName = DiscardFile.GetRealName(file.Source.Name);
                }
                else if (file.Source is DirectoryInfo)
                {
                    dia.Filter   = "File Directory|*";
                    dia.FileName = DiscardFile.GetRealName(file.Source.Name);
                }

                //Shows the dialog
                if (dia.ShowDialog() == DialogResult.Cancel)
                {
                    //If the user hit cancel, continue to the next file in the selection
                    return;
                }

                try
                {
                    file.Archive(dia.FileName);
                }
                catch (Exception)
                {
                    MessageBox.Show("Could not archive file, Please try again, or in another location", "Archive failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });

            fileButton.DropDown.Items.Add("Delete now...", null, (s, e) =>
            {
                if (MessageBox.Show("Are you sure you want to permanently delete this file?", "Delete now", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    file.Delete();
                }
            });

            fileButton.DropDown.Items.Add("-");
            (fileButton.DropDown.Items.Add("Set days left...") as ToolStripMenuItem).DropDown.Items.AddRange(new ToolStripItem[] {
                new ToolStripMenuItem("1 day", null, (s, e) => file.DaysLeft    = 1),
                new ToolStripMenuItem("3 days", null, (s, e) => file.DaysLeft   = 3),
                new ToolStripMenuItem("5 days", null, (s, e) => file.DaysLeft   = 5),
                new ToolStripMenuItem("7 days", null, (s, e) => file.DaysLeft   = 7),
                new ToolStripMenuItem("14 days", null, (s, e) => file.DaysLeft  = 14),
                new ToolStripMenuItem("30 days", null, (s, e) => file.DaysLeft  = 30),
                new ToolStripMenuItem("60 days", null, (s, e) => file.DaysLeft  = 60),
                new ToolStripMenuItem("999 days", null, (s, e) => file.DaysLeft = 999),
            });

            fileButton.DropDown.Items.Add(new ToolStripMenuItem("Warn before deletion", null, (s, e) =>
            {
                file.NoWarning = !file.NoWarning;
            })
            {
                Checked = !file.NoWarning
            });

            return(fileButton);
        }
예제 #6
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);
        }
예제 #7
0
        /// <summary>
        /// Called when OK is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnOk(object sender, EventArgs e)
        {
            try
            {
                //Create new discard file instance
                string      path    = Path.Combine((string)cboxCreateIn.SelectedItem, txtName.Text);
                DiscardFile newFile = new DiscardFile(rdoTypeFile.Checked ? (FileSystemInfo) new FileInfo(path) : (FileSystemInfo) new DirectoryInfo(path));

                //File exists. The file cannot be created
                if (newFile.Source.Exists)
                {
                    MessageBox.Show("That file name is already in use. Try a different name", "In use", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //Create the file/folder
                if (newFile.Source is FileInfo f)
                {
                    f.Create().Close();
                }
                else if (newFile.Source is DirectoryInfo d)
                {
                    d.Create();
                }

                //Apply days left
                if (rdoTimeDefault.Checked)
                {
                    //Leave untracked
                }
                else if (rdoTimeTomorrow.Checked)
                {
                    newFile.DaysLeft = 1;
                }
                else if (rdoTimeAfter.Checked)
                {
                    newFile.DaysLeft = (int)numTime.Value;
                }
                else if (rdoTimeAt.Checked)
                {
                    newFile.DaysLeft = (dtpTime.Value - DateTime.Now).Days;
                }

                //Apply nowarn
                if (!chkWarn.Checked)
                {
                    newFile.NoWarning = true;
                }

                //Open file is auto open is set
                if (chkAutoOpen.Checked)
                {
                    Process.Start(newFile.Source.FullName);
                }
            }

            //Something went wrong. Throw up an error box
            catch (Exception)
            {
                MessageBox.Show("The file/folder could not be created or some settings could not be applied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }



            //Closes dialog
            DialogResult = DialogResult.OK;
            Close();
        }