File: program.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 (360 lines) | stat: -rw-r--r-- 11,111 bytes parent folder | download
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
#include "grtpp_module_python.h"

#include "gtk/lf_mforms.h"

// linux ui includes
#include "gtk_helpers.h"
#include "program.h"
#include "main_form.h"
#include "wbdbg.h"

#include <gtkmm/main.h>
#include <gtkmm/stock.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/entry.h>
#include <gtkmm/accelgroup.h>
#include <gtkmm/builder.h>

// the rest, backend, etc ...
#include "workbench/wb_context.h"
#include "workbench/wb_context_ui.h"
#include "workbench/wb_module.h"

#include "grtpp_util.h" // for strfmt

#include "sqlide_main.h"

#include "base/string_utilities.h"

using base::strfmt;

#define REFRESH_TIMER 250

#include <gtkmm/messagedialog.h>

Program* Program::_instance = 0;

static void flush_main_thread()
{
  while (Gtk::Main::events_pending())
  {
    if (Gtk::Main::iteration(false))
    {
      // return value of true means quit() was called 
      Gtk::Main::quit();
      break;
    }
  }
}

//------------------------------------------------------------------------------
Program::Program(wb::WBOptions &wboptions)
{
  _instance = this;
  // Setup backend stuff
  _wb_context_ui = new wb::WBContextUI(wboptions.verbose);
  _wb_context = _wb_context_ui->get_wb();

  _grt_manager = _wb_context->get_grt_manager();

#ifdef ENBLE_DEBUG
  if ( !getenv("MWB_DATA_DIR") )
  {
    const char *path = "../share/mysql-workbench";
    g_message("MWB_DATA_DIR is unset! Setting MWB_DATA_DIR to predifined value '%s'", path);
    setenv("MWB_DATA_DIR", path, 1);
  }

  if ( !getenv("MWB_MODULE_DIR") )
  {
    const char *path = "../lib/mysql-workbench/modules";
    g_message("MWB_MODULE_DIR is unset! Setting MWB_MODULE_DIR to predifined value '%s'", path);
    setenv("MWB_MODULE_DIR", path, 1);
  }

  if ( !getenv("MWB_LIBRARY_DIR") )
  {
    const char *path = "../share/mysql-workbench/libraries";
    g_message("MWB_LIBRARY_DIR is unset! Setting MWB_LIBRARY_DIR to predifined value '%s'", path);
    setenv("MWB_LIBRARY_DIR", path, 1);
  }

  if ( !getenv("MWB_PLUGIN_DIR") )
  {
    const char *path = "../lib/mysql-workbench";
    g_message("MWB_PLUGIN_DIR is unset! Setting MWB_PLUGIN_DIR to predifined value '%s'", path);
    setenv("MWB_PLUGIN_DIR", path, 1);
  }
#endif
  if ( !getenv("MWB_DATA_DIR") || (!getenv("MWB_MODULE_DIR")))
  {
    g_print("Please start Workbench through mysql-workbench instead of calling mysql-workbench-bin directly\n");
    exit(1);
  }

  _grt_manager->set_datadir(getenv("MWB_DATA_DIR"));

  // Main form holds UI code, Glade wrapper, etc ...
  _main_form = new MainForm(_wb_context_ui);

  // Define a set of methods which backend can call to interact with user and frontend
  wb::WBFrontendCallbacks wbcallbacks;
  
  // Assign those callback methods
  wbcallbacks.show_file_dialog= sigc::mem_fun(this, &Program::show_file_dialog_becb);
  wbcallbacks.show_status_text= sigc::mem_fun(_main_form, &MainForm::show_status_text_becb);
  wbcallbacks.open_editor= sigc::mem_fun(_main_form, &MainForm::open_plugin_becb);
  wbcallbacks.show_editor= sigc::mem_fun(_main_form, &MainForm::show_plugin_becb);
  wbcallbacks.hide_editor= sigc::mem_fun(_main_form, &MainForm::hide_plugin_becb);
  wbcallbacks.perform_command= sigc::mem_fun(_main_form, &MainForm::perform_command_becb);
  wbcallbacks.create_diagram= sigc::mem_fun(_main_form, &MainForm::create_view_becb);
  wbcallbacks.destroy_view= sigc::mem_fun(_main_form, &MainForm::destroy_view_becb);
  wbcallbacks.switched_view= sigc::mem_fun(_main_form, &MainForm::switched_view_becb);
  wbcallbacks.create_main_form_view= sigc::mem_fun(_main_form, &MainForm::create_main_form_view_becb);
  wbcallbacks.destroy_main_form_view= sigc::mem_fun(_main_form, &MainForm::destroy_main_form_view_becb);
  wbcallbacks.tool_changed= sigc::mem_fun(_main_form, &MainForm::tool_changed_becb);
  wbcallbacks.refresh_gui= sigc::mem_fun(_main_form, &MainForm::refresh_gui_becb);
  wbcallbacks.lock_gui= sigc::mem_fun(_main_form, &MainForm::lock_gui_becb);
  wbcallbacks.quit_application= sigc::mem_fun(_main_form, &MainForm::quit_app_becb);

  wboptions.basedir = getenv("MWB_DATA_DIR");
  wboptions.plugin_search_path = getenv("MWB_PLUGIN_DIR");
  wboptions.struct_search_path = wboptions.basedir + "/grt";
  wboptions.module_search_path = getenv("MWB_MODULE_DIR");
  wboptions.library_search_path = getenv("MWB_LIBRARY_DIR");
  wboptions.cdbc_driver_search_path = getenv("DBC_DRIVER_PATH")?:"";
  if (wboptions.cdbc_driver_search_path.empty())
    wboptions.cdbc_driver_search_path= wboptions.library_search_path;
  wboptions.user_data_dir = std::string(g_get_home_dir()).append("/.mysql/workbench");


  _wb_context_ui->init(&wbcallbacks, &wboptions);

  _wb_context_ui->get_wb()->get_grt_manager()->get_dispatcher()->set_main_thread_flush_and_wait(flush_main_thread);

  {
    std::string form_name;
    sigc::slot<FormViewBase*, boost::shared_ptr<bec::UIForm> > form_creator;

    setup_sqlide(_wb_context_ui, form_name, form_creator);

    _main_form->register_form_view_factory(form_name, form_creator);
  }

  _main_form->setup_ui();

  // show the window only when everything is done
  _sig_finalize_initialization = Glib::signal_idle().connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Program::finalize_initialization), &wboptions), false));
