Subversion Repositories basico

Rev

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

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: tasks.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Tasks service
7
 
74 t00mlabs 8
import os
4 t00mlabs 9
from gi.repository import Gtk
10
from gi.repository.GdkPixbuf import Pixbuf
11
from gi.repository import Pango
12
from datetime import datetime
13
from dateutil import parser as dateparser
14
 
15
from .service import Service
16
 
17
 
18
class Tasks(Service):
19
    def initialize(self):
20
        self.get_services()
74 t00mlabs 21
        self.setup_widgets()
22
        self.__init_tasks_store()
23
        self.load_tasks()
4 t00mlabs 24
 
25
 
74 t00mlabs 26
    def __init_tasks_store(self):
27
        TASKS_FILE = self.get_file('TASKS')
28
        if not os.path.exists(TASKS_FILE):
29
            self.log.info("Creating store for tasks: %s" % TASKS_FILE)
30
            ftasks = open(TASKS_FILE, 'w')
31
            ftasks.close()
32
 
33
 
34
    def setup_widgets(self):
4 t00mlabs 35
        # setup widgets
74 t00mlabs 36
        self.window = self.gui.add_widget('boxProps')
4 t00mlabs 37
        self.pname = self.gui.add_widget('etyTaskName')
38
        self.pname.connect('activate', self.add_task)
39
        self.boxtask = self.gui.add_widget('boxTasks')
40
        self.btnadd = self.gui.add_widget('btnAddTask')
41
        self.btnadd.connect('clicked', self.add_task)
42
        self.btndel = self.gui.add_widget('btnDelTask')
43
        self.btndel.connect('clicked', self.delete_task)
44
        self.treeview = Gtk.TreeView()
45
        self.gui.add_widget('trvtaskwin', self.treeview)
74 t00mlabs 46
        self.gui.add_widget('boxAcceptCancelTasks')
4 t00mlabs 47
        self.boxtask.add(self.treeview)
48
 
49
        # setup model
50
        model = Gtk.ListStore(
51
            bool,           # CheckBox
52
            str             # Task name
53
        )
54
        self.treeview.set_model(model)
55
 
56
        # setup columns
57
        # Checkbox
58
        renderer = Gtk.CellRendererToggle()
59
        renderer.connect("toggled", self.on_cell_toggled)
74 t00mlabs 60
        column = Gtk.TreeViewColumn('', renderer, active=0)
4 t00mlabs 61
        column.set_visible(True)
62
        column.set_expand(False)
63
        column.set_clickable(False)
64
        column.set_sort_indicator(False)
65
        self.treeview.append_column(column)
66
 
67
        # Task name
68
        renderer = Gtk.CellRendererText()
69
        column = Gtk.TreeViewColumn('Task name', renderer, text=1)
70
        column.set_visible(True)
74 t00mlabs 71
        column.set_sort_column_id(1)
4 t00mlabs 72
        column.set_expand(True)
73
        column.set_clickable(True)
74
        column.set_sort_indicator(True)
75
        self.treeview.append_column(column)
76
 
77
        # Filters
78
        #Creating the filter, feeding it with the liststore model
79
        #~ self.task_filter = model.filter_new()
80
        #setting the filter function, note that we're not using the
81
        #~ self.task_filter.set_visible_func(self.task_filter_func)
82
        #~ self.task_filter.set_visible_column(1)
83
 
84
        # Treeview features
85
        self.treeview.set_headers_visible(True)
86
        self.treeview.set_enable_search(True)
74 t00mlabs 87
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.NONE)
4 t00mlabs 88
        self.treeview.set_search_column(1)
89
        self.treeview.connect('row-activated', self.double_click)
90
        selection = self.treeview.get_selection()
91
        selection.set_mode(Gtk.SelectionMode.SINGLE)
92
        self.treeview.set_search_entry(self.pname)
93
 
94
 
74 t00mlabs 95
    def clear(self):
96
        model = self.treeview.get_model()
97
        model.clear()
98
 
99
 
100
    def set_model(self, model):
101
        self.treeview.set_model(model)
102
 
103
 
4 t00mlabs 104
    # row task visibility
105
    def task_row_visibility(self, model, iter, data):
