File: MGBlobEditor.cc

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (581 lines) | stat: -rw-r--r-- 15,660 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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* Copyright (C) 2004 MySQL AB

   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; either version 2 of the License, or
   (at your option) any later version.

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "MGBlobEditor.h"
#include <gtkmm.h>

#include "myg_gtkutils.h"
#include "myg_utils.h"
#include "mygpriv.h"


class MGBlobListStore : public Glib::Object, public Gtk::TreeModel
{
  protected:
    virtual bool iter_is_valid(const Gtk::TreeModel::iterator& iter) const;
    virtual bool iter_next_vfunc(const Gtk::TreeModel::iterator& iter, Gtk::TreeModel::iterator& iter_next) const;

    virtual bool iter_children_vfunc(const Gtk::TreeModel::iterator& parent, Gtk::TreeModel::iterator& iter) const;
    virtual bool iter_has_child_vfunc(const Gtk::TreeModel::iterator& iter) const;
    virtual int iter_n_children_vfunc(const Gtk::TreeModel::iterator& iter) const;
    virtual int iter_n_root_children_vfunc() const;
    virtual bool iter_nth_child_vfunc(const Gtk::TreeModel::iterator& parent, int n, Gtk::TreeModel::iterator& iter) const;
    virtual bool iter_nth_root_child_vfunc(int n, Gtk::TreeModel::iterator& iter) const;
    virtual bool iter_parent_vfunc(const Gtk::TreeModel::iterator& child, Gtk::TreeModel::iterator& iter) const;

    virtual Gtk::TreeModel::Path get_path_vfunc(const Gtk::TreeModel::iterator& iter) const;
    virtual bool get_iter_vfunc(const Gtk::TreeModel::Path& path, Gtk::TreeModel::iterator& iter) const;

    virtual Gtk::TreeModelFlags get_flags_vfunc() const;
    virtual int get_n_columns_vfunc() const;
    virtual GType get_column_type_vfunc(int index) const;
    virtual void get_value_vfunc(const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value) const;

    guchar *_data;
    gsize _size;

    gint _bytes_per_row;
    int _stamp;

    MGBlobListStore(gint bytes_per_row);

  public:
    static Glib::RefPtr<MGBlobListStore> create(gint bpr);
    
    void set_data(char *data, gsize size);
};


Glib::RefPtr<MGBlobListStore> MGBlobListStore::create(gint bpr)
{
  MGBlobListStore *store;

  store= new MGBlobListStore(bpr);

  return Glib::RefPtr<MGBlobListStore>(store);
}


MGBlobListStore::MGBlobListStore(gint bytes_per_row)
  : Glib::ObjectBase(typeid(MGBlobListStore)),
    Glib::Object(),
   _bytes_per_row(bytes_per_row),
   _stamp(1)
{
}


void MGBlobListStore::set_data(char *data, gsize size)
{
  _data= (guchar*)data;
  _size= size;
  Gtk::TreeModel::Path p(Glib::ustring("0"));
  row_changed(p, get_iter(p));
  row_inserted(p, get_iter(p));
}


Gtk::TreeModel::Path MGBlobListStore::get_path_vfunc(const Gtk::TreeModel::iterator& iter) const
{
  Path path;
  path.push_back((long)iter.gobj()->user_data);
    
  return path;
}


bool MGBlobListStore::iter_next_vfunc(const Gtk::TreeModel::iterator& iter, Gtk::TreeModel::iterator& iter_next) const
{
  long next_row= 0;

  iter_next= Gtk::TreeModel::iterator();
  if (iter_is_valid(iter))
  {
    next_row= (long)iter.gobj()->user_data+1;
    if (next_row < iter_n_root_children_vfunc())
    {
      iter_next.set_stamp(_stamp);
      iter_next.gobj()->user_data= (gpointer)next_row;
      return true;
    }
  }
  return false;
}


bool MGBlobListStore::iter_has_child_vfunc(const Gtk::TreeModel::iterator& iter) const
{
  return false;
}


bool MGBlobListStore::iter_parent_vfunc(const Gtk::TreeModel::iterator& child, Gtk::TreeModel::iterator& iter) const
{
  iter= Gtk::TreeModel::iterator();
  return false;
}

bool MGBlobListStore::iter_is_valid(const Gtk::TreeModel::iterator& iter) const
{
  return iter.get_stamp() == _stamp;
}