//  _main_form->show();
  
  _idle_signal_conn = Glib::signal_timeout().connect(sigc::mem_fun(this, &Program::idle_stuff), REFRESH_TIMER);
}

//------------------------------------------------------------------------------
Program::~Program()
{ 
  delete _wb_context_ui;
  _wb_context = NULL;
  _grt_manager = NULL;
}


void Program::finalize_initialization(wb::WBOptions *options)
{
  _main_form->show();
  
  _wb_context_ui->init_finish(options);
}



bool Program::idle_stuff()
{
  // if there are tasks to be executed, schedule it to be done when idle so that the timer
  // doesn't get blocked during its execution
  _idleConnections.push_back(Glib::signal_idle().connect(sigc::bind_return(sigc::mem_fun(_wb_context, &wb::WBContext::flush_idle_tasks), false)));
  return true;
}


void Program::shutdown()
{
  _main_form->exiting();
  
  _wb_context_ui->finalize();
  
  //sigc::connection conn(idle_signal_conn);
  _sig_finalize_initialization.disconnect();
  _idle_signal_conn.disconnect();
  
  _wb_context->get_grt_manager()->terminate();

  _grt_manager->get_dispatcher()->shutdown();

  for (std::deque<sigc::connection>::iterator it = _idleConnections.begin(); it != _idleConnections.end(); it++)
    (*it).disconnect();

  _idleConnections.clear();

  delete _main_form;
  _main_form= 0;
}


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

