using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using UPImage;
using System.Drawing;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
namespace UPImage.Data
{
public class UPFolder : IDisposable
{
public bool Checked { get; set; }
// public FolderNode Parent { get; }
private int index;
private int level;
private String safeName;
private String fullname;
UPFolder parent;
List<UPFolder> kidFolders;
List<UPFile> files;
public bool IsDataStop { get; set; }
//
public int Index
{
get
{
return index;
}
set
{
if (index == value)
return;
index = value;
}
}
public int Level
{
get
{
return level;
}
}
public String SafeName
{
get
{
return safeName;
}
set
{
if (safeName == value)
return;
safeName = value;
}
}
public String FullName
{
get
{
return fullname;
}
set
{
if (fullname == value)
return;
fullname = value;
}
}
public UPFolder Parent
{
get
{
return parent;
}
set
{
if (parent == value)
return;
parent = value;
}
}
public List<UPFolder> KidFolders
{
get
{
return kidFolders;
}
set
{
if (kidFolders == value)
return;
kidFolders = value;
}
}
public List<UPFile> Files
{
get
{
return files;
}
set
{
if (files == value)
return;
files = value;
}
}
//parallel task
public int MaxDegreeOfParallelism
{
get
{
return _maxDegreeOfParallelism;
}
set
{
if (value == _maxDegreeOfParallelism)
return;
if ((value == 0) || (value > Environment.ProcessorCount))
_maxDegreeOfParallelism = -1;
else
_maxDegreeOfParallelism = value;
ParallelOption.MaxDegreeOfParallelism = _maxDegreeOfParallelism;
}
}
public ParallelOptions ParallelOption { get; private set; }
private int _maxDegreeOfParallelism = Environment.ProcessorCount;
private ThreadSafeRandom RandomGenerator = new ThreadSafeRandom();
private System.Diagnostics.Stopwatch stopwatch;
public UPFolder()
{
kidFolders = new List<UPFolder>();
files = new List<UPFile>();
index = 0;
level = 0;
safeName = "";
fullname = "";
parent = null;
stopwatch = new System.Diagnostics.Stopwatch();
ParallelOption = new ParallelOptions();
ParallelOption.TaskScheduler = null;
_maxDegreeOfParallelism = Environment.ProcessorCount;
ParallelOption.MaxDegreeOfParallelism = _maxDegreeOfParallelism;
IsDataStop = true;
}
public UPFolder(String sName, String sPath)
{
kidFolders = new List<UPFolder>();
files = new List<UPFile>();
index = 0;
level = 0;
safeName = sName;
fullname = sPath;
parent = null;
stopwatch = new System.Diagnostics.Stopwatch();
ParallelOption = new ParallelOptions();
ParallelOption.TaskScheduler = null;
_maxDegreeOfParallelism = Environment.ProcessorCount;
ParallelOption.MaxDegreeOfParallelism = _maxDegreeOfParallelism;
}
public UPFolder(String sName, UPFolder parentNode)
{
kidFolders = new List<UPFolder>();
files = new List<UPFile>();
index = 0;
level = parentNode.Level+1;
safeName = sName;
parent = parentNode;
fullname = String.Format("{0}\\{1}", parent.FullName, safeName);
stopwatch = new System.Diagnostics.Stopwatch();
ParallelOption = new ParallelOptions();
ParallelOption.TaskScheduler = null;
_maxDegreeOfParallelism = Environment.ProcessorCount;
ParallelOption.MaxDegreeOfParallelism = _maxDegreeOfParallelism;
}
//
~UPFolder()
{
// In case the client forgets to call
// Dispose , destructor will be invoked for
kidFolders.Clear();
files.Clear();
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose managed resources
if (kidFolders != null)
{
kidFolders.Clear();
kidFolders = null;
}
if (files != null)
{
files.Clear();
files = null;
}
}
// free native resources
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void FillFolderTree(String sCurPath)
{
stopwatch.Restart();
List<Task> tasks = new List<Task>(6);
if (kidFolders != null && kidFolders.Count > 0)
{
kidFolders.Clear();
}
if (files != null && files.Count > 0)
{
files.Clear();
}
DirectoryInfo directory;
try
{
// get the directory informaiton for this path.
directory = new DirectoryInfo(sCurPath);
// if the retrieved directory information points to a valid
// directory or drive in this case, add it to the root of the
// treeView.
if (directory.Exists == true)
{
// add the new node to the root level.
safeName = directory.Name;
fullname = directory.FullName;
FileInfo[] fileinfoes = directory.GetFiles();
Parallel.ForEach(directory.GetFiles(), ParallelOption, (f,loopstate) =>
{
if (IsDataStop)
{
loopstate.Stop();
}
UPFile file = new UPFile(f.Name, this);
if (!file.IsDataEmply)
{
lock (this)
{
files.Add(file);
}
}
});
GetSubDirs(this); // scan for any sub folders on this drive.
}
}
catch (Exception doh)
{
return;
}
}
public void GetImageData(String sCurPath,String hierachy, List<ImageData> bmpList,Size bmpSize,int penWidth,bool isGrayScale,bool isLetterOrDigit )
{
if (kidFolders != null && kidFolders.Count > 0)
{
kidFolders.Clear();
}
if (files != null && files.Count > 0)
{
files.Clear();
}
DirectoryInfo directory;
try
{
// get the directory informaiton for this path.
directory = new DirectoryInfo(sCurPath);
// if the retrieved directory information points to a valid
// directory or drive in this case, add it to the root of the
// treeView.
if (directory.Exists == true)
{
// add the new node to the root level.
safeName = directory.Name;
fullname = directory.FullName;
foreach(var f in directory.GetFiles())
{
if(IsDataStop)
{
break;
}
UPFile file = new UPFile(f.Name, this);
if (file.FillupDataSet())
{
if (bmpList != null)
{
Parallel.ForEach(file.Dataset.Datalayouts,ParallelOption, dl =>
{
foreach (var up in dl.UpUnipens)
{
if (up.Hierachy == hierachy)
{
UnipenBitmap upbitmap = new UnipenBitmap(up);
lock (this)
{
if (isLetterOrDigit)
{
if (Char.IsLetterOrDigit(up.Label.ToCharArray(1, 1)[0]))
{
Bitmap bmp = upbitmap.GetBitmap(penWidth, bmpSize, isGrayScale);
if (bmp != null)
{
bmpList.Add(new ImageData(up.Label, bmp));
}
}
}
else
{
Bitmap bmp = upbitmap.GetBitmap(penWidth, bmpSize, isGrayScale);
if (bmp != null)
{
bmpList.Add(new ImageData(up.Label, bmp));
}
}
}
}
}
});
}
}
}
GetImageDataInSubDirectories(this, hierachy, bmpList, bmpSize, penWidth, isGrayScale, isLetterOrDigit); // scan for any sub folders on this drive.
}
}
catch (Exception doh)
{
return;
}
}
/// <summary>
///
/// </summary>
/// <param name="parent"></param>
private void GetImageDataInSubDirectories(UPFolder parent, String hierachy, List<ImageData> bmpList, Size bmpSize,
int penWidth, bool isGrayScale, bool isLetterOrDigit)
{
DirectoryInfo directory;
try
{
// if we have not scanned this folder before
if (parent.KidFolders.Count == 0)
{
directory = new DirectoryInfo(parent.FullName);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
if (IsDataStop)
{
break;
}
UPFolder newNode = new UPFolder(dir.Name, parent);
newNode.Files = new List<UPFile>(dir.GetFiles().Count());
foreach(var f in dir.GetFiles())
{
if (IsDataStop)
{
break;
}
UPFile file = new UPFile(f.Name, newNode);
if (file.FillupDataSet())
{
if (bmpList != null)
{
Parallel.ForEach(file.Dataset.Datalayouts,ParallelOption, dl =>
{
foreach (var up in dl.UpUnipens)
{
if (up.Hierachy == hierachy)
{
UnipenBitmap upbitmap = new UnipenBitmap(up);
lock (this)
{
if (isLetterOrDigit)
{
if (Char.IsLetterOrDigit(up.Label.ToCharArray(1, 1)[0]))
{
Bitmap bmp = upbitmap.GetBitmap(penWidth, bmpSize, isGrayScale);
if (bmp != null)
{
bmpList.Add(new ImageData(up.Label, bmp));
}
}
}
else
{
Bitmap bmp = upbitmap.GetBitmap(penWidth, bmpSize, isGrayScale);
if (bmp != null)
{
bmpList.Add(new ImageData(up.Label, bmp));
}
}
}
}
}
});
}
}
}
//Get children nodes
GetImageDataInSubDirectories(newNode, hierachy, bmpList, bmpSize, penWidth, isGrayScale, isLetterOrDigit);
}
}
// now that we have the children of the parent, see if they
// have any child members that need to be scanned. Scanning
// the first level of sub folders insures that you properly
// see the '+' or '-' expanding controls on each node that represents
// a sub folder with it's own children.
foreach (UPFolder node in parent.KidFolders)
{
if (IsDataStop)
{
break;
}
// if we have not scanned this node before.
if (node.KidFolders.Count == 0)
{
// get the folder information for the specified path.
directory = new DirectoryInfo(node.FullName);
// check this folder for any possible sub-folders
foreach (DirectoryInfo dir in directory.GetDirectories())
{
UPFolder newNode = new UPFolder(dir.Name, parent);
newNode.Files = new List<UPFile>(dir.GetFiles().Count());
foreach(var f in dir.GetFiles())
{
if (IsDataStop)
{
break;
}
UPFile file = new UPFile(f.Name, newNode);
if (file.FillupDataSet())
{
if (bmpList != null)
{
foreach (var dl in file.Dataset.Datalayouts)
{
foreach (var up in dl.UpUnipens)
{
if (up.Hierachy == hierachy)
{
UnipenBitmap upbitmap = new UnipenBitmap(up);
lock (this)
{
if (isLetterOrDigit)
{
if (Char.IsLetterOrDigit(up.Label.ToCharArray(0, 1)[0]))
{
Bitmap bmp = upbitmap.GetBitmap(penWidth, bmpSize, isGrayScale);
if (bmp != null)
{
bmpList.Add(new ImageData(up.Label, bmp));
}
}
}
else
{
Bitmap bmp = upbitmap.GetBitmap(penWidth, bmpSize, isGrayScale);
if (bmp != null)
{
bmpList.Add(new ImageData(up.Label, bmp));
}
}
}
}
}
}
}
}
}
GetImageDataInSubDirectories(newNode, hierachy, bmpList, bmpSize, penWidth, isGrayScale, isLetterOrDigit);
}
}
}
}
catch (Exception ex)
{
throw;
}
}
private void GetSubDirs(UPFolder parent)
{
DirectoryInfo directory;
try
{
// if we have not scanned this folder before
if (parent.KidFolders.Count == 0)
{
directory = new DirectoryInfo(parent.FullName);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
if (IsDataStop)
{
break;
}
UPFolder newNode = new UPFolder(dir.Name, parent);
newNode.Files = new List<UPFile>(dir.GetFiles().Count());
Parallel.ForEach(dir.GetFiles(), ParallelOption, (f, loopstate) =>
{
if (IsDataStop)
{
loopstate.Stop();
}
UPFile file = new UPFile(f.Name, newNode);
if (!file.IsDataEmply)
{
lock (this)
{
newNode.Files.Add(file);
}
}
});
//Get children nodes
GetSubDirs(newNode);
//if folder has data file, add to folder tree
if (newNode.KidFolders.Count > 0 || newNode.Files.Count > 0)
{
parent.KidFolders.Add(newNode);
}
}
}
// now that we have the children of the parent, see if they
// have any child members that need to be scanned. Scanning
// the first level of sub folders insures that you properly
// see the '+' or '-' expanding controls on each node that represents
// a sub folder with it's own children.
foreach (UPFolder node in parent.KidFolders)
{
// if we have not scanned this node before.
if (node.KidFolders.Count == 0)
{
// get the folder information for the specified path.
directory = new DirectoryInfo(node.FullName);
// check this folder for any possible sub-folders
foreach (DirectoryInfo dir in directory.GetDirectories())
{
UPFolder newNode = new UPFolder(dir.Name, parent);
newNode.Files = new List<UPFile>(dir.GetFiles().Count());
Parallel.ForEach(dir.GetFiles(), ParallelOption, (f, loopstate) =>
{
UPFile file = new UPFile(f.Name, newNode);
if (!file.IsDataEmply)
{
lock (this)
{
newNode.Files.Add(file);
}
}
});
// add node to folder tree
parent.KidFolders.Add(newNode);
GetSubDirs(newNode);
}
}
}
}
catch (Exception ex)
{
throw;
}
}
}
}