Subversion Repositories basico

Rev

Rev 142 | Rev 147 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: callbacks.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: UI and related callbacks service
7
 
8
import os
9
import csv
10
import json
74 t00mlabs 11
import time
12
import glob
13
from os.path import basename
14
from datetime import datetime
4 t00mlabs 15
 
16
import gi
17
gi.require_version('Gtk', '3.0')
138 t00mlabs 18
#~ gi.require_version('WebKit', '3.0')
105 t00mlabs 19
from gi.repository import GLib
4 t00mlabs 20
from gi.repository import Gtk
21
from gi.repository import Gio
22
from gi.repository import Pango
23
from gi.repository.GdkPixbuf import Pixbuf
138 t00mlabs 24
#~ from gi.repository import WebKit
4 t00mlabs 25
 
74 t00mlabs 26
from concurrent.futures import ThreadPoolExecutor as Executor
27
 
4 t00mlabs 28
from .service import Service
29
 
74 t00mlabs 30
SEP = os.path.sep
4 t00mlabs 31
 
74 t00mlabs 32
# PROPKEYS = CSV headers. SAP Note metadata
33
PROPKEYS = ['id', 'title', 'type', 'componentkey',
34
            'componenttxt', 'category', 'priority', 'releaseon',
35
            'language', 'version']
36
 
37
# Extend PROPKEYS with custom basico metadata
38
PROPKEYS.extend (['Tasks', 'Bookmark'])
39
 
4 t00mlabs 40
class Callback(Service):
41
    def initialize(self):
42
        self.get_services()
43
 
44
    def get_services(self):
126 t00mlabs 45
        self.settings = self.app.get_service('Settings')
112 t00mlabs 46
        self.db = self.app.get_service('DB')
4 t00mlabs 47
        self.gui = self.app.get_service('GUI')
48
        self.uif = self.app.get_service("UIF")
49
        self.sap = self.app.get_service('SAP')
50
        self.tasks = self.app.get_service('Tasks')
51
        self.alert = self.app.get_service('Notify')
81 t00mlabs 52
        self.im = self.app.get_service('IM')
74 t00mlabs 53
        self.utils = self.app.get_service('Utils')
4 t00mlabs 54
 
55
    def execute_action(self, *args):
56
        action = args[0]
57
        action_name = action.get_name()
58
        try:
59
            callback = "self.%s()" % action_name.replace('-', '_')
60
            return eval(callback)
61
        except Exception as error:
62
            self.log.error(error)
63
            self.log.error("Callback for action '%s' not registered" % action_name)
64
            raise
65
 
66
 
67
    def actions_browse(self, *args):
126 t00mlabs 68
        SAP_NOTE_URL = self.settings.get('SAP', 'SAP_NOTE_URL')
101 t00mlabs 69
        sapnoteview = self.gui.get_widget('viewmenu')
128 t00mlabs 70
        visor = self.gui.get_widget('visor')
71
        sapnotes = visor.get_toggled()
74 t00mlabs 72
        lurl = []
73
        for sid in sapnotes:
74
            url = SAP_NOTE_URL % sid
75
            lurl.append(url)
133 t00mlabs 76
            self.debug("Browsing SAP Note %s" % sid)
74 t00mlabs 77
        self.utils.browse(lurl)
4 t00mlabs 78
 
79
 
80
    def actions_other_delete(self, *args):
74 t00mlabs 81
        db = self.get_service('DB')
101 t00mlabs 82
        sapnoteview = self.gui.get_widget('viewmenu')
129 t00mlabs 83
        visor = self.gui.get_widget('visor')
84
        sapnotes = visor.get_toggled()
4 t00mlabs 85
        sapnotes.sort()
128 t00mlabs 86
        winroot = self.gui.get_widget('mainwindow')
4 t00mlabs 87
 
88
        dialog = Gtk.MessageDialog(winroot, 0, Gtk.MessageType.WARNING,
89
            Gtk.ButtonsType.OK_CANCEL, "Are you sure?")
90
        dialog.set_title("Deleting SAP Notes...")
91
        dialog.set_modal(True)
