File: wb_context.h

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 (538 lines) | stat: -rw-r--r-- 18,005 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* 
 * Copyright (c) 2007, 2015, 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
 */

#pragma once

#ifndef _WIN32
#include <vector>
#endif

#include "base/ui_form.h"
#include "grt/grt_manager.h"
#include "base/notifications.h"

#include "wb_context_names.h"

#include "grts/structs.workbench.h"
#include "wb_backend_public_interface.h"

#include <grtpp_undo_manager.h>

#include "mforms/utilities.h"
#include "base/trackable.h"
#include "base/threading.h"


#include "wb_version.h"

#define WBContext_VERSION 5

#define WB_DBOBJECT_DRAG_TYPE "mysql-wb.DatabaseObject"
#define WB_CONTROL_DRAG_TYPE "mysql-wb.control"

namespace mdc {
  class CanvasView;
  class CanvasItem;
};

namespace bec {
  class Clipboard;
  class IconManager;
};

class SqlEditorForm;

namespace wb {
  class WBContextUI;
  class WBContextModel;
  class WBContextSQLIDE;
  class WorkbenchImpl;
  class WBComponent;

  class ModelFile;
  
  class TunnelManager;

  enum RefreshType 
  {
    RefreshNeeded, // Front end should schedule a refresh flush asap (usually called in worker thread).
    RefreshNothing,
    RefreshSchemaNoReload,
    RefreshNewDiagram,
    RefreshSelection,
    RefreshCloseEditor, // argument: object-id (close all if "")
    
    RefreshNewModel,

    RefreshOverviewNodeInfo,    // argument: node-id, overview ptr (bec::UIForm*)
    RefreshOverviewNodeChildren,// argument: node-id, overview ptr 

    RefreshDocument,
    RefreshCloseDocument,
    RefreshZoom,
    RefreshTimer,

    RefreshFinishEdits // Force all ongoing edit operations (eg in TreeView cells) to be committed
  };

  struct MYSQLWBBACKEND_PUBLIC_FUNC WBPaperSize
  {
    std::string name;
    std::string caption;
    double width;
    double height;
    bool margins_set;
    double margin_top;
    double margin_bottom;
    double margin_left;
    double margin_right;
    std::string description;
  };

  // basic toolbar names
  #define WB_TOOLBAR_OPTIONS "options"

  // main view types
  #define WB_MAIN_VIEW_DB_QUERY "dbquery"
  

  class ModelDiagramForm;
  class FindDialogBE;

  enum PageOrientation
  {
    Landscape,
    Portrait
  };

  
  enum RequestInputFlag
  {
    InputPassword = (1<<0)
  };
  
  
  struct MYSQLWBBACKEND_PUBLIC_FUNC WBFrontendCallbacks
  {
    // Args: type, title, file extensions
    boost::function<std::string (std::string,std::string,std::string)> show_file_dialog;

    // Show some text in the application's status bar: must be thread-safe
    boost::function<void (std::string)> show_status_text;

    // Open an editor
    // Args: grtmanager, module containing plugin, editor dll, editor class, edited object
    boost::function<NativeHandle (bec::GRTManager*, grt::Module*, std::string, std::string, grt::BaseListRef, bec::GUIPluginFlags)> open_editor;
    // Show/Hide an editor
    // Args: editor handle (e.g: window handle)
    boost::function<void (NativeHandle)> show_editor;
    boost::function<void (NativeHandle)> hide_editor;

    // Execute a built-in command
    boost::function<void (std::string)> perform_command;

    // Create a new diagram.
    boost::function<mdc::CanvasView* (const model_DiagramRef&)> create_diagram;
    // Destroy a previously created canvas view
    boost::function<void (mdc::CanvasView*)> destroy_view;
    // Signals the current view has been changed
    boost::function<void (mdc::CanvasView*)> switched_view;

    // Open the named type of main view tab with the given form object. ownership is passed to frontend
    // Args: type (eg query), bec::UIForm*
    boost::function<void (std::string, boost::shared_ptr<bec::UIForm>) > create_main_form_view;
    boost::function<void (bec::UIForm*)> destroy_main_form_view;
    
    // The tool for the view has been changed
    boost::function<void (mdc::CanvasView*)> tool_changed;

    // Refresh interface
    boost::function<void (RefreshType, std::string, NativeHandle)> refresh_gui;
    boost::function<void (bool)> lock_gui;

