/ src / bitmessagekivy / baseclass / maildetail.py
maildetail.py
  1  # pylint: disable=unused-argument, consider-using-f-string, import-error, attribute-defined-outside-init
  2  # pylint: disable=unnecessary-comprehension, no-member, no-name-in-module, too-few-public-methods
  3  
  4  """
  5  Maildetail screen for inbox, sent, draft and trash.
  6  """
  7  
  8  import os
  9  from datetime import datetime
 10  
 11  from kivy.core.clipboard import Clipboard
 12  from kivy.clock import Clock
 13  from kivy.properties import (
 14      StringProperty,
 15      NumericProperty
 16  )
 17  from kivy.uix.screenmanager import Screen
 18  from kivy.factory import Factory
 19  from kivy.app import App
 20  
 21  from kivymd.uix.button import MDFlatButton, MDIconButton
 22  from kivymd.uix.dialog import MDDialog
 23  from kivymd.uix.list import (
 24      OneLineListItem,
 25      IRightBodyTouch
 26  )
 27  
 28  from pybitmessage.bitmessagekivy.baseclass.common import (
 29      toast, avatar_image_first_letter, show_time_history, kivy_state_variables
 30  )
 31  from pybitmessage.bitmessagekivy.baseclass.popup import SenderDetailPopup
 32  from pybitmessage.bitmessagekivy.get_platform import platform
 33  from pybitmessage.helper_sql import sqlQuery
 34  
 35  
 36  class OneLineListTitle(OneLineListItem):
 37      """OneLineListTitle class for kivy Ui"""
 38      __events__ = ('on_long_press', )
 39      long_press_time = NumericProperty(1)
 40  
 41      def on_state(self, instance, value):
 42          """On state"""
 43          if value == 'down':
 44              lpt = self.long_press_time
 45              self._clockev = Clock.schedule_once(self._do_long_press, lpt)
 46          else:
 47              self._clockev.cancel()
 48  
 49      def _do_long_press(self, dt):
 50          """Do long press"""
 51          self.dispatch('on_long_press')
 52  
 53      def on_long_press(self, *largs):
 54          """On long press"""
 55          self.copymessageTitle(self.text)
 56  
 57      def copymessageTitle(self, title_text):
 58          """this method is for displaying dialog box"""
 59          self.title_text = title_text
 60          width = .8 if platform == 'android' else .55
 61          self.dialog_box = MDDialog(
 62              text=title_text,
 63              size_hint=(width, .25),
 64              buttons=[
 65                  MDFlatButton(
 66                      text="Copy", on_release=self.callback_for_copy_title
 67                  ),
 68                  MDFlatButton(
 69                      text="Cancel", on_release=self.callback_for_copy_title,
 70                  ),
 71              ],)
 72          self.dialog_box.open()
 73  
 74      def callback_for_copy_title(self, instance):
 75          """Callback of alert box"""
 76          if instance.text == 'Copy':
 77              Clipboard.copy(self.title_text)
 78          self.dialog_box.dismiss()
 79          toast(instance.text)
 80  
 81  
 82  class IconRightSampleWidget(IRightBodyTouch, MDIconButton):
 83      """IconRightSampleWidget class for kivy Ui"""
 84  
 85  
 86  class MailDetail(Screen):  # pylint: disable=too-many-instance-attributes
 87      """MailDetail Screen class for kivy Ui"""
 88  
 89      to_addr = StringProperty()
 90      from_addr = StringProperty()
 91      subject = StringProperty()
 92      message = StringProperty()
 93      status = StringProperty()
 94      page_type = StringProperty()
 95      time_tag = StringProperty()
 96      avatarImg = StringProperty()
 97      no_subject = '(no subject)'
 98  
 99      def __init__(self, *args, **kwargs):
