miranda/Speak/Speak/Storage/Settings.cs

161 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Virtuoso.Miranda.Plugins.Infrastructure;
using Speak.Opt;
namespace Speak.Storage
{
class Settings
{
private class Contact
{
public string Status;
public bool message_read;
public bool status_read;
public Contact(string status, bool rmessage, bool rstatus)
{
this.Status = status;
this.message_read = rmessage;
this.status_read = rstatus;
}
}
private static Settings instance;
private Dictionary<string, Contact> contactlist;
private long maxTextLength = 0;
private bool globalActive = true;
private bool unknownmessageread = true;
private bool unknownstatusread = true;
/// <summary>
/// Get a Setting Class
/// </summary>
/// <returns>Settings Object</returns>
public static Settings getInstance()
{
if (instance == null)
instance = new Settings();
return instance;
}
private Settings()
{
this.contactlist = new Dictionary<string, Contact>();
}
public void ReadContactList()
{
this.contactlist = new Dictionary<string, Contact>();
ReadOnlyCollection<ContactInfo> list = MirandaContext.Current.MirandaDatabase.GetContacts();
foreach (ContactInfo item in list)
{
if (item.UniqueID != null)
{
//System.Windows.Forms.MessageBox.Show("leer");
Contact c = new Contact(item.Status.ToString(),
Options.readDBBool("message", "speak_config", item.MirandaHandle, true),
Options.readDBBool("status", "speak_config", item.MirandaHandle, true));
if (!this.contactlist.Keys.Contains(item.UniqueID.ToString()))
{
this.contactlist.Add(item.UniqueID.ToString(), c);
}
}
}
}
public void SetGlobalSettings()
{
this.maxTextLength = Options.readDBLong("max_msg_size", "speak_config");
this.globalActive = Options.readDBBool("active", "speak_config", true);
this.unknownmessageread = Options.readDBBool("message_u", "speak_config", true);
this.unknownstatusread = Options.readDBBool("status_u", "speak_config", true);
}
public bool canMessageRead(string text, string uid)
{
if (!this.globalActive)
{
return false;
}
if (text.Length > this.maxTextLength)
{
return false;
}
Contact c;
try
{
c = this.contactlist[uid];
}
catch (System.Collections.Generic.KeyNotFoundException)
{
return this.unknownmessageread;
}
return c.message_read;
}
public string getSpeakString()
{
return "{0} meint: {1}";
}
public bool hasChangedStatus(string uid, string status)
{
Contact c;
try
{
c = this.contactlist[uid];
}
catch (System.Collections.Generic.KeyNotFoundException)
{
if (uid != "" && status != "")
{
ReadContactList();
return true;
}
return false;
}
if (c.Status != status)
{
this.contactlist[uid] = new Contact(status, c.message_read, c.status_read);
return true;
}
return false;
}
public bool canStatusRead(string uid)
{
Contact c = this.contactlist[uid];
if (c != null)
{
return c.status_read;
}
return this.unknownstatusread;
}
public string getStatusString(StatusMode? status)
{
switch (status)
{
case StatusMode.Offline:
return "{0} ist Off";
case StatusMode.Online:
return "{0} ist da";
case StatusMode.Away:
return "{0} ist nur mal kurz weg";
case StatusMode.Invisible:
return "{0} hat sich versteckt";
case StatusMode.NA:
return "{0} ist nicht mehr da";
case StatusMode.DND:
return "{0} will in ruhe gelassen werden, schnautze";
case StatusMode.Occupied:
return "{0} ist beschäftigt";
case StatusMode.FreeForChat:
return "{0} will chatten";
}
return "{0} ist {1}";
}
}
}