File: new_connection_wizard.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (196 lines) | stat: -rw-r--r-- 6,732 bytes parent folder | download | duplicates (2)
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
/* 
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "new_connection_wizard.h"

#include "cppdbc.h"
#include "grtpp_util.h"
#include "grt/common.h"
#include "mforms/utilities.h"
#include "base/string_utilities.h"

#include "new_server_instance_wizard.h"

using namespace mforms;
using namespace base;

//--------------------------------------------------------------------------------------------------

/**
 * Determines if the given connection is an SSH connection and returns true if so.
 */
static bool is_ssh_connection(const db_mgmt_ConnectionRef &connection)
{
  if (connection.is_valid())
  {
    std::string driver= connection->driver().is_valid() ? connection->driver()->name() : "";
    return (driver == "MysqlNativeSSH");
  }
  return false;
}

//--------------------------------------------------------------------------------------------------

/**
 * Determines if the given connection is a local connection (i.e. to the current box).
 */
static bool is_local_connection(const db_mgmt_ConnectionRef &connection)
{
  if (connection.is_valid())
  {
    std::string hostname= connection->parameterValues().get_string("hostName");

    if (!is_ssh_connection(connection) && (hostname == "localhost" || hostname.empty() || hostname == "127.0.0.1"))
      return true;
  }
  return false;
}

//----------------- NewConnectionWizard ------------------------------------------------------------

NewConnectionWizard::NewConnectionWizard(wb::WBContext *context, const db_mgmt_ManagementRef &mgmt)
  : mforms::Form(0), _context(context), _mgmt(mgmt), _panel(false), _top_vbox(false), _bottom_hbox(true)
{
  set_title(_("Manage DB Connections"));
  set_name("new_connection_wizard");
  
  _top_vbox.set_padding(MF_WINDOW_PADDING);
  _top_vbox.set_spacing(12);
  _top_vbox.add(&_panel, true, true);
  _top_vbox.add(&_bottom_hbox, false, true);
  
  _bottom_hbox.set_spacing(12);
  
  _panel.init(_mgmt);
  _panel.set_driver_changed_cb(boost::bind(&NewConnectionWizard::driver_changed_cb, this, _1));
    
  _conn_name= _panel.get_name_entry();

  scoped_connect(_config_button.signal_clicked(), boost::bind(&NewConnectionWizard::open_remote_mgm_config, this));
  _config_button.set_text(_("Configure Server Management..."));
  _config_button.enable_internal_padding(true);

  _bottom_hbox.add(&_config_button, false, true);
  _bottom_hbox.add_end(&_ok_button, false, true);
  _bottom_hbox.add_end(&_cancel_button, false, true);
  _bottom_hbox.add_end(&_test_button, false, true);
  
  _ok_button.set_text(_("OK"));
  _cancel_button.set_text(_("Cancel"));
  _test_button.set_text(_("Test Connection"));
  scoped_connect(_test_button.signal_clicked(), boost::bind(&grtui::DbConnectPanel::test_connection, &_panel));
  
  _ok_button.enable_internal_padding(true);
  _cancel_button.enable_internal_padding(true);  
  _test_button.enable_internal_padding(true);
  
  set_content(&_top_vbox);
  
  set_size(800, 500);
  center();
}

//--------------------------------------------------------------------------------------------------

NewConnectionWizard::~NewConnectionWizard()
{
}

//--------------------------------------------------------------------------------------------------

void NewConnectionWizard::driver_changed_cb(const db_mgmt_DriverRef &driver)
{
  _config_button.show(driver->name() != "MySQLFabric");
}


//--------------------------------------------------------------------------------------------------

void NewConnectionWizard::open_remote_mgm_config()
{
  NewServerInstanceWizard wizard(_context, _panel.get_connection());
  wizard.run_modal();
}

//--------------------------------------------------------------------------------------------------

db_mgmt_ConnectionRef NewConnectionWizard::run()
{ 
  _connection = db_mgmt_ConnectionRef(_mgmt.get_grt());
  _connection->driver(_mgmt->rdbms()[0]->defaultDriver());
  if (find_named_object_in_list(_connection->driver()->parameters(), "useSSL").is_valid())
  {
    // prefer SSL if possible by default
    _connection->parameterValues().set("useSSL", grt::IntegerRef(1));
  }
  _connection->hostIdentifier("Mysql@127.0.0.1:3306");
  _panel.get_be()->set_connection_and_update(_connection);
  _panel.get_be()->save_changes();
  // Return the newly created connection object.
  while (run_modal(&_ok_button, &_cancel_button))
  {
    // Check for duplicate names.
    bool name_ok= true;
    std::string name= _conn_name->get_string_value();
    if (base::trim(name).empty())
    {
      std::string text= _("Please enter a proper name for your new connection.");
      Utilities::show_error(_("Improper name"), text, _("OK"));
      name_ok= false;
    }
    
    if (name_ok)
    {
      GRTLIST_FOREACH(db_mgmt_Connection, _mgmt->storedConns(), conn)
      {
        if ((*conn)->name() == name)
        {
          std::string text= strfmt(_("A connection with the name %s exists already. Please choose another one."), name.c_str());
          Utilities::show_error(_("Duplicate name"), text, _("OK"));
          name_ok= false;
          break;
        }
      }
    }

    if (name_ok)
    {
      _connection->name(_conn_name->get_string_value().c_str());
      _mgmt->storedConns().insert(_connection);

      // Auto create an unconfigured server instance for this connection.
      // The module creates the instance, adds it to the stored instances and stores them on disk.
      if (_connection->driver()->name() != "MySQLFabric")
      {
        grt::BaseListRef args(_mgmt.get_grt());
        args.ginsert(_connection);
        if (is_local_connection(_connection))
          db_mgmt_ServerInstanceRef::cast_from(_mgmt.get_grt()->call_module_function("WbAdmin", "autoDetectLocalInstance", args));
        else
          db_mgmt_ServerInstanceRef::cast_from(_mgmt.get_grt()->call_module_function("WbAdmin", "autoDetectRemoteInstance", args));
      }

      return _connection;
    }
  }
  return db_mgmt_ConnectionRef();
}

//--------------------------------------------------------------------------------------------------