1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
//
// Authors:
// Ben Motmans <ben.motmans@gmail.com>
//
// Copyright (c) 2008 Ben Motmans
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using Gtk;
using System;
using System.Threading;
using System.Collections.Generic;
using Mono.Addins;
using MonoDevelop.Core;
using MonoDevelop.Ide;
using MonoDevelop.Database.Sql;
namespace MonoDevelop.Database.Components
{
[System.ComponentModel.Category("MonoDevelop.Database.Components")]
[System.ComponentModel.ToolboxItem(true)]
public partial class ConnectionSettingsWidget : Gtk.Bin
{
public event EventHandler NeedsValidation;
protected bool isDefaultSettings;
protected ListStore storeDatabases;
protected bool isDatabaseListEmpty;
protected bool isEditMode = false;
protected bool enablePasswordEntry;
protected bool enableUsernameEntry;
protected bool enableServerEntry;
protected bool enablePortEntry;
protected bool enableRefreshButton;
protected bool enableOpenButton;
protected bool enableTestButton;
protected bool enableIntegratedSecurity;
protected IDbFactory factory;
protected DatabaseConnectionSettings settings;
public ConnectionSettingsWidget (IDbFactory factory)
{
if (factory == null)
throw new ArgumentNullException ("factory");
this.factory = factory;
this.Build();
textConnectionString.Buffer.Changed += new EventHandler (ConnectionStringChanged);
checkCustom.Toggled += new EventHandler (CustomConnectionStringActivated);
storeDatabases = comboDatabase.Model as ListStore;
comboDatabase.Entry.Changed += new EventHandler (DatabaseChanged);
EnableServerEntry = true;
EnablePortEntry = true;
EnableUsernameEntry = true;
EnablePasswordEntry = true;
EnableRefreshButton = true;
EnableOpenButton = false;
}
public ConnectionSettingsWidget (IDbFactory factory, bool isEditMode):this(factory)
{
this.isEditMode = isEditMode;
}
public DatabaseConnectionSettings ConnectionSettings {
get {
if (settings == null)
settings = CreateDatabaseConnectionSettings ();
FillDatabaseConnectionSettings (settings);
return settings;
}
}
public bool EnableServerEntry {
get { return enableServerEntry; }
set {
enableServerEntry = value;
entryServer.Sensitive = value;
}
}
public bool EnablePortEntry {
get { return enablePortEntry; }
set {
enablePortEntry = value;
spinPort.Sensitive = value;
checkDefaultPort.Sensitive = value;
}
}
public bool EnableIntegratedSecurity {
get { return enableIntegratedSecurity; }
set {
enableIntegratedSecurity = value;
}
}
public bool EnableUsernameEntry {
get { return enableUsernameEntry; }
set {
enableUsernameEntry = value;
entryUsername.Sensitive = value;
}
}
public bool EnablePasswordEntry {
get { return enablePasswordEntry; }
set {
enablePasswordEntry = value;
entryPassword.Sensitive = value;
}
}
public bool EnableRefreshButton {
get { return enableRefreshButton; }
set {
enableRefreshButton = value;
buttonRefresh.Sensitive = value;
}
}
public bool EnableOpenButton {
get { return enableOpenButton; }
set {
enableOpenButton = value;
buttonOpen.Sensitive = value;
}
}
public bool EnableTestButton {
get { return enableTestButton; }
set {
enableTestButton = value;
buttonTest.Sensitive = value;
}
}
protected ComboBoxEntry ComboDatabase {
get { return comboDatabase; }
}
protected internal virtual void FillDatabaseConnectionSettings (DatabaseConnectionSettings settings)
{
settings.ConnectionString = textConnectionString.Buffer.Text;
settings.UseConnectionString = checkCustom.Active;
settings.MinPoolSize = (int)spinMinPoolSize.Value;
settings.MaxPoolSize = (int)spinMaxPoolSize.Value;
settings.Name = entryName.Text;
if (!checkIntegratedSecurity.Active) {
settings.Username = entryUsername.Text;
settings.Password = entryPassword.Text;
settings.UseIntegratedSecurity = false;
} else {
settings.UseIntegratedSecurity = true;
}
settings.Server = entryServer.Text;
if (spinPort.Sensitive)
settings.Port = (int)spinPort.Value;
else
settings.Port = 0;
settings.Database = comboDatabase.Entry.Text;
settings.SavePassword = checkSavePassword.Active;
}
public virtual void ShowSettings (DatabaseConnectionSettings settings)
{
checkCustom.Active = settings.UseConnectionString;
entryName.Text = String.IsNullOrEmpty (settings.Name) ? String.Empty : settings.Name;
entryPassword.Text = String.IsNullOrEmpty (settings.Password) ? String.Empty : settings.Password;
spinPort.Value = settings.Port > 0 ? settings.Port : spinPort.Value;
entryServer.Text = String.IsNullOrEmpty (settings.Server) ? String.Empty : settings.Server;
entryUsername.Text = String.IsNullOrEmpty (settings.Username) ? String.Empty : settings.Username;
textConnectionString.Buffer.Text = String.IsNullOrEmpty (settings.ConnectionString) ? String.Empty : settings.ConnectionString;
comboDatabase.Entry.Text = String.IsNullOrEmpty (settings.Database) ? String.Empty : settings.Database;
spinMinPoolSize.Value = settings.MinPoolSize;
spinMaxPoolSize.Value = settings.MaxPoolSize;
checkIntegratedSecurity.Sensitive = settings.CanUseIntegratedSecurity;
if (settings.UseIntegratedSecurity)
checkIntegratedSecurity.Active = true;
else
checkIntegratedSecurity.Active = false;
FillDatabaseConnectionSettings (settings);
}
protected internal virtual void AppendDatabase (DatabaseConnectionSettings settings)
{
storeDatabases.AppendValues (settings.Database);
isDatabaseListEmpty = false;
}
protected virtual void NameChanged (object sender, System.EventArgs e)
{
CheckSettings (false);
}
protected virtual void ServerChanged (object sender, System.EventArgs e)
{
CheckSettings (true);
}
protected virtual void PortChanged (object sender, System.EventArgs e)
{
CheckSettings (true);
}
protected virtual void UsernameChanged (object sender, System.EventArgs e)
{
CheckSettings (true);
}
protected virtual void PasswordChanged (object sender, System.EventArgs e)
{
CheckSettings (true);
}
protected virtual void MinPoolSizeChanged (object sender, System.EventArgs e)
{
if (spinMinPoolSize.Value > spinMaxPoolSize.Value)
spinMaxPoolSize.Value = spinMinPoolSize.Value;
}
protected virtual void MaxPoolSizeChanged (object sender, System.EventArgs e)
{
if (spinMaxPoolSize.Value < spinMinPoolSize.Value)
spinMinPoolSize.Value = spinMaxPoolSize.Value;
}
protected virtual void CustomConnectionStringActivated (object sender, System.EventArgs e)
{
bool sens = !checkCustom.Active;
entryPassword.Sensitive = sens && enablePasswordEntry;
entryUsername.Sensitive = sens && enableUsernameEntry;
entryServer.Sensitive = sens && enableServerEntry;
spinPort.Sensitive = sens && enablePortEntry;
checkDefaultPort.Sensitive = sens && enablePortEntry;
checkIntegratedSecurity.Sensitive = sens && enableIntegratedSecurity;
comboDatabase.Sensitive = sens;
buttonRefresh.Sensitive = sens && enableRefreshButton;
scrolledwindow.Sensitive = !sens;
}
protected virtual void ConnectionStringChanged (object sender, EventArgs e)
{
CheckSettings (true);
}
protected virtual void DatabaseChanged (object sender, EventArgs e)
{
if (isDatabaseListEmpty && comboDatabase.Entry.Text == AddinCatalog.GetString ("No databases found!")) {
comboDatabase.Entry.Text = String.Empty;
}
CheckSettings (true);
}
protected virtual void CheckSettings (bool requiresTest)
{
isDefaultSettings = false;
if (requiresTest)
labelTest.Text = String.Empty;
if (NeedsValidation != null)
NeedsValidation (this, EventArgs.Empty);
}
public virtual bool ValidateFields ()
{
bool ok = false;
if (checkCustom.Active) {
ok = textConnectionString.Buffer.Text.Length > 0;
} else {
bool alreadyExists = ConnectionContextService.DatabaseConnectionContextExist (ConnectionSettings);
TreeIter iter;
ok = entryName.Text.Length > 0
&& (entryServer.Text.Length > 0 || !enableServerEntry)
&& (entryUsername.Text.Length > 0 || !enableUsernameEntry)
&& (comboDatabase.Entry.Text.Length > 0)
&& (!alreadyExists || (isEditMode && ConnectionSettings.Name == entryName.Text));
if (alreadyExists && !isEditMode)
labelMessage.Markup = string.Concat ("<i>",
AddinCatalog.GetString ("Connection Name Already used, choose another."),"</i>");
else
labelMessage.Markup = "";
}
return ok;
}
protected virtual DatabaseConnectionSettings CreateDatabaseConnectionSettings ()
{
DatabaseConnectionSettings settings = new DatabaseConnectionSettings ();
settings.ProviderIdentifier = factory.Identifier;
FillDatabaseConnectionSettings (settings);
return settings;
}
protected virtual void RefreshClicked (object sender, System.EventArgs e)
{
DatabaseConnectionSettings settingsCopy = CreateDatabaseConnectionSettings ();
storeDatabases.Clear ();
ThreadPool.QueueUserWorkItem (new WaitCallback (RefreshClickedThreaded), settingsCopy);
}
protected virtual void RefreshClickedThreaded (object state)
{
DatabaseConnectionSettings settings = state as DatabaseConnectionSettings;
DatabaseConnectionContext context = new DatabaseConnectionContext (settings);
IDbFactory fac = DbFactoryService.GetDbFactory (settings.ProviderIdentifier);
FakeConnectionPool pool = null;
try {
pool = new FakeConnectionPool (fac, fac.ConnectionProvider, context);
pool.Initialize ();
if (pool.HasErrors) {
MessageService.ShowError (pool.Error);
return;
}
ISchemaProvider prov = fac.CreateSchemaProvider (pool);
DatabaseSchemaCollection databases = prov.GetDatabases ();
DispatchService.GuiDispatch (delegate () {
foreach (DatabaseSchema db in databases) {
storeDatabases.AppendValues (db.Name);
}
});
isDatabaseListEmpty = databases.Count == 0;
} catch {
} finally {
if (pool != null)
pool.Close ();
}
if (isDatabaseListEmpty) {
DispatchService.GuiDispatch (delegate () {
storeDatabases.AppendValues (AddinCatalog.GetString ("No databases found!"));
});
} else {
DispatchService.GuiDispatch (delegate () {
TreeIter iter;
if (storeDatabases.GetIterFirst (out iter))
comboDatabase.SetActiveIter (iter);
});
}
}
protected virtual void OpenClicked (object sender, System.EventArgs e)
{
//do nothing by default
//TODO: show a open file dialog with *.* as filter???
}
protected virtual void TestClicked (object sender, System.EventArgs e)
{
//TODO: use a Thread object, so the thread can be aborted when the entered values change
DatabaseConnectionSettings settingsCopy = CreateDatabaseConnectionSettings ();
labelTest.Text = AddinCatalog.GetString ("Trying to connect to database ...");
buttonTest.Sensitive = false;
ThreadPool.QueueUserWorkItem (new WaitCallback (TestClickedThreaded), settingsCopy);
}
protected virtual void TestClickedThreaded (object state)
{
DatabaseConnectionSettings settings = state as DatabaseConnectionSettings;
DatabaseConnectionContext context = new DatabaseConnectionContext (settings);
IDbFactory fac = DbFactoryService.GetDbFactory (settings.ProviderIdentifier);
bool success = false;
string error = null;
FakeConnectionPool pool = null;
IPooledDbConnection conn = null;
try {
pool = new FakeConnectionPool (fac, fac.ConnectionProvider, context);
success = pool.Initialize ();
error = pool.Error;
} catch (System.Data.DataException ex) {
error = ex.Message;
success = false;
} finally {
if (conn != null) {
conn.Release ();
conn.Dispose ();
}
if (pool != null)
pool.Close ();
}
DispatchService.GuiDispatch (delegate () {
buttonTest.Sensitive = true;
if (success)
labelTest.Text = AddinCatalog.GetString ("Test Succeeded.");
else
labelTest.Text = AddinCatalog.GetString ("Test Failed: {0}.", error);
});
}
protected virtual void OnCheckDefaultPortToggled (object sender, System.EventArgs e)
{
if (checkDefaultPort.Active)
spinPort.Sensitive = false;
else
spinPort.Sensitive = true;
}
protected virtual void OnCheckIntegratedSecurityToggled (object sender, System.EventArgs e)
{
if (checkIntegratedSecurity.Active) {
entryUsername.Sensitive = false;
entryPassword.Sensitive = false;
checkSavePassword.Sensitive = false;
} else {
entryUsername.Sensitive = true;
entryPassword.Sensitive = true;
checkSavePassword.Sensitive = true;
}
}
}
}
|