100          """Mail Details method"""
101          super(MailDetail, self).__init__(*args, **kwargs)
102          self.kivy_state = kivy_state_variables()
103          Clock.schedule_once(self.init_ui, 0)
104  
105      def init_ui(self, dt=0):
106          """Clock Schdule for method MailDetail mails"""
107          self.page_type = self.kivy_state.detail_page_type if self.kivy_state.detail_page_type else ''
108          try:
109              if self.kivy_state.detail_page_type in ('sent', 'draft'):
110                  App.get_running_app().set_mail_detail_header()
111              elif self.kivy_state.detail_page_type == 'inbox':
112                  data = sqlQuery(
113                      "select toaddress, fromaddress, subject, message, received from inbox"
114                      " where msgid = ?", self.kivy_state.mail_id)
115                  self.assign_mail_details(data)
116                  App.get_running_app().set_mail_detail_header()
117          except Exception as e:  # pylint: disable=unused-variable
118              print('Something wents wrong!!')
119  
120      def assign_mail_details(self, data):
121          """Assigning mail details"""
122          subject = data[0][2].decode() if isinstance(data[0][2], bytes) else data[0][2]
123          body = data[0][3].decode() if isinstance(data[0][2], bytes) else data[0][3]
124          self.to_addr = data[0][0] if len(data[0][0]) > 4 else ' '
125          self.from_addr = data[0][1]
126  
127          self.subject = subject.capitalize(
128          ) if subject.capitalize() else self.no_subject
129          self.message = body
130          if len(data[0]) == 7:
131              self.status = data[0][4]
132          self.time_tag = show_time_history(data[0][4]) if self.kivy_state.detail_page_type == 'inbox' \
133              else show_time_history(data[0][6])
134          self.avatarImg = os.path.join(self.kivy_state.imageDir, 'draft-icon.png') \
135              if self.kivy_state.detail_page_type == 'draft' \
136              else (os.path.join(self.kivy_state.imageDir, 'text_images', '{0}.png'.format(avatar_image_first_letter(
137                  self.subject.strip()))))
138          self.timeinseconds = data[0][4] if self.kivy_state.detail_page_type == 'inbox' else data[0][6]
139  
140      def delete_mail(self):
141          """Method for mail delete"""
142          msg_count_objs = App.get_running_app().root.ids.content_drawer.ids
143          self.kivy_state.searching_text = ''
144          self.children[0].children[0].active = True
145          if self.kivy_state.detail_page_type == 'sent':
146              App.get_running_app().root.ids.id_sent.ids.sent_search.ids.search_field.text = ''
147              msg_count_objs.send_cnt.ids.badge_txt.text = str(int(self.kivy_state.sent_count) - 1)
148              self.kivy_state.sent_count = str(int(self.kivy_state.sent_count) - 1)
149              self.parent.screens[2].ids.ml.clear_widgets()
150              self.parent.screens[2].loadSent(self.kivy_state.selected_address)
151          elif self.kivy_state.detail_page_type == 'inbox':
152              App.get_running_app().root.ids.id_inbox.ids.inbox_search.ids.search_field.text = ''
153              msg_count_objs.inbox_cnt.ids.badge_txt.text = str(
154                  int(self.kivy_state.inbox_count) - 1)
155              self.kivy_state.inbox_count = str(int(self.kivy_state.inbox_count) - 1)
156              self.parent.screens[0].ids.ml.clear_widgets()
157              self.parent.screens[0].loadMessagelist(self.kivy_state.selected_address)
158  
159          elif self.kivy_state.detail_page_type == 'draft':
160              msg_count_objs.draft_cnt.ids.badge_txt.text = str(
161                  int(self.kivy_state.draft_count) - 1)
162              self.kivy_state.draft_count = str(int(self.kivy_state.draft_count) - 1)
163              self.parent.screens[13].clear_widgets()
164              self.parent.screens[13].add_widget(Factory.Draft())
165  
166          if self.kivy_state.detail_page_type != 'draft':
167              msg_count_objs.trash_cnt.ids.badge_txt.text = str(
168                  int(self.kivy_state.trash_count) + 1)
169              msg_count_objs.allmail_cnt.ids.badge_txt.text = str(
170                  int(self.kivy_state.all_count) - 1)
171              self.kivy_state.trash_count = str(int(self.kivy_state.trash_count) + 1)
172              self.kivy_state.all_count = str(int(self.kivy_state.all_count) - 1) if \
173                  int(self.kivy_state.all_count) else '0'
174              self.parent.screens[3].clear_widgets()
175              self.parent.screens[3].add_widget(Factory.Trash())
176              self.parent.screens[14].clear_widgets()
177              self.parent.screens[14].add_widget(Factory.AllMails())
178          Clock.schedule_once(self.callback_for_delete, 4)
179  
180      def callback_for_delete(self, dt=0):
181          """Delete method from allmails"""
182          if self.kivy_state.detail_page_type:
183              self.children[0].children[0].active = False
184              App.get_running_app().set_common_header()
185              self.parent.current = 'allmails' \
186                  if self.kivy_state.is_allmail else self.kivy_state.detail_page_type
187              self.kivy_state.detail_page_type = ''
188              toast('Deleted')
189  
190      def get_message_details_to_reply(self, data):
191          """Getting message details and fill into fields when reply"""
192          sender_address = ' wrote:--------------\n'
193          message_time = '\n\n --------------On '
194          composer_obj = self.parent.screens[1].children[1].ids
195          composer_obj.ti.text = data[0][0]
196          composer_obj.composer_dropdown.text = data[0][0]
197          composer_obj.txt_input.text = data[0][1]
198          split_subject = data[0][2].split('Re:', 1)
199          composer_obj.subject.text = 'Re: ' + (split_subject[1] if len(split_subject) > 1 else split_subject[0])
200          time_obj = datetime.fromtimestamp(int(data[0][4]))
201          time_tag = time_obj.strftime("%d %b %Y, %I:%M %p")
202          sender_name = data[0][1]
203          composer_obj.body.text = (
204              message_time + time_tag + ', ' + sender_name + sender_address + data[0][3])
205          composer_obj.body.focus = True
206          composer_obj.body.cursor = (0, 0)
207  
208      def inbox_reply(self):
209          """Reply inbox messages"""
210          self.kivy_state.in_composer = True
211          App.get_running_app().root.ids.id_create.children[1].ids.rv.data = ''
212          App.get_running_app().root.ids.sc3.children[1].ids.rv.data = ''
213          self.parent.current = 'create'
214          App.get_running_app().set_navbar_for_composer()
215  
216      def get_message_details_for_draft_reply(self, data):
217          """Getting and setting message details fill into fields when draft reply"""
218          composer_ids = (
219              self.parent.parent.ids.id_create.children[1].ids)
220          composer_ids.ti.text = data[0][1]
221          composer_ids.btn.text = data[0][1]
222          composer_ids.txt_input.text = data[0][0]
223          composer_ids.subject.text = data[0][2] if data[0][2] != self.no_subject else ''
224          composer_ids.body.text = data[0][3]
225  
226      def write_msg(self, navApp):
227          """Write on draft mail"""
228          self.kivy_state.send_draft_mail = self.kivy_state.mail_id
229          self.parent.current = 'create'
230          navApp.set_navbar_for_composer()
231  
232      def detailedPopup(self):
233          """Detailed popup"""
234          obj = SenderDetailPopup()
235          obj.open()
236          arg = (self.to_addr, self.from_addr, self.timeinseconds)
237          obj.assignDetail(*arg)
238  
239      @staticmethod
240      def callback_for_menu_items(text_item, *arg):
241          """Callback of alert box"""
242          toast(text_item)