File: grt_message_list.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 (281 lines) | stat: -rw-r--r-- 7,828 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
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
/* 
 * 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 "grt_message_list.h"
#include "grt_manager.h"
#include "validation_manager.h"
#include "base/string_utilities.h"

using namespace bec;

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

MessageListStorage::MessageListStorage(GRTManager *grtm)
: _grtm(grtm)
{
  _error_icon= IconManager::get_instance()->get_icon_id("mini_error.png");
  _warning_icon= IconManager::get_instance()->get_icon_id("mini_warning.png");
  _info_icon= IconManager::get_instance()->get_icon_id("mini_notice.png");
  
  scoped_connect(ValidationManager::signal_notify(),boost::bind(&MessageListStorage::validation_notify, this, _1, _2, _3, _4));
}

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

MessageListBE *MessageListStorage::create_list(const std::string &filter_to_source)
{
  MessageListBE *list= new MessageListBE(this);
  
  return list;
}

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

void MessageListStorage::clear_all()
{
  _entries.clear();
}

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

void MessageListStorage::handle_message(const grt::Message &msg)
{
  if (msg.type == grt::OutputMsg)
  {
    if (_output_handler)
      _grtm->run_once_when_idle(boost::bind(_output_handler,msg.text));
    return;
  }
  
  MessageEntryRef entry(new MessageEntry());
  
  switch (msg.type)
  {
    case grt::ErrorMsg:
      entry->icon= _error_icon; 
      break;
    case grt::WarningMsg:
      entry->icon= _warning_icon;
      break;
    case grt::InfoMsg: 
      entry->icon= _info_icon;
      break;
    case grt::ControlMsg:
      entry->icon = -1; // hack
      break;
    case grt::ProgressMsg:
      // ignore
      return;

    default: entry->icon= 0; break;
  }
  entry->type = msg.type;
  entry->timestamp= msg.timestamp;
  entry->message= msg.text;
  std::string::size_type end= entry->message.size();
  while (end > 0 && entry->message[end-1] == '\n') --end;
  entry->message= entry->message.substr(0, end);
  entry->detail= msg.detail;
  if (entry->icon >= 0)
    _entries.push_back(entry);
  _new_message(entry);
}

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

void MessageListStorage::set_output_handler(const boost::function<void(std::string)> &handler)
{
  _output_handler = handler;
}

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

void MessageListStorage::validation_notify(const grt::Validator::Tag&, const grt::ObjectRef& o, const std::string& m, const int level)
{
  if (level != grt::NoErrorMsg)
  {
    grt::Message msg;
    
    msg.type = (grt::MessageType)level;
    msg.timestamp = time(NULL);
    msg.text = m;
    msg.progress = 0.0;
    
    handle_message(msg);
  }
}

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

MessageListBE::MessageListBE(MessageListStorage *owner)
: _owner(owner)
{
  _notified= false;

  _conn = _owner->signal_new_message()->connect(boost::bind(&MessageListBE::add_message, this, _1));
}

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

void MessageListBE::add_message(MessageListStorage::MessageEntryRef message)
{
  if (message->icon == -1)
    return; // Ignore control messages.

  if (!_owner->_grtm->in_main_thread())
  {
    _owner->_grtm->run_once_when_idle(boost::bind(&MessageListBE::add_message, this, message));
    return;
  }
  if (_wanted_sources.empty() || _wanted_sources.find(message->source) != _wanted_sources.end())
  {
    _entries.push_back(message);
    (*signal_row_added())();
  }
}

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

void MessageListBE::add_source(const std::string &source)
{
  _wanted_sources.insert(source);
}

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

void MessageListBE::remove_source(const std::string &source)
{
  _wanted_sources.erase(source);
}

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

void MessageListBE::clear()
{
  _entries.clear();
}

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

size_t MessageListBE::count_children(const NodeId &node)
{
  if (node.depth() == 0)
    return (int)_entries.size();
  return 0;
}

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

bool MessageListBE::get_field(const NodeId &node, ColumnId column, std::string &value)
{
  switch ((Column)column)
  {
  case Time:
    if (node[0] < _entries.size())
    {
      char buffer[100];
      const struct tm *tm= localtime(&_entries[node[0]]->timestamp);
      
      strftime(buffer, sizeof(buffer), "%H:%M:%S", tm);
      value= buffer;
      return true;
    }
    break;

  case Message:
    if (node[0] < _entries.size())
    {
      value= _entries[node[0]]->message;
      return true;
    }
    break;
  case Detail:
    if (node[0] < _entries.size())
    {
      value= _entries[node[0]]->detail;
      return true;
    }
  }
  return false;
}

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

IconId MessageListBE::get_field_icon(const NodeId &node, ColumnId column, IconSize size)
{
  if (node[0] < _entries.size())
    return _entries[node[0]]->icon;

  return 0;
}

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

/**
 *  Returns the type of the message in the list. Since only error, info and warning messages
 *  are handled here only these 3 types are returned.
 */
grt::MessageType bec::MessageListBE::get_message_type( const NodeId &node )
{
  if (node[0] < _entries.size())
    return _entries[node[0]]->type;

  return grt::InfoMsg;
}

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

size_t MessageListBE::count()
{
  _notified= false;
  return _entries.size();
}

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

MenuItemList MessageListBE::get_popup_items_for_nodes(const std::vector<NodeId> &nodes)
{
  MenuItemList menu;
  MenuItem item;

  item.type= bec::MenuAction;
  item.name= "clear_messages";
  item.caption= _("Clear");
  item.enabled= true;
  menu.push_back(item);

  return menu;
}

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

bool MessageListBE::activate_popup_item_for_nodes(const std::string &name, const std::vector<NodeId> &nodes)
{
  if (name == "clear_messages")
  {
    clear();
    do_ui_refresh();
  }
  else
    return false;
  
  return true;
}

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