106
        entry = self.gui.get_widget('etyTaskName')
107
        text = entry.get_text()
108
 
109
        return text.upper() in model.get_value(iter, 1)
110
 
111
 
112
    def task_filter_func(self, model, iter, data):
113
        """Tests if the task in the row is the one in the filter"""
114
        entry = self.gui.get_widget('etyTaskName')
115
        task = entry.get_text()
116
        task_row = model[iter][1]
117
 
118
        if task.upper() in task_row.upper():
119
            return True
120
        else:
121
            return False
122
 
123
 
124
    def delete_task(self, *args):
74 t00mlabs 125
        db = self.app.get_service('DB')
4 t00mlabs 126
        found = 0
127
        selection = self.treeview.get_selection()
128
        result = selection.get_selected()
129
        if result: #result could be None
130
            model, iter = result
131
            task = model.get_value(iter, 1)
74 t00mlabs 132
            sapnotes = db.get_notes()
4 t00mlabs 133
            for sapnote in sapnotes:
134
                try:
135
                    tasks = sapnotes[sapnote]['tasks']
136
                    if task in tasks:
137
                        found += 1
138
                except:
139
                    pass
140
            if found > 1:
141
                self.log.warning("Task %s is still assigned to other SAP Notes" % task)
142
                head = "Task could not be deleted"
143
                body = "Task %s is still assigned to other SAP Notes" % task
144
                self.uif.message_dialog(head , body)
145
            else:
146
                model.remove(iter)
74 t00mlabs 147
                self.log.info("Task deleted: %s" % task)
148
                self.save_tasks()
4 t00mlabs 149
 
150
 
151
 
152
    def add_task(self, *args):
153
        task = self.pname.get_text()
154
        model = self.treeview.get_model()
155
 
156
        found = False
157
        for row in model:
158
            if row[1].upper() == task.upper():
74 t00mlabs 159
                self.log.info("Task already exists: %s" % task)
4 t00mlabs 160
                found = True
161
 
162
        if not found and len(task) > 0:
163
            model.append([False, task])
74 t00mlabs 164
            self.log.info("Task added: %s" % task)
4 t00mlabs 165
 
166
        self.pname.set_text('')
74 t00mlabs 167
        self.save_tasks()
168
        self.load_tasks()
4 t00mlabs 169
 
170
 
171
 
74 t00mlabs 172
    def show_window(self, sapnotes=[], active=None):
173
        ntboperations = self.gui.get_widget('ntbOperations')
174
        ntboperations.set_current_page(1)
175
        winoper = self.gui.get_widget('tgbShowManage')
176
        switch = winoper.get_active()
177
        if active is None:
178
            if switch is True:
179
                active = True
180
            else:
181
                active = False
182
        winoper.set_active(active)
4 t00mlabs 183
        self.load_tasks(sapnotes)
184
 
185
 
186
    def get_services(self):
187
        self.gui = self.app.get_service("GUI")
188
        self.sap = self.app.get_service('SAP')
189
        self.uif = self.app.get_service('UIF')
190
        self.cb = self.app.get_service('Callbacks')
74 t00mlabs 191
        self.db = self.app.get_service('DB')
4 t00mlabs 192
 
193
 
194
    def double_click(self, treeview, row, col):
195
        model = treeview.get_model()
196
        self.pname.set_text(model[row][1])
197
 
198
 
199
    def populate(self, sapnotes):
200
        model = self.treeview.get_model()
201
        model.clear()
202
        #~ model.append([bool, str])
203
 
204
 
205
    def changed(self, *args):
206
        try:
207
            model, treeiters = self.selection.get_selected_rows()
208
            selected = set()
209
            if len(treeiters) > 0:
210
                for treeiter in treeiters:
211
                    if treeiter != None:
212
                        selected.add(model[treeiter][0])
213
            print (selected)
214
 
215
        except Exception as error:
216
            self.log.error (self.get_traceback())
217
 
218
 
74 t00mlabs 219
    def select(self, tasks):
220
        model = self.treeview.get_model()
221
 
222
        def select_task(model, path, iter, user_data=None):
223
            task = model.get_value(iter, 1)
224
            if task in tasks:
225
                model[path][0] = True
226
                self.log.debug("Task '%s' switch to true" % task)