bool MGBlobListStore::get_iter_vfunc(const Gtk::TreeModel::Path& path, Gtk::TreeModel::iterator& iter) const
{
  iter= Gtk::TreeModel::iterator();
  if (!path.empty())
  {
    long row_index= path[0];
    if (row_index < iter_n_root_children_vfunc())
    {
      iter.set_stamp(_stamp);
      iter.gobj()->user_data= (gpointer)row_index;
      return true;
    }
  }
  return false;
}

bool MGBlobListStore::iter_nth_root_child_vfunc(int n, Gtk::TreeModel::iterator& iter) const
{
  iter= Gtk::TreeModel::iterator();
  if (n < iter_n_root_children_vfunc())
  {
    iter.set_stamp(_stamp);
    iter.gobj()->user_data= (gpointer)(long)n;
    return true;
  }
  return false;
}


bool MGBlobListStore::iter_nth_child_vfunc(const Gtk::TreeModel::iterator& parent, int n, Gtk::TreeModel::iterator& iter) const
{
  return iter_nth_root_child_vfunc(n, iter);
}


int MGBlobListStore::iter_n_root_children_vfunc() const
{
  return _size / _bytes_per_row + (_size % _bytes_per_row > 0 ? 1 : 0);
}


int MGBlobListStore::iter_n_children_vfunc(const Gtk::TreeModel::iterator& iter) const
{
  return 0;
}

bool MGBlobListStore::iter_children_vfunc(const Gtk::TreeModel::iterator& parent, Gtk::TreeModel::iterator& iter) const
{
  return iter_nth_root_child_vfunc(0, iter);
}

Gtk::TreeModelFlags MGBlobListStore::get_flags_vfunc() const
{
  return Gtk::TreeModelFlags(0);
}

int MGBlobListStore::get_n_columns_vfunc() const
{
  return 21;
}

GType MGBlobListStore::get_column_type_vfunc(int index) const
{
  return G_TYPE_STRING;
}

void MGBlobListStore::get_value_vfunc(const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value) const
{
  char buffer[_bytes_per_row * 4];
  unsigned int line_num= (unsigned long)iter.gobj()->user_data;

  if (!iter_is_valid(iter))
  {
    value.init(G_TYPE_STRING);
    return;
  }

  Glib::Value<Glib::ustring> nvalue;
  nvalue.init(G_TYPE_STRING);
  if (column == 0)
  {
    sprintf(buffer, "%.8x", line_num * _bytes_per_row);
    nvalue.set(buffer);
  }
  else if (column >= _bytes_per_row + 1)
  {
    int i;
    buffer[0]= 0;
    for (i= 0; i < _bytes_per_row/4; i++)
    {
      guint offs= line_num * _bytes_per_row + i + (column-_bytes_per_row-1)*4;
      if (offs >= _size)
        strcat(buffer, " ");
      else if (isprint(_data[offs]))
        strncat(buffer, (char*)_data+offs, 1);
      else
      {
        switch (_data[offs])
        {
          /* no common fixed width fonts have these chars
        case 0:
          strcat(buffer, "\xE2\x90\x80");
          break;
        case '\n':
          strcat(buffer, "\xE2\x90\x8A");
          break;
        case '\r':
          strcat(buffer, "\xE2\x90\x8D");
          break;
           */
        default:
          strcat(buffer, "\xC2\xB7");
        }
      }
    }
    nvalue.set(buffer);
  }
  else
  {
    guint offs= line_num * _bytes_per_row + column - 1;
    
    if (offs >= _size)
    {
      nvalue.set("  ");
    }
    else
    {
      sprintf(buffer, "%.2x", _data[offs]);
      nvalue.set(buffer);
    }
  }
  value.init(Glib::Value<Glib::ustring>::value_type());
  value= nvalue;
}
  



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