int Program::confirm_action_becb(const std::string& title, const std::string& msg, const std::string& default_btn, const std::string& alt_btn, const std::string& other_btn)
{
  GtkAutoLock lock;
  Gtk::MessageDialog dlg(strfmt("<b>%s</b>\n%s", title.c_str(), msg.c_str()), true, Gtk::MESSAGE_QUESTION,
                         Gtk::BUTTONS_NONE, true);
  dlg.set_title(title);
  
  dlg.add_button(default_btn, 1);
  if (!other_btn.empty())
    dlg.add_button(other_btn, 3);
  dlg.add_button(alt_btn, 2);

  dlg.set_transient_for(*get_mainwindow());

  int response= dlg.run();
  
  switch (response)
  {
  case 1:
    return 1;
  case 2:
    return 0;
  case 3:
    return -1;
  default: // Escape
    if (default_btn == _("Cancel"))
      return 1;
    else if (alt_btn == _("Cancel"))
      return 0;
    else if (other_btn == _("Cancel"))
      return -1;
    return 0;
}
}

//------------------------------------------------------------------------------
std::string Program::show_file_dialog_becb(const std::string& type
                                          ,const std::string& title
                                          ,const std::string& extensions
                                          )
{
  Gtk::FileChooserDialog dlg(title
                             ,( type == "open" 
                                  ? Gtk::FILE_CHOOSER_ACTION_OPEN 
                                  : Gtk::FILE_CHOOSER_ACTION_SAVE
                              )
                            );

  dlg.set_transient_for(*(_main_form->get_mainwindow()));
  
  dlg.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
  if (type == "open")
    dlg.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
  else
    dlg.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);

  
  // extensions format:
  // AAA Files (*.aaa)|*.aaa,BBB Files (*.bbb)

  std::vector<std::string> exts(base::split(extensions,","));
  std::string default_ext;

  for (std::vector<std::string>::const_iterator iter= exts.begin();
       iter != exts.end(); ++iter)
  {
    Gtk::FileFilter filter;

    if (iter->find('|') != std::string::npos)
    {
      std::string descr, ext;
      
      descr= iter->substr(0, iter->find('|'));
      ext= iter->substr(iter->find('|')+1);

      if (default_ext.empty())
      {
        default_ext = ext;
        if (default_ext[0] == '*')
          default_ext = default_ext.substr(1);
        if (default_ext[0] == '.')
          default_ext = default_ext.substr(1);
      }
      filter.add_pattern(ext);
      filter.set_name(descr);
    }
    else if (*iter == "mwb")
    {
      if (default_ext.empty()) default_ext = "mwb";
      filter.add_pattern("*.mwb");
      filter.set_name("MySQL Workbench Models (*.mwb)");
    }
    else if (*iter == "sql")
    {
      if (default_ext.empty()) default_ext = "sql";
      filter.add_pattern("*.sql");
      filter.set_name("SQL Script Files");
    }
    else
    {
      if (default_ext.empty()) default_ext = *iter;
      filter.add_pattern("*."+*iter);
      filter.set_name(strfmt("%s files (*.%s)", iter->c_str(), iter->c_str()));
    }
    dlg.add_filter(filter);
  }

  Gtk::FileFilter filter;
  filter.add_pattern("*");
  filter.set_name("All Files");
  dlg.add_filter(filter);

  std::string file("");

  if (!default_ext.empty())
    default_ext = "."+default_ext;

  for (;;)
  {
    
    int result = dlg.run();

    if ( result == Gtk::RESPONSE_OK)
    {
      file = dlg.get_filename();
      if (!bec::has_suffix(file, default_ext) && type == "save")
	file = file + default_ext;

      if (type == "save" && g_file_test(file.c_str(), G_FILE_TEST_EXISTS))
      {
        if (confirm_action_becb(strfmt(_("\"%s\" Already Exists. Do you want to replace it?"), file.c_str()),
                              strfmt(_("Replacing it will overwrite its current contents.")),
                              _("Replace"), _("Cancel"), "") != 1)
          continue;
      }
    }
    break;
  }
  
  return file;
}

//------------------------------------------------------------------------------
Gtk::Window* Program::get_mainwindow() const
{
  return _main_form->get_mainwindow();
}

// get_mainwindow is declared in gtk_helpers.h
//------------------------------------------------------------------------------
void* get_mainwindow_impl()
{
  return Program::get_instance()->get_mainwindow();
}