summaryrefslogtreecommitdiff
path: root/src/gtk/errortab.cc
blob: 75830c11171ca4283410e29f51c454cf3093334a (plain)
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
// errortab.cc
//
// Copyright 2008 Daniel Burrows
//
//  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; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#include "errortab.h"

#include <generic/apt/apt.h>

#include <apt-pkg/error.h>

#include <generic/util/util.h>

#include "gui.h"

namespace gui
{
  ErrorStore::ErrorStore()
  {
    Gtk::TreeModelColumnRecord columns;
    columns.add(icon_column);
    columns.add(datetime_column);
    columns.add(text_column);
    store = Gtk::ListStore::create(columns);

    consume_apt_errors();

    consume_errors.connect(sigc::mem_fun(*this, &ErrorStore::consume_apt_errors));
    Glib::signal_timeout().connect(sigc::bind_return(sigc::mem_fun(*this, &ErrorStore::consume_apt_errors),
						     true),
				   5000);
  }

  void ErrorStore::consume_apt_errors()
  {
    bool new_error = false;

    while(!_error->empty())
      {
	std::string msg;
	bool is_error = _error->PopMessage(msg);
	new_error = new_error && is_error;

	Glib::ustring icon = is_error ? Gtk::Stock::DIALOG_ERROR.id
	                              : Gtk::Stock::DIALOG_WARNING.id;

	time_t curtime = time(NULL);
	tm ltime;
	std::string now;
	if(localtime_r(&curtime, &ltime) != NULL)
	  now = sstrftime("%c", &ltime);

	Gtk::TreeModel::iterator it = store->append();
	Gtk::TreeModel::Row row = *it;

	row[icon_column] = icon;
	row[datetime_column] = now;
	row[text_column] = msg;
      }

    if(new_error)
      error_added();
  }

  ErrorTab::ErrorTab(Glib::ustring label,
		     const ErrorStore &store)
    : Tab(Error, label,
	  Gnome::Glade::Xml::create(glade_main_file, "errors_scrolledwindow"),
	  "errors_scrolledwindow")
  {
    get_widget()->set_manage();

    Gtk::TreeView *errors_treeview = NULL;
    get_xml()->get_widget("errors_treeview", errors_treeview);

    {
      Gtk::CellRendererPixbuf *icon_renderer = manage(new Gtk::CellRendererPixbuf);
      Gtk::TreeViewColumn *icon_column = manage(new Gtk::TreeViewColumn("", *icon_renderer));
      icon_column->add_attribute(icon_renderer->property_stock_id(),
				 store.get_icon_column());
      errors_treeview->append_column(*icon_column);
    }

    errors_treeview->append_column("Time", store.get_datetime_column());

    {
      Gtk::CellRendererText *text_renderer = manage(new Gtk::CellRendererText);
      text_renderer->property_ellipsize() = Pango::ELLIPSIZE_END;

      Gtk::TreeViewColumn *error_column = manage(new Gtk::TreeViewColumn("Error", *text_renderer));
      error_column->add_attribute(text_renderer->property_text(), store.get_text_column());
      errors_treeview->append_column(*error_column);
    }
    errors_treeview->set_tooltip_column(store.get_text_column().index());

    errors_treeview->set_model(store.get_model());

    get_widget()->show();
  }

  void ErrorTab::show()
  {
    get_widget()->show_all();
  }
}