Subversion Repositories basico

Rev

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

Rev Author Line No. Line
141 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: sapnoteview.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Workplace service
7
 
8
import json
9
 
10
import gi
11
gi.require_version('Gdk', '3.0')
12
gi.require_version('Gtk', '3.0')
13
from gi.repository import Gtk
14
from gi.repository import Gdk
15
from gi.repository import Pango
16
 
144 t00mlabs 17
from .widgets import ListReview
141 t00mlabs 18
from .service import Service
19
 
20
 
21
class LaunchpadDialog(Gtk.VBox, Service):
22
    def __init__(self, app):
144 t00mlabs 23
        Gtk.VBox.__init__(self)
141 t00mlabs 24
        self.app = app
144 t00mlabs 25
        self.log = app.log
141 t00mlabs 26
        self.get_services()
27
        self.setup()
28
 
29
 
30
    def setup(self):
144 t00mlabs 31
        self.statusbar = self.gui.get_widget('statusbar')
141 t00mlabs 32
        menuview = self.gui.get_widget('tgbShowMenuView')
33
        menuview.set_active(False)
34
        self.set_property('margin', 6)
35
        self.set_spacing(6)
36
 
144 t00mlabs 37
        self.vbox = Gtk.VBox()
38
        self.vbox.set_vexpand(True)
39
        self.vbox.set_hexpand(True)
40
        self.pack_start(self.vbox, True, True, 0)
141 t00mlabs 41
 
42
        # Label
43
        title = Gtk.Label()
44
        title.set_markup('<big><b>Import from Launchpad</b></big>')
45
        title.modify_font(Pango.FontDescription('Arial 24'))
144 t00mlabs 46
        self.vbox.pack_start(title, False, True, 6)
141 t00mlabs 47
 
48
        # Instructions
49
        label = Gtk.Label()
50
        message =   "Write down the list of SAP Notes that you want to download\nThey can be separated by newline or by commas"
51
        label.set_markup('<big>%s</big>' % message)
52
        label.set_justify(Gtk.Justification.CENTER)
53
        label.modify_font(Pango.FontDescription('Arial 12'))
144 t00mlabs 54
        self.vbox.pack_start(label, False, True, 6)
141 t00mlabs 55
 
144 t00mlabs 56
        # Text Box
57
        # ~ self.revealer = Gtk.Revealer()
58
        self.textbox = Gtk.HBox()
59
        # ~ self.entry = Gtk.Entry()
60
        # ~ self.textbox.pack_start(self.entry, True, True, 6)
141 t00mlabs 61
        scrolledwindow = Gtk.ScrolledWindow()
62
        scrolledwindow.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
63
        scrolledwindow.set_shadow_type(Gtk.ShadowType.IN)
64
        scrolledwindow.set_hexpand(True)
65
        scrolledwindow.set_vexpand(True)
66
        textview = Gtk.TextView()
67
        self.textbuffer = textview.get_buffer()
68
        self.textbuffer.set_text("")
69
        scrolledwindow.add(textview)
144 t00mlabs 70
        self.textbox.add(scrolledwindow)
71
        self.vbox.pack_start(self.textbox, False, True, 0)
141 t00mlabs 72
 
144 t00mlabs 73
        # Review Box
74
        self.review = ListReview(self.app)
75
        self.debug(self.review)
76
        self.vbox.pack_start(self.review, True, True, 6)
141 t00mlabs 77
 
144 t00mlabs 78
        # Button box
141 t00mlabs 79
        buttonbox = Gtk.ButtonBox() #.new(Gtk.Orientation.HORIZONTAL)
80
        buttonbox.set_hexpand(False)
81
        buttonbox.set_vexpand(False)
82
        buttonbox.set_layout(Gtk.ButtonBoxStyle.EDGE )
83
 
144 t00mlabs 84
        ## Cancel Button
141 t00mlabs 85
        button = Gtk.Button('Cancel')
86
        button.set_can_focus(False)
87
        icon = self.im.get_new_image_icon('gtk-cancel', 24, 24)
88
        button.set_image(icon)
89
        button.set_always_show_image(True)
90
        button.set_relief(Gtk.ReliefStyle.NONE)
91
        button.connect("clicked", self.cancel)
92
        buttonbox.pack_start(button, False, False, 0)
93
 
144 t00mlabs 94
        ## Review Button
95
        button = Gtk.Button('Cancel')
96
        button.set_can_focus(False)
97
        icon = self.im.get_new_image_icon('gtk-cancel', 24, 24)
98
        button.set_image(icon)
99
        button.set_always_show_image(True)
100
        button.set_relief(Gtk.ReliefStyle.NONE)
101
        button.connect("clicked", self.cancel)
102
        buttonbox.pack_start(button, False, False, 0)
103
 
104
        ## Accept Button
141 t00mlabs 105
        button = Gtk.Button('Accept')
106
        button.set_can_focus(False)
107
        icon = self.im.get_new_image_icon('gtk-apply', 24, 24)
108
        button.set_image(icon)
109
        button.set_always_show_image(True)
110
        button.set_relief(Gtk.ReliefStyle.NONE)
111
        button.connect("clicked", self.accept)
112
        buttonbox.pack_start(button, True, True, 0)
144 t00mlabs 113
        self.vbox.pack_start(buttonbox, False, True, 6)
