log_dialog.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. #
  21. #
  22. import sys
  23. import os
  24. from functools import partial
  25. from PyQt5 import QtWidgets # pylint: disable=import-error
  26. from qubesadmin import Qubes
  27. from . import ui_logdlg # pylint: disable=no-name-in-module
  28. from . import clipboard
  29. # Display only this size of log
  30. LOG_DISPLAY_SIZE = 1024*1024
  31. class LogDialog(ui_logdlg.Ui_LogDialog, QtWidgets.QDialog):
  32. # pylint: disable=too-few-public-methods
  33. def __init__(self, app, logfiles, parent=None):
  34. super().__init__(parent)
  35. self.app = app
  36. self.logfiles = logfiles
  37. self.displayed_text = ""
  38. self.setupUi(self)
  39. self.copy_to_qubes_clipboard.clicked.connect(
  40. self.copy_to_clipboard_triggered)
  41. self.copy_to_qubes_clipboard.clicked.connect(
  42. self.copy_to_clipboard_triggered)
  43. self.__init_log_text__()
  44. def __init_log_text__(self):
  45. btns_in_row = 3
  46. count = 0
  47. for log_path in self.logfiles:
  48. button = QtWidgets.QPushButton(log_path)
  49. button.clicked.connect(partial(self.set_current_log, log_path))
  50. self.buttonsLayout.addWidget(button,
  51. count / btns_in_row, count % btns_in_row)
  52. count += 1
  53. self.buttonsLayout.itemAt(0).widget().click()
  54. def copy_to_clipboard_triggered(self):
  55. clipboard.copy_text_to_qubes_clipboard(self.displayed_text)
  56. def set_current_log(self, log_path):
  57. self.displayed_text = ""
  58. self.setWindowTitle(log_path)
  59. log = open(log_path)
  60. log.seek(0, os.SEEK_END)
  61. if log.tell() > LOG_DISPLAY_SIZE:
  62. self.displayed_text = self.tr(
  63. "(Showing only last %d bytes of file)\n") % LOG_DISPLAY_SIZE
  64. log.seek(log.tell()-LOG_DISPLAY_SIZE, os.SEEK_SET)
  65. else:
  66. log.seek(0, os.SEEK_SET)
  67. self.displayed_text += log.read()
  68. log.close()
  69. self.log_text.setPlainText(self.displayed_text)
  70. def main():
  71. qubes_app = Qubes()
  72. qt_app = QtWidgets.QApplication(sys.argv)
  73. log_window = LogDialog(qubes_app, sys.argv[1])
  74. log_window.show()
  75. qt_app.exec_()
  76. qt_app.exit()
  77. if __name__ == "__main__":
  78. main()