| 1 | # Copyright (C) 2007 Adriano Monteiro Marques |
|---|
| 2 | # |
|---|
| 3 | # Author: 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 USA |
|---|
| 18 | |
|---|
| 19 | import os |
|---|
| 20 | import gtk |
|---|
| 21 | import webbrowser |
|---|
| 22 | from ConfigParser import ConfigParser |
|---|
| 23 | |
|---|
| 24 | from umit.core.Paths import Path |
|---|
| 25 | from umit.core.I18N import _ |
|---|
| 26 | from umit.core.UmitConf import CommandProfile |
|---|
| 27 | from umit.core.CronParser import CronParser |
|---|
| 28 | from umit.core.Utils import open_url_as |
|---|
| 29 | |
|---|
| 30 | from higwidgets.higwindows import HIGWindow |
|---|
| 31 | from higwidgets.higboxes import HIGVBox, HIGHBox, hig_box_space_holder |
|---|
| 32 | from higwidgets.higlabels import HIGEntryLabel |
|---|
| 33 | from higwidgets.higframe import HIGFrame |
|---|
| 34 | from higwidgets.higbuttons import HIGButton |
|---|
| 35 | from higwidgets.higtables import HIGTable |
|---|
| 36 | from higwidgets.higdialogs import HIGAlertDialog |
|---|
| 37 | |
|---|
| 38 | from umit.gui.ProfileCombo import ProfileCombo |
|---|
| 39 | from umit.gui.FileChoosers import DirectoryChooserDialog |
|---|
| 40 | |
|---|
| 41 | pixmaps_dir = Path.pixmaps_dir |
|---|
| 42 | |
|---|
| 43 | if pixmaps_dir: |
|---|
| 44 | logo = os.path.join(pixmaps_dir, 'wizard_logo.png') |
|---|
| 45 | else: |
|---|
| 46 | logo = None |
|---|
| 47 | |
|---|
| 48 | class SchedSchemaEditor(HIGWindow): |
|---|
| 49 | """ |
|---|
| 50 | Scheduler Schemas Editor |
|---|
| 51 | """ |
|---|
| 52 | |
|---|
| 53 | def __init__(self, daddy=None): |
|---|
| 54 | HIGWindow.__init__(self) |
|---|
| 55 | |
|---|
| 56 | self.daddy = daddy |
|---|
| 57 | self.wtitle = _("Scan Scheduler Editor") |
|---|
| 58 | |
|---|
| 59 | # header |
|---|
| 60 | self.title_markup = "<span size='16500' weight='heavy'>%s</span>" |
|---|
| 61 | self.ttitle = HIGEntryLabel("") |
|---|
| 62 | self.ttitle.set_line_wrap(False) |
|---|
| 63 | self.ttitle.set_markup(self.title_markup % self.wtitle) |
|---|
| 64 | self.umit_logo = gtk.Image() |
|---|
| 65 | self.umit_logo.set_from_file(logo) |
|---|
| 66 | # schemas name |
|---|
| 67 | self.schema_name_lbl = HIGEntryLabel(_("Schema Name")) |
|---|
| 68 | self.schema_name = gtk.combo_box_entry_new_text() |
|---|
| 69 | self.schema_name.connect('changed', self._check_schema) |
|---|
| 70 | # target and scan profiles |
|---|
| 71 | #self.target_lbl = HIGEntryLabel(_("Target")) |
|---|
| 72 | #self.target = gtk.Entry() |
|---|
| 73 | self.scan_name_lbl = HIGEntryLabel(_("Scan Profile")) |
|---|
| 74 | self.scan_name = ProfileCombo() |
|---|
| 75 | self.scan_name.update() |
|---|
| 76 | self.scan_name.set_active(0) |
|---|
| 77 | self.scan_name.connect('changed', self._set_scan_command) |
|---|
| 78 | # scan command |
|---|
| 79 | self.scan_command_lbl = HIGEntryLabel(_("Command")) |
|---|
| 80 | self.scan_command = gtk.Entry() |
|---|
| 81 | # scheduling profile |
|---|
| 82 | self.sched_name_lbl = HIGEntryLabel(_("Scheduling Profile")) |
|---|
| 83 | self.sched_name = gtk.combo_box_new_text() |
|---|
| 84 | self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT) |
|---|
| 85 | blbl = self.sched_name_edit.get_children()[0].get_children()[0].get_children()[1] |
|---|
| 86 | blbl.set_text(_("Edit Profiles")) |
|---|
| 87 | self.sched_name_edit.connect('clicked', self._edit_schedprofiles) |
|---|
| 88 | # schema settings |
|---|
| 89 | self.schema_sett_frame = HIGFrame() |
|---|
| 90 | self.setting_saveto = gtk.CheckButton(_("Save outputs to")) |
|---|
| 91 | self.setting_saveto_entry = gtk.Entry() |
|---|
| 92 | self.setting_saveto_browse = gtk.Button(_("...")) |
|---|
| 93 | self.setting_saveto_browse.connect('clicked', self._select_saveto) |
|---|
| 94 | self.setting_mailto = gtk.CheckButton(_("Send output to email")) |
|---|
| 95 | self.setting_mailto_entry = gtk.Entry() |
|---|
| 96 | self.setting_smtp_lbl = HIGEntryLabel(_("SMTP Schema")) |
|---|
| 97 | self.setting_smtp = gtk.combo_box_new_text() |
|---|
| 98 | self.setting_addtoinv = gtk.CheckButton(_("Add to the Inventory")) |
|---|
| 99 | self.setting_enabled = gtk.CheckButton(_("Enabled")) |
|---|
| 100 | # bottom buttons |
|---|
| 101 | self.help = HIGButton(stock=gtk.STOCK_HELP) |
|---|
| 102 | self.help.connect('clicked', self._show_help) |
|---|
| 103 | self.apply = HIGButton(stock=gtk.STOCK_APPLY) |
|---|
| 104 | self.apply.connect('clicked', self._save_schema) |
|---|
| 105 | self.cancel = HIGButton(stock=gtk.STOCK_CANCEL) |
|---|
| 106 | self.cancel.connect('clicked', self._exit) |
|---|
| 107 | self.ok = HIGButton(stock=gtk.STOCK_OK) |
|---|
| 108 | self.ok.connect('clicked', self._save_schema_and_leave) |
|---|
| 109 | |
|---|
| 110 | self.load_smtp_schemas() |
|---|
| 111 | self._set_scan_command(None) |
|---|
| 112 | self.profile_running = None # no SchedProfileEditor instance is running. |
|---|
| 113 | self._load_pscheds() |
|---|
| 114 | self.load_schemas() |
|---|
| 115 | |
|---|
| 116 | self.__set_props() |
|---|
| 117 | self.__do_layout() |
|---|
| 118 | |
|---|
| 119 | self.connect('destroy', self._exit) |
|---|
| 120 | |
|---|
| 121 | def load_smtp_schemas(self): |
|---|
| 122 | """ |
|---|
| 123 | Load smtp profiles. |
|---|
| 124 | """ |
|---|
| 125 | schemas = ConfigParser() |
|---|
| 126 | schemas.read(Path.smtp_schemas) |
|---|
| 127 | |
|---|
| 128 | self.smtp_sections = [ ] |
|---|
| 129 | self.setting_smtp.get_model().clear() |
|---|
| 130 | for section in schemas.sections(): |
|---|
| 131 | self.smtp_sections.append(section) |
|---|
| 132 | self.setting_smtp.append_text(section) |
|---|
| 133 | |
|---|
| 134 | self.setting_smtp.set_active(0) |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | def load_schemas(self): |
|---|
| 138 | """ |
|---|
| 139 | Load schemas profiles. |
|---|
| 140 | """ |
|---|
| 141 | schemas = ConfigParser() |
|---|
| 142 | schemas.read(Path.sched_schemas) |
|---|
| 143 | |
|---|
| 144 | self.sections = [ ] |
|---|
| 145 | self.schema_name.get_model().clear() |
|---|
| 146 | for section in schemas.sections(): |
|---|
| 147 | self.sections.append(section) |
|---|
| 148 | self.schema_name.append_text(section) |
|---|
| 149 | |
|---|
| 150 | self.schema_name.set_active(0) |
|---|
| 151 | self._check_schema(None) |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | def _load_schema(self): |
|---|
| 155 | """ |
|---|
| 156 | Load current set schedule schema. |
|---|
| 157 | """ |
|---|
| 158 | schema = ConfigParser() |
|---|
| 159 | schema.read(Path.sched_schemas) |
|---|
| 160 | |
|---|
| 161 | values = {'command':self.scan_command.set_text, |
|---|
| 162 | 'saveto':self.setting_saveto_entry.set_text, |
|---|
| 163 | 'mailto':self.setting_mailto_entry.set_text} |
|---|
| 164 | enable = {'saveto':self.setting_saveto.set_active, |
|---|
| 165 | 'mailto':self.setting_mailto.set_active} |
|---|
| 166 | |
|---|
| 167 | for item in schema.items(self.schema_name.get_active_text()): |
|---|
| 168 | if item[0] == 'addtoinv': |
|---|
| 169 | self.setting_addtoinv.set_active(int(item[1])) |
|---|
| 170 | if item[1] == '2': |
|---|
| 171 | self.apply.set_sensitive(False) |
|---|
| 172 | self.ok.set_sensitive(False) |
|---|
| 173 | else: |
|---|
| 174 | self.apply.set_sensitive(True) |
|---|
| 175 | self.ok.set_sensitive(True) |
|---|
| 176 | elif item[0] == 'enabled': |
|---|
| 177 | self.setting_enabled.set_active(int(item[1])) |
|---|
| 178 | elif item[0] == 'profile': |
|---|
| 179 | pindx = self.profiles.index(item[1]) |
|---|
| 180 | self.sched_name.set_active(pindx) |
|---|
| 181 | elif item[0] == 'smtp': |
|---|
| 182 | if item[1]: |
|---|
| 183 | pindx = self.smtp_sections.index(item[1]) |
|---|
| 184 | self.setting_smtp.set_active(pindx) |
|---|
| 185 | else: |
|---|
| 186 | values[item[0]](item[1]) |
|---|
| 187 | if item[0] in ('saveto', 'mailto'): |
|---|
| 188 | if len(item[1]): |
|---|
| 189 | enable[item[0]](True) |
|---|
| 190 | else: |
|---|
| 191 | enable[item[0]](False) |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | def _check_schema(self, event): |
|---|
| 195 | """ |
|---|
| 196 | Check if current text in schema_name combobox is a schema name. |
|---|
| 197 | """ |
|---|
| 198 | if self.schema_name.get_active_text() in self.sections: |
|---|
| 199 | # load schema |
|---|
| 200 | self._load_schema() |
|---|
| 201 | else: |
|---|
| 202 | # reset to default values |
|---|
| 203 | self.apply.set_sensitive(True) |
|---|
| 204 | self.ok.set_sensitive(True) |
|---|
| 205 | self.setting_addtoinv.set_active(False) |
|---|
| 206 | self.setting_enabled.set_active(False) |
|---|
| 207 | self.setting_mailto.set_active(False) |
|---|
| 208 | self.setting_mailto_entry.set_text('') |
|---|
| 209 | self.setting_saveto.set_active(False) |
|---|
| 210 | self.setting_saveto_entry.set_text('') |
|---|
| 211 | |
|---|
| 212 | self.schema_sett_frame._set_label(self.schema_name.get_active_text() \ |
|---|
| 213 | + " - Settings") |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | def _set_scan_command(self, event): |
|---|
| 217 | """ |
|---|
| 218 | Set scan command based on chosen profile. |
|---|
| 219 | """ |
|---|
| 220 | profile = self.scan_name.get_selected_profile() |
|---|
| 221 | cmd_profile = CommandProfile() |
|---|
| 222 | command = cmd_profile.get_command(profile) |
|---|
| 223 | self.scan_command.set_text(command % '<target>') |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | def _load_pscheds(self): |
|---|
| 227 | """ |
|---|
| 228 | Load scheduling profiles. |
|---|
| 229 | """ |
|---|
| 230 | pscheds = ConfigParser() |
|---|
| 231 | pscheds.read(Path.sched_profiles) |
|---|
| 232 | |
|---|
| 233 | self.profiles = [ ] |
|---|
| 234 | self.sched_name.get_model().clear() |
|---|
| 235 | for section in pscheds.sections(): |
|---|
| 236 | self.sched_name.append_text(section) |
|---|
| 237 | self.profiles.append(section) |
|---|
| 238 | |
|---|
| 239 | self.sched_name.set_active(0) |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | def _edit_schedprofiles(self, event): |
|---|
| 243 | """ |
|---|
| 244 | Open Scheduling Profiles Editor. |
|---|
| 245 | """ |
|---|
| 246 | if self.profile_running: |
|---|
| 247 | return |
|---|
| 248 | |
|---|
| 249 | win = SchedProfileEditor(self, self.sched_name.get_active_text()) |
|---|
| 250 | win.show_all() |
|---|
| 251 | self.profile_running = win |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | def _select_saveto(self, event): |
|---|
| 255 | """ |
|---|
| 256 | Select directory to save file. |
|---|
| 257 | """ |
|---|
| 258 | dir_chooser = DirectoryChooserDialog(_("Select a directory")) |
|---|
| 259 | |
|---|
| 260 | dir_chooser.run() |
|---|
| 261 | dir_chosen = dir_chooser.get_filename() |
|---|
| 262 | dir_chooser.destroy() |
|---|
| 263 | self.setting_saveto_entry.set_text(dir_chosen) |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | def _save_schema(self, event): |
|---|
| 267 | """ |
|---|
| 268 | Save current schema. |
|---|
| 269 | """ |
|---|
| 270 | schema = self.schema_name.get_active_text() |
|---|
| 271 | command = self.scan_command.get_text() |
|---|
| 272 | schedule = self.sched_name.get_active_text() |
|---|
| 273 | mailto = self.setting_mailto.get_active() |
|---|
| 274 | |
|---|
| 275 | if not schema or not schedule or not command or '<target>' in command: |
|---|
| 276 | dlg = HIGAlertDialog(self, |
|---|
| 277 | message_format=_('Scheduling Schema - Error\ |
|---|
| 278 | while saving.'), |
|---|
| 279 | secondary_text=_("There is some error in at \ |
|---|
| 280 | least one of the following fields: \"Schema name\", \"Command\" or \"Scheduling\ |
|---|
| 281 | Profile\"\n\nCheck if \"Schema name\" is not empty.\nCheck if \"Command\" does\ |
|---|
| 282 | contain \"<target>\" on it.\nCheck if there is some \"Scheduling Profile\" \ |
|---|
| 283 | selected.")) |
|---|
| 284 | |
|---|
| 285 | dlg.run() |
|---|
| 286 | dlg.destroy() |
|---|
| 287 | return |
|---|
| 288 | |
|---|
| 289 | if mailto and not self.setting_smtp.get_active_text(): |
|---|
| 290 | dlg = HIGAlertDialog(self, |
|---|
| 291 | message_format=_('Scheduling Schema - Error\ |
|---|
| 292 | while saving.'), |
|---|
| 293 | secondary_text=_("You need to create a \ |
|---|
| 294 | a SMTP Schema for sending email.")) |
|---|
| 295 | |
|---|
| 296 | dlg.run() |
|---|
| 297 | dlg.destroy() |
|---|
| 298 | return |
|---|
| 299 | |
|---|
| 300 | # check for output existance |
|---|
| 301 | if self.setting_saveto.get_active() and \ |
|---|
| 302 | not os.path.isdir(self.setting_saveto_entry.get_text()): |
|---|
| 303 | |
|---|
| 304 | dlg = HIGAlertDialog(self, |
|---|
| 305 | message_format=_('Scheduling Schema - Error\ |
|---|
| 306 | while saving.'), |
|---|
| 307 | secondary_text=_("You especified an invalid \ |
|---|
| 308 | directory to save scans output.")) |
|---|
| 309 | |
|---|
| 310 | dlg.run() |
|---|
| 311 | dlg.destroy() |
|---|
| 312 | return |
|---|
| 313 | |
|---|
| 314 | # write schema to file |
|---|
| 315 | s_cfg = ConfigParser() |
|---|
| 316 | s_cfg.read(Path.sched_schemas) |
|---|
| 317 | |
|---|
| 318 | if not s_cfg.has_section(schema): |
|---|
| 319 | new_sec = True |
|---|
| 320 | s_cfg.add_section(schema) |
|---|
| 321 | else: |
|---|
| 322 | new_sec = False |
|---|
| 323 | |
|---|
| 324 | s_cfg.set(schema, 'profile', schedule) |
|---|
| 325 | s_cfg.set(schema, 'command', command) |
|---|
| 326 | if self.setting_enabled.get_active(): |
|---|
| 327 | s_cfg.set(schema, 'enabled', '1') |
|---|
| 328 | else: |
|---|
| 329 | s_cfg.set(schema, 'enabled', '0') |
|---|
| 330 | if self.setting_addtoinv.get_active(): |
|---|
| 331 | s_cfg.set(schema, 'addtoinv', '1') |
|---|
| 332 | else: |
|---|
| 333 | s_cfg.set(schema, 'addtoinv', '0') |
|---|
| 334 | if self.setting_saveto.get_active(): |
|---|
| 335 | s_cfg.set(schema, 'saveto', self.setting_saveto_entry.get_text()) |
|---|
| 336 | else: |
|---|
| 337 | s_cfg.set(schema, 'saveto', '') |
|---|
| 338 | if mailto: |
|---|
| 339 | s_cfg.set(schema, 'mailto', self.setting_mailto_entry.get_text()) |
|---|
| 340 | s_cfg.set(schema, 'smtp', self.setting_smtp.get_active_text()) |
|---|
| 341 | else: |
|---|
| 342 | s_cfg.set(schema, 'mailto', '') |
|---|
| 343 | s_cfg.set(schema, 'smtp', '') |
|---|
| 344 | |
|---|
| 345 | s_cfg.write(open(Path.sched_schemas, 'w')) |
|---|
| 346 | |
|---|
| 347 | if new_sec: |
|---|
| 348 | self.load_schemas() |
|---|
| 349 | |
|---|
| 350 | if self.daddy: |
|---|
| 351 | self.daddy.load_schemas() |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | def _save_schema_and_leave(self, event): |
|---|
| 355 | """ |
|---|
| 356 | Save current schema and close editor. |
|---|
| 357 | """ |
|---|
| 358 | self._save_schema(None) |
|---|
| 359 | self._exit(None) |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | def _show_help(self, event): |
|---|
| 363 | """ |
|---|
| 364 | Show help for Scan Scheduler Editor. |
|---|
| 365 | """ |
|---|
| 366 | webbrowser.open("file://%s" % os.path.join( |
|---|
| 367 | Path.docs_dir, |
|---|
| 368 | "scheduler.html#setting-up-a-schedule"), |
|---|
| 369 | new=open_url_as()) |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | def __set_props(self): |
|---|
| 373 | """ |
|---|
| 374 | Set window properties. |
|---|
| 375 | """ |
|---|
| 376 | self.set_title(self.wtitle) |
|---|
| 377 | self.set_default_size(440, -1) |
|---|
| 378 | |
|---|
| 379 | def __do_layout(self): |
|---|
| 380 | """ |
|---|
| 381 | Layout widgets in window. |
|---|
| 382 | """ |
|---|
| 383 | main_vbox = HIGVBox() |
|---|
| 384 | main_vbox.set_border_width(5) |
|---|
| 385 | main_vbox.set_spacing(12) |
|---|
| 386 | header_hbox = HIGHBox() |
|---|
| 387 | schema_table = HIGTable() |
|---|
| 388 | schedsn_hbox = HIGHBox() |
|---|
| 389 | sett_table = HIGTable() |
|---|
| 390 | btns_hbox = HIGHBox() |
|---|
| 391 | |
|---|
| 392 | header_hbox._pack_expand_fill(self.ttitle) |
|---|
| 393 | header_hbox._pack_noexpand_nofill(self.umit_logo) |
|---|
| 394 | |
|---|
| 395 | # schema name |
|---|
| 396 | schema_table.attach_label(self.schema_name_lbl, 0, 1, 0, 1) |
|---|
| 397 | schema_table.attach_entry(self.schema_name, 1, 2, 0, 1) |
|---|
| 398 | |
|---|
| 399 | # target and scan profile |
|---|
| 400 | schema_table.attach_label(self.scan_name_lbl, 0, 1, 1, 2) |
|---|
| 401 | schema_table.attach_entry(self.scan_name, 1, 2, 1, 2) |
|---|
| 402 | |
|---|
| 403 | # scan command |
|---|
| 404 | schema_table.attach_label(self.scan_command_lbl, 0, 1, 2, 3) |
|---|
| 405 | schema_table.attach_label(self.scan_command, 1, 2, 2, 3) |
|---|
| 406 | |
|---|
| 407 | # scheduling profile |
|---|
| 408 | schedsn_hbox._pack_expand_fill(self.sched_name) |
|---|
| 409 | schedsn_hbox._pack_noexpand_nofill(self.sched_name_edit) |
|---|
| 410 | |
|---|
| 411 | schema_table.attach_label(self.sched_name_lbl, 0, 1, 3, 4) |
|---|
| 412 | schema_table.attach_entry(schedsn_hbox, 1, 2, 3, 4) |
|---|
| 413 | |
|---|
| 414 | # settings frame |
|---|
| 415 | settings_align = gtk.Alignment(0.5, 0.5, 1, 1) |
|---|
| 416 | settings_align.set_padding(6, 0, 12, 0) |
|---|
| 417 | schemasett_hbox = HIGVBox() |
|---|
| 418 | |
|---|
| 419 | # saveto |
|---|
| 420 | sett_hbox = HIGHBox() |
|---|
| 421 | sett_hbox._pack_expand_fill(self.setting_saveto_entry) |
|---|
| 422 | sett_hbox._pack_noexpand_nofill(self.setting_saveto_browse) |
|---|
| 423 | sett_table.attach_label(self.setting_saveto, 0, 1, 0, 1) |
|---|
| 424 | sett_table.attach_entry(sett_hbox, 1, 2, 0, 1) |
|---|
| 425 | |
|---|
| 426 | # mailto, smtp |
|---|
| 427 | sett_hbox = HIGHBox() |
|---|
| 428 | sett_hbox._pack_expand_fill(self.setting_mailto_entry) |
|---|
| 429 | sett_hbox._pack_noexpand_nofill(self.setting_smtp_lbl) |
|---|
| 430 | sett_hbox._pack_expand_fill(self.setting_smtp) |
|---|
| 431 | sett_table.attach_label(self.setting_mailto, 0, 1, 1, 2) |
|---|
| 432 | sett_table.attach_entry(sett_hbox, 1, 2, 1, 2) |
|---|
| 433 | schemasett_hbox._pack_noexpand_nofill(sett_table) |
|---|
| 434 | |
|---|
| 435 | # add to inventory |
|---|
| 436 | sett_hbox = HIGHBox() |
|---|
| 437 | sett_hbox._pack_noexpand_nofill(self.setting_addtoinv) |
|---|
| 438 | schemasett_hbox._pack_noexpand_nofill(sett_hbox) |
|---|
| 439 | |
|---|
| 440 | # enabled/disabled |
|---|
| 441 | sett_hbox = HIGHBox() |
|---|
| 442 | sett_hbox._pack_noexpand_nofill(self.setting_enabled) |
|---|
| 443 | schemasett_hbox._pack_noexpand_nofill(sett_hbox) |
|---|
| 444 | settings_align.add(schemasett_hbox) |
|---|
| 445 | |
|---|
| 446 | self.schema_sett_frame.add(settings_align) |
|---|
| 447 | |
|---|
| 448 | # bottom buttons |
|---|
| 449 | btns_hbox.set_homogeneous(True) |
|---|
| 450 | btns_hbox._pack_expand_fill(self.help) |
|---|
| 451 | btns_hbox._pack_expand_fill(hig_box_space_holder()) |
|---|
| 452 | btns_hbox._pack_expand_fill(self.apply) |
|---|
| 453 | btns_hbox._pack_expand_fill(self.cancel) |
|---|
| 454 | btns_hbox._pack_expand_fill(self.ok) |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | main_vbox._pack_noexpand_nofill(header_hbox) |
|---|
| 458 | main_vbox._pack_noexpand_nofill(gtk.HSeparator()) |
|---|
| 459 | main_vbox._pack_noexpand_nofill(schema_table) |
|---|
| 460 | main_vbox._pack_noexpand_nofill(gtk.HSeparator()) |
|---|
| 461 | main_vbox._pack_noexpand_nofill(self.schema_sett_frame) |
|---|
| 462 | main_vbox.pack_end(btns_hbox, False, False, 0) |
|---|
| 463 | |
|---|
| 464 | self.add(main_vbox) |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | def _exit(self, event): |
|---|
| 468 | """ |
|---|
| 469 | Close current and window and profile editor if it is running. |
|---|
| 470 | """ |
|---|
| 471 | if self.profile_running: |
|---|
| 472 | self.profile_running._exit(None) |
|---|
| 473 | |
|---|
| 474 | if self.daddy: |
|---|
| 475 | self.daddy.schemawin = None |
|---|
| 476 | |
|---|
| 477 | self.destroy() |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | def _get_profile_running(self): |
|---|
| 481 | """ |
|---|
| 482 | Get profile editor running instance. |
|---|
| 483 | """ |
|---|
| 484 | return self.__profilerunning |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | def _set_profile_running(self, running): |
|---|
| 488 | """ |
|---|
| 489 | Set profile editor instance. |
|---|
| 490 | """ |
|---|
| 491 | self.__profilerunning = running |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | # Properties |
|---|
| 495 | profile_running = property(_get_profile_running, _set_profile_running) |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | class SchedProfileEditor(HIGWindow): |
|---|
| 499 | """ |
|---|
| 500 | Scheduling Profiles Editor |
|---|
| 501 | """ |
|---|
| 502 | |
|---|
| 503 | def __init__(self, daddy, profile=None): |
|---|
| 504 | HIGWindow.__init__(self) |
|---|
| 505 | self.daddy = daddy |
|---|
| 506 | |
|---|
| 507 | self.wtitle = _("Scheduling Profiles Editor") |
|---|
| 508 | self.start_profile = profile |
|---|
| 509 | |
|---|
| 510 | # header |
|---|
| 511 | self.title_markup = "<span size='16500' weight='heavy'>%s</span>" |
|---|
| 512 | self.ttitle = HIGEntryLabel("") |
|---|
| 513 | self.ttitle.set_line_wrap(False) |
|---|
| 514 | self.ttitle.set_markup(self.title_markup % self.wtitle) |
|---|
| 515 | self.umit_logo = gtk.Image() |
|---|
| 516 | self.umit_logo.set_from_file(logo) |
|---|
| 517 | # profiles name |
|---|
| 518 | self.schedp_name_lbl = HIGEntryLabel(_("Scheduling Profile")) |
|---|
| 519 | self.schedp_name = gtk.combo_box_entry_new_text() |
|---|
| 520 | self.schedp_name.connect('changed', self._check_profile) |
|---|
| 521 | # cron format |
|---|
| 522 | self.cron_frame = HIGFrame(_("Schedule")) |
|---|
| 523 | self.cron_minute_lbl = HIGEntryLabel(_("Minute")) |
|---|
| 524 | self.cron_minute = gtk.Entry() |
|---|
| 525 | self.cron_hour_lbl = HIGEntryLabel(_("Hour")) |
|---|
| 526 | self.cron_hour = gtk.Entry() |
|---|
| 527 | self.cron_day_lbl = HIGEntryLabel(_("Day of month")) |
|---|
| 528 | self.cron_day = gtk.Entry() |
|---|
| 529 | self.cron_month_lbl = HIGEntryLabel(_("Month")) |
|---|
| 530 | self.cron_month = gtk.Entry() |
|---|
| 531 | self.cron_weekday_lbl = HIGEntryLabel(_("Weekday")) |
|---|
| 532 | self.cron_weekday = gtk.Entry() |
|---|
| 533 | # bottom buttons |
|---|
| 534 | self.help = HIGButton(stock=gtk.STOCK_HELP) |
|---|
| 535 | self.help.connect('clicked', self._show_help) |
|---|
| 536 | self.apply = HIGButton(stock=gtk.STOCK_APPLY) |
|---|
| 537 | self.apply.connect('clicked', self._save_profile) |
|---|
| 538 | self.cancel = HIGButton(stock=gtk.STOCK_CANCEL) |
|---|
| 539 | self.cancel.connect('clicked', self._exit) |
|---|
| 540 | self.ok = HIGButton(stock=gtk.STOCK_OK) |
|---|
| 541 | self.ok.connect('clicked', self._save_profile_and_leave) |
|---|
| 542 | |
|---|
| 543 | self.load_profiles() |
|---|
| 544 | self.__set_props() |
|---|
| 545 | self.__do_layout() |
|---|
| 546 | |
|---|
| 547 | self.connect('destroy', self._exit) |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | def load_profiles(self): |
|---|
| 551 | """ |
|---|
| 552 | Load scheduling profiles. |
|---|
| 553 | """ |
|---|
| 554 | profiles = ConfigParser() |
|---|
| 555 | profiles.read(Path.sched_profiles) |
|---|
| 556 | |
|---|
| 557 | self.sections = [ ] |
|---|
| 558 | ind = 0 |
|---|
| 559 | for indx, section in enumerate(profiles.sections()): |
|---|
| 560 | self.sections.append(section) |
|---|
| 561 | self.schedp_name.append_text(section) |
|---|
| 562 | if section == self.start_profile: |
|---|
| 563 | ind = indx |
|---|
| 564 | |
|---|
| 565 | self.schedp_name.set_active(ind) |
|---|
| 566 | |
|---|
| 567 | self._check_profile(None) |
|---|
| 568 | |
|---|
| 569 | def _load_profile(self): |
|---|
| 570 | """ |
|---|
| 571 | Load current set schedule profile. |
|---|
| 572 | """ |
|---|
| 573 | profile = ConfigParser() |
|---|
| 574 | profile.read(Path.sched_profiles) |
|---|
| 575 | |
|---|
| 576 | values = {'minute':self.cron_minute.set_text, |
|---|
| 577 | 'hour':self.cron_hour.set_text, |
|---|
| 578 | 'day':self.cron_day.set_text, |
|---|
| 579 | 'month':self.cron_month.set_text, |
|---|
| 580 | 'weekday':self.cron_weekday.set_text} |
|---|
| 581 | |
|---|
| 582 | for item in profile.items(self.schedp_name.get_active_text()): |
|---|
| 583 | values[item[0]](item[1]) |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | def _check_profile(self, event): |
|---|
| 587 | """ |
|---|
| 588 | Check if current text in schedp_name combobox is a profile name. |
|---|
| 589 | """ |
|---|
| 590 | if self.schedp_name.get_active_text() in self.sections: |
|---|
| 591 | self._load_profile() |
|---|
| 592 | else: |
|---|
| 593 | self.cron_minute.set_text('') |
|---|
| 594 | self.cron_hour.set_text('') |
|---|
| 595 | self.cron_day.set_text('') |
|---|
| 596 | self.cron_month.set_text('') |
|---|
| 597 | self.cron_weekday.set_text('') |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | def _save_profile(self, event): |
|---|
| 601 | """ |
|---|
| 602 | Save scheduling profile. |
|---|
| 603 | """ |
|---|
| 604 | pname = self.schedp_name.get_active_text() |
|---|
| 605 | if not len(pname): |
|---|
| 606 | dlg = HIGAlertDialog(self, |
|---|
| 607 | message_format=_('Scheduling Profile - Error \ |
|---|
| 608 | while saving'), |
|---|
| 609 | secondary_text=_("You need to specify a name \ |
|---|
| 610 | for Profile.")) |
|---|
| 611 | dlg.run() |
|---|
| 612 | dlg.destroy() |
|---|
| 613 | return |
|---|
| 614 | |
|---|
| 615 | parser = CronParser() |
|---|
| 616 | minute = self.cron_minute.get_text() |
|---|
| 617 | hour = self.cron_hour.get_text() |
|---|
| 618 | day = self.cron_day.get_text() |
|---|
| 619 | month = self.cron_month.get_text() |
|---|
| 620 | weekday = self.cron_weekday.get_text() |
|---|
| 621 | try: |
|---|
| 622 | parser.parse_minute(minute) |
|---|
| 623 | parser.parse_hour(hour) |
|---|
| 624 | parser.parse_day(day) |
|---|
| 625 | parser.parse_month(month) |
|---|
| 626 | parser.parse_weekday(weekday) |
|---|
| 627 | except Exception, e: |
|---|
| 628 | dlg = HIGAlertDialog(self, |
|---|
| 629 | message_format=_('Scheduling Profile - Error \ |
|---|
| 630 | while saving'), |
|---|
| 631 | secondary_text=_("Check your cron syntax and \ |
|---|
| 632 | try to save again.")) |
|---|
| 633 | dlg.run() |
|---|
| 634 | dlg.destroy() |
|---|
| 635 | return |
|---|
| 636 | |
|---|
| 637 | # write profile to file |
|---|
| 638 | p_cfg = ConfigParser() |
|---|
| 639 | p_cfg.read(Path.sched_profiles) |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | if not p_cfg.has_section(pname): |
|---|
| 643 | new_sec = True |
|---|
| 644 | p_cfg.add_section(pname) |
|---|
| 645 | else: |
|---|
| 646 | new_sec = False |
|---|
| 647 | |
|---|
| 648 | p_cfg.set(pname, 'minute', minute) |
|---|
| 649 | p_cfg.set(pname, 'hour', hour) |
|---|
| 650 | p_cfg.set(pname, 'day', day) |
|---|
| 651 | p_cfg.set(pname, 'month', month) |
|---|
| 652 | p_cfg.set(pname, 'weekday', weekday) |
|---|
| 653 | |
|---|
| 654 | p_cfg.write(open(Path.sched_profiles, 'w')) |
|---|
| 655 | |
|---|
| 656 | if new_sec: # update daddy scheduling profiles list |
|---|
| 657 | self.daddy._load_pscheds() |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | def _save_profile_and_leave(self, event): |
|---|
| 661 | """ |
|---|
| 662 | Save scheduling profile and leave. |
|---|
| 663 | """ |
|---|
| 664 | self._save_profile(None) |
|---|
| 665 | self._exit(None) |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | def _show_help(self, event): |
|---|
| 669 | """ |
|---|
| 670 | Show help for Scheduling Profiles. |
|---|
| 671 | """ |
|---|
| 672 | webbrowser.open("file://%s" % os.path.join( |
|---|
| 673 | Path.docs_dir, |
|---|
| 674 | "scheduler.html#creating-a-new-scheduling-profile"), |
|---|
| 675 | new=open_url_as()) |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | def __set_props(self): |
|---|
| 679 | """ |
|---|
| 680 | Set window properties |
|---|
| 681 | """ |
|---|
| 682 | self.set_title(self.wtitle) |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | def __do_layout(self): |
|---|
| 686 | """ |
|---|
| 687 | Layout window widgets. |
|---|
| 688 | """ |
|---|
| 689 | main_vbox = HIGVBox() |
|---|
| 690 | main_vbox.set_border_width(5) |
|---|
| 691 | main_vbox.set_spacing(12) |
|---|
| 692 | header_hbox = HIGHBox() |
|---|
| 693 | schedp_hbox = HIGHBox() |
|---|
| 694 | cron_box = HIGVBox() |
|---|
| 695 | cron_table = HIGTable(5, 2) |
|---|
| 696 | btns_hbox = HIGHBox() |
|---|
| 697 | |
|---|
| 698 | header_hbox._pack_expand_fill(self.ttitle) |
|---|
| 699 | header_hbox._pack_noexpand_nofill(self.umit_logo) |
|---|
| 700 | |
|---|
| 701 | schedp_hbox._pack_noexpand_nofill(self.schedp_name_lbl) |
|---|
| 702 | schedp_hbox._pack_expand_fill(self.schedp_name) |
|---|
| 703 | |
|---|
| 704 | # cron format |
|---|
| 705 | settings_align = gtk.Alignment(0.5, 0.5, 1, 1) |
|---|
| 706 | settings_align.set_padding(6, 0, 12, 0) |
|---|
| 707 | |
|---|
| 708 | cron_table.attach(self.cron_minute_lbl, 0, 1, 0, 1) |
|---|
| 709 | cron_table.attach(self.cron_minute, 1, 2, 0, 1) |
|---|
| 710 | cron_table.attach(self.cron_hour_lbl, 0, 1, 1, 2) |
|---|
| 711 | cron_table.attach(self.cron_hour, 1, 2, 1, 2) |
|---|
| 712 | cron_table.attach(self.cron_day_lbl, 0, 1, 2, 3) |
|---|
| 713 | cron_table.attach(self.cron_day, 1, 2, 2, 3) |
|---|
| 714 | cron_table.attach(self.cron_month_lbl, 0, 1, 3, 4) |
|---|
| 715 | cron_table.attach(self.cron_month, 1, 2, 3, 4) |
|---|
| 716 | cron_table.attach(self.cron_weekday_lbl, 0, 1, 4, 5) |
|---|
| 717 | cron_table.attach(self.cron_weekday, 1, 2, 4, 5) |
|---|
| 718 | |
|---|
| 719 | cron_box._pack_noexpand_nofill(cron_table) |
|---|
| 720 | settings_align.add(cron_box) |
|---|
| 721 | self.cron_frame.add(settings_align) |
|---|
| 722 | |
|---|
| 723 | # bottom buttons |
|---|
| 724 | btns_hbox.set_homogeneous(True) |
|---|
| 725 | btns_hbox._pack_expand_fill(self.help) |
|---|
| 726 | btns_hbox._pack_expand_fill(hig_box_space_holder()) |
|---|
| 727 | btns_hbox._pack_expand_fill(self.apply) |
|---|
| 728 | btns_hbox._pack_expand_fill(self.cancel) |
|---|
| 729 | btns_hbox._pack_expand_fill(self.ok) |
|---|
| 730 | |
|---|
| 731 | main_vbox._pack_noexpand_nofill(header_hbox) |
|---|
| 732 | main_vbox._pack_noexpand_nofill(gtk.HSeparator()) |
|---|
| 733 | main_vbox._pack_noexpand_nofill(schedp_hbox) |
|---|
| 734 | main_vbox._pack_noexpand_nofill(self.cron_frame) |
|---|
| 735 | main_vbox.pack_end(btns_hbox, False, False, 0) |
|---|
| 736 | self.add(main_vbox) |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | def _exit(self, event): |
|---|
| 740 | """ |
|---|
| 741 | Close window and change profile editor instance on daddy. |
|---|
| 742 | """ |
|---|
| 743 | self.daddy.profile_running = None |
|---|
| 744 | self.destroy() |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | if __name__ == "__main__": |
|---|
| 748 | w = SchedSchemaEditor() |
|---|
| 749 | w.show_all() |
|---|
| 750 | w.connect('destroy', gtk.main_quit) |
|---|
| 751 | gtk.main() |
|---|
| 752 | |
|---|