"Fossies" - the Fresh Open Source Software Archive

Member "email-reminder-0.8.1/EmailReminder/Event.py" (19 Sep 2020, 7229 Bytes) of package /linux/privat/email-reminder-0.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "Event.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.8.0_vs_0.8.1.

    1 # This file is part of Email-Reminder.
    2 #
    3 # Copyright (C) 2020 by Francois Marier
    4 #
    5 # This program is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU Affero General Public License as published by
    7 # the Free Software Foundation, either version 3 of the License, or
    8 # (at your option) any later version.
    9 #
   10 # This program is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 # GNU Affero General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU Affero General Public License
   16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 from PySide2.QtCore import (
   19     Qt, QAbstractTableModel, QModelIndex)
   20 from EmailReminder.Utils import readXMLValue, writeXMLValue
   21 
   22 
   23 def parseDay(dayString, lastDay):
   24     try:
   25         day = int(dayString)
   26     except ValueError:
   27         return 1
   28 
   29     if day < 1:
   30         return 1
   31     if day > lastDay:
   32         return lastDay
   33 
   34     return day
   35 
   36 
   37 class EventModel(QAbstractTableModel):
   38     TYPE = 'event'
   39     COLUMN_NAMES = ["Event Name"]
   40 
   41     def __init__(self, document):
   42         super().__init__()
   43         self.document = document
   44         self.events = []
   45         self.column_count = len(self.COLUMN_NAMES)
   46 
   47     def _createEvent(self):
   48         event = self.document.createElement('event')
   49         event.setAttribute('type', self.TYPE)
   50         self.fillNewEvent(event)
   51 
   52         user = self.document.getElementsByTagName('email-reminder_user')
   53         events = user.item(0).getElementsByTagName('events')
   54         events.item(0).appendChild(event)
   55 
   56         return event
   57 
   58     def fillNewEvent(self, event):
   59         writeXMLValue(self.document, event, 'name', '<New Event>')
   60 
   61         reminders = self.document.createElement('reminders')
   62         sameday = self.document.createElement('reminder')
   63         sameday.setAttribute('type', 'same day')
   64         reminders.appendChild(sameday)
   65         event.appendChild(reminders)
   66 
   67     def addEvent(self, event, parent=QModelIndex()):
   68         self.beginInsertRows(parent, len(self.events), len(self.events))
   69         self.events.append(event)
   70         self.endInsertRows()
   71 
   72     def rowCount(self, parent=QModelIndex()):
   73         # pylint: disable=unused-argument
   74         return len(self.events)
   75 
   76     def columnCount(self, parent=QModelIndex()):
   77         # pylint: disable=unused-argument
   78         return self.column_count
   79 
   80     def headerData(self, section, orientation, role):
   81         if role != Qt.DisplayRole:
   82             return None
   83         if orientation != Qt.Horizontal:
   84             return None
   85         return self.COLUMN_NAMES[section]
   86 
   87     def getDataInternal(self, column, row):
   88         if column == 0:
   89             return readXMLValue(self.events[row], 'name')
   90 
   91         return None
   92 
   93     def data(self, index, role=Qt.DisplayRole):
   94         column = index.column()
   95         row = index.row()
   96 
   97         if role in (Qt.DisplayRole, Qt.EditRole):
   98             return self.getDataInternal(column, row)
   99 
  100         return None
  101 
  102     def flags(self, index):
  103         # pylint: disable=unused-argument,no-self-use
  104         return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable
  105 
  106     def getReminders(self, row):
  107         sameDay = False
  108         daysInAdvance = 0
  109 
  110         event = self.events[row]
  111         reminderList = event.getElementsByTagName('reminders')
  112         if reminderList:
  113             reminders = reminderList.item(0).getElementsByTagName('reminder')
  114             for reminder in reminders:
  115                 typeAttribute = reminder.getAttribute('type')
  116                 if typeAttribute == 'same day':
  117                     sameDay = True
  118                 elif typeAttribute == 'days before':
  119                     if reminder.hasChildNodes() and reminder.firstChild:
  120                         daysInAdvance = int(reminder.firstChild.nodeValue)
  121 
  122         return {'name': readXMLValue(self.events[row], 'name'),
  123                 'sameday': sameDay,
  124                 'daysinadvance': daysInAdvance}
  125 
  126     def deleteReminder(self, parent, typeName):
  127         # pylint: disable=no-self-use
  128         reminders = parent.getElementsByTagName('reminder')
  129         for reminder in reminders:
  130             if reminder.getAttribute('type') == typeName:
  131                 parent.removeChild(reminder)
  132 
  133     def setReminder(self, parent, typeName, nodeValue=None):
  134         # Look for an existing element to modify.
  135         for reminder in parent.getElementsByTagName('reminder'):
  136             if reminder.getAttribute('type') == typeName:
  137                 if nodeValue:
  138                     if reminder.hasChildNodes():
  139                         reminder.firstChild.nodeValue = nodeValue
  140                     else:
  141                         reminder.appendChild(
  142                             self.document.createTextNode(str(nodeValue)))
  143                 return  # nothing else to do
  144 
  145         # Create a new element.
  146         reminder = self.document.createElement('reminder')
  147         reminder.setAttribute('type', typeName)
  148         if nodeValue:
  149             reminder.appendChild(self.document.createTextNode(str(nodeValue)))
  150         parent.appendChild(reminder)
  151 
  152     def setReminders(self, row, values):
  153         event = self.events[row]
  154         reminderList = event.getElementsByTagName('reminders')
  155         if not reminderList:
  156             reminders = self.document.createElement('reminders')
  157             event.appendChild(reminders)
  158 
  159         if not values['sameday']:
  160             self.deleteReminder(reminderList.item(0), 'same day')
  161         else:
  162             self.setReminder(reminderList.item(0), 'same day')
  163 
  164         if values['daysinadvance'] == 0:
  165             self.deleteReminder(reminderList.item(0), 'days before')
  166         else:
  167             self.setReminder(reminderList.item(0), 'days before',
  168                              values['daysinadvance'])
  169 
  170     def setDataInternal(self, column, row, value):
  171         if column == 0:
  172             writeXMLValue(self.document, self.events[row], 'name', value)
  173 
  174     def setData(self, index, value, role=Qt.EditRole):
  175         if role != Qt.EditRole:
  176             return False
  177 
  178         column = index.column()
  179         row = index.row()
  180         self.setDataInternal(column, row, value)
  181         self.dataChanged.emit(index, index, [role])
  182         return True
  183 
  184     def insertRows(self, row, count, parent=QModelIndex()):
  185         if count < 1:
  186             return False
  187 
  188         # These are not currently needed.
  189         if count > 1:
  190             return False
  191         if row != self.rowCount():
  192             return False
  193 
  194         self.addEvent(self._createEvent(), parent)
  195         return True
  196 
  197     def removeRows(self, row, count, parent=QModelIndex()):
  198         if count < 1:
  199             return False
  200 
  201         # This is neither implemented nor currently needed.
  202         if count > 1:
  203             return False
  204 
  205         self.beginRemoveRows(parent, row, row)
  206         event = self.events[row]
  207         del self.events[row]
  208         event.parentNode.removeChild(event)
  209         self.endRemoveRows()
  210 
  211         return True
  212 
  213     def append(self):
  214         return self.insertRows(self.rowCount(), 1)
  215 
  216     def delete(self, row):
  217         return self.removeRows(row, 1)