    // Closes the application
    boost::function<bool ()> quit_application;
  };


  struct MYSQLWBBACKEND_PUBLIC_FUNC WBOptions
  {
    std::string basedir;
    std::string plugin_search_path;
    std::string struct_search_path;
    std::string module_search_path;
    std::string library_search_path;
    std::string cdbc_driver_search_path;
    std::string user_data_dir;
    std::string open_at_startup_type; // model, query, admin, script
    std::string open_at_startup;
    std::string open_connection;
    std::string run_at_startup; // script to be executed when started
    std::string run_language; // language of the script in run_at_startup
    bool force_sw_rendering;
    bool force_opengl_rendering;
    bool verbose;
    bool quit_when_done;
    bool testing;     // True if we are currently running unit tests.
    bool init_python; // True by default. Can be switched off for testing.
    bool full_init; // True by default. Should be switched off when the options are created for an already running instance of WB.

    WBOptions();
    bool parse_args(char **argv, int argc, int *retval = NULL);
  };

  
#define FOREACH_COMPONENT(list, iter) for (std::vector<WBComponent*>::iterator iter= list.begin(); iter != list.end(); ++iter)

  class MYSQLWBBACKEND_PUBLIC_FUNC WBContext : public base::trackable, base::Observer
  {
    friend class WorkbenchImpl;
    friend class WBComponent;
    friend class WBContextUI;

   public:
    WBContext(WBContextUI *ui, bool verbose = false);
    virtual ~WBContext();

    bool software_rendering_enforced();
    bool opengl_rendering_enforced();

    bool init_(WBFrontendCallbacks *callbacks, WBOptions *options);
    void init_finish_(WBOptions *options);
    void finalize();
    
    bool is_commercial();
    WBContextUI *get_ui() { return _uicontext; }

    bec::UIForm *get_active_form();
    bec::UIForm *get_active_main_form();

    WBContextModel *get_model_context() { return _model_context; }
    WBContextSQLIDE *get_sqlide_context() { return _sqlide_context; }
    
    // Document handling.
    void new_document();
    bool can_close_document(); // returns false for cancelled
    bool close_document(); 
    
    void close_document_finish();
    void new_model_finish();
    
    // save document

    bool save_as(const std::string &path);

    std::string get_filename() const;

    void report_bug(const std::string &errorInfo);

    // plugins
    void execute_plugin(const std::string &plugin_name, 
                        const bec::ArgumentPool &argpool= bec::ArgumentPool());

    void update_plugin_arguments_pool(bec::ArgumentPool &args);

    // DB Querying
    boost::shared_ptr<SqlEditorForm> add_new_query_window(const db_mgmt_ConnectionRef &target, bool restore_session = true);
    boost::shared_ptr<SqlEditorForm> add_new_query_window();
    
    // Admin
    void add_new_admin_window(const db_mgmt_ConnectionRef &target);

    // Generic plugin tabs
    void add_new_plugin_window(const std::string &plugin_id, const std::string &caption);

    // GUI Plugin
    void register_builtin_plugins(grt::ListRef<app_Plugin> plugins);

    void close_gui_plugin(NativeHandle handle);


    //
    void request_refresh(RefreshType type, const std::string &str, NativeHandle ptr= (NativeHandle)0);

    const std::string &get_user_datadir() const { return _user_datadir; }
    // TODO: Temporary solution need to make ModelFile grt class
    workbench_DocumentRef openModelFile(const std::string &file);
    std::string getTempDir();
    int closeModelFile();
    std::string getDbFilePath();

    bool open_document(const std::string &file);
    void open_script_file(const std::string &file);
    void open_recent_document(int index);
    bool has_unsaved_changes();
    bool save_changes();
    
    bool open_file_by_extension(const std::string &path, bool interactive);

    bec::PluginManager *get_plugin_manager() { return _plugin_manager; }
    template<class C> C *get_component()
    {
      return dynamic_cast<C*>(get_component_named(C::name()));
    }

    WBComponent *get_component_named(const std::string &name);

    WBComponent *get_component_handling(const model_ObjectRef &object);

    void foreach_component(const boost::function<void (WBComponent*)> &slot);

    bec::GRTManager *get_grt_manager() const { return _manager; }
    grt::GRT *get_grt() const { return _manager->get_grt(); }
    boost::shared_ptr<WorkbenchImpl> get_workbench() { return _workbench; };

