You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
8.5 KiB

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Database;
using Syncfusion.Windows.Forms.Tools;
using Syncfusion.WinForms.Input.Enums;
namespace OnDoc.Klassen
{
public static class clsProcessWatch
{
public static System.Timers.Timer watchtimer = new System.Timers.Timer(Convert.ToInt32(Properties.Settings.Default.OfficeWatchTimerIntervall));
static List<FileToCheck> FilestoCheck = new List<FileToCheck>();
public static void AddToList(string dokumentid, string filename, string application)
{
FilestoCheck.Add(new FileToCheck(dokumentid, filename, application));
watchtimer.Elapsed += WatchProcesses;
if (watchtimer.Enabled == false) { watchtimer.Enabled = true; }
}
public static void RemoveFromList(string dokumentid)
{
foreach (FileToCheck fc in FilestoCheck)
{
if (fc.dokumentid == dokumentid)
{
FilestoCheck.Remove(fc);
try
{
System.IO.File.Delete(fc.filename);
}
catch { }
break;
}
}
if (FilestoCheck.Count == 0)
{
watchtimer.Enabled = false;
}
}
private static void WatchProcesses(object source, ElapsedEventArgs e)
{
bool word = false;
bool excel = false;
bool pdf = false;
bool found = false;
watchtimer.Enabled = false;
found= false;
foreach (FileToCheck fc in FilestoCheck)
{
if (fc.filedatetime < DateTime.Now.AddSeconds(-5))
{
found = false;
Logging.Logging.Debug(fc.application + " / FileChek " + fc.filename + " / " + fc.filedatetime.ToString("yyyy-MM-dd hh:mm:ss") + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
if (fc.application == "Word") { word = true; }
if (fc.application == "Excel") { excel = true; }
if (fc.application == "PDF") { pdf = true; }
if (word)
{
Process[] localByName = Process.GetProcessesByName("WINWORD");
foreach (Process p in localByName)
{
if (p.MainWindowTitle.IndexOf(fc.dokumentid) > -1) { found = true; }
save_to_db(fc);
}
}
if (excel)
{
Process[] localByName = Process.GetProcessesByName("EXCEL");
foreach (Process p in localByName)
{
if (p.MainWindowTitle.IndexOf(fc.dokumentid) > 0) { found = true; }
save_to_db(fc);
}
}
if (!found)
{
Logging.Logging.Debug("Not Found " + fc.filename + " / " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
if (Check_Modified(fc) == true)
{
Save_File(fc.dokumentid, fc.filename);
Logging.DocLog.Info("Dokument gespeichert und geschlossen", "Processwatch", fc.dokumentid, "", fc.filename);
RemoveFromList(fc.dokumentid);
Remove_Dok_in_Bearbeitung(fc.dokumentid);
Remove_Dokumentbearbeitung_Zwingend(fc.dokumentid);
Remove_Approvals(fc.dokumentid);
return;
}
else
{
Logging.DocLog.Info("Dokument ohne speichern geschlossen", "Processwatch", fc.dokumentid, "", fc.filename);
RemoveFromList(fc.dokumentid);
Remove_Dok_in_Bearbeitung(fc.dokumentid);
return;
};
}
}
}
watchtimer.Enabled = true;
}
private static void Remove_Dok_in_Bearbeitung(string dokumentid)
{
DB db = new DB(AppParams.connectionstring);
db.Dok_in_Bearbeitung(2, dokumentid, AppParams.CurrentMitarbeiter);
db = null;
}
private static void Remove_Dokumentbearbeitung_Zwingend(string dokumentid)
{
DB db = new DB(AppParams.connectionstring);
db.Exec_SQL("Update dokument set bearbeitung_zwingend=0 where dokumentid='" + dokumentid + "'");
db = null;
}
private static void Remove_Approvals(string dokumentid)
{
DB db = new DB(AppParams.connectionstring);
db.Exec_SQL("Update dokument_bewilligung set aktiv=0, mutiert_am=getdate(), mutierer="+AppParams.CurrentMitarbeiter.ToString()+" where dokumentid='" + dokumentid + "'");
db = null;
}
private static void save_to_db(FileToCheck fc)
{
if (Check_Modified(fc))
{
try
{
System.IO.File.Copy(fc.filename, fc.filename + ".tmp");
Save_File(fc.dokumentid, fc.filename + ".tmp");
fc.filedatetime = DateTime.Now;
System.IO.File.Delete(fc.filename + ".tmp");
Logging.Logging.Debug(fc.application + " / save_to_db " + fc.filename + " / " + fc.filedatetime.ToString("yyyy-MM-dd hh:mm:ss") + "/"+DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
}
catch
{
Logging.Logging.Debug(fc.application + " / save_to_db faild " + fc.filename + " / " + fc.filedatetime.ToString("yyyy-MM-dd hh:mm:ss") +"/"+ DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
}
}
}
private static void Save_File(string dokumentid, string filename)
{
DB db = new DB(AppParams.connectionstring);
db.Get_Tabledata("Select * from dokument where dokumentid='" + dokumentid + "'", false, true);
db.Save_To_DB(dokumentid, filename);
db.set_approvalstate(dokumentid, false);
db.Exec_SQL("Update dokument set mutiertam = getdate(), mutierer="+AppParams.CurrentMitarbeiter+" where dokumentid='"+dokumentid+"'");
Logging.DocLog.Info("Dokument gespeichert", "Processwatch", dokumentid, "", filename);
db = null;
}
private static bool Check_Modified(FileToCheck fc)
{
DateTime lwt = System.IO.File.GetLastWriteTime(fc.filename);
int secdiff = (int)((lwt - fc.filedatetime).TotalSeconds);
Logging.Logging.Debug("Prozesswatch - Check Modified: " + lwt.ToString() + "," + fc.filedatetime.ToString(), "OnDoc", fc.dokumentid);
//Logging.DocLog.Debug("Prozesswatch - Check Modified: " + lwt.ToString() + "," + fc.filedatetime.ToString(), "Processwatch", fc.dokumentid, "", fc.filename);
//if ((lwt- fc.filedatetime).Seconds > 2)
if (secdiff > 2)
{
return true;
}
else
{
return false;
}
}
public static int check_open_files()
{
return FilestoCheck.Count;
}
}
public class FileToCheck
{
public string dokumentid { get; set; }
public string filename { get; set; }
public string application { get; set; }
public DateTime filedatetime { get; set; }
public FileToCheck(string dokumentid, string filename, string application)
{
this.dokumentid = dokumentid;
this.filename = filename;
this.application = application;
this.filedatetime = DateTime.Now;
//Logging.DocLog.Debug("Add Processwatch: " + DateTime.Now.ToString(), "New FileToCheck", dokumentid, "", "Add Processwatch");
Logging.Logging.Debug("Add Processwatch: " + DateTime.Now.ToString(),"OnDoc",this.dokumentid);
}
}
}