Merge regex patches and improve the regex again
[debian/pino.git] / src / status_bar_smart.vala
1 /* status_bar_smart.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using Gtk;
23 using TimeUtils;
24
25 public class StatusbarSmart : VBox {
26         public static enum StatusType {
27                 UPDATING,
28                 SENDING_DATA,
29                 DATA_SENT,
30                 FINISH_OK,
31                 FINISH_ERROR
32         }
33         
34         private HSeparator sep;
35         private Image img;
36         private Label label;
37         
38         private bool warning = false;
39         
40         public StatusbarSmart() {
41                 sep = new HSeparator();
42                 img = new Image();
43                 label = new Label("");
44                 
45                 var event_img = new EventBox();
46                 event_img.add(img);
47                 event_img.set_events(Gdk.EventMask.BUTTON_PRESS_MASK);
48                 event_img.button_press_event.connect(log_click);
49                 
50                 var hbox = new HBox(false, 5);
51                 hbox.pack_start(event_img, false, false, 0);
52                 hbox.pack_start(label, false, false, 0);
53                 
54                 pack_start(sep, false, false, 0);
55                 pack_start(hbox, false, false, 0);
56         }
57         
58         private bool log_click(Gdk.EventButton event) {
59                 if(event.button == 1) {
60                         LogWindow log_window = new LogWindow(log);
61                         log_window.show_all();
62                 }
63                 
64                 return false;
65         }
66         
67         private void push(string icon, string text) {
68                 img.set_from_stock(icon, Gtk.IconSize.MENU);
69                 label.set_label(text);
70         }
71         
72         private void push_custom(string icon_path, string text) {
73                 Gdk.PixbufAnimation anima = new Gdk.PixbufAnimation.from_file(icon_path);
74                 img.set_from_animation(anima);
75                 label.set_label(text);
76         }
77         
78         private string log = "";
79         
80         public void log_warning(string line) {
81                 warning = true;
82                 logging(line);
83         }
84         
85         public void logging(string line) {
86                 log += "[%s] %s\n".printf(get_current_time().format("%b %d %k:%M:%S"), line);
87         }
88         
89         public void set_status(StatusType status_type, string message = "") {
90                 switch(status_type) {
91                         case StatusType.FINISH_OK:
92                                 if(warning) {
93                                         push("gtk-dialog-warning", _("updated "));
94                                         warning = false;
95                                 } else {
96                                         push("gtk-apply", _("updated "));
97                                 }
98                                 break;
99                         
100                         case StatusType.UPDATING:
101                                 push_custom(Config.PROGRESS_PATH, _("updating... "));
102                                 break;
103                         
104                         case StatusType.SENDING_DATA:
105                                 push_custom(Config.PROGRESS_PATH, message);
106                                 break;
107                         
108                         case StatusType.DATA_SENT:
109                                 push("gtk-info", message);
110                                 break;
111                         
112                         case StatusType.FINISH_ERROR:
113                                 push("gtk-dialog-warning", message);
114                                 log_warning(message);
115                                 break;
116                 }
117         }
118 }