Subversion Repositories basico

Rev

Rev 142 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: sapnoteview.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Workplace service

import json

import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango

from .widgets import ListReview
from .service import Service


class LaunchpadDialog(Gtk.VBox, Service):
    def __init__(self, app):
        Gtk.VBox.__init__(self)
        self.app = app
        self.log = app.log
        self.get_services()
        self.setup()


    def setup(self):
        self.statusbar = self.gui.get_widget('statusbar')
        menuview = self.gui.get_widget('tgbShowMenuView')
        menuview.set_active(False)
        self.set_property('margin', 6)
        self.set_spacing(6)

        self.vbox = Gtk.VBox()
        self.vbox.set_vexpand(True)
        self.vbox.set_hexpand(True)
        self.pack_start(self.vbox, True, True, 0)

        # Label
        title = Gtk.Label()
        title.set_markup('<big><b>Import from Launchpad</b></big>')
        title.modify_font(Pango.FontDescription('Arial 24'))
        self.vbox.pack_start(title, False, True, 6)

        # Instructions
        label = Gtk.Label()
        message =   "Write down the list of SAP Notes that you want to download\nThey can be separated by newline or by commas"
        label.set_markup('<big>%s</big>' % message)
        label.set_justify(Gtk.Justification.CENTER)
        label.modify_font(Pango.FontDescription('Arial 12'))
        self.vbox.pack_start(label, False, True, 6)

        # Text Box
        # ~ self.revealer = Gtk.Revealer()
        self.textbox = Gtk.HBox()
        # ~ self.entry = Gtk.Entry()
        # ~ self.textbox.pack_start(self.entry, True, True, 6)
        scrolledwindow = Gtk.ScrolledWindow()
        scrolledwindow.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        scrolledwindow.set_shadow_type(Gtk.ShadowType.IN)
        scrolledwindow.set_hexpand(True)
        scrolledwindow.set_vexpand(True)
        textview = Gtk.TextView()
        self.textbuffer = textview.get_buffer()
        self.textbuffer.set_text("")
        scrolledwindow.add(textview)
        self.textbox.add(scrolledwindow)
        self.vbox.pack_start(self.textbox, False, True, 0)

        # Review Box
        self.review = ListReview(self.app)
        self.debug(self.review)
        self.vbox.pack_start(self.review, True, True, 6)

        # Button box
        buttonbox = Gtk.ButtonBox() #.new(Gtk.Orientation.HORIZONTAL)
        buttonbox.set_hexpand(False)
        buttonbox.set_vexpand(False)
        buttonbox.set_layout(Gtk.ButtonBoxStyle.EDGE )

        ## Cancel Button
        button = Gtk.Button('Cancel')
        button.set_can_focus(False)
        icon = self.im.get_new_image_icon('gtk-cancel', 24, 24)
        button.set_image(icon)
        button.set_always_show_image(True)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect("clicked", self.cancel)
        buttonbox.pack_start(button, False, False, 0)

        ## Review Button
        button = Gtk.Button('Cancel')
        button.set_can_focus(False)
        icon = self.im.get_new_image_icon('gtk-cancel', 24, 24)
        button.set_image(icon)
        button.set_always_show_image(True)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect("clicked", self.cancel)
        buttonbox.pack_start(button, False, False, 0)

        ## Accept Button
        button = Gtk.Button('Accept')
        button.set_can_focus(False)
        icon = self.im.get_new_image_icon('gtk-apply', 24, 24)
        button.set_image(icon)
        button.set_always_show_image(True)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect("clicked", self.accept)
        buttonbox.pack_start(button, True, True, 0)
        self.vbox.pack_start(buttonbox, False, True, 6)

        self.show_all()


    def get_services(self):
        self.im = self.app.get_service('IM')
        self.gui = self.app.get_service('GUI')
        self.cb = self.app.get_service('Callbacks')


    def accept(self, *args):
        bag = set()
        sapnotes = []

        lines = self.get_buffer()
        lines = lines.replace(' ', ',')
        lines = lines.replace('\n', ',')
        sapnotes.extend(lines.split(','))
        for sapnote in sapnotes:
            if len(sapnote.strip()) > 0:
                bag.add(sapnote.strip())
        self.debug("Bag Set: %s" % bag)
        lbag = list(bag)
        lbag.sort()
        self.textbox.hide()
        self.vbox.set_vexpand(True)
        self.review.fill(lbag)

        for sid in lbag:
            result = self.cb.import_notes_from_sapnet([sid])
            if result[sid]:
                self.statusbar.message("SAP Note %s downloaded successfully" % sid)
            self.debug(result)


    def cancel(self, *args):
        self.cb.show_about()


    def get_buffer(self):
        istart, iend = self.textbuffer.get_bounds()
        return self.textbuffer.get_text(istart, iend, False)



