3 // Christian Hergert <chris@mosaix.net>
4 // Ben Motmans <ben.motmans@gmail.com>
6 // Copyright (c) 2005 Christian Hergert
7 // Copyright (c) 2007 Ben Motmans
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
31 using System
.Xml
.Serialization
;
32 using System
.Collections
.Generic
;
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
)
62 throw new ArgumentNullException ("settings");
64 DatabaseConnectionContext context
= new DatabaseConnectionContext (settings
);
65 AddDatabaseConnectionContext (context
);
69 public static void AddDatabaseConnectionContext (DatabaseConnectionContext context
)
72 throw new ArgumentNullException ("context");
74 if (!contexts
.Contains (context
)) {
75 contexts
.Add (context
);
77 if (ConnectionContextAdded
!= null)
78 ConnectionContextAdded (null, new DatabaseConnectionContextEventArgs (context
));
82 public static void RemoveDatabaseConnectionContext (DatabaseConnectionContext context
)
85 throw new ArgumentNullException ("context");
87 contexts
.Remove (context
);
89 if (ConnectionContextRemoved
!= null)
90 ConnectionContextRemoved (null, new DatabaseConnectionContextEventArgs (context
));
93 public static void EditDatabaseConnectionContext (DatabaseConnectionContext context
)
96 throw new ArgumentNullException ("context");
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
)) {
108 using (FileStream fs
= File
.OpenRead (configFile
)) {
109 XmlSerializer serializer
= new XmlSerializer (typeof (DatabaseConnectionSettingsCollection
));
110 connections
= (DatabaseConnectionSettingsCollection
) serializer
.Deserialize (fs
);
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
);
124 sb
.Append ("Error: unable to load database provider '");
125 sb
.Append (settings
.ProviderIdentifier
);
126 sb
.Append ("' for connection '");
127 sb
.Append (settings
.Name
);
129 sb
.Append (Environment
.NewLine
);
133 contexts
.Add (new DatabaseConnectionContext (settings
));
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
;