Messenger/MSGer.tk/Program.cs

177 lines
7.1 KiB
C#
Raw Normal View History

2014-07-12 08:53:00 +00:00
//#define LOCAL_SERVER
using System;
2014-04-25 21:55:43 +00:00
using System.Collections.Generic;
2014-07-12 08:53:00 +00:00
using System.ComponentModel;
using System.Diagnostics;
2014-04-25 21:55:43 +00:00
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
2014-04-25 21:55:43 +00:00
using System.Text;
2014-07-12 08:53:00 +00:00
using System.Threading;
2014-04-25 21:55:43 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
2014-07-12 08:53:00 +00:00
using SzNPProjects;
2014-04-25 21:55:43 +00:00
namespace MSGer.tk
{
static class Program
{
public static MainForm MainF;
public static string ProcessName = Process.GetCurrentProcess().ProcessName;
2014-04-25 21:55:43 +00:00
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
2014-04-25 21:55:43 +00:00
{
Process[] pname = Process.GetProcessesByName(ProcessName);
if (pname.Length > 1 && !(args.Length > 0 && args[0] == "multi"))
Environment.Exit(0); //2014.09.26. - Felesleges bármi más műveletet végrehajtani, még nem is töltött be semmit
2014-04-25 21:55:43 +00:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
BeforeLogin.Create();
2014-04-25 21:55:43 +00:00
Console.WriteLine("Starting MSGer.tk...");
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
2014-04-25 21:55:43 +00:00
try
{
MainF = new MainForm();
Application.Run(MainF);
}
catch (FileNotFoundException e)
2014-04-25 21:55:43 +00:00
{
MessageBox.Show("Egy fájl nem található.\nA fájl neve:\n" + e.FileName + "\nEnnél a műveletnél: " + e.TargetSite);
2014-04-25 21:55:43 +00:00
}
}
public static readonly bool ShowFirstChangeExceptions = false;
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (MessageBox.Show("An unhandled error occured\n" + ((Exception)e.ExceptionObject).Message + "\nHere: " + ((Exception)e.ExceptionObject).TargetSite + ((e.IsTerminating) ? "\nThe program will exit(?)." : "") + "\n\nExit?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
Program.Exit(CurrentUser.UserID != 0); //2014.09.26. - Ha be van jelentkezve, elmenti a beállításokat - Csak mert így tán a legegyszerűbb
}
static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
if (ShowFirstChangeExceptions)
MessageBox.Show("An error occured (a first change exception):\n" + e.Exception.Message + "\nHere: " + e.Exception.TargetSite);
2014-04-25 21:55:43 +00:00
}
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0,
DateTimeKind.Utc);
public static DateTime UnixTimeToDateTime(string text)
{
double seconds = double.Parse(text, CultureInfo.InvariantCulture);
//2014.04.25.
DateTime time = Epoch.AddSeconds(seconds);
time = DateTime.SpecifyKind(time, DateTimeKind.Utc);
return time.ToLocalTime();
}
public static string DateTimeToUnixTime(DateTime time) //2014.08.28.
{
return ((time.Ticks - 621355968000000000) / 10000000).ToString();
}
private static void BeforeExit(bool savesettings)
{ //2014.09.01.
2014-04-25 21:55:43 +00:00
if (MainF != null)
{
MainF.Hide();
MainF.toolStripMenuItem4.Enabled = false; //2014.04.12.
MainF.toolStripMenuItem8.Enabled = false; //2014.04.12.
MainF.notifyIcon1.Dispose(); //2014.08.28.
2014-04-25 21:55:43 +00:00
if (CurrentUser.UserID != 0) //2014.04.18.
{
CurrentUser.SendChanges = false; //2014.08.30.
2014-04-25 21:55:43 +00:00
MainF.SetOnlineState(null, null); //2014.04.12.)
if (MainF.WindowState == FormWindowState.Maximized) //2014.04.18. - 2014.08.08.
Storage.Settings["windowstate"] = "1";
2014-04-25 21:55:43 +00:00
else if (MainF.WindowState == FormWindowState.Minimized)
Storage.Settings["windowstate"] = "2";
2014-04-25 21:55:43 +00:00
else if (MainF.WindowState == FormWindowState.Normal)
Storage.Settings["windowstate"] = "3";
Storage.Save(true); //2014.08.07.
2014-04-25 21:55:43 +00:00
}
}
if (savesettings) //2014.08.28.
Storage.Save(false); //2014.08.08.
if (Networking.ReceiverConnection != null)
Networking.ReceiverConnection.Close();
if (Networking.SenderConnection != null)
Networking.SenderConnection.Close();
}
public static void Exit(bool savesettings)
{ //2014.04.12.
BeforeExit(savesettings);
2014-07-27 08:28:59 +00:00
Environment.Exit(0); //Hatásosabb
2014-04-25 21:55:43 +00:00
MessageBox.Show("Hiba: Nem sikerült leállítani a programot.");
}
public static void Exit()
{ //2014.08.28. - Csak akkor ne mentse el a beállításokat, ha nem is töltötte még be őket
Exit(true);
}
public static void Restart(bool savesettings)
{ //2014.09.01.
BeforeExit(savesettings);
Process.Start(((ProcessName.Contains("vshost")) ? ProcessName.Replace(".vshost", "") : ProcessName) + ".exe");
Environment.Exit(0);
MessageBox.Show("Hiba: Nem sikerült leállítani a programot.");
}
2014-04-25 21:55:43 +00:00
}
static class EnumerableExtensions
2014-04-25 21:55:43 +00:00
{
public static bool HasSameElementsAs<T>(
this IEnumerable<T> first,
IEnumerable<T> second
)
2014-04-25 21:55:43 +00:00
{
var firstMap = first
.GroupBy(x => x)
.ToDictionary(x => x.Key, x => x.Count());
var secondMap = second
.GroupBy(x => x)
.ToDictionary(x => x.Key, x => x.Count());
return
firstMap.Keys.All(x =>
secondMap.Keys.Contains(x) && firstMap[x] == secondMap[x]
) &&
secondMap.Keys.All(x =>
firstMap.Keys.Contains(x) && secondMap[x] == firstMap[x]
);
}
2014-04-25 21:55:43 +00:00
public static bool Test(this object A, object B)
{
return object.ReferenceEquals(A, B);
}
2014-04-25 21:55:43 +00:00
///
/// Clones an object by using the .
///
/// The object to clone.
///
/// The object to be cloned must be serializable.
///
public static object Clone(object obj)
{
using (MemoryStream buffer = new MemoryStream())
2014-04-25 21:55:43 +00:00
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(buffer, obj);
buffer.Position = 0;
object temp = formatter.Deserialize(buffer);
return temp;
2014-04-25 21:55:43 +00:00
}
}
}
}