# -*- 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 . 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 are no ephemeris')) else: self.buffer.set_text(_('There are no 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 are no phrases')) else: self.buffer.set_text(_('There are no 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