Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/calendario.py
blob: b17abd558a0dacc87a2dd052dd1f81d09c72b20e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# -*- coding: utf-8 -*-
#
#Copyright (C) 2010, Yader Velasquez
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

from gettext import gettext as _
from fecha import FechaUnix, FechaNormal 
from archivo import crear_modelo, abrir_archivo, guardar_archivo, guardar_dato, borrar_dato, \
        comprobar_archivo, dict_meses, borrar_dict_meses, tareas_pendientes
import gtk
import pango
import logging

from sugar.activity import activity
from sugar.graphics.toolbutton import ToolButton

class CalendarioActivity(activity.Activity):
    '''the sugar class'''

    def __init__(self, handle, create_jobject=True):
        ''' init class''' 
        activity.Activity.__init__(self, handle, False)
        self.set_title(_('Calendario'))
        
        barra_nueva = gtk.Toolbar()
        boton_importar = ToolButton('document-generic')
        boton_calendario = ToolButton('computer')
        boton_importar.set_tooltip(_('Import ephemeris'))
        boton_calendario.set_tooltip(_('Calendar')) 
        boton_importar.connect('clicked', self._importar_cb)
        boton_calendario.connect('clicked', self._calendario_cb)
        barra_nueva.insert(boton_calendario, -1)
        barra_nueva.insert(boton_importar, -1)
        boton_importar.show()
        boton_calendario.show()
        barra_nueva.show()
        
        barra_herramientas = activity.ActivityToolbox(self)
        barra_herramientas.add_toolbar(_('Tools'), barra_nueva)
        self.set_toolbox(barra_herramientas)
        barra_herramientas.show()
        self.fecha_normal = FechaNormal()
        self._log = logging.getLogger('Log Calendario')
        self.path = self.get_activity_root()
        self.check_editar = False
	######################### set interface ########################

        #calendar
        self.calendario = gtk.Calendar()    
        self.fecha = _('Today ') + self.fecha_normal.fecha_actual()
        self.texto_fecha = gtk.Label(self.fecha)    
        self.lista_fecha = self.calendario.get_date() 
        #mark the calendar
        self.indice_dia = FechaUnix(self.lista_fecha)
        self.indice = self.indice_dia.fecha_unix()
        self.marcar_dia = self.indice_dia.indice_unix(self.indice)
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)        
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.mark_day(dia)
        
        #daily activities
        self.area_texto = gtk.ScrolledWindow() 
        self.area_texto.set_shadow_type(gtk.SHADOW_IN)
        self.area_texto.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
         

        self.lista_fecha_t = self.calendario.get_date()
        self.indice_dia_t = FechaUnix(self.lista_fecha_t)
        
        self.modelo = crear_modelo(self.indice_dia_t.fecha_unix(), self.path)
        self.actividades = gtk.TreeView(self.modelo)
        self.area_texto.add(self.actividades)
                
        #pending tasks
        self.area_tarea = gtk.ScrolledWindow()
        #self.area_tarea.set_shadow_type(ALGO)
        #self.area_tarea.set_sensitive(False)
        self.area_tarea.set_size_request(-1, 100)
        self.area_tarea.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        self.pendientes = gtk.TreeView(tareas_pendientes(self.path))
        self.area_tarea.add(self.pendientes)
        #expander pending task
        self.expandir_tareas = gtk.Expander(_('Pending Tasks'))
        self.expandir_tareas.add(self.area_tarea)

        #create columns
        self._crear_columna()


       
        #ephemeris 
        self.area_efem = gtk.ScrolledWindow()
        self.area_efem.set_shadow_type(gtk.SHADOW_IN)
        self.area_efem.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        self.buffer = gtk.TextBuffer()
        
        #ephemeris file
        if comprobar_archivo(self.path, 'efemeride'): 
            self.texto = abrir_archivo(self.path, 'efemeride')
            self._log.debug(self.texto)
            self._log.debug(self.fecha_normal.fecha_especial())
            if self.fecha_normal.fecha_especial() in self.texto:
                self.buffer.set_text(self.texto[self.fecha_normal.fecha_especial()])
                    
            else:
                self.buffer.set_text(_('There\'s not ephemeris'))
        else:
            self.buffer.set_text(_('There\'s not ephemeris imported'))
        self.efemeride = gtk.TextView(self.buffer) 
        self.efemeride.set_editable(False)
        self.efemeride.set_cursor_visible(False)
        self.area_efem.add(self.efemeride)
        self.expandir_efeme = gtk.Expander(_('Daily Ephemeris'))
        self.expandir_efeme.add(self.area_efem)

        #phrase of the week
        self.area_frase = gtk.ScrolledWindow()
        self.area_frase.set_shadow_type(gtk.SHADOW_IN)
        self.area_frase.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.buffer = gtk.TextBuffer()
        #phrase file
        if comprobar_archivo(self.path, 'frase'): 
            self.texto = abrir_archivo(self.path, 'frase')
            self._log.debug(self.texto)
            if self.fecha_normal.numero_semana() in self.texto:
                self.buffer.set_text(self.texto[self.fecha_normal.numero_semana()])
                    
            else:
                self.buffer.set_text(_('There\'s not phrases'))
        else:
            self.buffer.set_text(_('There\'s not phrases imported'))
        self.frases = gtk.TextView(self.buffer)
        self.frases.set_editable(False)
        self.frases.set_cursor_visible(False)
        self.area_frase.add(self.frases)
        self.area_frase.set_size_request(-1, 80)
        self.expandir_frases = gtk.Expander(_('Phrase of the week'))
        self.expandir_frases.add(self.area_frase)

        #text input
        self.area_entrada = gtk.ScrolledWindow()
        self.area_entrada.set_shadow_type(gtk.SHADOW_OUT)
        self.area_entrada.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.entrada = gtk.TextView()
        self.entrada.set_size_request(0, 80) 
        self.area_entrada.add(self.entrada)       

        #buttons
        self.boton1 = gtk.Button(_('Add new'))
        self.boton2 = gtk.Button(_('Remove'))
        self.boton3 = gtk.Button(_('Import'))
        self.boton4 = gtk.Button(_('Modify'))
              
        #frames
        self.marco_expandible = gtk.Frame(_('Notes'))
        self.marco_expandible_v = gtk.VBox()
        self.marco_expandible_v.pack_start(self.expandir_tareas, False)
        self.marco_expandible_v.pack_start(self.expandir_frases, False)
        self.marco_expandible_v.pack_start(self.expandir_efeme)
        self.marco_expandible.add(self.marco_expandible_v)
        self.marco_actividades = gtk.Frame(_('Activities:'))
        self.marco_actividades.add(self.area_texto)


        #combobox
        self.combo = gtk.combo_box_new_text()
        self.combo.set_size_request(180, -1)
        self.combo.append_text(_('Mathematics'))
        self.combo.append_text(_('Geography'))
        self.combo.append_text(_('Natural Science'))
        self.combo.append_text(_('Language'))
        self.combo.append_text(_('Without category'))
        self.combo.set_active(5) 
        
        #expander options
        self.expandir = gtk.Expander(_('Expand for options'))
        self.check_status = 0
        self.check = gtk.CheckButton(_('Important'))
        self.progre_status = 0
        self.progre = gtk.CheckButton(_('In Progress'))
        self.compl_status = 0
        self.compl = gtk.CheckButton(_('Completed'))
        self.compl.set_sensitive(False)
        self.expandir_h = gtk.HBox()
        self.expandir_h.pack_start(self.combo, False)
        self.expandir_h.pack_start(self.check, False)
        self.expandir_h.pack_start(self.progre, False)
        self.expandir_h.pack_start(self.compl, False)
        self.expandir.add(self.expandir_h)

        #file
        self.archivo = gtk.FileChooserWidget()
        self.archivo.set_current_folder('/media')
        self.combo_archivo = gtk.combo_box_new_text()
        self.combo_archivo.set_size_request(180, -1)
        self.combo_archivo.append_text(_('Ephemeris'))
        self.combo_archivo.append_text(_('Phrases'))
        self.combo_archivo.set_active(1)

        self.botones = gtk.HBox()
        self.botones.pack_start(self.combo_archivo, False)
        self.botones.pack_start(self.boton3)
        self.archivo.set_extra_widget(self.botones)

        #containers
        self.contenedor_h = gtk.HBox()
        self.contenedor_vd = gtk.VBox()
        self.contenedor_vi = gtk.VBox()
        self.sub_contenedor_h = gtk.HBox()
        self.contenedor_extendido = gtk.HBox()
        self.botones_contenedor = gtk.VBox()
       
        #callbacks
        self.calendario.connect('day_selected', self._dia_selec_cb)
        self.calendario.connect('next_month', self._sig_mes_cb)
        self.calendario.connect('prev_month', self._ant_mes_cb)
        self.calendario.connect('next_year', self._sig_ano_cb)
        self.calendario.connect('prev_year', self._ant_ano_cb)
        self.boton1.connect('clicked', self._nuevo_ingreso_cb)
        self.boton2.connect('clicked', self._borrar_fila_cb)
        self.boton3.connect('clicked', self._importar_archivo_cb)
        self.boton4.connect('clicked', self._editar_cb)
        self.check.connect('toggled', self._check_cb)
        self.progre.connect('toggled', self._progre_cb)
        self.compl.connect('toggled', self._compl_cb)

        ########################### Add Blocks ################################
        self.contenedor_h.pack_start(self.contenedor_vi, False, padding = 5) 
        self.contenedor_h.pack_start(self.contenedor_vd, padding = 5)

        #calendar
        self.contenedor_vi.pack_start(self.texto_fecha, False, padding = 5)
        self.contenedor_vi.pack_start(self.calendario, False, padding = 5)
        self.contenedor_vi.pack_start(self.marco_expandible, padding = 5)
        
        self.contenedor_vd.pack_start(self.marco_actividades)
        #self.contenedor_vd.pack_start(self.expandir_tareas, False)
        self.contenedor_vd.pack_start(self.contenedor_extendido, False)
        self.contenedor_extendido.pack_start(self.expandir)
        self.contenedor_vd.pack_start(self.sub_contenedor_h, False, padding = 5)
        
        self.sub_contenedor_h.pack_start(self.area_entrada)
        self.sub_contenedor_h.pack_start(self.botones_contenedor, False, padding = 3)
        self.botones_contenedor.pack_start(self.boton1)
        self.botones_contenedor.pack_start(self.boton4)
        self.botones_contenedor.pack_start(self.boton2)
        
        self.set_canvas(self.contenedor_h)
        self.contenedor_h.show_all()
        self.archivo.show_all()

    ############################# Callbacks and methods###########################
    def _check_cb(self, widget, data=None):
        '''return 1 if the checkbutton is now enable, else
        return 0'''
        if self.check_status is 1:
            self.check_status = 0
        else:
            self.check_status = 1

    def _progre_cb(self, widget, data=None):
        '''return 1 if the chckbutton is now enable, else
        return 0'''
        if self.progre_status is 1:
            self.progre_status = 0
        elif self.progre_status is 0:
            self.progre_status = 1
    
    def _compl_cb(self, widget, data=None):
            if self.progre_status is 2:
                self.progre_status = 0
            else:
                self.progre_status = 2
                self.desactivar_boton()
            
    def _dia_selec_cb(self, widget, data=None):
        '''when a date is selected'''
        self.lista_fecha = self.calendario.get_date()
        self.indice_dia = FechaUnix(self.lista_fecha)
        self.modelo = crear_modelo(self.indice_dia.fecha_unix(), self.path)
        self.actividades.set_model(self.modelo)
    
    def _sig_mes_cb(self, widget, data=None):
        '''when the calendar is changed to the next month'''
        #unmark last month days
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)      
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.unmark_day(dia)
        
        #mark next month
        if "12" in self.marcar_dia[2:4]:
            self.marcar_dia = int(self.marcar_dia) + 8900
        else:
            self.marcar_dia = int(self.marcar_dia) + 100
        self.marcar_dia = str(self.marcar_dia)
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)        
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.mark_day(dia)
     
    def _ant_mes_cb(self, widget, data=None):
        '''when the calendar is changed to the last month'''
        #unmark last month days
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)        
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.unmark_day(dia)

        #mark next month
        self._log.debug(self.marcar_dia[2:4])
        if "01" in self.marcar_dia[2:4]:
            self.marcar_dia = int(self.marcar_dia) - 8900
        else:
            self.marcar_dia = int(self.marcar_dia) - 100
        self.marcar_dia = str(self.marcar_dia)
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)        
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.mark_day(dia)

    def _sig_ano_cb(self, widget, data=None):
        '''next year in the calendar'''
        #unmark last year days
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)        
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.unmark_day(dia)

        self.marcar_dia = int(self.marcar_dia) + 10000
        self.marcar_dia = str(self.marcar_dia)
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.mark_day(dia)

    def _ant_ano_cb(self, widget, data=None):
        '''prev year in the calendar'''
        #unmark last year days
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)        
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.unmark_day(dia)
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.unmark_day(dia)

        self.marcar_dia = int(self.marcar_dia) - 10000
        self.marcar_dia = str(self.marcar_dia)
        self.lista_dias_marcados = dict_meses(self.path, self.marcar_dia)
        indice = self.marcar_dia[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.mark_day(dia)

    def _borrar_fila_cb(self, widget, data=None):
        '''deleted the selected row'''
        self.seleccion = self.actividades.get_selection()
        self.model, self.iter = self.seleccion.get_selected()
        self.enlace = self.modelo.get_path(self.iter)
        self.lista_fecha = self.calendario.get_date()
        self.indice_dia = FechaUnix(self.lista_fecha)  
        self.indice = self.indice_dia.fecha_unix()
        comprobar_vacio = borrar_dato(self.indice, self.enlace, self.path)
        if comprobar_vacio:
            borrar_dia = self.indice_dia.indice_unix(self.indice)
            borrar_dict_meses(self.path, borrar_dia)
            borrar_dia = int(borrar_dia[4:])
            self.calendario.unmark_day(borrar_dia)
	    
        self.modelo = crear_modelo(self.indice, self.path)
        self.actividades.set_model(self.modelo)
        self.pendientes.set_model(tareas_pendientes(self.path))

    def _nuevo_ingreso_cb(self, widget, data=None):
        '''when the user clicked on the save button, 
        the content is added to a python object and
        is saved in a file'''
        self.guardar()
                   
    def _importar_cb(self, widget, data=None):
        '''change to the import view'''
        self.set_canvas(self.archivo)

    def _calendario_cb(self, widget, data=None):
        '''change to the calendar view'''
        self.set_canvas(self.contenedor_h) 
    
    def _importar_archivo_cb(self, widget, data=None):
        '''import a file to phrase/ephemeris'''
        self.modelo_archivo = self.combo_archivo.get_model()
        self.index_archivo = self.combo_archivo.get_active()
        self.uri = self.archivo.get_uri()
        self.uri = self.uri[7:]
        guardar_archivo(self.path, self.uri, self.index_archivo + 1)

    def _editar_cb(self, widget, data=None):
        '''edit row'''
        self.compl.set_sensitive(True)
        self.seleccion = self.actividades.get_selection()
        self.model, self.iter = self.seleccion.get_selected()
        self.enlace = self.modelo.get_path(self.iter)
        val = self.modelo.get(self.iter, 0, 1, 2, 3, 4, 5, 6)
        self.buffer.set_text(val[1])
        self.entrada.set_buffer(self.buffer)
        self.combo.set_active(int(val[6])) 
        if val[3] is not None:
            self.check.set_active(True)

        if val[4] is not None:
            self.progre.set_active(True)
        self.check_editar = True
                
    def _crear_columna(self):
        '''create the columns for the treeview'''
        self.celda = gtk.CellRendererText()
        self.icono = gtk.CellRendererPixbuf()
        self.actividad = (_('Activity'))
        self.categoria = (_('Category'))
        self.prioridad = (_('Priority'))
        self.estado = (_('Status'))
        #for activities
        self.columna = gtk.TreeViewColumn('', self.celda, text = 0, cell_background = 5)
        self.actividades.append_column(self.columna)
        self.columna = gtk.TreeViewColumn(self.actividad, self.celda, text = 1, cell_background = 5)
        self.columna.set_min_width(365)
        self.actividades.append_column(self.columna)
        self.columna = gtk.TreeViewColumn(self.categoria, self.celda, text = 2, cell_background = 5)
        self.actividades.append_column(self.columna)
        self.columna = gtk.TreeViewColumn(self.prioridad, self.icono, stock_id = 3, cell_background = 5)
        self.actividades.append_column(self.columna)
        self.columna = gtk.TreeViewColumn(self.estado, self.icono, stock_id = 4, cell_background = 5)
        self.actividades.append_column(self.columna)
        #for pending task
        self.columna = gtk.TreeViewColumn(self.actividad, self.celda, text = 0, cell_background = 1)
        self.pendientes.append_column(self.columna)
    
    def guardar(self):
        self.buffer = self.entrada.get_buffer()
        self.inicio, self.final = self.buffer.get_bounds()
        self.cadena = self.buffer.get_text(self.inicio, self.final, False)
        
        #if the buffer is not empty or does not have space
        if self.cadena is not ' ' and self.cadena is not '' and self.cadena is not '  ': 
            self.lista_fecha = self.calendario.get_date()
            self.indice_dia = FechaUnix(self.lista_fecha) #id for save the date
            self.indice = self.indice_dia.fecha_unix()
            self.marcar_dia = self.indice_dia.indice_unix(self.indice)#id for mark the day
            self.modelocombo = self.combo.get_model()
            self.index = self.combo.get_active()
            self.categoria = self.modelocombo[self.index][0]
            self.combo_list = [self.index, self.categoria]
                        
            if self.check_editar:
                guardar_dato(self.indice, self.cadena, self.combo_list, self.index, self.check_status, \
                        self.progre_status, self.path, self.enlace)
                self.check_editar = False

            else:
                guardar_dato(self.indice, self.cadena, self.combo_list, self.index, self.check_status, \
                         self.progre_status, self.path)

            self.buffer.set_text(' ') 
            self.modelo = crear_modelo(self.indice, self.path)
            self.actividades.set_model(self.modelo)
            self.pendientes.set_model(tareas_pendientes(self.path))
            self.marcar_calendario(self.marcar_dia) #mark the day in the calendar
            self.desactivar_boton() #make inactive again the checkbutton
            self.combo.set_active(-1)
            self.compl.set_active(False)

    def marcar_calendario(self, fecha=None):
        '''mark the days that have activities stored'''
        guardar = 1
        self.lista_dias_marcados = dict_meses(self.path, fecha, guardar)        
        indice = fecha[:4]
        for dia in self.lista_dias_marcados[indice]:
            self.calendario.mark_day(dia)

    def desactivar_boton(self):
        '''inactive the checkbuttons'''
        if self.check_status is 1 or self.progre_status is 2:                  
                self.check.set_active(False)
        if self.progre_status is 1 or self.progre_status is 2:
                self.progre.set_active(False)

    def close(self, skip_save=False):
        '''Override the close method so we don't try to
        create a Journal entry'''
        activity.Activity.close(self, True)

    def read_file(self, filepath):
        pass

    def write_file(self, filepath):
        pass