Subversion Repositories basico

Rev

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