class FileChooserDialog(Gtk.VBox, Service):
    def __init__(self, app):
        Gtk.VBox.__init__(self, app)
        self.app = app
        self.log = self.app.log
        self.get_services()
        self.setup()


    def get_services(self):
        self.im = self.app.get_service('IM')
        self.gui = self.app.get_service('GUI')
        self.db = self.app.get_service('DB')
        self.uif = self.app.get_service('UIF')


    def setup(self):
        self.set_property('margin', 6)
        self.set_spacing(6)

        box = Gtk.VBox()
        self.fcw = Gtk.FileChooserWidget()

        # Add filters
        #~ filter_any = Gtk.FileFilter()
        #~ filter_any.set_name("Any file")
        #~ filter_any.add_pattern("*")
        #~ fcw.add_filter(filter_any)
        filter_json = Gtk.FileFilter()
        filter_json.set_name("JSON files")
        filter_json.add_mime_type("application/xml")
        filter_json.add_mime_type("application/json")
        filter_json.add_mime_type("text/json")
        filter_json.add_mime_type("application/x-javascript")
        filter_json.add_mime_type("text/x-javascript")
        filter_json.add_mime_type("application/x-json")
        filter_json.add_mime_type("text/x-json")
        self.fcw.add_filter(filter_json)
        #~ self.debug(dir(fcw))
        box.pack_start(self.fcw, True, True, 0)

        buttonbox = Gtk.ButtonBox() #.new(Gtk.Orientation.HORIZONTAL)
        buttonbox.set_hexpand(False)
        buttonbox.set_vexpand(False)
        buttonbox.set_layout(Gtk.ButtonBoxStyle.EDGE )

        button = Gtk.Button('Cancel')
        button.set_can_focus(False)
        icon = self.im.get_new_image_icon('gtk-cancel', 24, 24)
        button.set_image(icon)
        button.set_always_show_image(True)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect("clicked", self.cancel)
        buttonbox.pack_start(button, False, False, 0)

        button = Gtk.Button('Accept')
        button.set_can_focus(False)
        icon = self.im.get_new_image_icon('gtk-apply', 24, 24)
        button.set_image(icon)
        button.set_always_show_image(True)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect("clicked", self.accept)
        buttonbox.pack_start(button, True, True, 0)
        box.pack_start(buttonbox, False, True, 6)

        self.add(box)


    def accept(self, *args):
        filename = self.fcw.get_filename()
        self.debug(filename)

        with open(filename, 'r') as fp:
            try:
                bag = json.load(fp)
                self.db.import_sapnotes(bag)
                self.debug ("Imported %d notes from %s" % (len(bag), filename))
                visor = self.gui.get_widget('visor')
                visor.populate(bag)
                self.db.save_notes()
                self.uif.statusbar_msg("<b>Imported %d notes from %s</b>" % (len(bag), filename))
            except:
                self.debug("Error importing JSON contents")


    def cancel(self, *args):
        self.cb.show_about()