using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace JFileManager
{
internal class cFileNoteData
{
private bool m_blnRecordExists = false;
private int m_intRecNum = -1;
private int m_intIconIndex = 0;
private string m_strRecordFileName = "";
private string m_strRecordFolderName = "";
private string m_strRecordFullPath = "";
private string m_strRecordNotes = "";
// New standard layout for DB Name, Connection string and Table Name
private static string m_strDBName = "FileNotes.mdb";
private static string m_strConnectionString = cCommon.ConnectionString(m_strDBName);
private static string m_strTableName = "tblFileNotes";
internal cFileNoteData()
{
}
internal cFileNoteData(string strPath)
{
FileInfo fInfo = new FileInfo(strPath);
if (fInfo.Exists)
GetRecord(fInfo.Name);
}
public static bool FileNotesExists()
{
string strPath = Path.Combine(Application.StartupPath, m_strDBName);
try
{
FileInfo fInfo = new FileInfo(strPath);
return fInfo.Exists;
}
catch
{
return false;
}
}
public bool DeleteRecord()
{
ADODB.Connection adCON = new ADODB.Connection();
ADODB.Recordset adRS = new ADODB.Recordset();
bool blnReturn = false;
string strQuery = "SELECT * FROM " + m_strTableName + " WHERE intRecNum =";
strQuery += this.m_intRecNum.ToString();
adCON.Open(m_strConnectionString, "", "", 0);
adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
if (!adRS.EOF)
{
adRS.Delete(ADODB.AffectEnum.adAffectCurrent);
blnReturn = true;
}
adRS.Close();
adCON.Close();
return blnReturn;
}
public int FileCheck(ref ListView lv)
{
ADODB.Connection adCON = new ADODB.Connection();
ADODB.Recordset adRS = new ADODB.Recordset();
cFileNoteData fileNoteData;
FileInfo fInfo;
int intNumWrong = 0;
ListViewItem lvItem;
string strQuery = "SELECT * FROM " + m_strTableName;
adCON.Open(m_strConnectionString, "", "", 0);
adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
lv.Items.Clear();
while (!adRS.EOF)
{
fileNoteData = new cFileNoteData();
fileNoteData.GetRecordDetails(adRS);
fInfo = new FileInfo(fileNoteData.m_strRecordFullPath);
if (!fInfo.Exists)
{
lvItem = new ListViewItem(fileNoteData.m_strRecordFileName);
lvItem.SubItems.Add(fileNoteData.m_strRecordFolderName);
lvItem.Tag = fileNoteData;
lv.Items.Add(lvItem);
intNumWrong++;
}
adRS.MoveNext();
}
adRS.Close();
adCON.Close();
return intNumWrong;
}
private string GetField(ADODB.Recordset adRS, string strField)
{
string strReturn = "";
if (adRS.Fields[strField] != null)
strReturn = Convert.ToString(adRS.Fields[strField].Value);
return strReturn;
}
public string GetRecord(string strFileName)
{
ADODB.Connection adCON = new ADODB.Connection();
ADODB.Recordset adRS = new ADODB.Recordset();
string strQuery = "SELECT * FROM " + m_strTableName + " WHERE strFileName =";
strQuery += "\"";
strQuery += strFileName;
strQuery += "\"";
adCON.Open(m_strConnectionString, "", "", 0);
adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
if (!adRS.EOF)
{
GetRecordDetails(adRS);
}
else
{
this.m_intRecNum = -1;
this.m_blnRecordExists = false;
}
adRS.Close();
adCON.Close();
return this.m_strRecordNotes;
}
public bool GetRecordDetails(ADODB.Recordset adRS)
{
bool blnOK = false;
try
{
this.m_intRecNum = Convert.ToInt32(GetField(adRS, "intRecNum"));
this.m_strRecordFullPath = GetField(adRS, "strFullPath");
this.m_strRecordFolderName = GetField(adRS, "strFolderName");
this.m_strRecordFileName = GetField(adRS, "strFileName");
this.m_strRecordNotes = GetField(adRS, "strNotes");
this.m_intIconIndex = cCommon.GetSmallIconIndex(this.m_strRecordFullPath);
this.m_blnRecordExists = true;
}
catch
{
this.m_intRecNum = -1;
this.m_blnRecordExists = false;
}
return blnOK;
}
public int Search(string strSearchString, bool blnFileName, bool blnNotes, ref ListView lv)
{
if (strSearchString == "")
return ShowAll(ref lv);
ADODB.Connection adCON = new ADODB.Connection();
ADODB.Recordset adRS = new ADODB.Recordset();
cFileNoteData fileNoteData;
int intCount = 0;
int intNumRecs = 0;
ListViewItem lvItem;
string strQuery;
if (blnFileName && blnNotes)
{
strQuery = "SELECT * FROM " + m_strTableName + " WHERE strFileName LIKE ";
strQuery += "\"%";
strQuery += strSearchString;
strQuery += "%\"";
strQuery += " OR strNotes LIKE ";
strQuery += "\"%";
strQuery += strSearchString;
strQuery += "%\"";
}
else if (blnFileName)
{
strQuery = "SELECT * FROM " + m_strTableName + " WHERE strFileName LIKE ";
strQuery += "\"%";
strQuery += strSearchString;
strQuery += "%\"";
}
else if (blnNotes)
{
strQuery = "SELECT * FROM " + m_strTableName + " WHERE strNotes LIKE ";
strQuery += "\"%";
strQuery += strSearchString;
strQuery += "%\"";
}
else
{
return -1;
}
adCON.Open(m_strConnectionString, "", "", 0);
adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
intNumRecs = adRS.RecordCount;
while (!adRS.EOF)
{
fileNoteData = new cFileNoteData();
fileNoteData.GetRecordDetails(adRS);
lvItem = new ListViewItem(fileNoteData.m_strRecordFileName, fileNoteData.m_intIconIndex);
lvItem.SubItems.Add(fileNoteData.m_strRecordFolderName);
if (!blnNotes)
lvItem.SubItems.Add("");
else
{
if (fileNoteData.m_strRecordNotes.ToUpper().IndexOf(strSearchString.ToUpper()) != -1)
lvItem.SubItems.Add(fileNoteData.m_strRecordNotes);
}
lvItem.Tag = fileNoteData;
lvItem.ToolTipText = "Double Click To Go To File";
lv.Items.Add(lvItem);
intCount++;
adRS.MoveNext();
}
adRS.Close();
adCON.Close();
return intNumRecs;
}
private int ShowAll(ref ListView lv)
{
ADODB.Connection adCON = new ADODB.Connection();
ADODB.Recordset adRS = new ADODB.Recordset();
cFileNoteData fileNoteData;
int intCount = 0;
int intNumRecs = 0;
ListViewItem lvItem;
string strQuery = "SELECT * FROM " + m_strTableName + " ORDER BY strFileName";
adCON.Open(m_strConnectionString, "", "", 0);
adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
intNumRecs = adRS.RecordCount;
while (!adRS.EOF)
{
fileNoteData = new cFileNoteData();
fileNoteData.GetRecordDetails(adRS);
lvItem = new ListViewItem(fileNoteData.m_strRecordFileName, fileNoteData.m_intIconIndex);
lvItem.SubItems.Add(fileNoteData.m_strRecordFolderName);
lvItem.SubItems.Add(fileNoteData.m_strRecordNotes);
lvItem.Tag = fileNoteData;
lvItem.ToolTipText = "Double Click To Go To File";
lv.Items.Add(lvItem);
intCount++;
adRS.MoveNext();
}
adRS.Close();
adCON.Close();
return intNumRecs;
}
public bool UpdateRecord()
{
ADODB.Connection adCON = new ADODB.Connection();
ADODB.Recordset adRS = new ADODB.Recordset();
bool blnOK;
object[] fList = { "strFullPath", "strFolderName", "strFileName", "strNotes" };
object[] fValues = { this.m_strRecordFullPath, this.m_strRecordFolderName, this.m_strRecordFileName, this.m_strRecordNotes };
string strQuery = "SELECT * FROM " + m_strTableName + " WHERE intRecNum =";
strQuery += this.m_intRecNum.ToString();
adCON.Open(m_strConnectionString, "", "", 0);
adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
try
{
if (!adRS.EOF)
{
adRS.Update(fList, fValues);
blnOK = true;
}
else
{
adRS.AddNew(fList, fValues);
blnOK = true;
}
}
catch
{
blnOK = false;
}
finally
{
adRS.Close();
adCON.Close();
}
return blnOK;
}
#region Public Properties
// *************************************************************** Public Properties
public string FileName
{
get
{
return m_strRecordFileName;
}
set
{
m_strRecordFileName = value;
}
}
public string FolderName
{
get
{
return m_strRecordFolderName;
}
set
{
m_strRecordFolderName = value;
}
}
public string FullPath
{
get
{
return m_strRecordFullPath;
}
set
{
m_strRecordFullPath = value;
}
}
public string Notes
{
get
{
return m_strRecordNotes;
}
set
{
m_strRecordNotes = value;
}
}
public bool RecordExists
{
get
{
return m_blnRecordExists;
}
}
public int RecordNumber
{
get
{
return this.m_intRecNum;
}
}
#endregion
}
}