MGBlobEditor::MGBlobEditor(MGBlobEditor::ViewType vtypes, bool edit)
  : Gtk::Window(Gtk::WINDOW_TOPLEVEL), _topbox(false, 8),
   _bbox(Gtk::BUTTONBOX_END, 8), _button1(0), _button2(0),
   _vtypes(vtypes), _data(0), _size(0)
{
  _delete_on_close= false;

  set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
  set_default_size(600, 400);
  
  signal_delete_event().connect(sigc::mem_fun(*this,&MGBlobEditor::delete_event));
  
  _note.signal_switch_page().connect(sigc::mem_fun(*this,&MGBlobEditor::note_page_switched));
    
  add(_topbox);
  _topbox.pack_start(_note);
  set_border_width(12);

  set_title(_("Field Viewer"));

  if (vtypes & (VText|VBinary))
  {
    Gtk::ScrolledWindow *swin= Gtk::manage(new Gtk::ScrolledWindow());
    _text= Gtk::manage(new Gtk::TextView());
    
    swin->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
    swin->set_shadow_type(Gtk::SHADOW_IN);

    _text->set_editable(edit);
    swin->add(*_text);

    _note.append_page(*swin, _("Text"));
  }
  else
    _text= 0;

  if (vtypes & VImage)
  {
    Gtk::VBox *vb= Gtk::manage(new Gtk::VBox(false, 8));
    Gtk::ScrolledWindow *scroll= Gtk::manage(new Gtk::ScrolledWindow());

    _image= Gtk::manage(new Gtk::Image());
    _image_label= Gtk::manage(new Gtk::Label());

    scroll->add(*_image);
    
    vb->pack_start(*scroll, true, true);
    vb->pack_start(*_image_label, false, true);

    _note.append_page(*vb, _("Image"));
  }

  if (vtypes & VBinary)
  {
    Gtk::ScrolledWindow *swin= Gtk::manage(new Gtk::ScrolledWindow());

    swin->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
    swin->set_shadow_type(Gtk::SHADOW_IN);

    
    _tree_store= MGBlobListStore::create(16);

    _tree= Gtk::manage(new Gtk::TreeView());
    swin->add(*_tree);

    _tree->set_headers_visible(false);
    _tree->append_column("", _columns.offset);
    ((Gtk::CellRendererText*)_tree->get_column(0)->get_first_cell_renderer())->set_fixed_height_from_font(1);
    _tree->get_column(0)->set_fixed_width(100);
    
    // separator
    {
      Gtk::TreeViewColumn *col= Gtk::manage(new Gtk::TreeViewColumn(""));
      Gtk::CellRendererText *rend= Gtk::manage(new Gtk::CellRendererText());
      rend->set_fixed_height_from_font(1);
      rend->property_background()= "#000000";
      col->pack_start(*rend, false);
      col->set_fixed_width(1);
      col->set_max_width(1);
      _tree->append_column(*col);
    }

    // hex view
    for (int i= 0; i < 4; i++)
    {
      Gtk::TreeViewColumn *col= Gtk::manage(new Gtk::TreeViewColumn(""));

      for (int j= 0; j < 4; j++)
      {
        Gtk::CellRendererText *rend= Gtk::manage(new Gtk::CellRendererText());
        rend->set_fixed_height_from_font(1);
        if (!(i&1))
          rend->property_background()= "#eeeef0";
        col->pack_start(*rend, false);
        col->add_attribute(rend->property_text(), _columns.data[i*4+j]);
      }
      _tree->append_column(*col);
    }

    // separator
    {
      Gtk::TreeViewColumn *col= Gtk::manage(new Gtk::TreeViewColumn(""));
      Gtk::CellRendererText *rend= Gtk::manage(new Gtk::CellRendererText());
      rend->set_fixed_height_from_font(1);
      rend->property_background()= "#000000";
      col->pack_start(*rend, false);
      col->set_fixed_width(1);
      col->set_max_width(1);
      _tree->append_column(*col);
    }

    // ascii view
    Gtk::TreeViewColumn *col= Gtk::manage(new Gtk::TreeViewColumn(""));
    for (int i= 0; i < 4; i++)
    {
      Gtk::CellRendererText *rend= Gtk::manage(new Gtk::CellRendererText());
      col->pack_start(*rend, false);
      rend->set_fixed_height_from_font(1);
      rend->property_family()= "lucidatypewriter";
      if (!(i&1))
        rend->property_background()= "#eeeef0";
      col->add_attribute(rend->property_text(), _columns.text[i]);
    }
    _tree->append_column(*col);
    
    _note.append_page(*swin, _("Binary"));
  }

  _summary.set_alignment(0.0);
  _topbox.pack_start(_summary, false, true);

  _topbox.pack_start(_bbox, false, true);


  if (edit)
  {
    _button2= Gtk::manage(new Gtk::Button(Gtk::Stock::CANCEL));
    _button2->signal_clicked().connect(sigc::mem_fun(*this,&MGBlobEditor::close));
    _bbox.pack_start(*_button2, false, false);
  }
  
  _button1= Gtk::manage(new Gtk::Button(Gtk::Stock::OK));
  if (edit)
    _button1->signal_clicked().connect(sigc::mem_fun(*this,&MGBlobEditor::save));
  else
    _button1->signal_clicked().connect(sigc::mem_fun(*this,&MGBlobEditor::close));
  _bbox.pack_start(*_button1, false, false);

  show_all_children();
}