    bec::Clipboard *get_clipboard() const { return _clipboard; }

    workbench_WorkbenchRef get_root();
    workbench_DocumentRef get_document();
    grt::DictRef get_wb_options();

    std::string get_datadir() const { return _datadir; }
    
    bool cancel_idle_tasks();
    void flush_idle_tasks();

 // utilities for error reporting
    void show_exception(const std::string &operation, const std::exception &exc);
    void show_exception(const std::string &operation, const grt::grt_runtime_error &exc);

        
   template<class R>
    R execute_in_main_thread(const std::string &name, 
                             const boost::function<R ()> &function) THROW (grt::grt_runtime_error)
    {
      return _manager->get_dispatcher()->call_from_main_thread/*<R>*/(function, true, false);
    }
    void execute_in_main_thread(const std::string &name, 
                              const boost::function<void ()> &function, bool wait) THROW (grt::grt_runtime_error);
 
    grt::ValueRef execute_in_grt_thread(const std::string &name, 
                                            const boost::function<grt::ValueRef (grt::GRT*)> &function) THROW (grt::grt_runtime_error);

    void execute_async_in_grt_thread(const std::string &name, 
                                     const boost::function<grt::ValueRef (grt::GRT*)> &function) THROW (grt::grt_runtime_error);

    bool activate_live_object(const GrtObjectRef &object);

    std::string create_attached_file(const std::string &group, const std::string &tmpl);
    void save_attached_file_contents(const std::string &name, const char *data, size_t size);
    std::string get_attached_file_contents(const std::string &name);
    std::string get_attached_file_tmp_path(const std::string &name);
    void delete_attached_file(const std::string &name);
    std::string recreate_attached_file(const std::string &name, const std::string &data);
    int export_attached_file_contents(const std::string &name, const std::string &export_to);

    void block_user_interaction(bool flag);
    bool user_interaction_allowed() { return _user_interaction_blocked == 0; }
    
    // State handling.
    std::string read_state(const std::string &name, const std::string &domain, const std::string &default_value);
    int read_state(const std::string &name, const std::string &domain, const int &default_value);
    double read_state(const std::string &name, const std::string &domain, const double &default_value);
    bool read_state(const std::string &name, const std::string &domain, const bool &default_value);
     
    void save_state(const std::string &name, const std::string &domain, const std::string &value);
    void save_state(const std::string &name, const std::string &domain, const int &value);
    void save_state(const std::string &name, const std::string &domain, const double &value);
    void save_state(const std::string &name, const std::string &domain, const bool &value);

  protected:
    friend class WBContextModel; // to access _components
    
    bec::GRTManager *_manager;
    bec::PluginManager *_plugin_manager;

    workbench_WorkbenchRef _wb_root;

    int _user_interaction_blocked;
    bool _send_messages_to_shell;
    bool _asked_for_saving;
    bool _initialization_finished;
    bool _attachments_changed;
    
    std::string _datadir;
    std::string _user_datadir;

    struct RefreshRequest
    {
      RefreshType type;
      std::string str;
      NativeHandle ptr;
      double timestamp;
    };

    // Predicate for pending refresh removal on close.
    struct CancelRefreshCandidate
    {
      bool operator() (RefreshRequest request)
      {
        return (request.type == RefreshNewModel ||
          request.type == RefreshNewDiagram ||
          request.type == RefreshOverviewNodeChildren ||
          request.type == RefreshZoom ||
          request.type == RefreshDocument ||
          request.type == RefreshOverviewNodeInfo);
      }
    };

    std::list<RefreshRequest> _pending_refreshes;
    base::Mutex _pending_refresh_mutex;

    base::RecMutex _block_user_interaction_mutex;
    
    WBContextUI *_uicontext;
    WBContextModel *_model_context;
    WBContextSQLIDE *_sqlide_context;
    
    std::vector<WBComponent*> _components;

    boost::shared_ptr<WorkbenchImpl> _workbench;

    bec::Clipboard *_clipboard;

    ModelFile *_file;
    std::string _filename;
    // only used for comparing pointers
    grt::UndoAction *_save_point;
    
    TunnelManager *_tunnel_manager;

    ModelFile* _model_import_file;
    