227
            else:
228
                model[path][0] = False
229
            return False
230
 
231
        model.foreach(select_task)
232
 
233
 
4 t00mlabs 234
    def on_cell_toggled(self, widget, path):
74 t00mlabs 235
        cb = self.get_service('Callbacks')
4 t00mlabs 236
        model = self.treeview.get_model()
237
        model[path][0] = not model[path][0]
74 t00mlabs 238
        cb.check_task_link_button_status()
239
        #~ button = self.gui.get_widget('btnLinkTasksApply')
240
        #~ sapnoteview = self.gui.get_widget('sapnoteview')
4 t00mlabs 241
 
74 t00mlabs 242
        #~ tasks_selected = len(self.get_selected()) > 0
243
        #~ notes_selected = len(sapnoteview.get_selected_notes()) > 0
244
        #~ if  tasks_selected and notes_selected:
245
            #~ button.set_no_show_all(False)
246
            #~ button.show_all()
247
        #~ else:
248
            #~ button.hide()
249
            #~ button.set_no_show_all(True)
4 t00mlabs 250
 
251
 
252
 
74 t00mlabs 253
    def get_selected(self):
254
        model = self.treeview.get_model()
255
        tasks = set()
256
 
257
        def traverse_treeview(model, path, iter, user_data=None):
258
            toggled = model.get_value(iter, 0)
259
            if toggled:
260
                task = model.get_value(iter, 1)
261
                tasks.add(task)
262
            return False
263
 
264
        model.foreach(traverse_treeview)
265
        ltasks = list(tasks)
266
        ltasks.sort()
267
 
268
        #~ self.log.debug("Tasks selected: %s" % ', '.join(tasks))
269
        return ltasks
270
 
271
 
272
    #~ def get_selected_notes(self):
273
        #~ sapnoteview = self.gui.get_widget('sapnoteview')
274
        #~ return sapnoteview.get_selected_notes()
275
 
276
 
4 t00mlabs 277
    def link_to_task(self, *args):
74 t00mlabs 278
        sapnoteview = self.gui.get_widget('sapnoteview')
279
 
4 t00mlabs 280
        self.save_tasks()
74 t00mlabs 281
        sapnotes = list(sapnoteview.get_selected_notes())
4 t00mlabs 282
        model = self.treeview.get_model()
283
        tasks = []
284
        for row in model:
285
           link = row[0]
286
           if link == True:
287
                tasks.append(row[1])
288
 
289
        sapnotes.sort()
290
        tasks.sort()
291
        self.sap.link_to_task(sapnotes, tasks)
292
        self.cb.refresh_view(view='tasks')
293
 
294
 
295
    def get_all_tasks(self):
296
        tasks = []
297
        model = self.treeview.get_model()
298
        for row in model:
299
            tasks.append(row[1])
300
        tasks.sort()
301
 
302
        return tasks
303
 
304
 
305
    def load_tasks(self, sapnotes=[]):
306
        model = self.treeview.get_model()
74 t00mlabs 307
        TASKS_FILE = self.get_file('TASKS')
308
        alltasks = open(TASKS_FILE, 'r').readlines()
4 t00mlabs 309
        model.clear()
74 t00mlabs 310
        alltasks.sort()
4 t00mlabs 311
        for task in alltasks:
74 t00mlabs 312
            model.append([False, task[0:-1]])
4 t00mlabs 313
 
314
 
315
    def save_tasks(self):
74 t00mlabs 316
        TASKS_FILE = self.get_file('TASKS')
4 t00mlabs 317
        tasks = self.get_all_tasks()
318
 
74 t00mlabs 319
        ftasks = open(TASKS_FILE, 'w')
320
        for task in tasks:
321
            ftasks.write("%s\n" % task)
322
            self.log.info("Task saved: %s" % task)
323
        ftasks.close()
4 t00mlabs 324
 
325
 
74 t00mlabs 326
 
4 t00mlabs 327
    def save_tasks_from_stats(self, alltasks):
328
        settings = {}
74 t00mlabs 329
        tasks = ','.join(alltasks)
330
        settings['Tasks'] = tasks
4 t00mlabs 331
        self.config[self.section] = settings
332
        self.save_config()