* Makefile.am:
[monodevelop.git] / extras / MonoDevelop.Database / MonoDevelop.Database.Sql / ConnectionContextService.cs
blob6046a7326a4be52030d129b36f99914e0b2ea43a
1 //
2 // Authors:
3 // Christian Hergert <chris@mosaix.net>
4 // Ben Motmans <ben.motmans@gmail.com>
5 //
6 // Copyright (c) 2005 Christian Hergert
7 // Copyright (c) 2007 Ben Motmans
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
28 using System;
29 using System.IO;
30 using System.Text;
31 using System.Xml.Serialization;
32 using System.Collections.Generic;
33 using Mono.Addins;
34 using MonoDevelop.Core;
36 namespace MonoDevelop.Database.Sql
38 public static class ConnectionContextService
40 public static event DatabaseConnectionContextEventHandler ConnectionContextAdded;
41 public static event DatabaseConnectionContextEventHandler ConnectionContextRemoved;
42 public static event DatabaseConnectionContextEventHandler ConnectionContextEdited;
43 public static event DatabaseConnectionContextEventHandler ConnectionContextRefreshed;
45 private static DatabaseConnectionContextCollection contexts;
47 private static string configFile = null;
49 static ConnectionContextService ()
51 configFile = Path.Combine (PropertyService.ConfigPath, "MonoDevelop.Database.ConnectionManager.xml");
52 Initialize (configFile);
55 public static DatabaseConnectionContextCollection DatabaseConnections {
56 get { return contexts; }
59 public static DatabaseConnectionContext AddDatabaseConnectionContext (DatabaseConnectionSettings settings)
61 if (settings == null)
62 throw new ArgumentNullException ("settings");
64 DatabaseConnectionContext context = new DatabaseConnectionContext (settings);
65 AddDatabaseConnectionContext (context);
66 return context;
69 public static void AddDatabaseConnectionContext (DatabaseConnectionContext context)
71 if (context == null)
72 throw new ArgumentNullException ("context");
74 if (!contexts.Contains (context)) {
75 contexts.Add (context);
76 Save ();
77 if (ConnectionContextAdded != null)
78 ConnectionContextAdded (null, new DatabaseConnectionContextEventArgs (context));
82 public static void RemoveDatabaseConnectionContext (DatabaseConnectionContext context)
84 if (context == null)
85 throw new ArgumentNullException ("context");
87 contexts.Remove (context);
88 Save ();
89 if (ConnectionContextRemoved != null)
90 ConnectionContextRemoved (null, new DatabaseConnectionContextEventArgs (context));
93 public static void EditDatabaseConnectionContext (DatabaseConnectionContext context)
95 if (context == null)
96 throw new ArgumentNullException ("context");
98 Save ();
99 if (ConnectionContextEdited != null)
100 ConnectionContextEdited (null, new DatabaseConnectionContextEventArgs (context));
103 internal static void Initialize (string configFile)
105 DatabaseConnectionSettingsCollection connections = null;
106 if (File.Exists (configFile)) {
107 try {
108 using (FileStream fs = File.OpenRead (configFile)) {
109 XmlSerializer serializer = new XmlSerializer (typeof (DatabaseConnectionSettingsCollection));
110 connections = (DatabaseConnectionSettingsCollection) serializer.Deserialize (fs);
112 } catch {
113 LoggingService.LogError (AddinCatalog.GetString ("Unable to load stored SQL connection information."));
114 File.Delete (configFile);
118 contexts = new DatabaseConnectionContextCollection ();
119 if (connections != null) {
120 StringBuilder sb = new StringBuilder ();
121 foreach (DatabaseConnectionSettings settings in connections) {
122 IDbFactory fac = DbFactoryService.GetDbFactory (settings);
123 if (fac == null) {
124 sb.Append ("Error: unable to load database provider '");
125 sb.Append (settings.ProviderIdentifier);
126 sb.Append ("' for connection '");
127 sb.Append (settings.Name);
128 sb.Append ("'");
129 sb.Append (Environment.NewLine);
130 continue;
133 contexts.Add (new DatabaseConnectionContext (settings));
136 if (sb.Length > 0) {
137 Exception ex = new Exception (sb.ToString ());
138 QueryService.RaiseException (ex);
143 public static void Save ()
145 //temporarily empty all passwords that don't need to be saved
146 Dictionary<DatabaseConnectionSettings, string> tmp = new Dictionary<DatabaseConnectionSettings,string> ();
147 DatabaseConnectionSettingsCollection collection = new DatabaseConnectionSettingsCollection ();
148 foreach (DatabaseConnectionContext context in contexts) {
149 if (!context.ConnectionSettings.SavePassword) {
150 tmp.Add (context.ConnectionSettings, context.ConnectionSettings.Password);
151 context.ConnectionSettings.Password = null;
153 collection.Add (context.ConnectionSettings);
156 using (FileStream fs = new FileStream (configFile, FileMode.Create)) {
157 XmlSerializer serializer = new XmlSerializer (typeof (DatabaseConnectionSettingsCollection));
158 serializer.Serialize (fs, collection);
161 foreach (KeyValuePair<DatabaseConnectionSettings, string> pair in tmp)
162 pair.Key.Password = pair.Value;