ccrypt-caja.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Written by Jason K. MacDuffie
  4. # Encrypt files with ccrypt in Caja
  5. import os
  6. import subprocess
  7. import urllib
  8. import subprocess
  9. from gi.repository import Caja, GObject, Gio
  10. from locale import getlocale
  11. CCRYPT_SCHEMA = 'org.mate.applications-ccrypt'
  12. # No clue what the right way to do locales is, so this is my best attempt
  13. loc = getlocale()[0]
  14. encrypt_loc = {
  15. 'en': 'Encrypt',
  16. 'eo': 'Ĉifri'
  17. }
  18. decrypt_loc = {
  19. 'en': 'Decrypt',
  20. 'eo': 'Deĉifri'
  21. }
  22. def call_ccdecrypt(filename):
  23. zenity_prompt = [
  24. 'zenity',
  25. '--entry',
  26. '--hide-text',
  27. '--title=Decryption Key',
  28. '--text=Enter a key for ccdecrypt:',
  29. '--width=240',
  30. '--height=150'
  31. ]
  32. prompt_process = subprocess.Popen(zenity_prompt, stdout=subprocess.PIPE)
  33. decrypt_process = subprocess.Popen(['ccdecrypt', '-k', '-', filename],
  34. stdin=prompt_process.stdout)
  35. prompt_process.wait()
  36. decrypt_process.wait()
  37. # Check if all went well
  38. if prompt_process.returncode != 0:
  39. zenity_error_process = subprocess.Popen([
  40. 'zenity',
  41. '--error',
  42. '--title=Decryption Key',
  43. '--text=The operation was aborted.'
  44. ])
  45. return 'Aborted'
  46. if decrypt_process.returncode != 0:
  47. zenity_error_process = subprocess.Popen([
  48. 'zenity',
  49. '--error',
  50. '--title=Decryption Key',
  51. '--text=Decryption failed. The entered key may have been incorrect.'
  52. ])
  53. return 'Failure'
  54. return 'Success'
  55. def call_ccencrypt(filename):
  56. zenity_prompt1 = [
  57. 'zenity',
  58. '--entry',
  59. '--hide-text',
  60. '--title=Encryption Key',
  61. '--text=Enter a key for ccencrypt:',
  62. '--width=240',
  63. '--height=150'
  64. ]
  65. zenity_prompt2 = [
  66. 'zenity',
  67. '--entry',
  68. '--hide-text',
  69. '--title=Encryption key',
  70. '--text=Enter the same key again to confirm:',
  71. '--width=240',
  72. '--height=150'
  73. ]
  74. prompt1_proc = subprocess.Popen(zenity_prompt1, stdout=subprocess.PIPE)
  75. prompt1_output = prompt1_proc.communicate()[0]
  76. # Check if the prompt was cancelled
  77. if prompt1_proc.returncode != 0:
  78. zenity_error_process = subprocess.Popen([
  79. 'zenity',
  80. '--error',
  81. '--title=Encryption Key',
  82. '--text=The operation was aborted.'
  83. ])
  84. return 'Aborted'
  85. prompt2_proc = subprocess.Popen(zenity_prompt2, stdout=subprocess.PIPE)
  86. prompt2_output = prompt2_proc.communicate()[0]
  87. # Check if the prompt was cancelled
  88. if prompt2_proc.returncode != 0:
  89. zenity_error_process = subprocess.Popen([
  90. 'zenity',
  91. '--error',
  92. '--title=Encryption Key',
  93. '--text=The operation was aborted.'
  94. ])
  95. return 'Aborted'
  96. # Check the passwords match
  97. if prompt1_output != prompt2_output:
  98. mismatch_notify = subprocess.Popen([
  99. 'zenity',
  100. '--error',
  101. '--title=Encryption Key',
  102. '--text=The encryption keys did not match.'
  103. ])
  104. return 'Failure'
  105. encrypt_process = subprocess.Popen(['ccencrypt', '-k', '-', filename],
  106. stdin=subprocess.PIPE)
  107. encrypt_process.communicate(input=prompt2_output)
  108. # Check if all went well
  109. if encrypt_process.returncode != 0:
  110. zenity_error_process = subprocess.Popen([
  111. 'zenity',
  112. '--error',
  113. '--title=Encryption Key',
  114. '--text=Encryption failed for some reason.'
  115. ])
  116. return 'Failure'
  117. return 'Success'
  118. class CCryptExtension(Caja.MenuProvider, GObject.GObject):
  119. def __init__(self):
  120. pass
  121. def _ccencrypt_file(self, file):
  122. filename = urllib.unquote(file.get_uri()[7:])
  123. call_ccencrypt(filename)
  124. def _ccdecrypt_file(self, file):
  125. filename = urllib.unquote(file.get_uri()[7:])
  126. call_ccdecrypt(filename)
  127. def menu_activate_encryption(self, menu, file):
  128. self._ccencrypt_file(file)
  129. def menu_activate_decryption(self, menu, file):
  130. self._ccdecrypt_file(file)
  131. def get_file_items(self, window, files):
  132. if len(files) != 1:
  133. return
  134. file = files[0]
  135. filename = file.get_name()
  136. if file.is_directory() or file.get_uri_scheme() != 'file':
  137. return
  138. encryption_item = Caja.MenuItem(name='CajaPython::ccencrypt_file_item',
  139. label=encrypt_loc.get(loc,'Encrypt') + '...',
  140. tip='Encrypt this file using ccencrypt')
  141. encryption_item.connect('activate', self.menu_activate_encryption, file)
  142. # Check if file is .cpt or otherwise
  143. is_encrypted = file.get_name().endswith('.cpt')
  144. if not is_encrypted:
  145. return encryption_item,
  146. # Otherwise, include a decryption option
  147. decryption_item = Caja.MenuItem(name='CajaPython::ccdecrypt_file_item',
  148. label=decrypt_loc.get(loc,'Decrypt') + '...',
  149. tip='Decrypt this file using ccdecrypt')
  150. decryption_item.connect('activate', self.menu_activate_decryption, file)
  151. return (encryption_item, decryption_item)