mirror of
https://github.com/greenshot/greenshot
synced 2025-07-16 10:03:44 -07:00
Refactorings for the new IniConfig
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@853 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
232fd4e33d
commit
14b0863bcd
34 changed files with 338 additions and 499 deletions
|
@ -36,8 +36,6 @@ using Greenshot.Drawing;
|
|||
using Greenshot.Drawing.Fields;
|
||||
|
||||
namespace Greenshot.Configuration {
|
||||
public enum ScreenshotDestinations {Editor=1, FileDefault=2, FileWithDialog=4, Clipboard=8, Printer=16, EMail=32}
|
||||
|
||||
/// <summary>
|
||||
/// AppConfig is used for loading and saving the configuration. All public fields
|
||||
/// in this class are serialized with the BinaryFormatter and then saved to the
|
||||
|
@ -48,68 +46,10 @@ namespace Greenshot.Configuration {
|
|||
public class AppConfig {
|
||||
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(AppConfig));
|
||||
|
||||
//private static string loc = Assembly.GetExecutingAssembly().Location;
|
||||
//private static string oldFilename = Path.Combine(loc.Substring(0,loc.LastIndexOf(@"\")),"config.dat");
|
||||
private const string CONFIG_FILE_NAME = "config.dat";
|
||||
private static string configfilepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),@"Greenshot\");
|
||||
private static AppConfig instance = null;
|
||||
|
||||
public Dictionary<string, object> LastUsedFieldValues = new Dictionary<string, object>();
|
||||
|
||||
// the configuration part - all public vars are stored in the config file
|
||||
// don't use "null" and "0" as default value!
|
||||
|
||||
#region general application config
|
||||
public bool? General_RegisterHotkeys = true;
|
||||
public bool? General_IsFirstLaunch = true;
|
||||
#endregion
|
||||
|
||||
#region capture config
|
||||
public bool? Capture_Mousepointer = true;
|
||||
public bool? Capture_Windows_Interactive = false;
|
||||
public int Capture_Wait_Time = 100;
|
||||
public bool? Capture_Complete_Window = false;
|
||||
public bool? Capture_Window_Content = false;
|
||||
#endregion
|
||||
|
||||
#region user interface config
|
||||
public string Ui_Language = "";
|
||||
public bool? Ui_Effects_Flashlight = false;
|
||||
public bool? Ui_Effects_CameraSound = true;
|
||||
#endregion
|
||||
|
||||
#region output config
|
||||
public ScreenshotDestinations Output_Destinations = ScreenshotDestinations.Editor;
|
||||
|
||||
|
||||
public string Output_File_Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
|
||||
public string Output_File_FilenamePattern = "%title%_%YYYY%-%MM%-%DD%_%hh%-%mm%-%ss%";
|
||||
public string Output_File_Format = ImageFormat.Png.ToString();
|
||||
public bool? Output_File_CopyPathToClipboard = false;
|
||||
public int Output_File_JpegQuality = 80;
|
||||
public bool? Output_File_PromptJpegQuality = false;
|
||||
public int Output_File_IncrementingNumber = 1;
|
||||
|
||||
public string Output_FileAs_Fullpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"dummy.png");
|
||||
|
||||
public bool? Output_Print_PromptOptions = true;
|
||||
public bool? Output_Print_AllowRotate = true;
|
||||
public bool? Output_Print_AllowEnlarge = true;
|
||||
public bool? Output_Print_AllowShrink = true;
|
||||
public bool? Output_Print_Center = true;
|
||||
public bool? Output_Print_Timestamp = true;
|
||||
#endregion
|
||||
|
||||
#region editor config
|
||||
public Size Editor_WindowSize = new Size(540, 380);
|
||||
public Point Editor_WindowLocation = new Point(100, 100);
|
||||
public String Editor_WindowState = "Normal";
|
||||
public Rectangle Editor_Previous_Screenbounds = Rectangle.Empty;
|
||||
public Color[] Editor_RecentColors = new Color[12];
|
||||
public Font Editor_Font = null;
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// a private constructor because this is a singleton
|
||||
/// </summary>
|
||||
|
@ -122,170 +62,11 @@ namespace Greenshot.Configuration {
|
|||
/// <returns></returns>
|
||||
public static AppConfig GetInstance() {
|
||||
if (instance == null) {
|
||||
instance = Load();
|
||||
instance = new AppConfig();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void Reload() {
|
||||
AppConfig newInstance = Load();
|
||||
instance.Copy(newInstance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// loads the configuration from the config file
|
||||
/// </summary>
|
||||
/// <returns>an instance of AppConfig with all values set from the config file</returns>
|
||||
private static AppConfig Load() {
|
||||
AppConfig conf;
|
||||
CheckConfigFile();
|
||||
string configfilename = Path.Combine(configfilepath, CONFIG_FILE_NAME);
|
||||
try {
|
||||
LOG.Debug("Loading configuration from: " + configfilename);
|
||||
using (FileStream fileStream = File.Open(configfilename, FileMode.Open, FileAccess.Read)) {
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
||||
conf = (AppConfig) binaryFormatter.Deserialize(fileStream);
|
||||
}
|
||||
conf.SetDefaults();
|
||||
return conf;
|
||||
} catch (SerializationException e) {
|
||||
LOG.Error("Problem loading configuration from: " + configfilename, e);
|
||||
AppConfig config = new AppConfig();
|
||||
config.Store();
|
||||
return config;
|
||||
} catch (Exception e) {
|
||||
LOG.Error("Problem loading configuration from: " + configfilename, e);
|
||||
MessageBox.Show(String.Format("Could not load Greenshot's configuration file. Please check access permissions for '{0}'.\n",configfilename),"Error");
|
||||
Process.GetCurrentProcess().Kill();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks for the existence of a configuration file.
|
||||
/// First in greenshot's Applicationdata folder (where it is stored since 0.6),
|
||||
/// then (if it cannot be found there) in greenshot's program directory (where older
|
||||
/// versions might have stored it).
|
||||
/// If the latter is the case, the file is moved to the new location, so that a user does not lose
|
||||
/// their configuration after upgrading.
|
||||
/// If there is no file in both locations, a virgin config file is created.
|
||||
/// </summary>
|
||||
private static void CheckConfigFile() {
|
||||
// check if file is in the same location as started from, if this is the case
|
||||
// we will use this file instead of the Applicationdate folder
|
||||
// Done for Feature Request #2741508
|
||||
if (File.Exists(Path.Combine(Application.StartupPath, CONFIG_FILE_NAME))) {
|
||||
configfilepath = Application.StartupPath;
|
||||
} else if (!File.Exists(Path.Combine(configfilepath, CONFIG_FILE_NAME))) {
|
||||
Directory.CreateDirectory(configfilepath);
|
||||
new AppConfig().Store();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// saves the configuration values to the supplied config file
|
||||
/// </summary>
|
||||
public void Store() {
|
||||
Store(configfilepath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// saves the configuration values to the config path
|
||||
/// </summary>
|
||||
public void Store(string configpath) {
|
||||
string configfilename = Path.Combine(configpath, CONFIG_FILE_NAME);
|
||||
try {
|
||||
LOG.Debug("Saving configuration to: " + configfilename);
|
||||
using (FileStream fileStream = File.Open(configfilename, FileMode.Create)) {
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
formatter.Serialize(fileStream, this);
|
||||
}
|
||||
} catch (UnauthorizedAccessException e) {
|
||||
LOG.Error("Problem saving configuration to: " + configfilename, e);
|
||||
MessageBox.Show(Language.GetInstance().GetFormattedString(LangKey.config_unauthorizedaccess_write,configfilename),Language.GetInstance().GetString(LangKey.error));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// when new fields are added to this class, they are instanced
|
||||
/// with null by default. this method iterates over all public
|
||||
/// fields and uses reflection to set them to the proper default value.
|
||||
/// </summary>
|
||||
public void SetDefaults() {
|
||||
Type type = this.GetType();
|
||||
FieldInfo[] fieldInfos = type.GetFields();
|
||||
foreach (FieldInfo fi in fieldInfos) {
|
||||
object o = fi.GetValue(this);
|
||||
int i;
|
||||
if (o == null || (int.TryParse(o.ToString(), out i) && i == 0)) {
|
||||
// found field with value null. setting to default.
|
||||
AppConfig tmpConf = new AppConfig();
|
||||
Type tmpType = tmpConf.GetType();
|
||||
FieldInfo defaultField = tmpType.GetField(fi.Name);
|
||||
fi.SetValue(this, defaultField.GetValue(tmpConf));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Copy(AppConfig newConfig) {
|
||||
Type type = this.GetType();
|
||||
|
||||
// Copy fields
|
||||
FieldInfo[] fieldInfos = type.GetFields();
|
||||
foreach (FieldInfo fi in fieldInfos) {
|
||||
object newValue = fi.GetValue(newConfig);
|
||||
fi.SetValue(this, newValue);
|
||||
}
|
||||
// Update language
|
||||
if (newConfig.Ui_Language != null && !newConfig.Ui_Language.Equals(Language.GetInstance().CurrentLanguage)) {
|
||||
string newLang = Language.GetInstance().SetLanguage(newConfig.Ui_Language);
|
||||
// check if the language was not wat was supplied (near match)
|
||||
if (newConfig.Ui_Language.Equals(newLang)) {
|
||||
// Store change
|
||||
this.Store();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Properties GetAvailableProperties() {
|
||||
Properties properties = new Properties();
|
||||
Type type = typeof(AppConfig);
|
||||
FieldInfo[] fieldInfos = type.GetFields();
|
||||
foreach (FieldInfo fi in fieldInfos) {
|
||||
Type fieldType = fi.FieldType;
|
||||
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
|
||||
// We are dealing with a generic type that is nullable
|
||||
fieldType = Nullable.GetUnderlyingType(fieldType);
|
||||
}
|
||||
if (fieldType == typeof(string) || fieldType == typeof(bool) || fieldType == typeof(int)) {
|
||||
properties.AddProperty(fi.Name, fieldType.Name);
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void SetProperties(Properties properties) {
|
||||
Type type = this.GetType();
|
||||
FieldInfo[] fieldInfos = type.GetFields();
|
||||
foreach(string key in properties.Keys) {
|
||||
FieldInfo currentField = type.GetField(key);
|
||||
if (currentField != null) {
|
||||
object currentValue = currentField.GetValue(this);
|
||||
LOG.Debug("Before: " + currentField.Name + "=" + currentValue);
|
||||
if (currentField.FieldType == typeof(string)) {
|
||||
currentField.SetValue(this, properties.GetProperty(key));
|
||||
} else if (currentField.FieldType == typeof(bool) ||currentField.FieldType == typeof(bool?)) {
|
||||
currentField.SetValue(this, properties.GetBoolProperty(key));
|
||||
} else if (currentField.FieldType == typeof(int) || currentField.FieldType == typeof(int?)) {
|
||||
currentField.SetValue(this, properties.GetIntProperty(key));
|
||||
}
|
||||
LOG.Debug("After: " + currentField.Name + "=" + currentField.GetValue(this));
|
||||
} else {
|
||||
LOG.Warn("Configuration for " + key + " not found! (Incorrect key?)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateLastUsedFieldValue(IField f) {
|
||||
if(f.Value != null) {
|
||||
string key = GetKeyForField(f);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue