Subversion Repositories basico

Rev

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

Rev Author Line No. Line
340 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
"""
4
# File: srv_cb.py
5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: UI and related callbacks service
8
"""
9
 
10
import os
11
import json
12
import time
13
from concurrent.futures import ThreadPoolExecutor as Executor
14
 
15
import gi
16
gi.require_version('Gtk', '3.0')
17
from gi.repository import Gtk
18
from gi.repository import Gdk
19
 
20
from basico.core.mod_srv import Service
21
from basico.core.mod_env import FILE, LPATH, ATYPES, APP
22
from basico.widgets.wdg_visor_sapnotes import SAPNotesVisor
23
from basico.widgets.wdg_visor_toolbar import VisorToolbar
24
from basico.widgets.wdg_cols import CollectionsMgtView
25
from basico.widgets.wdg_settingsview import SettingsView
26
 
27
# PROPKEYS = CSV headers. SAP Note metadata
28
PROPKEYS = ['id', 'title', 'type', 'componentkey',
29
            'componenttxt', 'category', 'priority', 'releasedon',
30
            'language', 'version']
31
 
32
# Extend PROPKEYS with custom basico metadata
33
PROPKEYS.extend (['Bookmark'])
34
 
35
class Callback(Service):
36
    def initialize(self):
37
        self.get_services()
38
 
39
    def get_services(self):
40
        self.srvstg = self.get_service('Settings')
41
        self.srvdtb = self.get_service('DB')
42
        self.srvgui = self.get_service('GUI')
43
        self.srvuif = self.get_service("UIF")
44
        self.srvsap = self.get_service('SAP')
45
        self.srvicm = self.get_service('IM')
46
        self.srvutl = self.get_service('Utils')
47
        self.srvant = self.get_service('Annotation')
48
        self.srvbnr = self.get_service('BNR')
49
        self.srvclt = self.get_service('Collections')
356 t00mlabs 50
        self.srvatc = self.get_service('Attachment')
340 t00mlabs 51
 
52
 
53
    def gui_visor_switch_page(self, notebook, page, page_num):
54
        # 0|1|(2) -> SAP Notes Visor | Annotations Visor | (Help)
55
        self.srvgui.set_key_value('current_visor_tab', page_num)
56
        notebook_viewmenu = self.srvgui.get_widget('gtk_notebook_menuview')
57
        paned = self.srvgui.get_widget('gtk_hpaned')
58
 
59
        if page_num == 0:
60
            self.srvuif.set_widget_visibility('gtk_button_menu_views', True)
61
            self.srvuif.set_widget_visibility('gtk_label_total_notes', True)
62
            visor = self.srvgui.get_widget('visor_sapnotes')
63
            visible_filter = visor.get_visible_filter()
64
            visor.update_total_sapnotes_count()
65
            paned.set_position(400)
66
            notebook_viewmenu.set_current_page(0)
67
        elif page_num == 1:
68
            self.srvuif.set_widget_visibility('gtk_button_menu_views', False)
69
            self.srvuif.set_widget_visibility('gtk_label_total_notes', True)
70
            visor = self.srvgui.get_widget('visor_annotations')
71
            visor.populate_annotations()
349 t00mlabs 72
            paned.set_position(400)
340 t00mlabs 73
            notebook_viewmenu.set_current_page(1)
74
 
75
 
76
    def gui_show_visor_sapnotes(self):
77
        notebook = self.srvgui.get_widget('gtk_notebook_visor')
78
        notebook.set_current_page(0)
79
 
80
 
81
    def gui_show_visor_annotations(self):
82
        notebook = self.srvgui.get_widget('gtk_notebook_visor')
83
        notebook.set_current_page(1)
84
 
85
 
86
    def action_search(self, entry):
87
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
88
        visor_annotations = self.srvgui.get_widget('visor_annotations')
89
        term = entry.get_text()
90
        page = self.srvgui.get_key_value('current_visor_tab')
91
        completion = self.srvgui.get_widget('gtk_entrycompletion_visor')
92
        completion_model = completion.get_model()
93
        completion_model.clear()
94
 
95
        if page == 0:
96
            bag = self.srvdtb.search(term)
97
            visor_sapnotes.populate_sapnotes(bag)
98
            ebuffer = entry.get_buffer()
99
            ebuffer.delete_text(0, -1)
100
            msg = "Found %d SAP Notes for term '%s'" % (len(bag), term)
101
            self.log.info(msg)
102
            self.srvuif.statusbar_msg(msg)
103
        elif page == 1:
104
            annotations = self.srvant.search_term(term)
105
            visor_annotations.populate_annotations(annotations)
106
            msg = "Found %d annotations for term '%s'" % (len(annotations), term)
107
            self.log.info(msg)
108
            self.srvuif.statusbar_msg(msg)
109
 
110
 
111
 
112
    def sapnote_browse(self, button, sid):
113
        self.log.info("Browsing SAP Note %d" % int(sid))
114
        SAP_NOTE_URL = self.srvstg.get('SAP', 'SAP_NOTE_URL')
115
        url = SAP_NOTE_URL % sid
116
        self.srvutl.browse(url)
117
 
118
 
119
    def sapnote_download_pdf(self, button, sid):
120
        self.log.info("Browsing PDF for SAP Note %d" % int(sid))
121
        SAP_NOTE_URL_PDF = self.srvstg.get('SAP', 'SAP_NOTE_URL_PDF')
122
        url = SAP_NOTE_URL_PDF % sid
123
        self.srvutl.browse(url)
124
 
125
 
126
    def sapnote_delete(self, button, sid):
127
        visor = self.srvgui.get_widget('visor_sapnotes')
128
        viewmenu = self.srvgui.get_widget('viewmenu')
129
 
130
        answer = self.srvuif.warning_message_delete_sapnotes(button, 'Deleting SAP Notes', 'Are you sure?', [sid])
131
        if answer is True:
132
            self.srvdtb.delete(sid)
133
            self.srvuif.statusbar_msg("SAP Note %s deleted" % sid, True)
134
            visor.reload()
135
        else:
136
            self.log.info("SAP Note %s not deletd", sid)
137
 
138
 
139
    def sapnote_delete_view(self, button):
140
        visor = self.srvgui.get_widget('visor_sapnotes')
141
        viewmenu = self.srvgui.get_widget('viewmenu')
142
 
143
        bag = visor.get_filtered_bag()
144
        answer = self.srvuif.warning_message_delete_sapnotes(button, 'Deleting SAP Notes', 'Are you sure?', bag)
145
        if answer is True:
146
            for sid in bag:
147
                self.srvdtb.delete(sid)
148
            visor.reload()
149
            msg = "Deleted %d SAP Notes" % len(bag)
150
            self.log.info(msg)
151
            self.srvuif.statusbar_msg(msg, True)
152
        else:
153
            msg = "None of the %d SAP Notes has been deleted" % len(bag)
154
            self.log.info(msg)
155
            self.srvuif.statusbar_msg(msg, True)
156
        visor.reload()
157
        viewmenu.populate()
158
 
159
    def gui_hide_popover(self, popover):
160
        popover.hide()
161
 
162
 
163
    def gui_database_backup(self, *args):
164
        window = self.srvgui.get_window()
165
        question = "Backup database"
166
        message = "\nShould this backup include all your annotations?"
167
        wdialog = self.srvuif.message_dialog_question(question, message)
168
        res_bck_annot = wdialog.run()
169
        wdialog.destroy()
170
 
171
        dialog = Gtk.FileChooserDialog("Please choose target folder and backup name (without extension)", window,
172
            Gtk.FileChooserAction.SAVE,
173
            ("Cancel", Gtk.ResponseType.CANCEL,
174
            "Select", Gtk.ResponseType.OK))
175
        TIMESTAMP = self.srvutl.timestamp()
176
        TARGET_FILE = 'basico-%s' % TIMESTAMP
177
        dialog.set_filename(LPATH['BACKUP'])
178
        dialog.set_current_folder_uri(LPATH['BACKUP'])
179
        dialog.set_current_name(TARGET_FILE)
180
        dialog.set_default_size(800, 400)
181
        res_bck_file = dialog.run()
182
        self.log.debug("Backup file: %s", dialog.get_filename())
183
 
184
        if res_bck_file == Gtk.ResponseType.OK:
185
            backup_filename = dialog.get_filename()
186
            if res_bck_annot == Gtk.ResponseType.YES:
187
                bckname = self.srvbnr.backup(backup_filename, backup_annotations=True)
188
                msg = "Database with annotations backed up successfully to: %s" % bckname
189
                dialog.destroy()
190
            else:
191
                bckname = self.srvbnr.backup(backup_filename, backup_annotations=False)
192
                msg = "Database without annotations backed up successfully to: %s" % bckname
193
            self.log.info(msg)
194
            self.srvuif.statusbar_msg(msg, True)
195
            self.srvuif.copy_text_to_clipboard(bckname)
196
 
197
        else:
198
            self.srvuif.statusbar_msg("Backup aborted by user", True)
199
            self.log.info("Backup aborted")
200
        self.srvuif.grab_focus()
201
 
202
 
203
    def gui_database_restore(self, *args):
204
        visor_annotations = self.srvgui.get_widget('visor_annotations')
205
        window = self.srvgui.get_window()
206
        dialog = Gtk.FileChooserDialog("Please choose a backup file", window,
207
            Gtk.FileChooserAction.OPEN,
208
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
209
            Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
210
        dialog.set_current_folder(LPATH['BACKUP'])
211
        filter_zip = Gtk.FileFilter()
212
        filter_zip.set_name("Basico backup files")
213
        filter_zip.add_pattern("*.bco")
214
        dialog.add_filter(filter_zip)
215
        response = dialog.run()
216
        backup = dialog.get_filename()
217
        self.log.info("You are about to restore this file: %s" % backup)
218
        dialog.destroy()
219
 
220
        if response == Gtk.ResponseType.OK:
221
            res = self.srvbnr.test(backup)
222
            if res is None:
223
                dialog = self.srvuif.message_dialog_info("Backup file corrupted?", "It wasn't possible to read all data from this file")
224
                dialog.run()
225
                self.srvuif.statusbar_msg("Backup file corrupted. It wasn't possible to read all data from this file", True)
226
                self.log.info("Backup file corrupted?")
227
            else:
228
                # Ask if the process should force an overwrite in target database
229
                question = "Should the import process overwrite data?"
230
                message = "All metadata for SAP Notes will be overwrited and annotations with the same identifier will be overwriten"
231
                wdialog = self.srvuif.message_dialog_question(question, message)
232
                response = wdialog.run()
233
                wdialog.destroy()
234
                if response == Gtk.ResponseType.YES:
235
                    overwrite = True
236
                else:
237
                    overwrite = False
238
 
239
 
240
                # Ask for final confirmation
241
                sncount, ancount, clcount = res
242
                question = "Restoring backup %s" % os.path.basename(backup)
243
                message = "\n%s contains:\n\n<b>%6d SAP Notes</b>\n<b>%6d collections</b>\n<b>%6d annotations</b>\n\n" % (os.path.basename(backup), sncount, clcount, ancount)
244
                message += "Do you still want to restore this backup?\n"
245
                wdialog = self.srvuif.message_dialog_question(question, message)
246
                response = wdialog.run()
247
                wdialog.destroy()
248
                if response == Gtk.ResponseType.YES:
249
                    self.srvbnr.restore_from_backup(backup, overwrite)
250
                    self.srvdtb.load_notes()
251
                    self.srvclt.load_collections()
252
                    visor_annotations.populate_annotations()
253
                    self.gui_refresh_view()
254
                    self.gui_show_visor_sapnotes()
255
                    self.srvuif.statusbar_msg("Backup restored successfully", True)
256
                    self.log.info("Backup restored successfully")
257
                elif response == Gtk.ResponseType.NO:
258
                    self.srvuif.statusbar_msg("Restore aborted by user", True)
259
                    self.log.info("Restore aborted")
260
        elif response == Gtk.ResponseType.CANCEL:
261
            self.srvuif.statusbar_msg("Restore aborted by user", True)
262
            self.log.info("Restore aborted")
263
 
264
        dialog.destroy()
265
        self.srvuif.grab_focus()
266
 
267
 
268
    def gui_show_about(self, *args):
269
        notebook_menuview = self.srvgui.get_widget('gtk_notebook_menuview')
270
        about = self.srvgui.get_widget('widget_about')
271
        stack = self.srvgui.get_widget('gtk_stack_main')
272
 
273
        stack.set_visible_child_name('about')
274
        self.gui_hide_popover(self.srvgui.get_widget('gtk_popover_button_menu_system'))
275
        self.srvuif.set_widget_visibility('gtk_label_total_notes', False)
276
        self.srvuif.set_widget_visibility('gtk_button_dashboard', True)
277
 
278
        notebook_menuview.hide()
279
        self.gui_annotation_widget_hide()
280
        about.grab_focus()
281
 
282
 
283
    def gui_show_log(self, *args):
284
        notebook_menuview = self.srvgui.get_widget('gtk_notebook_menuview')
285
        logviewer = self.srvgui.get_widget('widget_logviewer')
286
        stack = self.srvgui.get_widget('gtk_stack_main')
287
 
288
        logviewer.update()
289
        self.gui_hide_popover(self.srvgui.get_widget('gtk_popover_button_menu_system'))
290
        stack.set_visible_child_name('log')
291
        self.srvuif.set_widget_visibility('gtk_button_dashboard', True)
292
 
293
        notebook_menuview.hide()
294
        self.gui_annotation_widget_hide()
295
        logviewer.grab_focus()
296
        self.srvuif.statusbar_msg("Displaying application log")
297
 
298
 
299
    def gui_show_settings(self, button):
300
        notebook_menuview = self.srvgui.get_widget('gtk_notebook_menuview')
301
        stack = self.srvgui.get_widget('gtk_stack_main')
302
        view_settings = self.srvgui.get_widget('widget_settings')
303
 
304
        stack.set_visible_child_name('settings')
305
        view_settings.update()
306
        self.gui_hide_popover(self.srvgui.get_widget('gtk_popover_button_menu_system'))
307
        self.srvuif.set_widget_visibility('gtk_label_total_notes', False)
308
        self.srvuif.set_widget_visibility('gtk_button_dashboard', True)
309
 
310
        notebook_menuview.hide()
311
        self.gui_annotation_widget_hide()
312
        view_settings.grab_focus()
313
        self.srvuif.statusbar_msg("Displaying application settings")
314
 
315
 
316
    def gui_show_dashboard(self, *args):
317
        stack = self.srvgui.get_widget('gtk_stack_main')
318
        notebook_menuview = self.srvgui.get_widget('gtk_notebook_menuview')
319
        viewmenu = self.srvgui.get_widget('viewmenu')
320
        current_view = viewmenu.get_view()
321
 
322
        notebook_menuview.show_all()
323
        stack.set_visible_child_name('visor')
324
        self.gui_hide_popover(self.srvgui.get_widget('gtk_popover_button_menu_system'))
325
        self.srvuif.set_widget_visibility('gtk_button_dashboard', False)
326
 
327
        if current_view == 'annotation':
328
            self.srvuif.set_widget_visibility('gtk_label_total_notes', True)
329
        else:
330
            self.srvuif.set_widget_visibility('gtk_label_total_notes', True)
331
        self.srvuif.statusbar_msg("Displaying application dashboard")
332
 
333
 
334
    def gui_toggle_help_visor(self, *args):
335
        button = self.srvgui.get_widget('gtk_togglebutton_help')
336
        notebook = self.srvgui.get_widget('gtk_notebook_visor')
337
 
338
        if button.get_active():
339
            self.srvuif.set_widget_visibility('gtk_notebook_help_page', True)
340
            notebook.set_current_page(2)
341
        else:
342
            self.srvuif.set_widget_visibility('gtk_notebook_help_page', False)
343
            notebook.set_current_page(0)
344
        self.srvuif.statusbar_msg("Displaying application help")
345
 
346
 
347
    def gui_lauch_help_visor(self, *args):
348
        self.srvutl.browse("file://%s" % FILE['HELP_INDEX'])
349
 
350
 
351
    def gui_annotation_widget_show(self, widget, sid='0000000000', action='create'):
352
        widget_annotation = self.srvgui.get_widget('widget_annotation')
353 t00mlabs 353
        widget = self.srvgui.get_widget('gtk_label_timestamp_created')
340 t00mlabs 354
 
355
        if action == 'create':
356
            self.gui_annotation_widget_clear()
357
            aid = self.srvant.gen_aid(sid)
358
        elif action == 'edit':
359
            aid = sid
360
 
361
        widget_annotation.set_metadata_to_widget(aid, action)
362
 
363
        self.srvuif.set_widget_visibility('gtk_vbox_container_annotations', True)
364
        widget.grab_focus()
365
 
366
 
367
    def gui_show_popover(self, button, popover):
368
        if popover.get_visible():
369
            popover.hide()
370
        else:
371
            popover.show_all()
372
 
373
 
374
    def switch_bookmark_current_view(self, *args):
375
        visor = self.srvgui.get_widget('visor_sapnotes')
376
        bag = visor.get_filtered_bag()
377
        try:
378
            for sid in bag:
379
                metadata = self.srvdtb.get_sapnote_metadata(sid)
380
                bookmark = metadata['bookmark']
381
                if bookmark:
382
                    self.sapnote_unbookmark([sid])
383
                else:
384
                    self.sapnote_bookmark([sid])
385
        except:
386
            self.log.error("Could not bookmark SAP Note %s" % sid)
387
            self.log.error(self.get_traceback())
388
        visor.populate_sapnotes(bag)
389
        msg = "%d SAP Notes (un)bookmarked" % len(bag)
390
        self.log.info(msg)
391
        self.srvuif.statusbar_msg(msg, True)
392
 
393
 
394
    def switch_bookmark(self, button, lsid, popover):
395
        visor = self.srvgui.get_widget('visor_sapnotes')
396
        try:
397
            for sid in lsid:
398
                metadata = self.srvdtb.get_sapnote_metadata(sid)
399
                bookmark = metadata['bookmark']
400
                if bookmark:
401
                    self.sapnote_unbookmark([sid])
402
                    self.srvuif.statusbar_msg("SAP Notes unbookmarked")
403
                else:
404
                    self.sapnote_bookmark([sid])
405
                    self.srvuif.statusbar_msg("SAP Notes bookmarked")
406
            popover.hide()
407
        except:
408
            self.log.error("Could not bookmark SAP Note %s" % sid)
409
            self.log.error(self.get_traceback())
410
        visor.populate_sapnotes()
411
 
412
 
413
    def sapnote_bookmark(self, lsid):
414
        self.srvdtb.set_bookmark(lsid)
415
 
416
 
417
    def sapnote_unbookmark(self, lsid):
418
        self.srvdtb.set_no_bookmark(lsid)
419
 
420
 
421
    def sapnote_import_from_launchpad(self, *args):
422
        db = self.get_service('DB')
423
        webdriver = self.get_service('Driver')
424
        textview = self.srvgui.get_widget('gtk_textview_download_launchpad')
425
        visor = self.srvgui.get_widget('visor_sapnotes')
426
 
427
        bag = []
428
        all_notes = []
429
        sapnotes = []
430
 
431
        dlbuffer = textview.get_buffer()
432
        istart, iend = dlbuffer.get_bounds()
433
        text = dlbuffer.get_text(istart, iend, False)
434
        lines = text.replace(' ', ',')
435
        lines = lines.replace('\n', ',')
436
        for sid in lines.split(','):
437
            sid = sid.strip()
438
            if len(sid) > 0:
439
                sapnotes.append(sid)
440
        for sid in sapnotes:
441
            is_valid = self.srvdtb.is_valid(sid)
442
            is_saved = self.srvdtb.get_sapnote_metadata(sid)
443
            if is_valid and not is_saved:
444
                bag.append(sid)
445
            if is_valid:
446
                all_notes.append(sid)
447
        lbag = list(bag)
448
        lbag.sort()
449
 
450
        if len(bag)> 0:
451
            driver = webdriver.open()
452
 
453
        winroot = self.srvgui.get_widget('gtk_app_window_main')
454
        msg = "%d SAP Notes to be downloaded: %s" % (len(bag), ', '.join(list(bag)))
455
        self.log.info(msg)
456
 
457
        result = {}
458
 
459
        self.srvsap.start_fetching(len(bag))
460
        dlbag = []
461
 
462
        # FIXME: max_workers = 1 = Threads disabled
463
        # Indeed, I think this is the best option right now.
464
        with Executor(max_workers=1) as exe:
465
            jobs = []
466
            for sapnote in lbag:
467
                job = exe.submit(self.srvsap.fetch, driver, sapnote)
468
                jobs.append(job)
469
 
470
            for job in jobs:
471
                rc, sapnote = job.result()
472
                msg = "\tRC SAP Note %s: %s" % (sapnote, rc)
473
                self.log.info(msg)
474
                result[sapnote] = rc
475
                if rc:
476
                    sid = "0"*(10 - len(sapnote)) + sapnote
477
                    dlbag.append(sid)
478
                time.sleep(0.2)
479
 
480
        dlbuffer.set_text('')
481
        popover = self.srvgui.get_widget('gtk_popover_toolbutton_import')
482
        self.gui_hide_popover(popover)
483
        if len(bag) > 0:
484
            webdriver.close(driver)
485
 
486
        self.srvsap.stop_fetching()
487
        db.save_notes()
488
        db.build_stats()
489
        self.log.info("Download completed.")
490
        self.srvuif.statusbar_msg("Download completed", True)
491
        visor.populate_sapnotes(all_notes)
492
        self.gui_show_visor_sapnotes()
493
        return result
494
 
495
 
496
    def expand_menuview(self):
497
        viewmenu = self.srvgui.get_widget('viewmenu')
498
        viewmenu.expand_all()
499
 
500
 
501
    def gui_viewmenu_filter(self, *args):
502
        entry = self.srvgui.get_widget('gtk_entry_filter_view')
503
        filter = entry.get_text()
504
        viewmenu = self.srvgui.get_widget('viewmenu')
505
        selection = viewmenu.get_selection()
506
 
507
        def gui_iterate_over_data(model, path, itr):
508
            rowkey = model.get(itr, 0)[0]
509
            rowtype, rowval = rowkey.split('@')
510
            dsc = model.get(itr, 1)[0]
511
            contents = model.get(itr, 1)[0]
512
            cleanstr = contents.replace('<b>', '')
513
            cleanstr = cleanstr.replace('</b>', '')
514
            model.set(itr, 1, '%s' % cleanstr)
515
            viewmenu.collapse_row(path)
516
 
517
            if len(filter) > 0:
518
                if filter.upper() in rowval.upper() or filter.upper() in dsc.upper():
519
                    viewmenu.expand_to_path (path)
520
                    selection.select_path(path)
521
                    model.set(itr, 1, '<b>%s</b>' % contents)
522
            else:
523
                return
524
 
525
        model = viewmenu.get_model()
526
        model.foreach(gui_iterate_over_data)
527
 
528
 
529
    def gui_viewmenu_select_first_entry(self):
530
        viewmenu = self.srvgui.get_widget('viewmenu')
531
        selection = viewmenu.get_selection()
532
        selection.select_path("0")
533
 
534
 
535
    def gui_filter_visor(self, entry):
536
        page = self.srvgui.get_key_value('current_visor_tab')
537
        if page == 0:
538
            visor = self.srvgui.get_widget('visor_sapnotes')
539
            visible_filter = visor.get_visible_filter()
540
            visible_filter.refilter()
541
            visor.update_total_sapnotes_count()
542
        elif page == 1:
543
            visor = self.srvgui.get_widget('visor_annotations')
544
            visor.populate_annotations()
545
            visible_filter = visor.get_visible_filter()
546
            visible_filter.refilter()
547
            visor.update_total_annotations_count()
548
 
549
 
550
    def gui_refresh_view(self, button=None, view=None):
551
        window = self.srvgui.get_widget('gtk_app_window_main')
552
        viewmenu = self.srvgui.get_widget('viewmenu')
553
        if view is None:
554
            view = viewmenu.get_view()
555
 
556
        if view is not None:
557
            viewlabel = self.srvgui.get_widget('gtk_label_current_view')
558
            name = "<b>%-10s</b>" % view.capitalize()
559
            viewlabel.set_markup(name)
560
        viewmenu.set_view(view)
561
        popover = self.srvgui.get_widget('gtk_popover_button_menu_views')
562
        popover.hide()
563
        self.gui_show_visor_sapnotes()
564
 
565
 
566
    def gui_toggle_menu_view(self, obj):
567
        paned = self.srvgui.get_widget('gtk_vbox_container_menu_view')
568
        button = self.srvgui.get_widget('gtk_toogletoolbutton_menu_view')
569
        if isinstance(obj, Gtk.ToggleToolButton):
570
            if button.get_active():
571
                paned.show_all()
572
            else:
573
                paned.hide()
574
        elif isinstance(obj, bool):
575
            if obj == True:
576
                button.set_active(True)
577
            else:
578
                button.set_active(False)
579
 
580
 
581
    def gui_toggle_fullscreen(self, button):
582
        icon_container = self.srvgui.get_widget('gtk_box_container_icon_fullscreen')
583
        icon_fullscreen = self.srvicm.get_new_image_icon('basico-fullscreen', 24, 24)
584
        icon_unfullscreen = self.srvicm.get_new_image_icon('basico-unfullscreen', 24, 24)
585
        active = button.get_active()
586
        window = self.srvgui.get_window()
587
        if active:
588
            self.srvgui.swap_widget(icon_container, icon_unfullscreen)
589
            window.fullscreen()
590
        else:
591
            self.srvgui.swap_widget(icon_container, icon_fullscreen)
592
            window.unfullscreen()
593
 
594
 
595
    def action_annotation_edit(self, aid):
596
        self.gui_annotation_widget_show(None, aid, 'edit')
597
 
598
 
599
    def action_annotation_duplicate(self, *args):
600
        self.log.debug("ACTION-DUPLICATE: %s" % args)
601
 
602
 
603
    def action_annotation_delete(self, *args):
604
        visor = self.srvgui.get_widget('visor_annotations')
605
        widget_annotation = self.srvgui.get_widget('widget_annotation')
606
        aid = widget_annotation.get_aid_from_widget()
607
 
608
        answer = self.srvuif.warning_message_delete_annotations(None, 'Deleting annotations', 'Are you sure?', [aid])
609
        if answer is True:
610
            title = self.srvant.get_title(aid)
611
            self.srvant.delete(aid)
612
            self.gui_annotation_widget_clear()
613
            self.srvuif.set_widget_visibility('gtk_vbox_container_annotations', False)
614
            visor.populate_annotations()
615
            self.srvuif.statusbar_msg("Annotation <i>'%s'</i> deleted" % title, True)
616
        else:
617
            self.log.info("Annotation %s hasn't been deleted" % title)
618
            self.srvuif.statusbar_msg("Action canceled. Nothing deleted.", True)
619
 
620
        self.srvuif.grab_focus()
621
 
622
 
623
    def action_annotation_accept(self, button, sid):
624
        widget_annotation = self.srvgui.get_widget('widget_annotation')
625
        visor_annotations = self.srvgui.get_widget('visor_annotations')
626
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
627
        viewmenu = self.srvgui.get_widget('viewmenu')
628
 
629
        aid = widget_annotation.get_aid_from_widget()
351 t00mlabs 630
        annotation = widget_annotation.get_metadata_from_widget()
340 t00mlabs 631
 
632
        if self.srvant.is_valid(aid):
633
            self.srvant.update(annotation)
634
            title = self.srvant.get_title(aid)
635
            self.srvuif.statusbar_msg("Updated annotation: %s" % title, True)
636
        else:
637
            self.srvant.create(annotation)
638
            title = self.srvant.get_title(aid)
639
            self.srvuif.statusbar_msg('New annotation created: %s' % title, True)
640
        visor_annotations.populate_annotations()
641
        visor_sapnotes.populate_sapnotes()
642
        self.gui_annotation_widget_clear()
643
        self.srvuif.set_widget_visibility('gtk_vbox_container_annotations', False)
644
        self.srvuif.grab_focus()
645
 
646
 
647
    def action_annotation_cancel(self, *args):
648
        statusbar = self.srvgui.get_widget('widget_statusbar')
649
        self.gui_annotation_widget_clear()
650
        self.srvuif.set_widget_visibility('gtk_vbox_container_annotations', False)
350 t00mlabs 651
        button = self.srvgui.get_widget('gtk_togglebutton_maximize_annotation_widget')
652
        button.set_active(False)
340 t00mlabs 653
        self.log.debug('Annotation canceled')
654
        self.srvuif.statusbar_msg("Annotation canceled")
655
        self.srvuif.grab_focus()
656
 
657
 
658
    def get_sapnotes_from_current_view(self, colname=None):
659
        viewmenu = self.srvgui.get_widget('viewmenu')
660
        view = viewmenu.get_view()
661
        rowtype = viewmenu.get_row_type()
662
        rowid = viewmenu.get_row_id()
663
 
664
        if view == 'collection':
665
            cols = self.srvclt.get_collections_by_row_title(colname)
666
            bag = []
667
            for colid in cols:
668
                bag.extend(self.srvdtb.get_notes_by_node('collection', colid))
669
        else:
670
            bag = self.srvdtb.get_notes_by_node(rowtype, rowid)
671
 
672
        return bag
673
 
674
 
675
    def action_collection_export_text_csv(self, *args):
676
        rootwin = self.srvgui.get_window()
677
        timestamp = self.srvutl.timestamp()
678
        filename = "%s%s.csv" % (LPATH['EXPORT'], timestamp)
679
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
680
 
681
        bag = visor_sapnotes.get_filtered_bag()
682
 
683
        dialog = Gtk.FileChooserDialog("Save file", rootwin,
684
            Gtk.FileChooserAction.SAVE,
685
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
686
                 Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
687
        dialog.set_filename(filename)
688
        dialog.set_current_name(os.path.basename(filename))
689
        response = dialog.run()
690
 
691
        if response == Gtk.ResponseType.OK:
692
            export_path = dialog.get_filename()
693
            res = self.srvbnr.export_to_text_csv(bag, export_path)
694
            self.log.info("%d SAP Notes exported to CSV format: %s", len(bag), export_path)
695
            self.srvuif.statusbar_msg("%d SAP Notes exported to CSV format: %s" % (len(bag), export_path), True)
696
            self.srvuif.copy_text_to_clipboard(export_path)
697
        else:
698
            self.log.info("Export canceled by user")
699
            self.srvuif.statusbar_msg("Export canceled by user", True)
700
        dialog.destroy()
701
 
702
 
703
    def action_collection_export_excel(self, *args):
704
        rootwin = self.srvgui.get_window()
705
        timestamp = self.srvutl.timestamp()
706
        filename = "%s%s.xlsx" % (LPATH['EXPORT'], timestamp)
707
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
708
 
709
        bag = visor_sapnotes.get_filtered_bag()
710
 
711
        dialog = Gtk.FileChooserDialog("Save file", rootwin,
712
            Gtk.FileChooserAction.SAVE,
713
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
714
                 Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
715
        dialog.set_filename(filename)
716
        dialog.set_current_name(os.path.basename(filename))
717
        response = dialog.run()
718
 
719
        if response == Gtk.ResponseType.OK:
720
            export_path = dialog.get_filename()
721
            res = self.srvbnr.export_to_excel(bag, export_path)
722
            if res:
723
                self.log.info("Selected SAP Notes exported to MS Excel format (xlsx): %s" % export_path)
724
                self.srvuif.statusbar_msg("%d SAP Notes exported to MS Excel format: %s" % (len(bag), export_path), True)
725
                self.srvuif.copy_text_to_clipboard(export_path)
726
            else:
727
                self.log.error(self.get_traceback())
728
        else:
729
            self.log.info("Export canceled by user")
730
            self.srvuif.statusbar_msg("Export canceled by user", True)
731
        dialog.destroy()
732
 
733
 
734
    def action_collection_export_basico(self, *args):
735
        rootwin = self.srvgui.get_window()
736
        timestamp = self.srvutl.timestamp()
737
        filename = "%s%s.bco" % (LPATH['EXPORT'], timestamp)
738
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
739
 
740
        bag = visor_sapnotes.get_filtered_bag()
741
 
742
        dialog = Gtk.FileChooserDialog("Save file", rootwin,
743
            Gtk.FileChooserAction.SAVE,
744
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
745
                 Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
746
        dialog.set_filename(filename)
747
        dialog.set_current_name(os.path.basename(filename))
748
        response = dialog.run()
749
 
750
        if response == Gtk.ResponseType.OK:
751
            export_path = dialog.get_filename()
752
            target = self.srvbnr.export_to_basico(bag, export_path)
753
            self.log.info("%d SAP Notes exported to Basico %s format: %s", len(bag), APP['version'], target)
754
            self.srvuif.statusbar_msg("%d SAP Notes exported to Basico %s format: %s" % (len(bag), APP['version'], target), True)
755
            self.srvuif.copy_text_to_clipboard(export_path)
756
        else:
757
            self.log.info("Export canceled by user")
758
            self.srvuif.statusbar_msg("Export canceled by user", True)
759
        dialog.destroy()
760
 
761
 
762
    def action_collection_copy_to_clipboard(self, button):
763
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
764
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
765
 
766
        bag = visor_sapnotes.get_filtered_bag()
767
        text = ''
768
        for sid in bag:
769
            metadata = self.srvdtb.get_sapnote_metadata(sid)
770
            text += "SAP Note %s: %s - Component: %s\n" % (str(int(sid)), metadata['title'], metadata['componentkey'])
771
        clipboard.set_text(text, -1)
772
 
773
        msg = "%d SAP Notes copied to the clipboard: %s" % (len(bag), ', '.join(list(bag)))
774
        self.log.info(msg)
775
        msg = "%d SAP Notes copied to the clipboard" % len(bag)
776
        self.srvuif.statusbar_msg(msg, True)
777
 
778
 
779
    def gui_annotation_widget_clear(self):
780
        a_wdg_timestamp = self.srvgui.get_widget('gtk_label_human_timestamp')
354 t00mlabs 781
        a_wdg_timestamp_created = self.srvgui.get_widget('gtk_label_timestamp_created')
340 t00mlabs 782
        a_wdg_title = self.srvgui.get_widget('gtk_entry_annotation_title')
783
        a_wdg_type = self.srvgui.get_widget('gtk_combobox_annotation_type')
784
        a_wdg_text = self.srvgui.get_widget('gtk_textview_annotation_text')
785
        a_wdg_link = self.srvgui.get_widget('gtk_entry_annotation_link')
786
        a_wdg_link_button = self.srvgui.get_widget('gtk_link_button_annotation_link')
787
        a_wdg_link_type = self.srvgui.get_widget('gtk_combobox_annotation_link_type')
788
 
789
        a_wdg_timestamp.set_text('')
354 t00mlabs 790
        a_wdg_timestamp_created.set_text('')
340 t00mlabs 791
        a_wdg_title.set_text('')
792
        textbuffer = a_wdg_text.get_buffer()
793
        textbuffer.set_text('')
794
        a_wdg_link.set_text('')
795
        a_wdg_link_button.set_uri('')
796
        self.gui_annotation_widget_hide()
797
 
798
 
799
    def gui_annotation_widget_hide(self):
800
        self.srvuif.set_widget_visibility('gtk_vbox_container_annotations', False)
801
 
802
 
803
    def gui_copy_to_clipboard_sapnote(self, widget, lsid, popover):
804
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
805
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
806
        text = ''
807
        for sid in lsid:
808
            metadata = self.srvdtb.get_sapnote_metadata(sid)
809
            text += "SAP Note %10s: %s - Component: %s\n" % (sid, metadata['title'], metadata['componentkey'])
810
        clipboard.set_text(text, -1)
811
        if popover is not None:
812
            popover.hide()
813
            self.srvuif.grab_focus()
814
 
815
 
816
    def gui_jump_to_sapnote(self, widget, sid):
817
        visor_sapnotes = self.srvgui.get_widget('visor_sapnotes')
818
        visor_sapnotes.populate_sapnotes([sid])
819
        self.gui_show_visor_sapnotes()
820
        self.srvuif.grab_focus()
821
        msg = "Jumping to SAP Note %s" % sid
822
        self.log.info(msg)
823
        self.srvuif.statusbar_msg(msg)
824
 
825
 
826
    def gui_link_to_sapnote(self, *args):
827
        pass
828
 
829
    def gui_switch_selection_atypes(self, switch, state):
830
        label = self.srvgui.get_widget('gtk_label_switch_select_atypes')
831
        switched = switch.get_active()
832
        switch.set_state(switched)
833
        if switched is True:
834
            label.set_text ("All selected")
835
 
836
        else:
837
            label.set_text("None selected")
838
 
839
        for name in ATYPES:
840
            button = self.srvgui.get_widget('gtk_button_type_%s' % name.lower())
841
            button.set_state(switched)
842
            button.set_active(switched)
843
 
844
 
845
    def gui_sapnotes_select_all_none(self, witch, state):
846
        visor = self.srvgui.get_widget('visor_sapnotes')
847
        model = visor.get_model()
848
 
849
        def get_selected_sapnotes(model, path, itr):
850
            component = model.get(itr, 5)
851
            if component != 'Annotation':
852
                model.set(itr, 2, state)
853
 
854
        model.foreach(get_selected_sapnotes)
348 t00mlabs 855
 
856
 
857
    def gui_maximize_annotation_window(self, *args):
352 t00mlabs 858
        vpaned = self.srvgui.get_widget('gtk_vpaned_visor')
348 t00mlabs 859
        stack_main = self.srvgui.get_widget('gtk_stack_main')
860
        toggle_button = self.srvgui.get_widget('gtk_togglebutton_maximize_annotation_widget')
861
        if toggle_button.get_active():
352 t00mlabs 862
            # ~ stack_main.hide()
863
            vpaned.set_position(0)
348 t00mlabs 864
        else:
352 t00mlabs 865
            # ~ stack_main.show_all()
866
            vpaned.set_position(600)
356 t00mlabs 867
 
868
 
869
    def gui_attachment_add(self, *args):
358 t00mlabs 870
        visor = self.srvgui.get_widget('visor_attachments')
356 t00mlabs 871
        # Get Annotation ID (AID) from widget, if any
872
        widget_annotation = self.srvgui.get_widget('widget_annotation')
873
        aid = widget_annotation.get_aid_from_widget()
874
 
875
        # Get attachments fro filechooser dialog
876
        attachments = self.gui_attachment_show_filechooser()
877
 
878
        # Add them to Basico database
879
        if attachments is not None:
880
            for attachment in attachments:
881
                # only allow files (avoid directories)
882
                if os.path.isfile(attachment):
883
                    self.srvatc.create(attachment, aid)
358 t00mlabs 884
            visor.populate_attachments()
356 t00mlabs 885
        else:
886
            self.log.warning("No files selected to attach")
887
 
888
    def gui_attachment_show_filechooser(self):
889
        filenames = None
890
        parentwin = self.srvgui.get_window()
891
        dialog = Gtk.FileChooserDialog(title="Open file(s) ...",
892
                                       parent=parentwin,
893
                                       action=Gtk.FileChooserAction.OPEN,
894
                                       buttons=("_Cancel",
895
                                                Gtk.ResponseType.CANCEL,
896
                                        "_Open", Gtk.ResponseType.ACCEPT))
897
        dialog.set_select_multiple(True)
898
        response = dialog.run()
899
        if response == Gtk.ResponseType.ACCEPT:
900
            filenames = dialog.get_filenames()
901
            i = 0
902
            while i < len(filenames):
903
                filename = filenames[i]
904
                self.log.debug("%s was selected", filename)
905
                i += 1
906
        dialog.destroy()
907
        return filenames
908