Example #1
0
            public FsxContext(string strRoot, string strVolumeLabel,
                              FsxMatchFn fMatch)
            {
                if (strRoot == null)
                {
                    throw new ArgumentNullException("strRoot");
                }
                if (strVolumeLabel == null)
                {
                    throw new ArgumentNullException("strVolumeLabel");
                }
                if (fMatch == null)
                {
                    throw new ArgumentNullException("fMatch");
                }

                this.RootDirectory = strRoot;
                this.VolumeLabel   = strVolumeLabel;
                this.Match         = fMatch;
            }
Example #2
0
        private static List <FsxContext> Find(FsxMatchFn fMatch, IStatusLogger sl,
                                              string strRootPath)
        {
            if (sl == null)
            {
                Debug.Assert(false); sl = new NullStatusLogger();
            }

            List <FsxContext> lContexts = new List <FsxContext>();

            if (!string.IsNullOrEmpty(strRootPath))
            {
                lContexts.Add(new FsxContext(strRootPath, string.Empty, fMatch));
            }
            else
            {
                DriveInfo[] vDrives = DriveInfo.GetDrives();
                if (vDrives == null)
                {
                    Debug.Assert(false); vDrives = new DriveInfo[0];
                }
                foreach (DriveInfo di in vDrives)
                {
                    if (di == null)
                    {
                        Debug.Assert(false); continue;
                    }

                    try
                    {
                        if (!di.IsReady)
                        {
                            continue;
                        }

                        string strRoot = di.RootDirectory.FullName;
                        if (string.IsNullOrEmpty(strRoot))
                        {
                            Debug.Assert(false); continue;
                        }

                        string strVolumeLabel = string.Empty;
                        try { strVolumeLabel = (di.VolumeLabel ?? string.Empty); }
                        catch (Exception) { Debug.Assert(false); }

                        lContexts.Add(new FsxContext(strRoot, strVolumeLabel, fMatch));
                    }
                    catch (Exception) { Debug.Assert(false); }
                }
            }

            for (int i = lContexts.Count - 1; i >= 0; --i)
            {
                FsxContext ctx = lContexts[i];

                try
                {
                    Thread th = new Thread(delegate()
                    {
                        try
                        {
                            try { FindInDirectory(ctx.RootDirectory, ctx); }
                            catch (Exception) { Debug.Assert(false); }

                            ctx.End = true;
                        }
                        catch (Exception) { Debug.Assert(false); }
                    });
                    th.Start();
                }
                catch (Exception) { Debug.Assert(false); lContexts.RemoveAt(i); }
            }

            lContexts.Sort(FsxContext.CompareByRoot);
            sl.SetText(GetSearchingText(lContexts), LogStatusType.Info);

            List <FsxContext> lRunning = new List <FsxContext>(lContexts);
            int msSleep = Math.Max(PwDefs.UIUpdateDelay / 4, 1);

            while (lRunning.Count != 0)
            {
                try
                {
                    Thread.Sleep(msSleep);

                    for (int i = lRunning.Count - 1; i >= 0; --i)
                    {
                        if (lRunning[i].End)
                        {
                            lRunning.RemoveAt(i);
                            sl.SetText(GetSearchingText(lRunning), LogStatusType.Info);
                        }
                    }

                    if (!sl.ContinueWork())
                    {
                        foreach (FsxContext ctx in lRunning)
                        {
                            ctx.End = true;
                        }
                        lRunning.Clear();
                    }
                }
                catch (Exception) { Debug.Assert(false); }
            }

            return(lContexts);
        }
Example #3
0
        private static string FindUI(MainForm mf, FsxMatchFn fMatch, string strRootPath)
        {
            if (mf == null)
            {
                Debug.Assert(false); return(null);
            }
            if (fMatch == null)
            {
                Debug.Assert(false); return(null);
            }

            Form          fOptDialog;
            IStatusLogger sl = StatusUtil.CreateStatusDialog(mf, out fOptDialog,
                                                             null, GetSearchingText(null), true, true);

            mf.UIBlockInteraction(true);

            List <FsxContext> lContexts = new List <FsxContext>();
            string            strExcp   = null;

            try { lContexts = Find(fMatch, sl, strRootPath); }
            catch (Exception ex) { strExcp = ex.Message; }

            bool bAborted = !sl.ContinueWork();

            mf.UIBlockInteraction(false);
            sl.EndLogging();

            if (!string.IsNullOrEmpty(strExcp))
            {
                MessageService.ShowWarning(strExcp);
                return(null);
            }

            Action <ListView> fInit = delegate(ListView lv)
            {
                int w  = lv.ClientSize.Width - UIUtil.GetVScrollBarWidth();
                int ws = w / 70;
                lv.Columns.Add(KPRes.File, w / 5);
                lv.Columns.Add(KPRes.Folder, (int)(((long)w * 2L) / 5L - (ws * 3)));
                lv.Columns.Add(KPRes.Size, w / 10 + ws, HorizontalAlignment.Right);
                lv.Columns.Add(KPRes.Type, w / 10 + ws);
                lv.Columns.Add(KPRes.LastModified, w / 5 + ws);
            };

            List <object> lItems = new List <object>();
            int           cFiles = 0;

            foreach (FsxContext ctx in lContexts)
            {
                List <FsxResult> lResults = ctx.GetResults();
                if (lResults.Count == 0)
                {
                    continue;
                }

                string strGroup = UrlUtil.EnsureTerminatingSeparator(ctx.RootDirectory, false);
                if (ctx.VolumeLabel.Length != 0)
                {
                    strGroup += " (" + ctx.VolumeLabel + ")";
                }
                lItems.Add(new ListViewGroup(strGroup));

                foreach (FsxResult r in lResults)
                {
                    try
                    {
                        FileInfo fi = new FileInfo(r.Path);

                        ListViewItem lvi = new ListViewItem(UrlUtil.GetFileName(r.Path));
                        lvi.SubItems.Add(UrlUtil.GetFileDirectory(r.Path, true, false));
                        lvi.SubItems.Add(StrUtil.FormatDataSizeKB((ulong)fi.Length));
                        lvi.SubItems.Add(r.Type);
                        lvi.SubItems.Add(TimeUtil.ToDisplayString(
                                             TimeUtil.ToLocal(fi.LastWriteTimeUtc, false)));
                        lvi.Tag = r.Path;

                        lItems.Add(lvi);
                        ++cFiles;
                    }
                    catch (Exception) { Debug.Assert(false); }
                }
            }

            string strSub = KPRes.ObjectsFound.Replace("{PARAM}", cFiles.ToString()) + ".";

            if (bAborted)
            {
                strSub += " " + (new OperationCanceledException()).Message;
            }

            ListViewForm dlg = new ListViewForm();

            dlg.InitEx(KPRes.SearchGroupName, strSub, null,
                       Properties.Resources.B48x48_XMag, lItems, null, fInit);
            UIUtil.ShowDialogAndDestroy(dlg, mf);

            return(dlg.ResultItem as string);
        }