|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +using System.IO; |
| 4 | +using Mono.Data.Sqlite; |
| 5 | + |
| 6 | +namespace SharpCpp |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Very simplified preferences, just to store key/value pairs of strings. |
| 10 | + /// </summary> |
| 11 | + public static class Preferences |
| 12 | + { |
| 13 | + public const string AppName = "SharpCppApp"; |
| 14 | + |
| 15 | + static readonly DbHelper _dbHelper; |
| 16 | + |
| 17 | + static readonly PreferencesDbHelper _preferencesDbHelper; |
| 18 | + |
| 19 | + static Preferences() |
| 20 | + { |
| 21 | + _dbHelper = new DbHelper(); |
| 22 | + _preferencesDbHelper = new PreferencesDbHelper(_dbHelper); |
| 23 | + } |
| 24 | + |
| 25 | + class PreferencesDbHelper |
| 26 | + { |
| 27 | + const string TableName = "Preferences"; |
| 28 | + |
| 29 | + const string IdColumn = "id"; |
| 30 | + |
| 31 | + const string ValueColumn = "value"; |
| 32 | + |
| 33 | + readonly DbHelper DbHelper; |
| 34 | + |
| 35 | + internal PreferencesDbHelper(DbHelper dbHelper) |
| 36 | + { |
| 37 | + DbHelper = dbHelper; |
| 38 | + CreateTableIfNone(); |
| 39 | + } |
| 40 | + |
| 41 | + void CreateTableIfNone() |
| 42 | + { |
| 43 | + using (var connection = DbHelper.CreateDbConnection()) { |
| 44 | + connection.Open(); |
| 45 | + |
| 46 | + var command = "create table if not exists " + |
| 47 | + $"{ TableName } (" + |
| 48 | + $" { IdColumn } text primary key, " + |
| 49 | + $" { ValueColumn } text" + |
| 50 | + ")"; |
| 51 | + |
| 52 | + using (var c = connection.CreateCommand()) { |
| 53 | + c.CommandText = command; |
| 54 | + c.CommandType = CommandType.Text; |
| 55 | + c.ExecuteNonQuery(); |
| 56 | + } |
| 57 | + |
| 58 | + connection.Close(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public string GetValue(string key) |
| 63 | + { |
| 64 | + string value = null; |
| 65 | + |
| 66 | + using (var connection = _dbHelper.CreateDbConnection()) { |
| 67 | + connection.Open(); |
| 68 | + |
| 69 | + using (var command = connection.CreateCommand()) { |
| 70 | + command.CommandText = $"SELECT {ValueColumn} FROM [{TableName}] WHERE {IdColumn}=@id"; |
| 71 | + command.Parameters.AddWithValue("@id", key); |
| 72 | + |
| 73 | + using (var reader = command.ExecuteReader()) { |
| 74 | + while (reader.Read()) { |
| 75 | + value = (string)reader[0]; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + connection.Close(); |
| 81 | + } |
| 82 | + |
| 83 | + return value; |
| 84 | + } |
| 85 | + |
| 86 | + public void PutValue(string key, string value) |
| 87 | + { |
| 88 | + using (var connection = _dbHelper.CreateDbConnection()) { |
| 89 | + connection.Open(); |
| 90 | + |
| 91 | + // insert or ignore |
| 92 | + |
| 93 | + using (var command = connection.CreateCommand()) { |
| 94 | + command.CommandText = $"INSERT OR IGNORE INTO [{TableName}] ({IdColumn}, {ValueColumn}) " + |
| 95 | + "VALUES (@id, @value)"; |
| 96 | + |
| 97 | + command.Parameters.AddWithValue("@id", key); |
| 98 | + command.Parameters.AddWithValue("@value", value); |
| 99 | + |
| 100 | + command.ExecuteNonQuery(); |
| 101 | + } |
| 102 | + |
| 103 | + // update |
| 104 | + |
| 105 | + using (var command = connection.CreateCommand()) { |
| 106 | + command.CommandText = $"UPDATE {TableName} SET {ValueColumn}=@value WHERE {IdColumn}=@id"; |
| 107 | + |
| 108 | + command.Parameters.AddWithValue("@id", key); |
| 109 | + command.Parameters.AddWithValue("@value", value); |
| 110 | + |
| 111 | + command.ExecuteNonQuery(); |
| 112 | + } |
| 113 | + |
| 114 | + connection.Close(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + class DbHelper |
| 120 | + { |
| 121 | + const string DbName = "SharpCppApp.db"; |
| 122 | + |
| 123 | + readonly string _dbFile; |
| 124 | + |
| 125 | + internal DbHelper() |
| 126 | + { |
| 127 | + _dbFile = CreateDbIfNone(); |
| 128 | + } |
| 129 | + |
| 130 | + string CreateDbIfNone() |
| 131 | + { |
| 132 | + string dbFile; |
| 133 | + |
| 134 | + var documents = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); |
| 135 | + var appDirectory = Path.Combine(documents, AppName); |
| 136 | + |
| 137 | + if (!Directory.Exists(appDirectory)) { |
| 138 | + Directory.CreateDirectory(appDirectory); |
| 139 | + } |
| 140 | + |
| 141 | + dbFile = Path.Combine(appDirectory, DbName); |
| 142 | + if (!File.Exists(dbFile)) { |
| 143 | + SqliteConnection.CreateFile(dbFile); |
| 144 | + } |
| 145 | + |
| 146 | + return dbFile; |
| 147 | + } |
| 148 | + |
| 149 | + public SqliteConnection CreateDbConnection() |
| 150 | + { |
| 151 | + return new SqliteConnection("Data Source=" + _dbFile); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + #region LastSelectedFilename |
| 156 | + |
| 157 | + const string LastSelectedFilenameKey = "last_selected_filename"; |
| 158 | + |
| 159 | + // cached |
| 160 | + static string _selectedFilename; |
| 161 | + |
| 162 | + static bool _selectedFilenameLazyLoaded; |
| 163 | + |
| 164 | + public static string LastSelectedFilename { |
| 165 | + get { |
| 166 | + if (!_selectedFilenameLazyLoaded) { |
| 167 | + _selectedFilename = _preferencesDbHelper.GetValue(LastSelectedFilenameKey); |
| 168 | + _selectedFilenameLazyLoaded = true; |
| 169 | + } |
| 170 | + |
| 171 | + return _selectedFilename; |
| 172 | + } |
| 173 | + set { |
| 174 | + _selectedFilename = value; |
| 175 | + _preferencesDbHelper.PutValue(LastSelectedFilenameKey, value); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + #endregion |
| 180 | + |
| 181 | + } |
| 182 | +} |
0 commit comments