| 1 | # Copyright (C) 2007 Insecure.Com LLC. |
|---|
| 2 | # |
|---|
| 3 | # Authors: Guilherme Polo <ggpolo@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 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 General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 18 | # USA |
|---|
| 19 | |
|---|
| 20 | """ |
|---|
| 21 | Timeline graph toolbar. |
|---|
| 22 | """ |
|---|
| 23 | |
|---|
| 24 | import gtk |
|---|
| 25 | import gobject |
|---|
| 26 | |
|---|
| 27 | from umitCore.I18N import _ |
|---|
| 28 | from umitCore.Logging import log |
|---|
| 29 | |
|---|
| 30 | from umitInventory.TLBase import view_mode |
|---|
| 31 | from umitInventory.TLBase import view_kind |
|---|
| 32 | from umitInventory.TLBase import view_mode_order |
|---|
| 33 | from umitInventory.TLBase import view_kind_order |
|---|
| 34 | from umitInventory.TLGraphPreferences import GraphPreferences |
|---|
| 35 | from umitInventory.RefreshDialog import custom_refresh |
|---|
| 36 | |
|---|
| 37 | REFRESH = [60, 3600, -1] |
|---|
| 38 | |
|---|
| 39 | class GraphControllerTB(gtk.Toolbar): |
|---|
| 40 | """ |
|---|
| 41 | Builds a Toolbar for controlling Interactive Timeline Graph. |
|---|
| 42 | """ |
|---|
| 43 | |
|---|
| 44 | def __init__(self, daddy, connector, graph_mode=None, graph_kind=None): |
|---|
| 45 | gtk.Toolbar.__init__(self) |
|---|
| 46 | |
|---|
| 47 | self.daddy = daddy |
|---|
| 48 | self.connector = connector |
|---|
| 49 | self.refresher = -1 |
|---|
| 50 | self.timeout = -1 |
|---|
| 51 | self.tooltips = gtk.Tooltips() |
|---|
| 52 | |
|---|
| 53 | index = (i for i in xrange(7)) |
|---|
| 54 | |
|---|
| 55 | self.insert(self.viewing_mode(graph_mode), index.next()) |
|---|
| 56 | self.insert(self.viewing_kind(graph_kind), index.next()) |
|---|
| 57 | self.insert(self.graph_kind(), index.next()) |
|---|
| 58 | self.insert(self.visibility(), index.next()) |
|---|
| 59 | self.insert(self.graph_pref(), index.next()) |
|---|
| 60 | self.insert(gtk.SeparatorToolItem(), index.next()) |
|---|
| 61 | self.insert(self.graph_refresh(), index.next()) |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | def packed(self, *widgets): |
|---|
| 65 | """ |
|---|
| 66 | Pack widgets in a gtk.HBox and returns a gtk.ToolItem |
|---|
| 67 | """ |
|---|
| 68 | tbox = gtk.HBox() |
|---|
| 69 | for w in widgets: |
|---|
| 70 | tbox.pack_start(w, False, False, 0) |
|---|
| 71 | |
|---|
| 72 | titem = gtk.ToolItem() |
|---|
| 73 | titem.add(tbox) |
|---|
| 74 | |
|---|
| 75 | return titem |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | def viewing_mode(self, omode=None): |
|---|
| 79 | """ |
|---|
| 80 | Setup combobox for managing viewing modes. |
|---|
| 81 | """ |
|---|
| 82 | self.viewmode = gtk.combo_box_new_text() |
|---|
| 83 | |
|---|
| 84 | for mode in view_mode_order: |
|---|
| 85 | self.viewmode.append_text(view_mode[mode]) |
|---|
| 86 | |
|---|
| 87 | if omode: |
|---|
| 88 | option = view_mode_order.index(omode) |
|---|
| 89 | else: |
|---|
| 90 | option = 0 |
|---|
| 91 | |
|---|
| 92 | self.viewmode.set_active(option) |
|---|
| 93 | |
|---|
| 94 | self.viewmode.connect('changed', self._change_graph_viewmode) |
|---|
| 95 | |
|---|
| 96 | return self.packed(self.viewmode) |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | def viewing_kind(self, okind=None): |
|---|
| 100 | """ |
|---|
| 101 | Combobox that displays possible ways on how graph may grab changes. |
|---|
| 102 | """ |
|---|
| 103 | self.evtsgraph = gtk.combo_box_new_text() |
|---|
| 104 | self.evtsgraph.append_text(_("Select visualization")) |
|---|
| 105 | |
|---|
| 106 | for kind in view_kind_order: |
|---|
| 107 | self.evtsgraph.append_text(view_kind[kind]) |
|---|
| 108 | |
|---|
| 109 | if okind: |
|---|
| 110 | option = view_kind_order.index(okind) + 1 |
|---|
| 111 | else: |
|---|
| 112 | option = 0 |
|---|
| 113 | |
|---|
| 114 | self.evtsgraph.set_active(option) |
|---|
| 115 | |
|---|
| 116 | self.evtsgraph.connect('changed', self._change_graph_viewkind) |
|---|
| 117 | |
|---|
| 118 | return self.packed(self.evtsgraph) |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | def graph_kind(self): |
|---|
| 122 | """ |
|---|
| 123 | Combobox for setting graph kind. Right now there is only line graph |
|---|
| 124 | and area graph. |
|---|
| 125 | """ |
|---|
| 126 | modes = (_("Line Graph"), _("Area Graph")) |
|---|
| 127 | self.cbgraphs = gtk.combo_box_new_text() |
|---|
| 128 | self.cbgraphs.append_text(_("Select a graph style")) |
|---|
| 129 | |
|---|
| 130 | for mode in modes: |
|---|
| 131 | self.cbgraphs.append_text(mode) |
|---|
| 132 | |
|---|
| 133 | self.cbgraphs.set_active(1) |
|---|
| 134 | self.cbgraphs.connect('changed', self._change_graph_kind) |
|---|
| 135 | |
|---|
| 136 | return self.packed(self.cbgraphs) |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | def visibility(self): |
|---|
| 140 | """ |
|---|
| 141 | Control for hiding/showing graph. |
|---|
| 142 | """ |
|---|
| 143 | modes = (_("Visible"), _("Hidden")) |
|---|
| 144 | self.visibility = gtk.combo_box_new_text() |
|---|
| 145 | self.visibility.append_text(_("Graph visibility")) |
|---|
| 146 | |
|---|
| 147 | for mode in modes: |
|---|
| 148 | self.visibility.append_text(mode) |
|---|
| 149 | |
|---|
| 150 | self.visibility.set_active(1) |
|---|
| 151 | self.visibility.connect('changed', self._change_graph_visibility) |
|---|
| 152 | |
|---|
| 153 | return self.packed(self.visibility) |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | def graph_pref(self): |
|---|
| 157 | """ |
|---|
| 158 | Button for opening Graph Preferences window. |
|---|
| 159 | """ |
|---|
| 160 | self.btn_gpref = gtk.Button(stock=gtk.STOCK_EDIT) |
|---|
| 161 | self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT) |
|---|
| 162 | # get label widget to change default label text "Edit" to |
|---|
| 163 | # "Graph Preferences" |
|---|
| 164 | blbl_box = self.btn_gpref.get_children()[0].get_children()[0] |
|---|
| 165 | blbl = blbl_box.get_children()[1] |
|---|
| 166 | blbl.set_text(_("Graph Preferences")) |
|---|
| 167 | |
|---|
| 168 | self.btn_gpref.connect('clicked', self._open_graph_pref) |
|---|
| 169 | self.btn_gpref.show() |
|---|
| 170 | |
|---|
| 171 | self.tooltips.set_tip(self.btn_gpref, _("Edit Graph Preferences")) |
|---|
| 172 | |
|---|
| 173 | return self.packed(self.btn_gpref) |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | def graph_refresh(self): |
|---|
| 177 | """ |
|---|
| 178 | Control graph refreshing. |
|---|
| 179 | """ |
|---|
| 180 | img = gtk.Image() |
|---|
| 181 | img.set_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_BUTTON) |
|---|
| 182 | |
|---|
| 183 | menubar = gtk.MenuBar() |
|---|
| 184 | menuitem = gtk.MenuItem() |
|---|
| 185 | menuitem.add(img) |
|---|
| 186 | |
|---|
| 187 | submenu = gtk.Menu() |
|---|
| 188 | |
|---|
| 189 | items = (_("Refresh Now"), _("Refresh Enabled"), |
|---|
| 190 | (_("Refresh Every minute"), 60), |
|---|
| 191 | (_("Refresh Every 5 minutes"), 300), |
|---|
| 192 | (_("Refresh Every hour"), 3600), (_("Custom"), -1) ) |
|---|
| 193 | |
|---|
| 194 | # default 5 mins refresh |
|---|
| 195 | radiogroup = gtk.RadioMenuItem(None, items[3][0]) |
|---|
| 196 | radiogroup.connect('toggled', self._queue_graph_refresh, items[3][1]) |
|---|
| 197 | self.timeout = items[3][1] |
|---|
| 198 | radiogroup.set_active(True) |
|---|
| 199 | |
|---|
| 200 | for indx, item in enumerate(items): |
|---|
| 201 | if indx == 0: # refresh now |
|---|
| 202 | submenu_item = gtk.MenuItem(item) |
|---|
| 203 | submenu_item.connect('activate', self._graph_refresh) |
|---|
| 204 | |
|---|
| 205 | submenu.add(submenu_item) |
|---|
| 206 | submenu.add(gtk.SeparatorMenuItem()) |
|---|
| 207 | elif indx == 1: # refresh enabled |
|---|
| 208 | submenu_item = gtk.CheckMenuItem(item) |
|---|
| 209 | submenu_item.connect('activate', self._graph_refresh_state, |
|---|
| 210 | submenu) |
|---|
| 211 | submenu_item.set_active(True) |
|---|
| 212 | |
|---|
| 213 | submenu.add(submenu_item) |
|---|
| 214 | submenu.add(gtk.SeparatorMenuItem()) |
|---|
| 215 | elif indx == 3: # refresh every 5 minutes, radio group |
|---|
| 216 | # entry has been created earlier, just add it |
|---|
| 217 | submenu.add(radiogroup) |
|---|
| 218 | |
|---|
| 219 | else: # other radio buttons |
|---|
| 220 | radio = gtk.RadioMenuItem(radiogroup, item[0]) |
|---|
| 221 | if indx == len(items) - 1: # custom refresh |
|---|
| 222 | radio.connect('button-press-event', |
|---|
| 223 | self._change_custom_refresh, item[1]) |
|---|
| 224 | |
|---|
| 225 | radio.connect('toggled', self._queue_graph_refresh, item[1]) |
|---|
| 226 | submenu.add(radio) |
|---|
| 227 | |
|---|
| 228 | menuitem.set_submenu(submenu) |
|---|
| 229 | menubar.add(menuitem) |
|---|
| 230 | |
|---|
| 231 | self.tooltips.set_tip(menubar, _("Graph Refresh")) |
|---|
| 232 | |
|---|
| 233 | return self.packed(menubar) |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | def update_graph(self): |
|---|
| 237 | """ |
|---|
| 238 | Update graph after setting new options for it. |
|---|
| 239 | """ |
|---|
| 240 | self.daddy.setup_new_graph() |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | def _change_graph_viewmode(self, event): |
|---|
| 244 | """ |
|---|
| 245 | Set new viewing mode. |
|---|
| 246 | """ |
|---|
| 247 | mode = view_mode_order[event.get_active()] |
|---|
| 248 | kind = self.evtsgraph.get_active() |
|---|
| 249 | |
|---|
| 250 | if kind == 1: |
|---|
| 251 | self.connector.emit('data-changed', mode, 'sum') |
|---|
| 252 | elif kind == 2: |
|---|
| 253 | self.connector.emit('data-changed', mode, 'category') |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | def _change_graph_viewkind(self, event): |
|---|
| 257 | """ |
|---|
| 258 | Changing how graph displays events will result in grabbing events in |
|---|
| 259 | a different way. |
|---|
| 260 | """ |
|---|
| 261 | active = event.get_active() |
|---|
| 262 | if active: # Didnt select "Select visualization" |
|---|
| 263 | mode = view_mode_order[self.viewmode.get_active()] |
|---|
| 264 | |
|---|
| 265 | if active == 1: |
|---|
| 266 | self.connector.emit('data-changed', mode, 'sum') |
|---|
| 267 | elif active == 2: |
|---|
| 268 | self.connector.emit('data-changed', mode, 'category') |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | def _change_graph_kind(self, event): |
|---|
| 272 | """ |
|---|
| 273 | Set new graph kind. |
|---|
| 274 | """ |
|---|
| 275 | active = event.get_active() |
|---|
| 276 | if active: # Didnt select "Select a graph style" |
|---|
| 277 | setattr(self.daddy, "graph_type", active-1) |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | def _change_graph_visibility(self, event): |
|---|
| 281 | """ |
|---|
| 282 | Change graph visibility. |
|---|
| 283 | """ |
|---|
| 284 | status = event.get_active() |
|---|
| 285 | if status: |
|---|
| 286 | if status == 1: |
|---|
| 287 | self.connector.emit('graph-show', True) |
|---|
| 288 | elif status == 2: |
|---|
| 289 | self.connector.emit('graph-show', False) |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | def _open_graph_pref(self, event): |
|---|
| 293 | """ |
|---|
| 294 | Open Graph Preferences window. |
|---|
| 295 | """ |
|---|
| 296 | w = GraphPreferences(self) |
|---|
| 297 | w.show_all() |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | def _graph_refresh(self, event): |
|---|
| 301 | """ |
|---|
| 302 | Perform graph refresh now. |
|---|
| 303 | """ |
|---|
| 304 | log.info(">>> TLGraph being refreshed!") |
|---|
| 305 | self.connector.emit('data-changed', None, None) |
|---|
| 306 | |
|---|
| 307 | return True |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | def _graph_refresh_state(self, event, submenu): |
|---|
| 311 | """ |
|---|
| 312 | Change graph refresh to enabled/disabled. |
|---|
| 313 | """ |
|---|
| 314 | if not event.get_active(): # disable timer |
|---|
| 315 | if self.refresher != -1: |
|---|
| 316 | gobject.source_remove(self.refresher) |
|---|
| 317 | self.refresher = -1 |
|---|
| 318 | |
|---|
| 319 | else: # enable timer, get current selection |
|---|
| 320 | for indx, child in enumerate(submenu.get_children()[4:]): |
|---|
| 321 | if child.get_active(): |
|---|
| 322 | self._queue_graph_refresh(None, REFRESH[indx]) |
|---|
| 323 | break |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | def _change_custom_refresh(self, event, what, what2): |
|---|
| 327 | """ |
|---|
| 328 | Change Custom refresh settings. |
|---|
| 329 | This is used when user clicks on 'Custom' but it is already selected, |
|---|
| 330 | so signal toggled isn't activated, instead, we handle |
|---|
| 331 | button-press-event so it opens dialog for changing custom minutes. |
|---|
| 332 | """ |
|---|
| 333 | if not event.get_active(): |
|---|
| 334 | return |
|---|
| 335 | |
|---|
| 336 | else: |
|---|
| 337 | if self.timeout == -1: |
|---|
| 338 | self.timeout = 60 # 1 minute |
|---|
| 339 | |
|---|
| 340 | ret = custom_refresh(self.timeout/60) |
|---|
| 341 | if ret: |
|---|
| 342 | self.timeout = ret * 60 |
|---|
| 343 | |
|---|
| 344 | if self.refresher != -1: |
|---|
| 345 | # remove previous timer |
|---|
| 346 | gobject.source_remove(self.refresher) |
|---|
| 347 | |
|---|
| 348 | self.refresher = gobject.timeout_add(self.timeout * 1000, |
|---|
| 349 | self._graph_refresh, None) |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | def _queue_graph_refresh(self, event, time): |
|---|
| 353 | """ |
|---|
| 354 | Perform graph refresh after some time. |
|---|
| 355 | """ |
|---|
| 356 | if self.refresher != -1: |
|---|
| 357 | # remove previous timer |
|---|
| 358 | gobject.source_remove(self.refresher) |
|---|
| 359 | |
|---|
| 360 | if time == -1: |
|---|
| 361 | if self.timeout == -1: |
|---|
| 362 | self.timeout = 60 # 1 minute |
|---|
| 363 | |
|---|
| 364 | if type(event.__class__) != type(type): |
|---|
| 365 | # Custom was toggled/activated or deactivated |
|---|
| 366 | if event.get_active(): |
|---|
| 367 | ret = custom_refresh(self.timeout/60) |
|---|
| 368 | if ret: |
|---|
| 369 | self.timeout = ret * 60 |
|---|
| 370 | else: |
|---|
| 371 | self.timeout = time |
|---|
| 372 | |
|---|
| 373 | self.refresher = gobject.timeout_add(self.timeout * 1000, |
|---|
| 374 | self._graph_refresh, None) |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | def __set_graph_mode(self, mode): |
|---|
| 378 | """ |
|---|
| 379 | Change graph mode. |
|---|
| 380 | """ |
|---|
| 381 | getattr(self.daddy, mode)() |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | def __set_graph_attr(self, (attr, value)): |
|---|
| 385 | """ |
|---|
| 386 | Change some graph attribute. |
|---|
| 387 | """ |
|---|
| 388 | setattr(self.daddy, attr, value) |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | def graph_attr(self, attr): |
|---|
| 392 | """ |
|---|
| 393 | Return some graph attribute. |
|---|
| 394 | """ |
|---|
| 395 | return getattr(self.daddy, attr) |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | # Properties |
|---|
| 399 | change_graph_mode = property(fset=__set_graph_mode) |
|---|
| 400 | change_graph_attr = property(fset=__set_graph_attr) |
|---|
| 401 | |
|---|