qvm-mru-entry 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/python
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Bruce Downs <bruceadowns@gmail.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. import os
  23. from optparse import OptionParser
  24. import gtk
  25. class QubesMruDialog(gtk.Dialog):
  26. entry = None
  27. mrufile = None
  28. def __init__(self, title, text, mrufile):
  29. self.mrufile = mrufile
  30. gtk.Dialog.__init__(
  31. self,
  32. title,
  33. None,
  34. 0,
  35. (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
  36. gtk.STOCK_OK, gtk.RESPONSE_OK))
  37. # setting the default response to 'ok'
  38. # does not work as advertised
  39. # using key-press-event instead
  40. #self.set_default_response(gtk.RESPONSE_OK)
  41. self.connect("destroy", lambda *w: gtk.main_quit())
  42. self.connect("response", self.response_callback)
  43. self.connect("key-press-event", self.key_press_callback)
  44. self.set_position(gtk.WIN_POS_CENTER)
  45. self.set_resizable(True)
  46. vbox = gtk.VBox(True, 5)
  47. self.vbox.pack_start(vbox, True, True, 0)
  48. vbox.set_border_width(5)
  49. label = gtk.Label()
  50. label.set_markup(text)
  51. vbox.pack_start(label, False, False, 0)
  52. # Create our entry
  53. self.entry = gtk.Entry()
  54. vbox.pack_start(self.entry, False, False, 0)
  55. # Create the completion object
  56. completion = gtk.EntryCompletion()
  57. # Assign the completion to the entry
  58. self.entry.set_completion(completion)
  59. # Create a tree model and use it as the completion model
  60. completion_model, firstline = self.create_completion_model()
  61. completion.set_model(completion_model)
  62. # Use model column 0 as the text column
  63. completion.set_text_column(0)
  64. if firstline:
  65. self.entry.set_text(firstline)
  66. self.show_all()
  67. def create_completion_model(self):
  68. store = gtk.ListStore(str)
  69. firstline = None
  70. if self.mrufile and os.access(self.mrufile, os.R_OK):
  71. # read lines from mru file
  72. lines = [line.strip() for line in open(self.mrufile)]
  73. for line in lines:
  74. if not firstline:
  75. firstline = line
  76. iter = store.append()
  77. store.set(iter, 0, line)
  78. return store, firstline
  79. def handle_ok(self):
  80. my_entry = self.entry.get_text()
  81. if len(my_entry) > 0:
  82. linesorg = []
  83. linesorg.insert(0, my_entry)
  84. # calc mru lines
  85. if os.access(self.mrufile, os.R_OK):
  86. # read lines from existing mru file
  87. fMru = open(self.mrufile, "r")
  88. linesorg += [line.strip() for line in fMru]
  89. fMru.close()
  90. #uniqify
  91. linesuniq = []
  92. for lineuniq in linesorg:
  93. if (len(lineuniq) > 0 and lineuniq not in linesuniq):
  94. linesuniq.append(lineuniq)
  95. # write to mru
  96. # up to 50 unique
  97. fMru = file(self.mrufile, "w")
  98. fMru.writelines( "%s\n" % line for line in linesuniq[:50] )
  99. fMru.close()
  100. # print entry to stdout
  101. print my_entry
  102. def response_callback(self, button, response_id):
  103. if response_id == gtk.RESPONSE_OK:
  104. self.handle_ok()
  105. self.destroy()
  106. def key_press_callback(self, button, event):
  107. if event.keyval == gtk.keysyms.Return:
  108. self.handle_ok()
  109. self.destroy()
  110. def main():
  111. usage = "usage: %prog [--title 'Qubes Title'] [--text 'Qubes Text'] [--mrufile 'mru file name']"
  112. parser = OptionParser (usage)
  113. parser.add_option ("-l", "--title",
  114. action="store",
  115. dest="title",
  116. default="Qubes MRU Dialog Entry",
  117. help="Set the dialog title [%default]")
  118. parser.add_option ("-x", "--text",
  119. action="store",
  120. dest="text",
  121. default="Enter Qubes text:",
  122. help="Set the dialog text [%default]")
  123. parser.add_option ("-f", "--mrufile",
  124. action="store",
  125. dest="mrufile",
  126. default='qvm-mru',
  127. help="MRU file name [%default]")
  128. (options, args) = parser.parse_args ()
  129. mrudir = os.path.expanduser('~') + os.sep + '.config'
  130. if not os.path.exists(mrudir):
  131. os.makedirs(mrudir)
  132. QubesMruDialog(options.title, options.text, mrudir + os.sep + options.mrufile)
  133. gtk.main()
  134. main()