92
        dialog.set_transient_for(winroot)
93
        dialog.format_secondary_text(
94
            "These SAP Notes will be deleted:\n%s" % ', '.join(sapnotes))
95
 
96
        response = dialog.run()
97
        if response == Gtk.ResponseType.OK:
74 t00mlabs 98
            db = self.get_service('DB')
129 t00mlabs 99
            visor = self.gui.get_widget('visor')
100
            sapnotes = visor.get_toggled()
4 t00mlabs 101
            for sapnote in sapnotes:
74 t00mlabs 102
                db.delete(sapnote)
4 t00mlabs 103
            self.search_notes()
104
            self.refresh_view()
105
            self.alert.show('Delete', 'Selected SAP Notes deleted', 'information')
133 t00mlabs 106
            self.debug("Selected SAP Notes deleted")
74 t00mlabs 107
            db.save_notes()
4 t00mlabs 108
        elif response == Gtk.ResponseType.CANCEL:
109
            self.alert.show('Delete', 'Delete action canceled by user', 'warning')
133 t00mlabs 110
            self.debug("Delete action canceled by user")
4 t00mlabs 111
 
112
        dialog.destroy()
113
 
114
        return response
115
 
91 t00mlabs 116
    def update_titlebar_title(self, title, icon, capitalize=True):
81 t00mlabs 117
        image = self.gui.get_widget('imgtitlebar')
84 t00mlabs 118
        icon = self.im.get_pixbuf_icon(icon.lower(), 24, 24)
81 t00mlabs 119
        image.set_from_pixbuf(icon)
120
        image.show_all()
121
        titlebar = self.gui.get_widget("lbltitlebar")
91 t00mlabs 122
        if capitalize:
123
            title = title.capitalize()
141 t00mlabs 124
        titlebar.set_markup("<big><b>%15s</b></big>" % title)
125
        titlebar.set_hexpand(True)
4 t00mlabs 126
 
81 t00mlabs 127
 
137 t00mlabs 128
    #~ def show_dashboard(self, *args):
129
        #~ notebook = self.gui.get_widget('mainbox')
130
        #~ notebook.set_current_page(0)
131
        #~ self.update_titlebar_title("Dashboard", "dashboard")
78 t00mlabs 132
 
133
 
137 t00mlabs 134
    #~ def show_workplace(self, *args):
135
        #~ notebook = self.gui.get_widget('mainbox')
136
        #~ notebook.set_current_page(1)
137
        #~ self.update_titlebar_title("Workplace", "workplace")
78 t00mlabs 138
 
139
 
137 t00mlabs 140
    #~ def show_school(self, *args):
141
        #~ notebook = self.gui.get_widget('mainbox')
142
        #~ notebook.set_current_page(2)
143
        #~ self.update_titlebar_title("My SAP School", "school", False)
81 t00mlabs 144
 
145
 
137 t00mlabs 146
    #~ def show_workbook(self, *args):
147
        #~ notebook = self.gui.get_widget('mainbox')
148
        #~ notebook.set_current_page(3)
149
        #~ self.update_titlebar_title("Workbook", "comments")
81 t00mlabs 150
 
83 t00mlabs 151
 
84 t00mlabs 152
    def toggle_search(self, *args):
153
        tgbsearch = self.gui.get_widget('tgbSearch')
154
        toggled = tgbsearch.get_active()
155
        tgbsearch.set_active(not toggled)
156
 
136 t00mlabs 157
    def show_about(self, *args):
158
        from .about import About
159
        visor = self.gui.get_widget('visor')
160
        self.gui.swap_widget(visor, About())
133 t00mlabs 161
 
93 t00mlabs 162
    def show_help(self, *args):
163
        notebook = self.gui.get_widget('mainbox')
164
        notebook.set_current_page(5)
165
        self.update_titlebar_title("Help", "basico_help")
84 t00mlabs 166
 
133 t00mlabs 167
 
122 t00mlabs 168
    def show_settings(self, button):
169
        notebook = self.gui.get_widget('mainbox')
170
        notebook.set_current_page(4)