void MGBlobEditor::note_page_switched(GtkNotebookPage* page, guint)
{
  if((_text != NULL) && (_note.get_current()->get_tab_label_text().compare("Binary") == 0))
  {
    char* c= g_strdup(_text->get_buffer()->get_text().c_str());
    _tree->set_model(Glib::RefPtr<Gtk::ListStore>());
    _tree_store->set_data(c, strlen(c));
    _tree->set_model(_tree_store);
  }
}

MGBlobEditor::~MGBlobEditor()
{
}


void MGBlobEditor::save()
{
  _save_signal.emit();
  
  close();
}

void MGBlobEditor::close()
{
  hide();
  if (_delete_on_close)
    delete this;
}


bool MGBlobEditor::delete_event(GdkEventAny *ev)
{
  close();
  return true;
}


void MGBlobEditor::set_delete_on_close(bool flag)
{
  _delete_on_close= flag;
}


void MGBlobEditor::get_data(gpointer &data, gsize &size)
{
  if (_vtypes & VText)
  {
    Glib::ustring text= _text->get_buffer()->get_text();

    size= text.bytes();
    data= g_strdup(text.c_str());
  }
  else
  {
    size= _size;
    data= g_memdup(_data, _size);
  }
}


static bool can_show_as_text(const char *data, gsize size)
{
  const gchar *end;
  
  if (g_utf8_validate(data, size, &end))
    return true;
  return false;
  /*
    gsize i;
  for (i= 0; i < size; i++)
  {
    if ((unsigned char)data[i] > 127 || data[i]==0)
      return false;
  }
  return true;
   */
}


void MGBlobEditor::set_data(gpointer data, gsize size)
{
  _data= data;
  _size= size;

  _summary.set_label(ufmt(_("Total Length: %i bytes"), size));

  if (_vtypes & VImage)
  {
    try
    {
      std::string fname="/tmp/qXXXXXX";
      int fd= Glib::mkstemp(fname);
      if (fd < 0)
      {
        g_message("could not create temporary data file for displaying image");
      }
      else
      {
        const char *format;
        
        ::write(fd, _data, _size);
        ::close(fd);
        
        Glib::RefPtr<Gdk::Pixbuf> pixbuf= Gdk::Pixbuf::create_from_file(fname);
        
        _image->set(pixbuf);
        
      switch (myx_guess_image_format((char*)_data, _size))
        {
        case MYX_IMG_JPEG:
          format= "JPEG";
          break;
        case MYX_IMG_PNG:
          format= "PNG";
          break;
        case MYX_IMG_BMP:
          format= "BMP";
          break;
        case MYX_IMG_GIF:
          format= "GIF";
          break;
        default:
          format= "Unknown";
        }
        _image_label->set_label(ufmt(_("Image size: %i x %i\nFormat: %s"), pixbuf->get_width(), pixbuf->get_height(),
                                     format));
      }
    }
    catch (Glib::Error &exc)
    {
      _image_label->set_label(_("Cannot display as image data."));
    }
  }

  if (_vtypes & VText)
  {
    // must convert 1st to a normal string because _length is in bytes, not chars
    _text->get_buffer()->set_text(Glib::ustring(std::string((char*)_data?:"", 0, _size)));
  }

  if (_vtypes & VBinary)
  {
    if (can_show_as_text((const char*)_data, _size))
      _text->get_buffer()->set_text(Glib::ustring(std::string((char*)_data?:"", 0, _size)));
    else
    {
      _text->get_buffer()->set_text(_("Cannot display binary data as text."));
      _text->set_sensitive(false);
    }
    _tree_store->set_data((char*)_data, size);
    _tree->set_model(_tree_store);
  }
}