Module: Lich::Common::GUI::Utilities
- Defined in:
- documented/common/gui/utilities.rb
Class Method Summary collapse
-
.create_button_css_provider(font_size: 12) ⇒ Gtk::CssProvider
Creates a CSS provider for buttons with a specified font size.
-
.create_message_dialog(parent: nil, icon: nil) ⇒ Proc
Creates a message dialog with a specified message and optional icon.
-
.create_tab_css_provider ⇒ Gtk::CssProvider
Creates a CSS provider for tabs in a notebook.
-
.game_code_to_realm(game_code) ⇒ String
Converts a game code to its corresponding realm name.
-
.realm_to_game_code(realm) ⇒ String
Converts a realm name to its corresponding game code.
-
.safe_file_operation(file_path, operation, content = nil) ⇒ String, ...
Performs a safe file operation (read, write, or backup) with error handling.
-
.sort_entries(entries, autosort_state) ⇒ Array<Hash>
Sorts an array of entries based on the autosort state.
-
.verified_file_operation(file_path, operation, content = nil) ⇒ String, ...
Performs a verified file operation (read, write, or backup) with error handling and verification.
Class Method Details
.create_button_css_provider(font_size: 12) ⇒ Gtk::CssProvider
Creates a CSS provider for buttons with a specified font size.
11 12 13 14 15 |
# File 'documented/common/gui/utilities.rb', line 11 def self.(font_size: 12) css = Gtk::CssProvider.new css.load_from_data("button {border-radius: 5px; font-size: #{font_size}px;}") css end |
.create_message_dialog(parent: nil, icon: nil) ⇒ Proc
Creates a message dialog with a specified message and optional icon.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'documented/common/gui/utilities.rb', line 34 def self.(parent: nil, icon: nil) ->() { dialog = Gtk::MessageDialog.new( parent: parent, flags: :modal, type: :info, buttons: :ok, message: ) dialog.title = "Message" dialog.set_icon(icon) if icon dialog.run dialog.destroy } end |
.create_tab_css_provider ⇒ Gtk::CssProvider
Creates a CSS provider for tabs in a notebook.
21 22 23 24 25 |
# File 'documented/common/gui/utilities.rb', line 21 def self.create_tab_css_provider css = Gtk::CssProvider.new css.load_from_data("notebook {border-width: 1px; border-color: #999999; border-style: solid;}") css end |
.game_code_to_realm(game_code) ⇒ String
Converts a game code to its corresponding realm name.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'documented/common/gui/utilities.rb', line 55 def self.game_code_to_realm(game_code) case game_code when "GS3" "GS Prime" when "GSF" "GS Shattered" when "GSX" "GS Platinum" when "GST" "GS Test" when "DR" "DR Prime" when "DRF" "DR Fallen" when "DRT" "DR Test" else game_code end end |
.realm_to_game_code(realm) ⇒ String
Converts a realm name to its corresponding game code.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'documented/common/gui/utilities.rb', line 81 def self.realm_to_game_code(realm) case realm.downcase when "gemstone iv", "prime" "GS3" when "gemstone iv shattered", "shattered" "GSF" when "gemstone iv platinum", "platinum" "GSX" when "gemstone iv prime test", "test" "GST" when "dragonrealms", "dr prime" "DR" when "dragonrealms the fallen", "dr fallen" "DRF" when "dragonrealms prime test", "dr test" "DRT" else "GS3" # Default to GS3 if unknown end end |
.safe_file_operation(file_path, operation, content = nil) ⇒ String, ...
Performs a safe file operation (read, write, or backup) with error handling.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'documented/common/gui/utilities.rb', line 110 def self.safe_file_operation(file_path, operation, content = nil) case operation when :read File.read(file_path) when :write # Create backup if file exists safe_file_operation(file_path, :backup) if File.exist?(file_path) # Write content to file with secure permissions File.open(file_path, 'w', 0600) do |file| file.write(content) end true when :backup return false unless File.exist?(file_path) backup_file = "#{file_path}.bak" FileUtils.cp(file_path, backup_file) true end rescue StandardError => e Lich.log "error: Error in file operation (#{operation}): #{e.}" operation == :read ? "" : false end |
.sort_entries(entries, autosort_state) ⇒ Array<Hash>
Sorts an array of entries based on the autosort state.
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'documented/common/gui/utilities.rb', line 181 def self.sort_entries(entries, autosort_state) if autosort_state # Sort by game name, account name, and character name entries.sort do |a, b| [a[:game_name], a[:user_id], a[:char_name]] <=> [b[:game_name], b[:user_id], b[:char_name]] end else # Sort by account name and character name (old Lich 4 style) entries.sort do |a, b| [a[:user_id].downcase, a[:char_name]] <=> [b[:user_id].downcase, b[:char_name]] end end end |
.verified_file_operation(file_path, operation, content = nil) ⇒ String, ...
Performs a verified file operation (read, write, or backup) with error handling and verification.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'documented/common/gui/utilities.rb', line 143 def self.verified_file_operation(file_path, operation, content = nil) case operation when :read File.read(file_path) when :write # Create backup if file exists safe_file_operation(file_path, :backup) if File.exist?(file_path) # Write content with forced synchronization and secure permissions File.open(file_path, 'w', 0600) do |file| file.write(content) file.flush # Force write to OS buffer file.fsync # Force OS to write to disk end # Verify write completed by reading back and comparing written_content = File.read(file_path) return written_content == content when :backup return false unless File.exist?(file_path) backup_file = "#{file_path}.bak" FileUtils.cp(file_path, backup_file) # Verify backup was created successfully File.exist?(backup_file) && File.size(backup_file) == File.size(file_path) end rescue StandardError => e Lich.log "error: Error in verified file operation (#{operation}): #{e.}" operation == :read ? "" : false end |