Class: Cosmos::LegalDialog

Inherits:
Qt::Dialog show all
Defined in:
lib/cosmos/gui/dialogs/legal_dialog.rb

Instance Method Summary collapse

Methods inherited from Qt::Dialog

#exec

Constructor Details

#initializeLegalDialog

Returns a new instance of LegalDialog.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cosmos/gui/dialogs/legal_dialog.rb', line 22

def initialize
  super() # MUST BE FIRST

  # Set Default Icon
  Cosmos.load_cosmos_icon

  self.window_title = 'Legal Agreement'
  layout = Qt::VBoxLayout.new
  self.layout = layout

  legal_image_filename = File.join(::Cosmos::USERPATH, 'config', 'data', 'legal.gif')
  legal_image_filename = File.join(::Cosmos::PATH, 'data', 'legal.gif') unless File.exist?(legal_image_filename)
  pixmap = Qt::Pixmap.new(legal_image_filename)
  label = Qt::Label.new
  label.setPixmap(pixmap)
  label.setFrameStyle(Qt::Frame::Box)
  layout.addWidget(label)

  legal_text = ''
  legal_text_filename = File.join(::Cosmos::USERPATH, 'config', 'data', 'legal.txt')
  legal_text_filename = File.join(::Cosmos::PATH, 'data', 'legal.txt') unless File.exist?(legal_text_filename)
  File.open(legal_text_filename, "r") {|file| legal_text << file.read}
  legal_text.gsub!("\r", '') unless Kernel.is_windows?

  text_edit = Qt::TextEdit.new
  text_edit.text = legal_text
  text_edit.setReadOnly(true)
  text_edit.setFrameStyle(Qt::Frame::Box)
  layout.addWidget(text_edit)

  @text_crc = Qt::TextEdit.new
  @text_crc.setFixedHeight(100)
  @text_crc.setReadOnly(true)
  @text_crc.setFrameStyle(Qt::Frame::Box)
  layout.addWidget(@text_crc)

  project_error_count = check_all_crcs()

  ok_button = Qt::PushButton.new('Ok')
  connect(ok_button, SIGNAL('clicked()'), self, SLOT('accept()'))

  update_crc_button = Qt::PushButton.new('Update Project CRCs')
  update_crc_button.connect(SIGNAL('clicked()')) do
    Cosmos.set_working_dir do
      output, status = Open3.capture2e("rake crc")
      project_error_count = check_all_crcs()
      if status.success?
        Qt::MessageBox.information(self, 'Success', 'Project CRCs updated successfully')
      else
        Qt::MessageBox.critical(self, 'Error', "Project CRCs update failed.\n\n#{output}")
      end
    end
  end

  cancel_button = Qt::PushButton.new('Cancel')
  connect(cancel_button, SIGNAL('clicked()'), self, SLOT('reject()'))

  hlayout = Qt::HBoxLayout.new
  hlayout.addWidget(ok_button, 0, Qt::AlignLeft)
  hlayout.addWidget(update_crc_button, 0, Qt::AlignCenter) if update_crc_button
  hlayout.addWidget(cancel_button, 0, Qt::AlignRight)
  layout.addLayout(hlayout)
  
  self.show()
  self.raise()
  result = exec()
  dispose()
  exit if result != Qt::Dialog::Accepted
end

Instance Method Details

#check_all_crcsObject

initialize



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cosmos/gui/dialogs/legal_dialog.rb', line 92

def check_all_crcs
  # Check each file in crc.txt
  result_text = ''
  missing_text = ''
  core_file_count, core_error_count, _, _ = check_crcs(::Cosmos::PATH, File.join(::Cosmos::PATH,'data','crc.txt'), result_text, missing_text, 'CORE')
  project_file_count = 0
  project_error_count = 0
  official = true
  if File.exist?(File.join(::Cosmos::USERPATH,'config','data','crc.txt'))
    project_file_count, project_error_count, _, official = check_crcs(::Cosmos::USERPATH, File.join(::Cosmos::USERPATH,'config','data','crc.txt'), result_text, missing_text, 'PROJECT')
  end

  final_text = ''
  if core_error_count == 0 and project_error_count == 0
    if missing_text.empty? and official
      @text_crc.setTextColor(Cosmos::GREEN)
    else
      @text_crc.setTextColor(Cosmos::YELLOW)
    end
    result_text = "COSMOS Verified #{core_file_count} Core and #{project_file_count} Project CRCs\n"
    final_text = result_text + missing_text
  else
    @text_crc.setTextColor(Cosmos::YELLOW)
    if core_error_count > 0
      if project_error_count > 0
        final_text = "Warning: #{core_error_count} Core and #{project_error_count} Project CRC checks failed!\n" << result_text << missing_text
      else
        final_text = "Warning: #{core_error_count} Core CRC checks failed!\n" << result_text << missing_text
      end
    else # project_error_count > 0
      final_text = "Warning: #{project_error_count} Project CRC checks failed!\n" << result_text << missing_text
    end
  end
  final_text = "Warning: Project CRC file updated by user\n  Remove USER_MODIFIED from config/data/crc.txt to clear this warning\n" << final_text unless official
  @text_crc.text = final_text

  return project_error_count
end

#check_crcs(base_path, filename, result_text, missing_text, file_type) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cosmos/gui/dialogs/legal_dialog.rb', line 131

def check_crcs(base_path, filename, result_text, missing_text, file_type)
  file_count = 0
  error_count = 0
  missing_count = 0
  official = true
  crc = Crc32.new(Crc32::DEFAULT_POLY, Crc32::DEFAULT_SEED, true, false)
  File.open(filename, 'r') do |file|
    file.each_line do |line|
      split_line = line.strip.scan(ConfigParser::PARSING_REGEX)
      if split_line[0] == 'USER_MODIFIED'
        official = false
        next
      end
      filename = File.join(base_path, split_line[0].remove_quotes)
      if File.exist?(filename)
        expected_crc = Integer(split_line[1])
        file_data = nil
        File.open(filename, 'rb') do |crc_file|
          file_data = crc_file.read.gsub("\x0D\x0A", "\x0A")
        end
        file_crc = crc.calc(file_data)
        filename = filename.gsub(base_path + '/', '')
        if file_crc != expected_crc
          result_text << "#{file_type} #{filename}\n  CRC Expected: #{sprintf("0x%08X", expected_crc)}, CRC Calculated: #{sprintf("0x%08X", file_crc)}\n"
          error_count += 1
        end
        file_count += 1
      else
        missing_text << "#{file_type} #{filename} is missing\n"
        missing_count += 1
      end
    end
  end
  return file_count, error_count, missing_count, official
end