141 t00mlabs 114
 
115
        self.show_all()
116
 
117
 
118
    def get_services(self):
119
        self.im = self.app.get_service('IM')
120
        self.gui = self.app.get_service('GUI')
121
        self.cb = self.app.get_service('Callbacks')
122
 
123
 
124
    def accept(self, *args):
144 t00mlabs 125
        bag = set()
126
        sapnotes = []
127
 
141 t00mlabs 128
        lines = self.get_buffer()
144 t00mlabs 129
        lines = lines.replace(' ', ',')
130
        lines = lines.replace('\n', ',')
131
        sapnotes.extend(lines.split(','))
132
        for sapnote in sapnotes:
133
            if len(sapnote.strip()) > 0:
134
                bag.add(sapnote.strip())
135
        self.debug("Bag Set: %s" % bag)
136
        lbag = list(bag)
137
        lbag.sort()
138
        self.textbox.hide()
139
        self.vbox.set_vexpand(True)
140
        self.review.fill(lbag)
141 t00mlabs 141
 
144 t00mlabs 142
        for sid in lbag:
143
            result = self.cb.import_notes_from_sapnet([sid])
144
            if result[sid]:
145
                self.statusbar.message("SAP Note %s downloaded successfully" % sid)
146
            self.debug(result)
141 t00mlabs 147
 
144 t00mlabs 148
 
141 t00mlabs 149
    def cancel(self, *args):
150
        self.cb.show_about()
151
 
152
 
153
    def get_buffer(self):
154
        istart, iend = self.textbuffer.get_bounds()
155
        return self.textbuffer.get_text(istart, iend, False)
156
 
157
 
158
 
159
class FileChooserDialog(Gtk.VBox, Service):
160
    def __init__(self, app):
161
        Gtk.VBox.__init__(self, app)
162
        self.app = app
163
        self.log = self.app.log
164
        self.get_services()
165
        self.setup()
166
 
167
 
168
    def get_services(self):
169
        self.im = self.app.get_service('IM')
170
        self.gui = self.app.get_service('GUI')
171
        self.db = self.app.get_service('DB')
142 t00mlabs 172
        self.uif = self.app.get_service('UIF')
141 t00mlabs 173
 
174
 
175
    def setup(self):
176
        self.set_property('margin', 6)
177
        self.set_spacing(6)
178
 
179
        box = Gtk.VBox()
180
        self.fcw = Gtk.FileChooserWidget()
181
 
182
        # Add filters
183
        #~ filter_any = Gtk.FileFilter()
184
        #~ filter_any.set_name("Any file")
185
        #~ filter_any.add_pattern("*")
186
        #~ fcw.add_filter(filter_any)
187
        filter_json = Gtk.FileFilter()
188
        filter_json.set_name("JSON files")
189
        filter_json.add_mime_type("application/xml")
190
        filter_json.add_mime_type("application/json")
191
        filter_json.add_mime_type("text/json")
192
        filter_json.add_mime_type("application/x-javascript")
193
        filter_json.add_mime_type("text/x-javascript")
194
        filter_json.add_mime_type("application/x-json")
195
        filter_json.add_mime_type("text/x-json")
196
        self.fcw.add_filter(filter_json)
197
        #~ self.debug(dir(fcw))
198
        box.pack_start(self.fcw, True, True, 0)
199
 
200
        buttonbox = Gtk.ButtonBox() #.new(Gtk.Orientation.HORIZONTAL)
201
        buttonbox.set_hexpand(False)
202
        buttonbox.set_vexpand(False)
203
        buttonbox.set_layout(Gtk.ButtonBoxStyle.EDGE )
204
 
205
        button = Gtk.Button('Cancel')
206
        button.set_can_focus(False)
207
        icon = self.im.get_new_image_icon('gtk-cancel', 24, 24)
208
        button.set_image(icon)
209
        button.set_always_show_image(True)
210
        button.set_relief(Gtk.ReliefStyle.NONE)
211
        button.connect("clicked", self.cancel)
212
        buttonbox.pack_start(button, False, False, 0)
213
 
214
        button = Gtk.Button('Accept')
215
        button.set_can_focus(False)
216
        icon = self.im.get_new_image_icon('gtk-apply', 24, 24)
217
        button.set_image(icon)
218
        button.set_always_show_image(True)
219
        button.set_relief(Gtk.ReliefStyle.NONE)
220
        button.connect("clicked", self.accept)
221
        buttonbox.pack_start(button, True, True, 0)
222
        box.pack_start(buttonbox, False, True, 6)
223
 
224
        self.add(box)
225
 
226
 
227
    def accept(self, *args):
228
        filename = self.fcw.get_filename()
229
        self.debug(filename)
230
 
231
        with open(filename, 'r') as fp:
232
            try:
233
                bag = json.load(fp)
234
                self.db.import_sapnotes(bag)
235
                self.debug ("Imported %d notes from %s" % (len(bag), filename))
236
                visor = self.gui.get_widget('visor')
237
                visor.populate(bag)
238
                self.db.save_notes()
142 t00mlabs 239
                self.uif.statusbar_msg("<b>Imported %d notes from %s</b>" % (len(bag), filename))
141 t00mlabs 240
            except:
241
                self.debug("Error importing JSON contents")
242
 
243
 
244
    def cancel(self, *args):
245
        self.cb.show_about()