171
        self.update_titlebar_title("Settings", "settings")
79 t00mlabs 172
 
122 t00mlabs 173
    def show_popover(self, button, popover):
110 t00mlabs 174
        if popover.get_visible():
175
            popover.hide()
176
        else:
177
            popover.show_all()
79 t00mlabs 178
 
122 t00mlabs 179
 
180
    def show_popover_menu(self, button, popover):
119 t00mlabs 181
        if popover.get_visible():
182
            popover.hide()
183
        else:
184
            popover.show_all()
185
 
186
 
74 t00mlabs 187
    def show_properties(self, *args):
188
        notebook = self.gui.get_widget('ntbOperations')
189
        toggle = self.gui.get_widget('tgbShowManage')
190
        notebook.set_current_page(0)
191
        toggle.set_active(True)
192
 
193
 
4 t00mlabs 194
    def actions_manage_tasks(self, *args):
74 t00mlabs 195
        self.tasks.show_window(active=True)
196
 
197
 
198
    def link_tasks_to_sapnotes(self, *args):
101 t00mlabs 199
        sapnoteview = self.gui.get_widget('viewmenu')
129 t00mlabs 200
        visor = self.gui.get_widget('visor')
201
        sapnotes = visor.get_toggled()
74 t00mlabs 202
        tasks = self.tasks.get_selected()
203
        self.sap.link_to_task(sapnotes, tasks)
4 t00mlabs 204
 
205
    def actions_bookmark(self, *args):
74 t00mlabs 206
        db = self.get_service('DB')
101 t00mlabs 207
        sapnoteview = self.gui.get_widget('viewmenu')
129 t00mlabs 208
        visor = self.gui.get_widget('visor')
209
        sapnotes = visor.get_toggled()
133 t00mlabs 210
        self.debug("Action bookmark triggered for SAP Notes: %s" % sapnotes)
4 t00mlabs 211
        view = sapnoteview.get_view()
212
        self.sap.set_bookmark(sapnotes)
74 t00mlabs 213
        db.save_notes()
4 t00mlabs 214
        self.refresh_view(view=view)
215
        self.alert.show('Bookmarks', 'Selected SAP Notes bookmarked', 'information')
216
 
217
 
218
    def actions_unbookmark(self, *args):
74 t00mlabs 219
        db = self.get_service('DB')
101 t00mlabs 220
        sapnoteview = self.gui.get_widget('viewmenu')
129 t00mlabs 221
        visor = self.gui.get_widget('visor')
222
        sapnotes = visor.get_toggled()
133 t00mlabs 223
        self.debug("Action unbookmark triggered for SAP Notes: %s" % sapnotes)
4 t00mlabs 224
        self.sap.set_no_bookmark(sapnotes)
74 t00mlabs 225
        db.save_notes()
4 t00mlabs 226
        self.refresh_view(view='bookmarks')
227
        self.alert.show('Bookmarks', 'Selected SAP Notes unbookmarked', 'information')
228
 
229
 
118 t00mlabs 230
    def bookmark(self, lsid):
231
        self.db.set_bookmark(lsid)
112 t00mlabs 232
 
118 t00mlabs 233
    def unbookmark(self, lsid):
234
        self.db.set_no_bookmark(lsid)
112 t00mlabs 235
 
118 t00mlabs 236
 
4 t00mlabs 237
    def actions_export_csv(self, *args):
74 t00mlabs 238
        db = self.get_service('DB')
128 t00mlabs 239
        winroot = self.gui.get_widget('mainwndow')
101 t00mlabs 240
        sapnoteview = self.gui.get_widget('viewmenu')
129 t00mlabs 241
        visor = self.gui.get_widget('visor')
242
        sapnotes = visor.get_toggled()
