Module: Lich::Common::GUI::Utilities

Defined in:
documented/common/gui/utilities.rb

Class Method Summary collapse

Class Method Details

.create_button_css_provider(font_size: 12) ⇒ Gtk::CssProvider

Creates a CSS provider for buttons with a specified font size.

Examples:

Creating a button CSS provider

provider = Lich::Common::GUI::Utilities.create_button_css_provider(font_size: 14)

Parameters:

  • font_size (Integer) (defaults to: 12)

    The font size for the button text.

Returns:

  • (Gtk::CssProvider)

    The CSS provider for buttons.



11
12
13
14
15
# File 'documented/common/gui/utilities.rb', line 11

def self.create_button_css_provider(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.

Examples:

Creating a message dialog

dialog = Lich::Common::GUI::Utilities.create_message_dialog(parent: window, icon: my_icon)
dialog.call("Hello, World!")

Parameters:

  • parent (Gtk::Window, nil) (defaults to: nil)

    The parent window for the dialog.

  • icon (Gdk::Pixbuf, nil) (defaults to: nil)

    The icon to display in the dialog.

Returns:

  • (Proc)

    A lambda that takes a message and shows the dialog.



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.create_message_dialog(parent: nil, icon: nil)
  ->(message) {
    dialog = Gtk::MessageDialog.new(
      parent: parent,
      flags: :modal,
      type: :info,
      buttons: :ok,
      message: message
    )
    dialog.title = "Message"
    dialog.set_icon(icon) if icon
    dialog.run
    dialog.destroy
  }
end

.create_tab_css_providerGtk::CssProvider

Creates a CSS provider for tabs in a notebook.

Examples:

Creating a tab CSS provider

provider = Lich::Common::GUI::Utilities.create_tab_css_provider

Returns:

  • (Gtk::CssProvider)

    The CSS provider for tabs.



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.

Examples:

Converting a game code to realm

realm = Lich::Common::GUI::Utilities.game_code_to_realm("GS3")

Parameters:

  • game_code (String)

    The game code to convert.

Returns:

  • (String)

    The corresponding realm name or the original game code if not found.



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.

Examples:

Converting a realm to game code

code = Lich::Common::GUI::Utilities.realm_to_game_code("gemstone iv")

Parameters:

  • realm (String)

    The realm name to convert.

Returns:

  • (String)

    The corresponding game code or “GS3” if not found.



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.

Examples:

Reading a file safely

content = Lich::Common::GUI::Utilities.safe_file_operation("path/to/file.txt", :read")

Parameters:

  • file_path (String)

    The path to the file.

  • operation (Symbol)

    The operation to perform (:read, :write, :backup).

  • content (String, nil) (defaults to: nil)

    The content to write if the operation is :write.

Returns:

  • (String, true, false)

    The content read, true if write/backup succeeded, or false on failure.

Raises:

  • (StandardError)

    If an error occurs during the file operation.



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.message}"
  operation == :read ? "" : false
end

.sort_entries(entries, autosort_state) ⇒ Array<Hash>

Sorts an array of entries based on the autosort state.

Examples:

Sorting entries

sorted_entries = Lich::Common::GUI::Utilities.sort_entries(entries, true)

Parameters:

  • entries (Array<Hash>)

    The entries to sort, each containing game_name, user_id, and char_name.

  • autosort_state (Boolean)

    The state indicating whether to use autosorting.

Returns:

  • (Array<Hash>)

    The sorted entries.



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.

Examples:

Writing to a file with verification

success = Lich::Common::GUI::Utilities.verified_file_operation("path/to/file.txt", :write, "New content")

Parameters:

  • file_path (String)

    The path to the file.

  • operation (Symbol)

    The operation to perform (:read, :write, :backup).

  • content (String, nil) (defaults to: nil)

    The content to write if the operation is :write.

Returns:

  • (String, true, false)

    The content read, true if write/backup succeeded, or false on failure.

Raises:

  • (StandardError)

    If an error occurs during the file operation.



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.message}"
  operation == :read ? "" : false
end