    bool _force_sw_rendering;     // Command line switch.
    bool _force_opengl_rendering; // Command line switch.

    grt::ListRef<app_PaperType> get_paper_types(grt::GRT *grt, boost::shared_ptr<grt::internal::Unserializer> unserializer);

    bool _other_connections_loaded;
    // setup
    void init_grt_tree(grt::GRT *grt, WBOptions *options, boost::shared_ptr<grt::internal::Unserializer> unserializer);
    void run_init_scripts_grt(grt::GRT *grt, WBOptions *options);
    void init_plugins_grt(grt::GRT *grt, WBOptions *options);
    void init_plugin_groups_grt(grt::GRT *grt, WBOptions *options);
    void init_object_listeners_grt(grt::GRT *grt);
    void init_properties_grt(workbench_DocumentRef &doc);
    void init_rdbms_modules(grt::GRT *grt);

    void do_close_document(bool destroying);

    grt::ValueRef setup_context_grt(grt::GRT *grt, WBOptions *options);

    void set_default_options(grt::DictRef options);

    void load_app_options(bool update);
    
    bool auto_save_document();
    std::string get_auto_save_dir();

    void cleanup_options();
  public:
    void save_app_options();
    void save_connections();
    void save_instances();

  protected:
    void add_recent_file(const std::string &file);

    void load_app_state(boost::shared_ptr<grt::internal::Unserializer> unserializer);
    void save_app_state();
    void load_starters(boost::shared_ptr<grt::internal::Unserializer> unserializer);
    void save_starters();

    grt::ValueRef save_grt(grt::GRT *grt);
    
    grt::ValueRef execute_plugin_grt(grt::GRT *grt, const app_PluginRef &plugin, const grt::BaseListRef &args);
    void plugin_finished(const grt::ValueRef &result, const app_PluginRef &plugin);

    bool handle_message(const grt::Message &msg);
    void handle_grt_message(bec::MessageListStorage::MessageEntryRef message);

    void reset_document();
    void reset_listeners();

    void option_dict_changed(grt::internal::OwnedDict*dict=0, bool added=false, const std::string& key="");

  private:
    // for base::Observer
    virtual void handle_notification(const std::string &name, void *sender, std::map<std::string, std::string> &info);

  public:
    ModelFile *get_file() { return _file; }

    bool install_module_file(const std::string &path);
    bool uninstall_module(grt::Module *module);
    void run_script_file(const std::string &path);
    
  private:
    bool find_connection_password(const db_mgmt_ConnectionRef &conn, std::string &password);

    void *do_request_password(const std::string &title, const std::string &service, bool reset_password,
      std::string *account, std::string *ret_password);
    void *do_find_connection_password(const std::string &hostId, const std::string &username, std::string *ret_password);

    void load_other_connections();

    void attempt_options_upgrade(xmlDocPtr xmldoc, const std::string &version);

    bool show_error(const std::string& title, const std::string& message);

  public:
    std::string request_connection_password(const db_mgmt_ConnectionRef &conn, bool force_asking);

  public: // front end callbacks
    boost::function<std::string (std::string,std::string,std::string)> show_file_dialog;
    boost::function<void (std::string)> show_status_text;
    boost::function<void (std::string,void*)> show_gui_plugin;

    boost::function<mdc::CanvasView* (const model_DiagramRef&)> create_diagram;
    boost::function<void (mdc::CanvasView*)> destroy_view;
    boost::function<void (mdc::CanvasView*)> switched_view;

    boost::function<void (std::string, boost::shared_ptr<bec::UIForm> )> create_main_form_view;
    boost::function<void (bec::UIForm*)> destroy_main_form_view;

    boost::function<void (mdc::CanvasView*)> tool_changed;

    boost::function<void (RefreshType, std::string, NativeHandle)> refresh_gui;
    boost::function<void (bool)> lock_gui;

    boost::function<void (std::string)> perform_command;
    
    boost::function<bool ()> quit_application;
  };

  struct GUILock
  {
    WBContext *_wb;
    
    GUILock(WBContext *wb, const std::string& message_title, const std::string& message)
      : _wb(wb)
    {
      mforms::Utilities::show_wait_message(message_title, message);
      _wb->block_user_interaction(true);
    }
    ~GUILock()
    {
      _wb->block_user_interaction(false);
      mforms::Utilities::hide_wait_message();
    }
  };
  
};