128 t00mlabs 243
        dialog = Gtk.FileChooserDialog("Save file", winroot,
4 t00mlabs 244
            Gtk.FileChooserAction.SAVE,
245
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
246
                 Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
247
        response = dialog.run()
248
 
249
        if response == Gtk.ResponseType.OK:
250
            export_path = dialog.get_filename()
251
            writer = csv.writer(open(export_path, 'w'), delimiter=';', quoting=csv.QUOTE_ALL)
252
            csvrow = []
74 t00mlabs 253
 
4 t00mlabs 254
            for key in PROPKEYS:
74 t00mlabs 255
                csvrow.append(key.capitalize())
4 t00mlabs 256
            writer.writerow(csvrow)
74 t00mlabs 257
 
258
            for tid in sapnotes:
259
                sid = "0"*(10 - len(tid)) + tid
133 t00mlabs 260
                self.debug("Exporting SAP Note %s to CSV file" % sid)
4 t00mlabs 261
                csvrow = []
74 t00mlabs 262
                props = db.get_sapnote_metadata(sid)
263
 
4 t00mlabs 264
                for prop in PROPKEYS:
265
                    if prop == 'tasks':
266
                        tasks = ', '.join(props[prop])
267
                        csvrow.append(tasks)
268
                    else:
269
                        csvrow.append(props[prop])
270
                writer.writerow(csvrow)
271
            self.alert.show('Export', 'Selected SAP Notes exported successfully to CSV format', 'information')
133 t00mlabs 272
            self.debug("Selected SAP Notes exported to CSV format: %s" % export_path)
4 t00mlabs 273
        else:
274
            self.alert.show('Export', 'Export canceled by user', 'warning')
133 t00mlabs 275
            self.debug("Export canceled by user")
4 t00mlabs 276
        dialog.destroy()
277
 
278
 
279
    def actions_export_txt(self, *args):
280
        rootwin = self.gui.get_widget('mainwndow')
101 t00mlabs 281
        sapnoteview = self.gui.get_widget('viewmenu')
129 t00mlabs 282
        visor = self.gui.get_widget('visor')
283
        sapnotes = visor.get_toggled()
4 t00mlabs 284
        dialog = Gtk.FileChooserDialog("Save file", rootwin,
285
            Gtk.FileChooserAction.SAVE,
286
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
287
                 Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
288
        response = dialog.run()
289
 
290
        if response == Gtk.ResponseType.OK:
291
            export_path = dialog.get_filename()
292
            fout = open(export_path, 'w')
293
            for sapnote in sapnotes:
294
                fout.write("%s\n" % sapnote)
295
            self.alert.show('Export', 'Selected SAP Notes exported successfully to TXT format', 'information')
133 t00mlabs 296
            self.debug("Selected SAP Notes exported to TXT format: %s" % export_path)
4 t00mlabs 297
        else:
298
            self.alert.show('Export', 'Export canceled by user', 'warning')
133 t00mlabs 299
            self.debug("Export canceled by user")
4 t00mlabs 300
        dialog.destroy()
301
 
302
 
74 t00mlabs 303
    def actions_export_json(self, *args):
304
        db = self.get_service('DB')
4 t00mlabs 305
        rootwin = self.gui.get_widget('mainwndow')
101 t00mlabs 306
        sapnoteview = self.gui.get_widget('viewmenu')
129 t00mlabs 307
        visor = self.gui.get_widget('visor')
308
        sapnotes = visor.get_toggled()
4 t00mlabs 309
        dialog = Gtk.FileChooserDialog("Save file", rootwin,
74 t00mlabs 310
            Gtk.FileChooserAction.SELECT_FOLDER,
4 t00mlabs 311
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
312
                 Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
313
        response = dialog.run()
314
 
315
        if response == Gtk.ResponseType.OK:
316
            export_path = dialog.get_filename()
317
            bag = {}
74 t00mlabs 318
            for tid in sapnotes:
319
                sid = "0"*(10 - len(tid)) + tid
320
                sapnote = db.get_sapnote_metadata(sid)
4 t00mlabs 321
                bag[sid] = sapnote
74 t00mlabs 322
            now = datetime.now()
323
            target = export_path + SEP + 'basico-%s.json' % now.strftime("%Y%m%d_%H%M%S")
324
            db.export_basico_package(bag, target)
325
            self.alert.show('Export', 'Selected SAP Notes exported successfully to JSON format', 'information')
133 t00mlabs 326
            self.debug("Selected SAP Notes exported to JSON: %s" % target)
4 t00mlabs 327
        else:
328
            self.alert.show('Export', 'Export canceled by user', 'warning')
133 t00mlabs 329
            self.debug("Export canceled by user")
4 t00mlabs 330
        dialog.destroy()
331
 
332
 
74 t00mlabs 333
    def actions_import_json(self, *args):
334
        self.import_notes_from_file()
4 t00mlabs 335
 
74 t00mlabs 336
 
337
    def actions_import_launchpad(self, *args):
338
        self.show_addsapnotes_dialog()
339
 
340
 
4 t00mlabs 341
    def set_search_filter_key(self, key):
342
        self.gui.set_key('cmbvalue', key)
343
 
344
 
345
    def get_search_filter_key(self):
346
        cmbvalue = self.gui.get_key('cmbvalue')
347
 
348
 
349
    def set_search_term(self, term):
141 t00mlabs 350
        self.debug("Search term: %s" % term)
351
        try:
352
            searchentry = self.gui.get_widget("stySearchInfo")
353
            searchentry.set_text(term)
354
        except:
355
            pass
4 t00mlabs 356
 
357
 
358
    def search_notes(self, *args):
74 t00mlabs 359
        db = self.get_service('DB')
4 t00mlabs 360
        searchentry = self.gui.get_widget("stySearchInfo")
361
        cmbvalue = self.gui.get_key('cmbvalue')
115 t00mlabs 362
 
4 t00mlabs 363
        try:
364
            term = searchentry.get_text()
365
        except:
366
            term = ''
74 t00mlabs 367
 
133 t00mlabs 368
        self.debug("cmbvalue: %s" % cmbvalue)
369
        self.debug("term: %s" % term)
115 t00mlabs 370
 
123 t00mlabs 371
        visor = self.gui.get_widget('visor')
74 t00mlabs 372
        sapnotes = db.get_notes()
4 t00mlabs 373
        found = {}
374
 
375
        if len(term) == 0:
376
            self.current_notes = found = sapnotes
133 t00mlabs 377
            self.debug("Found: %s" % len(found))
123 t00mlabs 378
            visor.populate(found)
133 t00mlabs 379
            self.debug("--> Displaying all database")
4 t00mlabs 380
            return
381
 
133 t00mlabs 382
        self.debug("Looking for '%s'" % term)
4 t00mlabs 383
        if cmbvalue == 'search':
384
            for sid in sapnotes:
385
                if term.upper() in sapnotes[sid]['title'].upper():
386
                   found[sid] = sapnotes[sid]
387
        elif cmbvalue == 'project':
388
            for sid in sapnotes:
389
                try:
390
                    projects = sapnotes[sid]['projects']
391
                    for project in projects:
392
                        if term.upper() in project.upper():
393
                           found[sid] = sapnotes[sid]
394
                except: pass
395
        elif cmbvalue == 'task':
396
            for sid in sapnotes:
397
                try:
398
                    tasks = sapnotes[sid]['tasks']
399
                    for task in tasks:
400
                        if term.upper() in task.upper():
401
                           found[sid] = sapnotes[sid]
402
                except: pass
74 t00mlabs 403
        elif cmbvalue == 'title':
404
            for sid in sapnotes:
405
                try:
406
                    title = sapnotes[sid]['title']
407
                    if term.upper() in title.upper():
408
                       found[sid] = sapnotes[sid]
409
                except: pass
410
        elif cmbvalue == 'priority':
411
            for sid in sapnotes:
412
                try:
413
                    priority = sapnotes[sid]['priority']
414
                    if term.upper() in priority.upper():
415
                       found[sid] = sapnotes[sid]
416
                except: pass
4 t00mlabs 417
        elif cmbvalue == 'component':
418
            for sid in sapnotes:
419
                if term.upper() in sapnotes[sid]['componentkey'].upper():
420
                   found[sid] = sapnotes[sid]
421
        elif cmbvalue == 'category':
422
            for sid in sapnotes:
423
                if term.upper() in sapnotes[sid]['category'].upper():
424
                   found[sid] = sapnotes[sid]
425
        elif cmbvalue == 'type':
426
            for sid in sapnotes:
427
                if term.upper() in sapnotes[sid]['type'].upper():
428
                   found[sid] = sapnotes[sid]
429
        elif cmbvalue == 'version':
430
            for sid in sapnotes:
431
                if term.upper() in sapnotes[sid]['version'].upper():
432
                   found[sid] = sapnotes[sid]
433
        elif cmbvalue == 'id':
434
            for sid in sapnotes:
435
                if term in sid:
436
                   found[sid] = sapnotes[sid]
133 t00mlabs 437
        self.debug("Term: '%s' (%d results)" % (term, len(found)))
4 t00mlabs 438
        self.current_notes = found
133 t00mlabs 439
        self.debug("Current Notes: %d" % len(self.current_notes))
123 t00mlabs 440
        visor.populate(found)
4 t00mlabs 441
 
442
 
443
    def import_notes(self, entry):
74 t00mlabs 444
        self.import_notes_from_sapnet()
4 t00mlabs 445
 
446
 
447
    def import_notes_from_file(self):
74 t00mlabs 448
        db = self.get_service('DB')
4 t00mlabs 449
        notebook = self.gui.get_widget('notebook')
130 t00mlabs 450
        dialog = Gtk.FileChooserDialog("Select Basico JSON file", None,
74 t00mlabs 451
            Gtk.FileChooserAction.OPEN,
452
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
453
                 Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
454
        response = dialog.run()
455
 
456
        if response == Gtk.ResponseType.OK:
457
            source = dialog.get_filename()
458
            try:
459
                with open(source, 'r') as fp:
460
                    bag = json.load(fp)
461
                    db.import_sapnotes(bag)
133 t00mlabs 462
                    self.debug ("Imported %d notes from %s" % (len(bag), source))
74 t00mlabs 463
            except Exception as error:
133 t00mlabs 464
                self.debug("SAP Notes database not found. Creating a new one")
74 t00mlabs 465
                self.save_notes()
101 t00mlabs 466
            sapnoteview = self.gui.get_widget('viewmenu')
4 t00mlabs 467
            self.current_notes = bag
468
            sapnoteview.populate(bag)
74 t00mlabs 469
            db.save_notes()
4 t00mlabs 470
            self.refresh_view()
471
            switch = self.gui.get_widget('schSelectNotesAllNone')
74 t00mlabs 472
        else:
4 t00mlabs 473
            self.alert.show('Import', 'Nothing imported', 'error')
133 t00mlabs 474
            self.debug("Nothing imported")
74 t00mlabs 475
        dialog.destroy()
4 t00mlabs 476
 
144 t00mlabs 477
    def import_notes_from_sapnet(self, bag):
74 t00mlabs 478
        db = self.get_service('DB')
479
        driver = self.get_service('Driver')
128 t00mlabs 480
        winroot = self.gui.get_widget('mainwindow')
133 t00mlabs 481
        self.debug("%d SAP Notes to be downloaded: %s" % (len(bag), ', '.join(list(bag))))
4 t00mlabs 482
 
144 t00mlabs 483
        result = {}
4 t00mlabs 484
 
74 t00mlabs 485
        self.sap.start_fetching(len(bag))
142 t00mlabs 486
        dlbag = []
74 t00mlabs 487
 
488
        # FIXME: max_workers = 1 = Threads disabled
130 t00mlabs 489
        with Executor(max_workers=1) as exe:
74 t00mlabs 490
            jobs = []
491
            for sapnote in bag:
492
                job = exe.submit(self.sap.fetch, sapnote)
493
                jobs.append(job)
494
 
495
            for job in jobs:
496
                rc, sapnote = job.result()
133 t00mlabs 497
                self.debug("\tRC SAP Note %s: %s" % (sapnote, rc))
144 t00mlabs 498
                result[sapnote] = rc
142 t00mlabs 499
                if rc:
500
                    sid = "0"*(10 - len(sapnote)) + sapnote
501
                    dlbag.append(sid)
127 t00mlabs 502
                time.sleep(0.2)
74 t00mlabs 503
 
504
        driver.close()
505
        self.sap.stop_fetching()
506
        db.save_notes()
507
        db.build_stats()
133 t00mlabs 508
        self.debug("Task completed.")
144 t00mlabs 509
        return result
510
        # ~ dlbag.sort()
511
        # ~ package = self.db.get_package(dlbag)
512
        # ~ menuview = self.gui.get_widget('viewmenu')
513
        # ~ menuview.populate(package)
514
        # ~ visor = self.gui.get_widget('visor')
515
        # ~ visor.populate(dlbag)
516
 
141 t00mlabs 517
        #~ notebook.set_current_page(0)
142 t00mlabs 518
        #~ dialog = Gtk.MessageDialog(winroot, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Task completed")
519
        #~ msgrc = ""
520
        #~ ko = 0
521
        #~ ok = 0
522
        #~ for sapnote in resnotes:
523
            #~ rc = resnotes[sapnote]
524
            #~ if rc:
525
                #~ ok += 1
526
            #~ else:
527
                #~ ko += 1
528
        #~ msgrc += "Downloaded: %d\nErroneus: %d" % (ok, ko)
529
        #~ self.alert.show('Download', msgrc, 'information')
530
        #~ dlbag = {}
531
        #~ mysapnotes = db.get_notes()
532
        #~ erroneus = set()
533
        #~ for sid in bag:
534
            #~ sid = "0"*(10 - len(sid)) + sid
535
            #~ try:
536
                #~ dlbag[sid] = mysapnotes[sid]
537
            #~ except Exception as error:
538
                #~ self.log.error(error)
539
                #~ erroneus.add(sid)
74 t00mlabs 540
 
142 t00mlabs 541
        #~ sapnoteview = self.gui.get_widget('viewmenu')
542
        #~ self.current_notes = dlbag
543
        #~ self.refresh_and_clear_view()
544
        #~ sapnoteview.populate(dlbag)
545
        #~ sapnoteview.expand_all()
4 t00mlabs 546
 
547
 
548
    def stop_dl_notes(self, *args):
549
        notebook = self.gui.get_widget('notebook')
550
        txtSAPNotes = self.gui.get_widget('txtSAPNotes')
551
        buffer = txtSAPNotes.get_buffer()
552
        buffer.set_text("")
553
        notebook.set_current_page(0)
554
        self.refresh_view()
74 t00mlabs 555
        boxMenu = self.gui.get_widget('boxMenu')
556
        boxMenu.show_all()
557
        self.refresh_and_clear_view()
4 t00mlabs 558
 
74 t00mlabs 559
 
560
    def rebuild_database(self, *args):
133 t00mlabs 561
        self.debug("Rebuild database...")
74 t00mlabs 562
 
563
        db = self.get_service('DB')
128 t00mlabs 564
        CACHE_DIR = self.get_var('CACHE', 'local')
565
        files = glob.glob("%s%s*.xml" % (CACHE_DIR, SEP))
74 t00mlabs 566
        sap = self.app.get_service('SAP')
567
        for filename in files:
133 t00mlabs 568
            self.debug("Filename: %s" % filename)
74 t00mlabs 569
            sid = basename(filename)[0:-4]
133 t00mlabs 570
            self.debug("SAP Note Id: %s" % sid)
74 t00mlabs 571
 
572
            valid = False
573
            if db.is_stored(sid):
133 t00mlabs 574
                self.debug("\tSAP Note %s will be analyzed again" % sid)
74 t00mlabs 575
                content = db.get_sapnote_content(sid)
576
                sapnote = sap.analyze_sapnote_metadata(sid, content)
577
                if len(sapnote) > 0:
578
                    db = self.get_service('DB')
579
                    db.add(sapnote)
580
                    db.store(sid, content)
581
                    valid = True
582
 
583
        self.refresh_view()
584
 
4 t00mlabs 585
    def refresh_and_clear_view(self, *args):
133 t00mlabs 586
        self.debug("Refresh & clear view ( I)")
4 t00mlabs 587
        switch = self.gui.get_widget('schSelectNotesAllNone')
101 t00mlabs 588
        sapnoteview = self.gui.get_widget('viewmenu')
4 t00mlabs 589
        self.set_search_filter_key('search')
590
        self.set_search_term('')
591
        self.search_notes()
592
 
74 t00mlabs 593
 
4 t00mlabs 594
    def refresh_view(self, action=None, callback=None, view=None):
133 t00mlabs 595
        self.debug("Refresh & clear view (II)")
4 t00mlabs 596
        window = self.gui.get_widget('mainwindow')
116 t00mlabs 597
        viewmenu = self.gui.get_widget('viewmenu')
74 t00mlabs 598
 
4 t00mlabs 599
        if view is not None:
600
            viewlabel = self.gui.get_widget('lblViewCurrent')
91 t00mlabs 601
            name = "<b>%-10s</b>" % view.capitalize()
4 t00mlabs 602
            viewlabel.set_markup(name)
116 t00mlabs 603
        viewmenu.set_view(view)
4 t00mlabs 604
 
605
 
80 t00mlabs 606
    def setup_dashboard(self):
607
        lblStatsSAPNotesCount = self.gui.add_widget('lblStatsSAPNotesCount')
608
 
74 t00mlabs 609
 
610
    def show_addsapnotes_dialog(self, *args):
101 t00mlabs 611
        sapnoteview = self.gui.get_widget('viewmenu')
74 t00mlabs 612
        sapnoteview.set_view('download')
613
        notebook = self.gui.get_widget('notebook')
614
        boxMenu = self.gui.get_widget('boxMenu')
615
        boxMenu.hide()
616
        notebook.set_current_page(1)
617
 
618
 
619
    def default_preferences(self, *args):
620
        prefs = self.get_service('Settings')
621
        gui = self.get_service('GUI')
622
        settings = prefs.get_default_settings()
623
        for key in settings:
624
            widget = gui.get_widget(key)
625
            widget.set_text(str(settings[key]))
626
 
627
        self.config[self.section] = settings
628
        self.save_config()
133 t00mlabs 629
        self.debug("Settings reverted to default")
74 t00mlabs 630
 
631
 
138 t00mlabs 632
    #~ def update_components_stats(self, *args):
633
        #~ statsviewer = self.gui.get_widget('scrStatsViewer')
634
        #~ view = WebKit.WebView()
635
        #~ chart = self.stats.build_pie_maincomp()
636
        #~ view.load_string(chart, 'text/html', 'UTF-8','/')
637
        #~ self.gui.swap_widget(statsviewer, view)
4 t00mlabs 638
 
74 t00mlabs 639
 
138 t00mlabs 640
    #~ def update_categories_stats(self, *args):
641
        #~ statsviewer = self.gui.get_widget('scrStatsViewer')
642
        #~ view = WebKit.WebView()
643
        #~ chart = self.stats.build_pie_categories()
644
        #~ view.load_string(chart, 'text/html', 'UTF-8','/')
645
        #~ self.gui.swap_widget(statsviewer, view)
4 t00mlabs 646
 
647
 
74 t00mlabs 648
    def check_task_link_button_status(self):
649
        tasks = self.get_service('Tasks')
650
        button = self.gui.get_widget('btnLinkTasksApply')
101 t00mlabs 651
        sapnoteview = self.gui.get_widget('viewmenu')
74 t00mlabs 652
 
653
        tasks_selected = len(tasks.get_selected()) > 0
654
        notes_selected = len(sapnoteview.get_selected_notes()) > 0
655
        if  tasks_selected and notes_selected:
656
            button.set_sensitive(True)
133 t00mlabs 657
            self.debug("Task link button enabled")
74 t00mlabs 658
        else:
659
            button.set_sensitive(False)
133 t00mlabs 660
            self.debug("Task link button disabled")
74 t00mlabs 661
 
662
 
4 t00mlabs 663
    def test(self, *args):
133 t00mlabs 664
        self.debug(args)